state.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* Copyright 2015 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. #include "./state.h"
  6. #include <stdlib.h> /* free, malloc */
  7. #include <brotli/types.h>
  8. #include "./huffman.h"
  9. #if defined(__cplusplus) || defined(c_plusplus)
  10. extern "C" {
  11. #endif
  12. BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s,
  13. brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
  14. if (!alloc_func) {
  15. s->alloc_func = BrotliDefaultAllocFunc;
  16. s->free_func = BrotliDefaultFreeFunc;
  17. s->memory_manager_opaque = 0;
  18. } else {
  19. s->alloc_func = alloc_func;
  20. s->free_func = free_func;
  21. s->memory_manager_opaque = opaque;
  22. }
  23. s->error_code = 0; /* BROTLI_DECODER_NO_ERROR */
  24. BrotliInitBitReader(&s->br);
  25. s->state = BROTLI_STATE_UNINITED;
  26. s->large_window = 0;
  27. s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
  28. s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;
  29. s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;
  30. s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE;
  31. s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
  32. s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
  33. s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
  34. s->buffer_length = 0;
  35. s->loop_counter = 0;
  36. s->pos = 0;
  37. s->rb_roundtrips = 0;
  38. s->partial_pos_out = 0;
  39. s->block_type_trees = NULL;
  40. s->block_len_trees = NULL;
  41. s->ringbuffer = NULL;
  42. s->ringbuffer_size = 0;
  43. s->new_ringbuffer_size = 0;
  44. s->ringbuffer_mask = 0;
  45. s->context_map = NULL;
  46. s->context_modes = NULL;
  47. s->dist_context_map = NULL;
  48. s->context_map_slice = NULL;
  49. s->dist_context_map_slice = NULL;
  50. s->sub_loop_counter = 0;
  51. s->literal_hgroup.codes = NULL;
  52. s->literal_hgroup.htrees = NULL;
  53. s->insert_copy_hgroup.codes = NULL;
  54. s->insert_copy_hgroup.htrees = NULL;
  55. s->distance_hgroup.codes = NULL;
  56. s->distance_hgroup.htrees = NULL;
  57. s->is_last_metablock = 0;
  58. s->is_uncompressed = 0;
  59. s->is_metadata = 0;
  60. s->should_wrap_ringbuffer = 0;
  61. s->canny_ringbuffer_allocation = 1;
  62. s->window_bits = 0;
  63. s->max_distance = 0;
  64. s->dist_rb[0] = 16;
  65. s->dist_rb[1] = 15;
  66. s->dist_rb[2] = 11;
  67. s->dist_rb[3] = 4;
  68. s->dist_rb_idx = 0;
  69. s->block_type_trees = NULL;
  70. s->block_len_trees = NULL;
  71. /* Make small negative indexes addressable. */
  72. s->symbol_lists = &s->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1];
  73. s->mtf_upper_bound = 63;
  74. s->dictionary = BrotliGetDictionary();
  75. s->transforms = BrotliGetTransforms();
  76. return BROTLI_TRUE;
  77. }
  78. void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {
  79. s->meta_block_remaining_len = 0;
  80. s->block_length[0] = 1U << 24;
  81. s->block_length[1] = 1U << 24;
  82. s->block_length[2] = 1U << 24;
  83. s->num_block_types[0] = 1;
  84. s->num_block_types[1] = 1;
  85. s->num_block_types[2] = 1;
  86. s->block_type_rb[0] = 1;
  87. s->block_type_rb[1] = 0;
  88. s->block_type_rb[2] = 1;
  89. s->block_type_rb[3] = 0;
  90. s->block_type_rb[4] = 1;
  91. s->block_type_rb[5] = 0;
  92. s->context_map = NULL;
  93. s->context_modes = NULL;
  94. s->dist_context_map = NULL;
  95. s->context_map_slice = NULL;
  96. s->literal_htree = NULL;
  97. s->dist_context_map_slice = NULL;
  98. s->dist_htree_index = 0;
  99. s->context_lookup = NULL;
  100. s->literal_hgroup.codes = NULL;
  101. s->literal_hgroup.htrees = NULL;
  102. s->insert_copy_hgroup.codes = NULL;
  103. s->insert_copy_hgroup.htrees = NULL;
  104. s->distance_hgroup.codes = NULL;
  105. s->distance_hgroup.htrees = NULL;
  106. }
  107. void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) {
  108. BROTLI_DECODER_FREE(s, s->context_modes);
  109. BROTLI_DECODER_FREE(s, s->context_map);
  110. BROTLI_DECODER_FREE(s, s->dist_context_map);
  111. BROTLI_DECODER_FREE(s, s->literal_hgroup.htrees);
  112. BROTLI_DECODER_FREE(s, s->insert_copy_hgroup.htrees);
  113. BROTLI_DECODER_FREE(s, s->distance_hgroup.htrees);
  114. }
  115. void BrotliDecoderStateCleanup(BrotliDecoderState* s) {
  116. BrotliDecoderStateCleanupAfterMetablock(s);
  117. BROTLI_DECODER_FREE(s, s->ringbuffer);
  118. BROTLI_DECODER_FREE(s, s->block_type_trees);
  119. }
  120. BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,
  121. HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t max_symbol,
  122. uint32_t ntrees) {
  123. /* Pack two allocations into one */
  124. const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5];
  125. const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
  126. const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
  127. /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */
  128. HuffmanCode** p = (HuffmanCode**)BROTLI_DECODER_ALLOC(s,
  129. code_size + htree_size);
  130. group->alphabet_size = (uint16_t)alphabet_size;
  131. group->max_symbol = (uint16_t)max_symbol;
  132. group->num_htrees = (uint16_t)ntrees;
  133. group->htrees = p;
  134. group->codes = (HuffmanCode*)(&p[ntrees]);
  135. return !!p;
  136. }
  137. #if defined(__cplusplus) || defined(c_plusplus)
  138. } /* extern "C" */
  139. #endif