state.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/types.h"
  8. #include "./huffman.h"
  9. #if defined(__cplusplus) || defined(c_plusplus)
  10. extern "C" {
  11. #endif
  12. /* Declared in decode.h */
  13. int BrotliStateIsStreamStart(const BrotliState* s);
  14. int BrotliStateIsStreamEnd(const BrotliState* s);
  15. static void* DefaultAllocFunc(void* opaque, size_t size) {
  16. BROTLI_UNUSED(opaque);
  17. return malloc(size);
  18. }
  19. static void DefaultFreeFunc(void* opaque, void* address) {
  20. BROTLI_UNUSED(opaque);
  21. free(address);
  22. }
  23. void BrotliStateInit(BrotliState* s) {
  24. BrotliStateInitWithCustomAllocators(s, 0, 0, 0);
  25. }
  26. void BrotliStateInitWithCustomAllocators(BrotliState* s,
  27. brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
  28. if (!alloc_func) {
  29. s->alloc_func = DefaultAllocFunc;
  30. s->free_func = DefaultFreeFunc;
  31. s->memory_manager_opaque = 0;
  32. } else {
  33. s->alloc_func = alloc_func;
  34. s->free_func = free_func;
  35. s->memory_manager_opaque = opaque;
  36. }
  37. BrotliInitBitReader(&s->br);
  38. s->state = BROTLI_STATE_UNINITED;
  39. s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
  40. s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;
  41. s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;
  42. s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE;
  43. s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
  44. s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
  45. s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
  46. s->buffer_length = 0;
  47. s->loop_counter = 0;
  48. s->pos = 0;
  49. s->rb_roundtrips = 0;
  50. s->partial_pos_out = 0;
  51. s->block_type_trees = NULL;
  52. s->block_len_trees = NULL;
  53. s->ringbuffer = NULL;
  54. s->context_map = NULL;
  55. s->context_modes = NULL;
  56. s->dist_context_map = NULL;
  57. s->context_map_slice = NULL;
  58. s->dist_context_map_slice = NULL;
  59. s->sub_loop_counter = 0;
  60. s->literal_hgroup.codes = NULL;
  61. s->literal_hgroup.htrees = NULL;
  62. s->insert_copy_hgroup.codes = NULL;
  63. s->insert_copy_hgroup.htrees = NULL;
  64. s->distance_hgroup.codes = NULL;
  65. s->distance_hgroup.htrees = NULL;
  66. s->custom_dict = NULL;
  67. s->custom_dict_size = 0;
  68. s->is_last_metablock = 0;
  69. s->window_bits = 0;
  70. s->max_distance = 0;
  71. s->dist_rb[0] = 16;
  72. s->dist_rb[1] = 15;
  73. s->dist_rb[2] = 11;
  74. s->dist_rb[3] = 4;
  75. s->dist_rb_idx = 0;
  76. s->block_type_trees = NULL;
  77. s->block_len_trees = NULL;
  78. /* Make small negative indexes addressable. */
  79. s->symbol_lists = &s->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1];
  80. s->mtf_upper_bound = 255;
  81. }
  82. void BrotliStateMetablockBegin(BrotliState* s) {
  83. s->meta_block_remaining_len = 0;
  84. s->block_length[0] = 1U << 28;
  85. s->block_length[1] = 1U << 28;
  86. s->block_length[2] = 1U << 28;
  87. s->num_block_types[0] = 1;
  88. s->num_block_types[1] = 1;
  89. s->num_block_types[2] = 1;
  90. s->block_type_rb[0] = 1;
  91. s->block_type_rb[1] = 0;
  92. s->block_type_rb[2] = 1;
  93. s->block_type_rb[3] = 0;
  94. s->block_type_rb[4] = 1;
  95. s->block_type_rb[5] = 0;
  96. s->context_map = NULL;
  97. s->context_modes = NULL;
  98. s->dist_context_map = NULL;
  99. s->context_map_slice = NULL;
  100. s->literal_htree = NULL;
  101. s->dist_context_map_slice = NULL;
  102. s->dist_htree_index = 0;
  103. s->context_lookup1 = NULL;
  104. s->context_lookup2 = NULL;
  105. s->literal_hgroup.codes = NULL;
  106. s->literal_hgroup.htrees = NULL;
  107. s->insert_copy_hgroup.codes = NULL;
  108. s->insert_copy_hgroup.htrees = NULL;
  109. s->distance_hgroup.codes = NULL;
  110. s->distance_hgroup.htrees = NULL;
  111. }
  112. void BrotliStateCleanupAfterMetablock(BrotliState* s) {
  113. BROTLI_FREE(s, s->context_modes);
  114. BROTLI_FREE(s, s->context_map);
  115. BROTLI_FREE(s, s->dist_context_map);
  116. BrotliHuffmanTreeGroupRelease(s, &s->literal_hgroup);
  117. BrotliHuffmanTreeGroupRelease(s, &s->insert_copy_hgroup);
  118. BrotliHuffmanTreeGroupRelease(s, &s->distance_hgroup);
  119. }
  120. void BrotliStateCleanup(BrotliState* s) {
  121. BrotliStateCleanupAfterMetablock(s);
  122. BROTLI_FREE(s, s->ringbuffer);
  123. BROTLI_FREE(s, s->block_type_trees);
  124. }
  125. int BrotliStateIsStreamStart(const BrotliState* s) {
  126. return (s->state == BROTLI_STATE_UNINITED &&
  127. BrotliGetAvailableBits(&s->br) == 0);
  128. }
  129. int BrotliStateIsStreamEnd(const BrotliState* s) {
  130. return s->state == BROTLI_STATE_DONE;
  131. }
  132. void BrotliHuffmanTreeGroupInit(BrotliState* s, HuffmanTreeGroup* group,
  133. uint32_t alphabet_size, uint32_t ntrees) {
  134. /* Pack two allocations into one */
  135. const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5];
  136. const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
  137. const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
  138. char* p = (char*)BROTLI_ALLOC(s, code_size + htree_size);
  139. group->alphabet_size = (uint16_t)alphabet_size;
  140. group->num_htrees = (uint16_t)ntrees;
  141. group->codes = (HuffmanCode*)p;
  142. group->htrees = (HuffmanCode**)(p + code_size);
  143. }
  144. void BrotliHuffmanTreeGroupRelease(BrotliState* s, HuffmanTreeGroup* group) {
  145. BROTLI_FREE(s, group->codes);
  146. group->htrees = NULL;
  147. }
  148. #if defined(__cplusplus) || defined(c_plusplus)
  149. } /* extern "C" */
  150. #endif