Skip to content

Instantly share code, notes, and snippets.

@carlopi
Created March 5, 2022 07:16
Show Gist options
  • Save carlopi/0b78c79218b6a900ea7c01688bc65475 to your computer and use it in GitHub Desktop.
Save carlopi/0b78c79218b6a900ea7c01688bc65475 to your computer and use it in GitHub Desktop.
atob / btoa Cheerp optimized implementation
"use strict";/*Compiled using Cheerp (R) by Leaning Technologies Ltd*/var H=Math.imul;var I=Math.fround;var oSlot=0;var nullArray=[null];var nullObj={d:nullArray,o:0};function y(){r=new TextDecoder();}function E(G){var h=0,g=null,f=0,e=0,b=0,a=0,d=0,c=null,j=0;h=G.length;if(h>>>0<100){g=w();if((h|0)===0)return g;f=0;while(1){e=G.charCodeAt(f);if(e>>>0>255)return null;b=f+1|0;a=e<<4&48;if(h>>>0>b>>>0){b=G.charCodeAt(b);if(b>>>0>255)return null;a|=(b>>>4);b=b<<2&60;}else{b=64;}d=f+2|0;if(h>>>0>d>>>0){d=G.charCodeAt(d);if(d>>>0>255)return null;b|=(d>>>6);d&=63;}else{d=64;}c=String.fromCharCode(o[e>>>2]<<24>>24);g=g.concat(c);if((a|0)===64){a=61;}else{a=o[a]|0;}c=String.fromCharCode(a<<24>>24);g=g.concat(c);if((b|0)===64){b=61;}else{b=o[b]|0;}c=String.fromCharCode(b<<24>>24);g=g.concat(c);if((d|0)===64){e=61;}else{e=o[d]|0;}c=String.fromCharCode(e<<24>>24);g=g.concat(c);f=f+3|0;if(f>>>0<h>>>0)continue;break;}return g;}else{g=new Uint8Array((+(((h<<2)+20>>>0)/3>>>0)));e=0;f=0;while(1){b=G.charCodeAt(f);if(b>>>0>255)return null;a=f+1|0;d=b<<4&48;if(h>>>0>a>>>0){a=G.charCodeAt(a);if(a>>>0>255)return null;d|=(a>>>4);a=a<<2&60;}else{a=64;}j=f+2|0;if(h>>>0>j>>>0){j=G.charCodeAt(j);if(j>>>0>255)return null;a|=(j>>>6);j&=63;}else{j=64;}g[0+e|0]=o[b>>>2]|0;if((d|0)===64){d=61;}else{d=o[d]|0;}g[0+(e+1|0)|0]=d;if((a|0)===64){a=61;}else{a=o[a]|0;}g[0+(e+2|0)|0]=a;if((j|0)===64){b=61;}else{b=o[j]|0;}g[0+(e+3|0)|0]=b;f=f+3|0;e=e+4|0;if(f>>>0<h>>>0)continue;break;}g[0+e|0]=0;c=r;return c.decode(new Uint8Array(g.buffer,0,(+(e>>>0))));}}function w(){return String(String());}function D(G){var h=0,a=null,e=0,g=0,f=0,d=0,b=0,c=null,j=0;h=G.length;if(h>>>0<100){a=w();if((h|0)===0)return a;d=0;f=0;g=0;e=0;while(1){b=G.charCodeAt(d);b=i[b<<24>>24]|0;if(b<<24>1056964608){if(b===64)return null;}else{e=e<<6|b<<24>>24;g=g+1|0;f=f+1|0;if((g|0)===4){c=String.fromCharCode(e<<8>>24);a=a.concat(c);c=String.fromCharCode(e<<16>>24);a=a.concat(c);c=String.fromCharCode(e<<24>>24);a=a.concat(c);e=0;g=0;}}d=d+1|0;if((d|0)!==(h|0))continue;break;}if((f&3|0)===1)return null;if((g|0)===2){h=e>>>4;}else{if((g|0)!==3)return a;c=String.fromCharCode(e<<14>>24);a=a.concat(c);h=e<<22>>24;}c=String.fromCharCode(h);a=a.concat(c);return a;}else{a=new Uint8Array((+(h>>>0)));b=0;d=0;f=0;g=0;e=0;while(1){j=G.charCodeAt(e);j=i[j<<24>>24]|0;if(j<<24>1056964608){if(j===64)return null;}else{d=d<<6|j<<24>>24;f=f+1|0;g=g+1|0;if((f|0)===4){a[0+b|0]=d>>>16;a[0+(b+1|0)|0]=d>>>8;a[0+(b+2|0)|0]=d;b=b+3|0;f=0;d=0;}}e=e+1|0;if((e|0)!==(h|0))continue;break;}if((g&3|0)===1)return null;if((f|0)===2){a[0+b|0]=d>>>4;b=b+1|0;}else if((f|0)===3){a[0+b|0]=d>>>10;a[0+(b+1|0)|0]=d>>>2;b=b+2|0;}a[0+b|0]=0;c=r;return c.decode(new Uint8Array(a.buffer,0,(+(b>>>0))));}}function z(){var b=0,a=0;a=0;while(1){i[a]=64;a=a+1|0;if((a|0)!==128)continue;break;}i[9]=65;i[10]=65;i[12]=65;i[13]=65;i[61]=65;a=0;while(1){b=o[a]|0;i[b<<24>>24]=a;a=a+1|0;if((a|0)!==64)continue;break;}a=0;while(1){String.fromCharCode(a);a=a+1|0;if((a|0)!==128)continue;break;}}var r=null;var i=new Uint8Array(128);var o=new Uint8Array([65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,48,49,50,51,52,53,54,55,56,57,43,47,0]);var cheerp={atob:null,btoa:null,};cheerp.atob=D;cheerp.btoa=E;cheerp.atob.promise=cheerp.btoa.promise=Promise.resolve();y();z();
//Example usage
console.log(cheerp.atob(cheerp.btoa("12312323")));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment