state.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE;
  29. s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
  30. s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
  31. s->buffer_length = 0;
  32. s->loop_counter = 0;
  33. s->pos = 0;
  34. s->rb_roundtrips = 0;
  35. s->partial_pos_out = 0;
  36. s->block_type_trees = NULL;
  37. s->block_len_trees = NULL;
  38. s->ringbuffer = NULL;
  39. s->ringbuffer_size = 0;
  40. s->new_ringbuffer_size = 0;
  41. s->ringbuffer_mask = 0;
  42. s->context_map = NULL;
  43. s->context_modes = NULL;
  44. s->dist_context_map = NULL;
  45. s->context_map_slice = NULL;
  46. s->dist_context_map_slice = NULL;
  47. s->literal_hgroup.codes = NULL;
  48. s->literal_hgroup.htrees = NULL;
  49. s->insert_copy_hgroup.codes = NULL;
  50. s->insert_copy_hgroup.htrees = NULL;
  51. s->distance_hgroup.codes = NULL;
  52. s->distance_hgroup.htrees = NULL;
  53. s->is_last_metablock = 0;
  54. s->is_uncompressed = 0;
  55. s->is_metadata = 0;
  56. s->should_wrap_ringbuffer = 0;
  57. s->canny_ringbuffer_allocation = 1;
  58. s->window_bits = 0;
  59. s->max_distance = 0;
  60. s->dist_rb[0] = 16;
  61. s->dist_rb[1] = 15;
  62. s->dist_rb[2] = 11;
  63. s->dist_rb[3] = 4;
  64. s->dist_rb_idx = 0;
  65. s->block_type_trees = NULL;
  66. s->block_len_trees = NULL;
  67. s->mtf_upper_bound = 63;
  68. s->dictionary = BrotliGetDictionary();
  69. s->transforms = BrotliGetTransforms();
  70. return BROTLI_TRUE;
  71. }
  72. void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {
  73. s->meta_block_remaining_len = 0;
  74. s->block_length[0] = 1U << 24;
  75. s->block_length[1] = 1U << 24;
  76. s->block_length[2] = 1U << 24;
  77. s->num_block_types[0] = 1;
  78. s->num_block_types[1] = 1;
  79. s->num_block_types[2] = 1;
  80. s->block_type_rb[0] = 1;
  81. s->block_type_rb[1] = 0;
  82. s->block_type_rb[2] = 1;
  83. s->block_type_rb[3] = 0;
  84. s->block_type_rb[4] = 1;
  85. s->block_type_rb[5] = 0;
  86. s->context_map = NULL;
  87. s->context_modes = NULL;
  88. s->dist_context_map = NULL;
  89. s->context_map_slice = NULL;
  90. s->literal_htree = NULL;
  91. s->dist_context_map_slice = NULL;
  92. s->dist_htree_index = 0;
  93. s->context_lookup = NULL;
  94. s->literal_hgroup.codes = NULL;
  95. s->literal_hgroup.htrees = NULL;
  96. s->insert_copy_hgroup.codes = NULL;
  97. s->insert_copy_hgroup.htrees = NULL;
  98. s->distance_hgroup.codes = NULL;
  99. s->distance_hgroup.htrees = NULL;
  100. }
  101. void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) {
  102. BROTLI_DECODER_FREE(s, s->context_modes);
  103. BROTLI_DECODER_FREE(s, s->context_map);
  104. BROTLI_DECODER_FREE(s, s->dist_context_map);
  105. BROTLI_DECODER_FREE(s, s->literal_hgroup.htrees);
  106. BROTLI_DECODER_FREE(s, s->insert_copy_hgroup.htrees);
  107. BROTLI_DECODER_FREE(s, s->distance_hgroup.htrees);
  108. }
  109. void BrotliDecoderStateCleanup(BrotliDecoderState* s) {
  110. BrotliDecoderStateCleanupAfterMetablock(s);
  111. BROTLI_DECODER_FREE(s, s->ringbuffer);
  112. BROTLI_DECODER_FREE(s, s->block_type_trees);
  113. }
  114. BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,
  115. HuffmanTreeGroup* group, uint32_t alphabet_size_max,
  116. uint32_t alphabet_size_limit, uint32_t ntrees) {
  117. /* 376 = 256 (1-st level table) + 4 + 7 + 15 + 31 + 63 (2-nd level mix-tables)
  118. This number is discovered "unlimited" "enough" calculator; it is actually
  119. a wee bigger than required in several cases (especially for alphabets with
  120. less than 16 symbols). */
  121. const size_t max_table_size = alphabet_size_limit + 376;
  122. const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
  123. const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
  124. /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */
  125. HuffmanCode** p = (HuffmanCode**)BROTLI_DECODER_ALLOC(s,
  126. code_size + htree_size);
  127. group->alphabet_size_max = (uint16_t)alphabet_size_max;
  128. group->alphabet_size_limit = (uint16_t)alphabet_size_limit;
  129. group->num_htrees = (uint16_t)ntrees;
  130. group->htrees = p;
  131. group->codes = (HuffmanCode*)(&p[ntrees]);
  132. return !!p;
  133. }
  134. #if defined(__cplusplus) || defined(c_plusplus)
  135. } /* extern "C" */
  136. #endif