11cb0ef41Sopenharmony_ci/* Copyright 2015 Google Inc. All Rights Reserved.
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci   Distributed under MIT license.
41cb0ef41Sopenharmony_ci   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
51cb0ef41Sopenharmony_ci*/
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "./state.h"
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#include <stdlib.h>  /* free, malloc */
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci#include <brotli/types.h>
121cb0ef41Sopenharmony_ci#include "./huffman.h"
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus)
151cb0ef41Sopenharmony_ciextern "C" {
161cb0ef41Sopenharmony_ci#endif
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciBROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s,
191cb0ef41Sopenharmony_ci    brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
201cb0ef41Sopenharmony_ci  if (!alloc_func) {
211cb0ef41Sopenharmony_ci    s->alloc_func = BrotliDefaultAllocFunc;
221cb0ef41Sopenharmony_ci    s->free_func = BrotliDefaultFreeFunc;
231cb0ef41Sopenharmony_ci    s->memory_manager_opaque = 0;
241cb0ef41Sopenharmony_ci  } else {
251cb0ef41Sopenharmony_ci    s->alloc_func = alloc_func;
261cb0ef41Sopenharmony_ci    s->free_func = free_func;
271cb0ef41Sopenharmony_ci    s->memory_manager_opaque = opaque;
281cb0ef41Sopenharmony_ci  }
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  s->error_code = 0; /* BROTLI_DECODER_NO_ERROR */
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  BrotliInitBitReader(&s->br);
331cb0ef41Sopenharmony_ci  s->state = BROTLI_STATE_UNINITED;
341cb0ef41Sopenharmony_ci  s->large_window = 0;
351cb0ef41Sopenharmony_ci  s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
361cb0ef41Sopenharmony_ci  s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE;
371cb0ef41Sopenharmony_ci  s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
381cb0ef41Sopenharmony_ci  s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  s->buffer_length = 0;
411cb0ef41Sopenharmony_ci  s->loop_counter = 0;
421cb0ef41Sopenharmony_ci  s->pos = 0;
431cb0ef41Sopenharmony_ci  s->rb_roundtrips = 0;
441cb0ef41Sopenharmony_ci  s->partial_pos_out = 0;
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  s->block_type_trees = NULL;
471cb0ef41Sopenharmony_ci  s->block_len_trees = NULL;
481cb0ef41Sopenharmony_ci  s->ringbuffer = NULL;
491cb0ef41Sopenharmony_ci  s->ringbuffer_size = 0;
501cb0ef41Sopenharmony_ci  s->new_ringbuffer_size = 0;
511cb0ef41Sopenharmony_ci  s->ringbuffer_mask = 0;
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  s->context_map = NULL;
541cb0ef41Sopenharmony_ci  s->context_modes = NULL;
551cb0ef41Sopenharmony_ci  s->dist_context_map = NULL;
561cb0ef41Sopenharmony_ci  s->context_map_slice = NULL;
571cb0ef41Sopenharmony_ci  s->dist_context_map_slice = NULL;
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  s->literal_hgroup.codes = NULL;
601cb0ef41Sopenharmony_ci  s->literal_hgroup.htrees = NULL;
611cb0ef41Sopenharmony_ci  s->insert_copy_hgroup.codes = NULL;
621cb0ef41Sopenharmony_ci  s->insert_copy_hgroup.htrees = NULL;
631cb0ef41Sopenharmony_ci  s->distance_hgroup.codes = NULL;
641cb0ef41Sopenharmony_ci  s->distance_hgroup.htrees = NULL;
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  s->is_last_metablock = 0;
671cb0ef41Sopenharmony_ci  s->is_uncompressed = 0;
681cb0ef41Sopenharmony_ci  s->is_metadata = 0;
691cb0ef41Sopenharmony_ci  s->should_wrap_ringbuffer = 0;
701cb0ef41Sopenharmony_ci  s->canny_ringbuffer_allocation = 1;
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  s->window_bits = 0;
731cb0ef41Sopenharmony_ci  s->max_distance = 0;
741cb0ef41Sopenharmony_ci  s->dist_rb[0] = 16;
751cb0ef41Sopenharmony_ci  s->dist_rb[1] = 15;
761cb0ef41Sopenharmony_ci  s->dist_rb[2] = 11;
771cb0ef41Sopenharmony_ci  s->dist_rb[3] = 4;
781cb0ef41Sopenharmony_ci  s->dist_rb_idx = 0;
791cb0ef41Sopenharmony_ci  s->block_type_trees = NULL;
801cb0ef41Sopenharmony_ci  s->block_len_trees = NULL;
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  s->mtf_upper_bound = 63;
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  s->dictionary = BrotliGetDictionary();
851cb0ef41Sopenharmony_ci  s->transforms = BrotliGetTransforms();
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  return BROTLI_TRUE;
881cb0ef41Sopenharmony_ci}
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_civoid BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {
911cb0ef41Sopenharmony_ci  s->meta_block_remaining_len = 0;
921cb0ef41Sopenharmony_ci  s->block_length[0] = 1U << 24;
931cb0ef41Sopenharmony_ci  s->block_length[1] = 1U << 24;
941cb0ef41Sopenharmony_ci  s->block_length[2] = 1U << 24;
951cb0ef41Sopenharmony_ci  s->num_block_types[0] = 1;
961cb0ef41Sopenharmony_ci  s->num_block_types[1] = 1;
971cb0ef41Sopenharmony_ci  s->num_block_types[2] = 1;
981cb0ef41Sopenharmony_ci  s->block_type_rb[0] = 1;
991cb0ef41Sopenharmony_ci  s->block_type_rb[1] = 0;
1001cb0ef41Sopenharmony_ci  s->block_type_rb[2] = 1;
1011cb0ef41Sopenharmony_ci  s->block_type_rb[3] = 0;
1021cb0ef41Sopenharmony_ci  s->block_type_rb[4] = 1;
1031cb0ef41Sopenharmony_ci  s->block_type_rb[5] = 0;
1041cb0ef41Sopenharmony_ci  s->context_map = NULL;
1051cb0ef41Sopenharmony_ci  s->context_modes = NULL;
1061cb0ef41Sopenharmony_ci  s->dist_context_map = NULL;
1071cb0ef41Sopenharmony_ci  s->context_map_slice = NULL;
1081cb0ef41Sopenharmony_ci  s->literal_htree = NULL;
1091cb0ef41Sopenharmony_ci  s->dist_context_map_slice = NULL;
1101cb0ef41Sopenharmony_ci  s->dist_htree_index = 0;
1111cb0ef41Sopenharmony_ci  s->context_lookup = NULL;
1121cb0ef41Sopenharmony_ci  s->literal_hgroup.codes = NULL;
1131cb0ef41Sopenharmony_ci  s->literal_hgroup.htrees = NULL;
1141cb0ef41Sopenharmony_ci  s->insert_copy_hgroup.codes = NULL;
1151cb0ef41Sopenharmony_ci  s->insert_copy_hgroup.htrees = NULL;
1161cb0ef41Sopenharmony_ci  s->distance_hgroup.codes = NULL;
1171cb0ef41Sopenharmony_ci  s->distance_hgroup.htrees = NULL;
1181cb0ef41Sopenharmony_ci}
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_civoid BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) {
1211cb0ef41Sopenharmony_ci  BROTLI_DECODER_FREE(s, s->context_modes);
1221cb0ef41Sopenharmony_ci  BROTLI_DECODER_FREE(s, s->context_map);
1231cb0ef41Sopenharmony_ci  BROTLI_DECODER_FREE(s, s->dist_context_map);
1241cb0ef41Sopenharmony_ci  BROTLI_DECODER_FREE(s, s->literal_hgroup.htrees);
1251cb0ef41Sopenharmony_ci  BROTLI_DECODER_FREE(s, s->insert_copy_hgroup.htrees);
1261cb0ef41Sopenharmony_ci  BROTLI_DECODER_FREE(s, s->distance_hgroup.htrees);
1271cb0ef41Sopenharmony_ci}
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_civoid BrotliDecoderStateCleanup(BrotliDecoderState* s) {
1301cb0ef41Sopenharmony_ci  BrotliDecoderStateCleanupAfterMetablock(s);
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  BROTLI_DECODER_FREE(s, s->ringbuffer);
1331cb0ef41Sopenharmony_ci  BROTLI_DECODER_FREE(s, s->block_type_trees);
1341cb0ef41Sopenharmony_ci}
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ciBROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,
1371cb0ef41Sopenharmony_ci    HuffmanTreeGroup* group, uint32_t alphabet_size_max,
1381cb0ef41Sopenharmony_ci    uint32_t alphabet_size_limit, uint32_t ntrees) {
1391cb0ef41Sopenharmony_ci  /* 376 = 256 (1-st level table) + 4 + 7 + 15 + 31 + 63 (2-nd level mix-tables)
1401cb0ef41Sopenharmony_ci     This number is discovered "unlimited" "enough" calculator; it is actually
1411cb0ef41Sopenharmony_ci     a wee bigger than required in several cases (especially for alphabets with
1421cb0ef41Sopenharmony_ci     less than 16 symbols). */
1431cb0ef41Sopenharmony_ci  const size_t max_table_size = alphabet_size_limit + 376;
1441cb0ef41Sopenharmony_ci  const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
1451cb0ef41Sopenharmony_ci  const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
1461cb0ef41Sopenharmony_ci  /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */
1471cb0ef41Sopenharmony_ci  HuffmanCode** p = (HuffmanCode**)BROTLI_DECODER_ALLOC(s,
1481cb0ef41Sopenharmony_ci      code_size + htree_size);
1491cb0ef41Sopenharmony_ci  group->alphabet_size_max = (uint16_t)alphabet_size_max;
1501cb0ef41Sopenharmony_ci  group->alphabet_size_limit = (uint16_t)alphabet_size_limit;
1511cb0ef41Sopenharmony_ci  group->num_htrees = (uint16_t)ntrees;
1521cb0ef41Sopenharmony_ci  group->htrees = p;
1531cb0ef41Sopenharmony_ci  group->codes = (HuffmanCode*)(&p[ntrees]);
1541cb0ef41Sopenharmony_ci  return !!p;
1551cb0ef41Sopenharmony_ci}
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus)
1581cb0ef41Sopenharmony_ci}  /* extern "C" */
1591cb0ef41Sopenharmony_ci#endif
160