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 <brotli/types.h> |
12 | |
13 | #include "../common/dictionary.h" |
14 | #include "huffman.h" |
15 | |
16 | #if defined(__cplusplus) || defined(c_plusplus) |
17 | extern "C" { |
18 | #endif |
19 | |
20 | BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s, |
21 | brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) { |
22 | if (!alloc_func) { |
23 | s->alloc_func = BrotliDefaultAllocFunc; |
24 | s->free_func = BrotliDefaultFreeFunc; |
25 | s->memory_manager_opaque = 0; |
26 | } else { |
27 | s->alloc_func = alloc_func; |
28 | s->free_func = free_func; |
29 | s->memory_manager_opaque = opaque; |
30 | } |
31 | |
32 | s->error_code = 0; /* BROTLI_DECODER_NO_ERROR */ |
33 | |
34 | BrotliInitBitReader(&s->br); |
35 | s->state = BROTLI_STATE_UNINITED; |
36 | s->large_window = 0; |
37 | s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; |
38 | s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE; |
39 | s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE; |
40 | s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE; |
41 | |
42 | s->buffer_length = 0; |
43 | s->loop_counter = 0; |
44 | s->pos = 0; |
45 | s->rb_roundtrips = 0; |
46 | s->partial_pos_out = 0; |
47 | s->used_input = 0; |
48 | |
49 | s->block_type_trees = NULL; |
50 | s->block_len_trees = NULL; |
51 | s->ringbuffer = NULL; |
52 | s->ringbuffer_size = 0; |
53 | s->new_ringbuffer_size = 0; |
54 | s->ringbuffer_mask = 0; |
55 | |
56 | s->context_map = NULL; |
57 | s->context_modes = NULL; |
58 | s->dist_context_map = NULL; |
59 | s->context_map_slice = NULL; |
60 | s->dist_context_map_slice = NULL; |
61 | |
62 | s->literal_hgroup.codes = NULL; |
63 | s->literal_hgroup.htrees = NULL; |
64 | s->insert_copy_hgroup.codes = NULL; |
65 | s->insert_copy_hgroup.htrees = NULL; |
66 | s->distance_hgroup.codes = NULL; |
67 | s->distance_hgroup.htrees = NULL; |
68 | |
69 | s->is_last_metablock = 0; |
70 | s->is_uncompressed = 0; |
71 | s->is_metadata = 0; |
72 | s->should_wrap_ringbuffer = 0; |
73 | s->canny_ringbuffer_allocation = 1; |
74 | |
75 | s->window_bits = 0; |
76 | s->max_distance = 0; |
77 | s->dist_rb[0] = 16; |
78 | s->dist_rb[1] = 15; |
79 | s->dist_rb[2] = 11; |
80 | s->dist_rb[3] = 4; |
81 | s->dist_rb_idx = 0; |
82 | s->block_type_trees = NULL; |
83 | s->block_len_trees = NULL; |
84 | |
85 | s->mtf_upper_bound = 63; |
86 | |
87 | s->compound_dictionary = NULL; |
88 | s->dictionary = |
89 | BrotliSharedDictionaryCreateInstance(alloc_func, free_func, opaque); |
90 | if (!s->dictionary) return BROTLI_FALSE; |
91 | |
92 | s->metadata_start_func = NULL; |
93 | s->metadata_chunk_func = NULL; |
94 | s->metadata_callback_opaque = 0; |
95 | |
96 | return BROTLI_TRUE; |
97 | } |
98 | |
99 | void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) { |
100 | s->meta_block_remaining_len = 0; |
101 | s->block_length[0] = 1U << 24; |
102 | s->block_length[1] = 1U << 24; |
103 | s->block_length[2] = 1U << 24; |
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_lookup = NULL; |
121 | s->literal_hgroup.codes = NULL; |
122 | s->literal_hgroup.htrees = NULL; |
123 | s->insert_copy_hgroup.codes = NULL; |
124 | s->insert_copy_hgroup.htrees = NULL; |
125 | s->distance_hgroup.codes = NULL; |
126 | s->distance_hgroup.htrees = NULL; |
127 | } |
128 | |
129 | void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) { |
130 | BROTLI_DECODER_FREE(s, s->context_modes); |
131 | BROTLI_DECODER_FREE(s, s->context_map); |
132 | BROTLI_DECODER_FREE(s, s->dist_context_map); |
133 | BROTLI_DECODER_FREE(s, s->literal_hgroup.htrees); |
134 | BROTLI_DECODER_FREE(s, s->insert_copy_hgroup.htrees); |
135 | BROTLI_DECODER_FREE(s, s->distance_hgroup.htrees); |
136 | } |
137 | |
138 | #ifdef BROTLI_REPORTING |
139 | /* When BROTLI_REPORTING is defined extra reporting module have to be linked. */ |
140 | void BrotliDecoderOnFinish(const BrotliDecoderState* s); |
141 | #define BROTLI_DECODER_ON_FINISH(s) BrotliDecoderOnFinish(s); |
142 | #else |
143 | #if !defined(BROTLI_DECODER_ON_FINISH) |
144 | #define BROTLI_DECODER_ON_FINISH(s) (void)(s); |
145 | #endif |
146 | #endif |
147 | |
148 | void BrotliDecoderStateCleanup(BrotliDecoderState* s) { |
149 | BrotliDecoderStateCleanupAfterMetablock(s); |
150 | |
151 | BROTLI_DECODER_ON_FINISH(s); |
152 | |
153 | BROTLI_DECODER_FREE(s, s->compound_dictionary); |
154 | BrotliSharedDictionaryDestroyInstance(s->dictionary); |
155 | s->dictionary = NULL; |
156 | BROTLI_DECODER_FREE(s, s->ringbuffer); |
157 | BROTLI_DECODER_FREE(s, s->block_type_trees); |
158 | } |
159 | |
160 | BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s, |
161 | HuffmanTreeGroup* group, uint32_t alphabet_size_max, |
162 | uint32_t alphabet_size_limit, uint32_t ntrees) { |
163 | /* 376 = 256 (1-st level table) + 4 + 7 + 15 + 31 + 63 (2-nd level mix-tables) |
164 | This number is discovered "unlimited" "enough" calculator; it is actually |
165 | a wee bigger than required in several cases (especially for alphabets with |
166 | less than 16 symbols). */ |
167 | const size_t max_table_size = alphabet_size_limit + 376; |
168 | const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size; |
169 | const size_t htree_size = sizeof(HuffmanCode*) * ntrees; |
170 | /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */ |
171 | HuffmanCode** p = (HuffmanCode**)BROTLI_DECODER_ALLOC(s, |
172 | code_size + htree_size); |
173 | group->alphabet_size_max = (uint16_t)alphabet_size_max; |
174 | group->alphabet_size_limit = (uint16_t)alphabet_size_limit; |
175 | group->num_htrees = (uint16_t)ntrees; |
176 | group->htrees = p; |
177 | group->codes = (HuffmanCode*)(&p[ntrees]); |
178 | return !!p; |
179 | } |
180 | |
181 | #if defined(__cplusplus) || defined(c_plusplus) |
182 | } /* extern "C" */ |
183 | #endif |
184 | |