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