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