state.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. /* Graphviz diagram that describes state transitions:
  19. digraph States {
  20. graph [compound=true]
  21. concentrate=true
  22. node [shape="box"]
  23. UNINITED -> {LARGE_WINDOW_BITS -> INITIALIZE}
  24. subgraph cluster_metablock_workflow {
  25. style="rounded"
  26. label=< <B>METABLOCK CYCLE</B> >
  27. METABLOCK_BEGIN -> METABLOCK_HEADER
  28. METABLOCK_HEADER:sw -> METADATA
  29. METABLOCK_HEADER:s -> UNCOMPRESSED
  30. METABLOCK_HEADER:se -> METABLOCK_DONE:ne
  31. METADATA:s -> METABLOCK_DONE:w
  32. UNCOMPRESSED:s -> METABLOCK_DONE:n
  33. METABLOCK_DONE:e -> METABLOCK_BEGIN:e [constraint="false"]
  34. }
  35. INITIALIZE -> METABLOCK_BEGIN
  36. METABLOCK_DONE -> DONE
  37. subgraph cluster_compressed_metablock {
  38. style="rounded"
  39. label=< <B>COMPRESSED METABLOCK</B> >
  40. subgraph cluster_command {
  41. style="rounded"
  42. label=< <B>HOT LOOP</B> >
  43. _METABLOCK_DONE_PORT_ [shape=point style=invis]
  44. {
  45. // Set different shape for nodes returning from "compressed metablock".
  46. node [shape=invhouse]; CMD_INNER CMD_POST_DECODE_LITERALS;
  47. CMD_POST_WRAP_COPY; CMD_INNER_WRITE; CMD_POST_WRITE_1;
  48. }
  49. CMD_BEGIN -> CMD_INNER -> CMD_POST_DECODE_LITERALS -> CMD_POST_WRAP_COPY
  50. // IO ("write") nodes are not in the hot loop!
  51. CMD_INNER_WRITE [style=dashed]
  52. CMD_INNER -> CMD_INNER_WRITE
  53. CMD_POST_WRITE_1 [style=dashed]
  54. CMD_POST_DECODE_LITERALS -> CMD_POST_WRITE_1
  55. CMD_POST_WRITE_2 [style=dashed]
  56. CMD_POST_WRAP_COPY -> CMD_POST_WRITE_2
  57. CMD_POST_WRITE_1 -> CMD_BEGIN:s [constraint="false"]
  58. CMD_INNER_WRITE -> {CMD_INNER CMD_POST_DECODE_LITERALS}
  59. [constraint="false"]
  60. CMD_BEGIN:ne -> CMD_POST_DECODE_LITERALS [constraint="false"]
  61. CMD_POST_WRAP_COPY -> CMD_BEGIN [constraint="false"]
  62. CMD_POST_DECODE_LITERALS -> CMD_BEGIN:ne [constraint="false"]
  63. CMD_POST_WRITE_2 -> CMD_POST_WRAP_COPY [constraint="false"]
  64. {rank=same; CMD_BEGIN; CMD_INNER; CMD_POST_DECODE_LITERALS;
  65. CMD_POST_WRAP_COPY}
  66. {rank=same; CMD_INNER_WRITE; CMD_POST_WRITE_1; CMD_POST_WRITE_2}
  67. {CMD_INNER CMD_POST_DECODE_LITERALS CMD_POST_WRAP_COPY} ->
  68. _METABLOCK_DONE_PORT_ [style=invis]
  69. {CMD_INNER_WRITE CMD_POST_WRITE_1} -> _METABLOCK_DONE_PORT_
  70. [constraint="false" style=invis]
  71. }
  72. BEFORE_COMPRESSED_METABLOCK_HEADER:s -> HUFFMAN_CODE_0:n
  73. HUFFMAN_CODE_0 -> HUFFMAN_CODE_1 -> HUFFMAN_CODE_2 -> HUFFMAN_CODE_3
  74. HUFFMAN_CODE_0 -> METABLOCK_HEADER_2 -> CONTEXT_MODES -> CONTEXT_MAP_1
  75. CONTEXT_MAP_1 -> CONTEXT_MAP_2 -> TREE_GROUP
  76. TREE_GROUP -> BEFORE_COMPRESSED_METABLOCK_BODY:e
  77. BEFORE_COMPRESSED_METABLOCK_BODY:s -> CMD_BEGIN:n
  78. HUFFMAN_CODE_3:e -> HUFFMAN_CODE_0:ne [constraint="false"]
  79. {rank=same; HUFFMAN_CODE_0; HUFFMAN_CODE_1; HUFFMAN_CODE_2; HUFFMAN_CODE_3}
  80. {rank=same; METABLOCK_HEADER_2; CONTEXT_MODES; CONTEXT_MAP_1; CONTEXT_MAP_2;
  81. TREE_GROUP}
  82. }
  83. METABLOCK_HEADER:e -> BEFORE_COMPRESSED_METABLOCK_HEADER:n
  84. _METABLOCK_DONE_PORT_ -> METABLOCK_DONE:se
  85. [constraint="false" ltail=cluster_command]
  86. UNINITED [shape=Mdiamond];
  87. DONE [shape=Msquare];
  88. }
  89. */
  90. typedef enum {
  91. BROTLI_STATE_UNINITED,
  92. BROTLI_STATE_LARGE_WINDOW_BITS,
  93. BROTLI_STATE_INITIALIZE,
  94. BROTLI_STATE_METABLOCK_BEGIN,
  95. BROTLI_STATE_METABLOCK_HEADER,
  96. BROTLI_STATE_METABLOCK_HEADER_2,
  97. BROTLI_STATE_CONTEXT_MODES,
  98. BROTLI_STATE_COMMAND_BEGIN,
  99. BROTLI_STATE_COMMAND_INNER,
  100. BROTLI_STATE_COMMAND_POST_DECODE_LITERALS,
  101. BROTLI_STATE_COMMAND_POST_WRAP_COPY,
  102. BROTLI_STATE_UNCOMPRESSED,
  103. BROTLI_STATE_METADATA,
  104. BROTLI_STATE_COMMAND_INNER_WRITE,
  105. BROTLI_STATE_METABLOCK_DONE,
  106. BROTLI_STATE_COMMAND_POST_WRITE_1,
  107. BROTLI_STATE_COMMAND_POST_WRITE_2,
  108. BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_HEADER,
  109. BROTLI_STATE_HUFFMAN_CODE_0,
  110. BROTLI_STATE_HUFFMAN_CODE_1,
  111. BROTLI_STATE_HUFFMAN_CODE_2,
  112. BROTLI_STATE_HUFFMAN_CODE_3,
  113. BROTLI_STATE_CONTEXT_MAP_1,
  114. BROTLI_STATE_CONTEXT_MAP_2,
  115. BROTLI_STATE_TREE_GROUP,
  116. BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_BODY,
  117. BROTLI_STATE_DONE
  118. } BrotliRunningState;
  119. typedef enum {
  120. BROTLI_STATE_METABLOCK_HEADER_NONE,
  121. BROTLI_STATE_METABLOCK_HEADER_EMPTY,
  122. BROTLI_STATE_METABLOCK_HEADER_NIBBLES,
  123. BROTLI_STATE_METABLOCK_HEADER_SIZE,
  124. BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED,
  125. BROTLI_STATE_METABLOCK_HEADER_RESERVED,
  126. BROTLI_STATE_METABLOCK_HEADER_BYTES,
  127. BROTLI_STATE_METABLOCK_HEADER_METADATA
  128. } BrotliRunningMetablockHeaderState;
  129. typedef enum {
  130. BROTLI_STATE_UNCOMPRESSED_NONE,
  131. BROTLI_STATE_UNCOMPRESSED_WRITE
  132. } BrotliRunningUncompressedState;
  133. typedef enum {
  134. BROTLI_STATE_TREE_GROUP_NONE,
  135. BROTLI_STATE_TREE_GROUP_LOOP
  136. } BrotliRunningTreeGroupState;
  137. typedef enum {
  138. BROTLI_STATE_CONTEXT_MAP_NONE,
  139. BROTLI_STATE_CONTEXT_MAP_READ_PREFIX,
  140. BROTLI_STATE_CONTEXT_MAP_HUFFMAN,
  141. BROTLI_STATE_CONTEXT_MAP_DECODE,
  142. BROTLI_STATE_CONTEXT_MAP_TRANSFORM
  143. } BrotliRunningContextMapState;
  144. typedef enum {
  145. BROTLI_STATE_HUFFMAN_NONE,
  146. BROTLI_STATE_HUFFMAN_SIMPLE_SIZE,
  147. BROTLI_STATE_HUFFMAN_SIMPLE_READ,
  148. BROTLI_STATE_HUFFMAN_SIMPLE_BUILD,
  149. BROTLI_STATE_HUFFMAN_COMPLEX,
  150. BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS
  151. } BrotliRunningHuffmanState;
  152. typedef enum {
  153. BROTLI_STATE_DECODE_UINT8_NONE,
  154. BROTLI_STATE_DECODE_UINT8_SHORT,
  155. BROTLI_STATE_DECODE_UINT8_LONG
  156. } BrotliRunningDecodeUint8State;
  157. typedef enum {
  158. BROTLI_STATE_READ_BLOCK_LENGTH_NONE,
  159. BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX
  160. } BrotliRunningReadBlockLengthState;
  161. typedef struct BrotliMetablockHeaderArena {
  162. BrotliRunningTreeGroupState substate_tree_group;
  163. BrotliRunningContextMapState substate_context_map;
  164. BrotliRunningHuffmanState substate_huffman;
  165. uint32_t sub_loop_counter;
  166. uint32_t repeat_code_len;
  167. uint32_t prev_code_len;
  168. /* For ReadHuffmanCode. */
  169. uint32_t symbol;
  170. uint32_t repeat;
  171. uint32_t space;
  172. /* Huffman table for "histograms". */
  173. HuffmanCode table[32];
  174. /* List of heads of symbol chains. */
  175. uint16_t* symbol_lists;
  176. /* Storage from symbol_lists. */
  177. uint16_t symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1 +
  178. BROTLI_NUM_COMMAND_SYMBOLS];
  179. /* Tails of symbol chains. */
  180. int next_symbol[32];
  181. uint8_t code_length_code_lengths[BROTLI_CODE_LENGTH_CODES];
  182. /* Population counts for the code lengths. */
  183. uint16_t code_length_histo[16];
  184. /* For HuffmanTreeGroupDecode. */
  185. int htree_index;
  186. HuffmanCode* next;
  187. /* For DecodeContextMap. */
  188. uint32_t context_index;
  189. uint32_t max_run_length_prefix;
  190. uint32_t code;
  191. HuffmanCode context_map_table[BROTLI_HUFFMAN_MAX_SIZE_272];
  192. } BrotliMetablockHeaderArena;
  193. typedef struct BrotliMetablockBodyArena {
  194. uint8_t dist_extra_bits[544];
  195. uint32_t dist_offset[544];
  196. } BrotliMetablockBodyArena;
  197. struct BrotliDecoderStateStruct {
  198. BrotliRunningState state;
  199. /* This counter is reused for several disjoint loops. */
  200. int loop_counter;
  201. BrotliBitReader br;
  202. brotli_alloc_func alloc_func;
  203. brotli_free_func free_func;
  204. void* memory_manager_opaque;
  205. /* Temporary storage for remaining input. Brotli stream format is designed in
  206. a way, that 64 bits are enough to make progress in decoding. */
  207. union {
  208. uint64_t u64;
  209. uint8_t u8[8];
  210. } buffer;
  211. uint32_t buffer_length;
  212. int pos;
  213. int max_backward_distance;
  214. int max_distance;
  215. int ringbuffer_size;
  216. int ringbuffer_mask;
  217. int dist_rb_idx;
  218. int dist_rb[4];
  219. int error_code;
  220. uint8_t* ringbuffer;
  221. uint8_t* ringbuffer_end;
  222. HuffmanCode* htree_command;
  223. const uint8_t* context_lookup;
  224. uint8_t* context_map_slice;
  225. uint8_t* dist_context_map_slice;
  226. /* This ring buffer holds a few past copy distances that will be used by
  227. some special distance codes. */
  228. HuffmanTreeGroup literal_hgroup;
  229. HuffmanTreeGroup insert_copy_hgroup;
  230. HuffmanTreeGroup distance_hgroup;
  231. HuffmanCode* block_type_trees;
  232. HuffmanCode* block_len_trees;
  233. /* This is true if the literal context map histogram type always matches the
  234. block type. It is then not needed to keep the context (faster decoding). */
  235. int trivial_literal_context;
  236. /* Distance context is actual after command is decoded and before distance is
  237. computed. After distance computation it is used as a temporary variable. */
  238. int distance_context;
  239. int meta_block_remaining_len;
  240. uint32_t block_length_index;
  241. uint32_t block_length[3];
  242. uint32_t num_block_types[3];
  243. uint32_t block_type_rb[6];
  244. uint32_t distance_postfix_bits;
  245. uint32_t num_direct_distance_codes;
  246. uint32_t num_dist_htrees;
  247. uint8_t* dist_context_map;
  248. HuffmanCode* literal_htree;
  249. uint8_t dist_htree_index;
  250. int copy_length;
  251. int distance_code;
  252. /* For partial write operations. */
  253. size_t rb_roundtrips; /* how many times we went around the ring-buffer */
  254. size_t partial_pos_out; /* how much output to the user in total */
  255. /* For InverseMoveToFrontTransform. */
  256. uint32_t mtf_upper_bound;
  257. uint32_t mtf[64 + 1];
  258. /* Less used attributes are at the end of this struct. */
  259. /* States inside function calls. */
  260. BrotliRunningMetablockHeaderState substate_metablock_header;
  261. BrotliRunningUncompressedState substate_uncompressed;
  262. BrotliRunningDecodeUint8State substate_decode_uint8;
  263. BrotliRunningReadBlockLengthState substate_read_block_length;
  264. unsigned int is_last_metablock : 1;
  265. unsigned int is_uncompressed : 1;
  266. unsigned int is_metadata : 1;
  267. unsigned int should_wrap_ringbuffer : 1;
  268. unsigned int canny_ringbuffer_allocation : 1;
  269. unsigned int large_window : 1;
  270. unsigned int size_nibbles : 8;
  271. uint32_t window_bits;
  272. int new_ringbuffer_size;
  273. uint32_t num_literal_htrees;
  274. uint8_t* context_map;
  275. uint8_t* context_modes;
  276. const BrotliDictionary* dictionary;
  277. const BrotliTransforms* transforms;
  278. uint32_t trivial_literal_contexts[8]; /* 256 bits */
  279. union {
  280. BrotliMetablockHeaderArena header;
  281. BrotliMetablockBodyArena body;
  282. } arena;
  283. };
  284. typedef struct BrotliDecoderStateStruct BrotliDecoderStateInternal;
  285. #define BrotliDecoderState BrotliDecoderStateInternal
  286. BROTLI_INTERNAL BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s,
  287. brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
  288. BROTLI_INTERNAL void BrotliDecoderStateCleanup(BrotliDecoderState* s);
  289. BROTLI_INTERNAL void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s);
  290. BROTLI_INTERNAL void BrotliDecoderStateCleanupAfterMetablock(
  291. BrotliDecoderState* s);
  292. BROTLI_INTERNAL BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(
  293. BrotliDecoderState* s, HuffmanTreeGroup* group, uint32_t alphabet_size_max,
  294. uint32_t alphabet_size_limit, uint32_t ntrees);
  295. #define BROTLI_DECODER_ALLOC(S, L) S->alloc_func(S->memory_manager_opaque, L)
  296. #define BROTLI_DECODER_FREE(S, X) { \
  297. S->free_func(S->memory_manager_opaque, X); \
  298. X = NULL; \
  299. }
  300. #if defined(__cplusplus) || defined(c_plusplus)
  301. } /* extern "C" */
  302. #endif
  303. #endif /* BROTLI_DEC_STATE_H_ */