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