state.h 3.0 KB

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