Crypto++  5.6.5
Free C++ class library of cryptographic schemes
zinflate.h
1 #ifndef CRYPTOPP_ZINFLATE_H
2 #define CRYPTOPP_ZINFLATE_H
3 
4 #include "cryptlib.h"
5 #include "secblock.h"
6 #include "filters.h"
7 #include "stdcpp.h"
8 
9 NAMESPACE_BEGIN(CryptoPP)
10 
11 //! \class LowFirstBitReader
12 //! \since Crypto++ 1.0
14 {
15 public:
16  LowFirstBitReader(BufferedTransformation &store)
17  : m_store(store), m_buffer(0), m_bitsBuffered(0) {}
18  unsigned int BitsBuffered() const {return m_bitsBuffered;}
19  unsigned long PeekBuffer() const {return m_buffer;}
20  bool FillBuffer(unsigned int length);
21  unsigned long PeekBits(unsigned int length);
22  void SkipBits(unsigned int length);
23  unsigned long GetBits(unsigned int length);
24 
25 private:
26  BufferedTransformation &m_store;
27  unsigned long m_buffer;
28  unsigned int m_bitsBuffered;
29 };
30 
31 struct CodeLessThan;
32 
33 //! \class HuffmanDecoder
34 //! \brief Huffman Decoder
35 //! \since Crypto++ 1.0
37 {
38 public:
39  typedef unsigned int code_t;
40  typedef unsigned int value_t;
41  enum {MAX_CODE_BITS = sizeof(code_t)*8};
42 
43  class Err : public Exception {public: Err(const std::string &what) : Exception(INVALID_DATA_FORMAT, "HuffmanDecoder: " + what) {}};
44 
45  HuffmanDecoder() : m_maxCodeBits(0), m_cacheBits(0), m_cacheMask(0), m_normalizedCacheMask(0) {}
46  HuffmanDecoder(const unsigned int *codeBitLengths, unsigned int nCodes)
47  : m_maxCodeBits(0), m_cacheBits(0), m_cacheMask(0), m_normalizedCacheMask(0)
48  {Initialize(codeBitLengths, nCodes);}
49 
50  void Initialize(const unsigned int *codeBitLengths, unsigned int nCodes);
51  unsigned int Decode(code_t code, /* out */ value_t &value) const;
52  bool Decode(LowFirstBitReader &reader, value_t &value) const;
53 
54 private:
55  friend struct CodeLessThan;
56 
57  struct CodeInfo
58  {
59  CodeInfo(code_t code=0, unsigned int len=0, value_t value=0) : code(code), len(len), value(value) {}
60  inline bool operator<(const CodeInfo &rhs) const {return code < rhs.code;}
61  code_t code;
62  unsigned int len;
63  value_t value;
64  };
65 
66  struct LookupEntry
67  {
68  unsigned int type;
69  union
70  {
71  value_t value;
72  const CodeInfo *begin;
73  };
74  union
75  {
76  unsigned int len;
77  const CodeInfo *end;
78  };
79  };
80 
81  static code_t NormalizeCode(code_t code, unsigned int codeBits);
82  void FillCacheEntry(LookupEntry &entry, code_t normalizedCode) const;
83 
84  unsigned int m_maxCodeBits, m_cacheBits, m_cacheMask, m_normalizedCacheMask;
85  std::vector<CodeInfo, AllocatorWithCleanup<CodeInfo> > m_codeToValue;
86  mutable std::vector<LookupEntry, AllocatorWithCleanup<LookupEntry> > m_cache;
87 };
88 
89 //! \class Inflator
90 //! \brief DEFLATE decompressor (RFC 1951)
91 //! \since Crypto++ 1.0
92 class Inflator : public AutoSignaling<Filter>
93 {
94 public:
95  class Err : public Exception
96  {
97  public:
98  Err(ErrorType e, const std::string &s)
99  : Exception(e, s) {}
100  };
101  //! \brief Exception thrown when a truncated stream is encountered
102  class UnexpectedEndErr : public Err {public: UnexpectedEndErr() : Err(INVALID_DATA_FORMAT, "Inflator: unexpected end of compressed block") {}};
103  //! \brief Exception thrown when a bad block is encountered
104  class BadBlockErr : public Err {public: BadBlockErr() : Err(INVALID_DATA_FORMAT, "Inflator: error in compressed block") {}};
105  //! \brief Exception thrown when an invalid distance is encountered
106  class BadDistanceErr : public Err {public: BadDistanceErr() : Err(INVALID_DATA_FORMAT, "Inflator: error in bit distance") {}};
107 
108  //! \brief RFC 1951 Decompressor
109  //! \param attachment the filter's attached transformation
110  //! \param repeat decompress multiple compressed streams in series
111  //! \param autoSignalPropagation 0 to turn off MessageEnd signal
112  Inflator(BufferedTransformation *attachment = NULLPTR, bool repeat = false, int autoSignalPropagation = -1);
113 
114  void IsolatedInitialize(const NameValuePairs &parameters);
115  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
116  bool IsolatedFlush(bool hardFlush, bool blocking);
117 
118  virtual unsigned int GetLog2WindowSize() const {return 15;}
119 
120 protected:
121  ByteQueue m_inQueue;
122 
123 private:
124  virtual unsigned int MaxPrestreamHeaderSize() const {return 0;}
125  virtual void ProcessPrestreamHeader() {}
126  virtual void ProcessDecompressedData(const byte *string, size_t length)
127  {AttachedTransformation()->Put(string, length);}
128  virtual unsigned int MaxPoststreamTailSize() const {return 0;}
129  virtual void ProcessPoststreamTail() {}
130 
131  void ProcessInput(bool flush);
132  void DecodeHeader();
133  bool DecodeBody();
134  void FlushOutput();
135  void OutputByte(byte b);
136  void OutputString(const byte *string, size_t length);
137  void OutputPast(unsigned int length, unsigned int distance);
138 
139  static const HuffmanDecoder *FixedLiteralDecoder();
140  static const HuffmanDecoder *FixedDistanceDecoder();
141 
142  const HuffmanDecoder& GetLiteralDecoder() const;
143  const HuffmanDecoder& GetDistanceDecoder() const;
144 
145  enum State {PRE_STREAM, WAIT_HEADER, DECODING_BODY, POST_STREAM, AFTER_END};
146  State m_state;
147  bool m_repeat, m_eof, m_wrappedAround;
148  byte m_blockType;
149  word16 m_storedLen;
150  enum NextDecode {LITERAL, LENGTH_BITS, DISTANCE, DISTANCE_BITS};
151  NextDecode m_nextDecode;
152  unsigned int m_literal, m_distance; // for LENGTH_BITS or DISTANCE_BITS
153  HuffmanDecoder m_dynamicLiteralDecoder, m_dynamicDistanceDecoder;
154  LowFirstBitReader m_reader;
155  SecByteBlock m_window;
156  size_t m_current, m_lastFlush;
157 };
158 
159 NAMESPACE_END
160 
161 #endif
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:150
ErrorType
Error types or categories.
Definition: cryptlib.h:155
const char * what() const
Retrieves a C-string describing the exception.
Definition: cryptlib.h:178
Exception thrown when a truncated stream is encountered.
Definition: zinflate.h:102
Exception(ErrorType errorType, const std::string &s)
Construct a new Exception.
Definition: cryptlib.h:175
Abstract base classes that provide a uniform interface to this library.
SecBlock<byte> typedef.
Definition: secblock.h:810
Interface for buffered transformations.
Definition: cryptlib.h:1343
Classes and functions for secure memory allocations.
Input data was received that did not conform to expected format.
Definition: cryptlib.h:165
bool operator<(const OID &lhs, const OID &rhs)
Compare two OIDs for ordering.
size_t Put(byte inByte, bool blocking=true)
Input a byte for processing.
Definition: cryptlib.h:1364
Exception thrown when an invalid distance is encountered.
Definition: zinflate.h:106
Huffman Decoder.
Definition: zinflate.h:36
Data structure used to store byte strings.
Definition: queue.h:19
Implementation of BufferedTransformation&#39;s attachment interface.
DEFLATE decompressor (RFC 1951)
Definition: zinflate.h:92
Provides auto signaling support.
Definition: simple.h:288
Crypto++ library namespace.
Exception thrown when a bad block is encountered.
Definition: zinflate.h:104
Interface for retrieving values given their names.
Definition: cryptlib.h:285