xref: /aosp_15_r20/external/puffin/src/puff_data.h (revision 07fb1d065b7cfb4729786fadd42a612532d2f466)
1*07fb1d06SElliott Hughes // Copyright 2017 The ChromiumOS Authors
2*07fb1d06SElliott Hughes // Use of this source code is governed by a BSD-style license that can be
3*07fb1d06SElliott Hughes // found in the LICENSE file.
4*07fb1d06SElliott Hughes 
5*07fb1d06SElliott Hughes #ifndef SRC_PUFF_DATA_H_
6*07fb1d06SElliott Hughes #define SRC_PUFF_DATA_H_
7*07fb1d06SElliott Hughes 
8*07fb1d06SElliott Hughes #include <cstddef>
9*07fb1d06SElliott Hughes #include <cstdint>
10*07fb1d06SElliott Hughes #include <functional>
11*07fb1d06SElliott Hughes 
12*07fb1d06SElliott Hughes namespace puffin {
13*07fb1d06SElliott Hughes 
14*07fb1d06SElliott Hughes // Data structure that is exchanged between the |PuffWriterInterface|,
15*07fb1d06SElliott Hughes // |PuffReaderInterface|, |Puffer|, and |Huffer|.
16*07fb1d06SElliott Hughes struct PuffData {
17*07fb1d06SElliott Hughes   enum class Type {
18*07fb1d06SElliott Hughes     // Used for reading/writing only one literal.
19*07fb1d06SElliott Hughes     kLiteral,
20*07fb1d06SElliott Hughes 
21*07fb1d06SElliott Hughes     // Used for reading/writing literals.
22*07fb1d06SElliott Hughes     kLiterals,
23*07fb1d06SElliott Hughes 
24*07fb1d06SElliott Hughes     // Used for reading/writing length/distance pairs.
25*07fb1d06SElliott Hughes     kLenDist,
26*07fb1d06SElliott Hughes 
27*07fb1d06SElliott Hughes     // Used for reading/writing a buffer as the Huffman table. The
28*07fb1d06SElliott Hughes     // implementations should copy the data (located in |metadata|) into puff
29*07fb1d06SElliott Hughes     // buffer.
30*07fb1d06SElliott Hughes     kBlockMetadata,
31*07fb1d06SElliott Hughes 
32*07fb1d06SElliott Hughes     // Used for reading/writing an end of block symbol. End of block can
33*07fb1d06SElliott Hughes     // contain the unused bits of data at the end of a deflate stream.
34*07fb1d06SElliott Hughes     kEndOfBlock,
35*07fb1d06SElliott Hughes   } type;
36*07fb1d06SElliott Hughes 
37*07fb1d06SElliott Hughes   // A function that once set, can read raw bytes from whatever its parameters
38*07fb1d06SElliott Hughes   // are set. This function reads |count| bytes from |buffer| and advances its
39*07fb1d06SElliott Hughes   // read offset forward. The next call to this function will start reading
40*07fb1d06SElliott Hughes   // after the last read byte. It returns false if it cannot read or the |count|
41*07fb1d06SElliott Hughes   // is larger than what is availabe in the buffer.
42*07fb1d06SElliott Hughes   // Used by:
43*07fb1d06SElliott Hughes   // PuffData::Type::kLiterals
44*07fb1d06SElliott Hughes   std::function<bool(uint8_t* buffer, size_t count)> read_fn;
45*07fb1d06SElliott Hughes 
46*07fb1d06SElliott Hughes   // Used by:
47*07fb1d06SElliott Hughes   // PuffData::Type::kBlockMetadata
48*07fb1d06SElliott Hughes   // PuffData::Type::kEndOfBlock
49*07fb1d06SElliott Hughes   // PuffData::Type::kLiterals
50*07fb1d06SElliott Hughes   // PuffData::Type::kLenDist
51*07fb1d06SElliott Hughes   size_t length;
52*07fb1d06SElliott Hughes 
53*07fb1d06SElliott Hughes   // Used by:
54*07fb1d06SElliott Hughes   // PuffData::Type::kEndOfBlock
55*07fb1d06SElliott Hughes   // PuffData::Type::kLenDist
56*07fb1d06SElliott Hughes   size_t distance;
57*07fb1d06SElliott Hughes 
58*07fb1d06SElliott Hughes   // Used by:
59*07fb1d06SElliott Hughes   // PuffData::Type::kEndOfBlock
60*07fb1d06SElliott Hughes   // PuffData::Type::kLiteral
61*07fb1d06SElliott Hughes   uint8_t byte;
62*07fb1d06SElliott Hughes 
63*07fb1d06SElliott Hughes   // 1: Header size.
64*07fb1d06SElliott Hughes   // 3: Lengths of next three arrays.
65*07fb1d06SElliott Hughes   // 286: Maximum number of literals/lengths codes lengths.
66*07fb1d06SElliott Hughes   // 30: Maximum number of distance codes lengths.
67*07fb1d06SElliott Hughes   // 19: Maximum number of header code lengths.
68*07fb1d06SElliott Hughes   // Used by:
69*07fb1d06SElliott Hughes   // PuffData::Type::kBlockMetadata
70*07fb1d06SElliott Hughes   uint8_t block_metadata[1 + 3 + 286 + 30 + 19];
71*07fb1d06SElliott Hughes };
72*07fb1d06SElliott Hughes 
73*07fb1d06SElliott Hughes // The headers for differentiating literals from length/distance pairs.
74*07fb1d06SElliott Hughes constexpr uint8_t kLiteralsHeader = 0x00;
75*07fb1d06SElliott Hughes constexpr uint8_t kLenDistHeader = 0x80;
76*07fb1d06SElliott Hughes 
77*07fb1d06SElliott Hughes }  // namespace puffin
78*07fb1d06SElliott Hughes 
79*07fb1d06SElliott Hughes #endif  // SRC_PUFF_DATA_H_
80