-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcodec.js
More file actions
363 lines (311 loc) · 7.7 KB
/
Copy pathcodec.js
File metadata and controls
363 lines (311 loc) · 7.7 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* Copyright (c) 2022 RethinkDNS and its authors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
const encNative = new TextEncoder();
const decNative = new TextDecoder();
export const b8 = 8;
export const b6 = 6;
// a char that's not in b32, b64 set; used to demarc
// key (domain) from value (blocklist); see trie.build
export const delim = "#"; // ascii: 35
// some domains are registered with _ and even work just fine
// ex: prebid_stats.mars.media
const underscore = "_"; // ascii: 95
// rfc-editor.org/rfc/rfc1035#section-2.3.1 characters
const period = "."; // ascii: 46
const numerals = "01234567890"; // ascii: 48..57
const alphabet = "abcdefghijklmnopqrstuvwxyz"; // ascii: 97..122
const hyphen = "-"; // ascii: 45
const asterix = "*"; // ascii: 42
// must be in lexographical order as defined by ascii/utf8; see trie.build
const validchars6 =
delim + asterix + hyphen + period + numerals + alphabet + underscore;
const memstat = { encode: 0, decode: 0, encode16: 0, decode16: 0 };
const memencode = new Map();
const memencode16 = new Map();
const memdecode = new Map();
const memdecode16 = new Map();
const { map: ord6, rev: chr6 } = index6(validchars6);
const { map: ord16, rev: chr16 } = index16();
function index16() {
return {
map: { get: (c) => c.charCodeAt(0) },
rev: { get: (n) => String.fromCharCode(n) },
};
}
// encode and decode adopted from:
// github.com/serverless-dns/serverless-dns/blob/d72be3f/src/commons/b32.js
function index6(str) {
const m = new Map();
const r = new Map();
let i = 0;
for (const c of str) {
m.set(c, i);
r.set(i, c);
i += 1;
}
return { map: m, rev: r };
}
export class Codec {
constructor(typ = 8) {
// either b8 or b5
this.typ = typ;
}
// encode memoizes poorly due to the way build works
// by appending / prepending list names against domains
// which means, no two values from 2 lists are alike.
encode(str6or8, pool = false) {
if (pool) {
const u6or8 = memencode.get(str6or8);
if (u6or8 != null) {
memstat.encode += 1;
return u6or8;
}
}
const u6or8 = this.encodeinner(str6or8);
if (pool) memencode.set(str6or8, u6or8);
return u6or8;
}
decode(u6or8, pool = true) {
let k = null;
if (pool) {
k = u6or8.join(",");
const str6or8 = memdecode.get(k);
if (str6or8 != null) {
memstat.decode += 1;
return str6or8;
}
}
const str6or8 = this.decodeinner(u6or8);
if (pool && k) memdecode.set(k, str6or8);
return str6or8;
}
encodeinner(str6or8) {
if (this.typ === b8) {
const str8 = str6or8;
// returns u8
return encNative.encode(str8);
}
const str6 = str6or8.toLowerCase();
const u6 = new Uint8Array(str6.length);
let i = 0;
for (const c of str6) {
const n = ord6.get(c);
if (n != null) {
u6[i++] = n;
} else {
throw new Error(
"encode: undef num: " +
n +
", for: " +
c +
", in: " +
str6 +
", res: " +
u6
);
}
}
return u6;
}
decodeinner(u6or8) {
if (this.typ === b8) {
const u8 = u6or8;
// returns str8or16
return decNative.decode(u8);
}
const u6 = u6or8;
let str6 = "";
for (const i of u6) {
const c = chr6.get(i);
if (c != null) {
str6 += c;
} else {
throw new Error(
"decode: undef char: " +
c +
", for: " +
i +
", in: " +
u6 +
", res: " +
str6
);
}
}
return str6;
}
encode16(str16, pool = true) {
if (pool) {
const u6or8 = memencode16.get(str16);
if (u6or8 != null) {
memstat.encode16 += 1;
return u6or8;
}
}
const u6or8 = this.encode16inner(str16);
if (pool) memencode16.set(str16, u6or8);
return u6or8;
}
decode16(u6or8, pool = true) {
let k = null;
if (pool) {
k = u6or8.join(",");
const str16 = memdecode16.get(k);
if (str16 != null) {
memstat.decode16 += 1;
return str16;
}
}
const str16 = this.decode16inner(u6or8);
if (pool && k) memdecode16.set(k, str16);
return str16;
}
encode16inner(str16) {
if (this.typ === b8) {
// returns u8
return this.encode(str16);
}
const W = 16;
const n = 6;
const mask = 2 ** n - 1;
const len16 = str16.length;
const len6 = Math.ceil((len16 * W) / n);
const u6 = new Uint8Array(len6);
let bits = 0;
let acc = 0;
let j = 0;
for (let i = 0; i < len16; i += 1) {
acc = (acc << W) | ord16.get(str16[i]);
bits += W;
while (bits >= n) {
u6[j++] = (acc >>> (bits - n)) & mask;
bits -= n;
}
}
if (bits > 0) {
u6[j++] = (acc << (n - bits)) & mask;
}
return u6;
}
decode16inner(u6or8) {
if (this.typ === b8) {
// returns str16
return this.decode(u6or8);
}
const u6 = u6or8;
const W = 6;
const n = 16;
const mask = 2 ** n - 1;
const len6 = u6.length;
let bits = 0;
let acc = 0;
let str16 = "";
for (let i = 0; i < len6; i += 1) {
acc = (acc << W) | u6[i];
bits += W;
if (bits >= n) {
str16 += chr16.get((acc >>> (bits - n)) & mask);
bits -= n;
}
}
if (bits > 0) {
const rem = (acc << (n - bits)) & mask;
// TODO: should there be a rem != 0 check?
if (rem !== 0) str16 += chr16.get(rem);
bits -= bits;
}
return str16;
}
decode16raw(u6or8) {
// returns u16
const str16 = this.decode16(u6or8);
return str2buf(str16);
}
decode8(u6or8) {
if (this.typ === b8) {
// no-op
return u6or8;
}
const W = 6;
const n = 8;
const mask = 2 ** n - 1;
const u6 = u6or8;
const len6 = u6.length;
const len8 = Math.ceil((len6 * W) / n);
const u8 = new Uint8Array(len8);
let bits = 0;
let acc = 0;
let j = 0;
for (let i = 0; i < len6; i += 1) {
acc = (acc << W) | u6[i];
bits += W;
if (bits >= n) {
u8[j++] = (acc >>> (bits - n)) & mask;
bits -= n;
}
}
// since encode does not pad, decode needn't worry about
// left-over (less than n) bits in acc.
// if (bits > 0) {
// const rem = (acc << (n - bits));
// u8[j++] = rem & mask;
// bits -= bits;
// }
// discard excess, if any
return u8.slice(0, j);
}
encode8(u6or8) {
if (this.typ === b8) {
// no-op
return u6or8;
}
const W = 8;
const n = 6;
const u8 = u6or8;
const mask = 2 ** n - 1;
const len8 = u8.length;
const len6 = Math.ceil((len8 * W) / n);
const u6 = new Uint8Array(len6);
let bits = 0;
let acc = 0;
let j = 0;
for (let i = 0; i < len8; i += 1) {
acc = (acc << W) | u8[i];
bits += W;
if (bits >= n) {
u6[j++] = (acc >>> (bits - n)) & mask;
bits -= n;
}
}
if (bits > 0) {
const rem = acc << (n - bits);
u6[j++] = rem & mask;
bits -= bits;
}
// j should always be equal to u6.length
// if (j !== u6.length) console.log("in: u8", u8, "out: u6", u6);
return u6;
}
delimEncoded() {
return this.encode(delim);
}
periodEncoded() {
return this.encode(period);
}
stats() {
return memstat;
}
}
export function str2buf(str16) {
const u16 = new Uint16Array(str16.length);
let i = 0;
for (const c of str16) {
u16[i++] = ord16.get(c);
}
return u16;
}