huangcm
2025-09-01 53d8e046ac1bf2ebe94f671983e3d3be059df91a
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
// 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_READER_H_
#define SRC_PUFF_READER_H_
 
#include <cstddef>
#include <cstdint>
 
#include "puffin/src/include/puffin/common.h"
#include "puffin/src/puff_data.h"
 
namespace puffin {
 
// An abstract class for reading data from 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 PuffReaderInterface {
 public:
  virtual ~PuffReaderInterface() = default;
 
  // Retrieves the next puff data available in the puffed buffer. Similar to
  // |PuffWriterInterface.Insert()| This function does not check for validity of
  // data.
  //
  // |data|  OUT  The next data available in the puffed buffer.
  virtual bool GetNext(PuffData* data) = 0;
 
  // Returns the number of bytes left in the puff buffer.
  virtual size_t BytesLeft() const = 0;
};
 
class BufferPuffReader : public PuffReaderInterface {
 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.
  BufferPuffReader(const uint8_t* puff_buf, size_t puff_size)
      : puff_buf_in_(puff_buf),
        puff_size_(puff_size),
        index_(0),
        state_(State::kReadingBlockMetadata) {}
 
  ~BufferPuffReader() override = default;
 
  bool GetNext(PuffData* pd) override;
  size_t BytesLeft() const override;
 
 private:
  // The pointer to the puffed stream. This should not be deallocated.
  const uint8_t* puff_buf_in_;
 
  // The size of the puffed buffer.
  size_t puff_size_;
 
  // Index to the offset of the next data in the puff buffer.
  size_t index_;
 
  // State when reading from the puffed buffer.
  enum class State {
    kReadingLenDist = 0,
    kReadingBlockMetadata,
  } state_;
 
  DISALLOW_COPY_AND_ASSIGN(BufferPuffReader);
};
 
}  // namespace puffin
 
#endif  // SRC_PUFF_READER_H_