puff_reader.h 2.1 KB

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