/src/libjxl/third_party/brotli/c/dec/state.c
Line | Count | Source |
1 | | /* Copyright 2015 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 "state.h" |
8 | | |
9 | | #include "../common/dictionary.h" |
10 | | #include "../common/platform.h" |
11 | | #include "huffman.h" |
12 | | |
13 | | #if defined(__cplusplus) || defined(c_plusplus) |
14 | | extern "C" { |
15 | | #endif |
16 | | |
17 | | #ifdef BROTLI_REPORTING |
18 | | /* When BROTLI_REPORTING is defined extra reporting module have to be linked. */ |
19 | | void BrotliDecoderOnStart(const BrotliDecoderState* s); |
20 | | void BrotliDecoderOnFinish(const BrotliDecoderState* s); |
21 | | #define BROTLI_DECODER_ON_START(s) BrotliDecoderOnStart(s); |
22 | | #define BROTLI_DECODER_ON_FINISH(s) BrotliDecoderOnFinish(s); |
23 | | #else |
24 | | #if !defined(BROTLI_DECODER_ON_START) |
25 | 0 | #define BROTLI_DECODER_ON_START(s) (void)(s); |
26 | | #endif |
27 | | #if !defined(BROTLI_DECODER_ON_FINISH) |
28 | 0 | #define BROTLI_DECODER_ON_FINISH(s) (void)(s); |
29 | | #endif |
30 | | #endif |
31 | | |
32 | | BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s, |
33 | 0 | brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) { |
34 | 0 | BROTLI_DECODER_ON_START(s); |
35 | 0 | if (!alloc_func) { |
36 | 0 | s->alloc_func = BrotliDefaultAllocFunc; |
37 | 0 | s->free_func = BrotliDefaultFreeFunc; |
38 | 0 | s->memory_manager_opaque = 0; |
39 | 0 | } else { |
40 | 0 | s->alloc_func = alloc_func; |
41 | 0 | s->free_func = free_func; |
42 | 0 | s->memory_manager_opaque = opaque; |
43 | 0 | } |
44 | |
|
45 | 0 | s->error_code = 0; /* BROTLI_DECODER_NO_ERROR */ |
46 | |
|
47 | 0 | BrotliInitBitReader(&s->br); |
48 | 0 | s->state = BROTLI_STATE_UNINITED; |
49 | 0 | s->large_window = 0; |
50 | 0 | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; |
51 | 0 | s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE; |
52 | 0 | s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE; |
53 | 0 | s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE; |
54 | |
|
55 | 0 | s->buffer_length = 0; |
56 | 0 | s->loop_counter = 0; |
57 | 0 | s->pos = 0; |
58 | 0 | s->rb_roundtrips = 0; |
59 | 0 | s->partial_pos_out = 0; |
60 | 0 | s->used_input = 0; |
61 | |
|
62 | 0 | s->block_type_trees = NULL; |
63 | 0 | s->block_len_trees = NULL; |
64 | 0 | s->ringbuffer = NULL; |
65 | 0 | s->ringbuffer_size = 0; |
66 | 0 | s->new_ringbuffer_size = 0; |
67 | 0 | s->ringbuffer_mask = 0; |
68 | |
|
69 | 0 | s->context_map = NULL; |
70 | 0 | s->context_modes = NULL; |
71 | 0 | s->dist_context_map = NULL; |
72 | 0 | s->context_map_slice = NULL; |
73 | 0 | s->dist_context_map_slice = NULL; |
74 | |
|
75 | 0 | s->literal_hgroup.codes = NULL; |
76 | 0 | s->literal_hgroup.htrees = NULL; |
77 | 0 | s->insert_copy_hgroup.codes = NULL; |
78 | 0 | s->insert_copy_hgroup.htrees = NULL; |
79 | 0 | s->distance_hgroup.codes = NULL; |
80 | 0 | s->distance_hgroup.htrees = NULL; |
81 | |
|
82 | 0 | s->is_last_metablock = 0; |
83 | 0 | s->is_uncompressed = 0; |
84 | 0 | s->is_metadata = 0; |
85 | 0 | s->should_wrap_ringbuffer = 0; |
86 | 0 | s->canny_ringbuffer_allocation = 1; |
87 | |
|
88 | 0 | s->window_bits = 0; |
89 | 0 | s->max_distance = 0; |
90 | 0 | s->dist_rb[0] = 16; |
91 | 0 | s->dist_rb[1] = 15; |
92 | 0 | s->dist_rb[2] = 11; |
93 | 0 | s->dist_rb[3] = 4; |
94 | 0 | s->dist_rb_idx = 0; |
95 | 0 | s->block_type_trees = NULL; |
96 | 0 | s->block_len_trees = NULL; |
97 | |
|
98 | 0 | s->mtf_upper_bound = 63; |
99 | |
|
100 | 0 | s->compound_dictionary = NULL; |
101 | 0 | s->dictionary = |
102 | 0 | BrotliSharedDictionaryCreateInstance(alloc_func, free_func, opaque); |
103 | 0 | if (!s->dictionary) return BROTLI_FALSE; |
104 | | |
105 | 0 | s->metadata_start_func = NULL; |
106 | 0 | s->metadata_chunk_func = NULL; |
107 | 0 | s->metadata_callback_opaque = 0; |
108 | |
|
109 | 0 | return BROTLI_TRUE; |
110 | 0 | } |
111 | | |
112 | 0 | void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) { |
113 | 0 | s->meta_block_remaining_len = 0; |
114 | 0 | s->block_length[0] = BROTLI_BLOCK_SIZE_CAP; |
115 | 0 | s->block_length[1] = BROTLI_BLOCK_SIZE_CAP; |
116 | 0 | s->block_length[2] = BROTLI_BLOCK_SIZE_CAP; |
117 | 0 | s->num_block_types[0] = 1; |
118 | 0 | s->num_block_types[1] = 1; |
119 | 0 | s->num_block_types[2] = 1; |
120 | 0 | s->block_type_rb[0] = 1; |
121 | 0 | s->block_type_rb[1] = 0; |
122 | 0 | s->block_type_rb[2] = 1; |
123 | 0 | s->block_type_rb[3] = 0; |
124 | 0 | s->block_type_rb[4] = 1; |
125 | 0 | s->block_type_rb[5] = 0; |
126 | 0 | s->context_map = NULL; |
127 | 0 | s->context_modes = NULL; |
128 | 0 | s->dist_context_map = NULL; |
129 | 0 | s->context_map_slice = NULL; |
130 | 0 | s->literal_htree = NULL; |
131 | 0 | s->dist_context_map_slice = NULL; |
132 | 0 | s->dist_htree_index = 0; |
133 | 0 | s->context_lookup = NULL; |
134 | 0 | s->literal_hgroup.codes = NULL; |
135 | 0 | s->literal_hgroup.htrees = NULL; |
136 | 0 | s->insert_copy_hgroup.codes = NULL; |
137 | 0 | s->insert_copy_hgroup.htrees = NULL; |
138 | 0 | s->distance_hgroup.codes = NULL; |
139 | 0 | s->distance_hgroup.htrees = NULL; |
140 | 0 | } |
141 | | |
142 | 0 | void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) { |
143 | 0 | BROTLI_DECODER_FREE(s, s->context_modes); |
144 | 0 | BROTLI_DECODER_FREE(s, s->context_map); |
145 | 0 | BROTLI_DECODER_FREE(s, s->dist_context_map); |
146 | 0 | BROTLI_DECODER_FREE(s, s->literal_hgroup.htrees); |
147 | 0 | BROTLI_DECODER_FREE(s, s->insert_copy_hgroup.htrees); |
148 | 0 | BROTLI_DECODER_FREE(s, s->distance_hgroup.htrees); |
149 | 0 | } |
150 | | |
151 | 0 | void BrotliDecoderStateCleanup(BrotliDecoderState* s) { |
152 | 0 | BrotliDecoderStateCleanupAfterMetablock(s); |
153 | |
|
154 | 0 | BROTLI_DECODER_ON_FINISH(s); |
155 | |
|
156 | 0 | BROTLI_DECODER_FREE(s, s->compound_dictionary); |
157 | 0 | BrotliSharedDictionaryDestroyInstance(s->dictionary); |
158 | 0 | s->dictionary = NULL; |
159 | 0 | BROTLI_DECODER_FREE(s, s->ringbuffer); |
160 | 0 | BROTLI_DECODER_FREE(s, s->block_type_trees); |
161 | 0 | } |
162 | | |
163 | | BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s, |
164 | | HuffmanTreeGroup* group, brotli_reg_t alphabet_size_max, |
165 | 0 | brotli_reg_t alphabet_size_limit, brotli_reg_t ntrees) { |
166 | | /* 376 = 256 (1-st level table) + 4 + 7 + 15 + 31 + 63 (2-nd level mix-tables) |
167 | | This number is discovered "unlimited" "enough" calculator; it is actually |
168 | | a wee bigger than required in several cases (especially for alphabets with |
169 | | less than 16 symbols). */ |
170 | 0 | const size_t max_table_size = alphabet_size_limit + 376; |
171 | 0 | const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size; |
172 | 0 | const size_t htree_size = sizeof(HuffmanCode*) * ntrees; |
173 | | /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */ |
174 | 0 | HuffmanCode** p = (HuffmanCode**)BROTLI_DECODER_ALLOC(s, |
175 | 0 | code_size + htree_size); |
176 | 0 | group->alphabet_size_max = (uint16_t)alphabet_size_max; |
177 | 0 | group->alphabet_size_limit = (uint16_t)alphabet_size_limit; |
178 | 0 | group->num_htrees = (uint16_t)ntrees; |
179 | 0 | group->htrees = p; |
180 | 0 | group->codes = p ? (HuffmanCode*)(&p[ntrees]) : NULL; |
181 | 0 | return !!p; |
182 | 0 | } |
183 | | |
184 | | #if defined(__cplusplus) || defined(c_plusplus) |
185 | | } /* extern "C" */ |
186 | | #endif |