-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbodec-browser.js
257 lines (222 loc) · 5.53 KB
/
bodec-browser.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
248
249
250
251
252
253
254
255
256
257
"use strict";
// This file must be served with UTF-8 encoding for the utf8 codec to work.
module.exports = {
Binary: Uint8Array,
// Utility functions
isBinary: isBinary,
create: create,
join: join,
// Binary input and output
copy: copy,
slice: slice,
// String input and output
toRaw: toRaw,
fromRaw: fromRaw,
toUnicode: toUnicode,
fromUnicode: fromUnicode,
toHex: toHex,
fromHex: fromHex,
toBase64: toBase64,
fromBase64: fromBase64,
toString: toString,
fromString: fromString,
// Array input and output
toArray: toArray,
fromArray: fromArray,
// Raw <-> Hex-encoded codec
decodeHex: decodeHex,
encodeHex: encodeHex,
decodeBase64: decodeBase64,
encodeBase64: encodeBase64,
// Unicode <-> Utf8-encoded-raw codec
encodeUtf8: encodeUtf8,
decodeUtf8: decodeUtf8,
// Hex <-> Nibble codec
nibbleToCode: nibbleToCode,
codeToNibble: codeToNibble
};
function isBinary(value) {
return value &&
typeof value === "object" &&
value instanceof Uint8Array || value.constructor.name === "Uint8Array";
}
function create(length) {
return new Uint8Array(length);
}
function join(chunks) {
var length = chunks.length;
var total = 0;
for (var i = 0; i < length; i++) {
total += chunks[i].length;
}
var binary = create(total);
var offset = 0;
for (i = 0; i < length; i++) {
var chunk = chunks[i];
copy(chunk, binary, offset);
offset += chunk.length;
}
return binary;
}
function slice(binary, start, end) {
if (end === undefined) {
end = binary.length;
if (start === undefined) start = 0;
}
return binary.subarray(start, end);
}
function copy(source, binary, offset) {
var length = source.length;
if (offset === undefined) {
offset = 0;
if (binary === undefined) binary = create(length);
}
for (var i = 0; i < length; i++) {
binary[i + offset] = source[i];
}
return binary;
}
// Like slice, but encode as a hex string
function toHex(binary, start, end) {
var hex = "";
if (end === undefined) {
end = binary.length;
if (start === undefined) start = 0;
}
for (var i = start; i < end; i++) {
var byte = binary[i];
hex += String.fromCharCode(nibbleToCode(byte >> 4)) +
String.fromCharCode(nibbleToCode(byte & 0xf));
}
return hex;
}
// Like copy, but decode from a hex string
function fromHex(hex, binary, offset) {
var length = hex.length / 2;
if (offset === undefined) {
offset = 0;
if (binary === undefined) binary = create(length);
}
var j = 0;
for (var i = 0; i < length; i++) {
binary[offset + i] = (codeToNibble(hex.charCodeAt(j++)) << 4)
| codeToNibble(hex.charCodeAt(j++));
}
return binary;
}
function toBase64(binary, start, end) {
return btoa(toRaw(binary, start, end));
}
function fromBase64(base64, binary, offset) {
return fromRaw(atob(base64), binary, offset);
}
function nibbleToCode(nibble) {
nibble |= 0;
return (nibble + (nibble < 10 ? 0x30 : 0x57))|0;
}
function codeToNibble(code) {
code |= 0;
return (code - ((code & 0x40) ? 0x57 : 0x30))|0;
}
function toUnicode(binary, start, end) {
return decodeUtf8(toRaw(binary, start, end));
}
function fromUnicode(unicode, binary, offset) {
return fromRaw(encodeUtf8(unicode), binary, offset);
}
function decodeHex(hex) {
var j = 0, l = hex.length;
var raw = "";
while (j < l) {
raw += String.fromCharCode(
(codeToNibble(hex.charCodeAt(j++)) << 4)
| codeToNibble(hex.charCodeAt(j++))
);
}
return raw;
}
function encodeHex(raw) {
var hex = "";
var length = raw.length;
for (var i = 0; i < length; i++) {
var byte = raw.charCodeAt(i);
hex += String.fromCharCode(nibbleToCode(byte >> 4)) +
String.fromCharCode(nibbleToCode(byte & 0xf));
}
return hex;
}
function decodeBase64(base64) {
return atob(base64);
}
function encodeBase64(raw) {
return btoa(raw);
}
function decodeUtf8(utf8) {
return decodeURIComponent(window.escape(utf8));
}
function encodeUtf8(unicode) {
return window.unescape(encodeURIComponent(unicode));
}
function toRaw(binary, start, end) {
var raw = "";
if (end === undefined) {
end = binary.length;
if (start === undefined) start = 0;
}
for (var i = start; i < end; i++) {
raw += String.fromCharCode(binary[i]);
}
return raw;
}
function fromRaw(raw, binary, offset) {
var length = raw.length;
if (offset === undefined) {
offset = 0;
if (binary === undefined) binary = create(length);
}
for (var i = 0; i < length; i++) {
binary[offset + i] = raw.charCodeAt(i);
}
return binary;
}
function toArray(binary, start, end) {
if (end === undefined) {
end = binary.length;
if (start === undefined) start = 0;
}
var length = end - start;
var array = new Array(length);
for (var i = 0; i < length; i++) {
array[i] = binary[i + start];
}
return array;
}
function fromArray(array, binary, offset) {
var length = array.length;
if (offset === undefined) {
offset = 0;
if (binary === undefined) binary = create(length);
}
for (var i = 0; i < length; i++) {
binary[offset + i] = array[i];
}
return binary;
}
var toStringCoders = {
base64: toBase64,
hex: toHex,
utf8: toUnicode,
'utf-8': toUnicode
};
function toString(binary, encoding) {
return ((encoding && toStringCoders[encoding]) || toStringCoders.utf8)(binary);
}
var fromStringCoders = {
base64: fromBase64,
hex: fromHex,
utf8: fromUnicode,
'utf-8': fromUnicode
};
function fromString(string, encoding) {
return ((encoding && fromStringCoders[encoding]) || fromStringCoders.utf8)(string);
}