1
|
var utils = require('./utils');
|
2
|
|
3
|
var table = `
|
4
|
( 0) |11111111|11000 1ff8 [13]
|
5
|
( 1) |11111111|11111111|1011000 7fffd8 [23]
|
6
|
( 2) |11111111|11111111|11111110|0010 fffffe2 [28]
|
7
|
( 3) |11111111|11111111|11111110|0011 fffffe3 [28]
|
8
|
( 4) |11111111|11111111|11111110|0100 fffffe4 [28]
|
9
|
( 5) |11111111|11111111|11111110|0101 fffffe5 [28]
|
10
|
( 6) |11111111|11111111|11111110|0110 fffffe6 [28]
|
11
|
( 7) |11111111|11111111|11111110|0111 fffffe7 [28]
|
12
|
( 8) |11111111|11111111|11111110|1000 fffffe8 [28]
|
13
|
( 9) |11111111|11111111|11101010 ffffea [24]
|
14
|
( 10) |11111111|11111111|11111111|111100 3ffffffc [30]
|
15
|
( 11) |11111111|11111111|11111110|1001 fffffe9 [28]
|
16
|
( 12) |11111111|11111111|11111110|1010 fffffea [28]
|
17
|
( 13) |11111111|11111111|11111111|111101 3ffffffd [30]
|
18
|
( 14) |11111111|11111111|11111110|1011 fffffeb [28]
|
19
|
( 15) |11111111|11111111|11111110|1100 fffffec [28]
|
20
|
( 16) |11111111|11111111|11111110|1101 fffffed [28]
|
21
|
( 17) |11111111|11111111|11111110|1110 fffffee [28]
|
22
|
( 18) |11111111|11111111|11111110|1111 fffffef [28]
|
23
|
( 19) |11111111|11111111|11111111|0000 ffffff0 [28]
|
24
|
( 20) |11111111|11111111|11111111|0001 ffffff1 [28]
|
25
|
( 21) |11111111|11111111|11111111|0010 ffffff2 [28]
|
26
|
( 22) |11111111|11111111|11111111|111110 3ffffffe [30]
|
27
|
( 23) |11111111|11111111|11111111|0011 ffffff3 [28]
|
28
|
( 24) |11111111|11111111|11111111|0100 ffffff4 [28]
|
29
|
( 25) |11111111|11111111|11111111|0101 ffffff5 [28]
|
30
|
( 26) |11111111|11111111|11111111|0110 ffffff6 [28]
|
31
|
( 27) |11111111|11111111|11111111|0111 ffffff7 [28]
|
32
|
( 28) |11111111|11111111|11111111|1000 ffffff8 [28]
|
33
|
( 29) |11111111|11111111|11111111|1001 ffffff9 [28]
|
34
|
( 30) |11111111|11111111|11111111|1010 ffffffa [28]
|
35
|
( 31) |11111111|11111111|11111111|1011 ffffffb [28]
|
36
|
' ' ( 32) |010100 14 [ 6]
|
37
|
'!' ( 33) |11111110|00 3f8 [10]
|
38
|
'"' ( 34) |11111110|01 3f9 [10]
|
39
|
'#' ( 35) |11111111|1010 ffa [12]
|
40
|
'$' ( 36) |11111111|11001 1ff9 [13]
|
41
|
'%' ( 37) |010101 15 [ 6]
|
42
|
'&' ( 38) |11111000 f8 [ 8]
|
43
|
''' ( 39) |11111111|010 7fa [11]
|
44
|
'(' ( 40) |11111110|10 3fa [10]
|
45
|
')' ( 41) |11111110|11 3fb [10]
|
46
|
'*' ( 42) |11111001 f9 [ 8]
|
47
|
'+' ( 43) |11111111|011 7fb [11]
|
48
|
',' ( 44) |11111010 fa [ 8]
|
49
|
'-' ( 45) |010110 16 [ 6]
|
50
|
'.' ( 46) |010111 17 [ 6]
|
51
|
'/' ( 47) |011000 18 [ 6]
|
52
|
'0' ( 48) |00000 0 [ 5]
|
53
|
'1' ( 49) |00001 1 [ 5]
|
54
|
'2' ( 50) |00010 2 [ 5]
|
55
|
'3' ( 51) |011001 19 [ 6]
|
56
|
'4' ( 52) |011010 1a [ 6]
|
57
|
'5' ( 53) |011011 1b [ 6]
|
58
|
'6' ( 54) |011100 1c [ 6]
|
59
|
'7' ( 55) |011101 1d [ 6]
|
60
|
'8' ( 56) |011110 1e [ 6]
|
61
|
'9' ( 57) |011111 1f [ 6]
|
62
|
':' ( 58) |1011100 5c [ 7]
|
63
|
';' ( 59) |11111011 fb [ 8]
|
64
|
'<' ( 60) |11111111|1111100 7ffc [15]
|
65
|
'=' ( 61) |100000 20 [ 6]
|
66
|
'>' ( 62) |11111111|1011 ffb [12]
|
67
|
'?' ( 63) |11111111|00 3fc [10]
|
68
|
'@' ( 64) |11111111|11010 1ffa [13]
|
69
|
'A' ( 65) |100001 21 [ 6]
|
70
|
'B' ( 66) |1011101 5d [ 7]
|
71
|
'C' ( 67) |1011110 5e [ 7]
|
72
|
'D' ( 68) |1011111 5f [ 7]
|
73
|
'E' ( 69) |1100000 60 [ 7]
|
74
|
'F' ( 70) |1100001 61 [ 7]
|
75
|
'G' ( 71) |1100010 62 [ 7]
|
76
|
'H' ( 72) |1100011 63 [ 7]
|
77
|
'I' ( 73) |1100100 64 [ 7]
|
78
|
'J' ( 74) |1100101 65 [ 7]
|
79
|
'K' ( 75) |1100110 66 [ 7]
|
80
|
'L' ( 76) |1100111 67 [ 7]
|
81
|
'M' ( 77) |1101000 68 [ 7]
|
82
|
'N' ( 78) |1101001 69 [ 7]
|
83
|
'O' ( 79) |1101010 6a [ 7]
|
84
|
'P' ( 80) |1101011 6b [ 7]
|
85
|
'Q' ( 81) |1101100 6c [ 7]
|
86
|
'R' ( 82) |1101101 6d [ 7]
|
87
|
'S' ( 83) |1101110 6e [ 7]
|
88
|
'T' ( 84) |1101111 6f [ 7]
|
89
|
'U' ( 85) |1110000 70 [ 7]
|
90
|
'V' ( 86) |1110001 71 [ 7]
|
91
|
'W' ( 87) |1110010 72 [ 7]
|
92
|
'X' ( 88) |11111100 fc [ 8]
|
93
|
'Y' ( 89) |1110011 73 [ 7]
|
94
|
'Z' ( 90) |11111101 fd [ 8]
|
95
|
'[' ( 91) |11111111|11011 1ffb [13]
|
96
|
'\\' ( 92) |11111111|11111110|000 7fff0 [19]
|
97
|
']' ( 93) |11111111|11100 1ffc [13]
|
98
|
'^' ( 94) |11111111|111100 3ffc [14]
|
99
|
'_' ( 95) |100010 22 [ 6]
|
100
|
'\`' ( 96) |11111111|1111101 7ffd [15]
|
101
|
'a' ( 97) |00011 3 [ 5]
|
102
|
'b' ( 98) |100011 23 [ 6]
|
103
|
'c' ( 99) |00100 4 [ 5]
|
104
|
'd' (100) |100100 24 [ 6]
|
105
|
'e' (101) |00101 5 [ 5]
|
106
|
'f' (102) |100101 25 [ 6]
|
107
|
'g' (103) |100110 26 [ 6]
|
108
|
'h' (104) |100111 27 [ 6]
|
109
|
'i' (105) |00110 6 [ 5]
|
110
|
'j' (106) |1110100 74 [ 7]
|
111
|
'k' (107) |1110101 75 [ 7]
|
112
|
'l' (108) |101000 28 [ 6]
|
113
|
'm' (109) |101001 29 [ 6]
|
114
|
'n' (110) |101010 2a [ 6]
|
115
|
'o' (111) |00111 7 [ 5]
|
116
|
'p' (112) |101011 2b [ 6]
|
117
|
'q' (113) |1110110 76 [ 7]
|
118
|
'r' (114) |101100 2c [ 6]
|
119
|
's' (115) |01000 8 [ 5]
|
120
|
't' (116) |01001 9 [ 5]
|
121
|
'u' (117) |101101 2d [ 6]
|
122
|
'v' (118) |1110111 77 [ 7]
|
123
|
'w' (119) |1111000 78 [ 7]
|
124
|
'x' (120) |1111001 79 [ 7]
|
125
|
'y' (121) |1111010 7a [ 7]
|
126
|
'z' (122) |1111011 7b [ 7]
|
127
|
'{' (123) |11111111|1111110 7ffe [15]
|
128
|
'|' (124) |11111111|100 7fc [11]
|
129
|
'}' (125) |11111111|111101 3ffd [14]
|
130
|
'~' (126) |11111111|11101 1ffd [13]
|
131
|
(127) |11111111|11111111|11111111|1100 ffffffc [28]
|
132
|
(128) |11111111|11111110|0110 fffe6 [20]
|
133
|
(129) |11111111|11111111|010010 3fffd2 [22]
|
134
|
(130) |11111111|11111110|0111 fffe7 [20]
|
135
|
(131) |11111111|11111110|1000 fffe8 [20]
|
136
|
(132) |11111111|11111111|010011 3fffd3 [22]
|
137
|
(133) |11111111|11111111|010100 3fffd4 [22]
|
138
|
(134) |11111111|11111111|010101 3fffd5 [22]
|
139
|
(135) |11111111|11111111|1011001 7fffd9 [23]
|
140
|
(136) |11111111|11111111|010110 3fffd6 [22]
|
141
|
(137) |11111111|11111111|1011010 7fffda [23]
|
142
|
(138) |11111111|11111111|1011011 7fffdb [23]
|
143
|
(139) |11111111|11111111|1011100 7fffdc [23]
|
144
|
(140) |11111111|11111111|1011101 7fffdd [23]
|
145
|
(141) |11111111|11111111|1011110 7fffde [23]
|
146
|
(142) |11111111|11111111|11101011 ffffeb [24]
|
147
|
(143) |11111111|11111111|1011111 7fffdf [23]
|
148
|
(144) |11111111|11111111|11101100 ffffec [24]
|
149
|
(145) |11111111|11111111|11101101 ffffed [24]
|
150
|
(146) |11111111|11111111|010111 3fffd7 [22]
|
151
|
(147) |11111111|11111111|1100000 7fffe0 [23]
|
152
|
(148) |11111111|11111111|11101110 ffffee [24]
|
153
|
(149) |11111111|11111111|1100001 7fffe1 [23]
|
154
|
(150) |11111111|11111111|1100010 7fffe2 [23]
|
155
|
(151) |11111111|11111111|1100011 7fffe3 [23]
|
156
|
(152) |11111111|11111111|1100100 7fffe4 [23]
|
157
|
(153) |11111111|11111110|11100 1fffdc [21]
|
158
|
(154) |11111111|11111111|011000 3fffd8 [22]
|
159
|
(155) |11111111|11111111|1100101 7fffe5 [23]
|
160
|
(156) |11111111|11111111|011001 3fffd9 [22]
|
161
|
(157) |11111111|11111111|1100110 7fffe6 [23]
|
162
|
(158) |11111111|11111111|1100111 7fffe7 [23]
|
163
|
(159) |11111111|11111111|11101111 ffffef [24]
|
164
|
(160) |11111111|11111111|011010 3fffda [22]
|
165
|
(161) |11111111|11111110|11101 1fffdd [21]
|
166
|
(162) |11111111|11111110|1001 fffe9 [20]
|
167
|
(163) |11111111|11111111|011011 3fffdb [22]
|
168
|
(164) |11111111|11111111|011100 3fffdc [22]
|
169
|
(165) |11111111|11111111|1101000 7fffe8 [23]
|
170
|
(166) |11111111|11111111|1101001 7fffe9 [23]
|
171
|
(167) |11111111|11111110|11110 1fffde [21]
|
172
|
(168) |11111111|11111111|1101010 7fffea [23]
|
173
|
(169) |11111111|11111111|011101 3fffdd [22]
|
174
|
(170) |11111111|11111111|011110 3fffde [22]
|
175
|
(171) |11111111|11111111|11110000 fffff0 [24]
|
176
|
(172) |11111111|11111110|11111 1fffdf [21]
|
177
|
(173) |11111111|11111111|011111 3fffdf [22]
|
178
|
(174) |11111111|11111111|1101011 7fffeb [23]
|
179
|
(175) |11111111|11111111|1101100 7fffec [23]
|
180
|
(176) |11111111|11111111|00000 1fffe0 [21]
|
181
|
(177) |11111111|11111111|00001 1fffe1 [21]
|
182
|
(178) |11111111|11111111|100000 3fffe0 [22]
|
183
|
(179) |11111111|11111111|00010 1fffe2 [21]
|
184
|
(180) |11111111|11111111|1101101 7fffed [23]
|
185
|
(181) |11111111|11111111|100001 3fffe1 [22]
|
186
|
(182) |11111111|11111111|1101110 7fffee [23]
|
187
|
(183) |11111111|11111111|1101111 7fffef [23]
|
188
|
(184) |11111111|11111110|1010 fffea [20]
|
189
|
(185) |11111111|11111111|100010 3fffe2 [22]
|
190
|
(186) |11111111|11111111|100011 3fffe3 [22]
|
191
|
(187) |11111111|11111111|100100 3fffe4 [22]
|
192
|
(188) |11111111|11111111|1110000 7ffff0 [23]
|
193
|
(189) |11111111|11111111|100101 3fffe5 [22]
|
194
|
(190) |11111111|11111111|100110 3fffe6 [22]
|
195
|
(191) |11111111|11111111|1110001 7ffff1 [23]
|
196
|
(192) |11111111|11111111|11111000|00 3ffffe0 [26]
|
197
|
(193) |11111111|11111111|11111000|01 3ffffe1 [26]
|
198
|
(194) |11111111|11111110|1011 fffeb [20]
|
199
|
(195) |11111111|11111110|001 7fff1 [19]
|
200
|
(196) |11111111|11111111|100111 3fffe7 [22]
|
201
|
(197) |11111111|11111111|1110010 7ffff2 [23]
|
202
|
(198) |11111111|11111111|101000 3fffe8 [22]
|
203
|
(199) |11111111|11111111|11110110|0 1ffffec [25]
|
204
|
(200) |11111111|11111111|11111000|10 3ffffe2 [26]
|
205
|
(201) |11111111|11111111|11111000|11 3ffffe3 [26]
|
206
|
(202) |11111111|11111111|11111001|00 3ffffe4 [26]
|
207
|
(203) |11111111|11111111|11111011|110 7ffffde [27]
|
208
|
(204) |11111111|11111111|11111011|111 7ffffdf [27]
|
209
|
(205) |11111111|11111111|11111001|01 3ffffe5 [26]
|
210
|
(206) |11111111|11111111|11110001 fffff1 [24]
|
211
|
(207) |11111111|11111111|11110110|1 1ffffed [25]
|
212
|
(208) |11111111|11111110|010 7fff2 [19]
|
213
|
(209) |11111111|11111111|00011 1fffe3 [21]
|
214
|
(210) |11111111|11111111|11111001|10 3ffffe6 [26]
|
215
|
(211) |11111111|11111111|11111100|000 7ffffe0 [27]
|
216
|
(212) |11111111|11111111|11111100|001 7ffffe1 [27]
|
217
|
(213) |11111111|11111111|11111001|11 3ffffe7 [26]
|
218
|
(214) |11111111|11111111|11111100|010 7ffffe2 [27]
|
219
|
(215) |11111111|11111111|11110010 fffff2 [24]
|
220
|
(216) |11111111|11111111|00100 1fffe4 [21]
|
221
|
(217) |11111111|11111111|00101 1fffe5 [21]
|
222
|
(218) |11111111|11111111|11111010|00 3ffffe8 [26]
|
223
|
(219) |11111111|11111111|11111010|01 3ffffe9 [26]
|
224
|
(220) |11111111|11111111|11111111|1101 ffffffd [28]
|
225
|
(221) |11111111|11111111|11111100|011 7ffffe3 [27]
|
226
|
(222) |11111111|11111111|11111100|100 7ffffe4 [27]
|
227
|
(223) |11111111|11111111|11111100|101 7ffffe5 [27]
|
228
|
(224) |11111111|11111110|1100 fffec [20]
|
229
|
(225) |11111111|11111111|11110011 fffff3 [24]
|
230
|
(226) |11111111|11111110|1101 fffed [20]
|
231
|
(227) |11111111|11111111|00110 1fffe6 [21]
|
232
|
(228) |11111111|11111111|101001 3fffe9 [22]
|
233
|
(229) |11111111|11111111|00111 1fffe7 [21]
|
234
|
(230) |11111111|11111111|01000 1fffe8 [21]
|
235
|
(231) |11111111|11111111|1110011 7ffff3 [23]
|
236
|
(232) |11111111|11111111|101010 3fffea [22]
|
237
|
(233) |11111111|11111111|101011 3fffeb [22]
|
238
|
(234) |11111111|11111111|11110111|0 1ffffee [25]
|
239
|
(235) |11111111|11111111|11110111|1 1ffffef [25]
|
240
|
(236) |11111111|11111111|11110100 fffff4 [24]
|
241
|
(237) |11111111|11111111|11110101 fffff5 [24]
|
242
|
(238) |11111111|11111111|11111010|10 3ffffea [26]
|
243
|
(239) |11111111|11111111|1110100 7ffff4 [23]
|
244
|
(240) |11111111|11111111|11111010|11 3ffffeb [26]
|
245
|
(241) |11111111|11111111|11111100|110 7ffffe6 [27]
|
246
|
(242) |11111111|11111111|11111011|00 3ffffec [26]
|
247
|
(243) |11111111|11111111|11111011|01 3ffffed [26]
|
248
|
(244) |11111111|11111111|11111100|111 7ffffe7 [27]
|
249
|
(245) |11111111|11111111|11111101|000 7ffffe8 [27]
|
250
|
(246) |11111111|11111111|11111101|001 7ffffe9 [27]
|
251
|
(247) |11111111|11111111|11111101|010 7ffffea [27]
|
252
|
(248) |11111111|11111111|11111101|011 7ffffeb [27]
|
253
|
(249) |11111111|11111111|11111111|1110 ffffffe [28]
|
254
|
(250) |11111111|11111111|11111101|100 7ffffec [27]
|
255
|
(251) |11111111|11111111|11111101|101 7ffffed [27]
|
256
|
(252) |11111111|11111111|11111101|110 7ffffee [27]
|
257
|
(253) |11111111|11111111|11111101|111 7ffffef [27]
|
258
|
(254) |11111111|11111111|11111110|000 7fffff0 [27]
|
259
|
(255) |11111111|11111111|11111011|10 3ffffee [26]
|
260
|
EOS (256) |11111111|11111111|11111111|111111 3fffffff [30]
|
261
|
`;
|
262
|
|
263
|
table = table.split('\n').filter(function(line) {
|
264
|
return line.length > 0;
|
265
|
});
|
266
|
|
267
|
function createNode() {
|
268
|
// See `encodeBits`
|
269
|
var arr = new Array(256);
|
270
|
for (var i = 0; i < arr.length; i++)
|
271
|
arr[i] = 0;
|
272
|
return arr;
|
273
|
}
|
274
|
|
275
|
var decode = createNode();
|
276
|
var encode = createNode();
|
277
|
|
278
|
function encodeBits(bits) {
|
279
|
var num = parseInt(bits, 2);
|
280
|
|
281
|
return num;
|
282
|
}
|
283
|
|
284
|
table.forEach(function(line) {
|
285
|
var match = line.match(/\(\s*([\d]+)\)\s+\|([^\s]+)\s+([^\s]+)/);
|
286
|
var octet = match[1] | 0;
|
287
|
var bits = match[2].split(/\|/g);
|
288
|
var hex = parseInt(match[3], 16);
|
289
|
|
290
|
var node = decode;
|
291
|
var totalLen = 0;
|
292
|
for (var i = 0; i < bits.length - 1; i++) {
|
293
|
var b = encodeBits(bits[i]);
|
294
|
totalLen += bits[i].length;
|
295
|
if (node[b] === 0)
|
296
|
node[b] = createNode();
|
297
|
node = node[b];
|
298
|
}
|
299
|
totalLen += bits[i].length;
|
300
|
node[encodeBits(bits[i])] = (bits[i].length << 9) | octet;
|
301
|
encode[octet] = [ totalLen, hex ];
|
302
|
});
|
303
|
|
304
|
// Wrap lines after 79 chars
|
305
|
out = 'exports.decode =\n' + utils.wrap(JSON.stringify(decode)) + ';';
|
306
|
console.log(out);
|
307
|
|
308
|
out = 'exports.encode =\n' + utils.wrap(JSON.stringify(encode)) + ';';
|
309
|
console.log(out);
|