• 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_WRITER_H_
6 #define SRC_PUFF_WRITER_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 writing data into 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 PuffWriterInterface {
20  public:
21   virtual ~PuffWriterInterface() = default;
22 
23   // Inserts data. This function does not need to check for the validity of data
24   // . e.g. length > 285, etc.
25   //
26   // |pd|          IN   The data to put into the puffed buffer. |pd.type|
27   //                    defines the type of the data.
28   // Returns false if it fails.
29   virtual bool Insert(const PuffData& pd) = 0;
30 
31   // Fluesh any buffer or internal state to the output.
32   // Returns false if it fails.
33   virtual bool Flush() = 0;
34 
35   // Returns the number of bytes processed and written into the puff buffer.
36   virtual size_t Size() = 0;
37 };
38 
39 class BufferPuffWriter : public PuffWriterInterface {
40  public:
41   // Sets the parameters of puff buffer.
42   //
43   // |puff_buf|  IN  The input puffed stream. It is owned by the caller and must
44   //                 be valid during the lifetime of the object.
45   // |puff_size| IN  The size of the puffed stream.
BufferPuffWriter(uint8_t * puff_buf,size_t puff_size)46   BufferPuffWriter(uint8_t* puff_buf, size_t puff_size)
47       : puff_buf_out_(puff_buf),
48         puff_size_(puff_size),
49         index_(0),
50         len_index_(0),
51         cur_literals_length_(0),
52         state_(State::kWritingNonLiteral) {}
53 
54   ~BufferPuffWriter() override = default;
55 
56   bool Insert(const PuffData& pd) override;
57   bool Flush() override;
58   size_t Size() override;
59 
60  private:
61   // Flushes the literals into the output and resets the state.
62   bool FlushLiterals();
63 
64   // The pointer to the puffed stream. This should not be deallocated.
65   uint8_t* puff_buf_out_;
66 
67   // The size of the puffed buffer.
68   size_t puff_size_;
69 
70   // The offset to the next data in the buffer.
71   size_t index_;
72 
73   // Marks where the length of data should be written after the |index_| has
74   // moved forward.
75   size_t len_index_;
76 
77   // The number of literals currently been written (or cached).
78   size_t cur_literals_length_;
79 
80   // States when writing into the puffed buffer.
81   enum class State {
82     kWritingNonLiteral = 0,
83     kWritingSmallLiteral,
84     kWritingLargeLiteral,
85   } state_;
86 
87   DISALLOW_COPY_AND_ASSIGN(BufferPuffWriter);
88 };
89 
90 }  // namespace puffin
91 
92 #endif  // SRC_PUFF_WRITER_H_
93