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