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