OLD | NEW |
1 /* Copyright 2015 Google Inc. All Rights Reserved. | 1 /* Copyright 2015 Google Inc. All Rights Reserved. |
2 | 2 |
3 Distributed under MIT license. | 3 Distributed under MIT license. |
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
5 */ | 5 */ |
6 | 6 |
7 /* Brotli state for partial streaming decoding. */ | 7 /* Brotli state for partial streaming decoding. */ |
8 | 8 |
9 #ifndef BROTLI_DEC_STATE_H_ | 9 #ifndef BROTLI_DEC_STATE_H_ |
10 #define BROTLI_DEC_STATE_H_ | 10 #define BROTLI_DEC_STATE_H_ |
11 | 11 |
| 12 #include "../common/constants.h" |
| 13 #include <brotli/types.h> |
12 #include "./bit_reader.h" | 14 #include "./bit_reader.h" |
13 #include "./huffman.h" | 15 #include "./huffman.h" |
14 #include "./types.h" | |
15 #include "./port.h" | 16 #include "./port.h" |
16 | 17 |
17 #if defined(__cplusplus) || defined(c_plusplus) | 18 #if defined(__cplusplus) || defined(c_plusplus) |
18 extern "C" { | 19 extern "C" { |
19 #endif | 20 #endif |
20 | 21 |
21 typedef enum { | 22 typedef enum { |
22 BROTLI_STATE_UNINITED, | 23 BROTLI_STATE_UNINITED, |
23 BROTLI_STATE_METABLOCK_BEGIN, | 24 BROTLI_STATE_METABLOCK_BEGIN, |
24 BROTLI_STATE_METABLOCK_HEADER, | 25 BROTLI_STATE_METABLOCK_HEADER, |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 BROTLI_STATE_DECODE_UINT8_NONE, | 87 BROTLI_STATE_DECODE_UINT8_NONE, |
87 BROTLI_STATE_DECODE_UINT8_SHORT, | 88 BROTLI_STATE_DECODE_UINT8_SHORT, |
88 BROTLI_STATE_DECODE_UINT8_LONG | 89 BROTLI_STATE_DECODE_UINT8_LONG |
89 } BrotliRunningDecodeUint8State; | 90 } BrotliRunningDecodeUint8State; |
90 | 91 |
91 typedef enum { | 92 typedef enum { |
92 BROTLI_STATE_READ_BLOCK_LENGTH_NONE, | 93 BROTLI_STATE_READ_BLOCK_LENGTH_NONE, |
93 BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX | 94 BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX |
94 } BrotliRunningReadBlockLengthState; | 95 } BrotliRunningReadBlockLengthState; |
95 | 96 |
96 struct BrotliStateStruct { | 97 struct BrotliDecoderStateStruct { |
97 BrotliRunningState state; | 98 BrotliRunningState state; |
98 | 99 |
99 /* This counter is reused for several disjoint loops. */ | 100 /* This counter is reused for several disjoint loops. */ |
100 int loop_counter; | 101 int loop_counter; |
101 | 102 |
102 BrotliBitReader br; | 103 BrotliBitReader br; |
103 | 104 |
104 brotli_alloc_func alloc_func; | 105 brotli_alloc_func alloc_func; |
105 brotli_free_func free_func; | 106 brotli_free_func free_func; |
106 void* memory_manager_opaque; | 107 void* memory_manager_opaque; |
107 | 108 |
108 /* Temporary storage for remaining input. */ | 109 /* Temporary storage for remaining input. */ |
109 union { | 110 union { |
110 uint64_t u64; | 111 uint64_t u64; |
111 uint8_t u8[8]; | 112 uint8_t u8[8]; |
112 } buffer; | 113 } buffer; |
113 uint32_t buffer_length; | 114 uint32_t buffer_length; |
114 | 115 |
115 int pos; | 116 int pos; |
116 int max_backward_distance; | 117 int max_backward_distance; |
117 int max_backward_distance_minus_custom_dict_size; | |
118 int max_distance; | 118 int max_distance; |
119 int ringbuffer_size; | 119 int ringbuffer_size; |
120 int ringbuffer_mask; | 120 int ringbuffer_mask; |
121 int dist_rb_idx; | 121 int dist_rb_idx; |
122 int dist_rb[4]; | 122 int dist_rb[4]; |
123 int error_code; | 123 int error_code; |
124 uint32_t sub_loop_counter; | 124 uint32_t sub_loop_counter; |
125 uint8_t* ringbuffer; | 125 uint8_t* ringbuffer; |
126 uint8_t* ringbuffer_end; | 126 uint8_t* ringbuffer_end; |
127 HuffmanCode* htree_command; | 127 HuffmanCode* htree_command; |
(...skipping 17 matching lines...) Expand all Loading... |
145 uint32_t block_length_index; | 145 uint32_t block_length_index; |
146 uint32_t block_length[3]; | 146 uint32_t block_length[3]; |
147 uint32_t num_block_types[3]; | 147 uint32_t num_block_types[3]; |
148 uint32_t block_type_rb[6]; | 148 uint32_t block_type_rb[6]; |
149 uint32_t distance_postfix_bits; | 149 uint32_t distance_postfix_bits; |
150 uint32_t num_direct_distance_codes; | 150 uint32_t num_direct_distance_codes; |
151 int distance_postfix_mask; | 151 int distance_postfix_mask; |
152 uint32_t num_dist_htrees; | 152 uint32_t num_dist_htrees; |
153 uint8_t* dist_context_map; | 153 uint8_t* dist_context_map; |
154 HuffmanCode* literal_htree; | 154 HuffmanCode* literal_htree; |
155 uint8_t literal_htree_index; | |
156 uint8_t dist_htree_index; | 155 uint8_t dist_htree_index; |
157 uint32_t repeat_code_len; | 156 uint32_t repeat_code_len; |
158 uint32_t prev_code_len; | 157 uint32_t prev_code_len; |
159 | 158 |
160 int copy_length; | 159 int copy_length; |
161 int distance_code; | 160 int distance_code; |
162 | 161 |
163 /* For partial write operations */ | 162 /* For partial write operations */ |
164 size_t rb_roundtrips; /* How many times we went around the ringbuffer */ | 163 size_t rb_roundtrips; /* How many times we went around the ring-buffer */ |
165 size_t partial_pos_out; /* How much output to the user in total (<= rb) */ | 164 size_t partial_pos_out; /* How much output to the user in total */ |
166 | 165 |
167 /* For ReadHuffmanCode */ | 166 /* For ReadHuffmanCode */ |
168 uint32_t symbol; | 167 uint32_t symbol; |
169 uint32_t repeat; | 168 uint32_t repeat; |
170 uint32_t space; | 169 uint32_t space; |
171 | 170 |
172 HuffmanCode table[32]; | 171 HuffmanCode table[32]; |
173 /* List of of symbol chains. */ | 172 /* List of of symbol chains. */ |
174 uint16_t* symbol_lists; | 173 uint16_t* symbol_lists; |
175 /* Storage from symbol_lists. */ | 174 /* Storage from symbol_lists. */ |
176 uint16_t symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1 + | 175 uint16_t symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1 + |
177 BROTLI_HUFFMAN_MAX_CODE_LENGTHS_SIZE]; | 176 BROTLI_NUM_COMMAND_SYMBOLS]; |
178 /* Tails of symbol chains. */ | 177 /* Tails of symbol chains. */ |
179 int next_symbol[32]; | 178 int next_symbol[32]; |
180 uint8_t code_length_code_lengths[18]; | 179 uint8_t code_length_code_lengths[BROTLI_CODE_LENGTH_CODES]; |
181 /* Population counts for the code lengths */ | 180 /* Population counts for the code lengths */ |
182 uint16_t code_length_histo[16]; | 181 uint16_t code_length_histo[16]; |
183 | 182 |
184 /* For HuffmanTreeGroupDecode */ | 183 /* For HuffmanTreeGroupDecode */ |
185 int htree_index; | 184 int htree_index; |
186 HuffmanCode* next; | 185 HuffmanCode* next; |
187 | 186 |
188 /* For DecodeContextMap */ | 187 /* For DecodeContextMap */ |
189 uint32_t context_index; | 188 uint32_t context_index; |
190 uint32_t max_run_length_prefix; | 189 uint32_t max_run_length_prefix; |
191 uint32_t code; | 190 uint32_t code; |
192 HuffmanCode context_map_table[BROTLI_HUFFMAN_MAX_SIZE_272]; | 191 HuffmanCode context_map_table[BROTLI_HUFFMAN_MAX_SIZE_272]; |
193 | 192 |
194 /* For InverseMoveToFrontTransform */ | 193 /* For InverseMoveToFrontTransform */ |
195 uint32_t mtf_upper_bound; | 194 uint32_t mtf_upper_bound; |
196 uint8_t mtf[256 + 4]; | 195 uint32_t mtf[64 + 1]; |
197 | 196 |
198 /* For custom dictionaries */ | 197 /* For custom dictionaries */ |
199 const uint8_t* custom_dict; | 198 const uint8_t* custom_dict; |
200 int custom_dict_size; | 199 int custom_dict_size; |
201 | 200 |
202 /* less used attributes are in the end of this struct */ | 201 /* less used attributes are in the end of this struct */ |
203 /* States inside function calls */ | 202 /* States inside function calls */ |
204 BrotliRunningMetablockHeaderState substate_metablock_header; | 203 BrotliRunningMetablockHeaderState substate_metablock_header; |
205 BrotliRunningTreeGroupState substate_tree_group; | 204 BrotliRunningTreeGroupState substate_tree_group; |
206 BrotliRunningContextMapState substate_context_map; | 205 BrotliRunningContextMapState substate_context_map; |
207 BrotliRunningUncompressedState substate_uncompressed; | 206 BrotliRunningUncompressedState substate_uncompressed; |
208 BrotliRunningHuffmanState substate_huffman; | 207 BrotliRunningHuffmanState substate_huffman; |
209 BrotliRunningDecodeUint8State substate_decode_uint8; | 208 BrotliRunningDecodeUint8State substate_decode_uint8; |
210 BrotliRunningReadBlockLengthState substate_read_block_length; | 209 BrotliRunningReadBlockLengthState substate_read_block_length; |
211 | 210 |
212 uint8_t is_last_metablock; | 211 unsigned int is_last_metablock : 1; |
213 uint8_t is_uncompressed; | 212 unsigned int is_uncompressed : 1; |
214 uint8_t is_metadata; | 213 unsigned int is_metadata : 1; |
215 uint8_t size_nibbles; | 214 unsigned int should_wrap_ringbuffer : 1; |
| 215 unsigned int size_nibbles : 8; |
216 uint32_t window_bits; | 216 uint32_t window_bits; |
217 | 217 |
| 218 int new_ringbuffer_size; |
| 219 |
218 uint32_t num_literal_htrees; | 220 uint32_t num_literal_htrees; |
219 uint8_t* context_map; | 221 uint8_t* context_map; |
220 uint8_t* context_modes; | 222 uint8_t* context_modes; |
| 223 |
| 224 uint32_t trivial_literal_contexts[8]; /* 256 bits */ |
221 }; | 225 }; |
222 | 226 |
223 typedef struct BrotliStateStruct BrotliStateInternal; | 227 typedef struct BrotliDecoderStateStruct BrotliDecoderStateInternal; |
224 #define BrotliState BrotliStateInternal | 228 #define BrotliDecoderState BrotliDecoderStateInternal |
225 | 229 |
226 BROTLI_INTERNAL void BrotliStateInit(BrotliState* s); | 230 BROTLI_INTERNAL void BrotliDecoderStateInit(BrotliDecoderState* s); |
227 BROTLI_INTERNAL void BrotliStateInitWithCustomAllocators(BrotliState* s, | 231 BROTLI_INTERNAL void BrotliDecoderStateInitWithCustomAllocators( |
228 brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque); | 232 BrotliDecoderState* s, brotli_alloc_func alloc_func, |
229 BROTLI_INTERNAL void BrotliStateCleanup(BrotliState* s); | 233 brotli_free_func free_func, void* opaque); |
230 BROTLI_INTERNAL void BrotliStateMetablockBegin(BrotliState* s); | 234 BROTLI_INTERNAL void BrotliDecoderStateCleanup(BrotliDecoderState* s); |
231 BROTLI_INTERNAL void BrotliStateCleanupAfterMetablock(BrotliState* s); | 235 BROTLI_INTERNAL void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s); |
232 BROTLI_INTERNAL void BrotliHuffmanTreeGroupInit(BrotliState* s, | 236 BROTLI_INTERNAL void BrotliDecoderStateCleanupAfterMetablock( |
233 HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t ntrees); | 237 BrotliDecoderState* s); |
234 BROTLI_INTERNAL void BrotliHuffmanTreeGroupRelease(BrotliState* s, | 238 BROTLI_INTERNAL BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit( |
235 HuffmanTreeGroup* group); | 239 BrotliDecoderState* s, HuffmanTreeGroup* group, uint32_t alphabet_size, |
| 240 uint32_t ntrees); |
| 241 BROTLI_INTERNAL void BrotliDecoderHuffmanTreeGroupRelease( |
| 242 BrotliDecoderState* s, HuffmanTreeGroup* group); |
236 | 243 |
237 #if defined(__cplusplus) || defined(c_plusplus) | 244 #if defined(__cplusplus) || defined(c_plusplus) |
238 } /* extern "C" */ | 245 } /* extern "C" */ |
239 #endif | 246 #endif |
240 | 247 |
241 #endif /* BROTLI_DEC_STATE_H_ */ | 248 #endif /* BROTLI_DEC_STATE_H_ */ |
OLD | NEW |