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
static void* DefaultAllocFunc(void* opaque, size_t size) {
19
BROTLI_UNUSED(opaque);
23
static void DefaultFreeFunc(void* opaque, void* address) {
24
BROTLI_UNUSED(opaque);
28
void BrotliDecoderStateInit(BrotliDecoderState* s) {
29
BrotliDecoderStateInitWithCustomAllocators(s, 0, 0, 0);
32
void BrotliDecoderStateInitWithCustomAllocators(BrotliDecoderState* s,
33
brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
35
s->alloc_func = DefaultAllocFunc;
36
s->free_func = DefaultFreeFunc;
37
s->memory_manager_opaque = 0;
39
s->alloc_func = alloc_func;
40
s->free_func = free_func;
41
s->memory_manager_opaque = opaque;
44
s->error_code = 0; /* BROTLI_DECODER_NO_ERROR */
46
BrotliInitBitReader(&s->br);
47
s->state = BROTLI_STATE_UNINITED;
48
s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
49
s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;
50
s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;
51
s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE;
52
s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
53
s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
54
s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
56
s->dictionary = BrotliGetDictionary();
62
s->partial_pos_out = 0;
64
s->block_type_trees = NULL;
65
s->block_len_trees = NULL;
67
s->ringbuffer_size = 0;
68
s->new_ringbuffer_size = 0;
69
s->ringbuffer_mask = 0;
71
s->context_map = NULL;
72
s->context_modes = NULL;
73
s->dist_context_map = NULL;
74
s->context_map_slice = NULL;
75
s->dist_context_map_slice = NULL;
77
s->sub_loop_counter = 0;
79
s->literal_hgroup.codes = NULL;
80
s->literal_hgroup.htrees = NULL;
81
s->insert_copy_hgroup.codes = NULL;
82
s->insert_copy_hgroup.htrees = NULL;
83
s->distance_hgroup.codes = NULL;
84
s->distance_hgroup.htrees = NULL;
86
s->is_last_metablock = 0;
87
s->is_uncompressed = 0;
89
s->should_wrap_ringbuffer = 0;
90
s->canny_ringbuffer_allocation = 1;
99
s->block_type_trees = NULL;
100
s->block_len_trees = NULL;
102
/* Make small negative indexes addressable. */
103
s->symbol_lists = &s->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1];
105
s->mtf_upper_bound = 63;
108
void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {
109
s->meta_block_remaining_len = 0;
110
s->block_length[0] = 1U << 28;
111
s->block_length[1] = 1U << 28;
112
s->block_length[2] = 1U << 28;
113
s->num_block_types[0] = 1;
114
s->num_block_types[1] = 1;
115
s->num_block_types[2] = 1;
116
s->block_type_rb[0] = 1;
117
s->block_type_rb[1] = 0;
118
s->block_type_rb[2] = 1;
119
s->block_type_rb[3] = 0;
120
s->block_type_rb[4] = 1;
121
s->block_type_rb[5] = 0;
122
s->context_map = NULL;
123
s->context_modes = NULL;
124
s->dist_context_map = NULL;
125
s->context_map_slice = NULL;
126
s->literal_htree = NULL;
127
s->dist_context_map_slice = NULL;
128
s->dist_htree_index = 0;
129
s->context_lookup1 = NULL;
130
s->context_lookup2 = NULL;
131
s->literal_hgroup.codes = NULL;
132
s->literal_hgroup.htrees = NULL;
133
s->insert_copy_hgroup.codes = NULL;
134
s->insert_copy_hgroup.htrees = NULL;
135
s->distance_hgroup.codes = NULL;
136
s->distance_hgroup.htrees = NULL;
139
void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) {
140
BROTLI_FREE(s, s->context_modes);
141
BROTLI_FREE(s, s->context_map);
142
BROTLI_FREE(s, s->dist_context_map);
143
BROTLI_FREE(s, s->literal_hgroup.htrees);
144
BROTLI_FREE(s, s->insert_copy_hgroup.htrees);
145
BROTLI_FREE(s, s->distance_hgroup.htrees);
148
void BrotliDecoderStateCleanup(BrotliDecoderState* s) {
149
BrotliDecoderStateCleanupAfterMetablock(s);
151
BROTLI_FREE(s, s->ringbuffer);
152
BROTLI_FREE(s, s->block_type_trees);
155
BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,
156
HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t ntrees) {
157
/* Pack two allocations into one */
158
const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5];
159
const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
160
const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
161
/* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */
162
HuffmanCode** p = (HuffmanCode**)BROTLI_ALLOC(s, code_size + htree_size);
163
group->alphabet_size = (uint16_t)alphabet_size;
164
group->num_htrees = (uint16_t)ntrees;
166
group->codes = (HuffmanCode*)(&p[ntrees]);
170
#if defined(__cplusplus) || defined(c_plusplus)