1 /* Copyright 2015 Google Inc. All Rights Reserved.
3 Distributed under MIT license.
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
9 //#include <stdlib.h> /* free, malloc */
11 #include <brotli/types.h>
12 #include "./huffman.h"
14 #if defined(__cplusplus) || defined(c_plusplus)
18 BROTLI_BOOL
BrotliDecoderStateInit(BrotliDecoderState
* s
,
19 brotli_alloc_func alloc_func
, brotli_free_func free_func
, void* opaque
) {
21 s
->alloc_func
= BrotliDefaultAllocFunc
;
22 s
->free_func
= BrotliDefaultFreeFunc
;
23 s
->memory_manager_opaque
= 0;
25 s
->alloc_func
= alloc_func
;
26 s
->free_func
= free_func
;
27 s
->memory_manager_opaque
= opaque
;
30 s
->error_code
= 0; /* BROTLI_DECODER_NO_ERROR */
32 BrotliInitBitReader(&s
->br
);
33 s
->state
= BROTLI_STATE_UNINITED
;
35 s
->substate_metablock_header
= BROTLI_STATE_METABLOCK_HEADER_NONE
;
36 s
->substate_tree_group
= BROTLI_STATE_TREE_GROUP_NONE
;
37 s
->substate_context_map
= BROTLI_STATE_CONTEXT_MAP_NONE
;
38 s
->substate_uncompressed
= BROTLI_STATE_UNCOMPRESSED_NONE
;
39 s
->substate_huffman
= BROTLI_STATE_HUFFMAN_NONE
;
40 s
->substate_decode_uint8
= BROTLI_STATE_DECODE_UINT8_NONE
;
41 s
->substate_read_block_length
= BROTLI_STATE_READ_BLOCK_LENGTH_NONE
;
47 s
->partial_pos_out
= 0;
49 s
->block_type_trees
= NULL
;
50 s
->block_len_trees
= NULL
;
52 s
->ringbuffer_size
= 0;
53 s
->new_ringbuffer_size
= 0;
54 s
->ringbuffer_mask
= 0;
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
;
62 s
->sub_loop_counter
= 0;
64 s
->literal_hgroup
.codes
= NULL
;
65 s
->literal_hgroup
.htrees
= NULL
;
66 s
->insert_copy_hgroup
.codes
= NULL
;
67 s
->insert_copy_hgroup
.htrees
= NULL
;
68 s
->distance_hgroup
.codes
= NULL
;
69 s
->distance_hgroup
.htrees
= NULL
;
71 s
->is_last_metablock
= 0;
72 s
->is_uncompressed
= 0;
74 s
->should_wrap_ringbuffer
= 0;
75 s
->canny_ringbuffer_allocation
= 1;
84 s
->block_type_trees
= NULL
;
85 s
->block_len_trees
= NULL
;
87 /* Make small negative indexes addressable. */
88 s
->symbol_lists
= &s
->symbols_lists_array
[BROTLI_HUFFMAN_MAX_CODE_LENGTH
+ 1];
90 s
->mtf_upper_bound
= 63;
92 s
->dictionary
= BrotliGetDictionary();
93 s
->transforms
= BrotliGetTransforms();
98 void BrotliDecoderStateMetablockBegin(BrotliDecoderState
* s
) {
99 s
->meta_block_remaining_len
= 0;
100 s
->block_length
[0] = 1U << 24;
101 s
->block_length
[1] = 1U << 24;
102 s
->block_length
[2] = 1U << 24;
103 s
->num_block_types
[0] = 1;
104 s
->num_block_types
[1] = 1;
105 s
->num_block_types
[2] = 1;
106 s
->block_type_rb
[0] = 1;
107 s
->block_type_rb
[1] = 0;
108 s
->block_type_rb
[2] = 1;
109 s
->block_type_rb
[3] = 0;
110 s
->block_type_rb
[4] = 1;
111 s
->block_type_rb
[5] = 0;
112 s
->context_map
= NULL
;
113 s
->context_modes
= NULL
;
114 s
->dist_context_map
= NULL
;
115 s
->context_map_slice
= NULL
;
116 s
->literal_htree
= NULL
;
117 s
->dist_context_map_slice
= NULL
;
118 s
->dist_htree_index
= 0;
119 s
->context_lookup
= NULL
;
120 s
->literal_hgroup
.codes
= NULL
;
121 s
->literal_hgroup
.htrees
= NULL
;
122 s
->insert_copy_hgroup
.codes
= NULL
;
123 s
->insert_copy_hgroup
.htrees
= NULL
;
124 s
->distance_hgroup
.codes
= NULL
;
125 s
->distance_hgroup
.htrees
= NULL
;
128 void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState
* s
) {
129 BROTLI_DECODER_FREE(s
, s
->context_modes
);
130 BROTLI_DECODER_FREE(s
, s
->context_map
);
131 BROTLI_DECODER_FREE(s
, s
->dist_context_map
);
132 BROTLI_DECODER_FREE(s
, s
->literal_hgroup
.htrees
);
133 BROTLI_DECODER_FREE(s
, s
->insert_copy_hgroup
.htrees
);
134 BROTLI_DECODER_FREE(s
, s
->distance_hgroup
.htrees
);
137 void BrotliDecoderStateCleanup(BrotliDecoderState
* s
) {
138 BrotliDecoderStateCleanupAfterMetablock(s
);
140 BROTLI_DECODER_FREE(s
, s
->ringbuffer
);
141 BROTLI_DECODER_FREE(s
, s
->block_type_trees
);
144 BROTLI_BOOL
BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState
* s
,
145 HuffmanTreeGroup
* group
, uint32_t alphabet_size
, uint32_t max_symbol
,
147 /* Pack two allocations into one */
148 const size_t max_table_size
= kMaxHuffmanTableSize
[(alphabet_size
+ 31) >> 5];
149 const size_t code_size
= sizeof(HuffmanCode
) * ntrees
* max_table_size
;
150 const size_t htree_size
= sizeof(HuffmanCode
*) * ntrees
;
151 /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */
152 HuffmanCode
** p
= (HuffmanCode
**)BROTLI_DECODER_ALLOC(s
,
153 code_size
+ htree_size
);
154 group
->alphabet_size
= (uint16_t)alphabet_size
;
155 group
->max_symbol
= (uint16_t)max_symbol
;
156 group
->num_htrees
= (uint16_t)ntrees
;
158 group
->codes
= (HuffmanCode
*)(&p
[ntrees
]);
162 #if defined(__cplusplus) || defined(c_plusplus)