Home | History | Annotate | Download | only in src
      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_DATA_H_
      6 #define SRC_PUFF_DATA_H_
      7 
      8 #include <cstddef>
      9 #include <cstdint>
     10 #include <functional>
     11 
     12 namespace puffin {
     13 
     14 // Data structure that is exchanged between the |PuffWriterInterface|,
     15 // |PuffReaderInterface|, |Puffer|, and |Huffer|.
     16 struct PuffData {
     17   enum class Type {
     18     // Used for reading/writing only one literal.
     19     kLiteral,
     20 
     21     // Used for reading/writing literals.
     22     kLiterals,
     23 
     24     // Used for reading/writing length/distance pairs.
     25     kLenDist,
     26 
     27     // Used for reading/writing a buffer as the Huffman table. The
     28     // implementations should copy the data (located in |metadata|) into puff
     29     // buffer.
     30     kBlockMetadata,
     31 
     32     // Used for reading/writing an end of block symbol. End of block can
     33     // contain the unused bits of data at the end of a deflate stream.
     34     kEndOfBlock,
     35   } type;
     36 
     37   // A function that once set, can read raw bytes from whatever its parameters
     38   // are set. This function reads |count| bytes from |buffer| and advances its
     39   // read offset forward. The next call to this function will start reading
     40   // after the last read byte. It returns false if it cannot read or the |count|
     41   // is larger than what is availabe in the buffer.
     42   // Used by:
     43   // PuffData::Type::kLiterals
     44   std::function<bool(uint8_t* buffer, size_t count)> read_fn;
     45 
     46   // Used by:
     47   // PuffData::Type::kBlockMetadata
     48   // PuffData::Type::kEndOfBlock
     49   // PuffData::Type::kLiterals
     50   // PuffData::Type::kLenDist
     51   size_t length;
     52 
     53   // Used by:
     54   // PuffData::Type::kEndOfBlock
     55   // PuffData::Type::kLenDist
     56   size_t distance;
     57 
     58   // Used by:
     59   // PuffData::Type::kEndOfBlock
     60   // PuffData::Type::kLiteral
     61   uint8_t byte;
     62 
     63   // 1: Header size.
     64   // 3: Lengths of next three arrays.
     65   // 286: Maximum number of literals/lengths codes lengths.
     66   // 30: Maximum number of distance codes lengths.
     67   // 19: Maximum number of header code lengths.
     68   // Used by:
     69   // PuffData::Type::kBlockMetadata
     70   uint8_t block_metadata[1 + 3 + 286 + 30 + 19];
     71 };
     72 
     73 // The headers for differentiating literals from length/distance pairs.
     74 constexpr uint8_t kLiteralsHeader = 0x00;
     75 constexpr uint8_t kLenDistHeader = 0x80;
     76 
     77 }  // namespace puffin
     78 
     79 #endif  // SRC_PUFF_DATA_H_
     80