mayan-Dby7Nm8-.js

8.2 KB · raw

1var e=256,t=285,n=178,r=77,i=n+r,a=new Uint8Array(e*2),o=new Uint8Array(e);(function(){let e=1;for(let n=0;n<255;n++)a[n]=e,o[e]=n,e<<=1,e&256&&(e^=t);for(let e=255;e<512;e++)a[e]=a[e-255]})();function s(e,t){return e===0||t===0?0:a[o[e]+o[t]]}function c(e,t){if(t===0)throw Error(`rs: divide by zero`);return e===0?0:a[(o[e]+255-o[t])%255]}function l(e,t){return e===0?0:a[o[e]*t%255]}function u(e){if(e===0)throw Error(`rs: inverse of zero`);return a[(255-o[e])%255]}function d(e,t){let n=new Uint8Array(e.length);for(let r=0;r<e.length;r++)n[r]=s(e[r],t);return n}function f(e,t){let n=Math.max(e.length,t.length),r=new Uint8Array(n);for(let t=0;t<e.length;t++)r[t+n-e.length]=e[t];for(let e=0;e<t.length;e++)r[e+n-t.length]^=t[e];return r}function p(e,t){let n=new Uint8Array(e.length+t.length-1);for(let r=0;r<t.length;r++)if(t[r]!==0)for(let i=0;i<e.length;i++)e[i]!==0&&(n[i+r]^=s(e[i],t[r]));return n}function m(e,t){let n=e[0];for(let r=1;r<e.length;r++)n=s(n,t)^e[r];return n}function h(e){let t=new Uint8Array([1]);for(let n=0;n<e;n++)t=p(t,new Uint8Array([1,a[n]]));return t}var g=new Map;function _(e){let t=g.get(e);return t||(t=h(e),g.set(e,t)),t}function v(e,t){let n=_(t),r=new Uint8Array(e.length+t);r.set(e);for(let t=0;t<e.length;t++){let e=r[t];if(e!==0)for(let i=1;i<n.length;i++)r[t+i]^=s(n[i],e)}return r.set(e),r}function y(e,t){let n=new Uint8Array(t+1);n[0]=0;for(let r=0;r<t;r++)n[r+1]=m(e,a[r]);return n}function b(e,t){let n=new Uint8Array([1]),r=new Uint8Array([1]);for(let i=0;i<t;i++){r=new Uint8Array([...r,0]);let t=e[i+1];for(let r=1;r<n.length;r++)t^=s(n[n.length-1-r],e[i+1-r]);if(t!==0){if(r.length>n.length){let e=d(r,t);r=d(n,u(t)),n=e}n=f(n,d(r,t))}}let i=0;for(;i<n.length&&n[i]===0;)i++;return n=n.slice(i),(n.length-1)*2>t?null:n}function x(e,t){let n=e.length-1,r=[];for(let n=0;n<255;n++)if(m(e,l(2,n))===0){let e=(n-1+255)%255;e<t&&r.push(e)}return r.length===n?r:null}function S(e,t,n){let r=n.map(t=>e.length-1-t),i=(()=>{let e=new Uint8Array([1]);for(let t of r)e=p(e,f(new Uint8Array([1]),new Uint8Array([l(2,t),0])));return e})(),a=new Uint8Array(t.length-1);for(let e=0;e<a.length;e++)a[e]=t[t.length-1-e];let o=p(a,i);o=o.slice(o.length-i.length+1);let d=new Uint8Array(i.length-1);for(let e=0;e<d.length;e++)(i.length-1-e)%2==1&&(d[e]=i[e]);let h=new Uint8Array(e);for(let t=0;t<r.length;t++){let n=r[t],i=l(2,n),a=u(i),f=m(o,a),p=m(d,a);if(p===0)throw Error(`rs: denominator zero in Forney`);let g=s(i,c(f,p));h[e.length-1-n]^=g}return h}function C(e,t){let n=y(e,t),r=!1;for(let e=1;e<n.length;e++)if(n[e]!==0){r=!0;break}if(!r)return e.slice(0,e.length-t);let i=b(n,t);if(!i)return null;let a=x(i,e.length);if(!a)return null;let o;try{o=S(e,n,a)}catch{return null}let s=y(o,t);for(let e=1;e<s.length;e++)if(s[e]!==0)return null;return o.slice(0,e.length-t)}function w(e,t){if(e.length===0)return new Uint8Array;let a=Math.ceil(e.length/n),o=new Uint8Array(a*i);for(let t=0;t<a;t++){let a=t*n,s=e.slice(a,a+n),c=v(s.length===n?s:(()=>{let e=new Uint8Array(n);return e.set(s),e})(),r);o.set(c,t*i)}return o}function T(e,t,a){if(t===0)return new Uint8Array;if(e.length%i!==0)throw Error(`rs: received length ${e.length} not a multiple of ${i}`);let o=e.length/i,s=new Uint8Array(o*n);for(let t=0;t<o;t++){let a=C(e.slice(t*i,(t+1)*i),r);if(!a)throw Error(`rs: block ${t} uncorrectable`);s.set(a,t*n)}return s.slice(0,t)}var E=i,D=65535;function O(e){let t=e+4,n=Math.max(1,Math.ceil(t/178))*E*8;for(let e=64;e<=600;e++){let t=e*e,r=4*e*2-16,i=e>128?k(e):0;if(t-729-r-i>=n)return e}return 600}function k(e){return Math.floor((e-4)/32)*(e-4)*2}function A(e,t,n,r,i){for(let a=0;a<r;a++)for(let r=0;r<i;r++){let i=t+a,o=n+r;i>=0&&i<e.length&&o>=0&&o<e[0].length&&(e[i][o]=!0)}}function j(e,t,n,r,i,a,o,s){let c=Math.max(Math.abs(a-r),Math.abs(o-i)),l=Math.floor(s/2);for(let u=0;u<=c;u++){let d=Math.round(r+(a-r)*u/c),f=Math.round(i+(o-i)*u/c);A(e,t+d-l,n+f-l,s,s)}}function M(e,t,n){let r=t-11,i=n-11,a=[1,11],o=[10,21],s=[19,11],c=[10,1];j(e,r,i,a[0],a[1],o[0],o[1],3),j(e,r,i,o[0],o[1],s[0],s[1],3),j(e,r,i,s[0],s[1],c[0],c[1],3),j(e,r,i,c[0],c[1],a[0],a[1],3),j(e,r,i,19,8,22,4,3),j(e,r,i,19,14,22,18,3),A(e,r+0,i+20,3,3)}function N(e,t,n,r){return e<2||e>=r-2||t<2||t>=r-2?!0:e>=n-13&&e<=n+13&&t>=n-13&&t<=n+13||r>128&&(e>=2&&e<r-2&&(e-2)%32==0&&e!==2||t>=2&&t<r-2&&(t-2)%32==0&&t!==2)}function P(e,t=3){if(e.length>65535)throw Error(`Mayan payload too large: ${e.length} bytes (max ${D})`);let n=O(e.length),r=Math.floor(n/2),i=new Uint8Array(4);i[0]=77,i[1]=1,i[2]=e.length&255,i[3]=e.length>>8&255;let a=new Uint8Array(i.length+e.length);a.set(i),a.set(e,i.length);let o=w(a),s=[];for(let e of o)for(let t=7;t>=0;t--)s.push((e>>t&1)==1);let c=Array.from({length:n},()=>Array(n).fill(!1));if(n>128)for(let e=2;e<n-2;e++)for(let t=34;t<n-2;t+=32){let n=e%2==0;c[t][e]=n,c[e][t]=n}M(c,r,r);let l=0;for(let e=2;e<n-2&&l<s.length;e++)for(let t=2;t<n-2&&l<s.length;t++)N(e,t,r,n)||(c[e][t]=s[l++]);let u=F(c,n,t),d=n*.6<=210;return{canvas:u,symbolSize:n,payloadBytes:e.length,printable:d}}function F(e,t,n){let r=document.createElement(`canvas`);r.width=t*n,r.height=t*n;let i=r.getContext(`2d`);if(!i)throw Error(`Mayan: 2D canvas unavailable`);i.fillStyle=`#ffffff`,i.fillRect(0,0,r.width,r.height),i.fillStyle=`#000000`;for(let r=0;r<t;r++)for(let a=0;a<t;a++)e[r][a]&&i.fillRect(a*n,r*n,n,n);return r}function I(e){let t=R(e),n=e.width,r=e.height,i=z(t,n,r);if(!i)return null;let a=B(t,i,n);if(!a||a<1)return null;let o=Math.round(Math.min(n,r)/a);if(o<64)return null;let s=V(t,i,a);return L(H(t,{x:i.cx,y:i.cy},a,o,s),o)}function L(e,t){let n=Math.floor(t/2),r=U(e,n,t,32);if(r.length<32)return null;let i=W(r);if(i[0]!==77||i[1]!==1)return null;let a=i[2]|i[3]<<8;if(a>65535)return null;let o=4+a,s=Math.ceil(o/178)*255*8,c=U(e,n,t,s);if(c.length<s)return null;let l=new Uint8Array(W(c)),u;try{u=T(l,o)}catch{return null}return u.slice(4,4+a)}function R(e){let{data:t,width:n,height:r}=e,i=Array.from({length:r},()=>Array(n).fill(!1)),a=new Float64Array((n+1)*(r+1));for(let e=0;e<r;e++){let r=0;for(let i=0;i<n;i++){let o=(e*n+i)*4,s=.299*t[o]+.587*t[o+1]+.114*t[o+2];r+=s,a[(e+1)*(n+1)+(i+1)]=a[e*(n+1)+(i+1)]+r}}for(let e=0;e<r;e++)for(let o=0;o<n;o++){let s=(e*n+o)*4,c=.299*t[s]+.587*t[s+1]+.114*t[s+2],l=Math.max(0,o-9),u=Math.max(0,e-9),d=Math.min(n-1,o+9),f=Math.min(r-1,e+9),p=a[(f+1)*(n+1)+(d+1)]-a[u*(n+1)+(d+1)]-a[(f+1)*(n+1)+l]+a[u*(n+1)+l],m=(d-l+1)*(f-u+1);i[e][o]=c<p/m-8}return i}function z(e,t,n){let r=[],i=Math.floor(n*.2),a=Math.floor(n*.8);for(let n=i;n<a;n+=2){let i=0;for(;i<t;){for(;i<t&&!e[n][i];)i++;if(i>=t)break;let a=i;for(;i<t&&e[n][i];)i++;let o=i-a,s=i;for(;i<t&&!e[n][i];)i++;let c=i-s,l=i;for(;i<t&&e[n][i];)i++;let u=i-l;if(o>=2&&u>=2&&Math.abs(o-u)<=o*.5&&c>o*1.5){let e=Math.floor((a+i)/2),t=i-a;r.push({cx:e,cy:n,size:t})}}}if(r.length<3)return null;let o=r.reduce((e,t)=>e.size>t.size?e:t),s=o.cy;for(;s>0&&e[s][o.cx];)s--;let c=o.cy;for(;c<n-1&&(e[c][o.cx]||o.cx>0&&e[c][o.cx-1]||o.cx<t-1&&e[c][o.cx+1]);)c++;let l=Math.floor((s+c)/2);return{cx:o.cx,cy:l,size:o.size}}function B(e,t,n){let r=t.cy,i=[];for(let a=Math.max(0,t.cx-t.size);a<t.cx+t.size&&!(a>=n);a++)if(e[r][a]&&(a===0||!e[r][a-1])){let o=0,s=a;for(;s<n&&e[r][s];)o++,s++;o>1&&o<t.size/2&&i.push(o)}return i.length===0?t.size/23:i.reduce((e,t)=>e+t,0)/i.length/3}function V(e,t,n){let r=[{angle:0,dr:-10,dc:10},{angle:90,dr:10,dc:10},{angle:180,dr:10,dc:-10},{angle:270,dr:-10,dc:-10}],i=0,a=-1;for(let{angle:o,dr:s,dc:c}of r){let r=Math.round(t.cy+s*n),l=Math.round(t.cx+c*n),u=0;for(let t=-1;t<=1;t++)for(let i=-1;i<=1;i++){let a=r+Math.round(t*n),o=l+Math.round(i*n);a>=0&&a<e.length&&o>=0&&o<e[0].length&&e[a][o]&&u++}u>a&&(a=u,i=o)}return i}function H(e,t,n,r,i){let a=Math.floor(r/2),o=Array.from({length:r},()=>Array(r).fill(!1));for(let s=0;s<r;s++)for(let c=0;c<r;c++){let r=s-a,l=c-a;i===90&&([r,l]=[l,-r]),i===180&&([r,l]=[-r,-l]),i===270&&([r,l]=[-l,r]);let u=Math.round(t.x+l*n),d=Math.round(t.y+r*n);d>=0&&d<e.length&&u>=0&&u<e[0].length&&(o[s][c]=e[d][u])}return o}function U(e,t,n,r){let i=[];for(let a=2;a<n-2&&i.length<r;a++)for(let o=2;o<n-2&&i.length<r;o++)N(a,o,t,n)||i.push(e[a][o]);return i}function W(e){let t=[];for(let n=0;n<e.length;n+=8){let r=0;for(let t=0;t<8&&n+t<e.length;t++)e[n+t]&&(r|=1<<7-t);t.push(r)}return t}export{D as MAX_PAYLOAD_BYTES,I as decodeMayan,P as encodeMayan};