OLD | NEW |
1 /* Copyright 2015 Google Inc. All Rights Reserved. | 1 /* Copyright 2015 Google Inc. All Rights Reserved. |
2 | 2 |
3 Distributed under MIT license. | 3 Distributed under MIT license. |
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
5 */ | 5 */ |
6 | 6 |
7 #include "./state.h" | 7 #include "./state.h" |
8 | 8 |
9 #include <stdlib.h> /* free, malloc */ | 9 #include <stdlib.h> /* free, malloc */ |
10 | 10 |
| 11 #include <brotli/types.h> |
11 #include "./huffman.h" | 12 #include "./huffman.h" |
12 #include "./types.h" | |
13 | 13 |
14 #if defined(__cplusplus) || defined(c_plusplus) | 14 #if defined(__cplusplus) || defined(c_plusplus) |
15 extern "C" { | 15 extern "C" { |
16 #endif | 16 #endif |
17 | 17 |
18 /* Declared in decode.h */ | |
19 int BrotliStateIsStreamStart(const BrotliState* s); | |
20 int BrotliStateIsStreamEnd(const BrotliState* s); | |
21 | |
22 static void* DefaultAllocFunc(void* opaque, size_t size) { | 18 static void* DefaultAllocFunc(void* opaque, size_t size) { |
23 BROTLI_UNUSED(opaque); | 19 BROTLI_UNUSED(opaque); |
24 return malloc(size); | 20 return malloc(size); |
25 } | 21 } |
26 | 22 |
27 static void DefaultFreeFunc(void* opaque, void* address) { | 23 static void DefaultFreeFunc(void* opaque, void* address) { |
28 BROTLI_UNUSED(opaque); | 24 BROTLI_UNUSED(opaque); |
29 free(address); | 25 free(address); |
30 } | 26 } |
31 | 27 |
32 void BrotliStateInit(BrotliState* s) { | 28 void BrotliDecoderStateInit(BrotliDecoderState* s) { |
33 BrotliStateInitWithCustomAllocators(s, 0, 0, 0); | 29 BrotliDecoderStateInitWithCustomAllocators(s, 0, 0, 0); |
34 } | 30 } |
35 | 31 |
36 void BrotliStateInitWithCustomAllocators(BrotliState* s, | 32 void BrotliDecoderStateInitWithCustomAllocators(BrotliDecoderState* s, |
37 brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) { | 33 brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) { |
38 if (!alloc_func) { | 34 if (!alloc_func) { |
39 s->alloc_func = DefaultAllocFunc; | 35 s->alloc_func = DefaultAllocFunc; |
40 s->free_func = DefaultFreeFunc; | 36 s->free_func = DefaultFreeFunc; |
41 s->memory_manager_opaque = 0; | 37 s->memory_manager_opaque = 0; |
42 } else { | 38 } else { |
43 s->alloc_func = alloc_func; | 39 s->alloc_func = alloc_func; |
44 s->free_func = free_func; | 40 s->free_func = free_func; |
45 s->memory_manager_opaque = opaque; | 41 s->memory_manager_opaque = opaque; |
46 } | 42 } |
(...skipping 10 matching lines...) Expand all Loading... |
57 | 53 |
58 s->buffer_length = 0; | 54 s->buffer_length = 0; |
59 s->loop_counter = 0; | 55 s->loop_counter = 0; |
60 s->pos = 0; | 56 s->pos = 0; |
61 s->rb_roundtrips = 0; | 57 s->rb_roundtrips = 0; |
62 s->partial_pos_out = 0; | 58 s->partial_pos_out = 0; |
63 | 59 |
64 s->block_type_trees = NULL; | 60 s->block_type_trees = NULL; |
65 s->block_len_trees = NULL; | 61 s->block_len_trees = NULL; |
66 s->ringbuffer = NULL; | 62 s->ringbuffer = NULL; |
| 63 s->ringbuffer_size = 0; |
| 64 s->new_ringbuffer_size = 0; |
| 65 s->ringbuffer_mask = 0; |
67 | 66 |
68 s->context_map = NULL; | 67 s->context_map = NULL; |
69 s->context_modes = NULL; | 68 s->context_modes = NULL; |
70 s->dist_context_map = NULL; | 69 s->dist_context_map = NULL; |
71 s->context_map_slice = NULL; | 70 s->context_map_slice = NULL; |
72 s->dist_context_map_slice = NULL; | 71 s->dist_context_map_slice = NULL; |
73 | 72 |
74 s->sub_loop_counter = 0; | 73 s->sub_loop_counter = 0; |
75 | 74 |
76 s->literal_hgroup.codes = NULL; | 75 s->literal_hgroup.codes = NULL; |
77 s->literal_hgroup.htrees = NULL; | 76 s->literal_hgroup.htrees = NULL; |
78 s->insert_copy_hgroup.codes = NULL; | 77 s->insert_copy_hgroup.codes = NULL; |
79 s->insert_copy_hgroup.htrees = NULL; | 78 s->insert_copy_hgroup.htrees = NULL; |
80 s->distance_hgroup.codes = NULL; | 79 s->distance_hgroup.codes = NULL; |
81 s->distance_hgroup.htrees = NULL; | 80 s->distance_hgroup.htrees = NULL; |
82 | 81 |
83 s->custom_dict = NULL; | 82 s->custom_dict = NULL; |
84 s->custom_dict_size = 0; | 83 s->custom_dict_size = 0; |
85 | 84 |
86 s->is_last_metablock = 0; | 85 s->is_last_metablock = 0; |
| 86 s->should_wrap_ringbuffer = 0; |
87 s->window_bits = 0; | 87 s->window_bits = 0; |
88 s->max_distance = 0; | 88 s->max_distance = 0; |
89 s->dist_rb[0] = 16; | 89 s->dist_rb[0] = 16; |
90 s->dist_rb[1] = 15; | 90 s->dist_rb[1] = 15; |
91 s->dist_rb[2] = 11; | 91 s->dist_rb[2] = 11; |
92 s->dist_rb[3] = 4; | 92 s->dist_rb[3] = 4; |
93 s->dist_rb_idx = 0; | 93 s->dist_rb_idx = 0; |
94 s->block_type_trees = NULL; | 94 s->block_type_trees = NULL; |
95 s->block_len_trees = NULL; | 95 s->block_len_trees = NULL; |
96 | 96 |
97 /* Make small negative indexes addressable. */ | 97 /* Make small negative indexes addressable. */ |
98 s->symbol_lists = &s->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1]; | 98 s->symbol_lists = &s->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1]; |
99 | 99 |
100 s->mtf_upper_bound = 255; | 100 s->mtf_upper_bound = 63; |
101 } | 101 } |
102 | 102 |
103 void BrotliStateMetablockBegin(BrotliState* s) { | 103 void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) { |
104 s->meta_block_remaining_len = 0; | 104 s->meta_block_remaining_len = 0; |
105 s->block_length[0] = 1U << 28; | 105 s->block_length[0] = 1U << 28; |
106 s->block_length[1] = 1U << 28; | 106 s->block_length[1] = 1U << 28; |
107 s->block_length[2] = 1U << 28; | 107 s->block_length[2] = 1U << 28; |
108 s->num_block_types[0] = 1; | 108 s->num_block_types[0] = 1; |
109 s->num_block_types[1] = 1; | 109 s->num_block_types[1] = 1; |
110 s->num_block_types[2] = 1; | 110 s->num_block_types[2] = 1; |
111 s->block_type_rb[0] = 1; | 111 s->block_type_rb[0] = 1; |
112 s->block_type_rb[1] = 0; | 112 s->block_type_rb[1] = 0; |
113 s->block_type_rb[2] = 1; | 113 s->block_type_rb[2] = 1; |
114 s->block_type_rb[3] = 0; | 114 s->block_type_rb[3] = 0; |
115 s->block_type_rb[4] = 1; | 115 s->block_type_rb[4] = 1; |
116 s->block_type_rb[5] = 0; | 116 s->block_type_rb[5] = 0; |
117 s->context_map = NULL; | 117 s->context_map = NULL; |
118 s->context_modes = NULL; | 118 s->context_modes = NULL; |
119 s->dist_context_map = NULL; | 119 s->dist_context_map = NULL; |
120 s->context_map_slice = NULL; | 120 s->context_map_slice = NULL; |
121 s->literal_htree_index = 0; | |
122 s->literal_htree = NULL; | 121 s->literal_htree = NULL; |
123 s->dist_context_map_slice = NULL; | 122 s->dist_context_map_slice = NULL; |
124 s->dist_htree_index = 0; | 123 s->dist_htree_index = 0; |
125 s->context_lookup1 = NULL; | 124 s->context_lookup1 = NULL; |
126 s->context_lookup2 = NULL; | 125 s->context_lookup2 = NULL; |
127 s->literal_hgroup.codes = NULL; | 126 s->literal_hgroup.codes = NULL; |
128 s->literal_hgroup.htrees = NULL; | 127 s->literal_hgroup.htrees = NULL; |
129 s->insert_copy_hgroup.codes = NULL; | 128 s->insert_copy_hgroup.codes = NULL; |
130 s->insert_copy_hgroup.htrees = NULL; | 129 s->insert_copy_hgroup.htrees = NULL; |
131 s->distance_hgroup.codes = NULL; | 130 s->distance_hgroup.codes = NULL; |
132 s->distance_hgroup.htrees = NULL; | 131 s->distance_hgroup.htrees = NULL; |
133 } | 132 } |
134 | 133 |
135 void BrotliStateCleanupAfterMetablock(BrotliState* s) { | 134 void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) { |
136 BROTLI_FREE(s, s->context_modes); | 135 BROTLI_FREE(s, s->context_modes); |
137 BROTLI_FREE(s, s->context_map); | 136 BROTLI_FREE(s, s->context_map); |
138 BROTLI_FREE(s, s->dist_context_map); | 137 BROTLI_FREE(s, s->dist_context_map); |
139 | 138 |
140 BrotliHuffmanTreeGroupRelease(s, &s->literal_hgroup); | 139 BrotliDecoderHuffmanTreeGroupRelease(s, &s->literal_hgroup); |
141 BrotliHuffmanTreeGroupRelease(s, &s->insert_copy_hgroup); | 140 BrotliDecoderHuffmanTreeGroupRelease(s, &s->insert_copy_hgroup); |
142 BrotliHuffmanTreeGroupRelease(s, &s->distance_hgroup); | 141 BrotliDecoderHuffmanTreeGroupRelease(s, &s->distance_hgroup); |
143 } | 142 } |
144 | 143 |
145 void BrotliStateCleanup(BrotliState* s) { | 144 void BrotliDecoderStateCleanup(BrotliDecoderState* s) { |
146 BrotliStateCleanupAfterMetablock(s); | 145 BrotliDecoderStateCleanupAfterMetablock(s); |
147 | 146 |
148 BROTLI_FREE(s, s->ringbuffer); | 147 BROTLI_FREE(s, s->ringbuffer); |
149 BROTLI_FREE(s, s->block_type_trees); | 148 BROTLI_FREE(s, s->block_type_trees); |
150 } | 149 } |
151 | 150 |
152 int BrotliStateIsStreamStart(const BrotliState* s) { | 151 BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s, |
153 return (s->state == BROTLI_STATE_UNINITED && | 152 HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t ntrees) { |
154 BrotliGetAvailableBits(&s->br) == 0); | |
155 } | |
156 | |
157 int BrotliStateIsStreamEnd(const BrotliState* s) { | |
158 return s->state == BROTLI_STATE_DONE; | |
159 } | |
160 | |
161 void BrotliHuffmanTreeGroupInit(BrotliState* s, HuffmanTreeGroup* group, | |
162 uint32_t alphabet_size, uint32_t ntrees) { | |
163 /* Pack two allocations into one */ | 153 /* Pack two allocations into one */ |
164 const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5]; | 154 const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5]; |
165 const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size; | 155 const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size; |
166 const size_t htree_size = sizeof(HuffmanCode*) * ntrees; | 156 const size_t htree_size = sizeof(HuffmanCode*) * ntrees; |
167 char* p = (char*)BROTLI_ALLOC(s, code_size + htree_size); | 157 /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */ |
| 158 HuffmanCode** p = (HuffmanCode**)BROTLI_ALLOC(s, code_size + htree_size); |
168 group->alphabet_size = (uint16_t)alphabet_size; | 159 group->alphabet_size = (uint16_t)alphabet_size; |
169 group->num_htrees = (uint16_t)ntrees; | 160 group->num_htrees = (uint16_t)ntrees; |
170 group->codes = (HuffmanCode*)p; | 161 group->htrees = (HuffmanCode**)p; |
171 group->htrees = (HuffmanCode**)(p + code_size); | 162 group->codes = (HuffmanCode*)(&p[ntrees]); |
| 163 return !!p; |
172 } | 164 } |
173 | 165 |
174 void BrotliHuffmanTreeGroupRelease(BrotliState* s, HuffmanTreeGroup* group) { | 166 void BrotliDecoderHuffmanTreeGroupRelease( |
175 BROTLI_FREE(s, group->codes); | 167 BrotliDecoderState* s, HuffmanTreeGroup* group) { |
| 168 BROTLI_FREE(s, group->htrees); |
176 group->htrees = NULL; | 169 group->htrees = NULL; |
177 } | 170 } |
178 | 171 |
179 #if defined(__cplusplus) || defined(c_plusplus) | 172 #if defined(__cplusplus) || defined(c_plusplus) |
180 } /* extern "C" */ | 173 } /* extern "C" */ |
181 #endif | 174 #endif |
OLD | NEW |