18 inline bool operator()(CryptoPP::HuffmanDecoder::code_t lhs,
const CryptoPP::HuffmanDecoder::CodeInfo &rhs)
19 {
return lhs < rhs.code;}
21 inline bool operator()(
const CryptoPP::HuffmanDecoder::CodeInfo &lhs,
const CryptoPP::HuffmanDecoder::CodeInfo &rhs)
22 {
return lhs.code < rhs.code;}
43 return m_buffer & (((
unsigned long)1 << length) - 1);
55 unsigned long result =
PeekBits(length);
62 return code << (MAX_CODE_BITS - codeBits);
83 throw Err(
"null code");
85 m_maxCodeBits = *std::max_element(codeBits, codeBits+nCodes);
87 if (m_maxCodeBits > MAX_CODE_BITS)
88 throw Err(
"code length exceeds maximum");
90 if (m_maxCodeBits == 0)
91 throw Err(
"null code");
95 std::fill(blCount.
begin(), blCount.
end(), 0);
97 for (i=0; i<nCodes; i++)
98 blCount[codeBits[i]]++;
104 for (i=2; i<=m_maxCodeBits; i++)
107 if (code > code + blCount[i-1])
108 throw Err(
"codes oversubscribed");
109 code += blCount[i-1];
110 if (code > (code << 1))
111 throw Err(
"codes oversubscribed");
117 const word64 shiftedMaxCode = ((
word64)1 << m_maxCodeBits);
118 if (code > shiftedMaxCode - blCount[m_maxCodeBits])
119 throw Err(
"codes oversubscribed");
120 else if (m_maxCodeBits != 1 && code < shiftedMaxCode - blCount[m_maxCodeBits])
121 throw Err(
"codes incomplete");
124 m_codeToValue.resize(nCodes - blCount[0]);
126 for (i=0; i<nCodes; i++)
128 unsigned int len = codeBits[i];
131 code = NormalizeCode(nextCode[len]++, len);
132 m_codeToValue[j].code =
code;
133 m_codeToValue[j].len = len;
134 m_codeToValue[j].value = i;
138 std::sort(m_codeToValue.begin(), m_codeToValue.end());
141 m_cacheBits =
STDMIN(9U, m_maxCodeBits);
142 m_cacheMask = (1 << m_cacheBits) - 1;
143 m_normalizedCacheMask = NormalizeCode(m_cacheMask, m_cacheBits);
148 if (m_cache.size() != shiftedCache)
149 m_cache.resize((
size_t)shiftedCache);
151 for (i=0; i<m_cache.size(); i++)
157 normalizedCode &= m_normalizedCacheMask;
158 const CodeInfo &codeInfo = *(std::upper_bound(m_codeToValue.begin(), m_codeToValue.end(), normalizedCode,
CodeLessThan())-1);
159 if (codeInfo.
len <= m_cacheBits)
167 entry.
begin = &codeInfo;
168 const CodeInfo *last = & *(std::upper_bound(m_codeToValue.begin(), m_codeToValue.end(), normalizedCode + ~m_normalizedCacheMask,
CodeLessThan())-1);
169 if (codeInfo.
len == last->
len)
187 code_t normalizedCode = 0;
192 FillCacheEntry(entry, normalizedCode);
201 const CodeInfo &codeInfo = (entry.type == 2)
202 ? entry.begin[(normalizedCode << m_cacheBits) >> (MAX_CODE_BITS - (entry.len - m_cacheBits))]
203 : *(std::upper_bound(entry.begin, entry.end, normalizedCode,
CodeLessThan())-1);
204 value = codeInfo.
value;
211 bool result = reader.
FillBuffer(m_maxCodeBits);
214 unsigned int codeBits = Decode(reader.
PeekBuffer(), value);
225 , m_state(PRE_STREAM), m_repeat(repeat), m_eof(0), m_wrappedAround(0)
226 , m_blockType(0xff), m_storedLen(0xffff), m_nextDecode(), m_literal(0)
227 , m_distance(0), m_reader(m_inQueue), m_current(0), m_lastFlush(0)
312 Output(0, NULL, 0, messageEnd, blocking);
347 const size_t MAX_HEADER_SIZE =
BitsToBytes(3+5+5+4+19*7+286*15+19*15);
405 static const unsigned int border[] = {
406 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
407 std::fill(codeLengths.
begin(), codeLengths+19, 0);
408 for (i=0; i<hclen+4; i++)
414 for (i = 0; i < hlit+257+hdist+1; )
416 unsigned int k = 0,
count = 0, repeater = 0;
433 repeater = codeLengths[i-1];
448 if (i +
count > hlit+257+hdist+1)
450 std::fill(codeLengths + i, codeLengths + i +
count, repeater);
454 if (hdist == 0 && codeLengths[hlit+257] == 0)
477 bool blockEnd =
false;
498 static const unsigned int lengthStarts[] = {
499 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
500 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
501 static const unsigned int lengthExtraBits[] = {
502 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
503 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
504 static const unsigned int distanceStarts[] = {
505 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
506 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
507 8193, 12289, 16385, 24577};
508 static const unsigned int distanceExtraBits[] = {
509 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
510 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
528 else if (m_literal == 256)
539 bits = lengthExtraBits[m_literal-257];
578 for (
unsigned int i=0; i<buffer.size(); i++)
604 unsigned int codeLengths[288];
605 std::fill(codeLengths + 0, codeLengths + 144, 8);
606 std::fill(codeLengths + 144, codeLengths + 256, 9);
607 std::fill(codeLengths + 256, codeLengths + 280, 7);
608 std::fill(codeLengths + 280, codeLengths + 288, 8);
619 unsigned int codeLengths[32];
620 std::fill(codeLengths + 0, codeLengths + 32, 5);
Inflator(BufferedTransformation *attachment=NULL, bool repeat=false, int autoSignalPropagation=-1)
RFC 1951 Decompressor.
unsigned int m_bitsBuffered
iterator end()
Provides an iterator pointing beyond the last element in the memory block.
virtual void ProcessDecompressedData(const byte *string, size_t length)
HuffmanDecoder * operator()() const
use this to make sure LazyPut is finalized in event of exception
Stack-based SecBlock that grows into the heap.
Restricts the instantiation of a class to one static object without locks.
size_t BitsToBytes(size_t bitCount)
Returns the number of 8-bit bytes or octets required for the specified number of bits.
unsigned long GetBits(unsigned int length)
#define NAMESPACE_BEGIN(x)
size_type size() const
Provides the count of elements in the SecBlock.
virtual unsigned int GetLog2WindowSize() const
Classes for automatic resource management.
virtual void ProcessPrestreamHeader()
bool IsolatedFlush(bool hardFlush, bool blocking)
Flushes data buffered by this object, without signal propagation.
void New(size_type newSize)
Change size without preserving contents.
void SkipBits(unsigned int length)
void OutputPast(unsigned int length, unsigned int distance)
static code_t NormalizeCode(code_t code, unsigned int codeBits)
virtual void ProcessPoststreamTail()
void ProcessInput(bool flush)
byte BitReverse(byte value)
Reverses bits in a 8-bit value.
Pointer that overloads operator ->
bool FillBuffer(unsigned int length)
size_t Output(int outputSite, const byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel=DEFAULT_CHANNEL)
Forward processed data on to attached transformation.
Classes and functions for secure memory allocations.
const byte * Spy(size_t &contiguousSize) const
void IsolatedInitialize(const NameValuePairs ¶meters)
Initialize or reinitialize this object, without signal propagation.
const HuffmanDecoder & GetDistanceDecoder() const
bool GetValue(const char *name, T &value) const
Get a named value.
const HuffmanDecoder & GetLiteralDecoder() const
virtual unsigned int MaxPrestreamHeaderSize() const
void Detach(BufferedTransformation *newAttachment=NULL)
Replace an attached transformation.
HuffmanDecoder m_dynamicLiteralDecoder
void Initialize(const unsigned int *codeBitLengths, unsigned int nCodes)
BufferedTransformation * AttachedTransformation()
Retrieve attached transformation.
bool operator()(CryptoPP::HuffmanDecoder::code_t lhs, const CryptoPP::HuffmanDecoder::CodeInfo &rhs)
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
unsigned long long word64
Fixed size stack-based SecBlock.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
#define CRYPTOPP_ASSERT(exp)
iterator begin()
Provides an iterator pointing to the first element in the memory block.
void FillCacheEntry(LookupEntry &entry, code_t normalizedCode) const
PlatformStyle::TableColorType type
uint8_t const size_t const size
unsigned int BitsBuffered() const
void * memcpy(void *a, const void *b, size_t c)
#define CRYPTOPP_UNUSED(x)
lword CurrentSize() const
Provides auto signaling support.
int GetAutoSignalPropagation() const
Retrieve automatic signal propagation value.
Implementation of BufferedTransformation's attachment interface.
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
std::vector< char * > parameters
void OutputString(const byte *string, size_t length)
unsigned long PeekBuffer() const
BufferedTransformation & m_store
HuffmanDecoder * operator()() const
unsigned long PeekBits(unsigned int length)
virtual unsigned int MaxPoststreamTailSize() const
HuffmanDecoder m_dynamicDistanceDecoder
LowFirstBitReader m_reader
unsigned int Decode(code_t code, value_t &value) const
Interface for retrieving values given their names.
bool operator()(const CryptoPP::HuffmanDecoder::CodeInfo &lhs, const CryptoPP::HuffmanDecoder::CodeInfo &rhs)