state.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. /* Brotli state for partial streaming decoding. */
  6. #ifndef BROTLI_DEC_STATE_H_
  7. #define BROTLI_DEC_STATE_H_
  8. #include "../common/constants.h"
  9. #include "../common/dictionary.h"
  10. #include "../common/platform.h"
  11. #include "../common/transform.h"
  12. #include <brotli/types.h>
  13. #include "./bit_reader.h"
  14. #include "./huffman.h"
  15. #if defined(__cplusplus) || defined(c_plusplus)
  16. extern "C" {
  17. #endif
  18. typedef enum {
  19. BROTLI_STATE_UNINITED,
  20. BROTLI_STATE_LARGE_WINDOW_BITS,
  21. BROTLI_STATE_INITIALIZE,
  22. BROTLI_STATE_METABLOCK_BEGIN,
  23. BROTLI_STATE_METABLOCK_HEADER,
  24. BROTLI_STATE_METABLOCK_HEADER_2,
  25. BROTLI_STATE_CONTEXT_MODES,
  26. BROTLI_STATE_COMMAND_BEGIN,
  27. BROTLI_STATE_COMMAND_INNER,
  28. BROTLI_STATE_COMMAND_POST_DECODE_LITERALS,
  29. BROTLI_STATE_COMMAND_POST_WRAP_COPY,
  30. BROTLI_STATE_UNCOMPRESSED,
  31. BROTLI_STATE_METADATA,
  32. BROTLI_STATE_COMMAND_INNER_WRITE,
  33. BROTLI_STATE_METABLOCK_DONE,
  34. BROTLI_STATE_COMMAND_POST_WRITE_1,
  35. BROTLI_STATE_COMMAND_POST_WRITE_2,
  36. BROTLI_STATE_HUFFMAN_CODE_0,
  37. BROTLI_STATE_HUFFMAN_CODE_1,
  38. BROTLI_STATE_HUFFMAN_CODE_2,
  39. BROTLI_STATE_HUFFMAN_CODE_3,
  40. BROTLI_STATE_CONTEXT_MAP_1,
  41. BROTLI_STATE_CONTEXT_MAP_2,
  42. BROTLI_STATE_TREE_GROUP,
  43. BROTLI_STATE_DONE
  44. } BrotliRunningState;
  45. typedef enum {
  46. BROTLI_STATE_METABLOCK_HEADER_NONE,
  47. BROTLI_STATE_METABLOCK_HEADER_EMPTY,
  48. BROTLI_STATE_METABLOCK_HEADER_NIBBLES,
  49. BROTLI_STATE_METABLOCK_HEADER_SIZE,
  50. BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED,
  51. BROTLI_STATE_METABLOCK_HEADER_RESERVED,
  52. BROTLI_STATE_METABLOCK_HEADER_BYTES,
  53. BROTLI_STATE_METABLOCK_HEADER_METADATA
  54. } BrotliRunningMetablockHeaderState;
  55. typedef enum {
  56. BROTLI_STATE_UNCOMPRESSED_NONE,
  57. BROTLI_STATE_UNCOMPRESSED_WRITE
  58. } BrotliRunningUncompressedState;
  59. typedef enum {
  60. BROTLI_STATE_TREE_GROUP_NONE,
  61. BROTLI_STATE_TREE_GROUP_LOOP
  62. } BrotliRunningTreeGroupState;
  63. typedef enum {
  64. BROTLI_STATE_CONTEXT_MAP_NONE,
  65. BROTLI_STATE_CONTEXT_MAP_READ_PREFIX,
  66. BROTLI_STATE_CONTEXT_MAP_HUFFMAN,
  67. BROTLI_STATE_CONTEXT_MAP_DECODE,
  68. BROTLI_STATE_CONTEXT_MAP_TRANSFORM
  69. } BrotliRunningContextMapState;
  70. typedef enum {
  71. BROTLI_STATE_HUFFMAN_NONE,
  72. BROTLI_STATE_HUFFMAN_SIMPLE_SIZE,
  73. BROTLI_STATE_HUFFMAN_SIMPLE_READ,
  74. BROTLI_STATE_HUFFMAN_SIMPLE_BUILD,
  75. BROTLI_STATE_HUFFMAN_COMPLEX,
  76. BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS
  77. } BrotliRunningHuffmanState;
  78. typedef enum {
  79. BROTLI_STATE_DECODE_UINT8_NONE,
  80. BROTLI_STATE_DECODE_UINT8_SHORT,
  81. BROTLI_STATE_DECODE_UINT8_LONG
  82. } BrotliRunningDecodeUint8State;
  83. typedef enum {
  84. BROTLI_STATE_READ_BLOCK_LENGTH_NONE,
  85. BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX
  86. } BrotliRunningReadBlockLengthState;
  87. struct BrotliDecoderStateStruct {
  88. BrotliRunningState state;
  89. /* This counter is reused for several disjoint loops. */
  90. int loop_counter;
  91. BrotliBitReader br;
  92. brotli_alloc_func alloc_func;
  93. brotli_free_func free_func;
  94. void* memory_manager_opaque;
  95. /* Temporary storage for remaining input. */
  96. union {
  97. uint64_t u64;
  98. uint8_t u8[8];
  99. } buffer;
  100. uint32_t buffer_length;
  101. int pos;
  102. int max_backward_distance;
  103. int max_distance;
  104. int ringbuffer_size;
  105. int ringbuffer_mask;
  106. int dist_rb_idx;
  107. int dist_rb[4];
  108. int error_code;
  109. uint32_t sub_loop_counter;
  110. uint8_t* ringbuffer;
  111. uint8_t* ringbuffer_end;
  112. HuffmanCode* htree_command;
  113. const uint8_t* context_lookup;
  114. uint8_t* context_map_slice;
  115. uint8_t* dist_context_map_slice;
  116. /* This ring buffer holds a few past copy distances that will be used by
  117. some special distance codes. */
  118. HuffmanTreeGroup literal_hgroup;
  119. HuffmanTreeGroup insert_copy_hgroup;
  120. HuffmanTreeGroup distance_hgroup;
  121. HuffmanCode* block_type_trees;
  122. HuffmanCode* block_len_trees;
  123. /* This is true if the literal context map histogram type always matches the
  124. block type. It is then not needed to keep the context (faster decoding). */
  125. int trivial_literal_context;
  126. /* Distance context is actual after command is decoded and before distance is
  127. computed. After distance computation it is used as a temporary variable. */
  128. int distance_context;
  129. int meta_block_remaining_len;
  130. uint32_t block_length_index;
  131. uint32_t block_length[3];
  132. uint32_t num_block_types[3];
  133. uint32_t block_type_rb[6];
  134. uint32_t distance_postfix_bits;
  135. uint32_t num_direct_distance_codes;
  136. int distance_postfix_mask;
  137. uint32_t num_dist_htrees;
  138. uint8_t* dist_context_map;
  139. HuffmanCode* literal_htree;
  140. uint8_t dist_htree_index;
  141. uint32_t repeat_code_len;
  142. uint32_t prev_code_len;
  143. int copy_length;
  144. int distance_code;
  145. /* For partial write operations. */
  146. size_t rb_roundtrips; /* how many times we went around the ring-buffer */
  147. size_t partial_pos_out; /* how much output to the user in total */
  148. /* For ReadHuffmanCode. */
  149. uint32_t symbol;
  150. uint32_t repeat;
  151. uint32_t space;
  152. HuffmanCode table[32];
  153. /* List of heads of symbol chains. */
  154. uint16_t* symbol_lists;
  155. /* Storage from symbol_lists. */
  156. uint16_t symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1 +
  157. BROTLI_NUM_COMMAND_SYMBOLS];
  158. /* Tails of symbol chains. */
  159. int next_symbol[32];
  160. uint8_t code_length_code_lengths[BROTLI_CODE_LENGTH_CODES];
  161. /* Population counts for the code lengths. */
  162. uint16_t code_length_histo[16];
  163. /* For HuffmanTreeGroupDecode. */
  164. int htree_index;
  165. HuffmanCode* next;
  166. /* For DecodeContextMap. */
  167. uint32_t context_index;
  168. uint32_t max_run_length_prefix;
  169. uint32_t code;
  170. HuffmanCode context_map_table[BROTLI_HUFFMAN_MAX_SIZE_272];
  171. /* For InverseMoveToFrontTransform. */
  172. uint32_t mtf_upper_bound;
  173. uint32_t mtf[64 + 1];
  174. /* Less used attributes are at the end of this struct. */
  175. /* States inside function calls. */
  176. BrotliRunningMetablockHeaderState substate_metablock_header;
  177. BrotliRunningTreeGroupState substate_tree_group;
  178. BrotliRunningContextMapState substate_context_map;
  179. BrotliRunningUncompressedState substate_uncompressed;
  180. BrotliRunningHuffmanState substate_huffman;
  181. BrotliRunningDecodeUint8State substate_decode_uint8;
  182. BrotliRunningReadBlockLengthState substate_read_block_length;
  183. unsigned int is_last_metablock : 1;
  184. unsigned int is_uncompressed : 1;
  185. unsigned int is_metadata : 1;
  186. unsigned int should_wrap_ringbuffer : 1;
  187. unsigned int canny_ringbuffer_allocation : 1;
  188. unsigned int large_window : 1;
  189. unsigned int size_nibbles : 8;
  190. uint32_t window_bits;
  191. int new_ringbuffer_size;
  192. uint32_t num_literal_htrees;
  193. uint8_t* context_map;
  194. uint8_t* context_modes;
  195. const BrotliDictionary* dictionary;
  196. const BrotliTransforms* transforms;
  197. uint32_t trivial_literal_contexts[8]; /* 256 bits */
  198. };
  199. typedef struct BrotliDecoderStateStruct BrotliDecoderStateInternal;
  200. #define BrotliDecoderState BrotliDecoderStateInternal
  201. BROTLI_INTERNAL BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s,
  202. brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
  203. BROTLI_INTERNAL void BrotliDecoderStateCleanup(BrotliDecoderState* s);
  204. BROTLI_INTERNAL void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s);
  205. BROTLI_INTERNAL void BrotliDecoderStateCleanupAfterMetablock(
  206. BrotliDecoderState* s);
  207. BROTLI_INTERNAL BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(
  208. BrotliDecoderState* s, HuffmanTreeGroup* group, uint32_t alphabet_size,
  209. uint32_t max_symbol, uint32_t ntrees);
  210. #define BROTLI_DECODER_ALLOC(S, L) S->alloc_func(S->memory_manager_opaque, L)
  211. #define BROTLI_DECODER_FREE(S, X) { \
  212. S->free_func(S->memory_manager_opaque, X); \
  213. X = NULL; \
  214. }
  215. #if defined(__cplusplus) || defined(c_plusplus)
  216. } /* extern "C" */
  217. #endif
  218. #endif /* BROTLI_DEC_STATE_H_ */