comparison mupdf-source/thirdparty/brotli/c/dec/decode.c @ 2:b50eed0cc0ef upstream

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