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