monotone

monotone Mtn Source Tree

Root/cryptopp/zinflate.h

1#ifndef CRYPTOPP_ZINFLATE_H
2#define CRYPTOPP_ZINFLATE_H
3
4#include "filters.h"
5#include <vector>
6
7NAMESPACE_BEGIN(CryptoPP)
8
9//! .
10class LowFirstBitReader
11{
12public:
13LowFirstBitReader(BufferedTransformation &store)
14: m_store(store), m_buffer(0), m_bitsBuffered(0) {}
15unsigned long BitsLeft() const {return m_store.MaxRetrievable() * 8 + m_bitsBuffered;}
16unsigned int BitsBuffered() const {return m_bitsBuffered;}
17unsigned long PeekBuffer() const {return m_buffer;}
18bool FillBuffer(unsigned int length);
19unsigned long PeekBits(unsigned int length);
20void SkipBits(unsigned int length);
21unsigned long GetBits(unsigned int length);
22
23private:
24BufferedTransformation &m_store;
25unsigned long m_buffer;
26unsigned int m_bitsBuffered;
27};
28
29struct CodeLessThan;
30
31//! Huffman Decoder
32class HuffmanDecoder
33{
34public:
35typedef unsigned int code_t;
36typedef unsigned int value_t;
37enum {MAX_CODE_BITS = sizeof(code_t)*8};
38
39class Err : public Exception {public: Err(const std::string &what) : Exception(INVALID_DATA_FORMAT, "HuffmanDecoder: " + what) {}};
40
41HuffmanDecoder() {}
42HuffmanDecoder(const unsigned int *codeBitLengths, unsigned int nCodes){Initialize(codeBitLengths, nCodes);}
43
44void Initialize(const unsigned int *codeBitLengths, unsigned int nCodes);
45unsigned int Decode(code_t code, /* out */ value_t &value) const;
46bool Decode(LowFirstBitReader &reader, value_t &value) const;
47
48private:
49friend struct CodeLessThan;
50
51struct CodeInfo
52{
53CodeInfo(code_t code=0, unsigned int len=0, value_t value=0) : code(code), len(len), value(value) {}
54inline bool operator<(const CodeInfo &rhs) const {return code < rhs.code;}
55code_t code;
56unsigned int len;
57value_t value;
58};
59
60struct LookupEntry
61{
62unsigned int type;
63union
64{
65value_t value;
66const CodeInfo *begin;
67};
68union
69{
70unsigned int len;
71const CodeInfo *end;
72};
73};
74
75static code_t NormalizeCode(code_t code, unsigned int codeBits);
76void FillCacheEntry(LookupEntry &entry, code_t normalizedCode) const;
77
78unsigned int m_maxCodeBits, m_cacheBits, m_cacheMask, m_normalizedCacheMask;
79std::vector<CodeInfo, AllocatorWithCleanup<CodeInfo> > m_codeToValue;
80mutable std::vector<LookupEntry, AllocatorWithCleanup<LookupEntry> > m_cache;
81};
82
83//! DEFLATE (RFC 1951) decompressor
84
85class Inflator : public AutoSignaling<Filter>
86{
87public:
88class Err : public Exception
89{
90public:
91Err(ErrorType e, const std::string &s)
92: Exception(e, s) {}
93};
94class UnexpectedEndErr : public Err {public: UnexpectedEndErr() : Err(INVALID_DATA_FORMAT, "Inflator: unexpected end of compressed block") {}};
95class BadBlockErr : public Err {public: BadBlockErr() : Err(INVALID_DATA_FORMAT, "Inflator: error in compressed block") {}};
96
97/*! \param repeat decompress multiple compressed streams in series
98\param autoSignalPropagation 0 to turn off MessageEnd signal
99*/
100Inflator(BufferedTransformation *attachment = NULL, bool repeat = false, int autoSignalPropagation = -1);
101
102void IsolatedInitialize(const NameValuePairs &parameters);
103unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking);
104bool IsolatedFlush(bool hardFlush, bool blocking);
105
106virtual unsigned int GetLog2WindowSize() const {return 15;}
107
108protected:
109ByteQueue m_inQueue;
110
111private:
112virtual unsigned int MaxPrestreamHeaderSize() const {return 0;}
113virtual void ProcessPrestreamHeader() {}
114virtual void ProcessDecompressedData(const byte *string, unsigned int length)
115{AttachedTransformation()->Put(string, length);}
116virtual unsigned int MaxPoststreamTailSize() const {return 0;}
117virtual void ProcessPoststreamTail() {}
118
119void ProcessInput(bool flush);
120void DecodeHeader();
121bool DecodeBody();
122void FlushOutput();
123void OutputByte(byte b);
124void OutputString(const byte *string, unsigned int length);
125void OutputPast(unsigned int length, unsigned int distance);
126
127enum State {PRE_STREAM, WAIT_HEADER, DECODING_BODY, POST_STREAM, AFTER_END};
128State m_state;
129bool m_repeat, m_eof, m_decodersInitializedWithFixedCodes;
130byte m_blockType;
131word16 m_storedLen;
132enum NextDecode {LITERAL, LENGTH_BITS, DISTANCE, DISTANCE_BITS};
133NextDecode m_nextDecode;
134unsigned int m_literal, m_distance;// for LENGTH_BITS or DISTANCE_BITS
135HuffmanDecoder m_literalDecoder, m_distanceDecoder;
136LowFirstBitReader m_reader;
137SecByteBlock m_window;
138unsigned int m_maxDistance, m_current, m_lastFlush;
139};
140
141NAMESPACE_END
142
143#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status