-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman.js
230 lines (202 loc) · 6.96 KB
/
huffman.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
'use strict';
/* From a table mapping bitstrings to a corresponding symbol,
* build a state table for a state machine which can decode
* Huffman-coded data 4 bits at a time
*
* `table` is like: { 'bitstring' => symbol, ... } */
function prepareDecoder(table) {
/* First find which states our state machine should have, and number them */
const prefixes = new Map(); /* prefix of a valid bitstring -> state # */
prefixes.set('', 0);
var stateNumber = 1;
for (var key of table.keys()) {
for (var i = 1; i < key.length; i++) {
const prefix = key.slice(0, i);
if (!prefixes.has(prefix))
prefixes.set(prefix, stateNumber++);
}
}
/* Now generate transition table for each state */
const states = new Array(stateNumber + 3);
prefixes.forEach(function(n, prefix) {
/* 'emit' stores the symbol value(s) to recognize if we receive input X
* while in state N
* 'goto' is the next state to transition to if we receive input X
* 'bitIndex' is the number of bits consumed from input X to recognize
* the first symbol in `emit` */
var state = states[n] = {
number: n,
prefix: prefix,
emit: new Array(16),
goto: new Array(16),
bitIndex: new Array(16)
};
for (var i = 0; i < 16; i++) {
const input = i.toString(2).padStart(4, '0');
var bitstring = prefix + input;
var emit = state.emit[i] = [];
/* Search table of valid codes to see any which match a prefix of `bitstring` */
buildEmit: while (true) {
for (var code of table.keys()) {
if (bitstring.startsWith(code)) {
if (!emit.length) /* Is this the first item going into `emit`? */
state.bitIndex[i] = code.length - prefix.length;
emit.push(table.get(code));
bitstring = bitstring.slice(code.length);
continue buildEmit;
}
}
break;
}
state.goto[i] = prefixes.get(bitstring);
Object.freeze(state);
}
});
/* At the end of the state array, add 3 special states for handling either
* 3, 2, or 1 'extra' bits at the beginning of the coded input */
for (var nBits = 1; nBits <= 3; nBits++) {
var state = states[states.length - nBits] = {
emit: new Array(16),
goto: new Array(16),
bitIndex: new Array(16)
}
for (var i = 0; i < 2 ** nBits; i++) {
var input = i.toString(2).padStart(nBits, '0');
var emit = state.emit[i] = [];
buildEmit: while (true) {
for (var code of table.keys()) {
if (input.startsWith(code)) {
if (!emit.length)
state.bitIndex[i] = code.length;
emit.push(table.get(code));
input = input.slice(code.length);
continue buildEmit;
}
}
break;
}
state.goto[i] = prefixes.get(input);
Object.freeze(state);
}
}
Object.freeze(states);
return states;
}
/* `start` is an inclusive index, `end` is an exclusive one */
function decodeBuffer(buffer, start, end, decoder) {
var position = start;
var result = [];
var state = decoder[0];
while (position < end) {
var value = buffer[position];
result.push(...state.emit[value >> 4]);
var goto = state.goto[value >> 4];
if (goto !== undefined) {
state = decoder[goto];
} else if (position < end-1) { /* The last byte may be padded */
throw new Error("Invalid input to Huffman decoder");
} else {
return result;
}
result.push(...state.emit[value & 0xF]);
var goto = state.goto[value & 0xF];
if (goto !== undefined) {
state = decoder[goto];
} else if (position < end-1) { /* As above */
throw new Error("Invalid input to Huffman decoder");
} else {
return result;
}
position++;
}
return result;
}
/* Only decode a single symbol from the input
* As with `decodeBuffer`, [start,end) are indices into `buffer`
* `bitIndex` is the bit which we should start decoding from in `buffer[start]`,
* where the MSB is numbered as 0 and the LSB as 7
* Returns an array of: [nextByteIndex, nextBitIndex, symbol] */
function decodeOne(buffer, start, end, bitIndex, decoder) {
var position = start;
var state = decoder[0];
/* Any 'extra' bits (less than a full byte) to consume at beginning of coded input? */
if (bitIndex && position < end) {
var value = buffer[position];
/* Optionally consume 1, 2, or 3 leading bits to leave a multiple of 4 bits */
var nBits = 4 - (bitIndex & 3);
if (nBits && nBits < 4) {
state = decoder[decoder.length - nBits];
var bits = (bitIndex < 4 ? value >> 4 : value) & ((1 << nBits) - 1);
var emit = state.emit[bits];
if (emit.length) {
bitIndex = bitIndex + state.bitIndex[bits];
if (bitIndex == 8)
position++;
return [position, bitIndex % 8, emit[0]];
}
var goto = state.goto[bits];
if (goto !== undefined) {
state = decoder[goto];
} else if (position < end-1) { /* The last byte may be padded */
throw new Error("Invalid input to Huffman decoder");
} else {
throw new Error("End of input (reached padding)");
}
}
/* Then consume the remaining 4 bits if there are any */
if (bitIndex <= 4) {
var emit = state.emit[value & 0xF];
if (emit.length) {
bitIndex = 4 + state.bitIndex[value & 0xF];
if (bitIndex == 8)
position++;
return [position, bitIndex % 8, emit[0]];
}
var goto = state.goto[value & 0xF];
if (goto !== undefined) {
state = decoder[goto];
} else if (position < end-1) { /* See above */
throw new Error("Invalid input to Huffman decoder");
} else {
throw new Error("End of input (reached padding)");
}
}
position++;
}
/* Consume full bytes, looking for a valid coded symbol */
while (position < end) {
var value = buffer[position];
var emit = state.emit[value >> 4];
if (emit.length) {
return [position, state.bitIndex[value >> 4], emit[0]];
}
var goto = state.goto[value >> 4];
if (goto !== undefined) {
state = decoder[goto];
} else if (position < end-1) { /* See above */
throw new Error("Invalid input to Huffman decoder");
} else {
throw new Error("End of input (reached padding)");
}
var emit = state.emit[value & 0xF];
if (emit.length) {
bitIndex = state.bitIndex[value & 0xF] + 4;
if (bitIndex == 8)
position++
return [position, bitIndex % 8, emit[0]];
}
var goto = state.goto[value & 0xF];
if (goto !== undefined) {
state = decoder[goto];
} else if (position < end-1) { /* See above */
throw new Error("Invalid input to Huffman decoder");
} else {
throw new Error("End of input (reached padding)");
}
position++;
}
throw new Error("No more input");
}
module.exports.prepareDecoder = prepareDecoder;
module.exports.decodeBuffer = decodeBuffer;
module.exports.decodeOne = decodeOne;