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  class UnexpectedEndErr : public Err {public: UnexpectedEndErr() : Err(INVALID_DATA_FORMAT, "Inflator: unexpected end of compressed block") {}};
102  class BadBlockErr : public Err {public: BadBlockErr() : Err(INVALID_DATA_FORMAT, "Inflator: error in compressed block") {}};
103 
104  //! \brief RFC 1951 Decompressor
105  //! \param attachment the filter's attached transformation
106  //! \param repeat decompress multiple compressed streams in series
107  //! \param autoSignalPropagation 0 to turn off MessageEnd signal
108  Inflator(BufferedTransformation *attachment = NULL, bool repeat = false, int autoSignalPropagation = -1);
109 
110  void IsolatedInitialize(const NameValuePairs &parameters);
111  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
112  bool IsolatedFlush(bool hardFlush, bool blocking);
113 
114  virtual unsigned int GetLog2WindowSize() const {return 15;}
115 
116 protected:
117  ByteQueue m_inQueue;
118 
119 private:
120  virtual unsigned int MaxPrestreamHeaderSize() const {return 0;}
121  virtual void ProcessPrestreamHeader() {}
122  virtual void ProcessDecompressedData(const byte *string, size_t length)
123  {AttachedTransformation()->Put(string, length);}
124  virtual unsigned int MaxPoststreamTailSize() const {return 0;}
125  virtual void ProcessPoststreamTail() {}
126 
127  void ProcessInput(bool flush);
128  void DecodeHeader();
129  bool DecodeBody();
130  void FlushOutput();
131  void OutputByte(byte b);
132  void OutputString(const byte *string, size_t length);
133  void OutputPast(unsigned int length, unsigned int distance);
134 
135  static const HuffmanDecoder *FixedLiteralDecoder();
136  static const HuffmanDecoder *FixedDistanceDecoder();
137 
138  const HuffmanDecoder& GetLiteralDecoder() const;
139  const HuffmanDecoder& GetDistanceDecoder() const;
140 
141  enum State {PRE_STREAM, WAIT_HEADER, DECODING_BODY, POST_STREAM, AFTER_END};
142  State m_state;
143  bool m_repeat, m_eof, m_wrappedAround;
144  byte m_blockType;
145  word16 m_storedLen;
146  enum NextDecode {LITERAL, LENGTH_BITS, DISTANCE, DISTANCE_BITS};
147  NextDecode m_nextDecode;
148  unsigned int m_literal, m_distance; // for LENGTH_BITS or DISTANCE_BITS
149  HuffmanDecoder m_dynamicLiteralDecoder, m_dynamicDistanceDecoder;
150  LowFirstBitReader m_reader;
151  SecByteBlock m_window;
152  size_t m_current, m_lastFlush;
153 };
154 
155 NAMESPACE_END
156 
157 #endif
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:140
const char * what() const
Retrieves a C-string describing the exception.
Definition: cryptlib.h:168
ErrorType
Error types or categories.
Definition: cryptlib.h:145
Exception(ErrorType errorType, const std::string &s)
Construct a new Exception.
Definition: cryptlib.h:165
Abstract base classes that provide a uniform interface to this library.
SecBlock typedef.
Definition: secblock.h:731
Interface for buffered transformations.
Definition: cryptlib.h:1352
Classes and functions for secure memory allocations.
Input data was received that did not conform to expected format.
Definition: cryptlib.h:155
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:1376
Huffman Decoder.
Definition: zinflate.h:36
Data structure used to store byte strings.
Definition: queue.h:20
Implementation of BufferedTransformation's attachment interface.
DEFLATE decompressor (RFC 1951)
Definition: zinflate.h:92
Provides auto signaling support.
Definition: simple.h:280
Crypto++ library namespace.
Interface for retrieving values given their names.
Definition: cryptlib.h:279