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