state.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "../common/dictionary.h"
  8. #include <brotli/types.h>
  9. #include "huffman.h"
  10. #if defined(__cplusplus) || defined(c_plusplus)
  11. extern "C" {
  12. #endif
  13. BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s,
  14. brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
  15. if (!alloc_func) {
  16. s->alloc_func = BrotliDefaultAllocFunc;
  17. s->free_func = BrotliDefaultFreeFunc;
  18. s->memory_manager_opaque = 0;
  19. } else {
  20. s->alloc_func = alloc_func;
  21. s->free_func = free_func;
  22. s->memory_manager_opaque = opaque;
  23. }
  24. s->error_code = 0; /* BROTLI_DECODER_NO_ERROR */
  25. BrotliInitBitReader(&s->br);
  26. s->state = BROTLI_STATE_UNINITED;
  27. s->large_window = 0;
  28. s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
  29. s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE;
  30. s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
  31. s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
  32. s->buffer_length = 0;
  33. s->loop_counter = 0;
  34. s->pos = 0;
  35. s->rb_roundtrips = 0;
  36. s->partial_pos_out = 0;
  37. s->block_type_trees = NULL;
  38. s->block_len_trees = NULL;
  39. s->ringbuffer = NULL;
  40. s->ringbuffer_size = 0;
  41. s->new_ringbuffer_size = 0;
  42. s->ringbuffer_mask = 0;
  43. s->context_map = NULL;
  44. s->context_modes = NULL;
  45. s->dist_context_map = NULL;
  46. s->context_map_slice = NULL;
  47. s->dist_context_map_slice = NULL;
  48. s->literal_hgroup.codes = NULL;
  49. s->literal_hgroup.htrees = NULL;
  50. s->insert_copy_hgroup.codes = NULL;
  51. s->insert_copy_hgroup.htrees = NULL;
  52. s->distance_hgroup.codes = NULL;
  53. s->distance_hgroup.htrees = NULL;
  54. s->is_last_metablock = 0;
  55. s->is_uncompressed = 0;
  56. s->is_metadata = 0;
  57. s->should_wrap_ringbuffer = 0;
  58. s->canny_ringbuffer_allocation = 1;
  59. s->window_bits = 0;
  60. s->max_distance = 0;
  61. s->dist_rb[0] = 16;
  62. s->dist_rb[1] = 15;
  63. s->dist_rb[2] = 11;
  64. s->dist_rb[3] = 4;
  65. s->dist_rb_idx = 0;
  66. s->block_type_trees = NULL;
  67. s->block_len_trees = NULL;
  68. s->mtf_upper_bound = 63;
  69. s->compound_dictionary = NULL;
  70. s->dictionary =
  71. BrotliSharedDictionaryCreateInstance(alloc_func, free_func, opaque);
  72. if (!s->dictionary) return BROTLI_FALSE;
  73. return BROTLI_TRUE;
  74. }
  75. void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {
  76. s->meta_block_remaining_len = 0;
  77. s->block_length[0] = 1U << 24;
  78. s->block_length[1] = 1U << 24;
  79. s->block_length[2] = 1U << 24;
  80. s->num_block_types[0] = 1;
  81. s->num_block_types[1] = 1;
  82. s->num_block_types[2] = 1;
  83. s->block_type_rb[0] = 1;
  84. s->block_type_rb[1] = 0;
  85. s->block_type_rb[2] = 1;
  86. s->block_type_rb[3] = 0;
  87. s->block_type_rb[4] = 1;
  88. s->block_type_rb[5] = 0;
  89. s->context_map = NULL;
  90. s->context_modes = NULL;
  91. s->dist_context_map = NULL;
  92. s->context_map_slice = NULL;
  93. s->literal_htree = NULL;
  94. s->dist_context_map_slice = NULL;
  95. s->dist_htree_index = 0;
  96. s->context_lookup = NULL;
  97. s->literal_hgroup.codes = NULL;
  98. s->literal_hgroup.htrees = NULL;
  99. s->insert_copy_hgroup.codes = NULL;
  100. s->insert_copy_hgroup.htrees = NULL;
  101. s->distance_hgroup.codes = NULL;
  102. s->distance_hgroup.htrees = NULL;
  103. }
  104. void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) {
  105. BROTLI_DECODER_FREE(s, s->context_modes);
  106. BROTLI_DECODER_FREE(s, s->context_map);
  107. BROTLI_DECODER_FREE(s, s->dist_context_map);
  108. BROTLI_DECODER_FREE(s, s->literal_hgroup.htrees);
  109. BROTLI_DECODER_FREE(s, s->insert_copy_hgroup.htrees);
  110. BROTLI_DECODER_FREE(s, s->distance_hgroup.htrees);
  111. }
  112. void BrotliDecoderStateCleanup(BrotliDecoderState* s) {
  113. BrotliDecoderStateCleanupAfterMetablock(s);
  114. BROTLI_DECODER_FREE(s, s->compound_dictionary);
  115. BrotliSharedDictionaryDestroyInstance(s->dictionary);
  116. s->dictionary = NULL;
  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_max,
  122. uint32_t alphabet_size_limit, uint32_t ntrees) {
  123. /* 376 = 256 (1-st level table) + 4 + 7 + 15 + 31 + 63 (2-nd level mix-tables)
  124. This number is discovered "unlimited" "enough" calculator; it is actually
  125. a wee bigger than required in several cases (especially for alphabets with
  126. less than 16 symbols). */
  127. const size_t max_table_size = alphabet_size_limit + 376;
  128. const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
  129. const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
  130. /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */
  131. HuffmanCode** p = (HuffmanCode**)BROTLI_DECODER_ALLOC(s,
  132. code_size + htree_size);
  133. group->alphabet_size_max = (uint16_t)alphabet_size_max;
  134. group->alphabet_size_limit = (uint16_t)alphabet_size_limit;
  135. group->num_htrees = (uint16_t)ntrees;
  136. group->htrees = p;
  137. group->codes = (HuffmanCode*)(&p[ntrees]);
  138. return !!p;
  139. }
  140. #if defined(__cplusplus) || defined(c_plusplus)
  141. } /* extern "C" */
  142. #endif