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