-
Notifications
You must be signed in to change notification settings - Fork 3
/
my_base45_2.js
61 lines (55 loc) · 1.7 KB
/
my_base45_2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function encode(uint8array) {
var output = [];
for (var i = 0, length = uint8array.length; i < length; i+=2) {
if (uint8array.length -i > 1) {
var x = (uint8array[i]<<8)+ uint8array[i+1]
var [ e, x ] = divmod(x, 45*45)
var [ d, c ] = divmod(x, 45)
output.push(fromCharCode(c) + fromCharCode(d) + fromCharCode(e))
} else {
var x = uint8array[i]
var [ d, c ] = divmod(x, 45)
output.push(fromCharCode(c) + fromCharCode(d))
}
}
return output.join('')
};
var divmod = function divmod(a,b) {
var remainder = a
var quotient = 0
if (a >= b) {
remainder = a % b
quotient = (a - remainder) / b
}
return [ quotient, remainder ]
}
const BASE45_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"
var fromCharCode = function fromCharCode(c) {
return BASE45_CHARSET.charAt(c);
};
function decode(str) {
var output = []
var buf = []
for(var i = 0, length=str.length; i < length; i++) {
//console.log(i);
var j = BASE45_CHARSET.indexOf(str[i])
if (j < 0)
console.log('Base45 decode: unknown character n.', i, j);
//throw new Error('Base45 decode: unknown character');
buf.push(j)
}
for(var i = 0, length=buf.length; i < length; i+=3) {
var x = buf[i] + buf[i + 1] * 45
if (length - i >= 3) {
var [d, c] = divmod(x + buf[i + 2] * 45 * 45,256)
output.push(d)
output.push(c)
} else {
output.push(x)
}
}
console.log("output",output);
var enc = new TextEncoder();
return {"enc" : enc.encode(output), "raw" : output};
//return Buffer.from(output);
};