1 /* Copyright 2013 Google Inc. All Rights Reserved.
3 Distributed under MIT license.
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
10 #include "./bit_reader.h"
11 #include "./context.h"
13 #include "./dictionary.h"
15 #include "./transform.h"
16 #include "./huffman.h"
23 #if defined(__cplusplus) || defined(c_plusplus)
27 #ifdef BROTLI_DECODE_DEBUG
28 #define BROTLI_LOG_UINT(name) \
29 printf("[%s] %s = %lu\n", __func__, #name, (unsigned long)(name))
30 #define BROTLI_LOG_ARRAY_INDEX(array_name, idx) \
31 printf("[%s] %s[%lu] = %lu\n", __func__, #array_name, \
32 (unsigned long)(idx), (unsigned long)array_name[idx])
33 #define BROTLI_LOG(x) printf x
35 #define BROTLI_LOG_UINT(name)
36 #define BROTLI_LOG_ARRAY_INDEX(array_name, idx)
40 static const uint32_t kDefaultCodeLength
= 8;
41 static const uint32_t kCodeLengthRepeatCode
= 16;
42 static const uint32_t kNumLiteralCodes
= 256;
43 static const uint32_t kNumInsertAndCopyCodes
= 704;
44 static const uint32_t kNumBlockLengthCodes
= 26;
45 static const int kLiteralContextBits
= 6;
46 static const int kDistanceContextBits
= 2;
48 #define HUFFMAN_TABLE_BITS 8U
49 #define HUFFMAN_TABLE_MASK 0xff
51 #define CODE_LENGTH_CODES 18
52 static const uint8_t kCodeLengthCodeOrder
[CODE_LENGTH_CODES
] = {
53 1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15,
56 /* Static prefix code for the complex code length code lengths. */
57 static const uint8_t kCodeLengthPrefixLength
[16] = {
58 2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 3, 2, 2, 2, 4,
61 static const uint8_t kCodeLengthPrefixValue
[16] = {
62 0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5,
65 #define NUM_DISTANCE_SHORT_CODES 16
67 BrotliState
* BrotliCreateState(
68 brotli_alloc_func alloc_func
, brotli_free_func free_func
, void* opaque
) {
69 BrotliState
* state
= 0;
70 if (!alloc_func
&& !free_func
) {
71 state
= (BrotliState
*)malloc(sizeof(BrotliState
));
72 } else if (alloc_func
&& free_func
) {
73 state
= (BrotliState
*)alloc_func(opaque
, sizeof(BrotliState
));
76 (void)BROTLI_FAILURE();
79 BrotliStateInitWithCustomAllocators(state
, alloc_func
, free_func
, opaque
);
83 /* Deinitializes and frees BrotliState instance. */
84 void BrotliDestroyState(BrotliState
* state
) {
88 brotli_free_func free_func
= state
->free_func
;
89 void* opaque
= state
->memory_manager_opaque
;
90 BrotliStateCleanup(state
);
91 free_func(opaque
, state
);
95 /* Decodes a number in the range [9..24], by reading 1 - 7 bits.
96 Precondition: bit-reader accumulator has at least 7 bits. */
97 static uint32_t DecodeWindowBits(BrotliBitReader
* br
) {
99 BrotliTakeBits(br
, 1, &n
);
103 BrotliTakeBits(br
, 3, &n
);
107 BrotliTakeBits(br
, 3, &n
);
114 static BROTLI_INLINE BROTLI_NO_ASAN
void memmove16(
115 uint8_t* dst
, uint8_t* src
) {
116 #if BROTLI_SAFE_MEMMOVE
117 /* For x86 this compiles to the same binary as signle memcpy.
118 On ARM memcpy is not inlined, so it works slower.
119 This implementation makes decompression 1% slower than regular one,
120 and 2% slower than NEON implementation.
123 memcpy(buffer
, src
, 16);
124 memcpy(dst
, buffer
, 16);
125 #elif defined(__ARM_NEON__)
126 vst1q_u8(dst
, vld1q_u8(src
));
128 /* memcpy is unsafe for overlapping regions and ASAN detects this.
129 But, because of optimizations, it works exactly as memmove:
130 copies data to registers first, and then stores them to dst. */
131 memcpy(dst
, src
, 16);
135 /* Decodes a number in the range [0..255], by reading 1 - 11 bits. */
136 static BROTLI_NOINLINE BrotliResult
DecodeVarLenUint8(BrotliState
* s
,
137 BrotliBitReader
* br
, uint32_t* value
) {
139 switch (s
->substate_decode_uint8
) {
140 case BROTLI_STATE_DECODE_UINT8_NONE
:
141 if (PREDICT_FALSE(!BrotliSafeReadBits(br
, 1, &bits
))) {
142 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
146 return BROTLI_RESULT_SUCCESS
;
148 /* No break, transit to the next state. */
150 case BROTLI_STATE_DECODE_UINT8_SHORT
:
151 if (PREDICT_FALSE(!BrotliSafeReadBits(br
, 3, &bits
))) {
152 s
->substate_decode_uint8
= BROTLI_STATE_DECODE_UINT8_SHORT
;
153 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
157 s
->substate_decode_uint8
= BROTLI_STATE_DECODE_UINT8_NONE
;
158 return BROTLI_RESULT_SUCCESS
;
160 /* Use output value as a temporary storage. It MUST be persisted. */
162 /* No break, transit to the next state. */
164 case BROTLI_STATE_DECODE_UINT8_LONG
:
165 if (PREDICT_FALSE(!BrotliSafeReadBits(br
, *value
, &bits
))) {
166 s
->substate_decode_uint8
= BROTLI_STATE_DECODE_UINT8_LONG
;
167 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
169 *value
= (1U << *value
) + bits
;
170 s
->substate_decode_uint8
= BROTLI_STATE_DECODE_UINT8_NONE
;
171 return BROTLI_RESULT_SUCCESS
;
174 return BROTLI_FAILURE();
178 /* Decodes a metablock length and flags by reading 2 - 31 bits. */
179 static BrotliResult BROTLI_NOINLINE
DecodeMetaBlockLength(BrotliState
* s
,
180 BrotliBitReader
* br
) {
184 switch (s
->substate_metablock_header
) {
185 case BROTLI_STATE_METABLOCK_HEADER_NONE
:
186 if (!BrotliSafeReadBits(br
, 1, &bits
)) {
187 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
189 s
->is_last_metablock
= (uint8_t)bits
;
190 s
->meta_block_remaining_len
= 0;
191 s
->is_uncompressed
= 0;
193 if (!s
->is_last_metablock
) {
194 s
->substate_metablock_header
= BROTLI_STATE_METABLOCK_HEADER_NIBBLES
;
197 s
->substate_metablock_header
= BROTLI_STATE_METABLOCK_HEADER_EMPTY
;
198 /* No break, transit to the next state. */
200 case BROTLI_STATE_METABLOCK_HEADER_EMPTY
:
201 if (!BrotliSafeReadBits(br
, 1, &bits
)) {
202 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
205 s
->substate_metablock_header
= BROTLI_STATE_METABLOCK_HEADER_NONE
;
206 return BROTLI_RESULT_SUCCESS
;
208 s
->substate_metablock_header
= BROTLI_STATE_METABLOCK_HEADER_NIBBLES
;
209 /* No break, transit to the next state. */
211 case BROTLI_STATE_METABLOCK_HEADER_NIBBLES
:
212 if (!BrotliSafeReadBits(br
, 2, &bits
)) {
213 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
215 s
->size_nibbles
= (uint8_t)(bits
+ 4);
219 s
->substate_metablock_header
= BROTLI_STATE_METABLOCK_HEADER_RESERVED
;
222 s
->substate_metablock_header
= BROTLI_STATE_METABLOCK_HEADER_SIZE
;
223 /* No break, transit to the next state. */
225 case BROTLI_STATE_METABLOCK_HEADER_SIZE
:
227 for (; i
< s
->size_nibbles
; ++i
) {
228 if (!BrotliSafeReadBits(br
, 4, &bits
)) {
230 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
232 if (i
+ 1 == s
->size_nibbles
&& s
->size_nibbles
> 4 && bits
== 0) {
233 return BROTLI_FAILURE();
235 s
->meta_block_remaining_len
|= (int)(bits
<< (i
* 4));
237 s
->substate_metablock_header
=
238 BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED
;
239 /* No break, transit to the next state. */
241 case BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED
:
242 if (!s
->is_last_metablock
&& !s
->is_metadata
) {
243 if (!BrotliSafeReadBits(br
, 1, &bits
)) {
244 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
246 s
->is_uncompressed
= (uint8_t)bits
;
248 ++s
->meta_block_remaining_len
;
249 s
->substate_metablock_header
= BROTLI_STATE_METABLOCK_HEADER_NONE
;
250 return BROTLI_RESULT_SUCCESS
;
252 case BROTLI_STATE_METABLOCK_HEADER_RESERVED
:
253 if (!BrotliSafeReadBits(br
, 1, &bits
)) {
254 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
257 return BROTLI_FAILURE();
259 s
->substate_metablock_header
= BROTLI_STATE_METABLOCK_HEADER_BYTES
;
260 /* No break, transit to the next state. */
262 case BROTLI_STATE_METABLOCK_HEADER_BYTES
:
263 if (!BrotliSafeReadBits(br
, 2, &bits
)) {
264 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
267 s
->substate_metablock_header
= BROTLI_STATE_METABLOCK_HEADER_NONE
;
268 return BROTLI_RESULT_SUCCESS
;
270 s
->size_nibbles
= (uint8_t)bits
;
271 s
->substate_metablock_header
= BROTLI_STATE_METABLOCK_HEADER_METADATA
;
272 /* No break, transit to the next state. */
274 case BROTLI_STATE_METABLOCK_HEADER_METADATA
:
276 for (; i
< s
->size_nibbles
; ++i
) {
277 if (!BrotliSafeReadBits(br
, 8, &bits
)) {
279 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
281 if (i
+ 1 == s
->size_nibbles
&& s
->size_nibbles
> 1 && bits
== 0) {
282 return BROTLI_FAILURE();
284 s
->meta_block_remaining_len
|= (int)(bits
<< (i
* 8));
286 s
->substate_metablock_header
=
287 BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED
;
291 return BROTLI_FAILURE();
296 /* Decodes the Huffman code.
297 This method doesn't read data from the bit reader, BUT drops the amount of
298 bits that correspond to the decoded symbol.
299 bits MUST contain at least 15 (BROTLI_HUFFMAN_MAX_CODE_LENGTH) valid bits. */
300 static BROTLI_INLINE
uint32_t DecodeSymbol(uint32_t bits
,
301 const HuffmanCode
* table
,
302 BrotliBitReader
* br
) {
303 table
+= bits
& HUFFMAN_TABLE_MASK
;
304 if (table
->bits
> HUFFMAN_TABLE_BITS
) {
305 uint32_t nbits
= table
->bits
- HUFFMAN_TABLE_BITS
;
306 BrotliDropBits(br
, HUFFMAN_TABLE_BITS
);
307 table
+= table
->value
;
308 table
+= (bits
>> HUFFMAN_TABLE_BITS
) & BitMask(nbits
);
310 BrotliDropBits(br
, table
->bits
);
314 /* Reads and decodes the next Huffman code from bit-stream.
315 This method peeks 16 bits of input and drops 0 - 15 of them. */
316 static BROTLI_INLINE
uint32_t ReadSymbol(const HuffmanCode
* table
,
317 BrotliBitReader
* br
) {
318 return DecodeSymbol(BrotliGet16BitsUnmasked(br
), table
, br
);
321 /* Same as DecodeSymbol, but it is known that there is less than 15 bits of
322 input are currently available. */
323 static BROTLI_NOINLINE
int SafeDecodeSymbol(const HuffmanCode
* table
,
327 uint32_t available_bits
= BrotliGetAvailableBits(br
);
328 if (available_bits
== 0) {
329 if (table
->bits
== 0) {
330 *result
= table
->value
;
333 return 0; /* No valid bits at all. */
335 val
= (uint32_t)BrotliGetBitsUnmasked(br
);
336 table
+= val
& HUFFMAN_TABLE_MASK
;
337 if (table
->bits
<= HUFFMAN_TABLE_BITS
) {
338 if (table
->bits
<= available_bits
) {
339 BrotliDropBits(br
, table
->bits
);
340 *result
= table
->value
;
343 return 0; /* Not enough bits for the first level. */
346 if (available_bits
<= HUFFMAN_TABLE_BITS
) {
347 return 0; /* Not enough bits to move to the second level. */
350 /* Speculatively drop HUFFMAN_TABLE_BITS. */
351 val
= (val
& BitMask(table
->bits
)) >> HUFFMAN_TABLE_BITS
;
352 available_bits
-= HUFFMAN_TABLE_BITS
;
353 table
+= table
->value
+ val
;
354 if (available_bits
< table
->bits
) {
355 return 0; /* Not enough bits for the second level. */
358 BrotliDropBits(br
, HUFFMAN_TABLE_BITS
+ table
->bits
);
359 *result
= table
->value
;
363 static BROTLI_INLINE
int SafeReadSymbol(const HuffmanCode
* table
,
367 if (PREDICT_TRUE(BrotliSafeGetBits(br
, 15, &val
))) {
368 *result
= DecodeSymbol(val
, table
, br
);
371 return SafeDecodeSymbol(table
, br
, result
);
375 /* Makes a look-up in first level Huffman table. Peeks 8 bits. */
376 static BROTLI_INLINE
void PreloadSymbol(int safe
,
377 const HuffmanCode
* table
,
384 table
+= BrotliGetBits(br
, HUFFMAN_TABLE_BITS
);
386 *value
= table
->value
;
389 /* Decodes the next Huffman code using data prepared by PreloadSymbol.
390 Reads 0 - 15 bits. Also peeks 8 following bits. */
391 static BROTLI_INLINE
uint32_t ReadPreloadedSymbol(const HuffmanCode
* table
,
395 uint32_t result
= *value
;
396 if (PREDICT_FALSE(*bits
> HUFFMAN_TABLE_BITS
)) {
397 uint32_t val
= BrotliGet16BitsUnmasked(br
);
398 const HuffmanCode
* ext
= table
+ (val
& HUFFMAN_TABLE_MASK
) + *value
;
399 uint32_t mask
= BitMask((*bits
- HUFFMAN_TABLE_BITS
));
400 BrotliDropBits(br
, HUFFMAN_TABLE_BITS
);
401 ext
+= (val
>> HUFFMAN_TABLE_BITS
) & mask
;
402 BrotliDropBits(br
, ext
->bits
);
405 BrotliDropBits(br
, *bits
);
407 PreloadSymbol(0, table
, br
, bits
, value
);
411 static BROTLI_INLINE
uint32_t Log2Floor(uint32_t x
) {
420 /* Reads (s->symbol + 1) symbols.
421 Totally 1..4 symbols are read, 1..10 bits each.
422 The list of symbols MUST NOT contain duplicates.
424 static BrotliResult
ReadSimpleHuffmanSymbols(uint32_t alphabet_size
,
426 /* max_bits == 1..10; symbol == 0..3; 1..40 bits will be read. */
427 BrotliBitReader
* br
= &s
->br
;
428 uint32_t max_bits
= Log2Floor(alphabet_size
- 1);
429 uint32_t i
= s
->sub_loop_counter
;
430 uint32_t num_symbols
= s
->symbol
;
431 while (i
<= num_symbols
) {
433 if (PREDICT_FALSE(!BrotliSafeReadBits(br
, max_bits
, &v
))) {
434 s
->sub_loop_counter
= i
;
435 s
->substate_huffman
= BROTLI_STATE_HUFFMAN_SIMPLE_READ
;
436 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
438 if (v
>= alphabet_size
) {
439 return BROTLI_FAILURE();
441 s
->symbols_lists_array
[i
] = (uint16_t)v
;
442 BROTLI_LOG_UINT(s
->symbols_lists_array
[i
]);
446 for (i
= 0; i
< num_symbols
; ++i
) {
448 for (; k
<= num_symbols
; ++k
) {
449 if (s
->symbols_lists_array
[i
] == s
->symbols_lists_array
[k
]) {
450 return BROTLI_FAILURE();
455 return BROTLI_RESULT_SUCCESS
;
458 /* Process single decoded symbol code length:
459 A) reset the repeat variable
460 B) remember code length (if it is not 0)
461 C) extend corredponding index-chain
462 D) reduce the huffman space
463 E) update the histogram
465 static BROTLI_INLINE
void ProcessSingleCodeLength(uint32_t code_len
,
466 uint32_t* symbol
, uint32_t* repeat
, uint32_t* space
,
467 uint32_t* prev_code_len
, uint16_t* symbol_lists
,
468 uint16_t* code_length_histo
, int* next_symbol
) {
470 if (code_len
!= 0) { /* code_len == 1..15 */
471 symbol_lists
[next_symbol
[code_len
]] = (uint16_t)(*symbol
);
472 next_symbol
[code_len
] = (int)(*symbol
);
473 *prev_code_len
= code_len
;
474 *space
-= 32768U >> code_len
;
475 code_length_histo
[code_len
]++;
480 /* Process repeated symbol code length.
481 A) Check if it is the extension of previous repeat sequence; if the decoded
482 value is not kCodeLengthRepeatCode, then it is a new symbol-skip
483 B) Update repeat variable
484 C) Check if operation is feasible (fits alphapet)
485 D) For each symbol do the same operations as in ProcessSingleCodeLength
487 PRECONDITION: code_len == kCodeLengthRepeatCode or kCodeLengthRepeatCode + 1
489 static BROTLI_INLINE
void ProcessRepeatedCodeLength(uint32_t code_len
,
490 uint32_t repeat_delta
, uint32_t alphabet_size
, uint32_t* symbol
,
491 uint32_t* repeat
, uint32_t* space
, uint32_t* prev_code_len
,
492 uint32_t* repeat_code_len
, uint16_t* symbol_lists
,
493 uint16_t* code_length_histo
, int* next_symbol
) {
495 uint32_t new_len
= 0;
496 if (code_len
== kCodeLengthRepeatCode
) {
497 new_len
= *prev_code_len
;
499 if (*repeat_code_len
!= new_len
) {
501 *repeat_code_len
= new_len
;
503 old_repeat
= *repeat
;
506 *repeat
<<= code_len
- 14U;
508 *repeat
+= repeat_delta
+ 3U;
509 repeat_delta
= *repeat
- old_repeat
;
510 if (*symbol
+ repeat_delta
> alphabet_size
) {
511 (void)BROTLI_FAILURE();
512 *symbol
= alphabet_size
;
516 if (*repeat_code_len
!= 0) {
517 unsigned last
= *symbol
+ repeat_delta
;
518 int next
= next_symbol
[*repeat_code_len
];
520 symbol_lists
[next
] = (uint16_t)*symbol
;
522 } while (++(*symbol
) != last
);
523 next_symbol
[*repeat_code_len
] = next
;
524 *space
-= repeat_delta
<< (15 - *repeat_code_len
);
525 code_length_histo
[*repeat_code_len
] = (uint16_t)
526 (code_length_histo
[*repeat_code_len
] + repeat_delta
);
528 *symbol
+= repeat_delta
;
532 /* Reads and decodes symbol codelengths. */
533 static BrotliResult
ReadSymbolCodeLengths(
534 uint32_t alphabet_size
, BrotliState
* s
) {
535 BrotliBitReader
* br
= &s
->br
;
536 uint32_t symbol
= s
->symbol
;
537 uint32_t repeat
= s
->repeat
;
538 uint32_t space
= s
->space
;
539 uint32_t prev_code_len
= s
->prev_code_len
;
540 uint32_t repeat_code_len
= s
->repeat_code_len
;
541 uint16_t* symbol_lists
= s
->symbol_lists
;
542 uint16_t* code_length_histo
= s
->code_length_histo
;
543 int* next_symbol
= s
->next_symbol
;
544 if (!BrotliWarmupBitReader(br
)) {
545 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
547 while (symbol
< alphabet_size
&& space
> 0) {
548 const HuffmanCode
* p
= s
->table
;
550 if (!BrotliCheckInputAmount(br
, BROTLI_SHORT_FILL_BIT_WINDOW_READ
)) {
553 s
->prev_code_len
= prev_code_len
;
554 s
->repeat_code_len
= repeat_code_len
;
556 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
558 BrotliFillBitWindow16(br
);
559 p
+= BrotliGetBitsUnmasked(br
) &
560 BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH
);
561 BrotliDropBits(br
, p
->bits
); /* Use 1..5 bits */
562 code_len
= p
->value
; /* code_len == 0..17 */
563 if (code_len
< kCodeLengthRepeatCode
) {
564 ProcessSingleCodeLength(code_len
, &symbol
, &repeat
, &space
,
565 &prev_code_len
, symbol_lists
, code_length_histo
, next_symbol
);
566 } else { /* code_len == 16..17, extra_bits == 2..3 */
567 uint32_t repeat_delta
=
568 (uint32_t)BrotliGetBitsUnmasked(br
) & BitMask(code_len
- 14U);
569 BrotliDropBits(br
, code_len
- 14U);
570 ProcessRepeatedCodeLength(code_len
, repeat_delta
, alphabet_size
,
571 &symbol
, &repeat
, &space
, &prev_code_len
, &repeat_code_len
,
572 symbol_lists
, code_length_histo
, next_symbol
);
576 return BROTLI_RESULT_SUCCESS
;
579 static BrotliResult
SafeReadSymbolCodeLengths(
580 uint32_t alphabet_size
, BrotliState
* s
) {
581 BrotliBitReader
* br
= &s
->br
;
582 while (s
->symbol
< alphabet_size
&& s
->space
> 0) {
583 const HuffmanCode
* p
= s
->table
;
586 uint32_t available_bits
= BrotliGetAvailableBits(br
);
587 if (available_bits
!= 0) {
588 bits
= (uint32_t)BrotliGetBitsUnmasked(br
);
590 p
+= bits
& BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH
);
591 if (p
->bits
> available_bits
) goto pullMoreInput
;
592 code_len
= p
->value
; /* code_len == 0..17 */
593 if (code_len
< kCodeLengthRepeatCode
) {
594 BrotliDropBits(br
, p
->bits
);
595 ProcessSingleCodeLength(code_len
, &s
->symbol
, &s
->repeat
, &s
->space
,
596 &s
->prev_code_len
, s
->symbol_lists
, s
->code_length_histo
,
598 } else { /* code_len == 16..17, extra_bits == 2..3 */
599 uint32_t extra_bits
= code_len
- 14U;
600 uint32_t repeat_delta
= (bits
>> p
->bits
) & BitMask(extra_bits
);
601 if (available_bits
< p
->bits
+ extra_bits
) goto pullMoreInput
;
602 BrotliDropBits(br
, p
->bits
+ extra_bits
);
603 ProcessRepeatedCodeLength(code_len
, repeat_delta
, alphabet_size
,
604 &s
->symbol
, &s
->repeat
, &s
->space
, &s
->prev_code_len
,
605 &s
->repeat_code_len
, s
->symbol_lists
, s
->code_length_histo
,
611 if (!BrotliPullByte(br
)) {
612 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
615 return BROTLI_RESULT_SUCCESS
;
618 /* Reads and decodes 15..18 codes using static prefix code.
619 Each code is 2..4 bits long. In total 30..72 bits are used. */
620 static BrotliResult
ReadCodeLengthCodeLengths(BrotliState
* s
) {
621 BrotliBitReader
* br
= &s
->br
;
622 uint32_t num_codes
= s
->repeat
;
623 unsigned space
= s
->space
;
624 uint32_t i
= s
->sub_loop_counter
;
625 for (; i
< CODE_LENGTH_CODES
; ++i
) {
626 const uint8_t code_len_idx
= kCodeLengthCodeOrder
[i
];
629 if (PREDICT_FALSE(!BrotliSafeGetBits(br
, 4, &ix
))) {
630 uint32_t available_bits
= BrotliGetAvailableBits(br
);
631 if (available_bits
!= 0) {
632 ix
= BrotliGetBitsUnmasked(br
) & 0xF;
636 if (kCodeLengthPrefixLength
[ix
] > available_bits
) {
637 s
->sub_loop_counter
= i
;
638 s
->repeat
= num_codes
;
640 s
->substate_huffman
= BROTLI_STATE_HUFFMAN_COMPLEX
;
641 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
644 v
= kCodeLengthPrefixValue
[ix
];
645 BrotliDropBits(br
, kCodeLengthPrefixLength
[ix
]);
646 s
->code_length_code_lengths
[code_len_idx
] = (uint8_t)v
;
647 BROTLI_LOG_ARRAY_INDEX(s
->code_length_code_lengths
, code_len_idx
);
649 space
= space
- (32U >> v
);
651 ++s
->code_length_histo
[v
];
652 if (space
- 1U >= 32U) {
653 /* space is 0 or wrapped around */
658 if (!(num_codes
== 1 || space
== 0)) {
659 return BROTLI_FAILURE();
661 return BROTLI_RESULT_SUCCESS
;
664 /* Decodes the Huffman tables.
665 There are 2 scenarios:
666 A) Huffman code contains only few symbols (1..4). Those symbols are read
667 directly; their code lengths are defined by the number of symbols.
668 For this scenario 4 - 45 bits will be read.
671 B.1) Small Huffman table is decoded; it is specified with code lengths
672 encoded with predefined entropy code. 32 - 74 bits are used.
673 B.2) Decoded table is used to decode code lengths of symbols in resulting
674 Huffman table. In worst case 3520 bits are read.
676 static BrotliResult
ReadHuffmanCode(uint32_t alphabet_size
,
678 uint32_t* opt_table_size
,
680 BrotliBitReader
* br
= &s
->br
;
681 /* Unnecessary masking, but might be good for safety. */
682 alphabet_size
&= 0x3ff;
684 switch (s
->substate_huffman
) {
685 case BROTLI_STATE_HUFFMAN_NONE
:
686 if (!BrotliSafeReadBits(br
, 2, &s
->sub_loop_counter
)) {
687 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
689 BROTLI_LOG_UINT(s
->sub_loop_counter
);
690 /* The value is used as follows:
692 0 for no skipping, 2 skips 2 code lengths, 3 skips 3 code lengths */
693 if (s
->sub_loop_counter
!= 1) {
695 s
->repeat
= 0; /* num_codes */
696 memset(&s
->code_length_histo
[0], 0, sizeof(s
->code_length_histo
[0]) *
697 (BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH
+ 1));
698 memset(&s
->code_length_code_lengths
[0], 0,
699 sizeof(s
->code_length_code_lengths
));
700 s
->substate_huffman
= BROTLI_STATE_HUFFMAN_COMPLEX
;
703 /* No break, transit to the next state. */
705 case BROTLI_STATE_HUFFMAN_SIMPLE_SIZE
:
706 /* Read symbols, codes & code lengths directly. */
707 if (!BrotliSafeReadBits(br
, 2, &s
->symbol
)) { /* num_symbols */
708 s
->substate_huffman
= BROTLI_STATE_HUFFMAN_SIMPLE_SIZE
;
709 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
711 s
->sub_loop_counter
= 0;
712 /* No break, transit to the next state. */
713 case BROTLI_STATE_HUFFMAN_SIMPLE_READ
: {
714 BrotliResult result
= ReadSimpleHuffmanSymbols(alphabet_size
, s
);
715 if (result
!= BROTLI_RESULT_SUCCESS
) {
718 /* No break, transit to the next state. */
720 case BROTLI_STATE_HUFFMAN_SIMPLE_BUILD
: {
722 if (s
->symbol
== 3) {
724 if (!BrotliSafeReadBits(br
, 1, &bits
)) {
725 s
->substate_huffman
= BROTLI_STATE_HUFFMAN_SIMPLE_BUILD
;
726 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
730 BROTLI_LOG_UINT(s
->symbol
);
731 table_size
= BrotliBuildSimpleHuffmanTable(
732 table
, HUFFMAN_TABLE_BITS
, s
->symbols_lists_array
, s
->symbol
);
733 if (opt_table_size
) {
734 *opt_table_size
= table_size
;
736 s
->substate_huffman
= BROTLI_STATE_HUFFMAN_NONE
;
737 return BROTLI_RESULT_SUCCESS
;
740 Complex
: /* Decode Huffman-coded code lengths. */
741 case BROTLI_STATE_HUFFMAN_COMPLEX
: {
743 BrotliResult result
= ReadCodeLengthCodeLengths(s
);
744 if (result
!= BROTLI_RESULT_SUCCESS
) {
747 BrotliBuildCodeLengthsHuffmanTable(s
->table
,
748 s
->code_length_code_lengths
,
749 s
->code_length_histo
);
750 memset(&s
->code_length_histo
[0], 0, sizeof(s
->code_length_histo
));
751 for (i
= 0; i
<= BROTLI_HUFFMAN_MAX_CODE_LENGTH
; ++i
) {
752 s
->next_symbol
[i
] = (int)i
- (BROTLI_HUFFMAN_MAX_CODE_LENGTH
+ 1);
753 s
->symbol_lists
[(int)i
- (BROTLI_HUFFMAN_MAX_CODE_LENGTH
+ 1)] = 0xFFFF;
757 s
->prev_code_len
= kDefaultCodeLength
;
759 s
->repeat_code_len
= 0;
761 s
->substate_huffman
= BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS
;
762 /* No break, transit to the next state. */
764 case BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS
: {
766 BrotliResult result
= ReadSymbolCodeLengths(alphabet_size
, s
);
767 if (result
== BROTLI_RESULT_NEEDS_MORE_INPUT
) {
768 result
= SafeReadSymbolCodeLengths(alphabet_size
, s
);
770 if (result
!= BROTLI_RESULT_SUCCESS
) {
775 BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", s
->space
));
776 return BROTLI_FAILURE();
778 table_size
= BrotliBuildHuffmanTable(table
, HUFFMAN_TABLE_BITS
,
779 s
->symbol_lists
, s
->code_length_histo
);
780 if (opt_table_size
) {
781 *opt_table_size
= table_size
;
783 s
->substate_huffman
= BROTLI_STATE_HUFFMAN_NONE
;
784 return BROTLI_RESULT_SUCCESS
;
788 return BROTLI_FAILURE();
792 /* Decodes a block length by reading 3..39 bits. */
793 static BROTLI_INLINE
uint32_t ReadBlockLength(const HuffmanCode
* table
,
794 BrotliBitReader
* br
) {
797 code
= ReadSymbol(table
, br
);
798 nbits
= kBlockLengthPrefixCode
[code
].nbits
; /* nbits == 2..24 */
799 return kBlockLengthPrefixCode
[code
].offset
+ BrotliReadBits(br
, nbits
);
802 /* WARNING: if state is not BROTLI_STATE_READ_BLOCK_LENGTH_NONE, then
803 reading can't be continued with ReadBlockLength. */
804 static BROTLI_INLINE
int SafeReadBlockLength(BrotliState
* s
,
806 const HuffmanCode
* table
,
807 BrotliBitReader
* br
) {
809 if (s
->substate_read_block_length
== BROTLI_STATE_READ_BLOCK_LENGTH_NONE
) {
810 if (!SafeReadSymbol(table
, br
, &index
)) {
814 index
= s
->block_length_index
;
818 uint32_t nbits
= kBlockLengthPrefixCode
[index
].nbits
; /* nbits == 2..24 */
819 if (!BrotliSafeReadBits(br
, nbits
, &bits
)) {
820 s
->block_length_index
= index
;
821 s
->substate_read_block_length
= BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX
;
824 *result
= kBlockLengthPrefixCode
[index
].offset
+ bits
;
825 s
->substate_read_block_length
= BROTLI_STATE_READ_BLOCK_LENGTH_NONE
;
831 1) initialize list L with values 0, 1,... 255
832 2) For each input element X:
834 2.2) remove X-th element from L
836 2.4) append Y to output
838 In most cases max(Y) <= 7, so most of L remains intact.
839 To reduce the cost of initialization, we reuse L, remember the upper bound
840 of Y values, and reinitialize only first elements in L.
842 Most of input values are 0 and 1. To reduce number of branches, we replace
843 inner for loop with do-while.
845 static BROTLI_NOINLINE
void InverseMoveToFrontTransform(uint8_t* v
,
846 uint32_t v_len
, BrotliState
* state
) {
847 /* Reinitialize elements that could have been changed. */
849 uint32_t upper_bound
= state
->mtf_upper_bound
;
850 uint8_t* mtf
= state
->mtf
;
851 /* Load endian-aware constant. */
852 const uint8_t b0123
[4] = {0, 1, 2, 3};
854 memcpy(&pattern
, &b0123
, 4);
856 /* Initialize list using 4 consequent values pattern. */
857 *(uint32_t*)mtf
= pattern
;
859 pattern
+= 0x04040404; /* Advance all 4 values by 4. */
860 *(uint32_t*)(mtf
+ i
) = pattern
;
862 } while (i
<= upper_bound
);
864 /* Transform the input. */
866 for (i
= 0; i
< v_len
; ++i
) {
868 uint8_t value
= mtf
[index
];
873 mtf
[index
+ 1] = mtf
[index
];
877 /* Remember amount of elements to be reinitialized. */
878 state
->mtf_upper_bound
= upper_bound
;
882 /* Decodes a series of Huffman table using ReadHuffmanCode function. */
883 static BrotliResult
HuffmanTreeGroupDecode(HuffmanTreeGroup
* group
,
885 if (s
->substate_tree_group
!= BROTLI_STATE_TREE_GROUP_LOOP
) {
886 s
->next
= group
->codes
;
888 s
->substate_tree_group
= BROTLI_STATE_TREE_GROUP_LOOP
;
890 while (s
->htree_index
< group
->num_htrees
) {
892 BrotliResult result
=
893 ReadHuffmanCode(group
->alphabet_size
, s
->next
, &table_size
, s
);
894 if (result
!= BROTLI_RESULT_SUCCESS
) return result
;
895 group
->htrees
[s
->htree_index
] = s
->next
;
896 s
->next
+= table_size
;
899 s
->substate_tree_group
= BROTLI_STATE_TREE_GROUP_NONE
;
900 return BROTLI_RESULT_SUCCESS
;
903 /* Decodes a context map.
904 Decoding is done in 4 phases:
905 1) Read auxiliary information (6..16 bits) and allocate memory.
906 In case of trivial context map, decoding is finished at this phase.
907 2) Decode Huffman table using ReadHuffmanCode function.
908 This table will be used for reading context map items.
909 3) Read context map items; "0" values could be run-length encoded.
910 4) Optionally, apply InverseMoveToFront transform to the resulting map.
912 static BrotliResult
DecodeContextMap(uint32_t context_map_size
,
913 uint32_t* num_htrees
,
914 uint8_t** context_map_arg
,
916 BrotliBitReader
* br
= &s
->br
;
917 BrotliResult result
= BROTLI_RESULT_SUCCESS
;
919 switch((int)s
->substate_context_map
) {
920 case BROTLI_STATE_CONTEXT_MAP_NONE
:
921 result
= DecodeVarLenUint8(s
, br
, num_htrees
);
922 if (result
!= BROTLI_RESULT_SUCCESS
) {
926 s
->context_index
= 0;
927 BROTLI_LOG_UINT(context_map_size
);
928 BROTLI_LOG_UINT(*num_htrees
);
929 *context_map_arg
= (uint8_t*)BROTLI_ALLOC(s
, (size_t)context_map_size
);
930 if (*context_map_arg
== 0) {
931 return BROTLI_FAILURE();
933 if (*num_htrees
<= 1) {
934 memset(*context_map_arg
, 0, (size_t)context_map_size
);
935 return BROTLI_RESULT_SUCCESS
;
937 s
->substate_context_map
= BROTLI_STATE_CONTEXT_MAP_READ_PREFIX
;
938 /* No break, continue to next state. */
939 case BROTLI_STATE_CONTEXT_MAP_READ_PREFIX
: {
941 /* In next stage ReadHuffmanCode uses at least 4 bits, so it is safe
942 to peek 4 bits ahead. */
943 if (!BrotliSafeGetBits(br
, 5, &bits
)) {
944 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
946 if ((bits
& 1) != 0) { /* Use RLE for zeroes. */
947 s
->max_run_length_prefix
= (bits
>> 1) + 1;
948 BrotliDropBits(br
, 5);
950 s
->max_run_length_prefix
= 0;
951 BrotliDropBits(br
, 1);
953 BROTLI_LOG_UINT(s
->max_run_length_prefix
);
954 s
->substate_context_map
= BROTLI_STATE_CONTEXT_MAP_HUFFMAN
;
955 /* No break, continue to next state. */
957 case BROTLI_STATE_CONTEXT_MAP_HUFFMAN
:
958 result
= ReadHuffmanCode(*num_htrees
+ s
->max_run_length_prefix
,
959 s
->context_map_table
, NULL
, s
);
960 if (result
!= BROTLI_RESULT_SUCCESS
) return result
;
962 s
->substate_context_map
= BROTLI_STATE_CONTEXT_MAP_DECODE
;
963 /* No break, continue to next state. */
964 case BROTLI_STATE_CONTEXT_MAP_DECODE
: {
965 uint32_t context_index
= s
->context_index
;
966 uint32_t max_run_length_prefix
= s
->max_run_length_prefix
;
967 uint8_t* context_map
= *context_map_arg
;
968 uint32_t code
= s
->code
;
969 if (code
!= 0xFFFF) {
972 while (context_index
< context_map_size
) {
973 if (!SafeReadSymbol(s
->context_map_table
, br
, &code
)) {
975 s
->context_index
= context_index
;
976 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
978 BROTLI_LOG_UINT(code
);
981 context_map
[context_index
++] = 0;
984 if (code
> max_run_length_prefix
) {
985 context_map
[context_index
++] =
986 (uint8_t)(code
- max_run_length_prefix
);
992 if (!BrotliSafeReadBits(br
, code
, &reps
)) {
994 s
->context_index
= context_index
;
995 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
998 BROTLI_LOG_UINT(reps
);
999 if (context_index
+ reps
> context_map_size
) {
1000 return BROTLI_FAILURE();
1003 context_map
[context_index
++] = 0;
1007 /* No break, continue to next state. */
1009 case BROTLI_STATE_CONTEXT_MAP_TRANSFORM
: {
1011 if (!BrotliSafeReadBits(br
, 1, &bits
)) {
1012 s
->substate_context_map
= BROTLI_STATE_CONTEXT_MAP_TRANSFORM
;
1013 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
1016 InverseMoveToFrontTransform(*context_map_arg
, context_map_size
, s
);
1018 s
->substate_context_map
= BROTLI_STATE_CONTEXT_MAP_NONE
;
1019 return BROTLI_RESULT_SUCCESS
;
1023 return BROTLI_FAILURE();
1026 /* Decodes a command or literal and updates block type ringbuffer.
1027 Reads 3..54 bits. */
1028 static BROTLI_INLINE
int DecodeBlockTypeAndLength(int safe
,
1029 BrotliState
* s
, int tree_type
) {
1030 uint32_t max_block_type
= s
->num_block_types
[tree_type
];
1031 int tree_offset
= tree_type
* BROTLI_HUFFMAN_MAX_TABLE_SIZE
;
1032 const HuffmanCode
* type_tree
= &s
->block_type_trees
[tree_offset
];
1033 const HuffmanCode
* len_tree
= &s
->block_len_trees
[tree_offset
];
1034 BrotliBitReader
* br
= &s
->br
;
1035 uint32_t* ringbuffer
= &s
->block_type_rb
[tree_type
* 2];
1036 uint32_t block_type
;
1038 /* Read 0..15 + 3..39 bits */
1040 block_type
= ReadSymbol(type_tree
, br
);
1041 s
->block_length
[tree_type
] = ReadBlockLength(len_tree
, br
);
1043 BrotliBitReaderState memento
;
1044 BrotliBitReaderSaveState(br
, &memento
);
1045 if (!SafeReadSymbol(type_tree
, br
, &block_type
)) return 0;
1046 if (!SafeReadBlockLength(s
, &s
->block_length
[tree_type
], len_tree
, br
)) {
1047 s
->substate_read_block_length
= BROTLI_STATE_READ_BLOCK_LENGTH_NONE
;
1048 BrotliBitReaderRestoreState(br
, &memento
);
1053 if (block_type
== 1) {
1054 block_type
= ringbuffer
[1] + 1;
1055 } else if (block_type
== 0) {
1056 block_type
= ringbuffer
[0];
1060 if (block_type
>= max_block_type
) {
1061 block_type
-= max_block_type
;
1063 ringbuffer
[0] = ringbuffer
[1];
1064 ringbuffer
[1] = block_type
;
1068 /* Decodes the block type and updates the state for literal context.
1069 Reads 3..54 bits. */
1070 static BROTLI_INLINE
int DecodeLiteralBlockSwitchInternal(int safe
,
1072 uint8_t context_mode
;
1073 uint32_t context_offset
;
1074 if (!DecodeBlockTypeAndLength(safe
, s
, 0)) {
1077 context_offset
= s
->block_type_rb
[1] << kLiteralContextBits
;
1078 s
->context_map_slice
= s
->context_map
+ context_offset
;
1079 s
->literal_htree_index
= s
->context_map_slice
[0];
1080 s
->literal_htree
= s
->literal_hgroup
.htrees
[s
->literal_htree_index
];
1081 context_mode
= s
->context_modes
[s
->block_type_rb
[1]];
1082 s
->context_lookup1
= &kContextLookup
[kContextLookupOffsets
[context_mode
]];
1083 s
->context_lookup2
= &kContextLookup
[kContextLookupOffsets
[context_mode
+ 1]];
1087 static void BROTLI_NOINLINE
DecodeLiteralBlockSwitch(BrotliState
* s
) {
1088 DecodeLiteralBlockSwitchInternal(0, s
);
1091 static int BROTLI_NOINLINE
SafeDecodeLiteralBlockSwitch(BrotliState
* s
) {
1092 return DecodeLiteralBlockSwitchInternal(1, s
);
1095 /* Block switch for insert/copy length.
1096 Reads 3..54 bits. */
1097 static BROTLI_INLINE
int DecodeCommandBlockSwitchInternal(int safe
,
1099 if (!DecodeBlockTypeAndLength(safe
, s
, 1)) {
1102 s
->htree_command
= s
->insert_copy_hgroup
.htrees
[s
->block_type_rb
[3]];
1106 static void BROTLI_NOINLINE
DecodeCommandBlockSwitch(BrotliState
* s
) {
1107 DecodeCommandBlockSwitchInternal(0, s
);
1109 static int BROTLI_NOINLINE
SafeDecodeCommandBlockSwitch(BrotliState
* s
) {
1110 return DecodeCommandBlockSwitchInternal(1, s
);
1113 /* Block switch for distance codes.
1114 Reads 3..54 bits. */
1115 static BROTLI_INLINE
int DecodeDistanceBlockSwitchInternal(int safe
,
1117 if (!DecodeBlockTypeAndLength(safe
, s
, 2)) {
1120 s
->dist_context_map_slice
=
1121 s
->dist_context_map
+ (s
->block_type_rb
[5] << kDistanceContextBits
);
1122 s
->dist_htree_index
= s
->dist_context_map_slice
[s
->distance_context
];
1126 static void BROTLI_NOINLINE
DecodeDistanceBlockSwitch(BrotliState
* s
) {
1127 DecodeDistanceBlockSwitchInternal(0, s
);
1130 static int BROTLI_NOINLINE
SafeDecodeDistanceBlockSwitch(BrotliState
* s
) {
1131 return DecodeDistanceBlockSwitchInternal(1, s
);
1134 static BrotliResult
WriteRingBuffer(size_t* available_out
, uint8_t** next_out
,
1135 size_t* total_out
, BrotliState
* s
) {
1136 size_t pos
= (s
->pos
> s
->ringbuffer_size
) ?
1137 (size_t)s
->ringbuffer_size
: (size_t)(s
->pos
);
1138 uint8_t* start
= s
->ringbuffer
1139 + (s
->partial_pos_out
& (size_t)s
->ringbuffer_mask
);
1140 size_t partial_pos_rb
=
1141 (s
->rb_roundtrips
* (size_t)s
->ringbuffer_size
) + pos
;
1142 size_t to_write
= (partial_pos_rb
- s
->partial_pos_out
);
1143 size_t num_written
= *available_out
;
1144 if (num_written
> to_write
) {
1145 num_written
= to_write
;
1147 if (s
->meta_block_remaining_len
< 0) {
1148 return BROTLI_FAILURE();
1150 memcpy(*next_out
, start
, num_written
);
1151 *next_out
+= num_written
;
1152 *available_out
-= num_written
;
1153 BROTLI_LOG_UINT(to_write
);
1154 BROTLI_LOG_UINT(num_written
);
1155 s
->partial_pos_out
+= (size_t)num_written
;
1156 *total_out
= s
->partial_pos_out
;
1157 if (num_written
< to_write
) {
1158 return BROTLI_RESULT_NEEDS_MORE_OUTPUT
;
1160 return BROTLI_RESULT_SUCCESS
;
1163 static BrotliResult BROTLI_NOINLINE
CopyUncompressedBlockToOutput(
1164 size_t* available_out
, uint8_t** next_out
, size_t* total_out
,
1168 switch (s
->substate_uncompressed
) {
1169 case BROTLI_STATE_UNCOMPRESSED_NONE
: {
1170 int nbytes
= (int)BrotliGetRemainingBytes(&s
->br
);
1171 if (nbytes
> s
->meta_block_remaining_len
) {
1172 nbytes
= s
->meta_block_remaining_len
;
1174 if (s
->pos
+ nbytes
> s
->ringbuffer_size
) {
1175 nbytes
= s
->ringbuffer_size
- s
->pos
;
1177 /* Copy remaining bytes from s->br.buf_ to ringbuffer. */
1178 BrotliCopyBytes(&s
->ringbuffer
[s
->pos
], &s
->br
, (size_t)nbytes
);
1180 s
->meta_block_remaining_len
-= nbytes
;
1181 if (s
->pos
< s
->ringbuffer_size
) {
1182 if (s
->meta_block_remaining_len
== 0) {
1183 return BROTLI_RESULT_SUCCESS
;
1185 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
1187 s
->substate_uncompressed
= BROTLI_STATE_UNCOMPRESSED_WRITE
;
1188 /*s->partial_pos_rb += (size_t)s->ringbuffer_size;*/
1189 /* No break, continue to next state */
1191 case BROTLI_STATE_UNCOMPRESSED_WRITE
: {
1192 BrotliResult result
= WriteRingBuffer(
1193 available_out
, next_out
, total_out
, s
);
1194 if (result
!= BROTLI_RESULT_SUCCESS
) {
1199 s
->max_distance
= s
->max_backward_distance
;
1200 s
->substate_uncompressed
= BROTLI_STATE_UNCOMPRESSED_NONE
;
1205 return BROTLI_FAILURE();
1208 int BrotliDecompressedSize(size_t encoded_size
,
1209 const uint8_t* encoded_buffer
,
1210 size_t* decoded_size
) {
1212 int next_block_header
;
1213 BrotliStateInit(&s
);
1214 s
.br
.next_in
= encoded_buffer
;
1215 s
.br
.avail_in
= encoded_size
;
1216 if (!BrotliWarmupBitReader(&s
.br
)) {
1219 DecodeWindowBits(&s
.br
);
1220 if (DecodeMetaBlockLength(&s
, &s
.br
) != BROTLI_RESULT_SUCCESS
) {
1223 *decoded_size
= (size_t)s
.meta_block_remaining_len
;
1224 if (s
.is_last_metablock
) {
1227 if (!s
.is_uncompressed
|| !BrotliJumpToByteBoundary(&s
.br
)) {
1230 next_block_header
= BrotliPeekByte(&s
.br
, (size_t)s
.meta_block_remaining_len
);
1231 return (next_block_header
!= -1) && ((next_block_header
& 3) == 3);
1234 /* Allocates the smallest feasible ring buffer.
1236 If we know the data size is small, do not allocate more ringbuffer
1237 size than needed to reduce memory usage.
1239 This method is called before the first non-empty non-metadata block is
1240 processed. When this method is called, metablock size and flags MUST be
1243 static int BROTLI_NOINLINE
BrotliAllocateRingBuffer(BrotliState
* s
,
1244 BrotliBitReader
* br
) {
1245 /* We need the slack region for the following reasons:
1246 - doing up to two 16-byte copies for fast backward copying
1247 - inserting transformed dictionary word (5 prefix + 24 base + 8 suffix) */
1248 static const int kRingBufferWriteAheadSlack
= 42;
1249 int is_last
= s
->is_last_metablock
;
1250 s
->ringbuffer_size
= 1 << s
->window_bits
;
1252 if (s
->is_uncompressed
) {
1253 int next_block_header
= BrotliPeekByte(br
,
1254 (size_t)s
->meta_block_remaining_len
);
1255 if (next_block_header
!= -1) { /* Peek succeeded */
1256 if ((next_block_header
& 3) == 3) { /* ISLAST and ISEMPTY */
1262 /* We need at least 2 bytes of ring buffer size to get the last two
1263 bytes for context from there */
1265 while (s
->ringbuffer_size
>= s
->meta_block_remaining_len
* 2
1266 && s
->ringbuffer_size
> 32) {
1267 s
->ringbuffer_size
>>= 1;
1271 /* But make it fit the custom dictionary if there is one. */
1272 while (s
->ringbuffer_size
< s
->custom_dict_size
) {
1273 s
->ringbuffer_size
<<= 1;
1276 s
->ringbuffer_mask
= s
->ringbuffer_size
- 1;
1277 s
->ringbuffer
= (uint8_t*)BROTLI_ALLOC(s
, (size_t)(s
->ringbuffer_size
+
1278 kRingBufferWriteAheadSlack
+ kBrotliMaxDictionaryWordLength
));
1279 if (s
->ringbuffer
== 0) {
1282 s
->ringbuffer_end
= s
->ringbuffer
+ s
->ringbuffer_size
;
1283 s
->ringbuffer
[s
->ringbuffer_size
- 2] = 0;
1284 s
->ringbuffer
[s
->ringbuffer_size
- 1] = 0;
1285 if (s
->custom_dict
) {
1286 memcpy(&s
->ringbuffer
[(-s
->custom_dict_size
) & s
->ringbuffer_mask
],
1287 s
->custom_dict
, (size_t)s
->custom_dict_size
);
1293 /* Reads 1..256 2-bit context modes. */
1294 static BrotliResult
ReadContextModes(BrotliState
* s
) {
1295 BrotliBitReader
* br
= &s
->br
;
1296 int i
= s
->loop_counter
;
1298 while (i
< (int)s
->num_block_types
[0]) {
1300 if (!BrotliSafeReadBits(br
, 2, &bits
)) {
1301 s
->loop_counter
= i
;
1302 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
1304 s
->context_modes
[i
] = (uint8_t)(bits
<< 1);
1305 BROTLI_LOG_ARRAY_INDEX(s
->context_modes
, i
);
1308 return BROTLI_RESULT_SUCCESS
;
1311 static BROTLI_INLINE
void TakeDistanceFromRingBuffer(BrotliState
* s
) {
1312 if (s
->distance_code
== 0) {
1314 s
->distance_code
= s
->dist_rb
[s
->dist_rb_idx
& 3];
1316 int distance_code
= s
->distance_code
<< 1;
1317 /* kDistanceShortCodeIndexOffset has 2-bit values from LSB: */
1318 /* 3, 2, 1, 0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2 */
1319 const uint32_t kDistanceShortCodeIndexOffset
= 0xaaafff1b;
1320 /* kDistanceShortCodeValueOffset has 2-bit values from LSB: */
1321 /*-0, 0,-0, 0,-1, 1,-2, 2,-3, 3,-1, 1,-2, 2,-3, 3 */
1322 const uint32_t kDistanceShortCodeValueOffset
= 0xfa5fa500;
1323 int v
= (s
->dist_rb_idx
+
1324 (int)(kDistanceShortCodeIndexOffset
>> distance_code
)) & 0x3;
1325 s
->distance_code
= s
->dist_rb
[v
];
1326 v
= (int)(kDistanceShortCodeValueOffset
>> distance_code
) & 0x3;
1327 if ((distance_code
& 0x3) != 0) {
1328 s
->distance_code
+= v
;
1330 s
->distance_code
-= v
;
1331 if (s
->distance_code
<= 0) {
1332 /* A huge distance will cause a BROTLI_FAILURE() soon. */
1333 /* This is a little faster than failing here. */
1334 s
->distance_code
= 0x0fffffff;
1340 static BROTLI_INLINE
int SafeReadBits(
1341 BrotliBitReader
* const br
, uint32_t n_bits
, uint32_t* val
) {
1343 return BrotliSafeReadBits(br
, n_bits
, val
);
1350 /* Precondition: s->distance_code < 0 */
1351 static BROTLI_INLINE
int ReadDistanceInternal(int safe
,
1352 BrotliState
* s
, BrotliBitReader
* br
) {
1354 BrotliBitReaderState memento
;
1355 HuffmanCode
* distance_tree
= s
->distance_hgroup
.htrees
[s
->dist_htree_index
];
1357 s
->distance_code
= (int)ReadSymbol(distance_tree
, br
);
1360 BrotliBitReaderSaveState(br
, &memento
);
1361 if (!SafeReadSymbol(distance_tree
, br
, &code
)) {
1364 s
->distance_code
= (int)code
;
1366 /* Convert the distance code to the actual distance by possibly */
1367 /* looking up past distances from the s->ringbuffer. */
1368 if ((s
->distance_code
& ~0xf) == 0) {
1369 TakeDistanceFromRingBuffer(s
);
1370 --s
->block_length
[2];
1373 distval
= s
->distance_code
- (int)s
->num_direct_distance_codes
;
1378 if (!safe
&& (s
->distance_postfix_bits
== 0)) {
1379 nbits
= ((uint32_t)distval
>> 1) + 1;
1380 offset
= ((2 + (distval
& 1)) << nbits
) - 4;
1381 s
->distance_code
= (int)s
->num_direct_distance_codes
+
1382 offset
+ (int)BrotliReadBits(br
, nbits
);
1384 /* This branch also works well when s->distance_postfix_bits == 0 */
1386 postfix
= distval
& s
->distance_postfix_mask
;
1387 distval
>>= s
->distance_postfix_bits
;
1388 nbits
= ((uint32_t)distval
>> 1) + 1;
1390 if (!SafeReadBits(br
, nbits
, &bits
)) {
1391 s
->distance_code
= -1; /* Restore precondition. */
1392 BrotliBitReaderRestoreState(br
, &memento
);
1396 bits
= BrotliReadBits(br
, nbits
);
1398 offset
= ((2 + (distval
& 1)) << nbits
) - 4;
1399 s
->distance_code
= (int)s
->num_direct_distance_codes
+
1400 ((offset
+ (int)bits
) << s
->distance_postfix_bits
) + postfix
;
1403 s
->distance_code
= s
->distance_code
- NUM_DISTANCE_SHORT_CODES
+ 1;
1404 --s
->block_length
[2];
1408 static BROTLI_INLINE
void ReadDistance(BrotliState
* s
, BrotliBitReader
* br
) {
1409 ReadDistanceInternal(0, s
, br
);
1412 static BROTLI_INLINE
int SafeReadDistance(BrotliState
* s
, BrotliBitReader
* br
) {
1413 return ReadDistanceInternal(1, s
, br
);
1416 static BROTLI_INLINE
int ReadCommandInternal(int safe
,
1417 BrotliState
* s
, BrotliBitReader
* br
, int* insert_length
) {
1419 uint32_t insert_len_extra
= 0;
1420 uint32_t copy_length
;
1422 BrotliBitReaderState memento
;
1424 cmd_code
= ReadSymbol(s
->htree_command
, br
);
1426 BrotliBitReaderSaveState(br
, &memento
);
1427 if (!SafeReadSymbol(s
->htree_command
, br
, &cmd_code
)) {
1431 v
= kCmdLut
[cmd_code
];
1432 s
->distance_code
= v
.distance_code
;
1433 s
->distance_context
= v
.context
;
1434 s
->dist_htree_index
= s
->dist_context_map_slice
[s
->distance_context
];
1435 *insert_length
= v
.insert_len_offset
;
1437 if (PREDICT_FALSE(v
.insert_len_extra_bits
!= 0)) {
1438 insert_len_extra
= BrotliReadBits(br
, v
.insert_len_extra_bits
);
1440 copy_length
= BrotliReadBits(br
, v
.copy_len_extra_bits
);
1442 if (!SafeReadBits(br
, v
.insert_len_extra_bits
, &insert_len_extra
) ||
1443 !SafeReadBits(br
, v
.copy_len_extra_bits
, ©_length
)) {
1444 BrotliBitReaderRestoreState(br
, &memento
);
1448 s
->copy_length
= (int)copy_length
+ v
.copy_len_offset
;
1449 --s
->block_length
[1];
1450 *insert_length
+= (int)insert_len_extra
;
1454 static BROTLI_INLINE
void ReadCommand(BrotliState
* s
, BrotliBitReader
* br
,
1455 int* insert_length
) {
1456 ReadCommandInternal(0, s
, br
, insert_length
);
1459 static BROTLI_INLINE
int SafeReadCommand(BrotliState
* s
, BrotliBitReader
* br
,
1460 int* insert_length
) {
1461 return ReadCommandInternal(1, s
, br
, insert_length
);
1464 static BROTLI_INLINE
int WarmupBitReader(int safe
, BrotliBitReader
* const br
) {
1468 return BrotliWarmupBitReader(br
);
1471 static BROTLI_INLINE
int CheckInputAmount(int safe
,
1472 BrotliBitReader
* const br
, size_t num
) {
1476 return BrotliCheckInputAmount(br
, num
);
1479 #define BROTLI_SAFE(METHOD) { \
1481 if (! Safe ## METHOD ) { \
1482 result = BROTLI_RESULT_NEEDS_MORE_INPUT; \
1483 goto saveStateAndReturn; \
1490 static BROTLI_INLINE BrotliResult
ProcessCommandsInternal(int safe
,
1493 int i
= s
->loop_counter
;
1494 BrotliResult result
= BROTLI_RESULT_SUCCESS
;
1495 BrotliBitReader
* br
= &s
->br
;
1497 if (!CheckInputAmount(safe
, br
, 28) || !WarmupBitReader(safe
, br
)) {
1498 result
= BROTLI_RESULT_NEEDS_MORE_INPUT
;
1499 goto saveStateAndReturn
;
1502 /* Jump into state machine. */
1503 if (s
->state
== BROTLI_STATE_COMMAND_BEGIN
) {
1505 } else if (s
->state
== BROTLI_STATE_COMMAND_INNER
) {
1507 } else if (s
->state
== BROTLI_STATE_COMMAND_POST_DECODE_LITERALS
) {
1508 goto CommandPostDecodeLiterals
;
1509 } else if (s
->state
== BROTLI_STATE_COMMAND_POST_WRAP_COPY
) {
1510 goto CommandPostWrapCopy
;
1512 return BROTLI_FAILURE();
1517 s
->state
= BROTLI_STATE_COMMAND_BEGIN
;
1519 if (!CheckInputAmount(safe
, br
, 28)) { /* 156 bits + 7 bytes */
1520 s
->state
= BROTLI_STATE_COMMAND_BEGIN
;
1521 result
= BROTLI_RESULT_NEEDS_MORE_INPUT
;
1522 goto saveStateAndReturn
;
1524 if (PREDICT_FALSE(s
->block_length
[1] == 0)) {
1525 BROTLI_SAFE(DecodeCommandBlockSwitch(s
));
1528 /* Read the insert/copy length in the command */
1529 BROTLI_SAFE(ReadCommand(s
, br
, &i
));
1531 BROTLI_LOG_UINT(s
->copy_length
);
1532 BROTLI_LOG_UINT(s
->distance_code
);
1534 goto CommandPostDecodeLiterals
;
1536 s
->meta_block_remaining_len
-= i
;
1540 s
->state
= BROTLI_STATE_COMMAND_INNER
;
1542 /* Read the literals in the command */
1543 if (s
->trivial_literal_context
) {
1546 PreloadSymbol(safe
, s
->literal_htree
, br
, &bits
, &value
);
1548 if (!CheckInputAmount(safe
, br
, 28)) { /* 162 bits + 7 bytes */
1549 s
->state
= BROTLI_STATE_COMMAND_INNER
;
1550 result
= BROTLI_RESULT_NEEDS_MORE_INPUT
;
1551 goto saveStateAndReturn
;
1553 if (PREDICT_FALSE(s
->block_length
[0] == 0)) {
1554 BROTLI_SAFE(DecodeLiteralBlockSwitch(s
));
1555 PreloadSymbol(safe
, s
->literal_htree
, br
, &bits
, &value
);
1558 s
->ringbuffer
[pos
] = (uint8_t)ReadPreloadedSymbol(
1559 s
->literal_htree
, br
, &bits
, &value
);
1562 if (!SafeReadSymbol(s
->literal_htree
, br
, &literal
)) {
1563 result
= BROTLI_RESULT_NEEDS_MORE_INPUT
;
1564 goto saveStateAndReturn
;
1566 s
->ringbuffer
[pos
] = (uint8_t)literal
;
1568 --s
->block_length
[0];
1569 BROTLI_LOG_UINT(s
->literal_htree_index
);
1570 BROTLI_LOG_ARRAY_INDEX(s
->ringbuffer
, pos
);
1572 if (PREDICT_FALSE(pos
== s
->ringbuffer_size
)) {
1573 s
->state
= BROTLI_STATE_COMMAND_INNER_WRITE
;
1575 goto saveStateAndReturn
;
1579 uint8_t p1
= s
->ringbuffer
[(pos
- 1) & s
->ringbuffer_mask
];
1580 uint8_t p2
= s
->ringbuffer
[(pos
- 2) & s
->ringbuffer_mask
];
1582 const HuffmanCode
* hc
;
1584 if (!CheckInputAmount(safe
, br
, 28)) { /* 162 bits + 7 bytes */
1585 s
->state
= BROTLI_STATE_COMMAND_INNER
;
1586 result
= BROTLI_RESULT_NEEDS_MORE_INPUT
;
1587 goto saveStateAndReturn
;
1589 if (PREDICT_FALSE(s
->block_length
[0] == 0)) {
1590 BROTLI_SAFE(DecodeLiteralBlockSwitch(s
));
1592 context
= s
->context_lookup1
[p1
] | s
->context_lookup2
[p2
];
1593 BROTLI_LOG_UINT(context
);
1594 hc
= s
->literal_hgroup
.htrees
[s
->context_map_slice
[context
]];
1597 p1
= (uint8_t)ReadSymbol(hc
, br
);
1600 if (!SafeReadSymbol(hc
, br
, &literal
)) {
1601 result
= BROTLI_RESULT_NEEDS_MORE_INPUT
;
1602 goto saveStateAndReturn
;
1604 p1
= (uint8_t)literal
;
1606 s
->ringbuffer
[pos
] = p1
;
1607 --s
->block_length
[0];
1608 BROTLI_LOG_UINT(s
->context_map_slice
[context
]);
1609 BROTLI_LOG_ARRAY_INDEX(s
->ringbuffer
, pos
& s
->ringbuffer_mask
);
1611 if (PREDICT_FALSE(pos
== s
->ringbuffer_size
)) {
1612 s
->state
= BROTLI_STATE_COMMAND_INNER_WRITE
;
1614 goto saveStateAndReturn
;
1618 if (s
->meta_block_remaining_len
<= 0) {
1619 s
->state
= BROTLI_STATE_METABLOCK_DONE
;
1620 goto saveStateAndReturn
;
1623 CommandPostDecodeLiterals
:
1625 s
->state
= BROTLI_STATE_COMMAND_POST_DECODE_LITERALS
;
1627 if (s
->distance_code
>= 0) {
1629 s
->distance_code
= s
->dist_rb
[s
->dist_rb_idx
& 3];
1630 goto postReadDistance
; /* We already have the implicit distance */
1632 /* Read distance code in the command, unless it was implicitly zero. */
1633 if (PREDICT_FALSE(s
->block_length
[2] == 0)) {
1634 BROTLI_SAFE(DecodeDistanceBlockSwitch(s
));
1636 BROTLI_SAFE(ReadDistance(s
, br
));
1638 BROTLI_LOG_UINT(s
->distance_code
);
1639 if (s
->max_distance
!= s
->max_backward_distance
) {
1640 if (pos
< s
->max_backward_distance_minus_custom_dict_size
) {
1641 s
->max_distance
= pos
+ s
->custom_dict_size
;
1643 s
->max_distance
= s
->max_backward_distance
;
1647 /* Apply copy of LZ77 back-reference, or static dictionary reference if
1648 the distance is larger than the max LZ77 distance */
1649 if (s
->distance_code
> s
->max_distance
) {
1650 if (i
>= kBrotliMinDictionaryWordLength
&&
1651 i
<= kBrotliMaxDictionaryWordLength
) {
1652 int offset
= kBrotliDictionaryOffsetsByLength
[i
];
1653 int word_id
= s
->distance_code
- s
->max_distance
- 1;
1654 uint32_t shift
= kBrotliDictionarySizeBitsByLength
[i
];
1655 int mask
= (int)BitMask(shift
);
1656 int word_idx
= word_id
& mask
;
1657 int transform_idx
= word_id
>> shift
;
1658 offset
+= word_idx
* i
;
1659 if (transform_idx
< kNumTransforms
) {
1660 const uint8_t* word
= &kBrotliDictionary
[offset
];
1662 if (transform_idx
== 0) {
1663 memcpy(&s
->ringbuffer
[pos
], word
, (size_t)len
);
1665 len
= TransformDictionaryWord(
1666 &s
->ringbuffer
[pos
], word
, len
, transform_idx
);
1669 s
->meta_block_remaining_len
-= len
;
1670 if (pos
>= s
->ringbuffer_size
) {
1671 /*s->partial_pos_rb += (size_t)s->ringbuffer_size;*/
1672 s
->state
= BROTLI_STATE_COMMAND_POST_WRITE_1
;
1673 goto saveStateAndReturn
;
1676 BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
1677 "len: %d bytes left: %d\n",
1678 pos
, s
->distance_code
, i
,
1679 s
->meta_block_remaining_len
));
1680 return BROTLI_FAILURE();
1683 BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
1684 "len: %d bytes left: %d\n", pos
, s
->distance_code
, i
,
1685 s
->meta_block_remaining_len
));
1686 return BROTLI_FAILURE();
1689 const uint8_t *ringbuffer_end_minus_copy_length
=
1690 s
->ringbuffer_end
- i
;
1691 uint8_t* copy_src
= &s
->ringbuffer
[
1692 (pos
- s
->distance_code
) & s
->ringbuffer_mask
];
1693 uint8_t* copy_dst
= &s
->ringbuffer
[pos
];
1694 /* update the recent distances cache */
1695 s
->dist_rb
[s
->dist_rb_idx
& 3] = s
->distance_code
;
1697 s
->meta_block_remaining_len
-= i
;
1698 if (PREDICT_FALSE(s
->meta_block_remaining_len
< 0)) {
1699 BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
1700 "len: %d bytes left: %d\n", pos
, s
->distance_code
, i
,
1701 s
->meta_block_remaining_len
));
1702 return BROTLI_FAILURE();
1704 /* There is 128+ bytes of slack in the ringbuffer allocation.
1705 Also, we have 16 short codes, that make these 16 bytes irrelevant
1706 in the ringbuffer. Let's copy over them as a first guess.
1708 memmove16(copy_dst
, copy_src
);
1709 /* Now check if the copy extends over the ringbuffer end,
1710 or if the copy overlaps with itself, if yes, do wrap-copy. */
1711 if (copy_src
< copy_dst
) {
1712 if (copy_dst
>= ringbuffer_end_minus_copy_length
) {
1713 goto CommandPostWrapCopy
;
1715 if (copy_src
+ i
> copy_dst
) {
1716 goto postSelfintersecting
;
1719 if (copy_src
>= ringbuffer_end_minus_copy_length
) {
1720 goto CommandPostWrapCopy
;
1722 if (copy_dst
+ i
> copy_src
) {
1723 goto postSelfintersecting
;
1729 memcpy(copy_dst
+ 16, copy_src
+ 16, (size_t)(i
- 16));
1731 /* This branch covers about 45% cases.
1732 Fixed size short copy allows more compiler optimizations. */
1733 memmove16(copy_dst
+ 16, copy_src
+ 16);
1737 if (s
->meta_block_remaining_len
<= 0) {
1738 /* Next metablock, if any */
1739 s
->state
= BROTLI_STATE_METABLOCK_DONE
;
1740 goto saveStateAndReturn
;
1744 postSelfintersecting
:
1746 s
->ringbuffer
[pos
] =
1747 s
->ringbuffer
[(pos
- s
->distance_code
) & s
->ringbuffer_mask
];
1750 if (s
->meta_block_remaining_len
<= 0) {
1751 /* Next metablock, if any */
1752 s
->state
= BROTLI_STATE_METABLOCK_DONE
;
1753 goto saveStateAndReturn
;
1758 CommandPostWrapCopy
:
1759 s
->state
= BROTLI_STATE_COMMAND_POST_WRAP_COPY
;
1761 s
->ringbuffer
[pos
] =
1762 s
->ringbuffer
[(pos
- s
->distance_code
) & s
->ringbuffer_mask
];
1764 if (pos
== s
->ringbuffer_size
) {
1765 /*s->partial_pos_rb += (size_t)s->ringbuffer_size;*/
1766 s
->state
= BROTLI_STATE_COMMAND_POST_WRITE_2
;
1767 goto saveStateAndReturn
;
1770 if (s
->meta_block_remaining_len
<= 0) {
1771 /* Next metablock, if any */
1772 s
->state
= BROTLI_STATE_METABLOCK_DONE
;
1773 goto saveStateAndReturn
;
1780 s
->loop_counter
= i
;
1786 static BROTLI_NOINLINE BrotliResult
ProcessCommands(BrotliState
* s
) {
1787 return ProcessCommandsInternal(0, s
);
1790 static BROTLI_NOINLINE BrotliResult
SafeProcessCommands(BrotliState
* s
) {
1791 return ProcessCommandsInternal(1, s
);
1794 BrotliResult
BrotliDecompressBuffer(size_t encoded_size
,
1795 const uint8_t* encoded_buffer
,
1796 size_t* decoded_size
,
1797 uint8_t* decoded_buffer
) {
1799 BrotliResult result
;
1800 size_t total_out
= 0;
1801 size_t available_in
= encoded_size
;
1802 const uint8_t* next_in
= encoded_buffer
;
1803 size_t available_out
= *decoded_size
;
1804 uint8_t* next_out
= decoded_buffer
;
1805 BrotliStateInit(&s
);
1806 result
= BrotliDecompressStream(&available_in
, &next_in
, &available_out
,
1807 &next_out
, &total_out
, &s
);
1808 *decoded_size
= total_out
;
1809 BrotliStateCleanup(&s
);
1810 if (result
!= BROTLI_RESULT_SUCCESS
) {
1811 result
= BROTLI_RESULT_ERROR
;
1816 BrotliResult
BrotliDecompress(BrotliInput input
, BrotliOutput output
) {
1818 BrotliResult result
;
1819 BrotliStateInit(&s
);
1820 result
= BrotliDecompressStreaming(input
, output
, 1, &s
);
1821 if (result
== BROTLI_RESULT_NEEDS_MORE_INPUT
) {
1822 /* Not ok: it didn't finish even though this is a non-streaming function. */
1823 result
= BROTLI_FAILURE();
1825 BrotliStateCleanup(&s
);
1829 BrotliResult
BrotliDecompressBufferStreaming(size_t* available_in
,
1830 const uint8_t** next_in
,
1832 size_t* available_out
,
1836 BrotliResult result
= BrotliDecompressStream(available_in
, next_in
,
1837 available_out
, next_out
, total_out
, s
);
1838 if (finish
&& result
== BROTLI_RESULT_NEEDS_MORE_INPUT
) {
1839 result
= BROTLI_FAILURE();
1844 BrotliResult
BrotliDecompressStreaming(BrotliInput input
, BrotliOutput output
,
1845 int finish
, BrotliState
* s
) {
1846 const size_t kBufferSize
= 65536;
1847 BrotliResult result
;
1848 uint8_t* input_buffer
;
1849 uint8_t* output_buffer
;
1851 const uint8_t* next_in
;
1854 if (s
->legacy_input_buffer
== 0) {
1855 s
->legacy_input_buffer
= (uint8_t*)BROTLI_ALLOC(s
, kBufferSize
);
1857 if (s
->legacy_output_buffer
== 0) {
1858 s
->legacy_output_buffer
= (uint8_t*)BROTLI_ALLOC(s
, kBufferSize
);
1860 if (s
->legacy_input_buffer
== 0 || s
->legacy_output_buffer
== 0) {
1861 return BROTLI_FAILURE();
1863 input_buffer
= s
->legacy_input_buffer
;
1864 output_buffer
= s
->legacy_output_buffer
;
1866 /* Push remaining output. */
1867 if (s
->legacy_output_len
> s
->legacy_output_pos
) {
1868 size_t to_write
= s
->legacy_output_len
- s
->legacy_output_pos
;
1869 int num_written
= BrotliWrite(
1870 output
, output_buffer
+ s
->legacy_output_pos
, to_write
);
1871 if (num_written
< 0) {
1872 return BROTLI_FAILURE();
1874 s
->legacy_output_pos
+= (size_t)num_written
;
1875 if ((size_t)num_written
< to_write
) {
1876 return BROTLI_RESULT_NEEDS_MORE_OUTPUT
;
1879 s
->legacy_output_pos
= 0;
1881 avail_in
= s
->legacy_input_len
- s
->legacy_input_pos
;
1882 next_in
= input_buffer
+ s
->legacy_input_pos
;
1887 size_t avail_out
= kBufferSize
;
1888 uint8_t* next_out
= output_buffer
;
1889 result
= BrotliDecompressStream(&avail_in
, &next_in
,
1890 &avail_out
, &next_out
, &total_out
, s
);
1891 s
->legacy_input_pos
= (size_t)(next_out
- input_buffer
);
1892 to_write
= (size_t)(next_out
- output_buffer
);
1893 num_written
= BrotliWrite(output
, output_buffer
, to_write
);
1894 if (num_written
< 0) {
1895 return BROTLI_FAILURE();
1897 if ((size_t)num_written
< to_write
) {
1898 s
->legacy_output_len
= to_write
;
1899 s
->legacy_output_pos
= (size_t)num_written
;
1900 return BROTLI_RESULT_NEEDS_MORE_OUTPUT
;
1902 if (result
== BROTLI_RESULT_NEEDS_MORE_INPUT
) {
1903 int num_read
= BrotliRead(input
, input_buffer
, kBufferSize
);
1904 if (num_read
< 0 || (num_read
== 0 && finish
)) {
1905 return BROTLI_FAILURE();
1907 if (num_read
== 0) {
1908 s
->legacy_input_len
= 0;
1909 s
->legacy_input_pos
= 0;
1910 return BROTLI_RESULT_NEEDS_MORE_INPUT
;
1912 avail_in
= (size_t)num_read
;
1913 next_in
= input_buffer
;
1914 s
->legacy_input_len
= avail_in
;
1915 s
->legacy_input_pos
= 0;
1916 } else if (result
!= BROTLI_RESULT_NEEDS_MORE_OUTPUT
) {
1917 /* Success or failure. */
1923 /* Invariant: input stream is never overconsumed:
1924 * invalid input implies that the whole stream is invalid -> any amount of
1925 input could be read and discarded
1926 * when result is "needs more input", then at leat one more byte is REQUIRED
1927 to complete decoding; all input data MUST be consumed by decoder, so
1928 client could swap the input buffer
1929 * when result is "needs more output" decoder MUST ensure that it doesn't
1930 hold more than 7 bits in bit reader; this saves client from swapping input
1931 buffer ahead of time
1932 * when result is "success" decoder MUST return all unused data back to input
1933 buffer; this is possible because the invariant is hold on enter
1935 BrotliResult
BrotliDecompressStream(size_t* available_in
,
1936 const uint8_t** next_in
, size_t* available_out
, uint8_t** next_out
,
1937 size_t* total_out
, BrotliState
* s
) {
1938 BrotliResult result
= BROTLI_RESULT_SUCCESS
;
1939 BrotliBitReader
* br
= &s
->br
;
1940 if (s
->buffer_length
== 0) { /* Just connect bit reader to input stream. */
1941 br
->avail_in
= *available_in
;
1942 br
->next_in
= *next_in
;
1944 /* At least one byte of input is required. More than one byte of input may
1945 be required to complete the transaction -> reading more data must be
1946 done in a loop -> do it in a main loop. */
1947 result
= BROTLI_RESULT_NEEDS_MORE_INPUT
;
1948 br
->next_in
= &s
->buffer
.u8
[0];
1952 if (result
!= BROTLI_RESULT_SUCCESS
) { /* Error | needs more input/output */
1953 if (result
== BROTLI_RESULT_NEEDS_MORE_INPUT
) {
1954 if (s
->ringbuffer
!= 0) { /* Proactively push output. */
1955 WriteRingBuffer(available_out
, next_out
, total_out
, s
);
1957 if (s
->buffer_length
!= 0) { /* Used with internal buffer. */
1958 if (br
->avail_in
== 0) { /* Successfully finished read transaction. */
1959 /* Accamulator contains less than 8 bits, because internal buffer
1960 is expanded byte-by-byte until it is enough to complete read. */
1961 s
->buffer_length
= 0;
1962 /* Switch to input stream and restart. */
1963 result
= BROTLI_RESULT_SUCCESS
;
1964 br
->avail_in
= *available_in
;
1965 br
->next_in
= *next_in
;
1967 } else if (*available_in
!= 0) {
1968 /* Not enough data in buffer, but can take one more byte from
1970 result
= BROTLI_RESULT_SUCCESS
;
1971 s
->buffer
.u8
[s
->buffer_length
] = **next_in
;
1973 br
->avail_in
= s
->buffer_length
;
1976 /* Retry with more data in buffer. */
1979 /* Can't finish reading and no more input.*/
1981 } else { /* Input stream doesn't contain enough input. */
1982 /* Copy tail to internal buffer and return. */
1983 *next_in
= br
->next_in
;
1984 *available_in
= br
->avail_in
;
1985 while (*available_in
) {
1986 s
->buffer
.u8
[s
->buffer_length
] = **next_in
;
1996 /* Fail or needs more output. */
1998 if (s
->buffer_length
!= 0) {
1999 /* Just consumed the buffered input and produced some output. Otherwise
2000 it would result in "needs more input". Reset internal buffer.*/
2001 s
->buffer_length
= 0;
2003 /* Using input stream in last iteration. When decoder switches to input
2004 stream it has less than 8 bits in accamulator, so it is safe to
2005 return unused accamulator bits there. */
2006 BrotliBitReaderUnload(br
);
2007 *available_in
= br
->avail_in
;
2008 *next_in
= br
->next_in
;
2013 case BROTLI_STATE_UNINITED
:
2014 /* Prepare to the first read. */
2015 if (!BrotliWarmupBitReader(br
)) {
2016 result
= BROTLI_RESULT_NEEDS_MORE_INPUT
;
2019 /* Decode window size. */
2020 s
->window_bits
= DecodeWindowBits(br
); /* Reads 1..7 bits. */
2021 BROTLI_LOG_UINT(s
->window_bits
);
2022 if (s
->window_bits
== 9) {
2023 /* Value 9 is reserved for future use. */
2024 result
= BROTLI_FAILURE();
2027 s
->max_backward_distance
= (1 << s
->window_bits
) - 16;
2028 s
->max_backward_distance_minus_custom_dict_size
=
2029 s
->max_backward_distance
- s
->custom_dict_size
;
2031 /* Allocate memory for both block_type_trees and block_len_trees. */
2032 s
->block_type_trees
= (HuffmanCode
*)BROTLI_ALLOC(s
,
2033 6 * BROTLI_HUFFMAN_MAX_TABLE_SIZE
* sizeof(HuffmanCode
));
2034 if (s
->block_type_trees
== 0) {
2035 result
= BROTLI_FAILURE();
2038 s
->block_len_trees
= s
->block_type_trees
+
2039 3 * BROTLI_HUFFMAN_MAX_TABLE_SIZE
;
2041 s
->state
= BROTLI_STATE_METABLOCK_BEGIN
;
2042 /* No break, continue to next state */
2043 case BROTLI_STATE_METABLOCK_BEGIN
:
2044 BrotliStateMetablockBegin(s
);
2045 BROTLI_LOG_UINT(s
->pos
);
2046 s
->state
= BROTLI_STATE_METABLOCK_HEADER
;
2047 /* No break, continue to next state */
2048 case BROTLI_STATE_METABLOCK_HEADER
:
2049 result
= DecodeMetaBlockLength(s
, br
); /* Reads 2 - 31 bits. */
2050 if (result
!= BROTLI_RESULT_SUCCESS
) {
2053 BROTLI_LOG_UINT(s
->is_last_metablock
);
2054 BROTLI_LOG_UINT(s
->meta_block_remaining_len
);
2055 BROTLI_LOG_UINT(s
->is_metadata
);
2056 BROTLI_LOG_UINT(s
->is_uncompressed
);
2057 if (s
->is_metadata
|| s
->is_uncompressed
) {
2058 if (!BrotliJumpToByteBoundary(br
)) {
2059 result
= BROTLI_FAILURE();
2063 if (s
->is_metadata
) {
2064 s
->state
= BROTLI_STATE_METADATA
;
2067 if (s
->meta_block_remaining_len
== 0) {
2068 s
->state
= BROTLI_STATE_METABLOCK_DONE
;
2071 if (!s
->ringbuffer
) {
2072 if (!BrotliAllocateRingBuffer(s
, br
)) {
2073 result
= BROTLI_FAILURE();
2077 if (s
->is_uncompressed
) {
2078 s
->state
= BROTLI_STATE_UNCOMPRESSED
;
2081 s
->loop_counter
= 0;
2082 s
->state
= BROTLI_STATE_HUFFMAN_CODE_0
;
2084 case BROTLI_STATE_UNCOMPRESSED
: {
2085 int bytes_copied
= s
->meta_block_remaining_len
;
2086 result
= CopyUncompressedBlockToOutput(
2087 available_out
, next_out
, total_out
, s
);
2088 bytes_copied
-= s
->meta_block_remaining_len
;
2089 if (result
!= BROTLI_RESULT_SUCCESS
) {
2092 s
->state
= BROTLI_STATE_METABLOCK_DONE
;
2095 case BROTLI_STATE_METADATA
:
2096 for (; s
->meta_block_remaining_len
> 0; --s
->meta_block_remaining_len
) {
2098 /* Read one byte and ignore it. */
2099 if (!BrotliSafeReadBits(br
, 8, &bits
)) {
2100 result
= BROTLI_RESULT_NEEDS_MORE_INPUT
;
2104 if (result
== BROTLI_RESULT_SUCCESS
) {
2105 s
->state
= BROTLI_STATE_METABLOCK_DONE
;
2108 case BROTLI_STATE_HUFFMAN_CODE_0
:
2109 if (s
->loop_counter
>= 3) {
2110 s
->state
= BROTLI_STATE_METABLOCK_HEADER_2
;
2113 /* Reads 1..11 bits. */
2114 result
= DecodeVarLenUint8(s
, br
, &s
->num_block_types
[s
->loop_counter
]);
2115 if (result
!= BROTLI_RESULT_SUCCESS
) {
2118 s
->num_block_types
[s
->loop_counter
]++;
2119 BROTLI_LOG_UINT(s
->num_block_types
[s
->loop_counter
]);
2120 if (s
->num_block_types
[s
->loop_counter
] < 2) {
2124 s
->state
= BROTLI_STATE_HUFFMAN_CODE_1
;
2125 /* No break, continue to next state */
2126 case BROTLI_STATE_HUFFMAN_CODE_1
: {
2127 int tree_offset
= s
->loop_counter
* BROTLI_HUFFMAN_MAX_TABLE_SIZE
;
2128 result
= ReadHuffmanCode(s
->num_block_types
[s
->loop_counter
] + 2,
2129 &s
->block_type_trees
[tree_offset
], NULL
, s
);
2130 if (result
!= BROTLI_RESULT_SUCCESS
) break;
2131 s
->state
= BROTLI_STATE_HUFFMAN_CODE_2
;
2132 /* No break, continue to next state */
2134 case BROTLI_STATE_HUFFMAN_CODE_2
: {
2135 int tree_offset
= s
->loop_counter
* BROTLI_HUFFMAN_MAX_TABLE_SIZE
;
2136 result
= ReadHuffmanCode(kNumBlockLengthCodes
,
2137 &s
->block_len_trees
[tree_offset
], NULL
, s
);
2138 if (result
!= BROTLI_RESULT_SUCCESS
) break;
2139 s
->state
= BROTLI_STATE_HUFFMAN_CODE_3
;
2140 /* No break, continue to next state */
2142 case BROTLI_STATE_HUFFMAN_CODE_3
: {
2143 int tree_offset
= s
->loop_counter
* BROTLI_HUFFMAN_MAX_TABLE_SIZE
;
2144 if (!SafeReadBlockLength(s
, &s
->block_length
[s
->loop_counter
],
2145 &s
->block_len_trees
[tree_offset
], br
)) {
2146 result
= BROTLI_RESULT_NEEDS_MORE_INPUT
;
2149 BROTLI_LOG_UINT(s
->block_length
[s
->loop_counter
]);
2151 s
->state
= BROTLI_STATE_HUFFMAN_CODE_0
;
2154 case BROTLI_STATE_METABLOCK_HEADER_2
: {
2156 if (!BrotliSafeReadBits(br
, 6, &bits
)) {
2157 result
= BROTLI_RESULT_NEEDS_MORE_INPUT
;
2160 s
->distance_postfix_bits
= bits
& BitMask(2);
2162 s
->num_direct_distance_codes
= NUM_DISTANCE_SHORT_CODES
+
2163 (bits
<< s
->distance_postfix_bits
);
2164 BROTLI_LOG_UINT(s
->num_direct_distance_codes
);
2165 BROTLI_LOG_UINT(s
->distance_postfix_bits
);
2166 s
->distance_postfix_mask
= (int)BitMask(s
->distance_postfix_bits
);
2168 (uint8_t*)BROTLI_ALLOC(s
, (size_t)s
->num_block_types
[0]);
2169 if (s
->context_modes
== 0) {
2170 result
= BROTLI_FAILURE();
2173 s
->loop_counter
= 0;
2174 s
->state
= BROTLI_STATE_CONTEXT_MODES
;
2175 /* No break, continue to next state */
2177 case BROTLI_STATE_CONTEXT_MODES
:
2178 result
= ReadContextModes(s
);
2179 if (result
!= BROTLI_RESULT_SUCCESS
) {
2182 s
->state
= BROTLI_STATE_CONTEXT_MAP_1
;
2183 /* No break, continue to next state */
2184 case BROTLI_STATE_CONTEXT_MAP_1
: {
2186 result
= DecodeContextMap(s
->num_block_types
[0] << kLiteralContextBits
,
2187 &s
->num_literal_htrees
, &s
->context_map
, s
);
2188 if (result
!= BROTLI_RESULT_SUCCESS
) {
2191 s
->trivial_literal_context
= 1;
2192 for (j
= 0; j
< s
->num_block_types
[0] << kLiteralContextBits
; j
++) {
2193 if (s
->context_map
[j
] != j
>> kLiteralContextBits
) {
2194 s
->trivial_literal_context
= 0;
2198 s
->state
= BROTLI_STATE_CONTEXT_MAP_2
;
2199 /* No break, continue to next state */
2201 case BROTLI_STATE_CONTEXT_MAP_2
:
2203 uint32_t num_distance_codes
=
2204 s
->num_direct_distance_codes
+ (48U << s
->distance_postfix_bits
);
2205 result
= DecodeContextMap(
2206 s
->num_block_types
[2] << kDistanceContextBits
,
2207 &s
->num_dist_htrees
, &s
->dist_context_map
, s
);
2208 if (result
!= BROTLI_RESULT_SUCCESS
) {
2211 BrotliHuffmanTreeGroupInit(s
, &s
->literal_hgroup
, kNumLiteralCodes
,
2212 s
->num_literal_htrees
);
2213 BrotliHuffmanTreeGroupInit(s
, &s
->insert_copy_hgroup
,
2214 kNumInsertAndCopyCodes
,
2215 s
->num_block_types
[1]);
2216 BrotliHuffmanTreeGroupInit(s
, &s
->distance_hgroup
, num_distance_codes
,
2217 s
->num_dist_htrees
);
2218 if (s
->literal_hgroup
.codes
== 0 ||
2219 s
->insert_copy_hgroup
.codes
== 0 ||
2220 s
->distance_hgroup
.codes
== 0) {
2221 return BROTLI_FAILURE();
2224 s
->loop_counter
= 0;
2225 s
->state
= BROTLI_STATE_TREE_GROUP
;
2226 /* No break, continue to next state */
2227 case BROTLI_STATE_TREE_GROUP
:
2229 HuffmanTreeGroup
* hgroup
= NULL
;
2230 switch (s
->loop_counter
) {
2232 hgroup
= &s
->literal_hgroup
;
2235 hgroup
= &s
->insert_copy_hgroup
;
2238 hgroup
= &s
->distance_hgroup
;
2241 result
= HuffmanTreeGroupDecode(hgroup
, s
);
2243 if (result
!= BROTLI_RESULT_SUCCESS
) break;
2245 if (s
->loop_counter
>= 3) {
2246 uint8_t context_mode
= s
->context_modes
[s
->block_type_rb
[1]];
2247 s
->context_map_slice
= s
->context_map
;
2248 s
->dist_context_map_slice
= s
->dist_context_map
;
2249 s
->context_lookup1
=
2250 &kContextLookup
[kContextLookupOffsets
[context_mode
]];
2251 s
->context_lookup2
=
2252 &kContextLookup
[kContextLookupOffsets
[context_mode
+ 1]];
2253 s
->htree_command
= s
->insert_copy_hgroup
.htrees
[0];
2254 s
->literal_htree
= s
->literal_hgroup
.htrees
[s
->literal_htree_index
];
2255 s
->state
= BROTLI_STATE_COMMAND_BEGIN
;
2258 case BROTLI_STATE_COMMAND_BEGIN
:
2259 case BROTLI_STATE_COMMAND_INNER
:
2260 case BROTLI_STATE_COMMAND_POST_DECODE_LITERALS
:
2261 case BROTLI_STATE_COMMAND_POST_WRAP_COPY
:
2262 result
= ProcessCommands(s
);
2263 if (result
== BROTLI_RESULT_NEEDS_MORE_INPUT
) {
2264 result
= SafeProcessCommands(s
);
2267 case BROTLI_STATE_COMMAND_INNER_WRITE
:
2268 case BROTLI_STATE_COMMAND_POST_WRITE_1
:
2269 case BROTLI_STATE_COMMAND_POST_WRITE_2
:
2270 result
= WriteRingBuffer(available_out
, next_out
, total_out
, s
);
2271 if (result
!= BROTLI_RESULT_SUCCESS
) {
2274 s
->pos
-= s
->ringbuffer_size
;
2276 s
->max_distance
= s
->max_backward_distance
;
2277 if (s
->state
== BROTLI_STATE_COMMAND_POST_WRITE_1
) {
2278 memcpy(s
->ringbuffer
, s
->ringbuffer_end
, (size_t)s
->pos
);
2279 if (s
->meta_block_remaining_len
<= 0) {
2280 /* Next metablock, if any */
2281 s
->state
= BROTLI_STATE_METABLOCK_DONE
;
2283 s
->state
= BROTLI_STATE_COMMAND_BEGIN
;
2286 } else if (s
->state
== BROTLI_STATE_COMMAND_POST_WRITE_2
) {
2287 s
->state
= BROTLI_STATE_COMMAND_POST_WRAP_COPY
;
2288 } else { /* BROTLI_STATE_COMMAND_INNER_WRITE */
2289 if (s
->loop_counter
== 0) {
2290 if (s
->meta_block_remaining_len
<= 0) {
2291 s
->state
= BROTLI_STATE_METABLOCK_DONE
;
2293 s
->state
= BROTLI_STATE_COMMAND_POST_DECODE_LITERALS
;
2297 s
->state
= BROTLI_STATE_COMMAND_INNER
;
2300 case BROTLI_STATE_METABLOCK_DONE
:
2301 BrotliStateCleanupAfterMetablock(s
);
2302 if (!s
->is_last_metablock
) {
2303 s
->state
= BROTLI_STATE_METABLOCK_BEGIN
;
2306 if (!BrotliJumpToByteBoundary(br
)) {
2307 result
= BROTLI_FAILURE();
2309 if (s
->buffer_length
== 0) {
2310 BrotliBitReaderUnload(br
);
2311 *available_in
= br
->avail_in
;
2312 *next_in
= br
->next_in
;
2314 s
->state
= BROTLI_STATE_DONE
;
2315 /* No break, continue to next state */
2316 case BROTLI_STATE_DONE
:
2317 if (s
->ringbuffer
!= 0) {
2318 result
= WriteRingBuffer(available_out
, next_out
, total_out
, s
);
2319 if (result
!= BROTLI_RESULT_SUCCESS
) {
2329 void BrotliSetCustomDictionary(
2330 size_t size
, const uint8_t* dict
, BrotliState
* s
) {
2331 s
->custom_dict
= dict
;
2332 s
->custom_dict_size
= (int) size
;
2336 #if defined(__cplusplus) || defined(c_plusplus)