Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to encode/decode ascii85 in javascript

I was looking for an alternative to Base64 which works fine on Unicode characters. I have found ASCII85 which works great however I found no code or command doing that in JS.

I just found this link which does not work for international characters and does not include decode function.

Here there are online Decoder/Encoder.

Even found codes in C language doing that(I don't have enough JS data handling knowledge to convert).

And some codes that I don't know how to run.

I have heard that JQuery does support Base64 but it seems that it does not support Ascii85.

Does somebody know anything about Ascii85 in JS which might help?

thanks

like image 981
gerrnar Avatar asked Jun 19 '13 07:06

gerrnar


3 Answers

Pure JavaScript Ascii85 AKA Base85 encoding/decoding functions:

function encode_ascii85(a) {
  var b, c, d, e, f, g, h, i, j, k;
  for (!/[^\x00-\xFF]/.test(a), b = "\x00\x00\x00\x00".slice(a.length % 4 || 4), a += b, 
  c = [], d = 0, e = a.length; e > d; d += 4) f = (a.charCodeAt(d) << 24) + (a.charCodeAt(d + 1) << 16) + (a.charCodeAt(d + 2) << 8) + a.charCodeAt(d + 3), 
  0 !== f ? (k = f % 85, f = (f - k) / 85, j = f % 85, f = (f - j) / 85, i = f % 85, 
  f = (f - i) / 85, h = f % 85, f = (f - h) / 85, g = f % 85, c.push(g + 33, h + 33, i + 33, j + 33, k + 33)) :c.push(122);
  return function(a, b) {
    for (var c = b; c > 0; c--) a.pop();
  }(c, b.length), "<~" + String.fromCharCode.apply(String, c) + "~>";
}

function decode_ascii85(a) {
  var c, d, e, f, g, h = String, l = "length", w = 255, x = "charCodeAt", y = "slice", z = "replace";
  for ("<~" === a[y](0, 2) && "~>" === a[y](-2), a = a[y](2, -2)[z](/\s/g, "")[z]("z", "!!!!!"), 
  c = "uuuuu"[y](a[l] % 5 || 5), a += c, e = [], f = 0, g = a[l]; g > f; f += 5) d = 52200625 * (a[x](f) - 33) + 614125 * (a[x](f + 1) - 33) + 7225 * (a[x](f + 2) - 33) + 85 * (a[x](f + 3) - 33) + (a[x](f + 4) - 33), 
  e.push(w & d >> 24, w & d >> 16, w & d >> 8, w & d);
  return function(a, b) {
    for (var c = b; c > 0; c--) a.pop();
  }(e, c[l]), h.fromCharCode.apply(h, e);
}

var myString='This is a test!';
var encoded=encode_ascii85(myString);
var decoded=decode_ascii85(encoded);
document.write(decoded);
like image 133
Dave Brown Avatar answered Nov 07 '22 05:11

Dave Brown


using dojo.js framework :

Decode Example + source

Encode Example + source

ascii85 Encoding using

summary:

dojo.require("dojox.encoding.ascii85");
var dc = dojox.encoding
var buf = dc.ascii85.decode("")
var a85 = dc.ascii85.encode("");
like image 3
Muath Avatar answered Nov 07 '22 03:11

Muath


You can use my JavaScript ASCII85 (Base85) implementation which can work both with byte arrays and strings

like image 1
nE0sIghT Avatar answered Nov 07 '22 04:11

nE0sIghT