MdeModulePkg BrotliLib: Fix the regression logic issue in loop
[mirror_edk2.git] / MdeModulePkg / Library / BrotliCustomDecompressLib / dec / state.h
CommitLineData
36ff6d80
SB
1/* Copyright 2015 Google Inc. All Rights Reserved.\r
2\r
3 Distributed under MIT license.\r
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT\r
5*/\r
6\r
7/* Brotli state for partial streaming decoding. */\r
8\r
9#ifndef BROTLI_DEC_STATE_H_\r
10#define BROTLI_DEC_STATE_H_\r
11\r
12#include "../common/constants.h"\r
13#include "../common/types.h"\r
14#include "./bit_reader.h"\r
15#include "./huffman.h"\r
16#include "./port.h"\r
17\r
18#if defined(__cplusplus) || defined(c_plusplus)\r
19extern "C" {\r
20#endif\r
21\r
22typedef enum {\r
23 BROTLI_STATE_UNINITED,\r
24 BROTLI_STATE_METABLOCK_BEGIN,\r
25 BROTLI_STATE_METABLOCK_HEADER,\r
26 BROTLI_STATE_METABLOCK_HEADER_2,\r
27 BROTLI_STATE_CONTEXT_MODES,\r
28 BROTLI_STATE_COMMAND_BEGIN,\r
29 BROTLI_STATE_COMMAND_INNER,\r
30 BROTLI_STATE_COMMAND_POST_DECODE_LITERALS,\r
31 BROTLI_STATE_COMMAND_POST_WRAP_COPY,\r
32 BROTLI_STATE_UNCOMPRESSED,\r
33 BROTLI_STATE_METADATA,\r
34 BROTLI_STATE_COMMAND_INNER_WRITE,\r
35 BROTLI_STATE_METABLOCK_DONE,\r
36 BROTLI_STATE_COMMAND_POST_WRITE_1,\r
37 BROTLI_STATE_COMMAND_POST_WRITE_2,\r
38 BROTLI_STATE_HUFFMAN_CODE_0,\r
39 BROTLI_STATE_HUFFMAN_CODE_1,\r
40 BROTLI_STATE_HUFFMAN_CODE_2,\r
41 BROTLI_STATE_HUFFMAN_CODE_3,\r
42 BROTLI_STATE_CONTEXT_MAP_1,\r
43 BROTLI_STATE_CONTEXT_MAP_2,\r
44 BROTLI_STATE_TREE_GROUP,\r
45 BROTLI_STATE_DONE\r
46} BrotliRunningState;\r
47\r
48typedef enum {\r
49 BROTLI_STATE_METABLOCK_HEADER_NONE,\r
50 BROTLI_STATE_METABLOCK_HEADER_EMPTY,\r
51 BROTLI_STATE_METABLOCK_HEADER_NIBBLES,\r
52 BROTLI_STATE_METABLOCK_HEADER_SIZE,\r
53 BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED,\r
54 BROTLI_STATE_METABLOCK_HEADER_RESERVED,\r
55 BROTLI_STATE_METABLOCK_HEADER_BYTES,\r
56 BROTLI_STATE_METABLOCK_HEADER_METADATA\r
57} BrotliRunningMetablockHeaderState;\r
58\r
59typedef enum {\r
60 BROTLI_STATE_UNCOMPRESSED_NONE,\r
61 BROTLI_STATE_UNCOMPRESSED_WRITE\r
62} BrotliRunningUncompressedState;\r
63\r
64typedef enum {\r
65 BROTLI_STATE_TREE_GROUP_NONE,\r
66 BROTLI_STATE_TREE_GROUP_LOOP\r
67} BrotliRunningTreeGroupState;\r
68\r
69typedef enum {\r
70 BROTLI_STATE_CONTEXT_MAP_NONE,\r
71 BROTLI_STATE_CONTEXT_MAP_READ_PREFIX,\r
72 BROTLI_STATE_CONTEXT_MAP_HUFFMAN,\r
73 BROTLI_STATE_CONTEXT_MAP_DECODE,\r
74 BROTLI_STATE_CONTEXT_MAP_TRANSFORM\r
75} BrotliRunningContextMapState;\r
76\r
77typedef enum {\r
78 BROTLI_STATE_HUFFMAN_NONE,\r
79 BROTLI_STATE_HUFFMAN_SIMPLE_SIZE,\r
80 BROTLI_STATE_HUFFMAN_SIMPLE_READ,\r
81 BROTLI_STATE_HUFFMAN_SIMPLE_BUILD,\r
82 BROTLI_STATE_HUFFMAN_COMPLEX,\r
83 BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS\r
84} BrotliRunningHuffmanState;\r
85\r
86typedef enum {\r
87 BROTLI_STATE_DECODE_UINT8_NONE,\r
88 BROTLI_STATE_DECODE_UINT8_SHORT,\r
89 BROTLI_STATE_DECODE_UINT8_LONG\r
90} BrotliRunningDecodeUint8State;\r
91\r
92typedef enum {\r
93 BROTLI_STATE_READ_BLOCK_LENGTH_NONE,\r
94 BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX\r
95} BrotliRunningReadBlockLengthState;\r
96\r
97struct BrotliDecoderStateStruct {\r
98 BrotliRunningState state;\r
99\r
100 /* This counter is reused for several disjoint loops. */\r
101 int loop_counter;\r
102\r
103 BrotliBitReader br;\r
104\r
105 brotli_alloc_func alloc_func;\r
106 brotli_free_func free_func;\r
107 void* memory_manager_opaque;\r
108\r
109 /* Temporary storage for remaining input. */\r
110 union {\r
111 uint64_t u64;\r
112 uint8_t u8[8];\r
113 } buffer;\r
114 uint32_t buffer_length;\r
115\r
116 int pos;\r
117 int max_backward_distance;\r
118 int max_backward_distance_minus_custom_dict_size;\r
119 int max_distance;\r
120 int ringbuffer_size;\r
121 int ringbuffer_mask;\r
122 int dist_rb_idx;\r
123 int dist_rb[4];\r
124 int error_code;\r
125 uint32_t sub_loop_counter;\r
126 uint8_t* ringbuffer;\r
127 uint8_t* ringbuffer_end;\r
128 HuffmanCode* htree_command;\r
129 const uint8_t* context_lookup1;\r
130 const uint8_t* context_lookup2;\r
131 uint8_t* context_map_slice;\r
132 uint8_t* dist_context_map_slice;\r
133\r
134 /* This ring buffer holds a few past copy distances that will be used by */\r
135 /* some special distance codes. */\r
136 HuffmanTreeGroup literal_hgroup;\r
137 HuffmanTreeGroup insert_copy_hgroup;\r
138 HuffmanTreeGroup distance_hgroup;\r
139 HuffmanCode* block_type_trees;\r
140 HuffmanCode* block_len_trees;\r
141 /* This is true if the literal context map histogram type always matches the\r
142 block type. It is then not needed to keep the context (faster decoding). */\r
143 int trivial_literal_context;\r
144 int distance_context;\r
145 int meta_block_remaining_len;\r
146 uint32_t block_length_index;\r
147 uint32_t block_length[3];\r
148 uint32_t num_block_types[3];\r
149 uint32_t block_type_rb[6];\r
150 uint32_t distance_postfix_bits;\r
151 uint32_t num_direct_distance_codes;\r
152 int distance_postfix_mask;\r
153 uint32_t num_dist_htrees;\r
154 uint8_t* dist_context_map;\r
155 HuffmanCode* literal_htree;\r
156 uint8_t dist_htree_index;\r
157 uint32_t repeat_code_len;\r
158 uint32_t prev_code_len;\r
159\r
160 int copy_length;\r
161 int distance_code;\r
162\r
163 /* For partial write operations */\r
164 size_t rb_roundtrips; /* How many times we went around the ringbuffer */\r
165 size_t partial_pos_out; /* How much output to the user in total (<= rb) */\r
166\r
167 /* For ReadHuffmanCode */\r
168 uint32_t symbol;\r
169 uint32_t repeat;\r
170 uint32_t space;\r
171\r
172 HuffmanCode table[32];\r
173 /* List of of symbol chains. */\r
174 uint16_t* symbol_lists;\r
175 /* Storage from symbol_lists. */\r
176 uint16_t symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1 +\r
177 BROTLI_NUM_COMMAND_SYMBOLS];\r
178 /* Tails of symbol chains. */\r
179 int next_symbol[32];\r
180 uint8_t code_length_code_lengths[BROTLI_CODE_LENGTH_CODES];\r
181 /* Population counts for the code lengths */\r
182 uint16_t code_length_histo[16];\r
183\r
184 /* For HuffmanTreeGroupDecode */\r
185 int htree_index;\r
186 HuffmanCode* next;\r
187\r
188 /* For DecodeContextMap */\r
189 uint32_t context_index;\r
190 uint32_t max_run_length_prefix;\r
191 uint32_t code;\r
192 HuffmanCode context_map_table[BROTLI_HUFFMAN_MAX_SIZE_272];\r
193\r
194 /* For InverseMoveToFrontTransform */\r
195 uint32_t mtf_upper_bound;\r
196 uint8_t mtf[256 + 4];\r
197\r
198 /* For custom dictionaries */\r
199 const uint8_t* custom_dict;\r
200 int custom_dict_size;\r
201\r
202 /* less used attributes are in the end of this struct */\r
203 /* States inside function calls */\r
204 BrotliRunningMetablockHeaderState substate_metablock_header;\r
205 BrotliRunningTreeGroupState substate_tree_group;\r
206 BrotliRunningContextMapState substate_context_map;\r
207 BrotliRunningUncompressedState substate_uncompressed;\r
208 BrotliRunningHuffmanState substate_huffman;\r
209 BrotliRunningDecodeUint8State substate_decode_uint8;\r
210 BrotliRunningReadBlockLengthState substate_read_block_length;\r
211\r
212 uint8_t is_last_metablock;\r
213 uint8_t is_uncompressed;\r
214 uint8_t is_metadata;\r
215 uint8_t size_nibbles;\r
216 uint32_t window_bits;\r
217\r
218 uint32_t num_literal_htrees;\r
219 uint8_t* context_map;\r
220 uint8_t* context_modes;\r
221\r
222 uint32_t trivial_literal_contexts[8]; /* 256 bits */\r
223};\r
224\r
225typedef struct BrotliDecoderStateStruct BrotliDecoderStateInternal;\r
226#define BrotliDecoderState BrotliDecoderStateInternal\r
227\r
228BROTLI_INTERNAL void BrotliDecoderStateInit(BrotliDecoderState* s);\r
229BROTLI_INTERNAL void BrotliDecoderStateInitWithCustomAllocators(\r
230 BrotliDecoderState* s, brotli_alloc_func alloc_func,\r
231 brotli_free_func free_func, void* opaque);\r
232BROTLI_INTERNAL void BrotliDecoderStateCleanup(BrotliDecoderState* s);\r
233BROTLI_INTERNAL void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s);\r
234BROTLI_INTERNAL void BrotliDecoderStateCleanupAfterMetablock(\r
235 BrotliDecoderState* s);\r
236BROTLI_INTERNAL void BrotliDecoderHuffmanTreeGroupInit(\r
237 BrotliDecoderState* s, HuffmanTreeGroup* group, uint32_t alphabet_size,\r
238 uint32_t ntrees);\r
239BROTLI_INTERNAL void BrotliDecoderHuffmanTreeGroupRelease(\r
240 BrotliDecoderState* s, HuffmanTreeGroup* group);\r
241\r
242#if defined(__cplusplus) || defined(c_plusplus)\r
243} /* extern "C" */\r
244#endif\r
245\r
246#endif /* BROTLI_DEC_STATE_H_ */\r