monotone

monotone Mtn Source Tree

Root/cryptopp/zdeflate.h

1#ifndef CRYPTOPP_ZDEFLATE_H
2#define CRYPTOPP_ZDEFLATE_H
3
4#include "filters.h"
5#include "misc.h"
6
7NAMESPACE_BEGIN(CryptoPP)
8
9//! .
10class LowFirstBitWriter : public Filter
11{
12public:
13LowFirstBitWriter(BufferedTransformation *attachment);
14void PutBits(unsigned long value, unsigned int length);
15void FlushBitBuffer();
16void ClearBitBuffer();
17
18void StartCounting();
19unsigned long FinishCounting();
20
21protected:
22bool m_counting;
23unsigned long m_bitCount;
24unsigned long m_buffer;
25unsigned int m_bitsBuffered, m_bytesBuffered;
26FixedSizeSecBlock<byte, 256> m_outputBuffer;
27};
28
29//! Huffman Encoder
30class HuffmanEncoder
31{
32public:
33typedef unsigned int code_t;
34typedef unsigned int value_t;
35
36HuffmanEncoder() {}
37HuffmanEncoder(const unsigned int *codeBits, unsigned int nCodes);
38void Initialize(const unsigned int *codeBits, unsigned int nCodes);
39
40static void GenerateCodeLengths(unsigned int *codeBits, unsigned int maxCodeBits, const unsigned int *codeCounts, unsigned int nCodes);
41
42void Encode(LowFirstBitWriter &writer, value_t value) const;
43
44struct Code
45{
46unsigned int code;
47unsigned int len;
48};
49
50SecBlock<Code> m_valueToCode;
51};
52
53//! DEFLATE (RFC 1951) compressor
54
55class Deflator : public LowFirstBitWriter
56{
57public:
58enum {MIN_DEFLATE_LEVEL = 0, DEFAULT_DEFLATE_LEVEL = 6, MAX_DEFLATE_LEVEL = 9};
59enum {MIN_LOG2_WINDOW_SIZE = 9, DEFAULT_LOG2_WINDOW_SIZE = 15, MAX_LOG2_WINDOW_SIZE = 15};
60Deflator(BufferedTransformation *attachment=NULL, int deflateLevel=DEFAULT_DEFLATE_LEVEL, int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE);
61//! possible parameter names: Log2WindowSize, DeflateLevel
62Deflator(const NameValuePairs &parameters, BufferedTransformation *attachment=NULL);
63
64//! this function can be used to set the deflate level in the middle of compression
65void SetDeflateLevel(int deflateLevel);
66int GetDeflateLevel() const {return m_deflateLevel;}
67int GetLog2WindowSize() const {return m_log2WindowSize;}
68
69void IsolatedInitialize(const NameValuePairs &parameters);
70unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking);
71bool IsolatedFlush(bool hardFlush, bool blocking);
72
73private:
74virtual void WritePrestreamHeader() {}
75virtual void ProcessUncompressedData(const byte *string, unsigned int length) {}
76virtual void WritePoststreamTail() {}
77
78enum {STORED = 0, STATIC = 1, DYNAMIC = 2};
79enum {MIN_MATCH = 3, MAX_MATCH = 258};
80
81void InitializeStaticEncoders();
82void Reset(bool forceReset = false);
83unsigned int FillWindow(const byte *str, unsigned int length);
84unsigned int ComputeHash(const byte *str) const;
85unsigned int LongestMatch(unsigned int &bestMatch) const;
86void InsertString(unsigned int start);
87void ProcessBuffer();
88
89void LiteralByte(byte b);
90void MatchFound(unsigned int distance, unsigned int length);
91void EncodeBlock(bool eof, unsigned int blockType);
92void EndBlock(bool eof);
93
94struct EncodedMatch
95{
96unsigned literalCode : 9;
97unsigned literalExtra : 5;
98unsigned distanceCode : 5;
99unsigned distanceExtra : 13;
100};
101
102int m_deflateLevel, m_log2WindowSize;
103unsigned int DSIZE, DMASK, HSIZE, HMASK, GOOD_MATCH, MAX_LAZYLENGTH, MAX_CHAIN_LENGTH;
104bool m_headerWritten, m_matchAvailable;
105unsigned int m_dictionaryEnd, m_stringStart, m_lookahead, m_minLookahead, m_previousMatch, m_previousLength;
106HuffmanEncoder m_staticLiteralEncoder, m_staticDistanceEncoder, m_dynamicLiteralEncoder, m_dynamicDistanceEncoder;
107SecByteBlock m_byteBuffer;
108SecBlock<word16> m_head, m_prev;
109FixedSizeSecBlock<unsigned int, 286> m_literalCounts;
110FixedSizeSecBlock<unsigned int, 30> m_distanceCounts;
111SecBlock<EncodedMatch> m_matchBuffer;
112unsigned int m_matchBufferEnd, m_blockStart, m_blockLength;
113};
114
115NAMESPACE_END
116
117#endif

Archive Download this file

Branches

Tags

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