• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2022 Google Inc. All Rights Reserved.
2 
3    Distributed under MIT license.
4    See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5 */
6 
7 /* Encoder state. */
8 
9 #ifndef BROTLI_ENC_STATE_H_
10 #define BROTLI_ENC_STATE_H_
11 
12 #include <brotli/types.h>
13 
14 #include "command.h"
15 #include "compress_fragment.h"
16 #include "compress_fragment_two_pass.h"
17 #include "hash.h"
18 #include "memory.h"
19 #include "params.h"
20 #include "ringbuffer.h"
21 
22 typedef enum BrotliEncoderStreamState {
23   /* Default state. */
24   BROTLI_STREAM_PROCESSING = 0,
25   /* Intermediate state; after next block is emitted, byte-padding should be
26      performed before getting back to default state. */
27   BROTLI_STREAM_FLUSH_REQUESTED = 1,
28   /* Last metablock was produced; no more input is acceptable. */
29   BROTLI_STREAM_FINISHED = 2,
30   /* Flushing compressed block and writing meta-data block header. */
31   BROTLI_STREAM_METADATA_HEAD = 3,
32   /* Writing metadata block body. */
33   BROTLI_STREAM_METADATA_BODY = 4
34 } BrotliEncoderStreamState;
35 
36 typedef enum BrotliEncoderFlintState {
37   BROTLI_FLINT_NEEDS_2_BYTES = 2,
38   BROTLI_FLINT_NEEDS_1_BYTE = 1,
39   BROTLI_FLINT_WAITING_FOR_PROCESSING = 0,
40   BROTLI_FLINT_WAITING_FOR_FLUSHING = -1,
41   BROTLI_FLINT_DONE = -2
42 } BrotliEncoderFlintState;
43 
44 typedef struct BrotliEncoderStateStruct {
45   BrotliEncoderParams params;
46 
47   MemoryManager memory_manager_;
48 
49   uint64_t input_pos_;
50   RingBuffer ringbuffer_;
51   size_t cmd_alloc_size_;
52   Command* commands_;
53   size_t num_commands_;
54   size_t num_literals_;
55   size_t last_insert_len_;
56   uint64_t last_flush_pos_;
57   uint64_t last_processed_pos_;
58   int dist_cache_[BROTLI_NUM_DISTANCE_SHORT_CODES];
59   int saved_dist_cache_[4];
60   uint16_t last_bytes_;
61   uint8_t last_bytes_bits_;
62   /* "Flint" is a tiny uncompressed block emitted before the continuation
63      block to unwire literal context from previous data. Despite being int8_t,
64      field is actually BrotliEncoderFlintState enum. */
65   int8_t flint_;
66   uint8_t prev_byte_;
67   uint8_t prev_byte2_;
68   size_t storage_size_;
69   uint8_t* storage_;
70 
71   Hasher hasher_;
72 
73   /* Hash table for FAST_ONE_PASS_COMPRESSION_QUALITY mode. */
74   int small_table_[1 << 10];  /* 4KiB */
75   int* large_table_;          /* Allocated only when needed */
76   size_t large_table_size_;
77 
78   BrotliOnePassArena* one_pass_arena_;
79   BrotliTwoPassArena* two_pass_arena_;
80 
81   /* Command and literal buffers for FAST_TWO_PASS_COMPRESSION_QUALITY. */
82   uint32_t* command_buf_;
83   uint8_t* literal_buf_;
84 
85   uint64_t total_in_;
86   uint8_t* next_out_;
87   size_t available_out_;
88   uint64_t total_out_;
89   /* Temporary buffer for padding flush bits or metadata block header / body. */
90   union {
91     uint64_t u64[2];
92     uint8_t u8[16];
93   } tiny_buf_;
94   uint32_t remaining_metadata_bytes_;
95   BrotliEncoderStreamState stream_state_;
96 
97   BROTLI_BOOL is_last_block_emitted_;
98   BROTLI_BOOL is_initialized_;
99 } BrotliEncoderStateStruct;
100 
101 typedef struct BrotliEncoderStateStruct BrotliEncoderStateInternal;
102 #define BrotliEncoderState BrotliEncoderStateInternal
103 
104 #endif  // BROTLI_ENC_STATE_H_
105