\r
#include <stdlib.h> /* free, malloc */\r
\r
-#include "../common/types.h"\r
+#include <brotli/types.h>\r
#include "./huffman.h"\r
\r
#if defined(__cplusplus) || defined(c_plusplus)\r
extern "C" {\r
#endif\r
\r
-static void* DefaultAllocFunc(void* opaque, size_t size) {\r
- BROTLI_UNUSED(opaque);\r
- return malloc(size);\r
-}\r
-\r
-static void DefaultFreeFunc(void* opaque, void* address) {\r
- BROTLI_UNUSED(opaque);\r
- free(address);\r
-}\r
-\r
-void BrotliDecoderStateInit(BrotliDecoderState* s) {\r
- BrotliDecoderStateInitWithCustomAllocators(s, 0, 0, 0);\r
-}\r
-\r
-void BrotliDecoderStateInitWithCustomAllocators(BrotliDecoderState* s,\r
+BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s,\r
brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {\r
if (!alloc_func) {\r
- s->alloc_func = DefaultAllocFunc;\r
- s->free_func = DefaultFreeFunc;\r
+ s->alloc_func = BrotliDefaultAllocFunc;\r
+ s->free_func = BrotliDefaultFreeFunc;\r
s->memory_manager_opaque = 0;\r
} else {\r
s->alloc_func = alloc_func;\r
s->memory_manager_opaque = opaque;\r
}\r
\r
+ s->error_code = 0; /* BROTLI_DECODER_NO_ERROR */\r
+\r
BrotliInitBitReader(&s->br);\r
s->state = BROTLI_STATE_UNINITED;\r
+ s->large_window = 0;\r
s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;\r
s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;\r
s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;\r
s->block_type_trees = NULL;\r
s->block_len_trees = NULL;\r
s->ringbuffer = NULL;\r
+ s->ringbuffer_size = 0;\r
+ s->new_ringbuffer_size = 0;\r
+ s->ringbuffer_mask = 0;\r
\r
s->context_map = NULL;\r
s->context_modes = NULL;\r
s->distance_hgroup.codes = NULL;\r
s->distance_hgroup.htrees = NULL;\r
\r
- s->custom_dict = NULL;\r
- s->custom_dict_size = 0;\r
-\r
s->is_last_metablock = 0;\r
+ s->is_uncompressed = 0;\r
+ s->is_metadata = 0;\r
+ s->should_wrap_ringbuffer = 0;\r
+ s->canny_ringbuffer_allocation = 1;\r
+\r
s->window_bits = 0;\r
s->max_distance = 0;\r
s->dist_rb[0] = 16;\r
/* Make small negative indexes addressable. */\r
s->symbol_lists = &s->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1];\r
\r
- s->mtf_upper_bound = 255;\r
+ s->mtf_upper_bound = 63;\r
+\r
+ s->dictionary = BrotliGetDictionary();\r
+ s->transforms = BrotliGetTransforms();\r
+\r
+ return BROTLI_TRUE;\r
}\r
\r
void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {\r
s->meta_block_remaining_len = 0;\r
- s->block_length[0] = 1U << 28;\r
- s->block_length[1] = 1U << 28;\r
- s->block_length[2] = 1U << 28;\r
+ s->block_length[0] = 1U << 24;\r
+ s->block_length[1] = 1U << 24;\r
+ s->block_length[2] = 1U << 24;\r
s->num_block_types[0] = 1;\r
s->num_block_types[1] = 1;\r
s->num_block_types[2] = 1;\r
s->literal_htree = NULL;\r
s->dist_context_map_slice = NULL;\r
s->dist_htree_index = 0;\r
- s->context_lookup1 = NULL;\r
- s->context_lookup2 = NULL;\r
+ s->context_lookup = NULL;\r
s->literal_hgroup.codes = NULL;\r
s->literal_hgroup.htrees = NULL;\r
s->insert_copy_hgroup.codes = NULL;\r
}\r
\r
void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) {\r
- BROTLI_FREE(s, s->context_modes);\r
- BROTLI_FREE(s, s->context_map);\r
- BROTLI_FREE(s, s->dist_context_map);\r
-\r
- BrotliDecoderHuffmanTreeGroupRelease(s, &s->literal_hgroup);\r
- BrotliDecoderHuffmanTreeGroupRelease(s, &s->insert_copy_hgroup);\r
- BrotliDecoderHuffmanTreeGroupRelease(s, &s->distance_hgroup);\r
+ BROTLI_DECODER_FREE(s, s->context_modes);\r
+ BROTLI_DECODER_FREE(s, s->context_map);\r
+ BROTLI_DECODER_FREE(s, s->dist_context_map);\r
+ BROTLI_DECODER_FREE(s, s->literal_hgroup.htrees);\r
+ BROTLI_DECODER_FREE(s, s->insert_copy_hgroup.htrees);\r
+ BROTLI_DECODER_FREE(s, s->distance_hgroup.htrees);\r
}\r
\r
void BrotliDecoderStateCleanup(BrotliDecoderState* s) {\r
BrotliDecoderStateCleanupAfterMetablock(s);\r
\r
- BROTLI_FREE(s, s->ringbuffer);\r
- BROTLI_FREE(s, s->block_type_trees);\r
+ BROTLI_DECODER_FREE(s, s->ringbuffer);\r
+ BROTLI_DECODER_FREE(s, s->block_type_trees);\r
}\r
\r
-void BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,\r
- HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t ntrees) {\r
+BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,\r
+ HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t max_symbol,\r
+ uint32_t ntrees) {\r
/* Pack two allocations into one */\r
const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5];\r
const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;\r
const size_t htree_size = sizeof(HuffmanCode*) * ntrees;\r
- char* p = (char*)BROTLI_ALLOC(s, code_size + htree_size);\r
+ /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */\r
+ HuffmanCode** p = (HuffmanCode**)BROTLI_DECODER_ALLOC(s,\r
+ code_size + htree_size);\r
group->alphabet_size = (uint16_t)alphabet_size;\r
+ group->max_symbol = (uint16_t)max_symbol;\r
group->num_htrees = (uint16_t)ntrees;\r
- group->codes = (HuffmanCode*)p;\r
- group->htrees = (HuffmanCode**)(p + code_size);\r
-}\r
-\r
-void BrotliDecoderHuffmanTreeGroupRelease(\r
- BrotliDecoderState* s, HuffmanTreeGroup* group) {\r
- BROTLI_FREE(s, group->codes);\r
- group->htrees = NULL;\r
+ group->htrees = p;\r
+ group->codes = (HuffmanCode*)(&p[ntrees]);\r
+ return !!p;\r
}\r
\r
#if defined(__cplusplus) || defined(c_plusplus)\r