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
80
81
82
83
84
85
86
87
88
89
90
91
92
// 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_WRITER_H_
#define SRC_PUFF_WRITER_H_
 
#include <cstddef>
#include <cstdint>
 
#include "puffin/src/include/puffin/common.h"
#include "puffin/src/puff_data.h"
 
namespace puffin {
 
// An abstract class for writing data into a puffed buffer. Data can be
// literals, lengths, distances, or metadata. Extensions of this class can
// define how the puffed data should reside in the puffed buffer.
class PuffWriterInterface {
 public:
  virtual ~PuffWriterInterface() = default;
 
  // Inserts data. This function does not need to check for the validity of data
  // . e.g. length > 285, etc.
  //
  // |pd|          IN   The data to put into the puffed buffer. |pd.type|
  //                    defines the type of the data.
  // Returns false if it fails.
  virtual bool Insert(const PuffData& pd) = 0;
 
  // Fluesh any buffer or internal state to the output.
  // Returns false if it fails.
  virtual bool Flush() = 0;
 
  // Returns the number of bytes processed and written into the puff buffer.
  virtual size_t Size() = 0;
};
 
class BufferPuffWriter : public PuffWriterInterface {
 public:
  // Sets the parameters of puff buffer.
  //
  // |puff_buf|  IN  The input puffed stream. It is owned by the caller and must
  //                 be valid during the lifetime of the object.
  // |puff_size| IN  The size of the puffed stream.
  BufferPuffWriter(uint8_t* puff_buf, size_t puff_size)
      : puff_buf_out_(puff_buf),
        puff_size_(puff_size),
        index_(0),
        len_index_(0),
        cur_literals_length_(0),
        state_(State::kWritingNonLiteral) {}
 
  ~BufferPuffWriter() override = default;
 
  bool Insert(const PuffData& pd) override;
  bool Flush() override;
  size_t Size() override;
 
 private:
  // Flushes the literals into the output and resets the state.
  bool FlushLiterals();
 
  // The pointer to the puffed stream. This should not be deallocated.
  uint8_t* puff_buf_out_;
 
  // The size of the puffed buffer.
  size_t puff_size_;
 
  // The offset to the next data in the buffer.
  size_t index_;
 
  // Marks where the length of data should be written after the |index_| has
  // moved forward.
  size_t len_index_;
 
  // The number of literals currently been written (or cached).
  size_t cur_literals_length_;
 
  // States when writing into the puffed buffer.
  enum class State {
    kWritingNonLiteral = 0,
    kWritingSmallLiteral,
    kWritingLargeLiteral,
  } state_;
 
  DISALLOW_COPY_AND_ASSIGN(BufferPuffWriter);
};
 
}  // namespace puffin
 
#endif  // SRC_PUFF_WRITER_H_