/src/brotli/c/dec/decode.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2013 Google Inc. All Rights Reserved. |
2 | | |
3 | | Distributed under MIT license. |
4 | | See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
5 | | */ |
6 | | |
7 | | #include <brotli/decode.h> |
8 | | |
9 | | #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 | 1.44k | #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 | 2.41G | #define HUFFMAN_TABLE_BITS 8U |
41 | 6.99k | #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 | 0 | BrotliDecoderState* state, BrotliDecoderParameter p, uint32_t value) { |
64 | 0 | if (state->state != BROTLI_STATE_UNINITED) return BROTLI_FALSE; |
65 | 0 | switch (p) { |
66 | 0 | case BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION: |
67 | 0 | state->canny_ringbuffer_allocation = !!value ? 0 : 1; |
68 | 0 | return BROTLI_TRUE; |
69 | | |
70 | 0 | case BROTLI_DECODER_PARAM_LARGE_WINDOW: |
71 | 0 | state->large_window = TO_BROTLI_BOOL(!!value); |
72 | 0 | return BROTLI_TRUE; |
73 | | |
74 | 0 | default: return BROTLI_FALSE; |
75 | 0 | } |
76 | 0 | } |
77 | | |
78 | | BrotliDecoderState* BrotliDecoderCreateInstance( |
79 | 4.59k | brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) { |
80 | 4.59k | BrotliDecoderState* state = 0; |
81 | 4.59k | if (!alloc_func && !free_func) { |
82 | 4.59k | state = (BrotliDecoderState*)malloc(sizeof(BrotliDecoderState)); |
83 | 4.59k | } else if (alloc_func && free_func) { |
84 | 0 | state = (BrotliDecoderState*)alloc_func(opaque, sizeof(BrotliDecoderState)); |
85 | 0 | } |
86 | 4.59k | if (state == 0) { |
87 | 0 | BROTLI_DUMP(); |
88 | 0 | return 0; |
89 | 0 | } |
90 | 4.59k | if (!BrotliDecoderStateInit(state, alloc_func, free_func, opaque)) { |
91 | 0 | BROTLI_DUMP(); |
92 | 0 | if (!alloc_func && !free_func) { |
93 | 0 | free(state); |
94 | 0 | } else if (alloc_func && free_func) { |
95 | 0 | free_func(opaque, state); |
96 | 0 | } |
97 | 0 | return 0; |
98 | 0 | } |
99 | 4.59k | return state; |
100 | 4.59k | } |
101 | | |
102 | | /* Deinitializes and frees BrotliDecoderState instance. */ |
103 | 4.59k | void BrotliDecoderDestroyInstance(BrotliDecoderState* state) { |
104 | 4.59k | if (!state) { |
105 | 0 | return; |
106 | 4.59k | } else { |
107 | 4.59k | brotli_free_func free_func = state->free_func; |
108 | 4.59k | void* opaque = state->memory_manager_opaque; |
109 | 4.59k | BrotliDecoderStateCleanup(state); |
110 | 4.59k | free_func(opaque, state); |
111 | 4.59k | } |
112 | 4.59k | } |
113 | | |
114 | | /* Saves error code and converts it to BrotliDecoderResult. */ |
115 | | static BROTLI_NOINLINE BrotliDecoderResult SaveErrorCode( |
116 | 6.03M | BrotliDecoderState* s, BrotliDecoderErrorCode e, size_t consumed_input) { |
117 | 6.03M | s->error_code = (int)e; |
118 | 6.03M | s->used_input += consumed_input; |
119 | 6.03M | if ((s->buffer_length != 0) && (s->br.next_in == s->br.last_in)) { |
120 | | /* If internal buffer is depleted at last, reset it. */ |
121 | 3 | s->buffer_length = 0; |
122 | 3 | } |
123 | 6.03M | switch (e) { |
124 | 88 | case BROTLI_DECODER_SUCCESS: |
125 | 88 | return BROTLI_DECODER_RESULT_SUCCESS; |
126 | | |
127 | 2.90M | case BROTLI_DECODER_NEEDS_MORE_INPUT: |
128 | 2.90M | return BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT; |
129 | | |
130 | 3.13M | case BROTLI_DECODER_NEEDS_MORE_OUTPUT: |
131 | 3.13M | return BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; |
132 | | |
133 | 1.44k | default: |
134 | 1.44k | return BROTLI_DECODER_RESULT_ERROR; |
135 | 6.03M | } |
136 | 6.03M | } |
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 | 4.59k | BrotliBitReader* br) { |
142 | 4.59k | brotli_reg_t n; |
143 | 4.59k | BROTLI_BOOL large_window = s->large_window; |
144 | 4.59k | s->large_window = BROTLI_FALSE; |
145 | 4.59k | BrotliTakeBits(br, 1, &n); |
146 | 4.59k | if (n == 0) { |
147 | 3.13k | s->window_bits = 16; |
148 | 3.13k | return BROTLI_DECODER_SUCCESS; |
149 | 3.13k | } |
150 | 1.45k | BrotliTakeBits(br, 3, &n); |
151 | 1.45k | if (n != 0) { |
152 | 823 | s->window_bits = (17u + n) & 63u; |
153 | 823 | return BROTLI_DECODER_SUCCESS; |
154 | 823 | } |
155 | 630 | BrotliTakeBits(br, 3, &n); |
156 | 630 | if (n == 1) { |
157 | 1 | if (large_window) { |
158 | 0 | BrotliTakeBits(br, 1, &n); |
159 | 0 | if (n == 1) { |
160 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS); |
161 | 0 | } |
162 | 0 | s->large_window = BROTLI_TRUE; |
163 | 0 | return BROTLI_DECODER_SUCCESS; |
164 | 1 | } else { |
165 | 1 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS); |
166 | 1 | } |
167 | 1 | } |
168 | 629 | if (n != 0) { |
169 | 572 | s->window_bits = (8u + n) & 63u; |
170 | 572 | return BROTLI_DECODER_SUCCESS; |
171 | 572 | } |
172 | 57 | s->window_bits = 17; |
173 | 57 | return BROTLI_DECODER_SUCCESS; |
174 | 629 | } |
175 | | |
176 | 514M | 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 | 514M | uint32_t buffer[4]; |
181 | 514M | memcpy(buffer, src, 16); |
182 | 514M | memcpy(dst, buffer, 16); |
183 | 514M | #endif |
184 | 514M | } |
185 | | |
186 | | /* Decodes a number in the range [0..255], by reading 1 - 11 bits. */ |
187 | | static BROTLI_NOINLINE BrotliDecoderErrorCode DecodeVarLenUint8( |
188 | 67.9k | BrotliDecoderState* s, BrotliBitReader* br, brotli_reg_t* value) { |
189 | 67.9k | brotli_reg_t bits; |
190 | 67.9k | switch (s->substate_decode_uint8) { |
191 | 66.2k | case BROTLI_STATE_DECODE_UINT8_NONE: |
192 | 66.2k | if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, 1, &bits))) { |
193 | 3.50k | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
194 | 3.50k | } |
195 | 62.7k | if (bits == 0) { |
196 | 56.4k | *value = 0; |
197 | 56.4k | return BROTLI_DECODER_SUCCESS; |
198 | 56.4k | } |
199 | | /* Fall through. */ |
200 | | |
201 | 7.09k | case BROTLI_STATE_DECODE_UINT8_SHORT: |
202 | 7.09k | if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, 3, &bits))) { |
203 | 798 | s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_SHORT; |
204 | 798 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
205 | 798 | } |
206 | 6.29k | if (bits == 0) { |
207 | 3.27k | *value = 1; |
208 | 3.27k | s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE; |
209 | 3.27k | return BROTLI_DECODER_SUCCESS; |
210 | 3.27k | } |
211 | | /* Use output value as a temporary storage. It MUST be persisted. */ |
212 | 3.02k | *value = bits; |
213 | | /* Fall through. */ |
214 | | |
215 | 3.96k | case BROTLI_STATE_DECODE_UINT8_LONG: |
216 | 3.96k | if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, *value, &bits))) { |
217 | 983 | s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_LONG; |
218 | 983 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
219 | 983 | } |
220 | 2.98k | *value = ((brotli_reg_t)1U << *value) + bits; |
221 | 2.98k | s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE; |
222 | 2.98k | return BROTLI_DECODER_SUCCESS; |
223 | | |
224 | 0 | default: |
225 | 0 | return |
226 | 0 | BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); /* COV_NF_LINE */ |
227 | 67.9k | } |
228 | 67.9k | } |
229 | | |
230 | | /* Decodes a metablock length and flags by reading 2 - 31 bits. */ |
231 | | static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength( |
232 | 36.9k | BrotliDecoderState* s, BrotliBitReader* br) { |
233 | 36.9k | brotli_reg_t bits; |
234 | 36.9k | int i; |
235 | 60.2k | for (;;) { |
236 | 60.2k | switch (s->substate_metablock_header) { |
237 | 22.3k | case BROTLI_STATE_METABLOCK_HEADER_NONE: |
238 | 22.3k | if (!BrotliSafeReadBits(br, 1, &bits)) { |
239 | 2.23k | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
240 | 2.23k | } |
241 | 20.1k | s->is_last_metablock = bits ? 1 : 0; |
242 | 20.1k | s->meta_block_remaining_len = 0; |
243 | 20.1k | s->is_uncompressed = 0; |
244 | 20.1k | s->is_metadata = 0; |
245 | 20.1k | if (!s->is_last_metablock) { |
246 | 19.0k | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NIBBLES; |
247 | 19.0k | break; |
248 | 19.0k | } |
249 | 1.09k | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_EMPTY; |
250 | | /* Fall through. */ |
251 | | |
252 | 1.11k | case BROTLI_STATE_METABLOCK_HEADER_EMPTY: |
253 | 1.11k | if (!BrotliSafeReadBits(br, 1, &bits)) { |
254 | 27 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
255 | 27 | } |
256 | 1.09k | if (bits) { |
257 | 63 | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; |
258 | 63 | return BROTLI_DECODER_SUCCESS; |
259 | 63 | } |
260 | 1.02k | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NIBBLES; |
261 | | /* Fall through. */ |
262 | | |
263 | 21.2k | case BROTLI_STATE_METABLOCK_HEADER_NIBBLES: |
264 | 21.2k | if (!BrotliSafeReadBits(br, 2, &bits)) { |
265 | 1.22k | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
266 | 1.22k | } |
267 | 20.0k | s->size_nibbles = (uint8_t)(bits + 4); |
268 | 20.0k | s->loop_counter = 0; |
269 | 20.0k | if (bits == 3) { |
270 | 4.33k | s->is_metadata = 1; |
271 | 4.33k | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_RESERVED; |
272 | 4.33k | break; |
273 | 4.33k | } |
274 | 15.7k | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_SIZE; |
275 | | /* Fall through. */ |
276 | | |
277 | 27.5k | case BROTLI_STATE_METABLOCK_HEADER_SIZE: |
278 | 27.5k | i = s->loop_counter; |
279 | 92.6k | for (; i < (int)s->size_nibbles; ++i) { |
280 | 77.0k | if (!BrotliSafeReadBits(br, 4, &bits)) { |
281 | 11.9k | s->loop_counter = i; |
282 | 11.9k | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
283 | 11.9k | } |
284 | 65.0k | if (i + 1 == (int)s->size_nibbles && s->size_nibbles > 4 && |
285 | 65.0k | bits == 0) { |
286 | 7 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE); |
287 | 7 | } |
288 | 65.0k | s->meta_block_remaining_len |= (int)(bits << (i * 4)); |
289 | 65.0k | } |
290 | 15.6k | s->substate_metablock_header = |
291 | 15.6k | BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED; |
292 | | /* Fall through. */ |
293 | | |
294 | 16.1k | case BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED: |
295 | 16.1k | if (!s->is_last_metablock) { |
296 | 15.1k | if (!BrotliSafeReadBits(br, 1, &bits)) { |
297 | 502 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
298 | 502 | } |
299 | 14.6k | s->is_uncompressed = bits ? 1 : 0; |
300 | 14.6k | } |
301 | 15.6k | ++s->meta_block_remaining_len; |
302 | 15.6k | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; |
303 | 15.6k | return BROTLI_DECODER_SUCCESS; |
304 | | |
305 | 4.81k | case BROTLI_STATE_METABLOCK_HEADER_RESERVED: |
306 | 4.81k | if (!BrotliSafeReadBits(br, 1, &bits)) { |
307 | 479 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
308 | 479 | } |
309 | 4.33k | if (bits != 0) { |
310 | 22 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_RESERVED); |
311 | 22 | } |
312 | 4.31k | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_BYTES; |
313 | | /* Fall through. */ |
314 | | |
315 | 4.53k | case BROTLI_STATE_METABLOCK_HEADER_BYTES: |
316 | 4.53k | if (!BrotliSafeReadBits(br, 2, &bits)) { |
317 | 225 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
318 | 225 | } |
319 | 4.30k | if (bits == 0) { |
320 | 2.79k | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; |
321 | 2.79k | return BROTLI_DECODER_SUCCESS; |
322 | 2.79k | } |
323 | 1.50k | s->size_nibbles = (uint8_t)bits; |
324 | 1.50k | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_METADATA; |
325 | | /* Fall through. */ |
326 | | |
327 | 1.81k | case BROTLI_STATE_METABLOCK_HEADER_METADATA: |
328 | 1.81k | i = s->loop_counter; |
329 | 3.57k | for (; i < (int)s->size_nibbles; ++i) { |
330 | 2.08k | if (!BrotliSafeReadBits(br, 8, &bits)) { |
331 | 331 | s->loop_counter = i; |
332 | 331 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
333 | 331 | } |
334 | 1.75k | if (i + 1 == (int)s->size_nibbles && s->size_nibbles > 1 && |
335 | 1.75k | bits == 0) { |
336 | 1 | return BROTLI_FAILURE( |
337 | 1 | BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE); |
338 | 1 | } |
339 | 1.75k | s->meta_block_remaining_len |= (int)(bits << (i * 8)); |
340 | 1.75k | } |
341 | 1.48k | ++s->meta_block_remaining_len; |
342 | 1.48k | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; |
343 | 1.48k | return BROTLI_DECODER_SUCCESS; |
344 | | |
345 | 0 | default: |
346 | 0 | return |
347 | 0 | BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); /* COV_NF_LINE */ |
348 | 60.2k | } |
349 | 60.2k | } |
350 | 36.9k | } |
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 | 1.36G | BrotliBitReader* br) { |
359 | 1.36G | BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table); |
360 | 1.36G | BROTLI_HC_ADJUST_TABLE_INDEX(table, bits & HUFFMAN_TABLE_MASK); |
361 | 1.36G | if (BROTLI_HC_FAST_LOAD_BITS(table) > HUFFMAN_TABLE_BITS) { |
362 | 12.7k | brotli_reg_t nbits = BROTLI_HC_FAST_LOAD_BITS(table) - HUFFMAN_TABLE_BITS; |
363 | 12.7k | BrotliDropBits(br, HUFFMAN_TABLE_BITS); |
364 | 12.7k | BROTLI_HC_ADJUST_TABLE_INDEX(table, |
365 | 12.7k | BROTLI_HC_FAST_LOAD_VALUE(table) + |
366 | 12.7k | ((bits >> HUFFMAN_TABLE_BITS) & BitMask(nbits))); |
367 | 12.7k | } |
368 | 1.36G | BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(table)); |
369 | 1.36G | return BROTLI_HC_FAST_LOAD_VALUE(table); |
370 | 1.36G | } |
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 | 281M | BrotliBitReader* br) { |
376 | 281M | return DecodeSymbol(BrotliGet16BitsUnmasked(br), table, br); |
377 | 281M | } |
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 | 1.15G | const HuffmanCode* table, BrotliBitReader* br, brotli_reg_t* result) { |
383 | 1.15G | brotli_reg_t val; |
384 | 1.15G | brotli_reg_t available_bits = BrotliGetAvailableBits(br); |
385 | 1.15G | BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table); |
386 | 1.15G | if (available_bits == 0) { |
387 | 109M | if (BROTLI_HC_FAST_LOAD_BITS(table) == 0) { |
388 | 107M | *result = BROTLI_HC_FAST_LOAD_VALUE(table); |
389 | 107M | return BROTLI_TRUE; |
390 | 107M | } |
391 | 2.29M | return BROTLI_FALSE; /* No valid bits at all. */ |
392 | 109M | } |
393 | 1.04G | val = BrotliGetBitsUnmasked(br); |
394 | 1.04G | BROTLI_HC_ADJUST_TABLE_INDEX(table, val & HUFFMAN_TABLE_MASK); |
395 | 1.04G | if (BROTLI_HC_FAST_LOAD_BITS(table) <= HUFFMAN_TABLE_BITS) { |
396 | 1.04G | if (BROTLI_HC_FAST_LOAD_BITS(table) <= available_bits) { |
397 | 1.04G | BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(table)); |
398 | 1.04G | *result = BROTLI_HC_FAST_LOAD_VALUE(table); |
399 | 1.04G | return BROTLI_TRUE; |
400 | 1.04G | } else { |
401 | 371k | return BROTLI_FALSE; /* Not enough bits for the first level. */ |
402 | 371k | } |
403 | 1.04G | } |
404 | 4.96k | if (available_bits <= HUFFMAN_TABLE_BITS) { |
405 | 3.43k | return BROTLI_FALSE; /* Not enough bits to move to the second level. */ |
406 | 3.43k | } |
407 | | |
408 | | /* Speculatively drop HUFFMAN_TABLE_BITS. */ |
409 | 1.52k | val = (val & BitMask(BROTLI_HC_FAST_LOAD_BITS(table))) >> HUFFMAN_TABLE_BITS; |
410 | 1.52k | available_bits -= HUFFMAN_TABLE_BITS; |
411 | 1.52k | BROTLI_HC_ADJUST_TABLE_INDEX(table, BROTLI_HC_FAST_LOAD_VALUE(table) + val); |
412 | 1.52k | if (available_bits < BROTLI_HC_FAST_LOAD_BITS(table)) { |
413 | 211 | return BROTLI_FALSE; /* Not enough bits for the second level. */ |
414 | 211 | } |
415 | | |
416 | 1.31k | BrotliDropBits(br, HUFFMAN_TABLE_BITS + BROTLI_HC_FAST_LOAD_BITS(table)); |
417 | 1.31k | *result = BROTLI_HC_FAST_LOAD_VALUE(table); |
418 | 1.31k | return BROTLI_TRUE; |
419 | 1.52k | } |
420 | | |
421 | | static BROTLI_INLINE BROTLI_BOOL SafeReadSymbol( |
422 | 2.23G | const HuffmanCode* table, BrotliBitReader* br, brotli_reg_t* result) { |
423 | 2.23G | brotli_reg_t val; |
424 | 2.23G | if (BROTLI_PREDICT_TRUE(BrotliSafeGetBits(br, 15, &val))) { |
425 | 1.08G | *result = DecodeSymbol(val, table, br); |
426 | 1.08G | return BROTLI_TRUE; |
427 | 1.08G | } |
428 | 1.15G | return SafeDecodeSymbol(table, br, result); |
429 | 2.23G | } |
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 | 1.04G | brotli_reg_t* value) { |
437 | 1.04G | if (safe) { |
438 | 242M | return; |
439 | 242M | } |
440 | 806M | BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table); |
441 | 806M | BROTLI_HC_ADJUST_TABLE_INDEX(table, BrotliGetBits(br, HUFFMAN_TABLE_BITS)); |
442 | 806M | *bits = BROTLI_HC_FAST_LOAD_BITS(table); |
443 | 806M | *value = BROTLI_HC_FAST_LOAD_VALUE(table); |
444 | 806M | } |
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 | 731M | brotli_reg_t* value) { |
452 | 731M | brotli_reg_t result = *value; |
453 | 731M | if (BROTLI_PREDICT_FALSE(*bits > HUFFMAN_TABLE_BITS)) { |
454 | 6.99k | brotli_reg_t val = BrotliGet16BitsUnmasked(br); |
455 | 6.99k | const HuffmanCode* ext = table + (val & HUFFMAN_TABLE_MASK) + *value; |
456 | 6.99k | brotli_reg_t mask = BitMask((*bits - HUFFMAN_TABLE_BITS)); |
457 | 6.99k | BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(ext); |
458 | 6.99k | BrotliDropBits(br, HUFFMAN_TABLE_BITS); |
459 | 6.99k | BROTLI_HC_ADJUST_TABLE_INDEX(ext, (val >> HUFFMAN_TABLE_BITS) & mask); |
460 | 6.99k | BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(ext)); |
461 | 6.99k | result = BROTLI_HC_FAST_LOAD_VALUE(ext); |
462 | 731M | } else { |
463 | 731M | BrotliDropBits(br, *bits); |
464 | 731M | } |
465 | 731M | PreloadSymbol(0, table, br, bits, value); |
466 | 731M | return result; |
467 | 731M | } |
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 | 150M | const int limit) { |
479 | | /* Calculate range where CheckInputAmount is always true. |
480 | | Start with the number of bytes we can read. */ |
481 | 150M | int64_t new_lim = br->guard_in - br->next_in; |
482 | | /* Convert to bits, since sybmols use variable number of bits. */ |
483 | 150M | new_lim *= 8; |
484 | | /* At most 15 bits per symbol, so this is safe. */ |
485 | 150M | new_lim /= 15; |
486 | 150M | const int kMaximalOverread = 4; |
487 | 150M | int pos_limit = limit; |
488 | 150M | int copies = 0; |
489 | 150M | if ((new_lim - kMaximalOverread) <= limit) { |
490 | | // Safe cast, since new_lim is already < num_steps |
491 | 82.5M | pos_limit = (int)(new_lim - kMaximalOverread); |
492 | 82.5M | } |
493 | 150M | if (pos_limit < 0) { |
494 | 67.7M | pos_limit = 0; |
495 | 67.7M | } |
496 | 150M | copies = pos_limit; |
497 | 150M | pos_limit += pos; |
498 | | /* Fast path, caller made sure it is safe to write, |
499 | | we verified that is is safe to read. */ |
500 | 272M | for (; pos < pos_limit; pos++) { |
501 | 121M | BROTLI_DCHECK(BrotliCheckInputAmount(br)); |
502 | 121M | ringbuffer[pos] = (uint8_t)ReadPreloadedSymbol(table, br, bits, value); |
503 | 121M | BROTLI_LOG_ARRAY_INDEX(ringbuffer, pos); |
504 | 121M | } |
505 | | /* Do the remainder, caller made sure it is safe to write, |
506 | | we need to bverify that it is safe to read. */ |
507 | 760M | while (BrotliCheckInputAmount(br) && copies < limit) { |
508 | 609M | ringbuffer[pos] = (uint8_t)ReadPreloadedSymbol(table, br, bits, value); |
509 | 609M | BROTLI_LOG_ARRAY_INDEX(ringbuffer, pos); |
510 | 609M | pos++; |
511 | 609M | copies++; |
512 | 609M | } |
513 | 150M | return copies; |
514 | 150M | } |
515 | | |
516 | 72.3k | static BROTLI_INLINE brotli_reg_t Log2Floor(brotli_reg_t x) { |
517 | 72.3k | brotli_reg_t result = 0; |
518 | 628k | while (x) { |
519 | 556k | x >>= 1; |
520 | 556k | ++result; |
521 | 556k | } |
522 | 72.3k | return result; |
523 | 72.3k | } |
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 | 72.3k | BrotliDecoderState* s) { |
531 | | /* max_bits == 1..11; symbol == 0..3; 1..44 bits will be read. */ |
532 | 72.3k | BrotliBitReader* br = &s->br; |
533 | 72.3k | BrotliMetablockHeaderArena* h = &s->arena.header; |
534 | 72.3k | brotli_reg_t max_bits = Log2Floor(alphabet_size_max - 1); |
535 | 72.3k | brotli_reg_t i = h->sub_loop_counter; |
536 | 72.3k | brotli_reg_t num_symbols = h->symbol; |
537 | 136k | while (i <= num_symbols) { |
538 | 89.4k | brotli_reg_t v; |
539 | 89.4k | if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, max_bits, &v))) { |
540 | 25.3k | h->sub_loop_counter = i; |
541 | 25.3k | h->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_READ; |
542 | 25.3k | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
543 | 25.3k | } |
544 | 64.1k | if (v >= alphabet_size_limit) { |
545 | 28 | return |
546 | 28 | BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET); |
547 | 28 | } |
548 | 64.1k | h->symbols_lists_array[i] = (uint16_t)v; |
549 | 64.1k | BROTLI_LOG_UINT(h->symbols_lists_array[i]); |
550 | 64.1k | ++i; |
551 | 64.1k | } |
552 | | |
553 | 64.0k | for (i = 0; i < num_symbols; ++i) { |
554 | 17.1k | brotli_reg_t k = i + 1; |
555 | 43.4k | for (; k <= num_symbols; ++k) { |
556 | 26.3k | if (h->symbols_lists_array[i] == h->symbols_lists_array[k]) { |
557 | 15 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME); |
558 | 15 | } |
559 | 26.3k | } |
560 | 17.1k | } |
561 | | |
562 | 46.9k | return BROTLI_DECODER_SUCCESS; |
563 | 46.9k | } |
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 | 379k | uint16_t* code_length_histo, int* next_symbol) { |
575 | 379k | *repeat = 0; |
576 | 379k | if (code_len != 0) { /* code_len == 1..15 */ |
577 | 375k | symbol_lists[next_symbol[code_len]] = (uint16_t)(*symbol); |
578 | 375k | next_symbol[code_len] = (int)(*symbol); |
579 | 375k | *prev_code_len = code_len; |
580 | 375k | *space -= 32768U >> code_len; |
581 | 375k | code_length_histo[code_len]++; |
582 | 375k | BROTLI_LOG(("[ReadHuffmanCode] code_length[%d] = %d\n", |
583 | 375k | (int)*symbol, (int)code_len)); |
584 | 375k | } |
585 | 379k | (*symbol)++; |
586 | 379k | } |
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 | 5.42k | uint16_t* code_length_histo, int* next_symbol) { |
603 | 5.42k | brotli_reg_t old_repeat; |
604 | 5.42k | brotli_reg_t extra_bits = 3; /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */ |
605 | 5.42k | brotli_reg_t new_len = 0; /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */ |
606 | 5.42k | if (code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) { |
607 | 3.42k | new_len = *prev_code_len; |
608 | 3.42k | extra_bits = 2; |
609 | 3.42k | } |
610 | 5.42k | if (*repeat_code_len != new_len) { |
611 | 1.39k | *repeat = 0; |
612 | 1.39k | *repeat_code_len = new_len; |
613 | 1.39k | } |
614 | 5.42k | old_repeat = *repeat; |
615 | 5.42k | if (*repeat > 0) { |
616 | 1.28k | *repeat -= 2; |
617 | 1.28k | *repeat <<= extra_bits; |
618 | 1.28k | } |
619 | 5.42k | *repeat += repeat_delta + 3U; |
620 | 5.42k | repeat_delta = *repeat - old_repeat; |
621 | 5.42k | if (*symbol + repeat_delta > alphabet_size) { |
622 | 96 | BROTLI_DUMP(); |
623 | 96 | *symbol = alphabet_size; |
624 | 96 | *space = 0xFFFFF; |
625 | 96 | return; |
626 | 96 | } |
627 | 5.32k | BROTLI_LOG(("[ReadHuffmanCode] code_length[%d..%d] = %d\n", |
628 | 5.32k | (int)*symbol, (int)(*symbol + repeat_delta - 1), (int)*repeat_code_len)); |
629 | 5.32k | if (*repeat_code_len != 0) { |
630 | 3.38k | brotli_reg_t last = *symbol + repeat_delta; |
631 | 3.38k | int next = next_symbol[*repeat_code_len]; |
632 | 47.8k | do { |
633 | 47.8k | symbol_lists[next] = (uint16_t)*symbol; |
634 | 47.8k | next = (int)*symbol; |
635 | 47.8k | } while (++(*symbol) != last); |
636 | 3.38k | next_symbol[*repeat_code_len] = next; |
637 | 3.38k | *space -= repeat_delta << (15 - *repeat_code_len); |
638 | 3.38k | code_length_histo[*repeat_code_len] = |
639 | 3.38k | (uint16_t)(code_length_histo[*repeat_code_len] + repeat_delta); |
640 | 3.38k | } else { |
641 | 1.93k | *symbol += repeat_delta; |
642 | 1.93k | } |
643 | 5.32k | } |
644 | | |
645 | | /* Reads and decodes symbol codelengths. */ |
646 | | static BrotliDecoderErrorCode ReadSymbolCodeLengths( |
647 | 17.8k | brotli_reg_t alphabet_size, BrotliDecoderState* s) { |
648 | 17.8k | BrotliBitReader* br = &s->br; |
649 | 17.8k | BrotliMetablockHeaderArena* h = &s->arena.header; |
650 | 17.8k | brotli_reg_t symbol = h->symbol; |
651 | 17.8k | brotli_reg_t repeat = h->repeat; |
652 | 17.8k | brotli_reg_t space = h->space; |
653 | 17.8k | brotli_reg_t prev_code_len = h->prev_code_len; |
654 | 17.8k | brotli_reg_t repeat_code_len = h->repeat_code_len; |
655 | 17.8k | uint16_t* symbol_lists = h->symbol_lists; |
656 | 17.8k | uint16_t* code_length_histo = h->code_length_histo; |
657 | 17.8k | int* next_symbol = h->next_symbol; |
658 | 17.8k | if (!BrotliWarmupBitReader(br)) { |
659 | 697 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
660 | 697 | } |
661 | 237k | while (symbol < alphabet_size && space > 0) { |
662 | 233k | const HuffmanCode* p = h->table; |
663 | 233k | brotli_reg_t code_len; |
664 | 233k | BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(p); |
665 | 233k | if (!BrotliCheckInputAmount(br)) { |
666 | 12.9k | h->symbol = symbol; |
667 | 12.9k | h->repeat = repeat; |
668 | 12.9k | h->prev_code_len = prev_code_len; |
669 | 12.9k | h->repeat_code_len = repeat_code_len; |
670 | 12.9k | h->space = space; |
671 | 12.9k | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
672 | 12.9k | } |
673 | 220k | BrotliFillBitWindow16(br); |
674 | 220k | BROTLI_HC_ADJUST_TABLE_INDEX(p, BrotliGetBitsUnmasked(br) & |
675 | 220k | BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH)); |
676 | 220k | BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p)); /* Use 1..5 bits. */ |
677 | 220k | code_len = BROTLI_HC_FAST_LOAD_VALUE(p); /* code_len == 0..17 */ |
678 | 220k | if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) { |
679 | 218k | ProcessSingleCodeLength(code_len, &symbol, &repeat, &space, |
680 | 218k | &prev_code_len, symbol_lists, code_length_histo, next_symbol); |
681 | 218k | } else { /* code_len == 16..17, extra_bits == 2..3 */ |
682 | 1.95k | brotli_reg_t extra_bits = |
683 | 1.95k | (code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) ? 2 : 3; |
684 | 1.95k | brotli_reg_t repeat_delta = |
685 | 1.95k | BrotliGetBitsUnmasked(br) & BitMask(extra_bits); |
686 | 1.95k | BrotliDropBits(br, extra_bits); |
687 | 1.95k | ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size, |
688 | 1.95k | &symbol, &repeat, &space, &prev_code_len, &repeat_code_len, |
689 | 1.95k | symbol_lists, code_length_histo, next_symbol); |
690 | 1.95k | } |
691 | 220k | } |
692 | 4.15k | h->space = space; |
693 | 4.15k | return BROTLI_DECODER_SUCCESS; |
694 | 17.1k | } |
695 | | |
696 | | static BrotliDecoderErrorCode SafeReadSymbolCodeLengths( |
697 | 13.6k | brotli_reg_t alphabet_size, BrotliDecoderState* s) { |
698 | 13.6k | BrotliBitReader* br = &s->br; |
699 | 13.6k | BrotliMetablockHeaderArena* h = &s->arena.header; |
700 | 13.6k | BROTLI_BOOL get_byte = BROTLI_FALSE; |
701 | 195k | while (h->symbol < alphabet_size && h->space > 0) { |
702 | 189k | const HuffmanCode* p = h->table; |
703 | 189k | brotli_reg_t code_len; |
704 | 189k | brotli_reg_t available_bits; |
705 | 189k | brotli_reg_t bits = 0; |
706 | 189k | BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(p); |
707 | 189k | if (get_byte && !BrotliPullByte(br)) return BROTLI_DECODER_NEEDS_MORE_INPUT; |
708 | 181k | get_byte = BROTLI_FALSE; |
709 | 181k | available_bits = BrotliGetAvailableBits(br); |
710 | 181k | if (available_bits != 0) { |
711 | 135k | bits = (uint32_t)BrotliGetBitsUnmasked(br); |
712 | 135k | } |
713 | 181k | BROTLI_HC_ADJUST_TABLE_INDEX(p, |
714 | 181k | bits & BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH)); |
715 | 181k | if (BROTLI_HC_FAST_LOAD_BITS(p) > available_bits) { |
716 | 15.3k | get_byte = BROTLI_TRUE; |
717 | 15.3k | continue; |
718 | 15.3k | } |
719 | 166k | code_len = BROTLI_HC_FAST_LOAD_VALUE(p); /* code_len == 0..17 */ |
720 | 166k | if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) { |
721 | 160k | BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p)); |
722 | 160k | ProcessSingleCodeLength(code_len, &h->symbol, &h->repeat, &h->space, |
723 | 160k | &h->prev_code_len, h->symbol_lists, h->code_length_histo, |
724 | 160k | h->next_symbol); |
725 | 160k | } else { /* code_len == 16..17, extra_bits == 2..3 */ |
726 | 5.25k | brotli_reg_t extra_bits = code_len - 14U; |
727 | 5.25k | brotli_reg_t repeat_delta = (bits >> BROTLI_HC_FAST_LOAD_BITS(p)) & |
728 | 5.25k | BitMask(extra_bits); |
729 | 5.25k | if (available_bits < BROTLI_HC_FAST_LOAD_BITS(p) + extra_bits) { |
730 | 1.78k | get_byte = BROTLI_TRUE; |
731 | 1.78k | continue; |
732 | 1.78k | } |
733 | 3.46k | BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p) + extra_bits); |
734 | 3.46k | ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size, |
735 | 3.46k | &h->symbol, &h->repeat, &h->space, &h->prev_code_len, |
736 | 3.46k | &h->repeat_code_len, h->symbol_lists, h->code_length_histo, |
737 | 3.46k | h->next_symbol); |
738 | 3.46k | } |
739 | 166k | } |
740 | 5.93k | return BROTLI_DECODER_SUCCESS; |
741 | 13.6k | } |
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 | 17.3k | static BrotliDecoderErrorCode ReadCodeLengthCodeLengths(BrotliDecoderState* s) { |
746 | 17.3k | BrotliBitReader* br = &s->br; |
747 | 17.3k | BrotliMetablockHeaderArena* h = &s->arena.header; |
748 | 17.3k | brotli_reg_t num_codes = h->repeat; |
749 | 17.3k | brotli_reg_t space = h->space; |
750 | 17.3k | brotli_reg_t i = h->sub_loop_counter; |
751 | 84.0k | for (; i < BROTLI_CODE_LENGTH_CODES; ++i) { |
752 | 82.9k | const uint8_t code_len_idx = kCodeLengthCodeOrder[i]; |
753 | 82.9k | brotli_reg_t ix; |
754 | 82.9k | brotli_reg_t v; |
755 | 82.9k | if (BROTLI_PREDICT_FALSE(!BrotliSafeGetBits(br, 4, &ix))) { |
756 | 12.0k | brotli_reg_t available_bits = BrotliGetAvailableBits(br); |
757 | 12.0k | if (available_bits != 0) { |
758 | 7.54k | ix = BrotliGetBitsUnmasked(br) & 0xF; |
759 | 7.54k | } else { |
760 | 4.53k | ix = 0; |
761 | 4.53k | } |
762 | 12.0k | if (kCodeLengthPrefixLength[ix] > available_bits) { |
763 | 6.91k | h->sub_loop_counter = i; |
764 | 6.91k | h->repeat = num_codes; |
765 | 6.91k | h->space = space; |
766 | 6.91k | h->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX; |
767 | 6.91k | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
768 | 6.91k | } |
769 | 12.0k | } |
770 | 76.0k | v = kCodeLengthPrefixValue[ix]; |
771 | 76.0k | BrotliDropBits(br, kCodeLengthPrefixLength[ix]); |
772 | 76.0k | h->code_length_code_lengths[code_len_idx] = (uint8_t)v; |
773 | 76.0k | BROTLI_LOG_ARRAY_INDEX(h->code_length_code_lengths, code_len_idx); |
774 | 76.0k | if (v != 0) { |
775 | 39.4k | space = space - (32U >> v); |
776 | 39.4k | ++num_codes; |
777 | 39.4k | ++h->code_length_histo[v]; |
778 | 39.4k | if (space - 1U >= 32U) { |
779 | | /* space is 0 or wrapped around. */ |
780 | 9.41k | break; |
781 | 9.41k | } |
782 | 39.4k | } |
783 | 76.0k | } |
784 | 10.4k | if (!(num_codes == 1 || space == 0)) { |
785 | 134 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_CL_SPACE); |
786 | 134 | } |
787 | 10.3k | return BROTLI_DECODER_SUCCESS; |
788 | 10.4k | } |
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 | 108k | BrotliDecoderState* s) { |
806 | 108k | BrotliBitReader* br = &s->br; |
807 | 108k | BrotliMetablockHeaderArena* h = &s->arena.header; |
808 | | /* State machine. */ |
809 | 119k | for (;;) { |
810 | 119k | switch (h->substate_huffman) { |
811 | 61.9k | case BROTLI_STATE_HUFFMAN_NONE: |
812 | 61.9k | if (!BrotliSafeReadBits(br, 2, &h->sub_loop_counter)) { |
813 | 4.08k | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
814 | 4.08k | } |
815 | 57.8k | 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 | 57.8k | if (h->sub_loop_counter != 1) { |
820 | 10.7k | h->space = 32; |
821 | 10.7k | h->repeat = 0; /* num_codes */ |
822 | 10.7k | memset(&h->code_length_histo[0], 0, sizeof(h->code_length_histo[0]) * |
823 | 10.7k | (BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1)); |
824 | 10.7k | memset(&h->code_length_code_lengths[0], 0, |
825 | 10.7k | sizeof(h->code_length_code_lengths)); |
826 | 10.7k | h->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX; |
827 | 10.7k | continue; |
828 | 10.7k | } |
829 | | /* Fall through. */ |
830 | | |
831 | 53.8k | case BROTLI_STATE_HUFFMAN_SIMPLE_SIZE: |
832 | | /* Read symbols, codes & code lengths directly. */ |
833 | 53.8k | if (!BrotliSafeReadBits(br, 2, &h->symbol)) { /* num_symbols */ |
834 | 6.80k | h->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_SIZE; |
835 | 6.80k | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
836 | 6.80k | } |
837 | 47.0k | h->sub_loop_counter = 0; |
838 | | /* Fall through. */ |
839 | | |
840 | 72.3k | case BROTLI_STATE_HUFFMAN_SIMPLE_READ: { |
841 | 72.3k | BrotliDecoderErrorCode result = |
842 | 72.3k | ReadSimpleHuffmanSymbols(alphabet_size_max, alphabet_size_limit, s); |
843 | 72.3k | if (result != BROTLI_DECODER_SUCCESS) { |
844 | 25.3k | return result; |
845 | 25.3k | } |
846 | 72.3k | } |
847 | | /* Fall through. */ |
848 | | |
849 | 47.4k | case BROTLI_STATE_HUFFMAN_SIMPLE_BUILD: { |
850 | 47.4k | brotli_reg_t table_size; |
851 | 47.4k | if (h->symbol == 3) { |
852 | 2.94k | brotli_reg_t bits; |
853 | 2.94k | if (!BrotliSafeReadBits(br, 1, &bits)) { |
854 | 525 | h->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_BUILD; |
855 | 525 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
856 | 525 | } |
857 | 2.42k | h->symbol += bits; |
858 | 2.42k | } |
859 | 46.9k | BROTLI_LOG_UINT(h->symbol); |
860 | 46.9k | table_size = BrotliBuildSimpleHuffmanTable(table, HUFFMAN_TABLE_BITS, |
861 | 46.9k | h->symbols_lists_array, |
862 | 46.9k | (uint32_t)h->symbol); |
863 | 46.9k | if (opt_table_size) { |
864 | 38.6k | *opt_table_size = table_size; |
865 | 38.6k | } |
866 | 46.9k | h->substate_huffman = BROTLI_STATE_HUFFMAN_NONE; |
867 | 46.9k | return BROTLI_DECODER_SUCCESS; |
868 | 47.4k | } |
869 | | |
870 | | /* Decode Huffman-coded code lengths. */ |
871 | 17.3k | case BROTLI_STATE_HUFFMAN_COMPLEX: { |
872 | 17.3k | brotli_reg_t i; |
873 | 17.3k | BrotliDecoderErrorCode result = ReadCodeLengthCodeLengths(s); |
874 | 17.3k | if (result != BROTLI_DECODER_SUCCESS) { |
875 | 7.04k | return result; |
876 | 7.04k | } |
877 | 10.3k | BrotliBuildCodeLengthsHuffmanTable(h->table, |
878 | 10.3k | h->code_length_code_lengths, |
879 | 10.3k | h->code_length_histo); |
880 | 10.3k | memset(&h->code_length_histo[0], 0, sizeof(h->code_length_histo)); |
881 | 175k | for (i = 0; i <= BROTLI_HUFFMAN_MAX_CODE_LENGTH; ++i) { |
882 | 165k | h->next_symbol[i] = (int)i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1); |
883 | 165k | h->symbol_lists[h->next_symbol[i]] = 0xFFFF; |
884 | 165k | } |
885 | | |
886 | 10.3k | h->symbol = 0; |
887 | 10.3k | h->prev_code_len = BROTLI_INITIAL_REPEATED_CODE_LENGTH; |
888 | 10.3k | h->repeat = 0; |
889 | 10.3k | h->repeat_code_len = 0; |
890 | 10.3k | h->space = 32768; |
891 | 10.3k | h->substate_huffman = BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS; |
892 | 10.3k | } |
893 | | /* Fall through. */ |
894 | | |
895 | 17.8k | case BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS: { |
896 | 17.8k | brotli_reg_t table_size; |
897 | 17.8k | BrotliDecoderErrorCode result = ReadSymbolCodeLengths( |
898 | 17.8k | alphabet_size_limit, s); |
899 | 17.8k | if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) { |
900 | 13.6k | result = SafeReadSymbolCodeLengths(alphabet_size_limit, s); |
901 | 13.6k | } |
902 | 17.8k | if (result != BROTLI_DECODER_SUCCESS) { |
903 | 7.75k | return result; |
904 | 7.75k | } |
905 | | |
906 | 10.0k | if (h->space != 0) { |
907 | 202 | BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", (int)h->space)); |
908 | 202 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE); |
909 | 202 | } |
910 | 9.89k | table_size = BrotliBuildHuffmanTable( |
911 | 9.89k | table, HUFFMAN_TABLE_BITS, h->symbol_lists, h->code_length_histo); |
912 | 9.89k | if (opt_table_size) { |
913 | 8.49k | *opt_table_size = table_size; |
914 | 8.49k | } |
915 | 9.89k | h->substate_huffman = BROTLI_STATE_HUFFMAN_NONE; |
916 | 9.89k | return BROTLI_DECODER_SUCCESS; |
917 | 10.0k | } |
918 | | |
919 | 0 | default: |
920 | 0 | return |
921 | 0 | BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); /* COV_NF_LINE */ |
922 | 119k | } |
923 | 119k | } |
924 | 108k | } |
925 | | |
926 | | /* Decodes a block length by reading 3..39 bits. */ |
927 | | static BROTLI_INLINE brotli_reg_t ReadBlockLength(const HuffmanCode* table, |
928 | 263k | BrotliBitReader* br) { |
929 | 263k | brotli_reg_t code; |
930 | 263k | brotli_reg_t nbits; |
931 | 263k | code = ReadSymbol(table, br); |
932 | 263k | nbits = _kBrotliPrefixCodeRanges[code].nbits; /* nbits == 2..24 */ |
933 | 263k | return _kBrotliPrefixCodeRanges[code].offset + BrotliReadBits24(br, nbits); |
934 | 263k | } |
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 | 185k | BrotliBitReader* br) { |
941 | 185k | brotli_reg_t index; |
942 | 185k | if (s->substate_read_block_length == BROTLI_STATE_READ_BLOCK_LENGTH_NONE) { |
943 | 182k | if (!SafeReadSymbol(table, br, &index)) { |
944 | 19.8k | return BROTLI_FALSE; |
945 | 19.8k | } |
946 | 182k | } else { |
947 | 2.26k | index = s->block_length_index; |
948 | 2.26k | } |
949 | 165k | { |
950 | 165k | brotli_reg_t bits; |
951 | 165k | brotli_reg_t nbits = _kBrotliPrefixCodeRanges[index].nbits; |
952 | 165k | brotli_reg_t offset = _kBrotliPrefixCodeRanges[index].offset; |
953 | 165k | if (!BrotliSafeReadBits(br, nbits, &bits)) { |
954 | 33.6k | s->block_length_index = index; |
955 | 33.6k | s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX; |
956 | 33.6k | return BROTLI_FALSE; |
957 | 33.6k | } |
958 | 131k | *result = offset + bits; |
959 | 131k | s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE; |
960 | 131k | return BROTLI_TRUE; |
961 | 165k | } |
962 | 165k | } |
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 | 1.07k | uint8_t* v, brotli_reg_t v_len, BrotliDecoderState* state) { |
980 | | /* Reinitialize elements that could have been changed. */ |
981 | 1.07k | brotli_reg_t i = 1; |
982 | 1.07k | brotli_reg_t upper_bound = state->mtf_upper_bound; |
983 | 1.07k | uint32_t* mtf = &state->mtf[1]; /* Make mtf[-1] addressable. */ |
984 | 1.07k | uint8_t* mtf_u8 = (uint8_t*)mtf; |
985 | | /* Load endian-aware constant. */ |
986 | 1.07k | const uint8_t b0123[4] = {0, 1, 2, 3}; |
987 | 1.07k | uint32_t pattern; |
988 | 1.07k | memcpy(&pattern, &b0123, 4); |
989 | | |
990 | | /* Initialize list using 4 consequent values pattern. */ |
991 | 1.07k | mtf[0] = pattern; |
992 | 25.4k | do { |
993 | 25.4k | pattern += 0x04040404; /* Advance all 4 values by 4. */ |
994 | 25.4k | mtf[i] = pattern; |
995 | 25.4k | i++; |
996 | 25.4k | } while (i <= upper_bound); |
997 | | |
998 | | /* Transform the input. */ |
999 | 1.07k | upper_bound = 0; |
1000 | 107k | for (i = 0; i < v_len; ++i) { |
1001 | 106k | int index = v[i]; |
1002 | 106k | uint8_t value = mtf_u8[index]; |
1003 | 106k | upper_bound |= v[i]; |
1004 | 106k | v[i] = value; |
1005 | 106k | mtf_u8[-1] = value; |
1006 | 929k | do { |
1007 | 929k | index--; |
1008 | 929k | mtf_u8[index + 1] = mtf_u8[index]; |
1009 | 929k | } while (index >= 0); |
1010 | 106k | } |
1011 | | /* Remember amount of elements to be reinitialized. */ |
1012 | 1.07k | state->mtf_upper_bound = upper_bound >> 2; |
1013 | 1.07k | } |
1014 | | |
1015 | | /* Decodes a series of Huffman table using ReadHuffmanCode function. */ |
1016 | | static BrotliDecoderErrorCode HuffmanTreeGroupDecode( |
1017 | 77.5k | HuffmanTreeGroup* group, BrotliDecoderState* s) { |
1018 | 77.5k | BrotliMetablockHeaderArena* h = &s->arena.header; |
1019 | 77.5k | if (h->substate_tree_group != BROTLI_STATE_TREE_GROUP_LOOP) { |
1020 | 35.3k | h->next = group->codes; |
1021 | 35.3k | h->htree_index = 0; |
1022 | 35.3k | h->substate_tree_group = BROTLI_STATE_TREE_GROUP_LOOP; |
1023 | 35.3k | } |
1024 | 124k | while (h->htree_index < group->num_htrees) { |
1025 | 90.0k | brotli_reg_t table_size; |
1026 | 90.0k | BrotliDecoderErrorCode result = ReadHuffmanCode(group->alphabet_size_max, |
1027 | 90.0k | group->alphabet_size_limit, h->next, &table_size, s); |
1028 | 90.0k | if (result != BROTLI_DECODER_SUCCESS) return result; |
1029 | 47.1k | group->htrees[h->htree_index] = h->next; |
1030 | 47.1k | h->next += table_size; |
1031 | 47.1k | ++h->htree_index; |
1032 | 47.1k | } |
1033 | 34.6k | h->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE; |
1034 | 34.6k | return BROTLI_DECODER_SUCCESS; |
1035 | 77.5k | } |
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 | 32.5k | BrotliDecoderState* s) { |
1049 | 32.5k | BrotliBitReader* br = &s->br; |
1050 | 32.5k | BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS; |
1051 | 32.5k | BrotliMetablockHeaderArena* h = &s->arena.header; |
1052 | | |
1053 | 32.5k | switch ((int)h->substate_context_map) { |
1054 | 28.0k | case BROTLI_STATE_CONTEXT_MAP_NONE: |
1055 | 28.0k | result = DecodeVarLenUint8(s, br, num_htrees); |
1056 | 28.0k | if (result != BROTLI_DECODER_SUCCESS) { |
1057 | 3.57k | return result; |
1058 | 3.57k | } |
1059 | 24.5k | (*num_htrees)++; |
1060 | 24.5k | h->context_index = 0; |
1061 | 24.5k | BROTLI_LOG_UINT(context_map_size); |
1062 | 24.5k | BROTLI_LOG_UINT(*num_htrees); |
1063 | 24.5k | *context_map_arg = |
1064 | 24.5k | (uint8_t*)BROTLI_DECODER_ALLOC(s, (size_t)context_map_size); |
1065 | 24.5k | if (*context_map_arg == 0) { |
1066 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP); |
1067 | 0 | } |
1068 | 24.5k | if (*num_htrees <= 1) { |
1069 | 22.5k | memset(*context_map_arg, 0, (size_t)context_map_size); |
1070 | 22.5k | return BROTLI_DECODER_SUCCESS; |
1071 | 22.5k | } |
1072 | 1.98k | h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_READ_PREFIX; |
1073 | | /* Fall through. */ |
1074 | | |
1075 | 2.74k | case BROTLI_STATE_CONTEXT_MAP_READ_PREFIX: { |
1076 | 2.74k | 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 | 2.74k | if (!BrotliSafeGetBits(br, 5, &bits)) { |
1080 | 806 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
1081 | 806 | } |
1082 | 1.93k | if ((bits & 1) != 0) { /* Use RLE for zeros. */ |
1083 | 557 | h->max_run_length_prefix = (bits >> 1) + 1; |
1084 | 557 | BrotliDropBits(br, 5); |
1085 | 1.38k | } else { |
1086 | 1.38k | h->max_run_length_prefix = 0; |
1087 | 1.38k | BrotliDropBits(br, 1); |
1088 | 1.38k | } |
1089 | 1.93k | BROTLI_LOG_UINT(h->max_run_length_prefix); |
1090 | 1.93k | h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_HUFFMAN; |
1091 | 1.93k | } |
1092 | | /* Fall through. */ |
1093 | | |
1094 | 3.00k | case BROTLI_STATE_CONTEXT_MAP_HUFFMAN: { |
1095 | 3.00k | brotli_reg_t alphabet_size = *num_htrees + h->max_run_length_prefix; |
1096 | 3.00k | result = ReadHuffmanCode(alphabet_size, alphabet_size, |
1097 | 3.00k | h->context_map_table, NULL, s); |
1098 | 3.00k | if (result != BROTLI_DECODER_SUCCESS) return result; |
1099 | 1.83k | h->code = 0xFFFF; |
1100 | 1.83k | h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_DECODE; |
1101 | 1.83k | } |
1102 | | /* Fall through. */ |
1103 | | |
1104 | 3.95k | case BROTLI_STATE_CONTEXT_MAP_DECODE: { |
1105 | 3.95k | brotli_reg_t context_index = h->context_index; |
1106 | 3.95k | brotli_reg_t max_run_length_prefix = h->max_run_length_prefix; |
1107 | 3.95k | uint8_t* context_map = *context_map_arg; |
1108 | 3.95k | brotli_reg_t code = h->code; |
1109 | 3.95k | BROTLI_BOOL skip_preamble = (code != 0xFFFF); |
1110 | 172k | while (context_index < context_map_size || skip_preamble) { |
1111 | 170k | if (!skip_preamble) { |
1112 | 169k | if (!SafeReadSymbol(h->context_map_table, br, &code)) { |
1113 | 1.68k | h->code = 0xFFFF; |
1114 | 1.68k | h->context_index = context_index; |
1115 | 1.68k | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
1116 | 1.68k | } |
1117 | 168k | BROTLI_LOG_UINT(code); |
1118 | | |
1119 | 168k | if (code == 0) { |
1120 | 31.8k | context_map[context_index++] = 0; |
1121 | 31.8k | continue; |
1122 | 31.8k | } |
1123 | 136k | if (code > max_run_length_prefix) { |
1124 | 133k | context_map[context_index++] = |
1125 | 133k | (uint8_t)(code - max_run_length_prefix); |
1126 | 133k | continue; |
1127 | 133k | } |
1128 | 136k | } else { |
1129 | 478 | skip_preamble = BROTLI_FALSE; |
1130 | 478 | } |
1131 | | /* RLE sub-stage. */ |
1132 | 2.79k | { |
1133 | 2.79k | brotli_reg_t reps; |
1134 | 2.79k | if (!BrotliSafeReadBits(br, code, &reps)) { |
1135 | 502 | h->code = code; |
1136 | 502 | h->context_index = context_index; |
1137 | 502 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
1138 | 502 | } |
1139 | 2.29k | reps += (brotli_reg_t)1U << code; |
1140 | 2.29k | BROTLI_LOG_UINT(reps); |
1141 | 2.29k | if (context_index + reps > context_map_size) { |
1142 | 33 | return |
1143 | 33 | BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT); |
1144 | 33 | } |
1145 | 30.2k | do { |
1146 | 30.2k | context_map[context_index++] = 0; |
1147 | 30.2k | } while (--reps); |
1148 | 2.25k | } |
1149 | 2.25k | } |
1150 | 3.95k | } |
1151 | | /* Fall through. */ |
1152 | | |
1153 | 2.23k | case BROTLI_STATE_CONTEXT_MAP_TRANSFORM: { |
1154 | 2.23k | brotli_reg_t bits; |
1155 | 2.23k | if (!BrotliSafeReadBits(br, 1, &bits)) { |
1156 | 508 | h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_TRANSFORM; |
1157 | 508 | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
1158 | 508 | } |
1159 | 1.72k | if (bits != 0) { |
1160 | 1.07k | InverseMoveToFrontTransform(*context_map_arg, context_map_size, s); |
1161 | 1.07k | } |
1162 | 1.72k | h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE; |
1163 | 1.72k | return BROTLI_DECODER_SUCCESS; |
1164 | 2.23k | } |
1165 | | |
1166 | 0 | default: |
1167 | 0 | return |
1168 | 0 | BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); /* COV_NF_LINE */ |
1169 | 32.5k | } |
1170 | 32.5k | } |
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 | 457k | int safe, BrotliDecoderState* s, int tree_type) { |
1176 | 457k | brotli_reg_t max_block_type = s->num_block_types[tree_type]; |
1177 | 457k | const HuffmanCode* type_tree = &s->block_type_trees[ |
1178 | 457k | tree_type * BROTLI_HUFFMAN_MAX_SIZE_258]; |
1179 | 457k | const HuffmanCode* len_tree = &s->block_len_trees[ |
1180 | 457k | tree_type * BROTLI_HUFFMAN_MAX_SIZE_26]; |
1181 | 457k | BrotliBitReader* br = &s->br; |
1182 | 457k | brotli_reg_t* ringbuffer = &s->block_type_rb[tree_type * 2]; |
1183 | 457k | brotli_reg_t block_type; |
1184 | 457k | if (max_block_type <= 1) { |
1185 | 1 | return BROTLI_FALSE; |
1186 | 1 | } |
1187 | | |
1188 | | /* Read 0..15 + 3..39 bits. */ |
1189 | 457k | if (!safe) { |
1190 | 263k | block_type = ReadSymbol(type_tree, br); |
1191 | 263k | s->block_length[tree_type] = ReadBlockLength(len_tree, br); |
1192 | 263k | } else { |
1193 | 194k | BrotliBitReaderState memento; |
1194 | 194k | BrotliBitReaderSaveState(br, &memento); |
1195 | 194k | if (!SafeReadSymbol(type_tree, br, &block_type)) return BROTLI_FALSE; |
1196 | 178k | if (!SafeReadBlockLength(s, &s->block_length[tree_type], len_tree, br)) { |
1197 | 50.3k | s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE; |
1198 | 50.3k | BrotliBitReaderRestoreState(br, &memento); |
1199 | 50.3k | return BROTLI_FALSE; |
1200 | 50.3k | } |
1201 | 178k | } |
1202 | | |
1203 | 390k | if (block_type == 1) { |
1204 | 176k | block_type = ringbuffer[1] + 1; |
1205 | 214k | } else if (block_type == 0) { |
1206 | 111k | block_type = ringbuffer[0]; |
1207 | 111k | } else { |
1208 | 103k | block_type -= 2; |
1209 | 103k | } |
1210 | 390k | if (block_type >= max_block_type) { |
1211 | 35.9k | block_type -= max_block_type; |
1212 | 35.9k | } |
1213 | 390k | ringbuffer[0] = ringbuffer[1]; |
1214 | 390k | ringbuffer[1] = block_type; |
1215 | 390k | return BROTLI_TRUE; |
1216 | 457k | } |
1217 | | |
1218 | | static BROTLI_INLINE void DetectTrivialLiteralBlockTypes( |
1219 | 12.2k | BrotliDecoderState* s) { |
1220 | 12.2k | size_t i; |
1221 | 110k | for (i = 0; i < 8; ++i) s->trivial_literal_contexts[i] = 0; |
1222 | 38.9k | for (i = 0; i < s->num_block_types[0]; i++) { |
1223 | 26.7k | size_t offset = i << BROTLI_LITERAL_CONTEXT_BITS; |
1224 | 26.7k | size_t error = 0; |
1225 | 26.7k | size_t sample = s->context_map[offset]; |
1226 | 26.7k | size_t j; |
1227 | 454k | for (j = 0; j < (1u << BROTLI_LITERAL_CONTEXT_BITS);) { |
1228 | | /* NOLINTNEXTLINE(bugprone-macro-repeated-side-effects) */ |
1229 | 427k | BROTLI_REPEAT_4({ error |= s->context_map[offset + j++] ^ sample; }) |
1230 | 427k | } |
1231 | 26.7k | if (error == 0) { |
1232 | 24.9k | s->trivial_literal_contexts[i >> 5] |= 1u << (i & 31); |
1233 | 24.9k | } |
1234 | 26.7k | } |
1235 | 12.2k | } |
1236 | | |
1237 | 207k | static BROTLI_INLINE void PrepareLiteralDecoding(BrotliDecoderState* s) { |
1238 | 207k | uint8_t context_mode; |
1239 | 207k | size_t trivial; |
1240 | 207k | brotli_reg_t block_type = s->block_type_rb[1]; |
1241 | 207k | brotli_reg_t context_offset = block_type << BROTLI_LITERAL_CONTEXT_BITS; |
1242 | 207k | s->context_map_slice = s->context_map + context_offset; |
1243 | 207k | trivial = s->trivial_literal_contexts[block_type >> 5]; |
1244 | 207k | s->trivial_literal_context = (trivial >> (block_type & 31)) & 1; |
1245 | 207k | s->literal_htree = s->literal_hgroup.htrees[s->context_map_slice[0]]; |
1246 | 207k | context_mode = s->context_modes[block_type] & 3; |
1247 | 207k | s->context_lookup = BROTLI_CONTEXT_LUT(context_mode); |
1248 | 207k | } |
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 | 231k | int safe, BrotliDecoderState* s) { |
1254 | 231k | if (!DecodeBlockTypeAndLength(safe, s, 0)) { |
1255 | 36.0k | return BROTLI_FALSE; |
1256 | 36.0k | } |
1257 | 195k | PrepareLiteralDecoding(s); |
1258 | 195k | return BROTLI_TRUE; |
1259 | 231k | } |
1260 | | |
1261 | 137k | static void BROTLI_NOINLINE DecodeLiteralBlockSwitch(BrotliDecoderState* s) { |
1262 | 137k | DecodeLiteralBlockSwitchInternal(0, s); |
1263 | 137k | } |
1264 | | |
1265 | | static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeLiteralBlockSwitch( |
1266 | 94.0k | BrotliDecoderState* s) { |
1267 | 94.0k | return DecodeLiteralBlockSwitchInternal(1, s); |
1268 | 94.0k | } |
1269 | | |
1270 | | /* Block switch for insert/copy length. |
1271 | | Reads 3..54 bits. */ |
1272 | | static BROTLI_INLINE BROTLI_BOOL DecodeCommandBlockSwitchInternal( |
1273 | 101k | int safe, BrotliDecoderState* s) { |
1274 | 101k | if (!DecodeBlockTypeAndLength(safe, s, 1)) { |
1275 | 24.1k | return BROTLI_FALSE; |
1276 | 24.1k | } |
1277 | 77.5k | s->htree_command = s->insert_copy_hgroup.htrees[s->block_type_rb[3]]; |
1278 | 77.5k | return BROTLI_TRUE; |
1279 | 101k | } |
1280 | | |
1281 | 21.2k | static void BROTLI_NOINLINE DecodeCommandBlockSwitch(BrotliDecoderState* s) { |
1282 | 21.2k | DecodeCommandBlockSwitchInternal(0, s); |
1283 | 21.2k | } |
1284 | | |
1285 | | static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeCommandBlockSwitch( |
1286 | 80.4k | BrotliDecoderState* s) { |
1287 | 80.4k | return DecodeCommandBlockSwitchInternal(1, s); |
1288 | 80.4k | } |
1289 | | |
1290 | | /* Block switch for distance codes. |
1291 | | Reads 3..54 bits. */ |
1292 | | static BROTLI_INLINE BROTLI_BOOL DecodeDistanceBlockSwitchInternal( |
1293 | 124k | int safe, BrotliDecoderState* s) { |
1294 | 124k | if (!DecodeBlockTypeAndLength(safe, s, 2)) { |
1295 | 6.61k | return BROTLI_FALSE; |
1296 | 6.61k | } |
1297 | 117k | s->dist_context_map_slice = s->dist_context_map + |
1298 | 117k | (s->block_type_rb[5] << BROTLI_DISTANCE_CONTEXT_BITS); |
1299 | 117k | s->dist_htree_index = s->dist_context_map_slice[s->distance_context]; |
1300 | 117k | return BROTLI_TRUE; |
1301 | 124k | } |
1302 | | |
1303 | 103k | static void BROTLI_NOINLINE DecodeDistanceBlockSwitch(BrotliDecoderState* s) { |
1304 | 103k | DecodeDistanceBlockSwitchInternal(0, s); |
1305 | 103k | } |
1306 | | |
1307 | | static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeDistanceBlockSwitch( |
1308 | 20.1k | BrotliDecoderState* s) { |
1309 | 20.1k | return DecodeDistanceBlockSwitchInternal(1, s); |
1310 | 20.1k | } |
1311 | | |
1312 | 6.38M | static size_t UnwrittenBytes(const BrotliDecoderState* s, BROTLI_BOOL wrap) { |
1313 | 6.38M | size_t pos = wrap && s->pos > s->ringbuffer_size ? |
1314 | 6.12M | (size_t)s->ringbuffer_size : (size_t)(s->pos); |
1315 | 6.38M | size_t partial_pos_rb = (s->rb_roundtrips * (size_t)s->ringbuffer_size) + pos; |
1316 | 6.38M | return partial_pos_rb - s->partial_pos_out; |
1317 | 6.38M | } |
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 | 6.38M | size_t* total_out, BROTLI_BOOL force) { |
1325 | 6.38M | uint8_t* start = |
1326 | 6.38M | s->ringbuffer + (s->partial_pos_out & (size_t)s->ringbuffer_mask); |
1327 | 6.38M | size_t to_write = UnwrittenBytes(s, BROTLI_TRUE); |
1328 | 6.38M | size_t num_written = *available_out; |
1329 | 6.38M | if (num_written > to_write) { |
1330 | 2.99M | num_written = to_write; |
1331 | 2.99M | } |
1332 | 6.38M | if (s->meta_block_remaining_len < 0) { |
1333 | 207 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1); |
1334 | 207 | } |
1335 | 6.38M | if (next_out && !*next_out) { |
1336 | 0 | *next_out = start; |
1337 | 6.38M | } else { |
1338 | 6.38M | if (next_out) { |
1339 | 6.38M | memcpy(*next_out, start, num_written); |
1340 | 6.38M | *next_out += num_written; |
1341 | 6.38M | } |
1342 | 6.38M | } |
1343 | 6.38M | *available_out -= num_written; |
1344 | 6.38M | BROTLI_LOG_UINT(to_write); |
1345 | 6.38M | BROTLI_LOG_UINT(num_written); |
1346 | 6.38M | s->partial_pos_out += num_written; |
1347 | 6.38M | if (total_out) { |
1348 | 6.38M | *total_out = s->partial_pos_out; |
1349 | 6.38M | } |
1350 | 6.38M | if (num_written < to_write) { |
1351 | 3.16M | if (s->ringbuffer_size == (1 << s->window_bits) || force) { |
1352 | 3.16M | return BROTLI_DECODER_NEEDS_MORE_OUTPUT; |
1353 | 3.16M | } else { |
1354 | 51 | return BROTLI_DECODER_SUCCESS; |
1355 | 51 | } |
1356 | 3.16M | } |
1357 | | /* Wrap ring buffer only if it has reached its maximal size. */ |
1358 | 3.22M | if (s->ringbuffer_size == (1 << s->window_bits) && |
1359 | 3.22M | s->pos >= s->ringbuffer_size) { |
1360 | 357k | s->pos -= s->ringbuffer_size; |
1361 | 357k | s->rb_roundtrips++; |
1362 | 357k | s->should_wrap_ringbuffer = (size_t)s->pos != 0 ? 1 : 0; |
1363 | 357k | } |
1364 | 3.22M | return BROTLI_DECODER_SUCCESS; |
1365 | 6.38M | } |
1366 | | |
1367 | 356k | static void BROTLI_NOINLINE WrapRingBuffer(BrotliDecoderState* s) { |
1368 | 356k | if (s->should_wrap_ringbuffer) { |
1369 | 29.5k | memcpy(s->ringbuffer, s->ringbuffer_end, (size_t)s->pos); |
1370 | 29.5k | s->should_wrap_ringbuffer = 0; |
1371 | 29.5k | } |
1372 | 356k | } |
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 | 122k | BrotliDecoderState* s) { |
1383 | 122k | uint8_t* old_ringbuffer = s->ringbuffer; |
1384 | 122k | if (s->ringbuffer_size == s->new_ringbuffer_size) { |
1385 | 119k | return BROTLI_TRUE; |
1386 | 119k | } |
1387 | | |
1388 | 3.32k | s->ringbuffer = (uint8_t*)BROTLI_DECODER_ALLOC(s, |
1389 | 3.32k | (size_t)(s->new_ringbuffer_size) + kRingBufferWriteAheadSlack); |
1390 | 3.32k | if (s->ringbuffer == 0) { |
1391 | | /* Restore previous value. */ |
1392 | 0 | s->ringbuffer = old_ringbuffer; |
1393 | 0 | return BROTLI_FALSE; |
1394 | 0 | } |
1395 | 3.32k | s->ringbuffer[s->new_ringbuffer_size - 2] = 0; |
1396 | 3.32k | s->ringbuffer[s->new_ringbuffer_size - 1] = 0; |
1397 | | |
1398 | 3.32k | if (!!old_ringbuffer) { |
1399 | 258 | memcpy(s->ringbuffer, old_ringbuffer, (size_t)s->pos); |
1400 | 258 | BROTLI_DECODER_FREE(s, old_ringbuffer); |
1401 | 258 | } |
1402 | | |
1403 | 3.32k | s->ringbuffer_size = s->new_ringbuffer_size; |
1404 | 3.32k | s->ringbuffer_mask = s->new_ringbuffer_size - 1; |
1405 | 3.32k | s->ringbuffer_end = s->ringbuffer + s->ringbuffer_size; |
1406 | | |
1407 | 3.32k | return BROTLI_TRUE; |
1408 | 3.32k | } |
1409 | | |
1410 | | static BrotliDecoderErrorCode BROTLI_NOINLINE |
1411 | 7.32k | SkipMetadataBlock(BrotliDecoderState* s) { |
1412 | 7.32k | BrotliBitReader* br = &s->br; |
1413 | 7.32k | int nbytes; |
1414 | | |
1415 | 7.32k | if (s->meta_block_remaining_len == 0) { |
1416 | 2.79k | return BROTLI_DECODER_SUCCESS; |
1417 | 2.79k | } |
1418 | | |
1419 | 4.53k | BROTLI_DCHECK((BrotliGetAvailableBits(br) & 7) == 0); |
1420 | | |
1421 | | /* Drain accumulator. */ |
1422 | 4.53k | if (BrotliGetAvailableBits(br) >= 8) { |
1423 | 588 | uint8_t buffer[8]; |
1424 | 588 | nbytes = (int)(BrotliGetAvailableBits(br)) >> 3; |
1425 | 588 | BROTLI_DCHECK(nbytes <= 8); |
1426 | 588 | if (nbytes > s->meta_block_remaining_len) { |
1427 | 292 | nbytes = s->meta_block_remaining_len; |
1428 | 292 | } |
1429 | 588 | BrotliCopyBytes(buffer, br, (size_t)nbytes); |
1430 | 588 | if (s->metadata_chunk_func) { |
1431 | 0 | s->metadata_chunk_func(s->metadata_callback_opaque, buffer, |
1432 | 0 | (size_t)nbytes); |
1433 | 0 | } |
1434 | 588 | s->meta_block_remaining_len -= nbytes; |
1435 | 588 | if (s->meta_block_remaining_len == 0) { |
1436 | 323 | return BROTLI_DECODER_SUCCESS; |
1437 | 323 | } |
1438 | 588 | } |
1439 | | |
1440 | | /* Direct access to metadata is possible. */ |
1441 | 4.20k | nbytes = (int)BrotliGetRemainingBytes(br); |
1442 | 4.20k | if (nbytes > s->meta_block_remaining_len) { |
1443 | 734 | nbytes = s->meta_block_remaining_len; |
1444 | 734 | } |
1445 | 4.20k | if (nbytes > 0) { |
1446 | 3.90k | if (s->metadata_chunk_func) { |
1447 | 0 | s->metadata_chunk_func(s->metadata_callback_opaque, br->next_in, |
1448 | 0 | (size_t)nbytes); |
1449 | 0 | } |
1450 | 3.90k | BrotliDropBytes(br, (size_t)nbytes); |
1451 | 3.90k | s->meta_block_remaining_len -= nbytes; |
1452 | 3.90k | if (s->meta_block_remaining_len == 0) { |
1453 | 994 | return BROTLI_DECODER_SUCCESS; |
1454 | 994 | } |
1455 | 3.90k | } |
1456 | | |
1457 | 3.21k | BROTLI_DCHECK(BrotliGetRemainingBytes(br) == 0); |
1458 | | |
1459 | 3.21k | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
1460 | 4.20k | } |
1461 | | |
1462 | | static BrotliDecoderErrorCode BROTLI_NOINLINE CopyUncompressedBlockToOutput( |
1463 | | size_t* available_out, uint8_t** next_out, size_t* total_out, |
1464 | 111k | BrotliDecoderState* s) { |
1465 | | /* TODO(eustas): avoid allocation for single uncompressed block. */ |
1466 | 111k | if (!BrotliEnsureRingBuffer(s)) { |
1467 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1); |
1468 | 0 | } |
1469 | | |
1470 | | /* State machine */ |
1471 | 112k | for (;;) { |
1472 | 112k | switch (s->substate_uncompressed) { |
1473 | 76.4k | case BROTLI_STATE_UNCOMPRESSED_NONE: { |
1474 | 76.4k | int nbytes = (int)BrotliGetRemainingBytes(&s->br); |
1475 | 76.4k | if (nbytes > s->meta_block_remaining_len) { |
1476 | 1.63k | nbytes = s->meta_block_remaining_len; |
1477 | 1.63k | } |
1478 | 76.4k | if (s->pos + nbytes > s->ringbuffer_size) { |
1479 | 1.14k | nbytes = s->ringbuffer_size - s->pos; |
1480 | 1.14k | } |
1481 | | /* Copy remaining bytes from s->br.buf_ to ring-buffer. */ |
1482 | 76.4k | BrotliCopyBytes(&s->ringbuffer[s->pos], &s->br, (size_t)nbytes); |
1483 | 76.4k | s->pos += nbytes; |
1484 | 76.4k | s->meta_block_remaining_len -= nbytes; |
1485 | 76.4k | if (s->pos < 1 << s->window_bits) { |
1486 | 75.0k | if (s->meta_block_remaining_len == 0) { |
1487 | 2.41k | return BROTLI_DECODER_SUCCESS; |
1488 | 2.41k | } |
1489 | 72.6k | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
1490 | 75.0k | } |
1491 | 1.35k | s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_WRITE; |
1492 | 1.35k | } |
1493 | | /* Fall through. */ |
1494 | | |
1495 | 37.4k | case BROTLI_STATE_UNCOMPRESSED_WRITE: { |
1496 | 37.4k | BrotliDecoderErrorCode result; |
1497 | 37.4k | result = WriteRingBuffer( |
1498 | 37.4k | s, available_out, next_out, total_out, BROTLI_FALSE); |
1499 | 37.4k | if (result != BROTLI_DECODER_SUCCESS) { |
1500 | 36.0k | return result; |
1501 | 36.0k | } |
1502 | 1.35k | if (s->ringbuffer_size == 1 << s->window_bits) { |
1503 | 1.35k | s->max_distance = s->max_backward_distance; |
1504 | 1.35k | } |
1505 | 1.35k | s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE; |
1506 | 1.35k | break; |
1507 | 37.4k | } |
1508 | 112k | } |
1509 | 112k | } |
1510 | 0 | BROTLI_DCHECK(0); /* Unreachable */ |
1511 | 0 | } |
1512 | | |
1513 | | static BROTLI_BOOL AttachCompoundDictionary( |
1514 | 0 | BrotliDecoderState* state, const uint8_t* data, size_t size) { |
1515 | 0 | BrotliDecoderCompoundDictionary* addon = state->compound_dictionary; |
1516 | 0 | if (state->state != BROTLI_STATE_UNINITED) return BROTLI_FALSE; |
1517 | 0 | if (!addon) { |
1518 | 0 | addon = (BrotliDecoderCompoundDictionary*)BROTLI_DECODER_ALLOC( |
1519 | 0 | state, sizeof(BrotliDecoderCompoundDictionary)); |
1520 | 0 | if (!addon) return BROTLI_FALSE; |
1521 | 0 | addon->num_chunks = 0; |
1522 | 0 | addon->total_size = 0; |
1523 | 0 | addon->br_length = 0; |
1524 | 0 | addon->br_copied = 0; |
1525 | 0 | addon->block_bits = -1; |
1526 | 0 | addon->chunk_offsets[0] = 0; |
1527 | 0 | state->compound_dictionary = addon; |
1528 | 0 | } |
1529 | 0 | if (addon->num_chunks == 15) return BROTLI_FALSE; |
1530 | 0 | addon->chunks[addon->num_chunks] = data; |
1531 | 0 | addon->num_chunks++; |
1532 | 0 | addon->total_size += (int)size; |
1533 | 0 | addon->chunk_offsets[addon->num_chunks] = addon->total_size; |
1534 | 0 | return BROTLI_TRUE; |
1535 | 0 | } |
1536 | | |
1537 | 0 | static void EnsureCoumpoundDictionaryInitialized(BrotliDecoderState* state) { |
1538 | 0 | BrotliDecoderCompoundDictionary* addon = state->compound_dictionary; |
1539 | | /* 256 = (1 << 8) slots in block map. */ |
1540 | 0 | int block_bits = 8; |
1541 | 0 | int cursor = 0; |
1542 | 0 | int index = 0; |
1543 | 0 | if (addon->block_bits != -1) return; |
1544 | 0 | while (((addon->total_size - 1) >> block_bits) != 0) block_bits++; |
1545 | 0 | block_bits -= 8; |
1546 | 0 | addon->block_bits = block_bits; |
1547 | 0 | while (cursor < addon->total_size) { |
1548 | 0 | while (addon->chunk_offsets[index + 1] < cursor) index++; |
1549 | 0 | addon->block_map[cursor >> block_bits] = (uint8_t)index; |
1550 | 0 | cursor += 1 << block_bits; |
1551 | 0 | } |
1552 | 0 | } |
1553 | | |
1554 | | static BROTLI_BOOL InitializeCompoundDictionaryCopy(BrotliDecoderState* s, |
1555 | 0 | int address, int length) { |
1556 | 0 | BrotliDecoderCompoundDictionary* addon = s->compound_dictionary; |
1557 | 0 | int index; |
1558 | 0 | EnsureCoumpoundDictionaryInitialized(s); |
1559 | 0 | index = addon->block_map[address >> addon->block_bits]; |
1560 | 0 | while (address >= addon->chunk_offsets[index + 1]) index++; |
1561 | 0 | if (addon->total_size < address + length) return BROTLI_FALSE; |
1562 | | /* Update the recent distances cache. */ |
1563 | 0 | s->dist_rb[s->dist_rb_idx & 3] = s->distance_code; |
1564 | 0 | ++s->dist_rb_idx; |
1565 | 0 | s->meta_block_remaining_len -= length; |
1566 | 0 | addon->br_index = index; |
1567 | 0 | addon->br_offset = address - addon->chunk_offsets[index]; |
1568 | 0 | addon->br_length = length; |
1569 | 0 | addon->br_copied = 0; |
1570 | 0 | return BROTLI_TRUE; |
1571 | 0 | } |
1572 | | |
1573 | 6.05M | static int GetCompoundDictionarySize(BrotliDecoderState* s) { |
1574 | 6.05M | return s->compound_dictionary ? s->compound_dictionary->total_size : 0; |
1575 | 6.05M | } |
1576 | | |
1577 | 0 | static int CopyFromCompoundDictionary(BrotliDecoderState* s, int pos) { |
1578 | 0 | BrotliDecoderCompoundDictionary* addon = s->compound_dictionary; |
1579 | 0 | int orig_pos = pos; |
1580 | 0 | while (addon->br_length != addon->br_copied) { |
1581 | 0 | uint8_t* copy_dst = &s->ringbuffer[pos]; |
1582 | 0 | const uint8_t* copy_src = |
1583 | 0 | addon->chunks[addon->br_index] + addon->br_offset; |
1584 | 0 | int space = s->ringbuffer_size - pos; |
1585 | 0 | int rem_chunk_length = (addon->chunk_offsets[addon->br_index + 1] - |
1586 | 0 | addon->chunk_offsets[addon->br_index]) - addon->br_offset; |
1587 | 0 | int length = addon->br_length - addon->br_copied; |
1588 | 0 | if (length > rem_chunk_length) length = rem_chunk_length; |
1589 | 0 | if (length > space) length = space; |
1590 | 0 | memcpy(copy_dst, copy_src, (size_t)length); |
1591 | 0 | pos += length; |
1592 | 0 | addon->br_offset += length; |
1593 | 0 | addon->br_copied += length; |
1594 | 0 | if (length == rem_chunk_length) { |
1595 | 0 | addon->br_index++; |
1596 | 0 | addon->br_offset = 0; |
1597 | 0 | } |
1598 | 0 | if (pos == s->ringbuffer_size) break; |
1599 | 0 | } |
1600 | 0 | return pos - orig_pos; |
1601 | 0 | } |
1602 | | |
1603 | | BROTLI_BOOL BrotliDecoderAttachDictionary( |
1604 | | BrotliDecoderState* state, BrotliSharedDictionaryType type, |
1605 | 0 | size_t data_size, const uint8_t data[BROTLI_ARRAY_PARAM(data_size)]) { |
1606 | 0 | brotli_reg_t i; |
1607 | 0 | brotli_reg_t num_prefix_before = state->dictionary->num_prefix; |
1608 | 0 | if (state->state != BROTLI_STATE_UNINITED) return BROTLI_FALSE; |
1609 | 0 | if (!BrotliSharedDictionaryAttach(state->dictionary, type, data_size, data)) { |
1610 | 0 | return BROTLI_FALSE; |
1611 | 0 | } |
1612 | 0 | for (i = num_prefix_before; i < state->dictionary->num_prefix; i++) { |
1613 | 0 | if (!AttachCompoundDictionary( |
1614 | 0 | state, state->dictionary->prefix[i], |
1615 | 0 | state->dictionary->prefix_size[i])) { |
1616 | 0 | return BROTLI_FALSE; |
1617 | 0 | } |
1618 | 0 | } |
1619 | 0 | return BROTLI_TRUE; |
1620 | 0 | } |
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 | 15.5k | BrotliDecoderState* s) { |
1630 | 15.5k | int window_size = 1 << s->window_bits; |
1631 | 15.5k | 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 | 15.5k | int min_size = s->ringbuffer_size ? s->ringbuffer_size : 1024; |
1635 | 15.5k | int output_size; |
1636 | | |
1637 | | /* If maximum is already reached, no further extension is retired. */ |
1638 | 15.5k | if (s->ringbuffer_size == window_size) { |
1639 | 6.07k | return; |
1640 | 6.07k | } |
1641 | | |
1642 | | /* Metadata blocks does not touch ring buffer. */ |
1643 | 9.49k | if (s->is_metadata) { |
1644 | 0 | return; |
1645 | 0 | } |
1646 | | |
1647 | 9.49k | if (!s->ringbuffer) { |
1648 | 4.37k | output_size = 0; |
1649 | 5.12k | } else { |
1650 | 5.12k | output_size = s->pos; |
1651 | 5.12k | } |
1652 | 9.49k | output_size += s->meta_block_remaining_len; |
1653 | 9.49k | min_size = min_size < output_size ? output_size : min_size; |
1654 | | |
1655 | 9.49k | 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 | 48.1k | while ((new_ringbuffer_size >> 1) >= min_size) { |
1660 | 38.6k | new_ringbuffer_size >>= 1; |
1661 | 38.6k | } |
1662 | 9.49k | } |
1663 | | |
1664 | 9.49k | s->new_ringbuffer_size = new_ringbuffer_size; |
1665 | 9.49k | } |
1666 | | |
1667 | | /* Reads 1..256 2-bit context modes. */ |
1668 | 14.4k | static BrotliDecoderErrorCode ReadContextModes(BrotliDecoderState* s) { |
1669 | 14.4k | BrotliBitReader* br = &s->br; |
1670 | 14.4k | int i = s->loop_counter; |
1671 | | |
1672 | 42.7k | while (i < (int)s->num_block_types[0]) { |
1673 | 30.4k | brotli_reg_t bits; |
1674 | 30.4k | if (!BrotliSafeReadBits(br, 2, &bits)) { |
1675 | 2.03k | s->loop_counter = i; |
1676 | 2.03k | return BROTLI_DECODER_NEEDS_MORE_INPUT; |
1677 | 2.03k | } |
1678 | 28.3k | s->context_modes[i] = (uint8_t)bits; |
1679 | 28.3k | BROTLI_LOG_ARRAY_INDEX(s->context_modes, i); |
1680 | 28.3k | i++; |
1681 | 28.3k | } |
1682 | 12.3k | return BROTLI_DECODER_SUCCESS; |
1683 | 14.4k | } |
1684 | | |
1685 | 132M | static BROTLI_INLINE void TakeDistanceFromRingBuffer(BrotliDecoderState* s) { |
1686 | 132M | int offset = s->distance_code - 3; |
1687 | 132M | if (s->distance_code <= 3) { |
1688 | | /* Compensate double distance-ring-buffer roll for dictionary items. */ |
1689 | 59.5M | s->distance_context = 1 >> s->distance_code; |
1690 | 59.5M | s->distance_code = s->dist_rb[(s->dist_rb_idx - offset) & 3]; |
1691 | 59.5M | s->dist_rb_idx -= s->distance_context; |
1692 | 72.6M | } else { |
1693 | 72.6M | int index_delta = 3; |
1694 | 72.6M | int delta; |
1695 | 72.6M | int base = s->distance_code - 10; |
1696 | 72.6M | if (s->distance_code < 10) { |
1697 | 31.7M | base = s->distance_code - 4; |
1698 | 40.8M | } else { |
1699 | 40.8M | index_delta = 2; |
1700 | 40.8M | } |
1701 | | /* Unpack one of six 4-bit values. */ |
1702 | 72.6M | delta = ((0x605142 >> (4 * base)) & 0xF) - 3; |
1703 | 72.6M | s->distance_code = s->dist_rb[(s->dist_rb_idx + index_delta) & 0x3] + delta; |
1704 | 72.6M | 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 | 28 | s->distance_code = 0x7FFFFFFF; |
1708 | 28 | } |
1709 | 72.6M | } |
1710 | 132M | } |
1711 | | |
1712 | | static BROTLI_INLINE BROTLI_BOOL SafeReadBits( |
1713 | 817M | BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) { |
1714 | 817M | if (n_bits != 0) { |
1715 | 153k | return BrotliSafeReadBits(br, n_bits, val); |
1716 | 817M | } else { |
1717 | 817M | *val = 0; |
1718 | 817M | return BROTLI_TRUE; |
1719 | 817M | } |
1720 | 817M | } |
1721 | | |
1722 | | static BROTLI_INLINE BROTLI_BOOL SafeReadBits32( |
1723 | 84.9M | BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) { |
1724 | 84.9M | if (n_bits != 0) { |
1725 | 17.6k | return BrotliSafeReadBits32(br, n_bits, val); |
1726 | 84.9M | } else { |
1727 | 84.9M | *val = 0; |
1728 | 84.9M | return BROTLI_TRUE; |
1729 | 84.9M | } |
1730 | 84.9M | } |
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 | 11.3k | static void CalculateDistanceLut(BrotliDecoderState* s) { |
1800 | 11.3k | BrotliMetablockBodyArena* b = &s->arena.body; |
1801 | 11.3k | brotli_reg_t npostfix = s->distance_postfix_bits; |
1802 | 11.3k | brotli_reg_t ndirect = s->num_direct_distance_codes; |
1803 | 11.3k | brotli_reg_t alphabet_size_limit = s->distance_hgroup.alphabet_size_limit; |
1804 | 11.3k | brotli_reg_t postfix = (brotli_reg_t)1u << npostfix; |
1805 | 11.3k | brotli_reg_t j; |
1806 | 11.3k | brotli_reg_t bits = 1; |
1807 | 11.3k | brotli_reg_t half = 0; |
1808 | | |
1809 | | /* Skip short codes. */ |
1810 | 11.3k | brotli_reg_t i = BROTLI_NUM_DISTANCE_SHORT_CODES; |
1811 | | |
1812 | | /* Fill direct codes. */ |
1813 | 363k | for (j = 0; j < ndirect; ++j) { |
1814 | 352k | b->dist_extra_bits[i] = 0; |
1815 | 352k | b->dist_offset[i] = j + 1; |
1816 | 352k | ++i; |
1817 | 352k | } |
1818 | | |
1819 | | /* Fill regular distance codes. */ |
1820 | 557k | while (i < alphabet_size_limit) { |
1821 | 545k | brotli_reg_t base = ndirect + ((((2 + half) << bits) - 4) << npostfix) + 1; |
1822 | | /* Always fill the complete group. */ |
1823 | 2.88M | for (j = 0; j < postfix; ++j) { |
1824 | 2.33M | b->dist_extra_bits[i] = (uint8_t)bits; |
1825 | 2.33M | b->dist_offset[i] = base + j; |
1826 | 2.33M | ++i; |
1827 | 2.33M | } |
1828 | 545k | bits = bits + half; |
1829 | 545k | half = half ^ 1; |
1830 | 545k | } |
1831 | 11.3k | } |
1832 | | |
1833 | | /* Precondition: s->distance_code < 0. */ |
1834 | | static BROTLI_INLINE BROTLI_BOOL ReadDistanceInternal( |
1835 | 253M | int safe, BrotliDecoderState* s, BrotliBitReader* br) { |
1836 | 253M | BrotliMetablockBodyArena* b = &s->arena.body; |
1837 | 253M | brotli_reg_t code; |
1838 | 253M | brotli_reg_t bits; |
1839 | 253M | BrotliBitReaderState memento; |
1840 | 253M | HuffmanCode* distance_tree = s->distance_hgroup.htrees[s->dist_htree_index]; |
1841 | 253M | if (!safe) { |
1842 | 57.2M | code = ReadSymbol(distance_tree, br); |
1843 | 196M | } else { |
1844 | 196M | BrotliBitReaderSaveState(br, &memento); |
1845 | 196M | if (!SafeReadSymbol(distance_tree, br, &code)) { |
1846 | 10.1k | return BROTLI_FALSE; |
1847 | 10.1k | } |
1848 | 196M | } |
1849 | 253M | --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 | 253M | s->distance_context = 0; |
1853 | 253M | if ((code & ~0xFu) == 0) { |
1854 | 132M | s->distance_code = (int)code; |
1855 | 132M | TakeDistanceFromRingBuffer(s); |
1856 | 132M | return BROTLI_TRUE; |
1857 | 132M | } |
1858 | 121M | if (!safe) { |
1859 | 36.7M | bits = BrotliReadBits32(br, b->dist_extra_bits[code]); |
1860 | 84.9M | } else { |
1861 | 84.9M | if (!SafeReadBits32(br, b->dist_extra_bits[code], &bits)) { |
1862 | 4.93k | ++s->block_length[2]; |
1863 | 4.93k | BrotliBitReaderRestoreState(br, &memento); |
1864 | 4.93k | return BROTLI_FALSE; |
1865 | 4.93k | } |
1866 | 84.9M | } |
1867 | 121M | s->distance_code = |
1868 | 121M | (int)(b->dist_offset[code] + (bits << s->distance_postfix_bits)); |
1869 | 121M | return BROTLI_TRUE; |
1870 | 121M | } |
1871 | | |
1872 | | static BROTLI_INLINE void ReadDistance( |
1873 | 57.2M | BrotliDecoderState* s, BrotliBitReader* br) { |
1874 | 57.2M | ReadDistanceInternal(0, s, br); |
1875 | 57.2M | } |
1876 | | |
1877 | | static BROTLI_INLINE BROTLI_BOOL SafeReadDistance( |
1878 | 196M | BrotliDecoderState* s, BrotliBitReader* br) { |
1879 | 196M | return ReadDistanceInternal(1, s, br); |
1880 | 196M | } |
1881 | | |
1882 | | static BROTLI_INLINE BROTLI_BOOL ReadCommandInternal( |
1883 | 574M | int safe, BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) { |
1884 | 574M | brotli_reg_t cmd_code; |
1885 | 574M | brotli_reg_t insert_len_extra = 0; |
1886 | 574M | brotli_reg_t copy_length; |
1887 | 574M | CmdLutElement v; |
1888 | 574M | BrotliBitReaderState memento; |
1889 | 574M | if (!safe) { |
1890 | 165M | cmd_code = ReadSymbol(s->htree_command, br); |
1891 | 408M | } else { |
1892 | 408M | BrotliBitReaderSaveState(br, &memento); |
1893 | 408M | if (!SafeReadSymbol(s->htree_command, br, &cmd_code)) { |
1894 | 128k | return BROTLI_FALSE; |
1895 | 128k | } |
1896 | 408M | } |
1897 | 574M | v = kCmdLut[cmd_code]; |
1898 | 574M | s->distance_code = v.distance_code; |
1899 | 574M | s->distance_context = v.context; |
1900 | 574M | s->dist_htree_index = s->dist_context_map_slice[s->distance_context]; |
1901 | 574M | *insert_length = v.insert_len_offset; |
1902 | 574M | if (!safe) { |
1903 | 165M | if (BROTLI_PREDICT_FALSE(v.insert_len_extra_bits != 0)) { |
1904 | 44.7k | insert_len_extra = BrotliReadBits24(br, v.insert_len_extra_bits); |
1905 | 44.7k | } |
1906 | 165M | copy_length = BrotliReadBits24(br, v.copy_len_extra_bits); |
1907 | 408M | } else { |
1908 | 408M | if (!SafeReadBits(br, v.insert_len_extra_bits, &insert_len_extra) || |
1909 | 408M | !SafeReadBits(br, v.copy_len_extra_bits, ©_length)) { |
1910 | 41.9k | BrotliBitReaderRestoreState(br, &memento); |
1911 | 41.9k | return BROTLI_FALSE; |
1912 | 41.9k | } |
1913 | 408M | } |
1914 | 574M | s->copy_length = (int)copy_length + v.copy_len_offset; |
1915 | 574M | --s->block_length[1]; |
1916 | 574M | *insert_length += (int)insert_len_extra; |
1917 | 574M | return BROTLI_TRUE; |
1918 | 574M | } |
1919 | | |
1920 | | static BROTLI_INLINE void ReadCommand( |
1921 | 165M | BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) { |
1922 | 165M | ReadCommandInternal(0, s, br, insert_length); |
1923 | 165M | } |
1924 | | |
1925 | | static BROTLI_INLINE BROTLI_BOOL SafeReadCommand( |
1926 | 408M | BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) { |
1927 | 408M | return ReadCommandInternal(1, s, br, insert_length); |
1928 | 408M | } |
1929 | | |
1930 | | static BROTLI_INLINE BROTLI_BOOL CheckInputAmount( |
1931 | 941M | int safe, BrotliBitReader* const br) { |
1932 | 941M | if (safe) { |
1933 | 638M | return BROTLI_TRUE; |
1934 | 638M | } |
1935 | 302M | return BrotliCheckInputAmount(br); |
1936 | 941M | } |
1937 | | |
1938 | | #define BROTLI_SAFE(METHOD) \ |
1939 | 828M | { \ |
1940 | 828M | if (safe) { \ |
1941 | 605M | if (!Safe##METHOD) { \ |
1942 | 252k | result = BROTLI_DECODER_NEEDS_MORE_INPUT; \ |
1943 | 252k | goto saveStateAndReturn; \ |
1944 | 252k | } \ |
1945 | 605M | } else { \ |
1946 | 222M | METHOD; \ |
1947 | 222M | } \ |
1948 | 828M | } |
1949 | | |
1950 | | static BROTLI_INLINE BrotliDecoderErrorCode ProcessCommandsInternal( |
1951 | 6.05M | int safe, BrotliDecoderState* s) { |
1952 | 6.05M | int pos = s->pos; |
1953 | 6.05M | int i = s->loop_counter; |
1954 | 6.05M | BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS; |
1955 | 6.05M | BrotliBitReader* br = &s->br; |
1956 | 6.05M | int compound_dictionary_size = GetCompoundDictionarySize(s); |
1957 | | |
1958 | 6.05M | if (!CheckInputAmount(safe, br)) { |
1959 | 2.91M | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
1960 | 2.91M | goto saveStateAndReturn; |
1961 | 2.91M | } |
1962 | 3.13M | if (!safe) { |
1963 | 200k | BROTLI_UNUSED(BrotliWarmupBitReader(br)); |
1964 | 200k | } |
1965 | | |
1966 | | /* Jump into state machine. */ |
1967 | 3.13M | if (s->state == BROTLI_STATE_COMMAND_BEGIN) { |
1968 | 255k | goto CommandBegin; |
1969 | 2.88M | } else if (s->state == BROTLI_STATE_COMMAND_INNER) { |
1970 | 2.57M | goto CommandInner; |
1971 | 2.57M | } else if (s->state == BROTLI_STATE_COMMAND_POST_DECODE_LITERALS) { |
1972 | 38.3k | goto CommandPostDecodeLiterals; |
1973 | 270k | } else if (s->state == BROTLI_STATE_COMMAND_POST_WRAP_COPY) { |
1974 | 270k | goto CommandPostWrapCopy; |
1975 | 270k | } else { |
1976 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); /* COV_NF_LINE */ |
1977 | 0 | } |
1978 | | |
1979 | 574M | CommandBegin: |
1980 | 574M | if (safe) { |
1981 | 408M | s->state = BROTLI_STATE_COMMAND_BEGIN; |
1982 | 408M | } |
1983 | 574M | if (!CheckInputAmount(safe, br)) { |
1984 | 14.7k | s->state = BROTLI_STATE_COMMAND_BEGIN; |
1985 | 14.7k | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
1986 | 14.7k | goto saveStateAndReturn; |
1987 | 14.7k | } |
1988 | 574M | if (BROTLI_PREDICT_FALSE(s->block_length[1] == 0)) { |
1989 | 101k | BROTLI_SAFE(DecodeCommandBlockSwitch(s)); |
1990 | 77.5k | goto CommandBegin; |
1991 | 101k | } |
1992 | | /* Read the insert/copy length in the command. */ |
1993 | 574M | BROTLI_SAFE(ReadCommand(s, br, &i)); |
1994 | 574M | BROTLI_LOG(("[ProcessCommandsInternal] pos = %d insert = %d copy = %d\n", |
1995 | 574M | pos, i, s->copy_length)); |
1996 | 574M | if (i == 0) { |
1997 | 198M | goto CommandPostDecodeLiterals; |
1998 | 198M | } |
1999 | 375M | s->meta_block_remaining_len -= i; |
2000 | | |
2001 | 377M | CommandInner: |
2002 | 377M | if (safe) { |
2003 | 293M | s->state = BROTLI_STATE_COMMAND_INNER; |
2004 | 293M | } |
2005 | | /* Read the literals in the command. */ |
2006 | 377M | if (s->trivial_literal_context) { |
2007 | 318M | brotli_reg_t bits; |
2008 | 318M | brotli_reg_t value; |
2009 | 318M | PreloadSymbol(safe, s->literal_htree, br, &bits, &value); |
2010 | 318M | 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 | 75.4M | int num_steps = i - 1; |
2018 | 75.4M | if (num_steps > 0 && ((brotli_reg_t)(num_steps) > s->block_length[0])) { |
2019 | | // Safe cast, since block_length < steps |
2020 | 57.2k | num_steps = (int)s->block_length[0]; |
2021 | 57.2k | } |
2022 | 75.4M | if (s->ringbuffer_size >= pos && |
2023 | 75.4M | (s->ringbuffer_size - pos) <= num_steps) { |
2024 | 11.1k | num_steps = s->ringbuffer_size - pos - 1; |
2025 | 11.1k | } |
2026 | 75.4M | if (num_steps < 0) { |
2027 | 0 | num_steps = 0; |
2028 | 0 | } |
2029 | 75.4M | num_steps = BrotliCopyPreloadedSymbolsToU8(s->literal_htree, br, &bits, |
2030 | 75.4M | &value, s->ringbuffer, pos, |
2031 | 75.4M | num_steps); |
2032 | 75.4M | pos += num_steps; |
2033 | 75.4M | s->block_length[0] -= (brotli_reg_t)num_steps; |
2034 | 75.4M | i -= num_steps; |
2035 | 75.4M | do { |
2036 | 75.4M | if (!CheckInputAmount(safe, br)) { |
2037 | 4.41k | s->state = BROTLI_STATE_COMMAND_INNER; |
2038 | 4.41k | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
2039 | 4.41k | goto saveStateAndReturn; |
2040 | 4.41k | } |
2041 | 75.4M | if (BROTLI_PREDICT_FALSE(s->block_length[0] == 0)) { |
2042 | 98.5k | goto NextLiteralBlock; |
2043 | 98.5k | } |
2044 | 75.3M | BrotliCopyPreloadedSymbolsToU8(s->literal_htree, br, &bits, &value, |
2045 | 75.3M | s->ringbuffer, pos, 1); |
2046 | 75.3M | --s->block_length[0]; |
2047 | 75.3M | BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos); |
2048 | 75.3M | ++pos; |
2049 | 75.3M | if (BROTLI_PREDICT_FALSE(pos == s->ringbuffer_size)) { |
2050 | 16.6k | s->state = BROTLI_STATE_COMMAND_INNER_WRITE; |
2051 | 16.6k | --i; |
2052 | 16.6k | goto saveStateAndReturn; |
2053 | 16.6k | } |
2054 | 75.3M | } while (--i != 0); |
2055 | 242M | } else { /* safe */ |
2056 | 1.40G | do { |
2057 | 1.40G | if (BROTLI_PREDICT_FALSE(s->block_length[0] == 0)) { |
2058 | 77.8k | goto NextLiteralBlock; |
2059 | 77.8k | } |
2060 | 1.40G | brotli_reg_t literal; |
2061 | 1.40G | if (!SafeReadSymbol(s->literal_htree, br, &literal)) { |
2062 | 2.49M | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
2063 | 2.49M | goto saveStateAndReturn; |
2064 | 2.49M | } |
2065 | 1.40G | s->ringbuffer[pos] = (uint8_t)literal; |
2066 | 1.40G | --s->block_length[0]; |
2067 | 1.40G | BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos); |
2068 | 1.40G | ++pos; |
2069 | 1.40G | if (BROTLI_PREDICT_FALSE(pos == s->ringbuffer_size)) { |
2070 | 26.2k | s->state = BROTLI_STATE_COMMAND_INNER_WRITE; |
2071 | 26.2k | --i; |
2072 | 26.2k | goto saveStateAndReturn; |
2073 | 26.2k | } |
2074 | 1.40G | } while (--i != 0); |
2075 | 242M | } |
2076 | 318M | } else { |
2077 | 59.7M | uint8_t p1 = s->ringbuffer[(pos - 1) & s->ringbuffer_mask]; |
2078 | 59.7M | uint8_t p2 = s->ringbuffer[(pos - 2) & s->ringbuffer_mask]; |
2079 | 285M | do { |
2080 | 285M | const HuffmanCode* hc; |
2081 | 285M | uint8_t context; |
2082 | 285M | if (!CheckInputAmount(safe, br)) { |
2083 | 2.31k | s->state = BROTLI_STATE_COMMAND_INNER; |
2084 | 2.31k | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
2085 | 2.31k | goto saveStateAndReturn; |
2086 | 2.31k | } |
2087 | 285M | if (BROTLI_PREDICT_FALSE(s->block_length[0] == 0)) { |
2088 | 55.5k | goto NextLiteralBlock; |
2089 | 55.5k | } |
2090 | 285M | context = BROTLI_CONTEXT(p1, p2, s->context_lookup); |
2091 | 285M | BROTLI_LOG_UINT(context); |
2092 | 285M | hc = s->literal_hgroup.htrees[s->context_map_slice[context]]; |
2093 | 285M | p2 = p1; |
2094 | 285M | if (!safe) { |
2095 | 58.7M | p1 = (uint8_t)ReadSymbol(hc, br); |
2096 | 227M | } else { |
2097 | 227M | brotli_reg_t literal; |
2098 | 227M | if (!SafeReadSymbol(hc, br, &literal)) { |
2099 | 3.51k | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
2100 | 3.51k | goto saveStateAndReturn; |
2101 | 3.51k | } |
2102 | 227M | p1 = (uint8_t)literal; |
2103 | 227M | } |
2104 | 285M | s->ringbuffer[pos] = p1; |
2105 | 285M | --s->block_length[0]; |
2106 | 285M | BROTLI_LOG_UINT(s->context_map_slice[context]); |
2107 | 285M | BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos & s->ringbuffer_mask); |
2108 | 285M | ++pos; |
2109 | 285M | if (BROTLI_PREDICT_FALSE(pos == s->ringbuffer_size)) { |
2110 | 8.06k | s->state = BROTLI_STATE_COMMAND_INNER_WRITE; |
2111 | 8.06k | --i; |
2112 | 8.06k | goto saveStateAndReturn; |
2113 | 8.06k | } |
2114 | 285M | } while (--i != 0); |
2115 | 59.7M | } |
2116 | 375M | BROTLI_LOG_UINT(s->meta_block_remaining_len); |
2117 | 375M | if (BROTLI_PREDICT_FALSE(s->meta_block_remaining_len <= 0)) { |
2118 | 3.50k | s->state = BROTLI_STATE_METABLOCK_DONE; |
2119 | 3.50k | goto saveStateAndReturn; |
2120 | 3.50k | } |
2121 | | |
2122 | 574M | CommandPostDecodeLiterals: |
2123 | 574M | if (safe) { |
2124 | 408M | s->state = BROTLI_STATE_COMMAND_POST_DECODE_LITERALS; |
2125 | 408M | } |
2126 | 574M | if (s->distance_code >= 0) { |
2127 | | /* Implicit distance case. */ |
2128 | 320M | s->distance_context = s->distance_code ? 0 : 1; |
2129 | 320M | --s->dist_rb_idx; |
2130 | 320M | s->distance_code = s->dist_rb[s->dist_rb_idx & 3]; |
2131 | 320M | } else { |
2132 | | /* Read distance code in the command, unless it was implicitly zero. */ |
2133 | 253M | if (BROTLI_PREDICT_FALSE(s->block_length[2] == 0)) { |
2134 | 124k | BROTLI_SAFE(DecodeDistanceBlockSwitch(s)); |
2135 | 117k | } |
2136 | 253M | BROTLI_SAFE(ReadDistance(s, br)); |
2137 | 253M | } |
2138 | 574M | BROTLI_LOG(("[ProcessCommandsInternal] pos = %d distance = %d\n", |
2139 | 574M | pos, s->distance_code)); |
2140 | 574M | if (s->max_distance != s->max_backward_distance) { |
2141 | 177M | s->max_distance = |
2142 | 177M | (pos < s->max_backward_distance) ? pos : s->max_backward_distance; |
2143 | 177M | } |
2144 | 574M | 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 | 574M | 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 | 59.4M | if (s->distance_code > BROTLI_MAX_ALLOWED_DISTANCE) { |
2152 | 28 | BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d " |
2153 | 28 | "len: %d bytes left: %d\n", |
2154 | 28 | pos, s->distance_code, i, s->meta_block_remaining_len)); |
2155 | 28 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_DISTANCE); |
2156 | 28 | } |
2157 | 59.4M | if (s->distance_code - s->max_distance - 1 < compound_dictionary_size) { |
2158 | 0 | int address = compound_dictionary_size - |
2159 | 0 | (s->distance_code - s->max_distance); |
2160 | 0 | if (!InitializeCompoundDictionaryCopy(s, address, i)) { |
2161 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY); |
2162 | 0 | } |
2163 | 0 | pos += CopyFromCompoundDictionary(s, pos); |
2164 | 0 | if (pos >= s->ringbuffer_size) { |
2165 | 0 | s->state = BROTLI_STATE_COMMAND_POST_WRITE_1; |
2166 | 0 | goto saveStateAndReturn; |
2167 | 0 | } |
2168 | 59.4M | } else if (i >= SHARED_BROTLI_MIN_DICTIONARY_WORD_LENGTH && |
2169 | 59.4M | i <= SHARED_BROTLI_MAX_DICTIONARY_WORD_LENGTH) { |
2170 | 59.4M | uint8_t p1 = s->ringbuffer[(pos - 1) & s->ringbuffer_mask]; |
2171 | 59.4M | uint8_t p2 = s->ringbuffer[(pos - 2) & s->ringbuffer_mask]; |
2172 | 59.4M | uint8_t dict_id = s->dictionary->context_based ? |
2173 | 0 | s->dictionary->context_map[BROTLI_CONTEXT(p1, p2, s->context_lookup)] |
2174 | 59.4M | : 0; |
2175 | 59.4M | const BrotliDictionary* words = s->dictionary->words[dict_id]; |
2176 | 59.4M | const BrotliTransforms* transforms = s->dictionary->transforms[dict_id]; |
2177 | 59.4M | int offset = (int)words->offsets_by_length[i]; |
2178 | 59.4M | brotli_reg_t shift = words->size_bits_by_length[i]; |
2179 | 59.4M | int address = |
2180 | 59.4M | s->distance_code - s->max_distance - 1 - compound_dictionary_size; |
2181 | 59.4M | int mask = (int)BitMask(shift); |
2182 | 59.4M | int word_idx = address & mask; |
2183 | 59.4M | int transform_idx = address >> shift; |
2184 | | /* Compensate double distance-ring-buffer roll. */ |
2185 | 59.4M | s->dist_rb_idx += s->distance_context; |
2186 | 59.4M | offset += word_idx * i; |
2187 | | /* If the distance is out of bound, select a next static dictionary if |
2188 | | there exist multiple. */ |
2189 | 59.4M | if ((transform_idx >= (int)transforms->num_transforms || |
2190 | 59.4M | words->size_bits_by_length[i] == 0) && |
2191 | 59.4M | s->dictionary->num_dictionaries > 1) { |
2192 | 0 | uint8_t dict_id2; |
2193 | 0 | int dist_remaining = address - |
2194 | 0 | (int)(((1u << shift) & ~1u)) * (int)transforms->num_transforms; |
2195 | 0 | for (dict_id2 = 0; dict_id2 < s->dictionary->num_dictionaries; |
2196 | 0 | dict_id2++) { |
2197 | 0 | const BrotliDictionary* words2 = s->dictionary->words[dict_id2]; |
2198 | 0 | if (dict_id2 != dict_id && words2->size_bits_by_length[i] != 0) { |
2199 | 0 | const BrotliTransforms* transforms2 = |
2200 | 0 | s->dictionary->transforms[dict_id2]; |
2201 | 0 | brotli_reg_t shift2 = words2->size_bits_by_length[i]; |
2202 | 0 | int num = (int)((1u << shift2) & ~1u) * |
2203 | 0 | (int)transforms2->num_transforms; |
2204 | 0 | if (dist_remaining < num) { |
2205 | 0 | dict_id = dict_id2; |
2206 | 0 | words = words2; |
2207 | 0 | transforms = transforms2; |
2208 | 0 | address = dist_remaining; |
2209 | 0 | shift = shift2; |
2210 | 0 | mask = (int)BitMask(shift); |
2211 | 0 | word_idx = address & mask; |
2212 | 0 | transform_idx = address >> shift; |
2213 | 0 | offset = (int)words->offsets_by_length[i] + word_idx * i; |
2214 | 0 | break; |
2215 | 0 | } |
2216 | 0 | dist_remaining -= num; |
2217 | 0 | } |
2218 | 0 | } |
2219 | 0 | } |
2220 | 59.4M | if (BROTLI_PREDICT_FALSE(words->size_bits_by_length[i] == 0)) { |
2221 | 56 | BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d " |
2222 | 56 | "len: %d bytes left: %d\n", |
2223 | 56 | pos, s->distance_code, i, s->meta_block_remaining_len)); |
2224 | 56 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_DICTIONARY); |
2225 | 56 | } |
2226 | 59.4M | if (BROTLI_PREDICT_FALSE(!words->data)) { |
2227 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET); |
2228 | 0 | } |
2229 | 59.4M | if (transform_idx < (int)transforms->num_transforms) { |
2230 | 59.4M | const uint8_t* word = &words->data[offset]; |
2231 | 59.4M | int len = i; |
2232 | 59.4M | if (transform_idx == transforms->cutOffTransforms[0]) { |
2233 | 59.1M | memcpy(&s->ringbuffer[pos], word, (size_t)len); |
2234 | 59.1M | BROTLI_LOG(("[ProcessCommandsInternal] dictionary word: [%.*s]\n", |
2235 | 59.1M | len, word)); |
2236 | 59.1M | } else { |
2237 | 327k | len = BrotliTransformDictionaryWord(&s->ringbuffer[pos], word, len, |
2238 | 327k | transforms, transform_idx); |
2239 | 327k | BROTLI_LOG(("[ProcessCommandsInternal] dictionary word: [%.*s]," |
2240 | 327k | " transform_idx = %d, transformed: [%.*s]\n", |
2241 | 327k | i, word, transform_idx, len, &s->ringbuffer[pos])); |
2242 | 327k | if (len == 0 && s->distance_code <= 120) { |
2243 | 0 | BROTLI_LOG(("Invalid length-0 dictionary word after transform\n")); |
2244 | 0 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_TRANSFORM); |
2245 | 0 | } |
2246 | 327k | } |
2247 | 59.4M | pos += len; |
2248 | 59.4M | s->meta_block_remaining_len -= len; |
2249 | 59.4M | if (pos >= s->ringbuffer_size) { |
2250 | 35.4k | s->state = BROTLI_STATE_COMMAND_POST_WRITE_1; |
2251 | 35.4k | goto saveStateAndReturn; |
2252 | 35.4k | } |
2253 | 59.4M | } else { |
2254 | 98 | BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d " |
2255 | 98 | "len: %d bytes left: %d\n", |
2256 | 98 | pos, s->distance_code, i, s->meta_block_remaining_len)); |
2257 | 98 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_TRANSFORM); |
2258 | 98 | } |
2259 | 59.4M | } else { |
2260 | 91 | BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d " |
2261 | 91 | "len: %d bytes left: %d\n", |
2262 | 91 | pos, s->distance_code, i, s->meta_block_remaining_len)); |
2263 | 91 | return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_DICTIONARY); |
2264 | 91 | } |
2265 | 514M | } else { |
2266 | 514M | int src_start = (pos - s->distance_code) & s->ringbuffer_mask; |
2267 | 514M | uint8_t* copy_dst = &s->ringbuffer[pos]; |
2268 | 514M | uint8_t* copy_src = &s->ringbuffer[src_start]; |
2269 | 514M | int dst_end = pos + i; |
2270 | 514M | int src_end = src_start + i; |
2271 | | /* Update the recent distances cache. */ |
2272 | 514M | s->dist_rb[s->dist_rb_idx & 3] = s->distance_code; |
2273 | 514M | ++s->dist_rb_idx; |
2274 | 514M | 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 | 514M | memmove16(copy_dst, copy_src); |
2279 | 514M | if (src_end > pos && dst_end > src_start) { |
2280 | | /* Regions intersect. */ |
2281 | 91.3M | goto CommandPostWrapCopy; |
2282 | 91.3M | } |
2283 | 423M | if (dst_end >= s->ringbuffer_size || src_end >= s->ringbuffer_size) { |
2284 | | /* At least one region wraps. */ |
2285 | 499k | goto CommandPostWrapCopy; |
2286 | 499k | } |
2287 | 422M | pos += i; |
2288 | 422M | if (i > 16) { |
2289 | 163k | if (i > 32) { |
2290 | 19.0k | memcpy(copy_dst + 16, copy_src + 16, (size_t)(i - 16)); |
2291 | 144k | } else { |
2292 | | /* This branch covers about 45% cases. |
2293 | | Fixed size short copy allows more compiler optimizations. */ |
2294 | 144k | memmove16(copy_dst + 16, copy_src + 16); |
2295 | 144k | } |
2296 | 163k | } |
2297 | 422M | } |
2298 | 482M | BROTLI_LOG_UINT(s->meta_block_remaining_len); |
2299 | 482M | if (s->meta_block_remaining_len <= 0) { |
2300 | | /* Next metablock, if any. */ |
2301 | 3.07k | s->state = BROTLI_STATE_METABLOCK_DONE; |
2302 | 3.07k | goto saveStateAndReturn; |
2303 | 482M | } else { |
2304 | 482M | goto CommandBegin; |
2305 | 482M | } |
2306 | 92.1M | CommandPostWrapCopy: |
2307 | 92.1M | { |
2308 | 92.1M | int wrap_guard = s->ringbuffer_size - pos; |
2309 | 1.40G | while (--i >= 0) { |
2310 | 1.31G | s->ringbuffer[pos] = |
2311 | 1.31G | s->ringbuffer[(pos - s->distance_code) & s->ringbuffer_mask]; |
2312 | 1.31G | ++pos; |
2313 | 1.31G | if (BROTLI_PREDICT_FALSE(--wrap_guard == 0)) { |
2314 | 270k | s->state = BROTLI_STATE_COMMAND_POST_WRITE_2; |
2315 | 270k | goto saveStateAndReturn; |
2316 | 270k | } |
2317 | 1.31G | } |
2318 | 92.1M | } |
2319 | 91.8M | if (s->meta_block_remaining_len <= 0) { |
2320 | | /* Next metablock, if any. */ |
2321 | 2.30k | s->state = BROTLI_STATE_METABLOCK_DONE; |
2322 | 2.30k | goto saveStateAndReturn; |
2323 | 91.8M | } else { |
2324 | 91.8M | goto CommandBegin; |
2325 | 91.8M | } |
2326 | | |
2327 | 231k | NextLiteralBlock: |
2328 | 231k | BROTLI_SAFE(DecodeLiteralBlockSwitch(s)); |
2329 | 195k | goto CommandInner; |
2330 | | |
2331 | 6.05M | saveStateAndReturn: |
2332 | 6.05M | s->pos = pos; |
2333 | 6.05M | s->loop_counter = i; |
2334 | 6.05M | return result; |
2335 | 231k | } |
2336 | | |
2337 | | #undef BROTLI_SAFE |
2338 | | |
2339 | | static BROTLI_NOINLINE BrotliDecoderErrorCode ProcessCommands( |
2340 | 3.11M | BrotliDecoderState* s) { |
2341 | 3.11M | return ProcessCommandsInternal(0, s); |
2342 | 3.11M | } |
2343 | | |
2344 | | static BROTLI_NOINLINE BrotliDecoderErrorCode SafeProcessCommands( |
2345 | 2.93M | BrotliDecoderState* s) { |
2346 | 2.93M | return ProcessCommandsInternal(1, s); |
2347 | 2.93M | } |
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 | 0 | uint8_t decoded_buffer[BROTLI_ARRAY_PARAM(*decoded_size)]) { |
2354 | 0 | BrotliDecoderState s; |
2355 | 0 | BrotliDecoderResult result; |
2356 | 0 | size_t total_out = 0; |
2357 | 0 | size_t available_in = encoded_size; |
2358 | 0 | const uint8_t* next_in = encoded_buffer; |
2359 | 0 | size_t available_out = *decoded_size; |
2360 | 0 | uint8_t* next_out = decoded_buffer; |
2361 | 0 | if (!BrotliDecoderStateInit(&s, 0, 0, 0)) { |
2362 | 0 | return BROTLI_DECODER_RESULT_ERROR; |
2363 | 0 | } |
2364 | 0 | result = BrotliDecoderDecompressStream( |
2365 | 0 | &s, &available_in, &next_in, &available_out, &next_out, &total_out); |
2366 | 0 | *decoded_size = total_out; |
2367 | 0 | BrotliDecoderStateCleanup(&s); |
2368 | 0 | if (result != BROTLI_DECODER_RESULT_SUCCESS) { |
2369 | 0 | result = BROTLI_DECODER_RESULT_ERROR; |
2370 | 0 | } |
2371 | 0 | return result; |
2372 | 0 | } |
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 | 6.03M | size_t* available_out, uint8_t** next_out, size_t* total_out) { |
2388 | 6.03M | BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS; |
2389 | 6.03M | BrotliBitReader* br = &s->br; |
2390 | 6.03M | size_t input_size = *available_in; |
2391 | 6.03M | #define BROTLI_SAVE_ERROR_CODE(code) \ |
2392 | 6.03M | SaveErrorCode(s, (code), input_size - *available_in) |
2393 | | /* Ensure that |total_out| is set, even if no data will ever be pushed out. */ |
2394 | 6.03M | if (total_out) { |
2395 | 6.03M | *total_out = s->partial_pos_out; |
2396 | 6.03M | } |
2397 | | /* Do not try to process further in a case of unrecoverable error. */ |
2398 | 6.03M | if ((int)s->error_code < 0) { |
2399 | 0 | return BROTLI_DECODER_RESULT_ERROR; |
2400 | 0 | } |
2401 | 6.03M | if (*available_out && (!next_out || !*next_out)) { |
2402 | 0 | return BROTLI_SAVE_ERROR_CODE( |
2403 | 0 | BROTLI_FAILURE(BROTLI_DECODER_ERROR_INVALID_ARGUMENTS)); |
2404 | 0 | } |
2405 | 6.03M | if (!*available_out) next_out = 0; |
2406 | 6.03M | if (s->buffer_length == 0) { /* Just connect bit reader to input stream. */ |
2407 | 6.02M | BrotliBitReaderSetInput(br, *next_in, *available_in); |
2408 | 6.02M | } 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 | 15.3k | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
2413 | 15.3k | BrotliBitReaderSetInput(br, &s->buffer.u8[0], s->buffer_length); |
2414 | 15.3k | } |
2415 | | /* State machine */ |
2416 | 12.9M | for (;;) { |
2417 | 12.9M | if (result != BROTLI_DECODER_SUCCESS) { |
2418 | | /* Error, needs more input/output. */ |
2419 | 6.06M | if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) { |
2420 | 2.92M | if (s->ringbuffer != 0) { /* Pro-actively push output. */ |
2421 | 2.89M | BrotliDecoderErrorCode intermediate_result = WriteRingBuffer(s, |
2422 | 2.89M | available_out, next_out, total_out, BROTLI_TRUE); |
2423 | | /* WriteRingBuffer checks s->meta_block_remaining_len validity. */ |
2424 | 2.89M | if ((int)intermediate_result < 0) { |
2425 | 19 | result = intermediate_result; |
2426 | 19 | break; |
2427 | 19 | } |
2428 | 2.89M | } |
2429 | 2.92M | if (s->buffer_length != 0) { /* Used with internal buffer. */ |
2430 | 29.3k | 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 | 10.8k | s->buffer_length = 0; |
2435 | | /* Switch to input stream and restart. */ |
2436 | 10.8k | result = BROTLI_DECODER_SUCCESS; |
2437 | 10.8k | BrotliBitReaderSetInput(br, *next_in, *available_in); |
2438 | 10.8k | continue; |
2439 | 18.4k | } else if (*available_in != 0) { |
2440 | | /* Not enough data in buffer, but can take one more byte from |
2441 | | input stream. */ |
2442 | 15.5k | result = BROTLI_DECODER_SUCCESS; |
2443 | 15.5k | BROTLI_DCHECK(s->buffer_length < 8); |
2444 | 15.5k | s->buffer.u8[s->buffer_length] = **next_in; |
2445 | 15.5k | s->buffer_length++; |
2446 | 15.5k | BrotliBitReaderSetInput(br, &s->buffer.u8[0], s->buffer_length); |
2447 | 15.5k | (*next_in)++; |
2448 | 15.5k | (*available_in)--; |
2449 | | /* Retry with more data in buffer. */ |
2450 | 15.5k | continue; |
2451 | 15.5k | } |
2452 | | /* Can't finish reading and no more input. */ |
2453 | 2.91k | break; |
2454 | 2.89M | } else { /* Input stream doesn't contain enough input. */ |
2455 | | /* Copy tail to internal buffer and return. */ |
2456 | 2.89M | *next_in = br->next_in; |
2457 | 2.89M | *available_in = BrotliBitReaderGetAvailIn(br); |
2458 | 2.91M | while (*available_in) { |
2459 | 13.0k | s->buffer.u8[s->buffer_length] = **next_in; |
2460 | 13.0k | s->buffer_length++; |
2461 | 13.0k | (*next_in)++; |
2462 | 13.0k | (*available_in)--; |
2463 | 13.0k | } |
2464 | 2.89M | break; |
2465 | 2.89M | } |
2466 | | /* Unreachable. */ |
2467 | 2.92M | } |
2468 | | |
2469 | | /* Fail or needs more output. */ |
2470 | | |
2471 | 3.13M | 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 | 1.55k | s->buffer_length = 0; |
2475 | 3.13M | } 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 | 3.13M | BrotliBitReaderUnload(br); |
2480 | 3.13M | *available_in = BrotliBitReaderGetAvailIn(br); |
2481 | 3.13M | *next_in = br->next_in; |
2482 | 3.13M | } |
2483 | 3.13M | break; |
2484 | 6.06M | } |
2485 | 6.87M | switch (s->state) { |
2486 | 4.59k | case BROTLI_STATE_UNINITED: |
2487 | | /* Prepare to the first read. */ |
2488 | 4.59k | if (!BrotliWarmupBitReader(br)) { |
2489 | 0 | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
2490 | 0 | break; |
2491 | 0 | } |
2492 | | /* Decode window size. */ |
2493 | 4.59k | result = DecodeWindowBits(s, br); /* Reads 1..8 bits. */ |
2494 | 4.59k | if (result != BROTLI_DECODER_SUCCESS) { |
2495 | 1 | break; |
2496 | 1 | } |
2497 | 4.59k | if (s->large_window) { |
2498 | 0 | s->state = BROTLI_STATE_LARGE_WINDOW_BITS; |
2499 | 0 | break; |
2500 | 0 | } |
2501 | 4.59k | s->state = BROTLI_STATE_INITIALIZE; |
2502 | 4.59k | break; |
2503 | | |
2504 | 0 | case BROTLI_STATE_LARGE_WINDOW_BITS: { |
2505 | 0 | brotli_reg_t bits; |
2506 | 0 | if (!BrotliSafeReadBits(br, 6, &bits)) { |
2507 | 0 | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
2508 | 0 | break; |
2509 | 0 | } |
2510 | 0 | s->window_bits = bits & 63u; |
2511 | 0 | if (s->window_bits < BROTLI_LARGE_MIN_WBITS || |
2512 | 0 | s->window_bits > BROTLI_LARGE_MAX_WBITS) { |
2513 | 0 | result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS); |
2514 | 0 | break; |
2515 | 0 | } |
2516 | 0 | s->state = BROTLI_STATE_INITIALIZE; |
2517 | 0 | } |
2518 | | /* Fall through. */ |
2519 | | |
2520 | 4.59k | case BROTLI_STATE_INITIALIZE: |
2521 | 4.59k | BROTLI_LOG_UINT(s->window_bits); |
2522 | | /* Maximum distance, see section 9.1. of the spec. */ |
2523 | 4.59k | 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 | 4.59k | s->block_type_trees = (HuffmanCode*)BROTLI_DECODER_ALLOC(s, |
2527 | 4.59k | sizeof(HuffmanCode) * 3 * |
2528 | 4.59k | (BROTLI_HUFFMAN_MAX_SIZE_258 + BROTLI_HUFFMAN_MAX_SIZE_26)); |
2529 | 4.59k | if (s->block_type_trees == 0) { |
2530 | 0 | result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES); |
2531 | 0 | break; |
2532 | 0 | } |
2533 | 4.59k | s->block_len_trees = |
2534 | 4.59k | s->block_type_trees + 3 * BROTLI_HUFFMAN_MAX_SIZE_258; |
2535 | | |
2536 | 4.59k | s->state = BROTLI_STATE_METABLOCK_BEGIN; |
2537 | | /* Fall through. */ |
2538 | | |
2539 | 20.1k | case BROTLI_STATE_METABLOCK_BEGIN: |
2540 | 20.1k | BrotliDecoderStateMetablockBegin(s); |
2541 | 20.1k | BROTLI_LOG_UINT(s->pos); |
2542 | 20.1k | s->state = BROTLI_STATE_METABLOCK_HEADER; |
2543 | | /* Fall through. */ |
2544 | | |
2545 | 36.9k | case BROTLI_STATE_METABLOCK_HEADER: |
2546 | 36.9k | result = DecodeMetaBlockLength(s, br); /* Reads 2 - 31 bits. */ |
2547 | 36.9k | if (result != BROTLI_DECODER_SUCCESS) { |
2548 | 16.9k | break; |
2549 | 16.9k | } |
2550 | 19.9k | BROTLI_LOG_UINT(s->is_last_metablock); |
2551 | 19.9k | BROTLI_LOG_UINT(s->meta_block_remaining_len); |
2552 | 19.9k | BROTLI_LOG_UINT(s->is_metadata); |
2553 | 19.9k | BROTLI_LOG_UINT(s->is_uncompressed); |
2554 | 19.9k | if (s->is_metadata || s->is_uncompressed) { |
2555 | 6.89k | if (!BrotliJumpToByteBoundary(br)) { |
2556 | 50 | result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_PADDING_1); |
2557 | 50 | break; |
2558 | 50 | } |
2559 | 6.89k | } |
2560 | 19.8k | if (s->is_metadata) { |
2561 | 4.26k | s->state = BROTLI_STATE_METADATA; |
2562 | 4.26k | if (s->metadata_start_func) { |
2563 | 0 | s->metadata_start_func(s->metadata_callback_opaque, |
2564 | 0 | (size_t)s->meta_block_remaining_len); |
2565 | 0 | } |
2566 | 4.26k | break; |
2567 | 4.26k | } |
2568 | 15.6k | if (s->meta_block_remaining_len == 0) { |
2569 | 63 | s->state = BROTLI_STATE_METABLOCK_DONE; |
2570 | 63 | break; |
2571 | 63 | } |
2572 | 15.5k | BrotliCalculateRingBufferSize(s); |
2573 | 15.5k | if (s->is_uncompressed) { |
2574 | 2.58k | s->state = BROTLI_STATE_UNCOMPRESSED; |
2575 | 2.58k | break; |
2576 | 2.58k | } |
2577 | 12.9k | s->state = BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_HEADER; |
2578 | | /* Fall through. */ |
2579 | | |
2580 | 12.9k | case BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_HEADER: { |
2581 | 12.9k | BrotliMetablockHeaderArena* h = &s->arena.header; |
2582 | 12.9k | s->loop_counter = 0; |
2583 | | /* Initialize compressed metablock header arena. */ |
2584 | 12.9k | h->sub_loop_counter = 0; |
2585 | | /* Make small negative indexes addressable. */ |
2586 | 12.9k | h->symbol_lists = |
2587 | 12.9k | &h->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1]; |
2588 | 12.9k | h->substate_huffman = BROTLI_STATE_HUFFMAN_NONE; |
2589 | 12.9k | h->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE; |
2590 | 12.9k | h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE; |
2591 | 12.9k | s->state = BROTLI_STATE_HUFFMAN_CODE_0; |
2592 | 12.9k | } |
2593 | | /* Fall through. */ |
2594 | | |
2595 | 52.3k | case BROTLI_STATE_HUFFMAN_CODE_0: |
2596 | 52.3k | if (s->loop_counter >= 3) { |
2597 | 12.4k | s->state = BROTLI_STATE_METABLOCK_HEADER_2; |
2598 | 12.4k | break; |
2599 | 12.4k | } |
2600 | | /* Reads 1..11 bits. */ |
2601 | 39.8k | result = DecodeVarLenUint8(s, br, &s->num_block_types[s->loop_counter]); |
2602 | 39.8k | if (result != BROTLI_DECODER_SUCCESS) { |
2603 | 1.70k | break; |
2604 | 1.70k | } |
2605 | 38.1k | s->num_block_types[s->loop_counter]++; |
2606 | 38.1k | BROTLI_LOG_UINT(s->num_block_types[s->loop_counter]); |
2607 | 38.1k | if (s->num_block_types[s->loop_counter] < 2) { |
2608 | 33.8k | s->loop_counter++; |
2609 | 33.8k | break; |
2610 | 33.8k | } |
2611 | 4.27k | s->state = BROTLI_STATE_HUFFMAN_CODE_1; |
2612 | | /* Fall through. */ |
2613 | | |
2614 | 7.50k | case BROTLI_STATE_HUFFMAN_CODE_1: { |
2615 | 7.50k | brotli_reg_t alphabet_size = s->num_block_types[s->loop_counter] + 2; |
2616 | 7.50k | int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_258; |
2617 | 7.50k | result = ReadHuffmanCode(alphabet_size, alphabet_size, |
2618 | 7.50k | &s->block_type_trees[tree_offset], NULL, s); |
2619 | 7.50k | if (result != BROTLI_DECODER_SUCCESS) break; |
2620 | 3.97k | s->state = BROTLI_STATE_HUFFMAN_CODE_2; |
2621 | 3.97k | } |
2622 | | /* Fall through. */ |
2623 | | |
2624 | 8.02k | case BROTLI_STATE_HUFFMAN_CODE_2: { |
2625 | 8.02k | brotli_reg_t alphabet_size = BROTLI_NUM_BLOCK_LEN_SYMBOLS; |
2626 | 8.02k | int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26; |
2627 | 8.02k | result = ReadHuffmanCode(alphabet_size, alphabet_size, |
2628 | 8.02k | &s->block_len_trees[tree_offset], NULL, s); |
2629 | 8.02k | if (result != BROTLI_DECODER_SUCCESS) break; |
2630 | 3.84k | s->state = BROTLI_STATE_HUFFMAN_CODE_3; |
2631 | 3.84k | } |
2632 | | /* Fall through. */ |
2633 | | |
2634 | 7.03k | case BROTLI_STATE_HUFFMAN_CODE_3: { |
2635 | 7.03k | int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26; |
2636 | 7.03k | if (!SafeReadBlockLength(s, &s->block_length[s->loop_counter], |
2637 | 7.03k | &s->block_len_trees[tree_offset], br)) { |
2638 | 3.22k | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
2639 | 3.22k | break; |
2640 | 3.22k | } |
2641 | 3.81k | BROTLI_LOG_UINT(s->block_length[s->loop_counter]); |
2642 | 3.81k | s->loop_counter++; |
2643 | 3.81k | s->state = BROTLI_STATE_HUFFMAN_CODE_0; |
2644 | 3.81k | break; |
2645 | 7.03k | } |
2646 | | |
2647 | 111k | case BROTLI_STATE_UNCOMPRESSED: { |
2648 | 111k | result = CopyUncompressedBlockToOutput( |
2649 | 111k | available_out, next_out, total_out, s); |
2650 | 111k | if (result != BROTLI_DECODER_SUCCESS) { |
2651 | 108k | break; |
2652 | 108k | } |
2653 | 2.41k | s->state = BROTLI_STATE_METABLOCK_DONE; |
2654 | 2.41k | break; |
2655 | 111k | } |
2656 | | |
2657 | 7.32k | case BROTLI_STATE_METADATA: |
2658 | 7.32k | result = SkipMetadataBlock(s); |
2659 | 7.32k | if (result != BROTLI_DECODER_SUCCESS) { |
2660 | 3.21k | break; |
2661 | 3.21k | } |
2662 | 4.10k | s->state = BROTLI_STATE_METABLOCK_DONE; |
2663 | 4.10k | break; |
2664 | | |
2665 | 17.5k | case BROTLI_STATE_METABLOCK_HEADER_2: { |
2666 | 17.5k | brotli_reg_t bits; |
2667 | 17.5k | if (!BrotliSafeReadBits(br, 6, &bits)) { |
2668 | 5.17k | result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
2669 | 5.17k | break; |
2670 | 5.17k | } |
2671 | 12.4k | s->distance_postfix_bits = bits & BitMask(2); |
2672 | 12.4k | bits >>= 2; |
2673 | 12.4k | s->num_direct_distance_codes = bits << s->distance_postfix_bits; |
2674 | 12.4k | BROTLI_LOG_UINT(s->num_direct_distance_codes); |
2675 | 12.4k | BROTLI_LOG_UINT(s->distance_postfix_bits); |
2676 | 12.4k | s->context_modes = |
2677 | 12.4k | (uint8_t*)BROTLI_DECODER_ALLOC(s, (size_t)s->num_block_types[0]); |
2678 | 12.4k | if (s->context_modes == 0) { |
2679 | 0 | result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES); |
2680 | 0 | break; |
2681 | 0 | } |
2682 | 12.4k | s->loop_counter = 0; |
2683 | 12.4k | s->state = BROTLI_STATE_CONTEXT_MODES; |
2684 | 12.4k | } |
2685 | | /* Fall through. */ |
2686 | | |
2687 | 14.4k | case BROTLI_STATE_CONTEXT_MODES: |
2688 | 14.4k | result = ReadContextModes(s); |
2689 | 14.4k | if (result != BROTLI_DECODER_SUCCESS) { |
2690 | 2.03k | break; |
2691 | 2.03k | } |
2692 | 12.3k | s->state = BROTLI_STATE_CONTEXT_MAP_1; |
2693 | | /* Fall through. */ |
2694 | | |
2695 | 18.4k | case BROTLI_STATE_CONTEXT_MAP_1: |
2696 | 18.4k | result = DecodeContextMap( |
2697 | 18.4k | s->num_block_types[0] << BROTLI_LITERAL_CONTEXT_BITS, |
2698 | 18.4k | &s->num_literal_htrees, &s->context_map, s); |
2699 | 18.4k | if (result != BROTLI_DECODER_SUCCESS) { |
2700 | 6.22k | break; |
2701 | 6.22k | } |
2702 | 12.2k | DetectTrivialLiteralBlockTypes(s); |
2703 | 12.2k | s->state = BROTLI_STATE_CONTEXT_MAP_2; |
2704 | | /* Fall through. */ |
2705 | | |
2706 | 14.0k | case BROTLI_STATE_CONTEXT_MAP_2: { |
2707 | 14.0k | brotli_reg_t npostfix = s->distance_postfix_bits; |
2708 | 14.0k | brotli_reg_t ndirect = s->num_direct_distance_codes; |
2709 | 14.0k | brotli_reg_t distance_alphabet_size_max = BROTLI_DISTANCE_ALPHABET_SIZE( |
2710 | 14.0k | npostfix, ndirect, BROTLI_MAX_DISTANCE_BITS); |
2711 | 14.0k | brotli_reg_t distance_alphabet_size_limit = distance_alphabet_size_max; |
2712 | 14.0k | BROTLI_BOOL allocation_success = BROTLI_TRUE; |
2713 | 14.0k | if (s->large_window) { |
2714 | 0 | BrotliDistanceCodeLimit limit = BrotliCalculateDistanceCodeLimit( |
2715 | 0 | BROTLI_MAX_ALLOWED_DISTANCE, (uint32_t)npostfix, |
2716 | 0 | (uint32_t)ndirect); |
2717 | 0 | distance_alphabet_size_max = BROTLI_DISTANCE_ALPHABET_SIZE( |
2718 | 0 | npostfix, ndirect, BROTLI_LARGE_MAX_DISTANCE_BITS); |
2719 | 0 | distance_alphabet_size_limit = limit.max_alphabet_size; |
2720 | 0 | } |
2721 | 14.0k | result = DecodeContextMap( |
2722 | 14.0k | s->num_block_types[2] << BROTLI_DISTANCE_CONTEXT_BITS, |
2723 | 14.0k | &s->num_dist_htrees, &s->dist_context_map, s); |
2724 | 14.0k | if (result != BROTLI_DECODER_SUCCESS) { |
2725 | 2.05k | break; |
2726 | 2.05k | } |
2727 | 12.0k | allocation_success &= BrotliDecoderHuffmanTreeGroupInit( |
2728 | 12.0k | s, &s->literal_hgroup, BROTLI_NUM_LITERAL_SYMBOLS, |
2729 | 12.0k | BROTLI_NUM_LITERAL_SYMBOLS, s->num_literal_htrees); |
2730 | 12.0k | allocation_success &= BrotliDecoderHuffmanTreeGroupInit( |
2731 | 12.0k | s, &s->insert_copy_hgroup, BROTLI_NUM_COMMAND_SYMBOLS, |
2732 | 12.0k | BROTLI_NUM_COMMAND_SYMBOLS, s->num_block_types[1]); |
2733 | 12.0k | allocation_success &= BrotliDecoderHuffmanTreeGroupInit( |
2734 | 12.0k | s, &s->distance_hgroup, distance_alphabet_size_max, |
2735 | 12.0k | distance_alphabet_size_limit, s->num_dist_htrees); |
2736 | 12.0k | if (!allocation_success) { |
2737 | 0 | return BROTLI_SAVE_ERROR_CODE( |
2738 | 0 | BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS)); |
2739 | 0 | } |
2740 | 12.0k | s->loop_counter = 0; |
2741 | 12.0k | s->state = BROTLI_STATE_TREE_GROUP; |
2742 | 12.0k | } |
2743 | | /* Fall through. */ |
2744 | | |
2745 | 77.5k | case BROTLI_STATE_TREE_GROUP: { |
2746 | 77.5k | HuffmanTreeGroup* hgroup = NULL; |
2747 | 77.5k | switch (s->loop_counter) { |
2748 | 23.3k | case 0: hgroup = &s->literal_hgroup; break; |
2749 | 25.1k | case 1: hgroup = &s->insert_copy_hgroup; break; |
2750 | 29.0k | case 2: hgroup = &s->distance_hgroup; break; |
2751 | 0 | default: return BROTLI_SAVE_ERROR_CODE(BROTLI_FAILURE( |
2752 | 77.5k | BROTLI_DECODER_ERROR_UNREACHABLE)); /* COV_NF_LINE */ |
2753 | 77.5k | } |
2754 | 77.5k | result = HuffmanTreeGroupDecode(hgroup, s); |
2755 | 77.5k | if (result != BROTLI_DECODER_SUCCESS) break; |
2756 | 34.6k | s->loop_counter++; |
2757 | 34.6k | if (s->loop_counter < 3) { |
2758 | 23.2k | break; |
2759 | 23.2k | } |
2760 | 11.3k | s->state = BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_BODY; |
2761 | 11.3k | } |
2762 | | /* Fall through. */ |
2763 | | |
2764 | 11.3k | case BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_BODY: |
2765 | 11.3k | PrepareLiteralDecoding(s); |
2766 | 11.3k | s->dist_context_map_slice = s->dist_context_map; |
2767 | 11.3k | s->htree_command = s->insert_copy_hgroup.htrees[0]; |
2768 | 11.3k | if (!BrotliEnsureRingBuffer(s)) { |
2769 | 0 | result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2); |
2770 | 0 | break; |
2771 | 0 | } |
2772 | 11.3k | CalculateDistanceLut(s); |
2773 | 11.3k | s->state = BROTLI_STATE_COMMAND_BEGIN; |
2774 | | /* Fall through. */ |
2775 | | |
2776 | 240k | case BROTLI_STATE_COMMAND_BEGIN: |
2777 | | /* Fall through. */ |
2778 | 2.80M | case BROTLI_STATE_COMMAND_INNER: |
2779 | | /* Fall through. */ |
2780 | 2.84M | case BROTLI_STATE_COMMAND_POST_DECODE_LITERALS: |
2781 | | /* Fall through. */ |
2782 | 3.11M | case BROTLI_STATE_COMMAND_POST_WRAP_COPY: |
2783 | 3.11M | result = ProcessCommands(s); |
2784 | 3.11M | if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) { |
2785 | 2.93M | result = SafeProcessCommands(s); |
2786 | 2.93M | } |
2787 | 3.11M | break; |
2788 | | |
2789 | 1.40M | case BROTLI_STATE_COMMAND_INNER_WRITE: |
2790 | | /* Fall through. */ |
2791 | 1.77M | case BROTLI_STATE_COMMAND_POST_WRITE_1: |
2792 | | /* Fall through. */ |
2793 | 3.30M | case BROTLI_STATE_COMMAND_POST_WRITE_2: |
2794 | 3.30M | result = WriteRingBuffer( |
2795 | 3.30M | s, available_out, next_out, total_out, BROTLI_FALSE); |
2796 | 3.30M | if (result != BROTLI_DECODER_SUCCESS) { |
2797 | 2.94M | break; |
2798 | 2.94M | } |
2799 | 356k | WrapRingBuffer(s); |
2800 | 356k | if (s->ringbuffer_size == 1 << s->window_bits) { |
2801 | 356k | s->max_distance = s->max_backward_distance; |
2802 | 356k | } |
2803 | 356k | if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_1) { |
2804 | 35.4k | BrotliDecoderCompoundDictionary* addon = s->compound_dictionary; |
2805 | 35.4k | if (addon && (addon->br_length != addon->br_copied)) { |
2806 | 0 | s->pos += CopyFromCompoundDictionary(s, s->pos); |
2807 | 0 | if (s->pos >= s->ringbuffer_size) continue; |
2808 | 0 | } |
2809 | 35.4k | if (s->meta_block_remaining_len == 0) { |
2810 | | /* Next metablock, if any. */ |
2811 | 325 | s->state = BROTLI_STATE_METABLOCK_DONE; |
2812 | 35.0k | } else { |
2813 | 35.0k | s->state = BROTLI_STATE_COMMAND_BEGIN; |
2814 | 35.0k | } |
2815 | 35.4k | break; |
2816 | 321k | } else if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_2) { |
2817 | 270k | s->state = BROTLI_STATE_COMMAND_POST_WRAP_COPY; |
2818 | 270k | } else { /* BROTLI_STATE_COMMAND_INNER_WRITE */ |
2819 | 50.9k | if (s->loop_counter == 0) { |
2820 | 17.5k | if (s->meta_block_remaining_len == 0) { |
2821 | 353 | s->state = BROTLI_STATE_METABLOCK_DONE; |
2822 | 17.1k | } else { |
2823 | 17.1k | s->state = BROTLI_STATE_COMMAND_POST_DECODE_LITERALS; |
2824 | 17.1k | } |
2825 | 17.5k | break; |
2826 | 17.5k | } |
2827 | 33.3k | s->state = BROTLI_STATE_COMMAND_INNER; |
2828 | 33.3k | } |
2829 | 303k | break; |
2830 | | |
2831 | 303k | case BROTLI_STATE_METABLOCK_DONE: |
2832 | 16.1k | if (s->meta_block_remaining_len < 0) { |
2833 | 445 | result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2); |
2834 | 445 | break; |
2835 | 445 | } |
2836 | 15.6k | BrotliDecoderStateCleanupAfterMetablock(s); |
2837 | 15.6k | if (!s->is_last_metablock) { |
2838 | 15.5k | s->state = BROTLI_STATE_METABLOCK_BEGIN; |
2839 | 15.5k | break; |
2840 | 15.5k | } |
2841 | 120 | if (!BrotliJumpToByteBoundary(br)) { |
2842 | 31 | result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_PADDING_2); |
2843 | 31 | break; |
2844 | 31 | } |
2845 | 89 | if (s->buffer_length == 0) { |
2846 | 87 | BrotliBitReaderUnload(br); |
2847 | 87 | *available_in = BrotliBitReaderGetAvailIn(br); |
2848 | 87 | *next_in = br->next_in; |
2849 | 87 | } |
2850 | 89 | s->state = BROTLI_STATE_DONE; |
2851 | | /* Fall through. */ |
2852 | | |
2853 | 151k | case BROTLI_STATE_DONE: |
2854 | 151k | if (s->ringbuffer != 0) { |
2855 | 151k | result = WriteRingBuffer( |
2856 | 151k | s, available_out, next_out, total_out, BROTLI_TRUE); |
2857 | 151k | if (result != BROTLI_DECODER_SUCCESS) { |
2858 | 151k | break; |
2859 | 151k | } |
2860 | 151k | } |
2861 | 88 | return BROTLI_SAVE_ERROR_CODE(result); |
2862 | 6.87M | } |
2863 | 6.87M | } |
2864 | 6.03M | return BROTLI_SAVE_ERROR_CODE(result); |
2865 | 6.03M | #undef BROTLI_SAVE_ERROR_CODE |
2866 | 6.03M | } |
2867 | | |
2868 | 0 | BROTLI_BOOL BrotliDecoderHasMoreOutput(const BrotliDecoderState* s) { |
2869 | | /* After unrecoverable error remaining output is considered nonsensical. */ |
2870 | 0 | if ((int)s->error_code < 0) { |
2871 | 0 | return BROTLI_FALSE; |
2872 | 0 | } |
2873 | 0 | return TO_BROTLI_BOOL( |
2874 | 0 | s->ringbuffer != 0 && UnwrittenBytes(s, BROTLI_FALSE) != 0); |
2875 | 0 | } |
2876 | | |
2877 | 0 | const uint8_t* BrotliDecoderTakeOutput(BrotliDecoderState* s, size_t* size) { |
2878 | 0 | uint8_t* result = 0; |
2879 | 0 | size_t available_out = *size ? *size : 1u << 24; |
2880 | 0 | size_t requested_out = available_out; |
2881 | 0 | BrotliDecoderErrorCode status; |
2882 | 0 | if ((s->ringbuffer == 0) || ((int)s->error_code < 0)) { |
2883 | 0 | *size = 0; |
2884 | 0 | return 0; |
2885 | 0 | } |
2886 | 0 | WrapRingBuffer(s); |
2887 | 0 | status = WriteRingBuffer(s, &available_out, &result, 0, BROTLI_TRUE); |
2888 | | /* Either WriteRingBuffer returns those "success" codes... */ |
2889 | 0 | if (status == BROTLI_DECODER_SUCCESS || |
2890 | 0 | status == BROTLI_DECODER_NEEDS_MORE_OUTPUT) { |
2891 | 0 | *size = requested_out - available_out; |
2892 | 0 | } else { |
2893 | | /* ... or stream is broken. Normally this should be caught by |
2894 | | BrotliDecoderDecompressStream, this is just a safeguard. */ |
2895 | 0 | if ((int)status < 0) SaveErrorCode(s, status, 0); |
2896 | 0 | *size = 0; |
2897 | 0 | result = 0; |
2898 | 0 | } |
2899 | 0 | return result; |
2900 | 0 | } |
2901 | | |
2902 | 0 | BROTLI_BOOL BrotliDecoderIsUsed(const BrotliDecoderState* s) { |
2903 | 0 | return TO_BROTLI_BOOL(s->state != BROTLI_STATE_UNINITED || |
2904 | 0 | BrotliGetAvailableBits(&s->br) != 0); |
2905 | 0 | } |
2906 | | |
2907 | 0 | BROTLI_BOOL BrotliDecoderIsFinished(const BrotliDecoderState* s) { |
2908 | 0 | return TO_BROTLI_BOOL(s->state == BROTLI_STATE_DONE) && |
2909 | 0 | !BrotliDecoderHasMoreOutput(s); |
2910 | 0 | } |
2911 | | |
2912 | 0 | BrotliDecoderErrorCode BrotliDecoderGetErrorCode(const BrotliDecoderState* s) { |
2913 | 0 | return (BrotliDecoderErrorCode)s->error_code; |
2914 | 0 | } |
2915 | | |
2916 | 0 | const char* BrotliDecoderErrorString(BrotliDecoderErrorCode c) { |
2917 | 0 | switch (c) { |
2918 | 0 | #define BROTLI_ERROR_CODE_CASE_(PREFIX, NAME, CODE) \ |
2919 | 0 | case BROTLI_DECODER ## PREFIX ## NAME: return #PREFIX #NAME; |
2920 | 0 | #define BROTLI_NOTHING_ |
2921 | 0 | BROTLI_DECODER_ERROR_CODES_LIST(BROTLI_ERROR_CODE_CASE_, BROTLI_NOTHING_) |
2922 | 0 | #undef BROTLI_ERROR_CODE_CASE_ |
2923 | 0 | #undef BROTLI_NOTHING_ |
2924 | 0 | default: return "INVALID"; |
2925 | 0 | } |
2926 | 0 | } |
2927 | | |
2928 | 0 | uint32_t BrotliDecoderVersion(void) { |
2929 | 0 | return BROTLI_VERSION; |
2930 | 0 | } |
2931 | | |
2932 | | void BrotliDecoderSetMetadataCallbacks( |
2933 | | BrotliDecoderState* state, |
2934 | | brotli_decoder_metadata_start_func start_func, |
2935 | 0 | brotli_decoder_metadata_chunk_func chunk_func, void* opaque) { |
2936 | 0 | state->metadata_start_func = start_func; |
2937 | 0 | state->metadata_chunk_func = chunk_func; |
2938 | 0 | state->metadata_callback_opaque = opaque; |
2939 | 0 | } |
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 |