Skip to content

Instantly share code, notes, and snippets.

@allenhwkim
Last active April 20, 2023 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allenhwkim/85b5ae97a1db34aa806b0ba62277d2d8 to your computer and use it in GitHub Desktop.
Save allenhwkim/85b5ae97a1db34aa806b0ba62277d2d8 to your computer and use it in GitHub Desktop.
pako string compression to/from base64 in a browser
import pako from 'pako';
// Usage:
// console.log(compress('hello'), decompress(compress('hello')));
export function compress(str) {
var unit8arr = pako.deflate(str);
return base64EncArr(unit8arr);
}
export function decompress(base64) {
var uint8arr = base64DecToArr(base64);
return pako.inflate(uint8arr, { to: 'string' });
}
// The following functions are expert from https://developer.mozilla.org/en-US/docs/Glossary/Base64
function b64ToUint6(nChr) {
return nChr > 64 && nChr < 91 ? nChr - 65
: nChr > 96 && nChr < 123 ? nChr - 71
: nChr > 47 && nChr < 58 ? nChr + 4
: nChr === 43 ? 62
: nChr === 47 ? 63 : 0;
}
function base64DecToArr(sBase64, nBlocksSize) {
const sB64Enc = sBase64.replace(/[^A-Za-z0-9+/]/g, "");
const nInLen = sB64Enc.length;
const nOutLen = nBlocksSize ?
Math.ceil(((nInLen * 3 + 1) >> 2) / nBlocksSize) * nBlocksSize : (nInLen * 3 + 1) >> 2;
const taBytes = new Uint8Array(nOutLen);
let nMod3, nMod4, nUint24 = 0, nOutIdx = 0;
for (let nInIdx = 0; nInIdx < nInLen; nInIdx++) {
nMod4 = nInIdx & 3;
nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << (6 * (3 - nMod4));
if (nMod4 === 3 || nInLen - nInIdx === 1) {
nMod3 = 0;
while (nMod3 < 3 && nOutIdx < nOutLen) {
taBytes[nOutIdx] = (nUint24 >>> ((16 >>> nMod3) & 24)) & 255;
nMod3++;
nOutIdx++;
}
nUint24 = 0;
}
}
return taBytes;
}
/* Base64 string to array encoding */
function uint6ToB64(nUint6) {
return nUint6 < 26 ? nUint6 + 65
: nUint6 < 52 ? nUint6 + 71
: nUint6 < 62 ? nUint6 - 4
: nUint6 === 62 ? 43
: nUint6 === 63 ? 47 : 65;
}
function base64EncArr(aBytes) {
let nMod3 = 2, sB64Enc = "";
const nLen = aBytes.length;
let nUint24 = 0;
for (let nIdx = 0; nIdx < nLen; nIdx++) {
nMod3 = nIdx % 3;
nUint24 |= aBytes[nIdx] << ((16 >>> nMod3) & 24);
if (nMod3 === 2 || aBytes.length - nIdx === 1) {
sB64Enc += String.fromCodePoint(
uint6ToB64((nUint24 >>> 18) & 63),
uint6ToB64((nUint24 >>> 12) & 63),
uint6ToB64((nUint24 >>> 6) & 63),
uint6ToB64(nUint24 & 63)
);
nUint24 = 0;
}
}
return (
sB64Enc.substring(0, sB64Enc.length - 2 + nMod3) +
(nMod3 === 2 ? "" : nMod3 === 1 ? "=" : "==")
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment