Crypto++ 8.2
Free C&
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
9NAMESPACE_BEGIN(CryptoPP)
10
11/// \since Crypto++ 1.0
13{
14public:
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
24private:
26 unsigned long m_buffer;
27 unsigned int m_bitsBuffered;
28};
29
30struct CodeLessThan;
31
32/// \brief Huffman Decoder
33/// \since Crypto++ 1.0
35{
36public:
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
52private:
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
89class Inflator : public AutoSignaling<Filter>
90{
91public:
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
117protected:
118 ByteQueue m_inQueue;
119
120private:
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
157NAMESPACE_END
158
159#endif
bool operator<(const OID &lhs, const OID &rhs)
Compare two OIDs for ordering.
Provides auto signaling support.
Definition: simple.h:290
Interface for buffered transformations.
Definition: cryptlib.h:1599
size_t Put(byte inByte, bool blocking=true)
Input a byte for processing.
Definition: cryptlib.h:1620
Data structure used to store byte strings.
Definition: queue.h:19
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:159
ErrorType
Error types or categories.
Definition: cryptlib.h:163
@ INVALID_DATA_FORMAT
Input data was received that did not conform to expected format.
Definition: cryptlib.h:173
const char * what() const
Retrieves a C-string describing the exception.
Definition: cryptlib.h:186
BufferedTransformation * AttachedTransformation()
Retrieve attached transformation.
Definition: filters.cpp:36
Huffman Decoder.
Definition: zinflate.h:35
Exception thrown when a bad block is encountered.
Definition: zinflate.h:101
Exception thrown when an invalid distance is encountered.
Definition: zinflate.h:103
Exception thrown when a truncated stream is encountered.
Definition: zinflate.h:99
DEFLATE decompressor (RFC 1951)
Definition: zinflate.h:90
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: zinflate.cpp:233
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: zinflate.cpp:301
bool IsolatedFlush(bool hardFlush, bool blocking)
Flushes data buffered by this object, without signal propagation.
Definition: zinflate.cpp:317
Interface for retrieving values given their names.
Definition: cryptlib.h:294
SecBlock<byte> typedef.
Definition: secblock.h:1058
Pointer that overloads operator ->
Definition: smartptr.h:37
Abstract base classes that provide a uniform interface to this library.
Implementation of BufferedTransformation's attachment interface.
Crypto++ library namespace.
Classes and functions for secure memory allocations.
Common C++ header files.