126fa459cSmrg/* Copyright 2015 Google Inc. All Rights Reserved. 226fa459cSmrg 326fa459cSmrg Distributed under MIT license. 426fa459cSmrg See file LICENSE for detail or copy at https://opensource.org/licenses/MIT 526fa459cSmrg*/ 626fa459cSmrg 726fa459cSmrg#include "./state.h" 826fa459cSmrg 926fa459cSmrg#include <stdlib.h> /* free, malloc */ 1026fa459cSmrg 1126fa459cSmrg#include <brotli/types.h> 1226fa459cSmrg#include "./huffman.h" 1326fa459cSmrg 1426fa459cSmrg#if defined(__cplusplus) || defined(c_plusplus) 1526fa459cSmrgextern "C" { 1626fa459cSmrg#endif 1726fa459cSmrg 1826fa459cSmrgBROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s, 1926fa459cSmrg brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) { 2026fa459cSmrg if (!alloc_func) { 2126fa459cSmrg s->alloc_func = BrotliDefaultAllocFunc; 2226fa459cSmrg s->free_func = BrotliDefaultFreeFunc; 2326fa459cSmrg s->memory_manager_opaque = 0; 2426fa459cSmrg } else { 2526fa459cSmrg s->alloc_func = alloc_func; 2626fa459cSmrg s->free_func = free_func; 2726fa459cSmrg s->memory_manager_opaque = opaque; 2826fa459cSmrg } 2926fa459cSmrg 3026fa459cSmrg s->error_code = 0; /* BROTLI_DECODER_NO_ERROR */ 3126fa459cSmrg 3226fa459cSmrg BrotliInitBitReader(&s->br); 3326fa459cSmrg s->state = BROTLI_STATE_UNINITED; 3426fa459cSmrg s->large_window = 0; 3526fa459cSmrg s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; 3626fa459cSmrg s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE; 3726fa459cSmrg s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE; 3826fa459cSmrg s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE; 3926fa459cSmrg 4026fa459cSmrg s->buffer_length = 0; 4126fa459cSmrg s->loop_counter = 0; 4226fa459cSmrg s->pos = 0; 4326fa459cSmrg s->rb_roundtrips = 0; 4426fa459cSmrg s->partial_pos_out = 0; 4526fa459cSmrg 4626fa459cSmrg s->block_type_trees = NULL; 4726fa459cSmrg s->block_len_trees = NULL; 4826fa459cSmrg s->ringbuffer = NULL; 4926fa459cSmrg s->ringbuffer_size = 0; 5026fa459cSmrg s->new_ringbuffer_size = 0; 5126fa459cSmrg s->ringbuffer_mask = 0; 5226fa459cSmrg 5326fa459cSmrg s->context_map = NULL; 5426fa459cSmrg s->context_modes = NULL; 5526fa459cSmrg s->dist_context_map = NULL; 5626fa459cSmrg s->context_map_slice = NULL; 5726fa459cSmrg s->dist_context_map_slice = NULL; 5826fa459cSmrg 5926fa459cSmrg s->literal_hgroup.codes = NULL; 6026fa459cSmrg s->literal_hgroup.htrees = NULL; 6126fa459cSmrg s->insert_copy_hgroup.codes = NULL; 6226fa459cSmrg s->insert_copy_hgroup.htrees = NULL; 6326fa459cSmrg s->distance_hgroup.codes = NULL; 6426fa459cSmrg s->distance_hgroup.htrees = NULL; 6526fa459cSmrg 6626fa459cSmrg s->is_last_metablock = 0; 6726fa459cSmrg s->is_uncompressed = 0; 6826fa459cSmrg s->is_metadata = 0; 6926fa459cSmrg s->should_wrap_ringbuffer = 0; 7026fa459cSmrg s->canny_ringbuffer_allocation = 1; 7126fa459cSmrg 7226fa459cSmrg s->window_bits = 0; 7326fa459cSmrg s->max_distance = 0; 7426fa459cSmrg s->dist_rb[0] = 16; 7526fa459cSmrg s->dist_rb[1] = 15; 7626fa459cSmrg s->dist_rb[2] = 11; 7726fa459cSmrg s->dist_rb[3] = 4; 7826fa459cSmrg s->dist_rb_idx = 0; 7926fa459cSmrg s->block_type_trees = NULL; 8026fa459cSmrg s->block_len_trees = NULL; 8126fa459cSmrg 8226fa459cSmrg s->mtf_upper_bound = 63; 8326fa459cSmrg 8426fa459cSmrg s->dictionary = BrotliGetDictionary(); 8526fa459cSmrg s->transforms = BrotliGetTransforms(); 8626fa459cSmrg 8726fa459cSmrg return BROTLI_TRUE; 8826fa459cSmrg} 8926fa459cSmrg 9026fa459cSmrgvoid BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) { 9126fa459cSmrg s->meta_block_remaining_len = 0; 9226fa459cSmrg s->block_length[0] = 1U << 24; 9326fa459cSmrg s->block_length[1] = 1U << 24; 9426fa459cSmrg s->block_length[2] = 1U << 24; 9526fa459cSmrg s->num_block_types[0] = 1; 9626fa459cSmrg s->num_block_types[1] = 1; 9726fa459cSmrg s->num_block_types[2] = 1; 9826fa459cSmrg s->block_type_rb[0] = 1; 9926fa459cSmrg s->block_type_rb[1] = 0; 10026fa459cSmrg s->block_type_rb[2] = 1; 10126fa459cSmrg s->block_type_rb[3] = 0; 10226fa459cSmrg s->block_type_rb[4] = 1; 10326fa459cSmrg s->block_type_rb[5] = 0; 10426fa459cSmrg s->context_map = NULL; 10526fa459cSmrg s->context_modes = NULL; 10626fa459cSmrg s->dist_context_map = NULL; 10726fa459cSmrg s->context_map_slice = NULL; 10826fa459cSmrg s->literal_htree = NULL; 10926fa459cSmrg s->dist_context_map_slice = NULL; 11026fa459cSmrg s->dist_htree_index = 0; 11126fa459cSmrg s->context_lookup = NULL; 11226fa459cSmrg s->literal_hgroup.codes = NULL; 11326fa459cSmrg s->literal_hgroup.htrees = NULL; 11426fa459cSmrg s->insert_copy_hgroup.codes = NULL; 11526fa459cSmrg s->insert_copy_hgroup.htrees = NULL; 11626fa459cSmrg s->distance_hgroup.codes = NULL; 11726fa459cSmrg s->distance_hgroup.htrees = NULL; 11826fa459cSmrg} 11926fa459cSmrg 12026fa459cSmrgvoid BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) { 12126fa459cSmrg BROTLI_DECODER_FREE(s, s->context_modes); 12226fa459cSmrg BROTLI_DECODER_FREE(s, s->context_map); 12326fa459cSmrg BROTLI_DECODER_FREE(s, s->dist_context_map); 12426fa459cSmrg BROTLI_DECODER_FREE(s, s->literal_hgroup.htrees); 12526fa459cSmrg BROTLI_DECODER_FREE(s, s->insert_copy_hgroup.htrees); 12626fa459cSmrg BROTLI_DECODER_FREE(s, s->distance_hgroup.htrees); 12726fa459cSmrg} 12826fa459cSmrg 12926fa459cSmrgvoid BrotliDecoderStateCleanup(BrotliDecoderState* s) { 13026fa459cSmrg BrotliDecoderStateCleanupAfterMetablock(s); 13126fa459cSmrg 13226fa459cSmrg BROTLI_DECODER_FREE(s, s->ringbuffer); 13326fa459cSmrg BROTLI_DECODER_FREE(s, s->block_type_trees); 13426fa459cSmrg} 13526fa459cSmrg 13626fa459cSmrgBROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s, 13726fa459cSmrg HuffmanTreeGroup* group, uint32_t alphabet_size_max, 13826fa459cSmrg uint32_t alphabet_size_limit, uint32_t ntrees) { 13926fa459cSmrg /* 376 = 256 (1-st level table) + 4 + 7 + 15 + 31 + 63 (2-nd level mix-tables) 14026fa459cSmrg This number is discovered "unlimited" "enough" calculator; it is actually 14126fa459cSmrg a wee bigger than required in several cases (especially for alphabets with 14226fa459cSmrg less than 16 symbols). */ 14326fa459cSmrg const size_t max_table_size = alphabet_size_limit + 376; 14426fa459cSmrg const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size; 14526fa459cSmrg const size_t htree_size = sizeof(HuffmanCode*) * ntrees; 14626fa459cSmrg /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */ 14726fa459cSmrg HuffmanCode** p = (HuffmanCode**)BROTLI_DECODER_ALLOC(s, 14826fa459cSmrg code_size + htree_size); 14926fa459cSmrg group->alphabet_size_max = (uint16_t)alphabet_size_max; 15026fa459cSmrg group->alphabet_size_limit = (uint16_t)alphabet_size_limit; 15126fa459cSmrg group->num_htrees = (uint16_t)ntrees; 15226fa459cSmrg group->htrees = p; 15326fa459cSmrg group->codes = (HuffmanCode*)(&p[ntrees]); 15426fa459cSmrg return !!p; 15526fa459cSmrg} 15626fa459cSmrg 15726fa459cSmrg#if defined(__cplusplus) || defined(c_plusplus) 15826fa459cSmrg} /* extern "C" */ 15926fa459cSmrg#endif 160