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