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