-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytes.js
executable file
·247 lines (216 loc) · 5.6 KB
/
bytes.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env node
let Bytes = {};
Bytes.textEncoder = new TextEncoder();
Bytes.textDecoder = new TextDecoder("utf-8", { fatal: true });
/** @typedef {String} Hex */
/**
* @param {Hex} hex
*/
Bytes.hexToBytes = function (hex) {
let bufLen = hex.length / 2;
let bytes = new Uint8Array(bufLen);
let i = 0;
let index = 0;
let lastIndex = hex.length - 2;
for (;;) {
if (i > lastIndex) {
break;
}
let h = hex.substr(i, 2);
let b = parseInt(h, 16);
let nan = isNaN(b);
if (nan) {
throw new Error(`'${h}' could not be parsed as hex`);
}
bytes[index] = b;
i += 2;
index += 1;
}
return bytes;
};
/**
* @param {Uint8Array} bytes
*/
Bytes.bytesToHex = function (bytes) {
/** @type {Array<String>} */
let hs = [];
for (let b of bytes) {
let h = b.toString(16);
h = h.padStart(2, "0");
hs.push(h);
}
let hex = hs.join("");
return hex;
};
/** @typedef {String} URLBase64 */
/** @typedef {String} RFCBase64 */
/**
* Decode Standard or URL-safe Base64 to a Uint8Array
* @param {URLBase64} urlBase64
*/
Bytes.urlBase64ToBytes = function (urlBase64) {
let rfcBase64 = urlBase64.replace(/_/g, "/");
rfcBase64 = rfcBase64.replace(/-/g, "+");
while (rfcBase64.length % 4 > 0) {
rfcBase64 += "=";
}
let binstr = atob(rfcBase64);
let bytes = new Uint8Array(binstr.length);
for (let i = 0; i < binstr.length; i += 1) {
bytes[i] = binstr.charCodeAt(i);
}
return bytes;
};
Bytes.base64ToBytes = Bytes.urlBase64ToBytes;
Bytes.rfcBase64ToBytes = function (rfcBase64) {
while (rfcBase64.length % 4 > 0) {
throw new Error(`'${rfcBase64}' could not be parsed as RFC Base64`);
}
let binstr = atob(rfcBase64);
let bytes = new Uint8Array(binstr.length);
for (let i = 0; i < binstr.length; i += 1) {
bytes[i] = binstr.charCodeAt(i);
}
return bytes;
};
/**
* Encode Uint8Array bytes as URL-safe Base64
* @param {Uint8Array} bytes
* @returns {URLBase64}
*/
Bytes.bytesToUrlBase64 = function (bytes) {
let binaryString = String.fromCharCode.apply(null, bytes);
let base64 = btoa(binaryString);
let urlBase64 = base64.replace(/\+/g, "-");
urlBase64 = urlBase64.replace(/[/]/g, "_");
urlBase64 = urlBase64.replace(/=+$/, "");
return urlBase64;
};
/**
* Encode Uint8Array bytes as Standard Base64
* @param {Uint8Array} bytes
* @returns {RFCBase64}
*/
Bytes.bytesToRfcBase64 = function (bytes) {
let binaryString = String.fromCharCode.apply(null, bytes);
let base64 = btoa(binaryString);
return base64;
};
async function main() {
let data = process.argv[2];
let bytes;
let text;
let rfcbase64;
let urlbase64;
let hex;
let decoders = [
{
from: "hex",
to: "bytes",
decode: function () {
bytes = Bytes.hexToBytes(data);
hex = data;
return true;
},
},
{
from: "rfcbase64",
to: "bytes",
decode: function () {
bytes = Bytes.rfcBase64ToBytes(data);
rfcbase64 = data;
return true;
},
},
{
from: "urlbase64",
to: "bytes",
decode: function () {
bytes = Bytes.urlBase64ToBytes(data);
urlbase64 = data;
return true;
},
},
{
from: "utf8",
to: "bytes",
decode: function () {
bytes = Bytes.textEncoder.encode(data);
text = data;
return true;
},
},
];
let encoders = [
{
from: "bytes",
to: "utf8",
encode: function () {
let text = Bytes.textDecoder.decode(bytes);
console.info(`Text: ${text}`);
return text;
},
},
{
from: "bytes",
to: "urlbase64",
encode: function () {
let urlbase64 = Bytes.bytesToUrlBase64(bytes);
console.info(`URL Base64: ${urlbase64}`);
return urlbase64;
},
},
{
from: "bytes",
to: "rfcbase64",
encode: function () {
let base64 = Bytes.bytesToRfcBase64(bytes);
console.info(`RFC Base64: ${base64}`);
return base64;
},
},
{
from: "bytes",
to: "hex",
encode: function () {
let hex = Bytes.bytesToHex(bytes);
console.info(`Hex: ${hex}`);
return hex;
},
},
];
for (let dec of decoders) {
try {
let decoded = dec.decode();
if (decoded) {
break;
}
} catch (e) {
// ignore
}
}
console.info();
if (text) {
console.info(`Detected: UTF-8`);
} else if (rfcbase64) {
console.info(`Detected: RFC Base64`);
} else if (urlbase64) {
console.info(`Detected: URL Base64`);
} else if (hex) {
console.info(`Detected: Hex`);
}
console.info();
for (let enc of encoders) {
try {
enc.encode();
} catch (e) {
// ignore
}
}
console.info();
}
main().catch(function (err) {
console.error("main() error:");
console.error(err.stack || err);
});
export default Bytes;