xref: /aosp_15_r20/external/puffin/src/puff_reader.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_READER_H_
6*07fb1d06SElliott Hughes #define SRC_PUFF_READER_H_
7*07fb1d06SElliott Hughes 
8*07fb1d06SElliott Hughes #include <cstddef>
9*07fb1d06SElliott Hughes #include <cstdint>
10*07fb1d06SElliott Hughes 
11*07fb1d06SElliott Hughes #include "puffin/src/include/puffin/common.h"
12*07fb1d06SElliott Hughes #include "puffin/src/puff_data.h"
13*07fb1d06SElliott Hughes 
14*07fb1d06SElliott Hughes namespace puffin {
15*07fb1d06SElliott Hughes 
16*07fb1d06SElliott Hughes // An abstract class for reading data from a puffed buffer. Data can be
17*07fb1d06SElliott Hughes // literals, lengths, distances, or metadata. Extensions of this class can
18*07fb1d06SElliott Hughes // define how the puffed data should reside in the puffed buffer.
19*07fb1d06SElliott Hughes class PuffReaderInterface {
20*07fb1d06SElliott Hughes  public:
21*07fb1d06SElliott Hughes   virtual ~PuffReaderInterface() = default;
22*07fb1d06SElliott Hughes 
23*07fb1d06SElliott Hughes   // Retrieves the next puff data available in the puffed buffer. Similar to
24*07fb1d06SElliott Hughes   // |PuffWriterInterface.Insert()| This function does not check for validity of
25*07fb1d06SElliott Hughes   // data.
26*07fb1d06SElliott Hughes   //
27*07fb1d06SElliott Hughes   // |data|  OUT  The next data available in the puffed buffer.
28*07fb1d06SElliott Hughes   virtual bool GetNext(PuffData* data) = 0;
29*07fb1d06SElliott Hughes 
30*07fb1d06SElliott Hughes   // Returns the number of bytes left in the puff buffer.
31*07fb1d06SElliott Hughes   virtual size_t BytesLeft() const = 0;
32*07fb1d06SElliott Hughes };
33*07fb1d06SElliott Hughes 
34*07fb1d06SElliott Hughes class BufferPuffReader : public PuffReaderInterface {
35*07fb1d06SElliott Hughes  public:
36*07fb1d06SElliott Hughes   // Sets the parameters of puff buffer.
37*07fb1d06SElliott Hughes   //
38*07fb1d06SElliott Hughes   // |puff_buf|  IN  The input puffed stream. It is owned by the caller and must
39*07fb1d06SElliott Hughes   //                 be valid during the lifetime of the object.
40*07fb1d06SElliott Hughes   // |puff_size| IN  The size of the puffed stream.
BufferPuffReader(const uint8_t * puff_buf,size_t puff_size)41*07fb1d06SElliott Hughes   BufferPuffReader(const uint8_t* puff_buf, size_t puff_size)
42*07fb1d06SElliott Hughes       : puff_buf_in_(puff_buf),
43*07fb1d06SElliott Hughes         puff_size_(puff_size),
44*07fb1d06SElliott Hughes         index_(0),
45*07fb1d06SElliott Hughes         state_(State::kReadingBlockMetadata) {}
46*07fb1d06SElliott Hughes 
47*07fb1d06SElliott Hughes   ~BufferPuffReader() override = default;
48*07fb1d06SElliott Hughes 
49*07fb1d06SElliott Hughes   bool GetNext(PuffData* pd) override;
50*07fb1d06SElliott Hughes   size_t BytesLeft() const override;
51*07fb1d06SElliott Hughes 
52*07fb1d06SElliott Hughes  private:
53*07fb1d06SElliott Hughes   // The pointer to the puffed stream. This should not be deallocated.
54*07fb1d06SElliott Hughes   const uint8_t* puff_buf_in_;
55*07fb1d06SElliott Hughes 
56*07fb1d06SElliott Hughes   // The size of the puffed buffer.
57*07fb1d06SElliott Hughes   size_t puff_size_;
58*07fb1d06SElliott Hughes 
59*07fb1d06SElliott Hughes   // Index to the offset of the next data in the puff buffer.
60*07fb1d06SElliott Hughes   size_t index_;
61*07fb1d06SElliott Hughes 
62*07fb1d06SElliott Hughes   // State when reading from the puffed buffer.
63*07fb1d06SElliott Hughes   enum class State {
64*07fb1d06SElliott Hughes     kReadingLenDist = 0,
65*07fb1d06SElliott Hughes     kReadingBlockMetadata,
66*07fb1d06SElliott Hughes   } state_;
67*07fb1d06SElliott Hughes 
68*07fb1d06SElliott Hughes   DISALLOW_COPY_AND_ASSIGN(BufferPuffReader);
69*07fb1d06SElliott Hughes };
70*07fb1d06SElliott Hughes 
71*07fb1d06SElliott Hughes }  // namespace puffin
72*07fb1d06SElliott Hughes 
73*07fb1d06SElliott Hughes #endif  // SRC_PUFF_READER_H_
74