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) • 4.36 kB
JavaScript
var F=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var B=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var i=(n,t)=>{for(var s in t)F(n,s,{get:t[s],enumerable:!0})},E=(n,t,s,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let c of B(t))!g.call(n,c)&&c!==s&&F(n,c,{get:()=>t[c],enumerable:!(r=_(t,c))||r.enumerable});return n};var k=n=>E(F({},"__esModule",{value:!0}),n);var l={};i(l,{calcSizeBase32:()=>A,decodeBase32:()=>h,encodeBase32:()=>C,encodeIntoBase32:()=>T});module.exports=k(l);var y={base32:new TextEncoder().encode("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"),base32hex:new TextEncoder().encode("0123456789ABCDEFGHIJKLMNOPQRSTUV"),base32crockford:new TextEncoder().encode("0123456789ABCDEFGHJKMNPQRSTVWXYZ")},a={base32:new Uint8Array(128).fill(32),base32hex:new Uint8Array(128).fill(32),base32crockford:new Uint8Array(128).fill(32)};y.base32.forEach((n,t)=>a.base32[n]=t);y.base32hex.forEach((n,t)=>a.base32hex[n]=t);y.base32crockford.forEach((n,t)=>a.base32crockford[n]=t);function A(n){return((n+4)/5|0)*8}function U(n,t,s,r,c){for(t+=4;t<n.length;t+=5){let e=n[t-4]<<16|n[t-3]<<8|n[t-2];n[s++]=r[e>>19],n[s++]=r[e>>14&31],n[s++]=r[e>>9&31],n[s++]=r[e>>4&31],e=e<<16|n[t-1]<<8|n[t],n[s++]=r[e>>15&31],n[s++]=r[e>>10&31],n[s++]=r[e>>5&31],n[s++]=r[e&31]}switch(t){case n.length+3:{let e=n[t-4]<<16;n[s++]=r[e>>19],n[s++]=r[e>>14&31],n[s++]=c,n[s++]=c,n[s++]=c,n[s++]=c,n[s++]=c,n[s++]=c;break}case n.length+2:{let e=n[t-4]<<16|n[t-3]<<8;n[s++]=r[e>>19],n[s++]=r[e>>14&31],n[s++]=r[e>>9&31],n[s++]=r[e>>4&31],n[s++]=c,n[s++]=c,n[s++]=c,n[s++]=c;break}case n.length+1:{let e=n[t-4]<<16|n[t-3]<<8|n[t-2];n[s++]=r[e>>19],n[s++]=r[e>>14&31],n[s++]=r[e>>9&31],n[s++]=r[e>>4&31],e<<=16,n[s++]=r[e>>15&31],n[s++]=c,n[s++]=c,n[s++]=c;break}case n.length:{let e=n[t-4]<<16|n[t-3]<<8|n[t-2];n[s++]=r[e>>19],n[s++]=r[e>>14&31],n[s++]=r[e>>9&31],n[s++]=r[e>>4&31],e=e<<16|n[t-1]<<8,n[s++]=r[e>>15&31],n[s++]=r[e>>10&31],n[s++]=r[e>>5&31],n[s++]=c;break}}return s}function m(n,t,s,r,c){for(let e=n.length-6;e<n.length;++e)if(n[e]===c){for(let o=e+1;o<n.length;++o)if(n[o]!==c)throw new TypeError(`Cannot decode input as base32: Invalid character (${String.fromCharCode(n[o])})`);n=n.subarray(0,e);break}switch((n.length-s)%8){case 6:case 3:case 1:throw new RangeError(`Cannot decode input as base32: Length (${n.length-s}), excluding padding, must not have a remainder of 1, 3, or 6 when divided by 8`)}for(t+=7;t<n.length;t+=8){let e=x(n[t-7],r)<<19|x(n[t-6],r)<<14|x(n[t-5],r)<<9|x(n[t-4],r)<<4;n[s++]=e>>16,n[s++]=e>>8&255,e=e<<16|x(n[t-3],r)<<15|x(n[t-2],r)<<10|x(n[t-1],r)<<5|x(n[t],r),n[s++]=e>>16&255,n[s++]=e>>8&255,n[s++]=e&255}switch(t){case n.length+5:{let e=x(n[t-7],r)<<19|x(n[t-6],r)<<14;n[s++]=e>>16;break}case n.length+3:{let e=x(n[t-7],r)<<19|x(n[t-6],r)<<14|x(n[t-5],r)<<9|x(n[t-4],r)<<4;n[s++]=e>>16,n[s++]=e>>8&255;break}case n.length+2:{let e=x(n[t-7],r)<<19|x(n[t-6],r)<<14|x(n[t-5],r)<<9|x(n[t-4],r)<<4;n[s++]=e>>16,n[s++]=e>>8&255,e=e<<16|x(n[t-3],r)<<15,n[s++]=e>>16&255;break}case n.length:{let e=x(n[t-7],r)<<19|x(n[t-6],r)<<14|x(n[t-5],r)<<9|x(n[t-4],r)<<4;n[s++]=e>>16,n[s++]=e>>8&255,e=e<<16|x(n[t-3],r)<<15|x(n[t-2],r)<<10|x(n[t-1],r)<<5,n[s++]=e>>16&255,n[s++]=e>>8&255;break}}return s}function x(n,t){let s=t[n]??32;if(s===32)throw new TypeError(`Cannot decode input as base32: Invalid character (${String.fromCharCode(n)})`);return s}function w(n,t){let s=n.length;if(n.byteOffset){let r=new Uint8Array(n.buffer);r.set(n),n=r.subarray(0,s)}return n=new Uint8Array(n.buffer.transfer(t)),n.set(n.subarray(0,s),t-s),[n,t-s]}function C(n,t={}){t.alphabet??="base32",typeof n=="string"?n=new TextEncoder().encode(n):n instanceof ArrayBuffer&&(n=new Uint8Array(n));let[s,r]=w(n,A(n.length));return U(s,r,0,y[t.alphabet],61),new TextDecoder().decode(s)}function T(n,t,s={}){s.alphabet??="base32",typeof n=="string"?n=new TextEncoder().encode(n):n instanceof ArrayBuffer&&(n=new Uint8Array(n));let r=A(n.length);if(t.length<r)throw new RangeError("Cannot encode input as base32: Output too small");t=t.subarray(0,r);let c=r-n.length;return t.set(n,c),U(t,c,0,y[s.alphabet],61)}function h(n,t={}){return t.alphabet??="base32",typeof n=="string"&&(n=new TextEncoder().encode(n)),n.subarray(0,m(n,0,0,a[t.alphabet],61))}0&&(module.exports={calcSizeBase32,decodeBase32,encodeBase32,encodeIntoBase32});