1 | var hpack = require('../hpack');
|
---|
2 | var utils = hpack.utils;
|
---|
3 | var huffman = hpack.huffman.decode;
|
---|
4 | var assert = utils.assert;
|
---|
5 |
|
---|
6 | var OffsetBuffer = require('obuf');
|
---|
7 |
|
---|
8 | function Decoder() {
|
---|
9 | this.buffer = new OffsetBuffer();
|
---|
10 | this.bitOffset = 0;
|
---|
11 |
|
---|
12 | // Used internally in decodeStr
|
---|
13 | this._huffmanNode = null;
|
---|
14 | }
|
---|
15 | module.exports = Decoder;
|
---|
16 |
|
---|
17 | Decoder.create = function create() {
|
---|
18 | return new Decoder();
|
---|
19 | };
|
---|
20 |
|
---|
21 | Decoder.prototype.isEmpty = function isEmpty() {
|
---|
22 | return this.buffer.isEmpty();
|
---|
23 | };
|
---|
24 |
|
---|
25 | Decoder.prototype.push = function push(chunk) {
|
---|
26 | this.buffer.push(chunk);
|
---|
27 | };
|
---|
28 |
|
---|
29 | Decoder.prototype.decodeBit = function decodeBit() {
|
---|
30 | // Need at least one octet
|
---|
31 | assert(this.buffer.has(1), 'Buffer too small for an int');
|
---|
32 |
|
---|
33 | var octet;
|
---|
34 | var offset = this.bitOffset;
|
---|
35 |
|
---|
36 | if (++this.bitOffset === 8) {
|
---|
37 | octet = this.buffer.readUInt8();
|
---|
38 | this.bitOffset = 0;
|
---|
39 | } else {
|
---|
40 | octet = this.buffer.peekUInt8();
|
---|
41 | }
|
---|
42 | return (octet >>> (7 - offset)) & 1;
|
---|
43 | };
|
---|
44 |
|
---|
45 | // Just for testing
|
---|
46 | Decoder.prototype.skipBits = function skipBits(n) {
|
---|
47 | this.bitOffset += n;
|
---|
48 | this.buffer.skip(this.bitOffset >> 3);
|
---|
49 | this.bitOffset &= 0x7;
|
---|
50 | };
|
---|
51 |
|
---|
52 | Decoder.prototype.decodeInt = function decodeInt() {
|
---|
53 | // Need at least one octet
|
---|
54 | assert(this.buffer.has(1), 'Buffer too small for an int');
|
---|
55 |
|
---|
56 | var prefix = 8 - this.bitOffset;
|
---|
57 |
|
---|
58 | // We are going to end up octet-aligned
|
---|
59 | this.bitOffset = 0;
|
---|
60 |
|
---|
61 | var max = (1 << prefix) - 1;
|
---|
62 | var octet = this.buffer.readUInt8() & max;
|
---|
63 |
|
---|
64 | // Fast case - int fits into the prefix
|
---|
65 | if (octet !== max)
|
---|
66 | return octet;
|
---|
67 |
|
---|
68 | // TODO(indutny): what about > 32bit numbers?
|
---|
69 | var res = 0;
|
---|
70 | var isLast = false;
|
---|
71 | var len = 0;
|
---|
72 | do {
|
---|
73 | octet = this.buffer.readUInt8();
|
---|
74 | isLast = (octet & 0x80) === 0;
|
---|
75 |
|
---|
76 | res <<= 7;
|
---|
77 | res |= octet & 0x7f;
|
---|
78 | len++;
|
---|
79 | } while (!isLast);
|
---|
80 | assert(isLast, 'Incomplete data for multi-octet integer');
|
---|
81 | assert(len <= 4, 'Integer does not fit into 32 bits');
|
---|
82 |
|
---|
83 | // Reverse bits
|
---|
84 | res = (res >>> 21) |
|
---|
85 | (((res >> 14) & 0x7f) << 7) |
|
---|
86 | (((res >> 7) & 0x7f) << 14) |
|
---|
87 | ((res & 0x7f) << 21);
|
---|
88 | res >>= (4 - len) * 7;
|
---|
89 |
|
---|
90 | // Append prefix max
|
---|
91 | res += max;
|
---|
92 |
|
---|
93 | return res;
|
---|
94 | };
|
---|
95 |
|
---|
96 | Decoder.prototype.decodeHuffmanWord = function decodeHuffmanWord(input,
|
---|
97 | inputBits,
|
---|
98 | out) {
|
---|
99 | var root = huffman;
|
---|
100 | var node = this._huffmanNode;
|
---|
101 | var word = input;
|
---|
102 | var bits = inputBits;
|
---|
103 |
|
---|
104 | for (; bits > 0; word &= (1 << bits) - 1) {
|
---|
105 | // Nudge the word bit length to match it
|
---|
106 | for (var i = Math.max(0, bits - 8); i < bits; i++) {
|
---|
107 | var subnode = node[word >>> i];
|
---|
108 | if (typeof subnode !== 'number') {
|
---|
109 | node = subnode;
|
---|
110 | bits = i;
|
---|
111 | break;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (subnode === 0)
|
---|
115 | continue;
|
---|
116 |
|
---|
117 | // Word bit length should match
|
---|
118 | if ((subnode >>> 9) !== bits - i) {
|
---|
119 | subnode = 0;
|
---|
120 | continue;
|
---|
121 | }
|
---|
122 |
|
---|
123 | var octet = subnode & 0x1ff;
|
---|
124 | assert(octet !== 256, 'EOS in encoding');
|
---|
125 | out.push(octet);
|
---|
126 | node = root;
|
---|
127 |
|
---|
128 | bits = i;
|
---|
129 | break;
|
---|
130 | }
|
---|
131 | if (subnode === 0)
|
---|
132 | break;
|
---|
133 | }
|
---|
134 | this._huffmanNode = node;
|
---|
135 |
|
---|
136 | return bits;
|
---|
137 | };
|
---|
138 |
|
---|
139 | Decoder.prototype.decodeStr = function decodeStr() {
|
---|
140 | var isHuffman = this.decodeBit();
|
---|
141 | var len = this.decodeInt();
|
---|
142 | assert(this.buffer.has(len), 'Not enough octets for string');
|
---|
143 |
|
---|
144 | if (!isHuffman)
|
---|
145 | return this.buffer.take(len);
|
---|
146 |
|
---|
147 | this._huffmanNode = huffman;
|
---|
148 |
|
---|
149 | var out = [];
|
---|
150 |
|
---|
151 | var word = 0;
|
---|
152 | var bits = 0;
|
---|
153 | var lastKey = 0;
|
---|
154 | for (var i = 0; i < len; i++) {
|
---|
155 | word <<= 8;
|
---|
156 | word |= this.buffer.readUInt8();
|
---|
157 | bits += 8;
|
---|
158 |
|
---|
159 | bits = this.decodeHuffmanWord(word, bits, out);
|
---|
160 | lastKey = word >> bits;
|
---|
161 | word &= (1 << bits) - 1;
|
---|
162 | }
|
---|
163 | assert(this._huffmanNode === huffman, '8-bit EOS');
|
---|
164 | assert(word + 1 === (1 << bits), 'Final sequence is not EOS');
|
---|
165 |
|
---|
166 | this._huffmanNode = null;
|
---|
167 |
|
---|
168 | return out;
|
---|
169 | };
|
---|