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_INCLUDE_PUFFIN_PUFFER_H_
6 #define SRC_INCLUDE_PUFFIN_PUFFER_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "puffin/common.h"
12 #include "puffin/stream.h"
13 
14 namespace puffin {
15 
16 class BitReaderInterface;
17 class PuffWriterInterface;
18 class HuffmanTable;
19 
20 class Puffer {
21  public:
22   // In older versions of puffin, there is a bug in the client which incorrectly
23   // identifies the number of bits to cache when number of bits for the current
24   // distance plus the number of bits for end of block Huffman code is smaller
25   // than the maximum number of bits needed for distance. If this situations
26   // happens at the very end of the block, it incorrectly tries to cache more
27   // bits than we have and crashes as a result. If |exclude_bad_distance_caches|
28   // is true, we identify those problematic deflate buffers and exclude them
29   // from the list of available deflates. The default is false.
30   explicit Puffer(bool exclude_bad_distance_caches);
31   Puffer();
32   ~Puffer();
33 
34   // Creates a puffed buffer from a deflate buffer.
35   //
36   // If |deflates| is not null, it will be populated with the location of the
37   // subblocks in the input data. In addition, the uncompressed deflate blocks
38   // will be ignored and will not be added to the |deflates|. For this case to
39   // happen correctly, the |pw| should write into an empty/null buffer,
40   // otherwise the created puff stream, will not match the deflate stream. In
41   // addition, in this case, the function will return when it reaches a final
42   // deflate subblock.
43   bool PuffDeflate(BitReaderInterface* br,
44                    PuffWriterInterface* pw,
45                    std::vector<BitExtent>* deflates) const;
46 
47  private:
48   std::unique_ptr<HuffmanTable> dyn_ht_;
49   std::unique_ptr<HuffmanTable> fix_ht_;
50 
51   bool exclude_bad_distance_caches_;
52 
53   DISALLOW_COPY_AND_ASSIGN(Puffer);
54 };
55 
56 }  // namespace puffin
57 
58 #endif  // SRC_INCLUDE_PUFFIN_PUFFER_H_
59