Skip to content

Instantly share code, notes, and snippets.

@Higgs1
Last active August 29, 2015 14:09
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 Higgs1/8c51cd24d8c3c1cd7b7a to your computer and use it in GitHub Desktop.
Save Higgs1/8c51cd24d8c3c1cd7b7a to your computer and use it in GitHub Desktop.
Ascii85 codecs for Javascript
/* This decoder supports both space and null byte folding,
and completely ignores all invalid characters (whitespace, misplaced x's and z's, etc).
Does not handle Adobe's <~ ~> frame.
Licensed under CC0. */
function a85decode(ascii85) {
for (var a = String, b = a.fromCharCode, c = ascii85.length, d = "", e = 0, f = 0, g = 0, h, i; i = a.charCodeAt(ascii85, e++) - 33, e <= c;
0 <= i && 85 > i ? (g = 85 * g + i, 4 < ++f && (h = b(g & 255), h = b((g >>>= 8) & 255) + h, h = b((g >>= 8) & 255) + h, d += b(g >> 8) + h, f = g = 0)) : f || (d += 88 == i ? " " : 89 == i ? "\0\0\0\0" : ""));
if (!f) return d;
for (e = 5 - f; g = 85 * g + 84, --e;);
return h = b(g & 255), h = b((g >>>= 8) & 255) + h, h = b((g >>= 8) & 255) + h, (d + b(g >> 8) + h).substring(0, d.length +-- f);
}
/*This encoder doesn't support space nor null byte folding.
This is equivalent to base64.a85encode(s).replace(b'z', b'!!!!!') in python.
This is compatible with Git's implementation.
Licensed under CC0. */
function a85encode(bytes) {
for (var a = String, b = a.charCodeAt, c = bytes.length, d = "", e = 0, f = 0; e = (b(bytes, f) << 24) + (++f < c && (b(bytes, f) << 16) + (++f < c && (b(bytes, f) << 8) + (++f < c && b(bytes, f++)))), f <= c; d += g)
for (var g = "", h = 6; --h; g = a.fromCharCode(e % 85 + 33) + g, e = Math.floor(e / 85));
return d.substring(0, d.length - (4 - c % 4) % 4);
}
/* This encoder supports both space and null byte folding and is thus the most compact.
This is equivalent to base64.a85encode(s, foldspaces=True) in python.
Adobe's implementation does not support space folding.
(" " -> "y", "\0\0\0\0" -> "z")
Licensed under CC0. */
function a85encode(bytes) {
for (var a = String, b = a.charCodeAt, c = bytes.length, d = "", e = 0, f = 0, g = (4 - c % 4) % 4; e = (b(bytes, f) << 24) + (++f < c && (b(bytes, f) << 16) + (++f < c && (b(bytes, f) << 8) + (++f < c && b(bytes, f++)))), f <= c; d += h)
if (e || f > c - g)
if (538976288 == e)
h = "y";
else
for (var h = "", i = 6; --i; h = a.fromCharCode(e % 85 + 33) + h, e = Math.floor(e / 85));
else
h = "z";
return !h.length - 1 ? d.substring(0, d.length - g) : d;
};
/*This encoder supports null byte folding, but not space folding.
This is equivalent to base64.a85encode(s) in python.
This is compatible with Adobe's implementation. (Just add <~ ~>)
("\0\0\0\0" -> "z")
Licensed under CC0. */
function a85encode(bytes) {
for (var a = String, b = a.charCodeAt, c = bytes.length, d = "", e = 0, f = 0, g = (4 - c % 4) % 4; e = (b(bytes, f) << 24) + (++f < c && (b(bytes, f) << 16) + (++f < c && (b(bytes, f) << 8) + (++f < c && b(bytes, f++)))), f <= c; d += h)
if (e || f > c - g)
for (var h = "", i = 6; --i; h = a.fromCharCode(e % 85 + 33) + h, e = Math.floor(e / 85));
else
h = "z";
return "z" != h ? d.substring(0, d.length - g) : d;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment