state.h 12 KB

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