byte-encodings
Version:
Utilities for encoding and decoding common formats like hex, base64, and varint. Ported from Deno's @std/encoding.
2 lines (1 loc) • 2.91 kB
JavaScript
var m={base32:new TextEncoder().encode("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"),base32hex:new TextEncoder().encode("0123456789ABCDEFGHIJKLMNOPQRSTUV"),base32crockford:new TextEncoder().encode("0123456789ABCDEFGHJKMNPQRSTVWXYZ")},w={base32:new Uint8Array(128).fill(32),base32hex:new Uint8Array(128).fill(32),base32crockford:new Uint8Array(128).fill(32)};m.base32.forEach((n,s)=>w.base32[n]=s);m.base32hex.forEach((n,s)=>w.base32hex[n]=s);m.base32crockford.forEach((n,s)=>w.base32crockford[n]=s);function k(n){return((n+4)/5|0)*8}function o(n,s,x,t,y){for(s+=4;s<n.length;s+=5){let c=n[s-4]<<16|n[s-3]<<8|n[s-2];n[x++]=t[c>>19],n[x++]=t[c>>14&31],n[x++]=t[c>>9&31],n[x++]=t[c>>4&31],c=c<<16|n[s-1]<<8|n[s],n[x++]=t[c>>15&31],n[x++]=t[c>>10&31],n[x++]=t[c>>5&31],n[x++]=t[c&31]}switch(s){case n.length+3:{let c=n[s-4]<<16;n[x++]=t[c>>19],n[x++]=t[c>>14&31],n[x++]=y,n[x++]=y,n[x++]=y,n[x++]=y,n[x++]=y,n[x++]=y;break}case n.length+2:{let c=n[s-4]<<16|n[s-3]<<8;n[x++]=t[c>>19],n[x++]=t[c>>14&31],n[x++]=t[c>>9&31],n[x++]=t[c>>4&31],n[x++]=y,n[x++]=y,n[x++]=y,n[x++]=y;break}case n.length+1:{let c=n[s-4]<<16|n[s-3]<<8|n[s-2];n[x++]=t[c>>19],n[x++]=t[c>>14&31],n[x++]=t[c>>9&31],n[x++]=t[c>>4&31],c<<=16,n[x++]=t[c>>15&31],n[x++]=y,n[x++]=y,n[x++]=y;break}case n.length:{let c=n[s-4]<<16|n[s-3]<<8|n[s-2];n[x++]=t[c>>19],n[x++]=t[c>>14&31],n[x++]=t[c>>9&31],n[x++]=t[c>>4&31],c=c<<16|n[s-1]<<8,n[x++]=t[c>>15&31],n[x++]=t[c>>10&31],n[x++]=t[c>>5&31],n[x++]=y;break}}return x}function U(n,s,x,t,y){for(let c=n.length-6;c<n.length;++c)if(n[c]===y){for(let A=c+1;A<n.length;++A)if(n[A]!==y)throw new TypeError(`Cannot decode input as base32: Invalid character (${String.fromCharCode(n[A])})`);n=n.subarray(0,c);break}switch((n.length-x)%8){case 6:case 3:case 1:throw new RangeError(`Cannot decode input as base32: Length (${n.length-x}), excluding padding, must not have a remainder of 1, 3, or 6 when divided by 8`)}for(s+=7;s<n.length;s+=8){let c=F(n[s-7],t)<<19|F(n[s-6],t)<<14|F(n[s-5],t)<<9|F(n[s-4],t)<<4;n[x++]=c>>16,n[x++]=c>>8&255,c=c<<16|F(n[s-3],t)<<15|F(n[s-2],t)<<10|F(n[s-1],t)<<5|F(n[s],t),n[x++]=c>>16&255,n[x++]=c>>8&255,n[x++]=c&255}switch(s){case n.length+5:{let c=F(n[s-7],t)<<19|F(n[s-6],t)<<14;n[x++]=c>>16;break}case n.length+3:{let c=F(n[s-7],t)<<19|F(n[s-6],t)<<14|F(n[s-5],t)<<9|F(n[s-4],t)<<4;n[x++]=c>>16,n[x++]=c>>8&255;break}case n.length+2:{let c=F(n[s-7],t)<<19|F(n[s-6],t)<<14|F(n[s-5],t)<<9|F(n[s-4],t)<<4;n[x++]=c>>16,n[x++]=c>>8&255,c=c<<16|F(n[s-3],t)<<15,n[x++]=c>>16&255;break}case n.length:{let c=F(n[s-7],t)<<19|F(n[s-6],t)<<14|F(n[s-5],t)<<9|F(n[s-4],t)<<4;n[x++]=c>>16,n[x++]=c>>8&255,c=c<<16|F(n[s-3],t)<<15|F(n[s-2],t)<<10|F(n[s-1],t)<<5,n[x++]=c>>16&255,n[x++]=c>>8&255;break}}return x}function F(n,s){let x=s[n]??32;if(x===32)throw new TypeError(`Cannot decode input as base32: Invalid character (${String.fromCharCode(n)})`);return x}export{m as a,w as b,k as c,o as d,U as e};