2015-03-20 15:13:15 +00:00
|
|
|
/* Copyright 2015 Google Inc. All Rights Reserved.
|
|
|
|
|
2015-12-11 10:11:51 +00:00
|
|
|
Distributed under MIT license.
|
2015-11-27 10:27:11 +00:00
|
|
|
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
2015-03-20 15:13:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "./state.h"
|
|
|
|
|
2016-01-22 09:19:41 +00:00
|
|
|
#include <stdlib.h> /* free, malloc */
|
|
|
|
|
2016-08-23 12:40:33 +00:00
|
|
|
#include <brotli/types.h>
|
2016-01-22 09:19:41 +00:00
|
|
|
#include "./huffman.h"
|
2015-03-20 15:13:15 +00:00
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2015-11-23 09:21:09 +00:00
|
|
|
static void* DefaultAllocFunc(void* opaque, size_t size) {
|
|
|
|
BROTLI_UNUSED(opaque);
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DefaultFreeFunc(void* opaque, void* address) {
|
|
|
|
BROTLI_UNUSED(opaque);
|
|
|
|
free(address);
|
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
void BrotliDecoderStateInit(BrotliDecoderState* s) {
|
|
|
|
BrotliDecoderStateInitWithCustomAllocators(s, 0, 0, 0);
|
2015-11-23 09:21:09 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
void BrotliDecoderStateInitWithCustomAllocators(BrotliDecoderState* s,
|
2015-11-23 09:21:09 +00:00
|
|
|
brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
|
|
|
|
if (!alloc_func) {
|
|
|
|
s->alloc_func = DefaultAllocFunc;
|
|
|
|
s->free_func = DefaultFreeFunc;
|
|
|
|
s->memory_manager_opaque = 0;
|
|
|
|
} else {
|
|
|
|
s->alloc_func = alloc_func;
|
|
|
|
s->free_func = free_func;
|
|
|
|
s->memory_manager_opaque = opaque;
|
|
|
|
}
|
|
|
|
|
2015-11-06 10:46:04 +00:00
|
|
|
BrotliInitBitReader(&s->br);
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_UNINITED;
|
2015-09-23 10:24:06 +00:00
|
|
|
s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
|
|
|
|
s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;
|
|
|
|
s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;
|
|
|
|
s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE;
|
|
|
|
s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
|
2015-09-24 16:35:25 +00:00
|
|
|
s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
|
2015-11-06 10:46:04 +00:00
|
|
|
s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
|
2015-03-20 15:13:15 +00:00
|
|
|
|
2017-03-06 13:22:45 +00:00
|
|
|
s->dictionary = BrotliGetDictionary();
|
|
|
|
|
2015-11-06 10:46:04 +00:00
|
|
|
s->buffer_length = 0;
|
2015-10-02 14:46:08 +00:00
|
|
|
s->loop_counter = 0;
|
|
|
|
s->pos = 0;
|
2015-11-06 10:46:04 +00:00
|
|
|
s->rb_roundtrips = 0;
|
|
|
|
s->partial_pos_out = 0;
|
2015-10-02 14:46:08 +00:00
|
|
|
|
2015-03-20 15:13:15 +00:00
|
|
|
s->block_type_trees = NULL;
|
|
|
|
s->block_len_trees = NULL;
|
|
|
|
s->ringbuffer = NULL;
|
2016-09-21 13:05:12 +00:00
|
|
|
s->ringbuffer_size = 0;
|
|
|
|
s->new_ringbuffer_size = 0;
|
|
|
|
s->ringbuffer_mask = 0;
|
2015-03-20 15:13:15 +00:00
|
|
|
|
|
|
|
s->context_map = NULL;
|
|
|
|
s->context_modes = NULL;
|
|
|
|
s->dist_context_map = NULL;
|
|
|
|
s->context_map_slice = NULL;
|
|
|
|
s->dist_context_map_slice = NULL;
|
|
|
|
|
2015-11-06 10:46:04 +00:00
|
|
|
s->sub_loop_counter = 0;
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
s->literal_hgroup.codes = NULL;
|
|
|
|
s->literal_hgroup.htrees = NULL;
|
|
|
|
s->insert_copy_hgroup.codes = NULL;
|
|
|
|
s->insert_copy_hgroup.htrees = NULL;
|
|
|
|
s->distance_hgroup.codes = NULL;
|
|
|
|
s->distance_hgroup.htrees = NULL;
|
2015-03-20 15:13:15 +00:00
|
|
|
|
2015-06-12 13:43:54 +00:00
|
|
|
s->custom_dict = NULL;
|
|
|
|
s->custom_dict_size = 0;
|
2015-08-10 11:35:23 +00:00
|
|
|
|
2015-09-23 10:24:06 +00:00
|
|
|
s->is_last_metablock = 0;
|
2016-08-22 11:28:22 +00:00
|
|
|
s->should_wrap_ringbuffer = 0;
|
2015-08-10 11:35:23 +00:00
|
|
|
s->window_bits = 0;
|
|
|
|
s->max_distance = 0;
|
|
|
|
s->dist_rb[0] = 16;
|
|
|
|
s->dist_rb[1] = 15;
|
|
|
|
s->dist_rb[2] = 11;
|
|
|
|
s->dist_rb[3] = 4;
|
|
|
|
s->dist_rb_idx = 0;
|
|
|
|
s->block_type_trees = NULL;
|
|
|
|
s->block_len_trees = NULL;
|
|
|
|
|
2015-08-28 13:20:24 +00:00
|
|
|
/* Make small negative indexes addressable. */
|
|
|
|
s->symbol_lists = &s->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1];
|
|
|
|
|
2016-10-19 14:27:57 +00:00
|
|
|
s->mtf_upper_bound = 63;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {
|
2015-08-10 11:35:23 +00:00
|
|
|
s->meta_block_remaining_len = 0;
|
2015-11-06 10:46:04 +00:00
|
|
|
s->block_length[0] = 1U << 28;
|
|
|
|
s->block_length[1] = 1U << 28;
|
|
|
|
s->block_length[2] = 1U << 28;
|
2015-08-10 11:35:23 +00:00
|
|
|
s->num_block_types[0] = 1;
|
|
|
|
s->num_block_types[1] = 1;
|
|
|
|
s->num_block_types[2] = 1;
|
|
|
|
s->block_type_rb[0] = 1;
|
|
|
|
s->block_type_rb[1] = 0;
|
|
|
|
s->block_type_rb[2] = 1;
|
|
|
|
s->block_type_rb[3] = 0;
|
|
|
|
s->block_type_rb[4] = 1;
|
|
|
|
s->block_type_rb[5] = 0;
|
|
|
|
s->context_map = NULL;
|
|
|
|
s->context_modes = NULL;
|
|
|
|
s->dist_context_map = NULL;
|
|
|
|
s->context_map_slice = NULL;
|
2015-08-28 13:20:24 +00:00
|
|
|
s->literal_htree = NULL;
|
2015-08-10 11:35:23 +00:00
|
|
|
s->dist_context_map_slice = NULL;
|
|
|
|
s->dist_htree_index = 0;
|
|
|
|
s->context_lookup1 = NULL;
|
|
|
|
s->context_lookup2 = NULL;
|
|
|
|
s->literal_hgroup.codes = NULL;
|
|
|
|
s->literal_hgroup.htrees = NULL;
|
|
|
|
s->insert_copy_hgroup.codes = NULL;
|
|
|
|
s->insert_copy_hgroup.htrees = NULL;
|
|
|
|
s->distance_hgroup.codes = NULL;
|
|
|
|
s->distance_hgroup.htrees = NULL;
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) {
|
2015-11-23 09:21:09 +00:00
|
|
|
BROTLI_FREE(s, s->context_modes);
|
|
|
|
BROTLI_FREE(s, s->context_map);
|
|
|
|
BROTLI_FREE(s, s->dist_context_map);
|
2017-03-09 16:34:16 +00:00
|
|
|
BROTLI_FREE(s, s->literal_hgroup.htrees);
|
|
|
|
BROTLI_FREE(s, s->insert_copy_hgroup.htrees);
|
|
|
|
BROTLI_FREE(s, s->distance_hgroup.htrees);
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
void BrotliDecoderStateCleanup(BrotliDecoderState* s) {
|
|
|
|
BrotliDecoderStateCleanupAfterMetablock(s);
|
2015-03-20 15:13:15 +00:00
|
|
|
|
2015-11-23 09:21:09 +00:00
|
|
|
BROTLI_FREE(s, s->ringbuffer);
|
|
|
|
BROTLI_FREE(s, s->block_type_trees);
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 14:27:57 +00:00
|
|
|
BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,
|
2016-07-25 08:17:04 +00:00
|
|
|
HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t ntrees) {
|
2015-11-23 09:21:09 +00:00
|
|
|
/* Pack two allocations into one */
|
2016-01-22 09:19:41 +00:00
|
|
|
const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5];
|
|
|
|
const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
|
|
|
|
const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
|
2016-10-19 14:27:57 +00:00
|
|
|
/* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */
|
|
|
|
HuffmanCode** p = (HuffmanCode**)BROTLI_ALLOC(s, code_size + htree_size);
|
2015-11-23 09:21:09 +00:00
|
|
|
group->alphabet_size = (uint16_t)alphabet_size;
|
|
|
|
group->num_htrees = (uint16_t)ntrees;
|
2017-03-09 16:34:16 +00:00
|
|
|
group->htrees = p;
|
2016-10-19 14:27:57 +00:00
|
|
|
group->codes = (HuffmanCode*)(&p[ntrees]);
|
|
|
|
return !!p;
|
2015-11-23 09:21:09 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 15:13:15 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
2016-02-18 14:03:44 +00:00
|
|
|
} /* extern "C" */
|
2015-03-20 15:13:15 +00:00
|
|
|
#endif
|