-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathBase94.mjs
164 lines (108 loc) · 3.33 KB
/
Base94.mjs
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
62
63
64
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
91
92
93
94
95
96
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Base94 functions.
*
* @author [email protected]]
* @license Apache-2.0
*/
import Utils from "../Utils.mjs";
import OperationError from "../errors/OperationError.mjs";
/**
* Base94's the input byte array, returning a string.
* Every four bytes of input are converted to five bytes of
* Base94 encoded output.
*
* @param {ArrayBuffer} data
* @param {boolean} [strictLength="true"]
* @returns {string}
*
* @example
* // returns "@Z<[+/- >5$@3z&T!Qh*|F.q+ZWIz&#J<[+][[4+trr# "
* // toBase94([48, 65, 6c, 6c, 6f, 20, 57, 6f, 72, 6c, 64, 21]);
* // e.g. toBase94(ToHex("Hello World!"))
*/
export function toBase94(data, strictLength=true) {
if (!data) return "";
if (data instanceof ArrayBuffer) {
data = new Uint8Array(data);
} else {
throw new OperationError(`Invalid - Input not instanceof ArrayBuffer.`);
}
const dataModLen = data.length % 4;
if (dataModLen > 0 && strictLength) {
throw new OperationError(`Invalid - Input byte length must be a multiple of 4.`);
}
let output = "", i = 0, j = 0, acc = 0;
const dataPad = new Uint8Array(data.length + (dataModLen > 0 ? (4 - dataModLen) : 0));
dataPad.set(data, 0);
while (i < dataPad.length) {
acc = 0;
for (j = 0; j < 4; j++) {
acc *= 256;
acc += dataPad[i + (3 - j)];
}
for (j = 0; j < 5; j++) {
output += String.fromCharCode((acc % 94)+32);
acc = Math.floor(acc / 94);
}
i += 4;
}
return output;
}
/**
* Un-Base94's the input string, returning a byte array.
* Every five bytes of Base94 encoded input are converted to
* four bytes of output.
*
* @param {string} data // Base94 encoded string
* @param {boolean} [strictLength="true"]
* @param {boolean} [removeInvalidChars="false"]
* @returns {byteArray}
*
* @example
* // returns [48, 65, 6c, 6c, 6f, 20, 57, 6f, 72, 6c, 64, 21]
* // fromBase94("@Z<[+/- >5$@3z&T!Qh*|F.q+ZWIz&#J<[+][[4+trr# ", true, true);
* // e.g. fromHex(fromBase94(....)); -> Hello World!
*/
export function fromBase94(data, strictLength=true, removeInvalidChars=false) {
if (!data) {
return [];
}
if (typeof data == "string") {
data = Utils.strToByteArray(data);
} else {
throw new OperationError(`Invalid - typeof base94 input is not a string.`);
}
const re = new RegExp("[^\x20-\x7e]", "g");
if (re.test(data)) {
if (removeInvalidChars) {
data = data.replace(re, "");
} else {
throw new OperationError(`Invalid content in Base94 string.`);
}
}
let stringModLen = data.length % 5;
if (stringModLen > 0) {
if (strictLength) {
throw new OperationError(`Invalid - Input string length must be a multiple of 5.`);
}
stringModLen = 5 - stringModLen;
while (stringModLen > 0) {
data.push(32);
stringModLen -= 1;
}
}
const output = [];
let i = 0, j = 0, acc = 0;
while (i < data.length) {
acc = 0;
for (j = 0; j < 5; j++) {
acc = (acc * 94) + data[i + 4 - j] - 32;
}
for (j = 0; j < 4; j++) {
output.push(acc % 256);
acc = Math.floor(acc / 256);
}
i += 5;
}
return output;
}