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