]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/BrotliCustomDecompressLib/dec/state.c
MdeModulePkg BrotliLib: Rename function with the specific lib name
[mirror_edk2.git] / MdeModulePkg / Library / BrotliCustomDecompressLib / dec / state.c
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#include "./state.h"\r
8\r
841b2590
SB
9//#include <stdlib.h> /* free, malloc */\r
10#include <BrotliDecompressLibInternal.h>\r
36ff6d80
SB
11\r
12#include "../common/types.h"\r
13#include "./huffman.h"\r
14\r
15#if defined(__cplusplus) || defined(c_plusplus)\r
16extern "C" {\r
17#endif\r
18\r
19static void* DefaultAllocFunc(void* opaque, size_t size) {\r
20 BROTLI_UNUSED(opaque);\r
792ace0a 21 return BrDummyMalloc(size);\r
36ff6d80
SB
22}\r
23\r
24static void DefaultFreeFunc(void* opaque, void* address) {\r
25 BROTLI_UNUSED(opaque);\r
792ace0a 26 BrDummyFree(address);\r
36ff6d80
SB
27}\r
28\r
29void BrotliDecoderStateInit(BrotliDecoderState* s) {\r
30 BrotliDecoderStateInitWithCustomAllocators(s, 0, 0, 0);\r
31}\r
32\r
33void BrotliDecoderStateInitWithCustomAllocators(BrotliDecoderState* s,\r
34 brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {\r
35 if (!alloc_func) {\r
36 s->alloc_func = DefaultAllocFunc;\r
37 s->free_func = DefaultFreeFunc;\r
38 s->memory_manager_opaque = 0;\r
39 } else {\r
40 s->alloc_func = alloc_func;\r
41 s->free_func = free_func;\r
42 s->memory_manager_opaque = opaque;\r
43 }\r
44\r
45 BrotliInitBitReader(&s->br);\r
46 s->state = BROTLI_STATE_UNINITED;\r
47 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;\r
48 s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;\r
49 s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;\r
50 s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE;\r
51 s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;\r
52 s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;\r
53 s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;\r
54\r
55 s->buffer_length = 0;\r
56 s->loop_counter = 0;\r
57 s->pos = 0;\r
58 s->rb_roundtrips = 0;\r
59 s->partial_pos_out = 0;\r
60\r
61 s->block_type_trees = NULL;\r
62 s->block_len_trees = NULL;\r
63 s->ringbuffer = NULL;\r
64\r
65 s->context_map = NULL;\r
66 s->context_modes = NULL;\r
67 s->dist_context_map = NULL;\r
68 s->context_map_slice = NULL;\r
69 s->dist_context_map_slice = NULL;\r
70\r
71 s->sub_loop_counter = 0;\r
72\r
73 s->literal_hgroup.codes = NULL;\r
74 s->literal_hgroup.htrees = NULL;\r
75 s->insert_copy_hgroup.codes = NULL;\r
76 s->insert_copy_hgroup.htrees = NULL;\r
77 s->distance_hgroup.codes = NULL;\r
78 s->distance_hgroup.htrees = NULL;\r
79\r
80 s->custom_dict = NULL;\r
81 s->custom_dict_size = 0;\r
82\r
83 s->is_last_metablock = 0;\r
84 s->window_bits = 0;\r
85 s->max_distance = 0;\r
86 s->dist_rb[0] = 16;\r
87 s->dist_rb[1] = 15;\r
88 s->dist_rb[2] = 11;\r
89 s->dist_rb[3] = 4;\r
90 s->dist_rb_idx = 0;\r
91 s->block_type_trees = NULL;\r
92 s->block_len_trees = NULL;\r
93\r
94 /* Make small negative indexes addressable. */\r
95 s->symbol_lists = &s->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1];\r
96\r
97 s->mtf_upper_bound = 255;\r
98}\r
99\r
100void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {\r
101 s->meta_block_remaining_len = 0;\r
102 s->block_length[0] = 1U << 28;\r
103 s->block_length[1] = 1U << 28;\r
104 s->block_length[2] = 1U << 28;\r
105 s->num_block_types[0] = 1;\r
106 s->num_block_types[1] = 1;\r
107 s->num_block_types[2] = 1;\r
108 s->block_type_rb[0] = 1;\r
109 s->block_type_rb[1] = 0;\r
110 s->block_type_rb[2] = 1;\r
111 s->block_type_rb[3] = 0;\r
112 s->block_type_rb[4] = 1;\r
113 s->block_type_rb[5] = 0;\r
114 s->context_map = NULL;\r
115 s->context_modes = NULL;\r
116 s->dist_context_map = NULL;\r
117 s->context_map_slice = NULL;\r
118 s->literal_htree = NULL;\r
119 s->dist_context_map_slice = NULL;\r
120 s->dist_htree_index = 0;\r
121 s->context_lookup1 = NULL;\r
122 s->context_lookup2 = NULL;\r
123 s->literal_hgroup.codes = NULL;\r
124 s->literal_hgroup.htrees = NULL;\r
125 s->insert_copy_hgroup.codes = NULL;\r
126 s->insert_copy_hgroup.htrees = NULL;\r
127 s->distance_hgroup.codes = NULL;\r
128 s->distance_hgroup.htrees = NULL;\r
129}\r
130\r
131void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) {\r
132 BROTLI_FREE(s, s->context_modes);\r
133 BROTLI_FREE(s, s->context_map);\r
134 BROTLI_FREE(s, s->dist_context_map);\r
135\r
136 BrotliDecoderHuffmanTreeGroupRelease(s, &s->literal_hgroup);\r
137 BrotliDecoderHuffmanTreeGroupRelease(s, &s->insert_copy_hgroup);\r
138 BrotliDecoderHuffmanTreeGroupRelease(s, &s->distance_hgroup);\r
139}\r
140\r
141void BrotliDecoderStateCleanup(BrotliDecoderState* s) {\r
142 BrotliDecoderStateCleanupAfterMetablock(s);\r
143\r
144 BROTLI_FREE(s, s->ringbuffer);\r
145 BROTLI_FREE(s, s->block_type_trees);\r
146}\r
147\r
148void BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,\r
149 HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t ntrees) {\r
150 /* Pack two allocations into one */\r
151 const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5];\r
152 const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;\r
153 const size_t htree_size = sizeof(HuffmanCode*) * ntrees;\r
154 char* p = (char*)BROTLI_ALLOC(s, code_size + htree_size);\r
155 group->alphabet_size = (uint16_t)alphabet_size;\r
156 group->num_htrees = (uint16_t)ntrees;\r
157 group->codes = (HuffmanCode*)p;\r
158 group->htrees = (HuffmanCode**)(p + code_size);\r
159}\r
160\r
161void BrotliDecoderHuffmanTreeGroupRelease(\r
162 BrotliDecoderState* s, HuffmanTreeGroup* group) {\r
163 BROTLI_FREE(s, group->codes);\r
164 group->htrees = NULL;\r
165}\r
166\r
167#if defined(__cplusplus) || defined(c_plusplus)\r
168} /* extern "C" */\r
169#endif\r