/src/h2o/deps/brotli/c/dec/decode.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2013 Google Inc. All Rights Reserved. |
2 | | |
3 | | Distributed under MIT license. |
4 | | See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
5 | | */ |
6 | | |
7 | | #include <brotli/decode.h> |
8 | | |
9 | | #ifdef __ARM_NEON__ |
10 | | #include <arm_neon.h> |
11 | | #endif |
12 | | |
13 | | #include <stdlib.h> /* free, malloc */ |
14 | | #include <string.h> /* memcpy, memset */ |
15 | | |
16 | | #include "../common/constants.h" |
17 | | #include "../common/dictionary.h" |
18 | | #include "../common/version.h" |
19 | | #include "./bit_reader.h" |
20 | | #include "./context.h" |
21 | | #include "./huffman.h" |
22 | | #include "./port.h" |
23 | | #include "./prefix.h" |
24 | | #include "./state.h" |
25 | | #include "./transform.h" |
26 | | |
27 | | #if defined(__cplusplus) || defined(c_plusplus) |
28 | | extern "C" { |
29 | | #endif |
30 | | |
31 | 0 | #define BROTLI_FAILURE(CODE) (BROTLI_DUMP(), CODE) |
32 | | |
33 | | #define BROTLI_LOG_UINT(name) \ |
34 | | BROTLI_LOG(("[%s] %s = %lu\n", __func__, #name, (unsigned long)(name))) |
35 | | #define BROTLI_LOG_ARRAY_INDEX(array_name, idx) \ |
36 | | BROTLI_LOG(("[%s] %s[%lu] = %lu\n", __func__, #array_name, \ |
37 | | (unsigned long)(idx), (unsigned long)array_name[idx])) |
38 | | |
39 | 0 | #define HUFFMAN_TABLE_BITS 8U |
40 | 0 | #define HUFFMAN_TABLE_MASK 0xff |
41 | | |
42 | | /* We need the slack region for the following reasons: |
43 | | - doing up to two 16-byte copies for fast backward copying |
44 | | - inserting transformed dictionary word (5 prefix + 24 base + 8 suffix) */ |
45 | | static const uint32_t kRingBufferWriteAheadSlack = 42; |
46 | | |
47 | | static const uint8_t kCodeLengthCodeOrder[BROTLI_CODE_LENGTH_CODES] = { |
48 | | 1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
49 | | }; |
50 | | |
51 | | /* Static prefix code for the complex code length code lengths. */ |
52 | | static const uint8_t kCodeLengthPrefixLength[16] = { |
53 | | 2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 3, 2, 2, 2, 4, |
54 | | }; |
55 | | |
56 | | static const uint8_t kCodeLengthPrefixValue[16] = { |
57 | | 0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5, |
58 | | }; |
59 | | |
60 | | BROTLI_BOOL BrotliDecoderSetParameter( |
61 | 0 | BrotliDecoderState* state, BrotliDecoderParameter p, uint32_t value) { |
62 | 0 | switch (p) { |
63 | 0 | case BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION: |
64 | 0 | state->canny_ringbuffer_allocation = !!value ? 0 : 1; |
65 | 0 | return BROTLI_TRUE; |
66 | | |
67 | 0 | default: return BROTLI_FALSE; |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | | BrotliDecoderState* BrotliDecoderCreateInstance( |
72 | 0 | brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) { |
73 | 0 | BrotliDecoderState* state = 0; |
74 | 0 | if (!alloc_func && !free_func) { |
75 | 0 | state = (BrotliDecoderState*)malloc(sizeof(BrotliDecoderState)); |
76 | 0 | } else if (alloc_func && free_func) { |
77 | 0 | state = (BrotliDecoderState*)alloc_func(opaque, sizeof(BrotliDecoderState)); |
78 | 0 | } |
79 | 0 | if (state == 0) { |
80 | 0 | BROTLI_DUMP(); |
81 | 0 | return 0; |
82 | 0 | } |
83 | 0 | BrotliDecoderStateInitWithCustomAllocators( |
84 | 0 | state, alloc_func, free_func, opaque); |
85 | 0 | return state; |
86 | 0 | } |
87 | | |
88 | | /* Deinitializes and frees BrotliDecoderState instance. */ |
89 | 0 | void BrotliDecoderDestroyInstance(BrotliDecoderState* state) { |
90 | 0 | if (!state) { |
91 | 0 | return; |
92 | 0 | } else { |
93 | 0 | brotli_free_func free_func = state->free_func; |
94 | 0 | void* opaque = state->memory_manager_opaque; |
95 | 0 | BrotliDecoderStateCleanup(state); |
96 | 0 | free_func(opaque, state); |
97 | 0 | } |
98 | 0 | } |
99 | | |
100 | | /* Saves error code and converts it to BrotliDecoderResult */ |
101 | | static BROTLI_NOINLINE BrotliDecoderResult SaveErrorCode( |
102 | 0 | BrotliDecoderState* s, BrotliDecoderErrorCode e) { |
103 | 0 | s->error_code = (int)e; |
104 | 0 | switch (e) { |
105 | 0 | case BROTLI_DECODER_SUCCESS: |
106 | 0 | return BROTLI_DECODER_RESULT_SUCCESS; |
107 | 0 | case BROTLI_DECODER_NEEDS_MORE_INPUT: |
108 | 0 | return BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT; |
109 | 0 | case BROTLI_DECODER_NEEDS_MORE_OUTPUT: |
110 | 0 | return BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; |
111 | 0 | default: |
112 | 0 | return BROTLI_DECODER_RESULT_ERROR; |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | | /* Decodes a number in the range [9..24], by reading 1 - 7 bits. |
117 | | Precondition: bit-reader accumulator has at least 7 bits. */ |
118 | 0 | static uint32_t DecodeWindowBits(BrotliBitReader* br) { |
119 | 0 | uint32_t n; |
120 | 0 | BrotliTakeBits(br, 1, &n); |
121 | 0 | if (n == 0) { |
122 | 0 | return 16; |
123 | 0 | } |
124 | 0 | BrotliTakeBits(br, 3, &n); |
125 | 0 | if (n != 0) { |
126 | 0 | return 17 + n; |
127 | 0 | } |
128 | 0 | BrotliTakeBits(br, 3, &n); |
129 | 0 | if (n != 0) { |
130 | 0 | return 8 + n; |
131 | 0 | } |
132 | 0 | return 17; |
133 | 0 | } |
134 | | |
135 | 0 | static BROTLI_INLINE void memmove16(uint8_t* dst, uint8_t* src) { |
136 | | #if defined(__ARM_NEON__) |
137 | | vst1q_u8(dst, vld1q_u8(src)); |
138 | | #else |
139 | 0 | uint32_t buffer[4]; |
140 | 0 | memcpy(buffer, src, 16); |
141 | 0 | memcpy(dst, buffer, 16); |
142 | 0 | #endif |
143 | 0 | } |
144 | | |
145 | | /* Decodes a number in the range [0..255], by reading 1 - 11 bits. */ |
146 | | static BROTLI_NOINLINE BrotliDecoderErrorCode DecodeVarLenUint8( |
147 | 0 | BrotliDecoderState* s, BrotliBitReader* br, uint32_t* value) { |
148 | 0 | uint32_t bits; |
149 | 0 | switch (s->substate_decode_uint8) { |
150 | 0 | case BROTLI_STATE_DECODE_UINT8_NONE: |
151 | 0 | if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, 1, &bits))) { |
152 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
153 | 0 | } |
154 | 0 | if (bits == 0) { |
155 | 0 | *value = 0; |
156 | 0 | return BROTLI_DECODER_SUCCESS; |
157 | 0 | } |
158 | | /* No break, transit to the next state. */ |
159 | | |
160 | 0 | case BROTLI_STATE_DECODE_UINT8_SHORT: |
161 | 0 | if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, 3, &bits))) { |
162 | 0 | s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_SHORT; |
163 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
164 | 0 | } |
165 | 0 | if (bits == 0) { |
166 | 0 | *value = 1; |
167 | 0 | s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE; |
168 | 0 | return BROTLI_DECODER_SUCCESS; |
169 | 0 | } |
170 | | /* Use output value as a temporary storage. It MUST be persisted. */ |
171 | 0 | *value = bits; |
172 | | /* No break, transit to the next state. */ |
173 | |
|
174 | 0 | case BROTLI_STATE_DECODE_UINT8_LONG: |
175 | 0 | if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, *value, &bits))) { |
176 | 0 | s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_LONG; |
177 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
178 | 0 | } |
179 | 0 | *value = (1U << *value) + bits; |
180 | 0 | s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE; |
181 | 0 | return BROTLI_DECODER_SUCCESS; |
182 | | |
183 | 0 | default: |
184 | 0 | return |
185 | 0 | BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); |
186 | 0 | } |
187 | 0 | } |
188 | | |
189 | | /* Decodes a metablock length and flags by reading 2 - 31 bits. */ |
190 | | static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength( |
191 | 0 | BrotliDecoderState* s, BrotliBitReader* br) { |
192 | 0 | uint32_t bits; |
193 | 0 | int i; |
194 | 0 | for (;;) { |
195 | 0 | switch (s->substate_metablock_header) { |
196 | 0 | case BROTLI_STATE_METABLOCK_HEADER_NONE: |
197 | 0 | if (!BrotliSafeReadBits(br, 1, &bits)) { |
198 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
199 | 0 | } |
200 | 0 | s->is_last_metablock = bits ? 1 : 0; |
201 | 0 | s->meta_block_remaining_len = 0; |
202 | 0 | s->is_uncompressed = 0; |
203 | 0 | s->is_metadata = 0; |
204 | 0 | if (!s->is_last_metablock) { |
205 | 0 | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NIBBLES; |
206 | 0 | break; |
207 | 0 | } |
208 | 0 | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_EMPTY; |
209 | | /* No break, transit to the next state. */ |
210 | |
|
211 | 0 | case BROTLI_STATE_METABLOCK_HEADER_EMPTY: |
212 | 0 | if (!BrotliSafeReadBits(br, 1, &bits)) { |
213 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
214 | 0 | } |
215 | 0 | if (bits) { |
216 | 0 | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; |
217 | 0 | return BROTLI_DECODER_SUCCESS; |
218 | 0 | } |
219 | 0 | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NIBBLES; |
220 | | /* No break, transit to the next state. */ |
221 | |
|
222 | 0 | case BROTLI_STATE_METABLOCK_HEADER_NIBBLES: |
223 | 0 | if (!BrotliSafeReadBits(br, 2, &bits)) { |
224 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
225 | 0 | } |
226 | 0 | s->size_nibbles = (uint8_t)(bits + 4); |
227 | 0 | s->loop_counter = 0; |
228 | 0 | if (bits == 3) { |
229 | 0 | s->is_metadata = 1; |
230 | 0 | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_RESERVED; |
231 | 0 | break; |
232 | 0 | } |
233 | 0 | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_SIZE; |
234 | | /* No break, transit to the next state. */ |
235 | |
|
236 | 0 | case BROTLI_STATE_METABLOCK_HEADER_SIZE: |
237 | 0 | i = s->loop_counter; |
238 | 0 | for (; i < (int)s->size_nibbles; ++i) { |
239 | 0 | if (!BrotliSafeReadBits(br, 4, &bits)) { |
240 | 0 | s->loop_counter = i; |
241 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
242 | 0 | } |
243 | 0 | if (i + 1 == s->size_nibbles && s->size_nibbles > 4 && bits == 0) { |
244 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE); |
245 | 0 | } |
246 | 0 | s->meta_block_remaining_len |= (int)(bits << (i * 4)); |
247 | 0 | } |
248 | 0 | s->substate_metablock_header = |
249 | 0 | BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED; |
250 | | /* No break, transit to the next state. */ |
251 | |
|
252 | 0 | case BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED: |
253 | 0 | if (!s->is_last_metablock) { |
254 | 0 | if (!BrotliSafeReadBits(br, 1, &bits)) { |
255 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
256 | 0 | } |
257 | 0 | s->is_uncompressed = bits ? 1 : 0; |
258 | 0 | } |
259 | 0 | ++s->meta_block_remaining_len; |
260 | 0 | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; |
261 | 0 | return BROTLI_DECODER_SUCCESS; |
262 | | |
263 | 0 | case BROTLI_STATE_METABLOCK_HEADER_RESERVED: |
264 | 0 | if (!BrotliSafeReadBits(br, 1, &bits)) { |
265 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
266 | 0 | } |
267 | 0 | if (bits != 0) { |
268 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_RESERVED); |
269 | 0 | } |
270 | 0 | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_BYTES; |
271 | | /* No break, transit to the next state. */ |
272 | |
|
273 | 0 | case BROTLI_STATE_METABLOCK_HEADER_BYTES: |
274 | 0 | if (!BrotliSafeReadBits(br, 2, &bits)) { |
275 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
276 | 0 | } |
277 | 0 | if (bits == 0) { |
278 | 0 | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; |
279 | 0 | return BROTLI_DECODER_SUCCESS; |
280 | 0 | } |
281 | 0 | s->size_nibbles = (uint8_t)bits; |
282 | 0 | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_METADATA; |
283 | | /* No break, transit to the next state. */ |
284 | |
|
285 | 0 | case BROTLI_STATE_METABLOCK_HEADER_METADATA: |
286 | 0 | i = s->loop_counter; |
287 | 0 | for (; i < (int)s->size_nibbles; ++i) { |
288 | 0 | if (!BrotliSafeReadBits(br, 8, &bits)) { |
289 | 0 | s->loop_counter = i; |
290 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
291 | 0 | } |
292 | 0 | if (i + 1 == s->size_nibbles && s->size_nibbles > 1 && bits == 0) { |
293 | 0 | return BROTLI_FAILURE( |
294 | 0 | BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE); |
295 | 0 | } |
296 | 0 | s->meta_block_remaining_len |= (int)(bits << (i * 8)); |
297 | 0 | } |
298 | 0 | ++s->meta_block_remaining_len; |
299 | 0 | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; |
300 | 0 | return BROTLI_DECODER_SUCCESS; |
301 | | |
302 | 0 | default: |
303 | 0 | return |
304 | 0 | BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); |
305 | 0 | } |
306 | 0 | } |
307 | 0 | } |
308 | | |
309 | | /* Decodes the Huffman code. |
310 | | This method doesn't read data from the bit reader, BUT drops the amount of |
311 | | bits that correspond to the decoded symbol. |
312 | | bits MUST contain at least 15 (BROTLI_HUFFMAN_MAX_CODE_LENGTH) valid bits. */ |
313 | | static BROTLI_INLINE uint32_t DecodeSymbol(uint32_t bits, |
314 | | const HuffmanCode* table, |
315 | 0 | BrotliBitReader* br) { |
316 | 0 | table += bits & HUFFMAN_TABLE_MASK; |
317 | 0 | if (table->bits > HUFFMAN_TABLE_BITS) { |
318 | 0 | uint32_t nbits = table->bits - HUFFMAN_TABLE_BITS; |
319 | 0 | BrotliDropBits(br, HUFFMAN_TABLE_BITS); |
320 | 0 | table += table->value; |
321 | 0 | table += (bits >> HUFFMAN_TABLE_BITS) & BitMask(nbits); |
322 | 0 | } |
323 | 0 | BrotliDropBits(br, table->bits); |
324 | 0 | return table->value; |
325 | 0 | } |
326 | | |
327 | | /* Reads and decodes the next Huffman code from bit-stream. |
328 | | This method peeks 16 bits of input and drops 0 - 15 of them. */ |
329 | | static BROTLI_INLINE uint32_t ReadSymbol(const HuffmanCode* table, |
330 | 0 | BrotliBitReader* br) { |
331 | 0 | return DecodeSymbol(BrotliGet16BitsUnmasked(br), table, br); |
332 | 0 | } |
333 | | |
334 | | /* Same as DecodeSymbol, but it is known that there is less than 15 bits of |
335 | | input are currently available. */ |
336 | | static BROTLI_NOINLINE BROTLI_BOOL SafeDecodeSymbol( |
337 | 0 | const HuffmanCode* table, BrotliBitReader* br, uint32_t* result) { |
338 | 0 | uint32_t val; |
339 | 0 | uint32_t available_bits = BrotliGetAvailableBits(br); |
340 | 0 | if (available_bits == 0) { |
341 | 0 | if (table->bits == 0) { |
342 | 0 | *result = table->value; |
343 | 0 | return BROTLI_TRUE; |
344 | 0 | } |
345 | 0 | return BROTLI_FALSE; /* No valid bits at all. */ |
346 | 0 | } |
347 | 0 | val = (uint32_t)BrotliGetBitsUnmasked(br); |
348 | 0 | table += val & HUFFMAN_TABLE_MASK; |
349 | 0 | if (table->bits <= HUFFMAN_TABLE_BITS) { |
350 | 0 | if (table->bits <= available_bits) { |
351 | 0 | BrotliDropBits(br, table->bits); |
352 | 0 | *result = table->value; |
353 | 0 | return BROTLI_TRUE; |
354 | 0 | } else { |
355 | 0 | return BROTLI_FALSE; /* Not enough bits for the first level. */ |
356 | 0 | } |
357 | 0 | } |
358 | 0 | if (available_bits <= HUFFMAN_TABLE_BITS) { |
359 | 0 | return BROTLI_FALSE; /* Not enough bits to move to the second level. */ |
360 | 0 | } |
361 | | |
362 | | /* Speculatively drop HUFFMAN_TABLE_BITS. */ |
363 | 0 | val = (val & BitMask(table->bits)) >> HUFFMAN_TABLE_BITS; |
364 | 0 | available_bits -= HUFFMAN_TABLE_BITS; |
365 | 0 | table += table->value + val; |
366 | 0 | if (available_bits < table->bits) { |
367 | 0 | return BROTLI_FALSE; /* Not enough bits for the second level. */ |
368 | 0 | } |
369 | | |
370 | 0 | BrotliDropBits(br, HUFFMAN_TABLE_BITS + table->bits); |
371 | 0 | *result = table->value; |
372 | 0 | return BROTLI_TRUE; |
373 | 0 | } |
374 | | |
375 | | static BROTLI_INLINE BROTLI_BOOL SafeReadSymbol( |
376 | 0 | const HuffmanCode* table, BrotliBitReader* br, uint32_t* result) { |
377 | 0 | uint32_t val; |
378 | 0 | if (BROTLI_PREDICT_TRUE(BrotliSafeGetBits(br, 15, &val))) { |
379 | 0 | *result = DecodeSymbol(val, table, br); |
380 | 0 | return BROTLI_TRUE; |
381 | 0 | } |
382 | 0 | return SafeDecodeSymbol(table, br, result); |
383 | 0 | } |
384 | | |
385 | | /* Makes a look-up in first level Huffman table. Peeks 8 bits. */ |
386 | | static BROTLI_INLINE void PreloadSymbol(int safe, |
387 | | const HuffmanCode* table, |
388 | | BrotliBitReader* br, |
389 | | uint32_t* bits, |
390 | 0 | uint32_t* value) { |
391 | 0 | if (safe) { |
392 | 0 | return; |
393 | 0 | } |
394 | 0 | table += BrotliGetBits(br, HUFFMAN_TABLE_BITS); |
395 | 0 | *bits = table->bits; |
396 | 0 | *value = table->value; |
397 | 0 | } |
398 | | |
399 | | /* Decodes the next Huffman code using data prepared by PreloadSymbol. |
400 | | Reads 0 - 15 bits. Also peeks 8 following bits. */ |
401 | | static BROTLI_INLINE uint32_t ReadPreloadedSymbol(const HuffmanCode* table, |
402 | | BrotliBitReader* br, |
403 | | uint32_t* bits, |
404 | 0 | uint32_t* value) { |
405 | 0 | uint32_t result = *value; |
406 | 0 | if (BROTLI_PREDICT_FALSE(*bits > HUFFMAN_TABLE_BITS)) { |
407 | 0 | uint32_t val = BrotliGet16BitsUnmasked(br); |
408 | 0 | const HuffmanCode* ext = table + (val & HUFFMAN_TABLE_MASK) + *value; |
409 | 0 | uint32_t mask = BitMask((*bits - HUFFMAN_TABLE_BITS)); |
410 | 0 | BrotliDropBits(br, HUFFMAN_TABLE_BITS); |
411 | 0 | ext += (val >> HUFFMAN_TABLE_BITS) & mask; |
412 | 0 | BrotliDropBits(br, ext->bits); |
413 | 0 | result = ext->value; |
414 | 0 | } else { |
415 | 0 | BrotliDropBits(br, *bits); |
416 | 0 | } |
417 | 0 | PreloadSymbol(0, table, br, bits, value); |
418 | 0 | return result; |
419 | 0 | } |
420 | | |
421 | 0 | static BROTLI_INLINE uint32_t Log2Floor(uint32_t x) { |
422 | 0 | uint32_t result = 0; |
423 | 0 | while (x) { |
424 | 0 | x >>= 1; |
425 | 0 | ++result; |
426 | 0 | } |
427 | 0 | return result; |
428 | 0 | } |
429 | | |
430 | | /* Reads (s->symbol + 1) symbols. |
431 | | Totally 1..4 symbols are read, 1..10 bits each. |
432 | | The list of symbols MUST NOT contain duplicates. |
433 | | */ |
434 | | static BrotliDecoderErrorCode ReadSimpleHuffmanSymbols( |
435 | 0 | uint32_t alphabet_size, BrotliDecoderState* s) { |
436 | | /* max_bits == 1..10; symbol == 0..3; 1..40 bits will be read. */ |
437 | 0 | BrotliBitReader* br = &s->br; |
438 | 0 | uint32_t max_bits = Log2Floor(alphabet_size - 1); |
439 | 0 | uint32_t i = s->sub_loop_counter; |
440 | 0 | uint32_t num_symbols = s->symbol; |
441 | 0 | while (i <= num_symbols) { |
442 | 0 | uint32_t v; |
443 | 0 | if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, max_bits, &v))) { |
444 | 0 | s->sub_loop_counter = i; |
445 | 0 | s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_READ; |
446 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
447 | 0 | } |
448 | 0 | if (v >= alphabet_size) { |
449 | 0 | return |
450 | 0 | BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET); |
451 | 0 | } |
452 | 0 | s->symbols_lists_array[i] = (uint16_t)v; |
453 | 0 | BROTLI_LOG_UINT(s->symbols_lists_array[i]); |
454 | 0 | ++i; |
455 | 0 | } |
456 | | |
457 | 0 | for (i = 0; i < num_symbols; ++i) { |
458 | 0 | uint32_t k = i + 1; |
459 | 0 | for (; k <= num_symbols; ++k) { |
460 | 0 | if (s->symbols_lists_array[i] == s->symbols_lists_array[k]) { |
461 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME); |
462 | 0 | } |
463 | 0 | } |
464 | 0 | } |
465 | | |
466 | 0 | return BROTLI_DECODER_SUCCESS; |
467 | 0 | } |
468 | | |
469 | | /* Process single decoded symbol code length: |
470 | | A) reset the repeat variable |
471 | | B) remember code length (if it is not 0) |
472 | | C) extend corresponding index-chain |
473 | | D) reduce the Huffman space |
474 | | E) update the histogram |
475 | | */ |
476 | | static BROTLI_INLINE void ProcessSingleCodeLength(uint32_t code_len, |
477 | | uint32_t* symbol, uint32_t* repeat, uint32_t* space, |
478 | | uint32_t* prev_code_len, uint16_t* symbol_lists, |
479 | 0 | uint16_t* code_length_histo, int* next_symbol) { |
480 | 0 | *repeat = 0; |
481 | 0 | if (code_len != 0) { /* code_len == 1..15 */ |
482 | 0 | symbol_lists[next_symbol[code_len]] = (uint16_t)(*symbol); |
483 | 0 | next_symbol[code_len] = (int)(*symbol); |
484 | 0 | *prev_code_len = code_len; |
485 | 0 | *space -= 32768U >> code_len; |
486 | 0 | code_length_histo[code_len]++; |
487 | 0 | BROTLI_LOG(("[ReadHuffmanCode] code_length[%d] = %d\n", *symbol, code_len)); |
488 | 0 | } |
489 | 0 | (*symbol)++; |
490 | 0 | } |
491 | | |
492 | | /* Process repeated symbol code length. |
493 | | A) Check if it is the extension of previous repeat sequence; if the decoded |
494 | | value is not BROTLI_REPEAT_PREVIOUS_CODE_LENGTH, then it is a new |
495 | | symbol-skip |
496 | | B) Update repeat variable |
497 | | C) Check if operation is feasible (fits alphabet) |
498 | | D) For each symbol do the same operations as in ProcessSingleCodeLength |
499 | | |
500 | | PRECONDITION: code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH or |
501 | | code_len == BROTLI_REPEAT_ZERO_CODE_LENGTH |
502 | | */ |
503 | | static BROTLI_INLINE void ProcessRepeatedCodeLength(uint32_t code_len, |
504 | | uint32_t repeat_delta, uint32_t alphabet_size, uint32_t* symbol, |
505 | | uint32_t* repeat, uint32_t* space, uint32_t* prev_code_len, |
506 | | uint32_t* repeat_code_len, uint16_t* symbol_lists, |
507 | 0 | uint16_t* code_length_histo, int* next_symbol) { |
508 | 0 | uint32_t old_repeat; |
509 | 0 | uint32_t extra_bits = 3; /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */ |
510 | 0 | uint32_t new_len = 0; /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */ |
511 | 0 | if (code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) { |
512 | 0 | new_len = *prev_code_len; |
513 | 0 | extra_bits = 2; |
514 | 0 | } |
515 | 0 | if (*repeat_code_len != new_len) { |
516 | 0 | *repeat = 0; |
517 | 0 | *repeat_code_len = new_len; |
518 | 0 | } |
519 | 0 | old_repeat = *repeat; |
520 | 0 | if (*repeat > 0) { |
521 | 0 | *repeat -= 2; |
522 | 0 | *repeat <<= extra_bits; |
523 | 0 | } |
524 | 0 | *repeat += repeat_delta + 3U; |
525 | 0 | repeat_delta = *repeat - old_repeat; |
526 | 0 | if (*symbol + repeat_delta > alphabet_size) { |
527 | 0 | BROTLI_DUMP(); |
528 | 0 | *symbol = alphabet_size; |
529 | 0 | *space = 0xFFFFF; |
530 | 0 | return; |
531 | 0 | } |
532 | 0 | BROTLI_LOG(("[ReadHuffmanCode] code_length[%d..%d] = %d\n", |
533 | 0 | *symbol, *symbol + repeat_delta - 1, *repeat_code_len)); |
534 | 0 | if (*repeat_code_len != 0) { |
535 | 0 | unsigned last = *symbol + repeat_delta; |
536 | 0 | int next = next_symbol[*repeat_code_len]; |
537 | 0 | do { |
538 | 0 | symbol_lists[next] = (uint16_t)*symbol; |
539 | 0 | next = (int)*symbol; |
540 | 0 | } while (++(*symbol) != last); |
541 | 0 | next_symbol[*repeat_code_len] = next; |
542 | 0 | *space -= repeat_delta << (15 - *repeat_code_len); |
543 | 0 | code_length_histo[*repeat_code_len] = |
544 | 0 | (uint16_t)(code_length_histo[*repeat_code_len] + repeat_delta); |
545 | 0 | } else { |
546 | 0 | *symbol += repeat_delta; |
547 | 0 | } |
548 | 0 | } |
549 | | |
550 | | /* Reads and decodes symbol codelengths. */ |
551 | | static BrotliDecoderErrorCode ReadSymbolCodeLengths( |
552 | 0 | uint32_t alphabet_size, BrotliDecoderState* s) { |
553 | 0 | BrotliBitReader* br = &s->br; |
554 | 0 | uint32_t symbol = s->symbol; |
555 | 0 | uint32_t repeat = s->repeat; |
556 | 0 | uint32_t space = s->space; |
557 | 0 | uint32_t prev_code_len = s->prev_code_len; |
558 | 0 | uint32_t repeat_code_len = s->repeat_code_len; |
559 | 0 | uint16_t* symbol_lists = s->symbol_lists; |
560 | 0 | uint16_t* code_length_histo = s->code_length_histo; |
561 | 0 | int* next_symbol = s->next_symbol; |
562 | 0 | if (!BrotliWarmupBitReader(br)) { |
563 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
564 | 0 | } |
565 | 0 | while (symbol < alphabet_size && space > 0) { |
566 | 0 | const HuffmanCode* p = s->table; |
567 | 0 | uint32_t code_len; |
568 | 0 | if (!BrotliCheckInputAmount(br, BROTLI_SHORT_FILL_BIT_WINDOW_READ)) { |
569 | 0 | s->symbol = symbol; |
570 | 0 | s->repeat = repeat; |
571 | 0 | s->prev_code_len = prev_code_len; |
572 | 0 | s->repeat_code_len = repeat_code_len; |
573 | 0 | s->space = space; |
574 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
575 | 0 | } |
576 | 0 | BrotliFillBitWindow16(br); |
577 | 0 | p += BrotliGetBitsUnmasked(br) & |
578 | 0 | BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH); |
579 | 0 | BrotliDropBits(br, p->bits); /* Use 1..5 bits */ |
580 | 0 | code_len = p->value; /* code_len == 0..17 */ |
581 | 0 | if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) { |
582 | 0 | ProcessSingleCodeLength(code_len, &symbol, &repeat, &space, |
583 | 0 | &prev_code_len, symbol_lists, code_length_histo, next_symbol); |
584 | 0 | } else { /* code_len == 16..17, extra_bits == 2..3 */ |
585 | 0 | uint32_t extra_bits = |
586 | 0 | (code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) ? 2 : 3; |
587 | 0 | uint32_t repeat_delta = |
588 | 0 | (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(extra_bits); |
589 | 0 | BrotliDropBits(br, extra_bits); |
590 | 0 | ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size, |
591 | 0 | &symbol, &repeat, &space, &prev_code_len, &repeat_code_len, |
592 | 0 | symbol_lists, code_length_histo, next_symbol); |
593 | 0 | } |
594 | 0 | } |
595 | 0 | s->space = space; |
596 | 0 | return BROTLI_DECODER_SUCCESS; |
597 | 0 | } |
598 | | |
599 | | static BrotliDecoderErrorCode SafeReadSymbolCodeLengths( |
600 | 0 | uint32_t alphabet_size, BrotliDecoderState* s) { |
601 | 0 | BrotliBitReader* br = &s->br; |
602 | 0 | BROTLI_BOOL get_byte = BROTLI_FALSE; |
603 | 0 | while (s->symbol < alphabet_size && s->space > 0) { |
604 | 0 | const HuffmanCode* p = s->table; |
605 | 0 | uint32_t code_len; |
606 | 0 | uint32_t available_bits; |
607 | 0 | uint32_t bits = 0; |
608 | 0 | if (get_byte && !BrotliPullByte(br)) return BROTLI_DECODER_NEEDS_MORE_INPUT; |
609 | 0 | get_byte = BROTLI_FALSE; |
610 | 0 | available_bits = BrotliGetAvailableBits(br); |
611 | 0 | if (available_bits != 0) { |
612 | 0 | bits = (uint32_t)BrotliGetBitsUnmasked(br); |
613 | 0 | } |
614 | 0 | p += bits & BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH); |
615 | 0 | if (p->bits > available_bits) { |
616 | 0 | get_byte = BROTLI_TRUE; |
617 | 0 | continue; |
618 | 0 | } |
619 | 0 | code_len = p->value; /* code_len == 0..17 */ |
620 | 0 | if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) { |
621 | 0 | BrotliDropBits(br, p->bits); |
622 | 0 | ProcessSingleCodeLength(code_len, &s->symbol, &s->repeat, &s->space, |
623 | 0 | &s->prev_code_len, s->symbol_lists, s->code_length_histo, |
624 | 0 | s->next_symbol); |
625 | 0 | } else { /* code_len == 16..17, extra_bits == 2..3 */ |
626 | 0 | uint32_t extra_bits = code_len - 14U; |
627 | 0 | uint32_t repeat_delta = (bits >> p->bits) & BitMask(extra_bits); |
628 | 0 | if (available_bits < p->bits + extra_bits) { |
629 | 0 | get_byte = BROTLI_TRUE; |
630 | 0 | continue; |
631 | 0 | } |
632 | 0 | BrotliDropBits(br, p->bits + extra_bits); |
633 | 0 | ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size, |
634 | 0 | &s->symbol, &s->repeat, &s->space, &s->prev_code_len, |
635 | 0 | &s->repeat_code_len, s->symbol_lists, s->code_length_histo, |
636 | 0 | s->next_symbol); |
637 | 0 | } |
638 | 0 | } |
639 | 0 | return BROTLI_DECODER_SUCCESS; |
640 | 0 | } |
641 | | |
642 | | /* Reads and decodes 15..18 codes using static prefix code. |
643 | | Each code is 2..4 bits long. In total 30..72 bits are used. */ |
644 | 0 | static BrotliDecoderErrorCode ReadCodeLengthCodeLengths(BrotliDecoderState* s) { |
645 | 0 | BrotliBitReader* br = &s->br; |
646 | 0 | uint32_t num_codes = s->repeat; |
647 | 0 | unsigned space = s->space; |
648 | 0 | uint32_t i = s->sub_loop_counter; |
649 | 0 | for (; i < BROTLI_CODE_LENGTH_CODES; ++i) { |
650 | 0 | const uint8_t code_len_idx = kCodeLengthCodeOrder[i]; |
651 | 0 | uint32_t ix; |
652 | 0 | uint32_t v; |
653 | 0 | if (BROTLI_PREDICT_FALSE(!BrotliSafeGetBits(br, 4, &ix))) { |
654 | 0 | uint32_t available_bits = BrotliGetAvailableBits(br); |
655 | 0 | if (available_bits != 0) { |
656 | 0 | ix = BrotliGetBitsUnmasked(br) & 0xF; |
657 | 0 | } else { |
658 | 0 | ix = 0; |
659 | 0 | } |
660 | 0 | if (kCodeLengthPrefixLength[ix] > available_bits) { |
661 | 0 | s->sub_loop_counter = i; |
662 | 0 | s->repeat = num_codes; |
663 | 0 | s->space = space; |
664 | 0 | s->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX; |
665 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
666 | 0 | } |
667 | 0 | } |
668 | 0 | v = kCodeLengthPrefixValue[ix]; |
669 | 0 | BrotliDropBits(br, kCodeLengthPrefixLength[ix]); |
670 | 0 | s->code_length_code_lengths[code_len_idx] = (uint8_t)v; |
671 | 0 | BROTLI_LOG_ARRAY_INDEX(s->code_length_code_lengths, code_len_idx); |
672 | 0 | if (v != 0) { |
673 | 0 | space = space - (32U >> v); |
674 | 0 | ++num_codes; |
675 | 0 | ++s->code_length_histo[v]; |
676 | 0 | if (space - 1U >= 32U) { |
677 | | /* space is 0 or wrapped around */ |
678 | 0 | break; |
679 | 0 | } |
680 | 0 | } |
681 | 0 | } |
682 | 0 | if (!(num_codes == 1 || space == 0)) { |
683 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_CL_SPACE); |
684 | 0 | } |
685 | 0 | return BROTLI_DECODER_SUCCESS; |
686 | 0 | } |
687 | | |
688 | | /* Decodes the Huffman tables. |
689 | | There are 2 scenarios: |
690 | | A) Huffman code contains only few symbols (1..4). Those symbols are read |
691 | | directly; their code lengths are defined by the number of symbols. |
692 | | For this scenario 4 - 45 bits will be read. |
693 | | |
694 | | B) 2-phase decoding: |
695 | | B.1) Small Huffman table is decoded; it is specified with code lengths |
696 | | encoded with predefined entropy code. 32 - 74 bits are used. |
697 | | B.2) Decoded table is used to decode code lengths of symbols in resulting |
698 | | Huffman table. In worst case 3520 bits are read. |
699 | | */ |
700 | | static BrotliDecoderErrorCode ReadHuffmanCode(uint32_t alphabet_size, |
701 | | HuffmanCode* table, |
702 | | uint32_t* opt_table_size, |
703 | 0 | BrotliDecoderState* s) { |
704 | 0 | BrotliBitReader* br = &s->br; |
705 | | /* Unnecessary masking, but might be good for safety. */ |
706 | 0 | alphabet_size &= 0x3ff; |
707 | | /* State machine */ |
708 | 0 | for (;;) { |
709 | 0 | switch (s->substate_huffman) { |
710 | 0 | case BROTLI_STATE_HUFFMAN_NONE: |
711 | 0 | if (!BrotliSafeReadBits(br, 2, &s->sub_loop_counter)) { |
712 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
713 | 0 | } |
714 | 0 | BROTLI_LOG_UINT(s->sub_loop_counter); |
715 | | /* The value is used as follows: |
716 | | 1 for simple code; |
717 | | 0 for no skipping, 2 skips 2 code lengths, 3 skips 3 code lengths */ |
718 | 0 | if (s->sub_loop_counter != 1) { |
719 | 0 | s->space = 32; |
720 | 0 | s->repeat = 0; /* num_codes */ |
721 | 0 | memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo[0]) * |
722 | 0 | (BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1)); |
723 | 0 | memset(&s->code_length_code_lengths[0], 0, |
724 | 0 | sizeof(s->code_length_code_lengths)); |
725 | 0 | s->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX; |
726 | 0 | continue; |
727 | 0 | } |
728 | | /* No break, transit to the next state. */ |
729 | | |
730 | 0 | case BROTLI_STATE_HUFFMAN_SIMPLE_SIZE: |
731 | | /* Read symbols, codes & code lengths directly. */ |
732 | 0 | if (!BrotliSafeReadBits(br, 2, &s->symbol)) { /* num_symbols */ |
733 | 0 | s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_SIZE; |
734 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
735 | 0 | } |
736 | 0 | s->sub_loop_counter = 0; |
737 | | /* No break, transit to the next state. */ |
738 | 0 | case BROTLI_STATE_HUFFMAN_SIMPLE_READ: { |
739 | 0 | BrotliDecoderErrorCode result = |
740 | 0 | ReadSimpleHuffmanSymbols(alphabet_size, s); |
741 | 0 | if (result != BROTLI_DECODER_SUCCESS) { |
742 | 0 | return result; |
743 | 0 | } |
744 | | /* No break, transit to the next state. */ |
745 | 0 | } |
746 | 0 | case BROTLI_STATE_HUFFMAN_SIMPLE_BUILD: { |
747 | 0 | uint32_t table_size; |
748 | 0 | if (s->symbol == 3) { |
749 | 0 | uint32_t bits; |
750 | 0 | if (!BrotliSafeReadBits(br, 1, &bits)) { |
751 | 0 | s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_BUILD; |
752 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
753 | 0 | } |
754 | 0 | s->symbol += bits; |
755 | 0 | } |
756 | 0 | BROTLI_LOG_UINT(s->symbol); |
757 | 0 | table_size = BrotliBuildSimpleHuffmanTable( |
758 | 0 | table, HUFFMAN_TABLE_BITS, s->symbols_lists_array, s->symbol); |
759 | 0 | if (opt_table_size) { |
760 | 0 | *opt_table_size = table_size; |
761 | 0 | } |
762 | 0 | s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE; |
763 | 0 | return BROTLI_DECODER_SUCCESS; |
764 | 0 | } |
765 | | |
766 | | /* Decode Huffman-coded code lengths. */ |
767 | 0 | case BROTLI_STATE_HUFFMAN_COMPLEX: { |
768 | 0 | uint32_t i; |
769 | 0 | BrotliDecoderErrorCode result = ReadCodeLengthCodeLengths(s); |
770 | 0 | if (result != BROTLI_DECODER_SUCCESS) { |
771 | 0 | return result; |
772 | 0 | } |
773 | 0 | BrotliBuildCodeLengthsHuffmanTable(s->table, |
774 | 0 | s->code_length_code_lengths, |
775 | 0 | s->code_length_histo); |
776 | 0 | memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo)); |
777 | 0 | for (i = 0; i <= BROTLI_HUFFMAN_MAX_CODE_LENGTH; ++i) { |
778 | 0 | s->next_symbol[i] = (int)i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1); |
779 | 0 | s->symbol_lists[s->next_symbol[i]] = 0xFFFF; |
780 | 0 | } |
781 | |
|
782 | 0 | s->symbol = 0; |
783 | 0 | s->prev_code_len = BROTLI_INITIAL_REPEATED_CODE_LENGTH; |
784 | 0 | s->repeat = 0; |
785 | 0 | s->repeat_code_len = 0; |
786 | 0 | s->space = 32768; |
787 | 0 | s->substate_huffman = BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS; |
788 | | /* No break, transit to the next state. */ |
789 | 0 | } |
790 | 0 | case BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS: { |
791 | 0 | uint32_t table_size; |
792 | 0 | BrotliDecoderErrorCode result = ReadSymbolCodeLengths(alphabet_size, s); |
793 | 0 | if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) { |
794 | 0 | result = SafeReadSymbolCodeLengths(alphabet_size, s); |
795 | 0 | } |
796 | 0 | if (result != BROTLI_DECODER_SUCCESS) { |
797 | 0 | return result; |
798 | 0 | } |
799 | | |
800 | 0 | if (s->space != 0) { |
801 | 0 | BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", s->space)); |
802 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE); |
803 | 0 | } |
804 | 0 | table_size = BrotliBuildHuffmanTable( |
805 | 0 | table, HUFFMAN_TABLE_BITS, s->symbol_lists, s->code_length_histo); |
806 | 0 | if (opt_table_size) { |
807 | 0 | *opt_table_size = table_size; |
808 | 0 | } |
809 | 0 | s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE; |
810 | 0 | return BROTLI_DECODER_SUCCESS; |
811 | 0 | } |
812 | | |
813 | 0 | default: |
814 | 0 | return |
815 | 0 | BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); |
816 | 0 | } |
817 | 0 | } |
818 | 0 | } |
819 | | |
820 | | /* Decodes a block length by reading 3..39 bits. */ |
821 | | static BROTLI_INLINE uint32_t ReadBlockLength(const HuffmanCode* table, |
822 | 0 | BrotliBitReader* br) { |
823 | 0 | uint32_t code; |
824 | 0 | uint32_t nbits; |
825 | 0 | code = ReadSymbol(table, br); |
826 | 0 | nbits = kBlockLengthPrefixCode[code].nbits; /* nbits == 2..24 */ |
827 | 0 | return kBlockLengthPrefixCode[code].offset + BrotliReadBits(br, nbits); |
828 | 0 | } |
829 | | |
830 | | /* WARNING: if state is not BROTLI_STATE_READ_BLOCK_LENGTH_NONE, then |
831 | | reading can't be continued with ReadBlockLength. */ |
832 | | static BROTLI_INLINE BROTLI_BOOL SafeReadBlockLength( |
833 | | BrotliDecoderState* s, uint32_t* result, const HuffmanCode* table, |
834 | 0 | BrotliBitReader* br) { |
835 | 0 | uint32_t index; |
836 | 0 | if (s->substate_read_block_length == BROTLI_STATE_READ_BLOCK_LENGTH_NONE) { |
837 | 0 | if (!SafeReadSymbol(table, br, &index)) { |
838 | 0 | return BROTLI_FALSE; |
839 | 0 | } |
840 | 0 | } else { |
841 | 0 | index = s->block_length_index; |
842 | 0 | } |
843 | 0 | { |
844 | 0 | uint32_t bits; |
845 | 0 | uint32_t nbits = kBlockLengthPrefixCode[index].nbits; /* nbits == 2..24 */ |
846 | 0 | if (!BrotliSafeReadBits(br, nbits, &bits)) { |
847 | 0 | s->block_length_index = index; |
848 | 0 | s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX; |
849 | 0 | return BROTLI_FALSE; |
850 | 0 | } |
851 | 0 | *result = kBlockLengthPrefixCode[index].offset + bits; |
852 | 0 | s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE; |
853 | 0 | return BROTLI_TRUE; |
854 | 0 | } |
855 | 0 | } |
856 | | |
857 | | /* Transform: |
858 | | 1) initialize list L with values 0, 1,... 255 |
859 | | 2) For each input element X: |
860 | | 2.1) let Y = L[X] |
861 | | 2.2) remove X-th element from L |
862 | | 2.3) prepend Y to L |
863 | | 2.4) append Y to output |
864 | | |
865 | | In most cases max(Y) <= 7, so most of L remains intact. |
866 | | To reduce the cost of initialization, we reuse L, remember the upper bound |
867 | | of Y values, and reinitialize only first elements in L. |
868 | | |
869 | | Most of input values are 0 and 1. To reduce number of branches, we replace |
870 | | inner for loop with do-while. |
871 | | */ |
872 | | static BROTLI_NOINLINE void InverseMoveToFrontTransform( |
873 | 0 | uint8_t* v, uint32_t v_len, BrotliDecoderState* state) { |
874 | | /* Reinitialize elements that could have been changed. */ |
875 | 0 | uint32_t i = 1; |
876 | 0 | uint32_t upper_bound = state->mtf_upper_bound; |
877 | 0 | uint32_t* mtf = &state->mtf[1]; /* Make mtf[-1] addressable. */ |
878 | 0 | uint8_t* mtf_u8 = (uint8_t*)mtf; |
879 | | /* Load endian-aware constant. */ |
880 | 0 | const uint8_t b0123[4] = {0, 1, 2, 3}; |
881 | 0 | uint32_t pattern; |
882 | 0 | memcpy(&pattern, &b0123, 4); |
883 | | |
884 | | /* Initialize list using 4 consequent values pattern. */ |
885 | 0 | mtf[0] = pattern; |
886 | 0 | do { |
887 | 0 | pattern += 0x04040404; /* Advance all 4 values by 4. */ |
888 | 0 | mtf[i] = pattern; |
889 | 0 | i++; |
890 | 0 | } while (i <= upper_bound); |
891 | | |
892 | | /* Transform the input. */ |
893 | 0 | upper_bound = 0; |
894 | 0 | for (i = 0; i < v_len; ++i) { |
895 | 0 | int index = v[i]; |
896 | 0 | uint8_t value = mtf_u8[index]; |
897 | 0 | upper_bound |= v[i]; |
898 | 0 | v[i] = value; |
899 | 0 | mtf_u8[-1] = value; |
900 | 0 | do { |
901 | 0 | index--; |
902 | 0 | mtf_u8[index + 1] = mtf_u8[index]; |
903 | 0 | } while (index >= 0); |
904 | 0 | } |
905 | | /* Remember amount of elements to be reinitialized. */ |
906 | 0 | state->mtf_upper_bound = upper_bound >> 2; |
907 | 0 | } |
908 | | |
909 | | /* Decodes a series of Huffman table using ReadHuffmanCode function. */ |
910 | | static BrotliDecoderErrorCode HuffmanTreeGroupDecode( |
911 | 0 | HuffmanTreeGroup* group, BrotliDecoderState* s) { |
912 | 0 | if (s->substate_tree_group != BROTLI_STATE_TREE_GROUP_LOOP) { |
913 | 0 | s->next = group->codes; |
914 | 0 | s->htree_index = 0; |
915 | 0 | s->substate_tree_group = BROTLI_STATE_TREE_GROUP_LOOP; |
916 | 0 | } |
917 | 0 | while (s->htree_index < group->num_htrees) { |
918 | 0 | uint32_t table_size; |
919 | 0 | BrotliDecoderErrorCode result = |
920 | 0 | ReadHuffmanCode(group->alphabet_size, s->next, &table_size, s); |
921 | 0 | if (result != BROTLI_DECODER_SUCCESS) return result; |
922 | 0 | group->htrees[s->htree_index] = s->next; |
923 | 0 | s->next += table_size; |
924 | 0 | ++s->htree_index; |
925 | 0 | } |
926 | 0 | s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE; |
927 | 0 | return BROTLI_DECODER_SUCCESS; |
928 | 0 | } |
929 | | |
930 | | /* Decodes a context map. |
931 | | Decoding is done in 4 phases: |
932 | | 1) Read auxiliary information (6..16 bits) and allocate memory. |
933 | | In case of trivial context map, decoding is finished at this phase. |
934 | | 2) Decode Huffman table using ReadHuffmanCode function. |
935 | | This table will be used for reading context map items. |
936 | | 3) Read context map items; "0" values could be run-length encoded. |
937 | | 4) Optionally, apply InverseMoveToFront transform to the resulting map. |
938 | | */ |
939 | | static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size, |
940 | | uint32_t* num_htrees, |
941 | | uint8_t** context_map_arg, |
942 | 0 | BrotliDecoderState* s) { |
943 | 0 | BrotliBitReader* br = &s->br; |
944 | 0 | BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS; |
945 | |
|
946 | 0 | switch ((int)s->substate_context_map) { |
947 | 0 | case BROTLI_STATE_CONTEXT_MAP_NONE: |
948 | 0 | result = DecodeVarLenUint8(s, br, num_htrees); |
949 | 0 | if (result != BROTLI_DECODER_SUCCESS) { |
950 | 0 | return result; |
951 | 0 | } |
952 | 0 | (*num_htrees)++; |
953 | 0 | s->context_index = 0; |
954 | 0 | BROTLI_LOG_UINT(context_map_size); |
955 | 0 | BROTLI_LOG_UINT(*num_htrees); |
956 | 0 | *context_map_arg = (uint8_t*)BROTLI_ALLOC(s, (size_t)context_map_size); |
957 | 0 | if (*context_map_arg == 0) { |
958 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP); |
959 | 0 | } |
960 | 0 | if (*num_htrees <= 1) { |
961 | 0 | memset(*context_map_arg, 0, (size_t)context_map_size); |
962 | 0 | return BROTLI_DECODER_SUCCESS; |
963 | 0 | } |
964 | 0 | s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_READ_PREFIX; |
965 | | /* No break, continue to next state. */ |
966 | 0 | case BROTLI_STATE_CONTEXT_MAP_READ_PREFIX: { |
967 | 0 | uint32_t bits; |
968 | | /* In next stage ReadHuffmanCode uses at least 4 bits, so it is safe |
969 | | to peek 4 bits ahead. */ |
970 | 0 | if (!BrotliSafeGetBits(br, 5, &bits)) { |
971 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
972 | 0 | } |
973 | 0 | if ((bits & 1) != 0) { /* Use RLE for zeros. */ |
974 | 0 | s->max_run_length_prefix = (bits >> 1) + 1; |
975 | 0 | BrotliDropBits(br, 5); |
976 | 0 | } else { |
977 | 0 | s->max_run_length_prefix = 0; |
978 | 0 | BrotliDropBits(br, 1); |
979 | 0 | } |
980 | 0 | BROTLI_LOG_UINT(s->max_run_length_prefix); |
981 | 0 | s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_HUFFMAN; |
982 | | /* No break, continue to next state. */ |
983 | 0 | } |
984 | 0 | case BROTLI_STATE_CONTEXT_MAP_HUFFMAN: |
985 | 0 | result = ReadHuffmanCode(*num_htrees + s->max_run_length_prefix, |
986 | 0 | s->context_map_table, NULL, s); |
987 | 0 | if (result != BROTLI_DECODER_SUCCESS) return result; |
988 | 0 | s->code = 0xFFFF; |
989 | 0 | s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_DECODE; |
990 | | /* No break, continue to next state. */ |
991 | 0 | case BROTLI_STATE_CONTEXT_MAP_DECODE: { |
992 | 0 | uint32_t context_index = s->context_index; |
993 | 0 | uint32_t max_run_length_prefix = s->max_run_length_prefix; |
994 | 0 | uint8_t* context_map = *context_map_arg; |
995 | 0 | uint32_t code = s->code; |
996 | 0 | BROTLI_BOOL skip_preamble = (code != 0xFFFF); |
997 | 0 | while (context_index < context_map_size || skip_preamble) { |
998 | 0 | if (!skip_preamble) { |
999 | 0 | if (!SafeReadSymbol(s->context_map_table, br, &code)) { |
1000 | 0 | s->code = 0xFFFF; |
1001 | 0 | s->context_index = context_index; |
1002 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
1003 | 0 | } |
1004 | 0 | BROTLI_LOG_UINT(code); |
1005 | |
|
1006 | 0 | if (code == 0) { |
1007 | 0 | context_map[context_index++] = 0; |
1008 | 0 | continue; |
1009 | 0 | } |
1010 | 0 | if (code > max_run_length_prefix) { |
1011 | 0 | context_map[context_index++] = |
1012 | 0 | (uint8_t)(code - max_run_length_prefix); |
1013 | 0 | continue; |
1014 | 0 | } |
1015 | 0 | } else { |
1016 | 0 | skip_preamble = BROTLI_FALSE; |
1017 | 0 | } |
1018 | | /* RLE sub-stage. */ |
1019 | 0 | { |
1020 | 0 | uint32_t reps; |
1021 | 0 | if (!BrotliSafeReadBits(br, code, &reps)) { |
1022 | 0 | s->code = code; |
1023 | 0 | s->context_index = context_index; |
1024 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
1025 | 0 | } |
1026 | 0 | reps += 1U << code; |
1027 | 0 | BROTLI_LOG_UINT(reps); |
1028 | 0 | if (context_index + reps > context_map_size) { |
1029 | 0 | return |
1030 | 0 | BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT); |
1031 | 0 | } |
1032 | 0 | do { |
1033 | 0 | context_map[context_index++] = 0; |
1034 | 0 | } while (--reps); |
1035 | 0 | } |
1036 | 0 | } |
1037 | | /* No break, continue to next state. */ |
1038 | 0 | } |
1039 | 0 | case BROTLI_STATE_CONTEXT_MAP_TRANSFORM: { |
1040 | 0 | uint32_t bits; |
1041 | 0 | if (!BrotliSafeReadBits(br, 1, &bits)) { |
1042 | 0 | s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_TRANSFORM; |
1043 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
1044 | 0 | } |
1045 | 0 | if (bits != 0) { |
1046 | 0 | InverseMoveToFrontTransform(*context_map_arg, context_map_size, s); |
1047 | 0 | } |
1048 | 0 | s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE; |
1049 | 0 | return BROTLI_DECODER_SUCCESS; |
1050 | 0 | } |
1051 | 0 | default: |
1052 | 0 | return |
1053 | 0 | BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); |
1054 | 0 | } |
1055 | 0 | } |
1056 | | |
1057 | | /* Decodes a command or literal and updates block type ring-buffer. |
1058 | | Reads 3..54 bits. */ |
1059 | | static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength( |
1060 | 0 | int safe, BrotliDecoderState* s, int tree_type) { |
1061 | 0 | uint32_t max_block_type = s->num_block_types[tree_type]; |
1062 | 0 | const HuffmanCode* type_tree = &s->block_type_trees[ |
1063 | 0 | tree_type * BROTLI_HUFFMAN_MAX_SIZE_258]; |
1064 | 0 | const HuffmanCode* len_tree = &s->block_len_trees[ |
1065 | 0 | tree_type * BROTLI_HUFFMAN_MAX_SIZE_26]; |
1066 | 0 | BrotliBitReader* br = &s->br; |
1067 | 0 | uint32_t* ringbuffer = &s->block_type_rb[tree_type * 2]; |
1068 | 0 | uint32_t block_type; |
1069 | | |
1070 | | /* Read 0..15 + 3..39 bits */ |
1071 | 0 | if (!safe) { |
1072 | 0 | block_type = ReadSymbol(type_tree, br); |
1073 | 0 | s->block_length[tree_type] = ReadBlockLength(len_tree, br); |
1074 | 0 | } else { |
1075 | 0 | BrotliBitReaderState memento; |
1076 | 0 | BrotliBitReaderSaveState(br, &memento); |
1077 | 0 | if (!SafeReadSymbol(type_tree, br, &block_type)) return BROTLI_FALSE; |
1078 | 0 | if (!SafeReadBlockLength(s, &s->block_length[tree_type], len_tree, br)) { |
1079 | 0 | s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE; |
1080 | 0 | BrotliBitReaderRestoreState(br, &memento); |
1081 | 0 | return BROTLI_FALSE; |
1082 | 0 | } |
1083 | 0 | } |
1084 | | |
1085 | 0 | if (block_type == 1) { |
1086 | 0 | block_type = ringbuffer[1] + 1; |
1087 | 0 | } else if (block_type == 0) { |
1088 | 0 | block_type = ringbuffer[0]; |
1089 | 0 | } else { |
1090 | 0 | block_type -= 2; |
1091 | 0 | } |
1092 | 0 | if (block_type >= max_block_type) { |
1093 | 0 | block_type -= max_block_type; |
1094 | 0 | } |
1095 | 0 | ringbuffer[0] = ringbuffer[1]; |
1096 | 0 | ringbuffer[1] = block_type; |
1097 | 0 | return BROTLI_TRUE; |
1098 | 0 | } |
1099 | | |
1100 | | static BROTLI_INLINE void DetectTrivialLiteralBlockTypes( |
1101 | 0 | BrotliDecoderState* s) { |
1102 | 0 | size_t i; |
1103 | 0 | for (i = 0; i < 8; ++i) s->trivial_literal_contexts[i] = 0; |
1104 | 0 | for (i = 0; i < s->num_block_types[0]; i++) { |
1105 | 0 | size_t offset = i << BROTLI_LITERAL_CONTEXT_BITS; |
1106 | 0 | size_t error = 0; |
1107 | 0 | size_t sample = s->context_map[offset]; |
1108 | 0 | size_t j; |
1109 | 0 | for (j = 0; j < (1u << BROTLI_LITERAL_CONTEXT_BITS);) { |
1110 | 0 | BROTLI_REPEAT(4, error |= s->context_map[offset + j++] ^ sample;) |
1111 | 0 | } |
1112 | 0 | if (error == 0) { |
1113 | 0 | s->trivial_literal_contexts[i >> 5] |= 1u << (i & 31); |
1114 | 0 | } |
1115 | 0 | } |
1116 | 0 | } |
1117 | | |
1118 | 0 | static BROTLI_INLINE void PrepareLiteralDecoding(BrotliDecoderState* s) { |
1119 | 0 | uint8_t context_mode; |
1120 | 0 | size_t trivial; |
1121 | 0 | uint32_t block_type = s->block_type_rb[1]; |
1122 | 0 | uint32_t context_offset = block_type << BROTLI_LITERAL_CONTEXT_BITS; |
1123 | 0 | s->context_map_slice = s->context_map + context_offset; |
1124 | 0 | trivial = s->trivial_literal_contexts[block_type >> 5]; |
1125 | 0 | s->trivial_literal_context = (trivial >> (block_type & 31)) & 1; |
1126 | 0 | s->literal_htree = s->literal_hgroup.htrees[s->context_map_slice[0]]; |
1127 | 0 | context_mode = s->context_modes[block_type]; |
1128 | 0 | s->context_lookup1 = &kContextLookup[kContextLookupOffsets[context_mode]]; |
1129 | 0 | s->context_lookup2 = &kContextLookup[kContextLookupOffsets[context_mode + 1]]; |
1130 | 0 | } |
1131 | | |
1132 | | /* Decodes the block type and updates the state for literal context. |
1133 | | Reads 3..54 bits. */ |
1134 | | static BROTLI_INLINE BROTLI_BOOL DecodeLiteralBlockSwitchInternal( |
1135 | 0 | int safe, BrotliDecoderState* s) { |
1136 | 0 | if (!DecodeBlockTypeAndLength(safe, s, 0)) { |
1137 | 0 | return BROTLI_FALSE; |
1138 | 0 | } |
1139 | 0 | PrepareLiteralDecoding(s); |
1140 | 0 | return BROTLI_TRUE; |
1141 | 0 | } |
1142 | | |
1143 | 0 | static void BROTLI_NOINLINE DecodeLiteralBlockSwitch(BrotliDecoderState* s) { |
1144 | 0 | DecodeLiteralBlockSwitchInternal(0, s); |
1145 | 0 | } |
1146 | | |
1147 | | static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeLiteralBlockSwitch( |
1148 | 0 | BrotliDecoderState* s) { |
1149 | 0 | return DecodeLiteralBlockSwitchInternal(1, s); |
1150 | 0 | } |
1151 | | |
1152 | | /* Block switch for insert/copy length. |
1153 | | Reads 3..54 bits. */ |
1154 | | static BROTLI_INLINE BROTLI_BOOL DecodeCommandBlockSwitchInternal( |
1155 | 0 | int safe, BrotliDecoderState* s) { |
1156 | 0 | if (!DecodeBlockTypeAndLength(safe, s, 1)) { |
1157 | 0 | return BROTLI_FALSE; |
1158 | 0 | } |
1159 | 0 | s->htree_command = s->insert_copy_hgroup.htrees[s->block_type_rb[3]]; |
1160 | 0 | return BROTLI_TRUE; |
1161 | 0 | } |
1162 | | |
1163 | 0 | static void BROTLI_NOINLINE DecodeCommandBlockSwitch(BrotliDecoderState* s) { |
1164 | 0 | DecodeCommandBlockSwitchInternal(0, s); |
1165 | 0 | } |
1166 | | static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeCommandBlockSwitch( |
1167 | 0 | BrotliDecoderState* s) { |
1168 | 0 | return DecodeCommandBlockSwitchInternal(1, s); |
1169 | 0 | } |
1170 | | |
1171 | | /* Block switch for distance codes. |
1172 | | Reads 3..54 bits. */ |
1173 | | static BROTLI_INLINE BROTLI_BOOL DecodeDistanceBlockSwitchInternal( |
1174 | 0 | int safe, BrotliDecoderState* s) { |
1175 | 0 | if (!DecodeBlockTypeAndLength(safe, s, 2)) { |
1176 | 0 | return BROTLI_FALSE; |
1177 | 0 | } |
1178 | 0 | s->dist_context_map_slice = s->dist_context_map + |
1179 | 0 | (s->block_type_rb[5] << BROTLI_DISTANCE_CONTEXT_BITS); |
1180 | 0 | s->dist_htree_index = s->dist_context_map_slice[s->distance_context]; |
1181 | 0 | return BROTLI_TRUE; |
1182 | 0 | } |
1183 | | |
1184 | 0 | static void BROTLI_NOINLINE DecodeDistanceBlockSwitch(BrotliDecoderState* s) { |
1185 | 0 | DecodeDistanceBlockSwitchInternal(0, s); |
1186 | 0 | } |
1187 | | |
1188 | | static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeDistanceBlockSwitch( |
1189 | 0 | BrotliDecoderState* s) { |
1190 | 0 | return DecodeDistanceBlockSwitchInternal(1, s); |
1191 | 0 | } |
1192 | | |
1193 | 0 | static size_t UnwrittenBytes(const BrotliDecoderState* s, BROTLI_BOOL wrap) { |
1194 | 0 | size_t pos = wrap && s->pos > s->ringbuffer_size ? |
1195 | 0 | (size_t)s->ringbuffer_size : (size_t)(s->pos); |
1196 | 0 | size_t partial_pos_rb = (s->rb_roundtrips * (size_t)s->ringbuffer_size) + pos; |
1197 | 0 | return partial_pos_rb - s->partial_pos_out; |
1198 | 0 | } |
1199 | | |
1200 | | /* Dumps output. |
1201 | | Returns BROTLI_DECODER_NEEDS_MORE_OUTPUT only if there is more output to push |
1202 | | and either ring-buffer is as big as window size, or |force| is true. |
1203 | | */ |
1204 | | static BrotliDecoderErrorCode BROTLI_NOINLINE WriteRingBuffer( |
1205 | | BrotliDecoderState* s, size_t* available_out, uint8_t** next_out, |
1206 | 0 | size_t* total_out, BROTLI_BOOL force) { |
1207 | 0 | uint8_t* start = |
1208 | 0 | s->ringbuffer + (s->partial_pos_out & (size_t)s->ringbuffer_mask); |
1209 | 0 | size_t to_write = UnwrittenBytes(s, BROTLI_TRUE); |
1210 | 0 | size_t num_written = *available_out; |
1211 | 0 | if (num_written > to_write) { |
1212 | 0 | num_written = to_write; |
1213 | 0 | } |
1214 | 0 | if (s->meta_block_remaining_len < 0) { |
1215 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1); |
1216 | 0 | } |
1217 | 0 | if (next_out && !*next_out) { |
1218 | 0 | *next_out = start; |
1219 | 0 | } else { |
1220 | 0 | if (next_out) { |
1221 | 0 | memcpy(*next_out, start, num_written); |
1222 | 0 | *next_out += num_written; |
1223 | 0 | } |
1224 | 0 | } |
1225 | 0 | *available_out -= num_written; |
1226 | 0 | BROTLI_LOG_UINT(to_write); |
1227 | 0 | BROTLI_LOG_UINT(num_written); |
1228 | 0 | s->partial_pos_out += num_written; |
1229 | 0 | if (total_out) { |
1230 | 0 | *total_out = s->partial_pos_out; |
1231 | 0 | } |
1232 | 0 | if (num_written < to_write) { |
1233 | 0 | if (s->ringbuffer_size == (1 << s->window_bits) || force) { |
1234 | 0 | return BROTLI_DECODER_NEEDS_MORE_OUTPUT; |
1235 | 0 | } else { |
1236 | 0 | return BROTLI_DECODER_SUCCESS; |
1237 | 0 | } |
1238 | 0 | } |
1239 | | /* Wrap ring buffer only if it has reached its maximal size. */ |
1240 | 0 | if (s->ringbuffer_size == (1 << s->window_bits) && |
1241 | 0 | s->pos >= s->ringbuffer_size) { |
1242 | 0 | s->pos -= s->ringbuffer_size; |
1243 | 0 | s->rb_roundtrips++; |
1244 | 0 | s->should_wrap_ringbuffer = (size_t)s->pos != 0 ? 1 : 0; |
1245 | 0 | } |
1246 | 0 | return BROTLI_DECODER_SUCCESS; |
1247 | 0 | } |
1248 | | |
1249 | 0 | static void BROTLI_NOINLINE WrapRingBuffer(BrotliDecoderState* s) { |
1250 | 0 | if (s->should_wrap_ringbuffer) { |
1251 | 0 | memcpy(s->ringbuffer, s->ringbuffer_end, (size_t)s->pos); |
1252 | 0 | s->should_wrap_ringbuffer = 0; |
1253 | 0 | } |
1254 | 0 | } |
1255 | | |
1256 | | /* Allocates ring-buffer. |
1257 | | |
1258 | | s->ringbuffer_size MUST be updated by BrotliCalculateRingBufferSize before |
1259 | | this function is called. |
1260 | | |
1261 | | Last two bytes of ring-buffer are initialized to 0, so context calculation |
1262 | | could be done uniformly for the first two and all other positions. |
1263 | | */ |
1264 | | static BROTLI_BOOL BROTLI_NOINLINE BrotliEnsureRingBuffer( |
1265 | 0 | BrotliDecoderState* s) { |
1266 | 0 | uint8_t* old_ringbuffer = s->ringbuffer; |
1267 | 0 | if (s->ringbuffer_size == s->new_ringbuffer_size) { |
1268 | 0 | return BROTLI_TRUE; |
1269 | 0 | } |
1270 | | |
1271 | 0 | s->ringbuffer = (uint8_t*)BROTLI_ALLOC(s, (size_t)(s->new_ringbuffer_size) + |
1272 | 0 | kRingBufferWriteAheadSlack); |
1273 | 0 | if (s->ringbuffer == 0) { |
1274 | | /* Restore previous value. */ |
1275 | 0 | s->ringbuffer = old_ringbuffer; |
1276 | 0 | return BROTLI_FALSE; |
1277 | 0 | } |
1278 | 0 | s->ringbuffer[s->new_ringbuffer_size - 2] = 0; |
1279 | 0 | s->ringbuffer[s->new_ringbuffer_size - 1] = 0; |
1280 | |
|
1281 | 0 | if (!!old_ringbuffer) { |
1282 | 0 | memcpy(s->ringbuffer, old_ringbuffer, (size_t)s->pos); |
1283 | 0 | BROTLI_FREE(s, old_ringbuffer); |
1284 | 0 | } |
1285 | |
|
1286 | 0 | s->ringbuffer_size = s->new_ringbuffer_size; |
1287 | 0 | s->ringbuffer_mask = s->new_ringbuffer_size - 1; |
1288 | 0 | s->ringbuffer_end = s->ringbuffer + s->ringbuffer_size; |
1289 | |
|
1290 | 0 | return BROTLI_TRUE; |
1291 | 0 | } |
1292 | | |
1293 | | static BrotliDecoderErrorCode BROTLI_NOINLINE CopyUncompressedBlockToOutput( |
1294 | | size_t* available_out, uint8_t** next_out, size_t* total_out, |
1295 | 0 | BrotliDecoderState* s) { |
1296 | | /* TODO: avoid allocation for single uncompressed block. */ |
1297 | 0 | if (!BrotliEnsureRingBuffer(s)) { |
1298 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1); |
1299 | 0 | } |
1300 | | |
1301 | | /* State machine */ |
1302 | 0 | for (;;) { |
1303 | 0 | switch (s->substate_uncompressed) { |
1304 | 0 | case BROTLI_STATE_UNCOMPRESSED_NONE: { |
1305 | 0 | int nbytes = (int)BrotliGetRemainingBytes(&s->br); |
1306 | 0 | if (nbytes > s->meta_block_remaining_len) { |
1307 | 0 | nbytes = s->meta_block_remaining_len; |
1308 | 0 | } |
1309 | 0 | if (s->pos + nbytes > s->ringbuffer_size) { |
1310 | 0 | nbytes = s->ringbuffer_size - s->pos; |
1311 | 0 | } |
1312 | | /* Copy remaining bytes from s->br.buf_ to ring-buffer. */ |
1313 | 0 | BrotliCopyBytes(&s->ringbuffer[s->pos], &s->br, (size_t)nbytes); |
1314 | 0 | s->pos += nbytes; |
1315 | 0 | s->meta_block_remaining_len -= nbytes; |
1316 | 0 | if (s->pos < 1 << s->window_bits) { |
1317 | 0 | if (s->meta_block_remaining_len == 0) { |
1318 | 0 | return BROTLI_DECODER_SUCCESS; |
1319 | 0 | } |
1320 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
1321 | 0 | } |
1322 | 0 | s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_WRITE; |
1323 | | /* No break, continue to next state */ |
1324 | 0 | } |
1325 | 0 | case BROTLI_STATE_UNCOMPRESSED_WRITE: { |
1326 | 0 | BrotliDecoderErrorCode result; |
1327 | 0 | result = WriteRingBuffer( |
1328 | 0 | s, available_out, next_out, total_out, BROTLI_FALSE); |
1329 | 0 | if (result != BROTLI_DECODER_SUCCESS) { |
1330 | 0 | return result; |
1331 | 0 | } |
1332 | 0 | if (s->ringbuffer_size == 1 << s->window_bits) { |
1333 | 0 | s->max_distance = s->max_backward_distance; |
1334 | 0 | } |
1335 | 0 | s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE; |
1336 | 0 | break; |
1337 | 0 | } |
1338 | 0 | } |
1339 | 0 | } |
1340 | 0 | BROTLI_DCHECK(0); /* Unreachable */ |
1341 | 0 | } |
1342 | | |
1343 | | /* Calculates the smallest feasible ring buffer. |
1344 | | |
1345 | | If we know the data size is small, do not allocate more ring buffer |
1346 | | size than needed to reduce memory usage. |
1347 | | |
1348 | | When this method is called, metablock size and flags MUST be decoded. |
1349 | | */ |
1350 | | static void BROTLI_NOINLINE BrotliCalculateRingBufferSize( |
1351 | 0 | BrotliDecoderState* s) { |
1352 | 0 | int window_size = 1 << s->window_bits; |
1353 | 0 | int new_ringbuffer_size = window_size; |
1354 | | /* We need at least 2 bytes of ring buffer size to get the last two |
1355 | | bytes for context from there */ |
1356 | 0 | int min_size = s->ringbuffer_size ? s->ringbuffer_size : 1024; |
1357 | 0 | int output_size; |
1358 | | |
1359 | | /* If maximum is already reached, no further extension is retired. */ |
1360 | 0 | if (s->ringbuffer_size == window_size) { |
1361 | 0 | return; |
1362 | 0 | } |
1363 | | |
1364 | | /* Metadata blocks does not touch ring buffer. */ |
1365 | 0 | if (s->is_metadata) { |
1366 | 0 | return; |
1367 | 0 | } |
1368 | | |
1369 | 0 | if (!s->ringbuffer) { |
1370 | 0 | output_size = 0; |
1371 | 0 | } else { |
1372 | 0 | output_size = s->pos; |
1373 | 0 | } |
1374 | 0 | output_size += s->meta_block_remaining_len; |
1375 | 0 | min_size = min_size < output_size ? output_size : min_size; |
1376 | |
|
1377 | 0 | if (!!s->canny_ringbuffer_allocation) { |
1378 | | /* Reduce ring buffer size to save memory when server is unscrupulous. |
1379 | | In worst case memory usage might be 1.5x bigger for a short period of |
1380 | | ring buffer reallocation.*/ |
1381 | 0 | while ((new_ringbuffer_size >> 1) >= min_size) { |
1382 | 0 | new_ringbuffer_size >>= 1; |
1383 | 0 | } |
1384 | 0 | } |
1385 | |
|
1386 | 0 | s->new_ringbuffer_size = new_ringbuffer_size; |
1387 | 0 | } |
1388 | | |
1389 | | /* Reads 1..256 2-bit context modes. */ |
1390 | 0 | static BrotliDecoderErrorCode ReadContextModes(BrotliDecoderState* s) { |
1391 | 0 | BrotliBitReader* br = &s->br; |
1392 | 0 | int i = s->loop_counter; |
1393 | |
|
1394 | 0 | while (i < (int)s->num_block_types[0]) { |
1395 | 0 | uint32_t bits; |
1396 | 0 | if (!BrotliSafeReadBits(br, 2, &bits)) { |
1397 | 0 | s->loop_counter = i; |
1398 | 0 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
1399 | 0 | } |
1400 | 0 | s->context_modes[i] = (uint8_t)(bits << 1); |
1401 | 0 | BROTLI_LOG_ARRAY_INDEX(s->context_modes, i); |
1402 | 0 | i++; |
1403 | 0 | } |
1404 | 0 | return BROTLI_DECODER_SUCCESS; |
1405 | 0 | } |
1406 | | |
1407 | 0 | static BROTLI_INLINE void TakeDistanceFromRingBuffer(BrotliDecoderState* s) { |
1408 | 0 | if (s->distance_code == 0) { |
1409 | 0 | --s->dist_rb_idx; |
1410 | 0 | s->distance_code = s->dist_rb[s->dist_rb_idx & 3]; |
1411 | | /* Compensate double distance-ring-buffer roll for dictionary items. */ |
1412 | 0 | s->distance_context = 1; |
1413 | 0 | } else { |
1414 | 0 | int distance_code = s->distance_code << 1; |
1415 | | /* kDistanceShortCodeIndexOffset has 2-bit values from LSB: */ |
1416 | | /* 3, 2, 1, 0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2 */ |
1417 | 0 | const uint32_t kDistanceShortCodeIndexOffset = 0xaaafff1b; |
1418 | | /* kDistanceShortCodeValueOffset has 2-bit values from LSB: */ |
1419 | | /*-0, 0,-0, 0,-1, 1,-2, 2,-3, 3,-1, 1,-2, 2,-3, 3 */ |
1420 | 0 | const uint32_t kDistanceShortCodeValueOffset = 0xfa5fa500; |
1421 | 0 | int v = (s->dist_rb_idx + |
1422 | 0 | (int)(kDistanceShortCodeIndexOffset >> distance_code)) & 0x3; |
1423 | 0 | s->distance_code = s->dist_rb[v]; |
1424 | 0 | v = (int)(kDistanceShortCodeValueOffset >> distance_code) & 0x3; |
1425 | 0 | if ((distance_code & 0x3) != 0) { |
1426 | 0 | s->distance_code += v; |
1427 | 0 | } else { |
1428 | 0 | s->distance_code -= v; |
1429 | 0 | if (s->distance_code <= 0) { |
1430 | | /* A huge distance will cause a BROTLI_FAILURE() soon. */ |
1431 | | /* This is a little faster than failing here. */ |
1432 | 0 | s->distance_code = 0x0fffffff; |
1433 | 0 | } |
1434 | 0 | } |
1435 | 0 | } |
1436 | 0 | } |
1437 | | |
1438 | | static BROTLI_INLINE BROTLI_BOOL SafeReadBits( |
1439 | 0 | BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { |
1440 | 0 | if (n_bits != 0) { |
1441 | 0 | return BrotliSafeReadBits(br, n_bits, val); |
1442 | 0 | } else { |
1443 | 0 | *val = 0; |
1444 | 0 | return BROTLI_TRUE; |
1445 | 0 | } |
1446 | 0 | } |
1447 | | |
1448 | | /* Precondition: s->distance_code < 0 */ |
1449 | | static BROTLI_INLINE BROTLI_BOOL ReadDistanceInternal( |
1450 | 0 | int safe, BrotliDecoderState* s, BrotliBitReader* br) { |
1451 | 0 | int distval; |
1452 | 0 | BrotliBitReaderState memento; |
1453 | 0 | HuffmanCode* distance_tree = s->distance_hgroup.htrees[s->dist_htree_index]; |
1454 | 0 | if (!safe) { |
1455 | 0 | s->distance_code = (int)ReadSymbol(distance_tree, br); |
1456 | 0 | } else { |
1457 | 0 | uint32_t code; |
1458 | 0 | BrotliBitReaderSaveState(br, &memento); |
1459 | 0 | if (!SafeReadSymbol(distance_tree, br, &code)) { |
1460 | 0 | return BROTLI_FALSE; |
1461 | 0 | } |
1462 | 0 | s->distance_code = (int)code; |
1463 | 0 | } |
1464 | | /* Convert the distance code to the actual distance by possibly */ |
1465 | | /* looking up past distances from the s->ringbuffer. */ |
1466 | 0 | s->distance_context = 0; |
1467 | 0 | if ((s->distance_code & ~0xf) == 0) { |
1468 | 0 | TakeDistanceFromRingBuffer(s); |
1469 | 0 | --s->block_length[2]; |
1470 | 0 | return BROTLI_TRUE; |
1471 | 0 | } |
1472 | 0 | distval = s->distance_code - (int)s->num_direct_distance_codes; |
1473 | 0 | if (distval >= 0) { |
1474 | 0 | uint32_t nbits; |
1475 | 0 | int postfix; |
1476 | 0 | int offset; |
1477 | 0 | if (!safe && (s->distance_postfix_bits == 0)) { |
1478 | 0 | nbits = ((uint32_t)distval >> 1) + 1; |
1479 | 0 | offset = ((2 + (distval & 1)) << nbits) - 4; |
1480 | 0 | s->distance_code = (int)s->num_direct_distance_codes + offset + |
1481 | 0 | (int)BrotliReadBits(br, nbits); |
1482 | 0 | } else { |
1483 | | /* This branch also works well when s->distance_postfix_bits == 0 */ |
1484 | 0 | uint32_t bits; |
1485 | 0 | postfix = distval & s->distance_postfix_mask; |
1486 | 0 | distval >>= s->distance_postfix_bits; |
1487 | 0 | nbits = ((uint32_t)distval >> 1) + 1; |
1488 | 0 | if (safe) { |
1489 | 0 | if (!SafeReadBits(br, nbits, &bits)) { |
1490 | 0 | s->distance_code = -1; /* Restore precondition. */ |
1491 | 0 | BrotliBitReaderRestoreState(br, &memento); |
1492 | 0 | return BROTLI_FALSE; |
1493 | 0 | } |
1494 | 0 | } else { |
1495 | 0 | bits = BrotliReadBits(br, nbits); |
1496 | 0 | } |
1497 | 0 | offset = ((2 + (distval & 1)) << nbits) - 4; |
1498 | 0 | s->distance_code = (int)s->num_direct_distance_codes + |
1499 | 0 | ((offset + (int)bits) << s->distance_postfix_bits) + postfix; |
1500 | 0 | } |
1501 | 0 | } |
1502 | 0 | s->distance_code = s->distance_code - BROTLI_NUM_DISTANCE_SHORT_CODES + 1; |
1503 | 0 | --s->block_length[2]; |
1504 | 0 | return BROTLI_TRUE; |
1505 | 0 | } |
1506 | | |
1507 | | static BROTLI_INLINE void ReadDistance( |
1508 | 0 | BrotliDecoderState* s, BrotliBitReader* br) { |
1509 | 0 | ReadDistanceInternal(0, s, br); |
1510 | 0 | } |
1511 | | |
1512 | | static BROTLI_INLINE BROTLI_BOOL SafeReadDistance( |
1513 | 0 | BrotliDecoderState* s, BrotliBitReader* br) { |
1514 | 0 | return ReadDistanceInternal(1, s, br); |
1515 | 0 | } |
1516 | | |
1517 | | static BROTLI_INLINE BROTLI_BOOL ReadCommandInternal( |
1518 | 0 | int safe, BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) { |
1519 | 0 | uint32_t cmd_code; |
1520 | 0 | uint32_t insert_len_extra = 0; |
1521 | 0 | uint32_t copy_length; |
1522 | 0 | CmdLutElement v; |
1523 | 0 | BrotliBitReaderState memento; |
1524 | 0 | if (!safe) { |
1525 | 0 | cmd_code = ReadSymbol(s->htree_command, br); |
1526 | 0 | } else { |
1527 | 0 | BrotliBitReaderSaveState(br, &memento); |
1528 | 0 | if (!SafeReadSymbol(s->htree_command, br, &cmd_code)) { |
1529 | 0 | return BROTLI_FALSE; |
1530 | 0 | } |
1531 | 0 | } |
1532 | 0 | v = kCmdLut[cmd_code]; |
1533 | 0 | s->distance_code = v.distance_code; |
1534 | 0 | s->distance_context = v.context; |
1535 | 0 | s->dist_htree_index = s->dist_context_map_slice[s->distance_context]; |
1536 | 0 | *insert_length = v.insert_len_offset; |
1537 | 0 | if (!safe) { |
1538 | 0 | if (BROTLI_PREDICT_FALSE(v.insert_len_extra_bits != 0)) { |
1539 | 0 | insert_len_extra = BrotliReadBits(br, v.insert_len_extra_bits); |
1540 | 0 | } |
1541 | 0 | copy_length = BrotliReadBits(br, v.copy_len_extra_bits); |
1542 | 0 | } else { |
1543 | 0 | if (!SafeReadBits(br, v.insert_len_extra_bits, &insert_len_extra) || |
1544 | 0 | !SafeReadBits(br, v.copy_len_extra_bits, ©_length)) { |
1545 | 0 | BrotliBitReaderRestoreState(br, &memento); |
1546 | 0 | return BROTLI_FALSE; |
1547 | 0 | } |
1548 | 0 | } |
1549 | 0 | s->copy_length = (int)copy_length + v.copy_len_offset; |
1550 | 0 | --s->block_length[1]; |
1551 | 0 | *insert_length += (int)insert_len_extra; |
1552 | 0 | return BROTLI_TRUE; |
1553 | 0 | } |
1554 | | |
1555 | | static BROTLI_INLINE void ReadCommand( |
1556 | 0 | BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) { |
1557 | 0 | ReadCommandInternal(0, s, br, insert_length); |
1558 | 0 | } |
1559 | | |
1560 | | static BROTLI_INLINE BROTLI_BOOL SafeReadCommand( |
1561 | 0 | BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) { |
1562 | 0 | return ReadCommandInternal(1, s, br, insert_length); |
1563 | 0 | } |
1564 | | |
1565 | | static BROTLI_INLINE BROTLI_BOOL CheckInputAmount( |
1566 | 0 | int safe, BrotliBitReader* const br, size_t num) { |
1567 | 0 | if (safe) { |
1568 | 0 | return BROTLI_TRUE; |
1569 | 0 | } |
1570 | 0 | return BrotliCheckInputAmount(br, num); |
1571 | 0 | } |
1572 | | |
1573 | | #define BROTLI_SAFE(METHOD) \ |
1574 | 0 | { \ |
1575 | 0 | if (safe) { \ |
1576 | 0 | if (!Safe##METHOD) { \ |
1577 | 0 | result = BROTLI_DECODER_NEEDS_MORE_INPUT; \ |
1578 | 0 | goto saveStateAndReturn; \ |
1579 | 0 | } \ |
1580 | 0 | } else { \ |
1581 | 0 | METHOD; \ |
1582 | 0 | } \ |
1583 | 0 | } |
1584 | | |
1585 | | static BROTLI_INLINE BrotliDecoderErrorCode ProcessCommandsInternal( |
1586 | 0 | int safe, BrotliDecoderState* s) { |
1587 | 0 | int pos = s->pos; |
1588 | 0 | int i = s->loop_counter; |
1589 | 0 | BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS; |
1590 | 0 | BrotliBitReader* br = &s->br; |
1591 | |
|
1592 | 0 | if (!CheckInputAmount(safe, br, 28)) { |
1593 | 0 | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
1594 | 0 | goto saveStateAndReturn; |
1595 | 0 | } |
1596 | 0 | if (!safe) { |
1597 | 0 | BROTLI_UNUSED(BrotliWarmupBitReader(br)); |
1598 | 0 | } |
1599 | | |
1600 | | /* Jump into state machine. */ |
1601 | 0 | if (s->state == BROTLI_STATE_COMMAND_BEGIN) { |
1602 | 0 | goto CommandBegin; |
1603 | 0 | } else if (s->state == BROTLI_STATE_COMMAND_INNER) { |
1604 | 0 | goto CommandInner; |
1605 | 0 | } else if (s->state == BROTLI_STATE_COMMAND_POST_DECODE_LITERALS) { |
1606 | 0 | goto CommandPostDecodeLiterals; |
1607 | 0 | } else if (s->state == BROTLI_STATE_COMMAND_POST_WRAP_COPY) { |
1608 | 0 | goto CommandPostWrapCopy; |
1609 | 0 | } else { |
1610 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); |
1611 | 0 | } |
1612 | | |
1613 | 0 | CommandBegin: |
1614 | 0 | if (safe) { |
1615 | 0 | s->state = BROTLI_STATE_COMMAND_BEGIN; |
1616 | 0 | } |
1617 | 0 | if (!CheckInputAmount(safe, br, 28)) { /* 156 bits + 7 bytes */ |
1618 | 0 | s->state = BROTLI_STATE_COMMAND_BEGIN; |
1619 | 0 | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
1620 | 0 | goto saveStateAndReturn; |
1621 | 0 | } |
1622 | 0 | if (BROTLI_PREDICT_FALSE(s->block_length[1] == 0)) { |
1623 | 0 | BROTLI_SAFE(DecodeCommandBlockSwitch(s)); |
1624 | 0 | goto CommandBegin; |
1625 | 0 | } |
1626 | | /* Read the insert/copy length in the command */ |
1627 | 0 | BROTLI_SAFE(ReadCommand(s, br, &i)); |
1628 | 0 | BROTLI_LOG(("[ProcessCommandsInternal] pos = %d insert = %d copy = %d\n", |
1629 | 0 | pos, i, s->copy_length)); |
1630 | 0 | if (i == 0) { |
1631 | 0 | goto CommandPostDecodeLiterals; |
1632 | 0 | } |
1633 | 0 | s->meta_block_remaining_len -= i; |
1634 | |
|
1635 | 0 | CommandInner: |
1636 | 0 | if (safe) { |
1637 | 0 | s->state = BROTLI_STATE_COMMAND_INNER; |
1638 | 0 | } |
1639 | | /* Read the literals in the command */ |
1640 | 0 | if (s->trivial_literal_context) { |
1641 | 0 | uint32_t bits; |
1642 | 0 | uint32_t value; |
1643 | 0 | PreloadSymbol(safe, s->literal_htree, br, &bits, &value); |
1644 | 0 | do { |
1645 | 0 | if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */ |
1646 | 0 | s->state = BROTLI_STATE_COMMAND_INNER; |
1647 | 0 | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
1648 | 0 | goto saveStateAndReturn; |
1649 | 0 | } |
1650 | 0 | if (BROTLI_PREDICT_FALSE(s->block_length[0] == 0)) { |
1651 | 0 | BROTLI_SAFE(DecodeLiteralBlockSwitch(s)); |
1652 | 0 | PreloadSymbol(safe, s->literal_htree, br, &bits, &value); |
1653 | 0 | if (!s->trivial_literal_context) goto CommandInner; |
1654 | 0 | } |
1655 | 0 | if (!safe) { |
1656 | 0 | s->ringbuffer[pos] = |
1657 | 0 | (uint8_t)ReadPreloadedSymbol(s->literal_htree, br, &bits, &value); |
1658 | 0 | } else { |
1659 | 0 | uint32_t literal; |
1660 | 0 | if (!SafeReadSymbol(s->literal_htree, br, &literal)) { |
1661 | 0 | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
1662 | 0 | goto saveStateAndReturn; |
1663 | 0 | } |
1664 | 0 | s->ringbuffer[pos] = (uint8_t)literal; |
1665 | 0 | } |
1666 | 0 | --s->block_length[0]; |
1667 | 0 | BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos); |
1668 | 0 | ++pos; |
1669 | 0 | if (BROTLI_PREDICT_FALSE(pos == s->ringbuffer_size)) { |
1670 | 0 | s->state = BROTLI_STATE_COMMAND_INNER_WRITE; |
1671 | 0 | --i; |
1672 | 0 | goto saveStateAndReturn; |
1673 | 0 | } |
1674 | 0 | } while (--i != 0); |
1675 | 0 | } else { |
1676 | 0 | uint8_t p1 = s->ringbuffer[(pos - 1) & s->ringbuffer_mask]; |
1677 | 0 | uint8_t p2 = s->ringbuffer[(pos - 2) & s->ringbuffer_mask]; |
1678 | 0 | do { |
1679 | 0 | const HuffmanCode* hc; |
1680 | 0 | uint8_t context; |
1681 | 0 | if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */ |
1682 | 0 | s->state = BROTLI_STATE_COMMAND_INNER; |
1683 | 0 | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
1684 | 0 | goto saveStateAndReturn; |
1685 | 0 | } |
1686 | 0 | if (BROTLI_PREDICT_FALSE(s->block_length[0] == 0)) { |
1687 | 0 | BROTLI_SAFE(DecodeLiteralBlockSwitch(s)); |
1688 | 0 | if (s->trivial_literal_context) goto CommandInner; |
1689 | 0 | } |
1690 | 0 | context = s->context_lookup1[p1] | s->context_lookup2[p2]; |
1691 | 0 | BROTLI_LOG_UINT(context); |
1692 | 0 | hc = s->literal_hgroup.htrees[s->context_map_slice[context]]; |
1693 | 0 | p2 = p1; |
1694 | 0 | if (!safe) { |
1695 | 0 | p1 = (uint8_t)ReadSymbol(hc, br); |
1696 | 0 | } else { |
1697 | 0 | uint32_t literal; |
1698 | 0 | if (!SafeReadSymbol(hc, br, &literal)) { |
1699 | 0 | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
1700 | 0 | goto saveStateAndReturn; |
1701 | 0 | } |
1702 | 0 | p1 = (uint8_t)literal; |
1703 | 0 | } |
1704 | 0 | s->ringbuffer[pos] = p1; |
1705 | 0 | --s->block_length[0]; |
1706 | 0 | BROTLI_LOG_UINT(s->context_map_slice[context]); |
1707 | 0 | BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos & s->ringbuffer_mask); |
1708 | 0 | ++pos; |
1709 | 0 | if (BROTLI_PREDICT_FALSE(pos == s->ringbuffer_size)) { |
1710 | 0 | s->state = BROTLI_STATE_COMMAND_INNER_WRITE; |
1711 | 0 | --i; |
1712 | 0 | goto saveStateAndReturn; |
1713 | 0 | } |
1714 | 0 | } while (--i != 0); |
1715 | 0 | } |
1716 | 0 | BROTLI_LOG_UINT(s->meta_block_remaining_len); |
1717 | 0 | if (BROTLI_PREDICT_FALSE(s->meta_block_remaining_len <= 0)) { |
1718 | 0 | s->state = BROTLI_STATE_METABLOCK_DONE; |
1719 | 0 | goto saveStateAndReturn; |
1720 | 0 | } |
1721 | | |
1722 | 0 | CommandPostDecodeLiterals: |
1723 | 0 | if (safe) { |
1724 | 0 | s->state = BROTLI_STATE_COMMAND_POST_DECODE_LITERALS; |
1725 | 0 | } |
1726 | 0 | if (s->distance_code >= 0) { |
1727 | | /* Implicit distance case. */ |
1728 | 0 | s->distance_context = s->distance_code ? 0 : 1; |
1729 | 0 | --s->dist_rb_idx; |
1730 | 0 | s->distance_code = s->dist_rb[s->dist_rb_idx & 3]; |
1731 | 0 | } else { |
1732 | | /* Read distance code in the command, unless it was implicitly zero. */ |
1733 | 0 | if (BROTLI_PREDICT_FALSE(s->block_length[2] == 0)) { |
1734 | 0 | BROTLI_SAFE(DecodeDistanceBlockSwitch(s)); |
1735 | 0 | } |
1736 | 0 | BROTLI_SAFE(ReadDistance(s, br)); |
1737 | 0 | } |
1738 | 0 | BROTLI_LOG(("[ProcessCommandsInternal] pos = %d distance = %d\n", |
1739 | 0 | pos, s->distance_code)); |
1740 | 0 | if (s->max_distance != s->max_backward_distance) { |
1741 | 0 | s->max_distance = |
1742 | 0 | (pos < s->max_backward_distance) ? pos : s->max_backward_distance; |
1743 | 0 | } |
1744 | 0 | i = s->copy_length; |
1745 | | /* Apply copy of LZ77 back-reference, or static dictionary reference if |
1746 | | the distance is larger than the max LZ77 distance */ |
1747 | 0 | if (s->distance_code > s->max_distance) { |
1748 | 0 | int address = s->distance_code - s->max_distance - 1; |
1749 | 0 | if (i >= BROTLI_MIN_DICTIONARY_WORD_LENGTH && |
1750 | 0 | i <= BROTLI_MAX_DICTIONARY_WORD_LENGTH) { |
1751 | 0 | int offset = (int)s->dictionary->offsets_by_length[i]; |
1752 | 0 | uint32_t shift = s->dictionary->size_bits_by_length[i]; |
1753 | 0 | int mask = (int)BitMask(shift); |
1754 | 0 | int word_idx = address & mask; |
1755 | 0 | int transform_idx = address >> shift; |
1756 | | /* Compensate double distance-ring-buffer roll. */ |
1757 | 0 | s->dist_rb_idx += s->distance_context; |
1758 | 0 | offset += word_idx * i; |
1759 | 0 | if (BROTLI_PREDICT_FALSE(!s->dictionary->data)) { |
1760 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET); |
1761 | 0 | } |
1762 | 0 | if (transform_idx < kNumTransforms) { |
1763 | 0 | const uint8_t* word = &s->dictionary->data[offset]; |
1764 | 0 | int len = i; |
1765 | 0 | if (transform_idx == 0) { |
1766 | 0 | memcpy(&s->ringbuffer[pos], word, (size_t)len); |
1767 | 0 | BROTLI_LOG(("[ProcessCommandsInternal] dictionary word: [%.*s]\n", |
1768 | 0 | len, word)); |
1769 | 0 | } else { |
1770 | 0 | len = TransformDictionaryWord( |
1771 | 0 | &s->ringbuffer[pos], word, len, transform_idx); |
1772 | 0 | BROTLI_LOG(("[ProcessCommandsInternal] dictionary word: [%.*s]," |
1773 | 0 | " transform_idx = %d, transformed: [%.*s]\n", |
1774 | 0 | i, word, transform_idx, len, &s->ringbuffer[pos])); |
1775 | 0 | } |
1776 | 0 | pos += len; |
1777 | 0 | s->meta_block_remaining_len -= len; |
1778 | 0 | if (pos >= s->ringbuffer_size) { |
1779 | | /*s->partial_pos_rb += (size_t)s->ringbuffer_size;*/ |
1780 | 0 | s->state = BROTLI_STATE_COMMAND_POST_WRITE_1; |
1781 | 0 | goto saveStateAndReturn; |
1782 | 0 | } |
1783 | 0 | } else { |
1784 | 0 | BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d " |
1785 | 0 | "len: %d bytes left: %d\n", |
1786 | 0 | pos, s->distance_code, i, s->meta_block_remaining_len)); |
1787 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_TRANSFORM); |
1788 | 0 | } |
1789 | 0 | } else { |
1790 | 0 | BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d " |
1791 | 0 | "len: %d bytes left: %d\n", |
1792 | 0 | pos, s->distance_code, i, s->meta_block_remaining_len)); |
1793 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_DICTIONARY); |
1794 | 0 | } |
1795 | 0 | } else { |
1796 | 0 | int src_start = (pos - s->distance_code) & s->ringbuffer_mask; |
1797 | 0 | uint8_t* copy_dst = &s->ringbuffer[pos]; |
1798 | 0 | uint8_t* copy_src = &s->ringbuffer[src_start]; |
1799 | 0 | int dst_end = pos + i; |
1800 | 0 | int src_end = src_start + i; |
1801 | | /* update the recent distances cache */ |
1802 | 0 | s->dist_rb[s->dist_rb_idx & 3] = s->distance_code; |
1803 | 0 | ++s->dist_rb_idx; |
1804 | 0 | s->meta_block_remaining_len -= i; |
1805 | | /* There are 32+ bytes of slack in the ring-buffer allocation. |
1806 | | Also, we have 16 short codes, that make these 16 bytes irrelevant |
1807 | | in the ring-buffer. Let's copy over them as a first guess. |
1808 | | */ |
1809 | 0 | memmove16(copy_dst, copy_src); |
1810 | 0 | if (src_end > pos && dst_end > src_start) { |
1811 | | /* Regions intersect. */ |
1812 | 0 | goto CommandPostWrapCopy; |
1813 | 0 | } |
1814 | 0 | if (dst_end >= s->ringbuffer_size || src_end >= s->ringbuffer_size) { |
1815 | | /* At least one region wraps. */ |
1816 | 0 | goto CommandPostWrapCopy; |
1817 | 0 | } |
1818 | 0 | pos += i; |
1819 | 0 | if (i > 16) { |
1820 | 0 | if (i > 32) { |
1821 | 0 | memcpy(copy_dst + 16, copy_src + 16, (size_t)(i - 16)); |
1822 | 0 | } else { |
1823 | | /* This branch covers about 45% cases. |
1824 | | Fixed size short copy allows more compiler optimizations. */ |
1825 | 0 | memmove16(copy_dst + 16, copy_src + 16); |
1826 | 0 | } |
1827 | 0 | } |
1828 | 0 | } |
1829 | 0 | BROTLI_LOG_UINT(s->meta_block_remaining_len); |
1830 | 0 | if (s->meta_block_remaining_len <= 0) { |
1831 | | /* Next metablock, if any */ |
1832 | 0 | s->state = BROTLI_STATE_METABLOCK_DONE; |
1833 | 0 | goto saveStateAndReturn; |
1834 | 0 | } else { |
1835 | 0 | goto CommandBegin; |
1836 | 0 | } |
1837 | 0 | CommandPostWrapCopy: |
1838 | 0 | { |
1839 | 0 | int wrap_guard = s->ringbuffer_size - pos; |
1840 | 0 | while (--i >= 0) { |
1841 | 0 | s->ringbuffer[pos] = |
1842 | 0 | s->ringbuffer[(pos - s->distance_code) & s->ringbuffer_mask]; |
1843 | 0 | ++pos; |
1844 | 0 | if (BROTLI_PREDICT_FALSE(--wrap_guard == 0)) { |
1845 | 0 | s->state = BROTLI_STATE_COMMAND_POST_WRITE_2; |
1846 | 0 | goto saveStateAndReturn; |
1847 | 0 | } |
1848 | 0 | } |
1849 | 0 | } |
1850 | 0 | if (s->meta_block_remaining_len <= 0) { |
1851 | | /* Next metablock, if any */ |
1852 | 0 | s->state = BROTLI_STATE_METABLOCK_DONE; |
1853 | 0 | goto saveStateAndReturn; |
1854 | 0 | } else { |
1855 | 0 | goto CommandBegin; |
1856 | 0 | } |
1857 | | |
1858 | 0 | saveStateAndReturn: |
1859 | 0 | s->pos = pos; |
1860 | 0 | s->loop_counter = i; |
1861 | 0 | return result; |
1862 | 0 | } |
1863 | | |
1864 | | #undef BROTLI_SAFE |
1865 | | |
1866 | | static BROTLI_NOINLINE BrotliDecoderErrorCode ProcessCommands( |
1867 | 0 | BrotliDecoderState* s) { |
1868 | 0 | return ProcessCommandsInternal(0, s); |
1869 | 0 | } |
1870 | | |
1871 | | static BROTLI_NOINLINE BrotliDecoderErrorCode SafeProcessCommands( |
1872 | 0 | BrotliDecoderState* s) { |
1873 | 0 | return ProcessCommandsInternal(1, s); |
1874 | 0 | } |
1875 | | |
1876 | | BrotliDecoderResult BrotliDecoderDecompress( |
1877 | | size_t encoded_size, const uint8_t* encoded_buffer, size_t* decoded_size, |
1878 | 0 | uint8_t* decoded_buffer) { |
1879 | 0 | BrotliDecoderState s; |
1880 | 0 | BrotliDecoderResult result; |
1881 | 0 | size_t total_out = 0; |
1882 | 0 | size_t available_in = encoded_size; |
1883 | 0 | const uint8_t* next_in = encoded_buffer; |
1884 | 0 | size_t available_out = *decoded_size; |
1885 | 0 | uint8_t* next_out = decoded_buffer; |
1886 | 0 | BrotliDecoderStateInit(&s); |
1887 | 0 | result = BrotliDecoderDecompressStream( |
1888 | 0 | &s, &available_in, &next_in, &available_out, &next_out, &total_out); |
1889 | 0 | *decoded_size = total_out; |
1890 | 0 | BrotliDecoderStateCleanup(&s); |
1891 | 0 | if (result != BROTLI_DECODER_RESULT_SUCCESS) { |
1892 | 0 | result = BROTLI_DECODER_RESULT_ERROR; |
1893 | 0 | } |
1894 | 0 | return result; |
1895 | 0 | } |
1896 | | |
1897 | | /* Invariant: input stream is never overconsumed: |
1898 | | * invalid input implies that the whole stream is invalid -> any amount of |
1899 | | input could be read and discarded |
1900 | | * when result is "needs more input", then at least one more byte is REQUIRED |
1901 | | to complete decoding; all input data MUST be consumed by decoder, so |
1902 | | client could swap the input buffer |
1903 | | * when result is "needs more output" decoder MUST ensure that it doesn't |
1904 | | hold more than 7 bits in bit reader; this saves client from swapping input |
1905 | | buffer ahead of time |
1906 | | * when result is "success" decoder MUST return all unused data back to input |
1907 | | buffer; this is possible because the invariant is hold on enter |
1908 | | */ |
1909 | | BrotliDecoderResult BrotliDecoderDecompressStream( |
1910 | | BrotliDecoderState* s, size_t* available_in, const uint8_t** next_in, |
1911 | 0 | size_t* available_out, uint8_t** next_out, size_t* total_out) { |
1912 | 0 | BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS; |
1913 | 0 | BrotliBitReader* br = &s->br; |
1914 | | /* Ensure that *total_out is set, even if no data will ever be pushed out. */ |
1915 | 0 | if (total_out) { |
1916 | 0 | *total_out = s->partial_pos_out; |
1917 | 0 | } |
1918 | | /* Do not try to process further in a case of unrecoverable error. */ |
1919 | 0 | if ((int)s->error_code < 0) { |
1920 | 0 | return BROTLI_DECODER_RESULT_ERROR; |
1921 | 0 | } |
1922 | 0 | if (*available_out && (!next_out || !*next_out)) { |
1923 | 0 | return SaveErrorCode( |
1924 | 0 | s, BROTLI_FAILURE(BROTLI_DECODER_ERROR_INVALID_ARGUMENTS)); |
1925 | 0 | } |
1926 | 0 | if (!*available_out) next_out = 0; |
1927 | 0 | if (s->buffer_length == 0) { /* Just connect bit reader to input stream. */ |
1928 | 0 | br->avail_in = *available_in; |
1929 | 0 | br->next_in = *next_in; |
1930 | 0 | } else { |
1931 | | /* At least one byte of input is required. More than one byte of input may |
1932 | | be required to complete the transaction -> reading more data must be |
1933 | | done in a loop -> do it in a main loop. */ |
1934 | 0 | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
1935 | 0 | br->next_in = &s->buffer.u8[0]; |
1936 | 0 | } |
1937 | | /* State machine */ |
1938 | 0 | for (;;) { |
1939 | 0 | if (result != BROTLI_DECODER_SUCCESS) { /* Error, needs more input/output */ |
1940 | 0 | if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) { |
1941 | 0 | if (s->ringbuffer != 0) { /* Pro-actively push output. */ |
1942 | 0 | BrotliDecoderErrorCode intermediate_result = WriteRingBuffer(s, |
1943 | 0 | available_out, next_out, total_out, BROTLI_TRUE); |
1944 | | /* WriteRingBuffer checks s->meta_block_remaining_len validity. */ |
1945 | 0 | if ((int)intermediate_result < 0) { |
1946 | 0 | result = intermediate_result; |
1947 | 0 | break; |
1948 | 0 | } |
1949 | 0 | } |
1950 | 0 | if (s->buffer_length != 0) { /* Used with internal buffer. */ |
1951 | 0 | if (br->avail_in == 0) { /* Successfully finished read transaction. */ |
1952 | | /* Accumulator contains less than 8 bits, because internal buffer |
1953 | | is expanded byte-by-byte until it is enough to complete read. */ |
1954 | 0 | s->buffer_length = 0; |
1955 | | /* Switch to input stream and restart. */ |
1956 | 0 | result = BROTLI_DECODER_SUCCESS; |
1957 | 0 | br->avail_in = *available_in; |
1958 | 0 | br->next_in = *next_in; |
1959 | 0 | continue; |
1960 | 0 | } else if (*available_in != 0) { |
1961 | | /* Not enough data in buffer, but can take one more byte from |
1962 | | input stream. */ |
1963 | 0 | result = BROTLI_DECODER_SUCCESS; |
1964 | 0 | s->buffer.u8[s->buffer_length] = **next_in; |
1965 | 0 | s->buffer_length++; |
1966 | 0 | br->avail_in = s->buffer_length; |
1967 | 0 | (*next_in)++; |
1968 | 0 | (*available_in)--; |
1969 | | /* Retry with more data in buffer. */ |
1970 | 0 | continue; |
1971 | 0 | } |
1972 | | /* Can't finish reading and no more input.*/ |
1973 | 0 | break; |
1974 | 0 | } else { /* Input stream doesn't contain enough input. */ |
1975 | | /* Copy tail to internal buffer and return. */ |
1976 | 0 | *next_in = br->next_in; |
1977 | 0 | *available_in = br->avail_in; |
1978 | 0 | while (*available_in) { |
1979 | 0 | s->buffer.u8[s->buffer_length] = **next_in; |
1980 | 0 | s->buffer_length++; |
1981 | 0 | (*next_in)++; |
1982 | 0 | (*available_in)--; |
1983 | 0 | } |
1984 | 0 | break; |
1985 | 0 | } |
1986 | | /* Unreachable. */ |
1987 | 0 | } |
1988 | | |
1989 | | /* Fail or needs more output. */ |
1990 | | |
1991 | 0 | if (s->buffer_length != 0) { |
1992 | | /* Just consumed the buffered input and produced some output. Otherwise |
1993 | | it would result in "needs more input". Reset internal buffer.*/ |
1994 | 0 | s->buffer_length = 0; |
1995 | 0 | } else { |
1996 | | /* Using input stream in last iteration. When decoder switches to input |
1997 | | stream it has less than 8 bits in accumulator, so it is safe to |
1998 | | return unused accumulator bits there. */ |
1999 | 0 | BrotliBitReaderUnload(br); |
2000 | 0 | *available_in = br->avail_in; |
2001 | 0 | *next_in = br->next_in; |
2002 | 0 | } |
2003 | 0 | break; |
2004 | 0 | } |
2005 | 0 | switch (s->state) { |
2006 | 0 | case BROTLI_STATE_UNINITED: |
2007 | | /* Prepare to the first read. */ |
2008 | 0 | if (!BrotliWarmupBitReader(br)) { |
2009 | 0 | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
2010 | 0 | break; |
2011 | 0 | } |
2012 | | /* Decode window size. */ |
2013 | 0 | s->window_bits = DecodeWindowBits(br); /* Reads 1..7 bits. */ |
2014 | 0 | BROTLI_LOG_UINT(s->window_bits); |
2015 | 0 | if (s->window_bits == 9) { |
2016 | | /* Value 9 is reserved for future use. */ |
2017 | 0 | result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS); |
2018 | 0 | break; |
2019 | 0 | } |
2020 | | /* Maximum distance, see section 9.1. of the spec. */ |
2021 | 0 | s->max_backward_distance = (1 << s->window_bits) - BROTLI_WINDOW_GAP; |
2022 | | |
2023 | | /* Allocate memory for both block_type_trees and block_len_trees. */ |
2024 | 0 | s->block_type_trees = (HuffmanCode*)BROTLI_ALLOC(s, |
2025 | 0 | sizeof(HuffmanCode) * 3 * |
2026 | 0 | (BROTLI_HUFFMAN_MAX_SIZE_258 + BROTLI_HUFFMAN_MAX_SIZE_26)); |
2027 | 0 | if (s->block_type_trees == 0) { |
2028 | 0 | result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES); |
2029 | 0 | break; |
2030 | 0 | } |
2031 | 0 | s->block_len_trees = |
2032 | 0 | s->block_type_trees + 3 * BROTLI_HUFFMAN_MAX_SIZE_258; |
2033 | |
|
2034 | 0 | s->state = BROTLI_STATE_METABLOCK_BEGIN; |
2035 | | /* No break, continue to next state */ |
2036 | 0 | case BROTLI_STATE_METABLOCK_BEGIN: |
2037 | 0 | BrotliDecoderStateMetablockBegin(s); |
2038 | 0 | BROTLI_LOG_UINT(s->pos); |
2039 | 0 | s->state = BROTLI_STATE_METABLOCK_HEADER; |
2040 | | /* No break, continue to next state */ |
2041 | 0 | case BROTLI_STATE_METABLOCK_HEADER: |
2042 | 0 | result = DecodeMetaBlockLength(s, br); /* Reads 2 - 31 bits. */ |
2043 | 0 | if (result != BROTLI_DECODER_SUCCESS) { |
2044 | 0 | break; |
2045 | 0 | } |
2046 | 0 | BROTLI_LOG_UINT(s->is_last_metablock); |
2047 | 0 | BROTLI_LOG_UINT(s->meta_block_remaining_len); |
2048 | 0 | BROTLI_LOG_UINT(s->is_metadata); |
2049 | 0 | BROTLI_LOG_UINT(s->is_uncompressed); |
2050 | 0 | if (s->is_metadata || s->is_uncompressed) { |
2051 | 0 | if (!BrotliJumpToByteBoundary(br)) { |
2052 | 0 | result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_PADDING_1); |
2053 | 0 | break; |
2054 | 0 | } |
2055 | 0 | } |
2056 | 0 | if (s->is_metadata) { |
2057 | 0 | s->state = BROTLI_STATE_METADATA; |
2058 | 0 | break; |
2059 | 0 | } |
2060 | 0 | if (s->meta_block_remaining_len == 0) { |
2061 | 0 | s->state = BROTLI_STATE_METABLOCK_DONE; |
2062 | 0 | break; |
2063 | 0 | } |
2064 | 0 | BrotliCalculateRingBufferSize(s); |
2065 | 0 | if (s->is_uncompressed) { |
2066 | 0 | s->state = BROTLI_STATE_UNCOMPRESSED; |
2067 | 0 | break; |
2068 | 0 | } |
2069 | 0 | s->loop_counter = 0; |
2070 | 0 | s->state = BROTLI_STATE_HUFFMAN_CODE_0; |
2071 | 0 | break; |
2072 | 0 | case BROTLI_STATE_UNCOMPRESSED: { |
2073 | 0 | result = CopyUncompressedBlockToOutput( |
2074 | 0 | available_out, next_out, total_out, s); |
2075 | 0 | if (result != BROTLI_DECODER_SUCCESS) { |
2076 | 0 | break; |
2077 | 0 | } |
2078 | 0 | s->state = BROTLI_STATE_METABLOCK_DONE; |
2079 | 0 | break; |
2080 | 0 | } |
2081 | 0 | case BROTLI_STATE_METADATA: |
2082 | 0 | for (; s->meta_block_remaining_len > 0; --s->meta_block_remaining_len) { |
2083 | 0 | uint32_t bits; |
2084 | | /* Read one byte and ignore it. */ |
2085 | 0 | if (!BrotliSafeReadBits(br, 8, &bits)) { |
2086 | 0 | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
2087 | 0 | break; |
2088 | 0 | } |
2089 | 0 | } |
2090 | 0 | if (result == BROTLI_DECODER_SUCCESS) { |
2091 | 0 | s->state = BROTLI_STATE_METABLOCK_DONE; |
2092 | 0 | } |
2093 | 0 | break; |
2094 | 0 | case BROTLI_STATE_HUFFMAN_CODE_0: |
2095 | 0 | if (s->loop_counter >= 3) { |
2096 | 0 | s->state = BROTLI_STATE_METABLOCK_HEADER_2; |
2097 | 0 | break; |
2098 | 0 | } |
2099 | | /* Reads 1..11 bits. */ |
2100 | 0 | result = DecodeVarLenUint8(s, br, &s->num_block_types[s->loop_counter]); |
2101 | 0 | if (result != BROTLI_DECODER_SUCCESS) { |
2102 | 0 | break; |
2103 | 0 | } |
2104 | 0 | s->num_block_types[s->loop_counter]++; |
2105 | 0 | BROTLI_LOG_UINT(s->num_block_types[s->loop_counter]); |
2106 | 0 | if (s->num_block_types[s->loop_counter] < 2) { |
2107 | 0 | s->loop_counter++; |
2108 | 0 | break; |
2109 | 0 | } |
2110 | 0 | s->state = BROTLI_STATE_HUFFMAN_CODE_1; |
2111 | | /* No break, continue to next state */ |
2112 | 0 | case BROTLI_STATE_HUFFMAN_CODE_1: { |
2113 | 0 | int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_258; |
2114 | 0 | result = ReadHuffmanCode(s->num_block_types[s->loop_counter] + 2, |
2115 | 0 | &s->block_type_trees[tree_offset], NULL, s); |
2116 | 0 | if (result != BROTLI_DECODER_SUCCESS) break; |
2117 | 0 | s->state = BROTLI_STATE_HUFFMAN_CODE_2; |
2118 | | /* No break, continue to next state */ |
2119 | 0 | } |
2120 | 0 | case BROTLI_STATE_HUFFMAN_CODE_2: { |
2121 | 0 | int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26; |
2122 | 0 | result = ReadHuffmanCode(BROTLI_NUM_BLOCK_LEN_SYMBOLS, |
2123 | 0 | &s->block_len_trees[tree_offset], NULL, s); |
2124 | 0 | if (result != BROTLI_DECODER_SUCCESS) break; |
2125 | 0 | s->state = BROTLI_STATE_HUFFMAN_CODE_3; |
2126 | | /* No break, continue to next state */ |
2127 | 0 | } |
2128 | 0 | case BROTLI_STATE_HUFFMAN_CODE_3: { |
2129 | 0 | int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26; |
2130 | 0 | if (!SafeReadBlockLength(s, &s->block_length[s->loop_counter], |
2131 | 0 | &s->block_len_trees[tree_offset], br)) { |
2132 | 0 | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
2133 | 0 | break; |
2134 | 0 | } |
2135 | 0 | BROTLI_LOG_UINT(s->block_length[s->loop_counter]); |
2136 | 0 | s->loop_counter++; |
2137 | 0 | s->state = BROTLI_STATE_HUFFMAN_CODE_0; |
2138 | 0 | break; |
2139 | 0 | } |
2140 | 0 | case BROTLI_STATE_METABLOCK_HEADER_2: { |
2141 | 0 | uint32_t bits; |
2142 | 0 | if (!BrotliSafeReadBits(br, 6, &bits)) { |
2143 | 0 | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
2144 | 0 | break; |
2145 | 0 | } |
2146 | 0 | s->distance_postfix_bits = bits & BitMask(2); |
2147 | 0 | bits >>= 2; |
2148 | 0 | s->num_direct_distance_codes = BROTLI_NUM_DISTANCE_SHORT_CODES + |
2149 | 0 | (bits << s->distance_postfix_bits); |
2150 | 0 | BROTLI_LOG_UINT(s->num_direct_distance_codes); |
2151 | 0 | BROTLI_LOG_UINT(s->distance_postfix_bits); |
2152 | 0 | s->distance_postfix_mask = (int)BitMask(s->distance_postfix_bits); |
2153 | 0 | s->context_modes = |
2154 | 0 | (uint8_t*)BROTLI_ALLOC(s, (size_t)s->num_block_types[0]); |
2155 | 0 | if (s->context_modes == 0) { |
2156 | 0 | result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES); |
2157 | 0 | break; |
2158 | 0 | } |
2159 | 0 | s->loop_counter = 0; |
2160 | 0 | s->state = BROTLI_STATE_CONTEXT_MODES; |
2161 | | /* No break, continue to next state */ |
2162 | 0 | } |
2163 | 0 | case BROTLI_STATE_CONTEXT_MODES: |
2164 | 0 | result = ReadContextModes(s); |
2165 | 0 | if (result != BROTLI_DECODER_SUCCESS) { |
2166 | 0 | break; |
2167 | 0 | } |
2168 | 0 | s->state = BROTLI_STATE_CONTEXT_MAP_1; |
2169 | | /* No break, continue to next state */ |
2170 | 0 | case BROTLI_STATE_CONTEXT_MAP_1: |
2171 | 0 | result = DecodeContextMap( |
2172 | 0 | s->num_block_types[0] << BROTLI_LITERAL_CONTEXT_BITS, |
2173 | 0 | &s->num_literal_htrees, &s->context_map, s); |
2174 | 0 | if (result != BROTLI_DECODER_SUCCESS) { |
2175 | 0 | break; |
2176 | 0 | } |
2177 | 0 | DetectTrivialLiteralBlockTypes(s); |
2178 | 0 | s->state = BROTLI_STATE_CONTEXT_MAP_2; |
2179 | | /* No break, continue to next state */ |
2180 | 0 | case BROTLI_STATE_CONTEXT_MAP_2: |
2181 | 0 | { |
2182 | 0 | uint32_t num_distance_codes = s->num_direct_distance_codes + |
2183 | 0 | ((2 * BROTLI_MAX_DISTANCE_BITS) << s->distance_postfix_bits); |
2184 | 0 | BROTLI_BOOL allocation_success = BROTLI_TRUE; |
2185 | 0 | result = DecodeContextMap( |
2186 | 0 | s->num_block_types[2] << BROTLI_DISTANCE_CONTEXT_BITS, |
2187 | 0 | &s->num_dist_htrees, &s->dist_context_map, s); |
2188 | 0 | if (result != BROTLI_DECODER_SUCCESS) { |
2189 | 0 | break; |
2190 | 0 | } |
2191 | 0 | allocation_success &= BrotliDecoderHuffmanTreeGroupInit( |
2192 | 0 | s, &s->literal_hgroup, BROTLI_NUM_LITERAL_SYMBOLS, |
2193 | 0 | s->num_literal_htrees); |
2194 | 0 | allocation_success &= BrotliDecoderHuffmanTreeGroupInit( |
2195 | 0 | s, &s->insert_copy_hgroup, BROTLI_NUM_COMMAND_SYMBOLS, |
2196 | 0 | s->num_block_types[1]); |
2197 | 0 | allocation_success &= BrotliDecoderHuffmanTreeGroupInit( |
2198 | 0 | s, &s->distance_hgroup, num_distance_codes, |
2199 | 0 | s->num_dist_htrees); |
2200 | 0 | if (!allocation_success) { |
2201 | 0 | return SaveErrorCode(s, |
2202 | 0 | BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS)); |
2203 | 0 | } |
2204 | 0 | } |
2205 | 0 | s->loop_counter = 0; |
2206 | 0 | s->state = BROTLI_STATE_TREE_GROUP; |
2207 | | /* No break, continue to next state */ |
2208 | 0 | case BROTLI_STATE_TREE_GROUP: |
2209 | 0 | { |
2210 | 0 | HuffmanTreeGroup* hgroup = NULL; |
2211 | 0 | switch (s->loop_counter) { |
2212 | 0 | case 0: |
2213 | 0 | hgroup = &s->literal_hgroup; |
2214 | 0 | break; |
2215 | 0 | case 1: |
2216 | 0 | hgroup = &s->insert_copy_hgroup; |
2217 | 0 | break; |
2218 | 0 | case 2: |
2219 | 0 | hgroup = &s->distance_hgroup; |
2220 | 0 | break; |
2221 | 0 | default: |
2222 | 0 | return SaveErrorCode(s, BROTLI_FAILURE( |
2223 | 0 | BROTLI_DECODER_ERROR_UNREACHABLE)); |
2224 | 0 | } |
2225 | 0 | result = HuffmanTreeGroupDecode(hgroup, s); |
2226 | 0 | } |
2227 | 0 | if (result != BROTLI_DECODER_SUCCESS) break; |
2228 | 0 | s->loop_counter++; |
2229 | 0 | if (s->loop_counter >= 3) { |
2230 | 0 | PrepareLiteralDecoding(s); |
2231 | 0 | s->dist_context_map_slice = s->dist_context_map; |
2232 | 0 | s->htree_command = s->insert_copy_hgroup.htrees[0]; |
2233 | 0 | if (!BrotliEnsureRingBuffer(s)) { |
2234 | 0 | result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2); |
2235 | 0 | break; |
2236 | 0 | } |
2237 | 0 | s->state = BROTLI_STATE_COMMAND_BEGIN; |
2238 | 0 | } |
2239 | 0 | break; |
2240 | 0 | case BROTLI_STATE_COMMAND_BEGIN: |
2241 | 0 | case BROTLI_STATE_COMMAND_INNER: |
2242 | 0 | case BROTLI_STATE_COMMAND_POST_DECODE_LITERALS: |
2243 | 0 | case BROTLI_STATE_COMMAND_POST_WRAP_COPY: |
2244 | 0 | result = ProcessCommands(s); |
2245 | 0 | if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) { |
2246 | 0 | result = SafeProcessCommands(s); |
2247 | 0 | } |
2248 | 0 | break; |
2249 | 0 | case BROTLI_STATE_COMMAND_INNER_WRITE: |
2250 | 0 | case BROTLI_STATE_COMMAND_POST_WRITE_1: |
2251 | 0 | case BROTLI_STATE_COMMAND_POST_WRITE_2: |
2252 | 0 | result = WriteRingBuffer( |
2253 | 0 | s, available_out, next_out, total_out, BROTLI_FALSE); |
2254 | 0 | if (result != BROTLI_DECODER_SUCCESS) { |
2255 | 0 | break; |
2256 | 0 | } |
2257 | 0 | WrapRingBuffer(s); |
2258 | 0 | if (s->ringbuffer_size == 1 << s->window_bits) { |
2259 | 0 | s->max_distance = s->max_backward_distance; |
2260 | 0 | } |
2261 | 0 | if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_1) { |
2262 | 0 | if (s->meta_block_remaining_len == 0) { |
2263 | | /* Next metablock, if any */ |
2264 | 0 | s->state = BROTLI_STATE_METABLOCK_DONE; |
2265 | 0 | } else { |
2266 | 0 | s->state = BROTLI_STATE_COMMAND_BEGIN; |
2267 | 0 | } |
2268 | 0 | break; |
2269 | 0 | } else if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_2) { |
2270 | 0 | s->state = BROTLI_STATE_COMMAND_POST_WRAP_COPY; |
2271 | 0 | } else { /* BROTLI_STATE_COMMAND_INNER_WRITE */ |
2272 | 0 | if (s->loop_counter == 0) { |
2273 | 0 | if (s->meta_block_remaining_len == 0) { |
2274 | 0 | s->state = BROTLI_STATE_METABLOCK_DONE; |
2275 | 0 | } else { |
2276 | 0 | s->state = BROTLI_STATE_COMMAND_POST_DECODE_LITERALS; |
2277 | 0 | } |
2278 | 0 | break; |
2279 | 0 | } |
2280 | 0 | s->state = BROTLI_STATE_COMMAND_INNER; |
2281 | 0 | } |
2282 | 0 | break; |
2283 | 0 | case BROTLI_STATE_METABLOCK_DONE: |
2284 | 0 | if (s->meta_block_remaining_len < 0) { |
2285 | 0 | result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2); |
2286 | 0 | break; |
2287 | 0 | } |
2288 | 0 | BrotliDecoderStateCleanupAfterMetablock(s); |
2289 | 0 | if (!s->is_last_metablock) { |
2290 | 0 | s->state = BROTLI_STATE_METABLOCK_BEGIN; |
2291 | 0 | break; |
2292 | 0 | } |
2293 | 0 | if (!BrotliJumpToByteBoundary(br)) { |
2294 | 0 | result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_PADDING_2); |
2295 | 0 | break; |
2296 | 0 | } |
2297 | 0 | if (s->buffer_length == 0) { |
2298 | 0 | BrotliBitReaderUnload(br); |
2299 | 0 | *available_in = br->avail_in; |
2300 | 0 | *next_in = br->next_in; |
2301 | 0 | } |
2302 | 0 | s->state = BROTLI_STATE_DONE; |
2303 | | /* No break, continue to next state */ |
2304 | 0 | case BROTLI_STATE_DONE: |
2305 | 0 | if (s->ringbuffer != 0) { |
2306 | 0 | result = WriteRingBuffer( |
2307 | 0 | s, available_out, next_out, total_out, BROTLI_TRUE); |
2308 | 0 | if (result != BROTLI_DECODER_SUCCESS) { |
2309 | 0 | break; |
2310 | 0 | } |
2311 | 0 | } |
2312 | 0 | return SaveErrorCode(s, result); |
2313 | 0 | } |
2314 | 0 | } |
2315 | 0 | return SaveErrorCode(s, result); |
2316 | 0 | } |
2317 | | |
2318 | 0 | BROTLI_BOOL BrotliDecoderHasMoreOutput(const BrotliDecoderState* s) { |
2319 | | /* After unrecoverable error remaining output is considered nonsensical. */ |
2320 | 0 | if ((int)s->error_code < 0) { |
2321 | 0 | return BROTLI_FALSE; |
2322 | 0 | } |
2323 | 0 | return TO_BROTLI_BOOL( |
2324 | 0 | s->ringbuffer != 0 && UnwrittenBytes(s, BROTLI_FALSE) != 0); |
2325 | 0 | } |
2326 | | |
2327 | 0 | const uint8_t* BrotliDecoderTakeOutput(BrotliDecoderState* s, size_t* size) { |
2328 | 0 | uint8_t* result = 0; |
2329 | 0 | size_t available_out = *size ? *size : 1u << 24; |
2330 | 0 | size_t requested_out = available_out; |
2331 | 0 | BrotliDecoderErrorCode status; |
2332 | 0 | if ((s->ringbuffer == 0) || ((int)s->error_code < 0)) { |
2333 | 0 | *size = 0; |
2334 | 0 | return 0; |
2335 | 0 | } |
2336 | 0 | WrapRingBuffer(s); |
2337 | 0 | status = WriteRingBuffer(s, &available_out, &result, 0, BROTLI_TRUE); |
2338 | | /* Either WriteRingBuffer returns those "success" codes... */ |
2339 | 0 | if (status == BROTLI_DECODER_SUCCESS || |
2340 | 0 | status == BROTLI_DECODER_NEEDS_MORE_OUTPUT) { |
2341 | 0 | *size = requested_out - available_out; |
2342 | 0 | } else { |
2343 | | /* ... or stream is broken. Normally this should be caught by |
2344 | | BrotliDecoderDecompressStream, this is just a safeguard. */ |
2345 | 0 | if ((int)status < 0) SaveErrorCode(s, status); |
2346 | 0 | *size = 0; |
2347 | 0 | result = 0; |
2348 | 0 | } |
2349 | 0 | return result; |
2350 | 0 | } |
2351 | | |
2352 | 0 | BROTLI_BOOL BrotliDecoderIsUsed(const BrotliDecoderState* s) { |
2353 | 0 | return TO_BROTLI_BOOL(s->state != BROTLI_STATE_UNINITED || |
2354 | 0 | BrotliGetAvailableBits(&s->br) != 0); |
2355 | 0 | } |
2356 | | |
2357 | 0 | BROTLI_BOOL BrotliDecoderIsFinished(const BrotliDecoderState* s) { |
2358 | 0 | return TO_BROTLI_BOOL(s->state == BROTLI_STATE_DONE) && |
2359 | 0 | !BrotliDecoderHasMoreOutput(s); |
2360 | 0 | } |
2361 | | |
2362 | 0 | BrotliDecoderErrorCode BrotliDecoderGetErrorCode(const BrotliDecoderState* s) { |
2363 | 0 | return (BrotliDecoderErrorCode)s->error_code; |
2364 | 0 | } |
2365 | | |
2366 | 0 | const char* BrotliDecoderErrorString(BrotliDecoderErrorCode c) { |
2367 | 0 | switch (c) { |
2368 | 0 | #define BROTLI_ERROR_CODE_CASE_(PREFIX, NAME, CODE) \ |
2369 | 0 | case BROTLI_DECODER ## PREFIX ## NAME: return #NAME; |
2370 | 0 | #define BROTLI_NOTHING_ |
2371 | 0 | BROTLI_DECODER_ERROR_CODES_LIST(BROTLI_ERROR_CODE_CASE_, BROTLI_NOTHING_) |
2372 | 0 | #undef BROTLI_ERROR_CODE_CASE_ |
2373 | 0 | #undef BROTLI_NOTHING_ |
2374 | 0 | default: return "INVALID"; |
2375 | 0 | } |
2376 | 0 | } |
2377 | | |
2378 | 0 | uint32_t BrotliDecoderVersion() { |
2379 | 0 | return BROTLI_VERSION; |
2380 | 0 | } |
2381 | | |
2382 | | #if defined(__cplusplus) || defined(c_plusplus) |
2383 | | } /* extern "C" */ |
2384 | | #endif |