state.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "../common/dictionary.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->used_input = 0;
  38. s->block_type_trees = NULL;
  39. s->block_len_trees = NULL;
  40. s->ringbuffer = NULL;
  41. s->ringbuffer_size = 0;
  42. s->new_ringbuffer_size = 0;
  43. s->ringbuffer_mask = 0;
  44. s->context_map = NULL;
  45. s->context_modes = NULL;
  46. s->dist_context_map = NULL;
  47. s->context_map_slice = NULL;
  48. s->dist_context_map_slice = NULL;
  49. s->literal_hgroup.codes = NULL;
  50. s->literal_hgroup.htrees = NULL;
  51. s->insert_copy_hgroup.codes = NULL;
  52. s->insert_copy_hgroup.htrees = NULL;
  53. s->distance_hgroup.codes = NULL;
  54. s->distance_hgroup.htrees = NULL;
  55. s->is_last_metablock = 0;
  56. s->is_uncompressed = 0;
  57. s->is_metadata = 0;
  58. s->should_wrap_ringbuffer = 0;
  59. s->canny_ringbuffer_allocation = 1;
  60. s->window_bits = 0;
  61. s->max_distance = 0;
  62. s->dist_rb[0] = 16;
  63. s->dist_rb[1] = 15;
  64. s->dist_rb[2] = 11;
  65. s->dist_rb[3] = 4;
  66. s->dist_rb_idx = 0;
  67. s->block_type_trees = NULL;
  68. s->block_len_trees = NULL;
  69. s->mtf_upper_bound = 63;
  70. s->compound_dictionary = NULL;
  71. s->dictionary =
  72. BrotliSharedDictionaryCreateInstance(alloc_func, free_func, opaque);
  73. if (!s->dictionary) return BROTLI_FALSE;
  74. s->metadata_start_func = NULL;
  75. s->metadata_chunk_func = NULL;
  76. s->metadata_callback_opaque = 0;
  77. return BROTLI_TRUE;
  78. }
  79. void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {
  80. s->meta_block_remaining_len = 0;
  81. s->block_length[0] = 1U << 24;
  82. s->block_length[1] = 1U << 24;
  83. s->block_length[2] = 1U << 24;
  84. s->num_block_types[0] = 1;
  85. s->num_block_types[1] = 1;
  86. s->num_block_types[2] = 1;
  87. s->block_type_rb[0] = 1;
  88. s->block_type_rb[1] = 0;
  89. s->block_type_rb[2] = 1;
  90. s->block_type_rb[3] = 0;
  91. s->block_type_rb[4] = 1;
  92. s->block_type_rb[5] = 0;
  93. s->context_map = NULL;
  94. s->context_modes = NULL;
  95. s->dist_context_map = NULL;
  96. s->context_map_slice = NULL;
  97. s->literal_htree = NULL;
  98. s->dist_context_map_slice = NULL;
  99. s->dist_htree_index = 0;
  100. s->context_lookup = NULL;
  101. s->literal_hgroup.codes = NULL;
  102. s->literal_hgroup.htrees = NULL;
  103. s->insert_copy_hgroup.codes = NULL;
  104. s->insert_copy_hgroup.htrees = NULL;
  105. s->distance_hgroup.codes = NULL;
  106. s->distance_hgroup.htrees = NULL;
  107. }
  108. void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) {
  109. BROTLI_DECODER_FREE(s, s->context_modes);
  110. BROTLI_DECODER_FREE(s, s->context_map);
  111. BROTLI_DECODER_FREE(s, s->dist_context_map);
  112. BROTLI_DECODER_FREE(s, s->literal_hgroup.htrees);
  113. BROTLI_DECODER_FREE(s, s->insert_copy_hgroup.htrees);
  114. BROTLI_DECODER_FREE(s, s->distance_hgroup.htrees);
  115. }
  116. #ifdef BROTLI_REPORTING
  117. /* When BROTLI_REPORTING is defined extra reporting module have to be linked. */
  118. void BrotliDecoderOnFinish(const BrotliDecoderState* s);
  119. #define BROTLI_DECODER_ON_FINISH(s) BrotliDecoderOnFinish(s);
  120. #else
  121. #if !defined(BROTLI_DECODER_ON_FINISH)
  122. #define BROTLI_DECODER_ON_FINISH(s) (void)(s);
  123. #endif
  124. #endif
  125. void BrotliDecoderStateCleanup(BrotliDecoderState* s) {
  126. BrotliDecoderStateCleanupAfterMetablock(s);
  127. BROTLI_DECODER_ON_FINISH(s);
  128. BROTLI_DECODER_FREE(s, s->compound_dictionary);
  129. BrotliSharedDictionaryDestroyInstance(s->dictionary);
  130. s->dictionary = NULL;
  131. BROTLI_DECODER_FREE(s, s->ringbuffer);
  132. BROTLI_DECODER_FREE(s, s->block_type_trees);
  133. }
  134. BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,
  135. HuffmanTreeGroup* group, uint32_t alphabet_size_max,
  136. uint32_t alphabet_size_limit, uint32_t ntrees) {
  137. /* 376 = 256 (1-st level table) + 4 + 7 + 15 + 31 + 63 (2-nd level mix-tables)
  138. This number is discovered "unlimited" "enough" calculator; it is actually
  139. a wee bigger than required in several cases (especially for alphabets with
  140. less than 16 symbols). */
  141. const size_t max_table_size = alphabet_size_limit + 376;
  142. const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
  143. const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
  144. /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */
  145. HuffmanCode** p = (HuffmanCode**)BROTLI_DECODER_ALLOC(s,
  146. code_size + htree_size);
  147. group->alphabet_size_max = (uint16_t)alphabet_size_max;
  148. group->alphabet_size_limit = (uint16_t)alphabet_size_limit;
  149. group->num_htrees = (uint16_t)ntrees;
  150. group->htrees = p;
  151. group->codes = (HuffmanCode*)(&p[ntrees]);
  152. return !!p;
  153. }
  154. #if defined(__cplusplus) || defined(c_plusplus)
  155. } /* extern "C" */
  156. #endif