Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

zdeflate.h

00001 #ifndef CRYPTOPP_ZDEFLATE_H
00002 #define CRYPTOPP_ZDEFLATE_H
00003 
00004 #include "filters.h"
00005 #include "misc.h"
00006 
00007 NAMESPACE_BEGIN(CryptoPP)
00008 
00009 //! .
00010 class LowFirstBitWriter : public Filter
00011 {
00012 public:
00013         LowFirstBitWriter(BufferedTransformation *attachment);
00014         void PutBits(unsigned long value, unsigned int length);
00015         void FlushBitBuffer();
00016         void ClearBitBuffer();
00017 
00018         void StartCounting();
00019         unsigned long FinishCounting();
00020 
00021 protected:
00022         bool m_counting;
00023         unsigned long m_bitCount;
00024         unsigned long m_buffer;
00025         unsigned int m_bitsBuffered, m_bytesBuffered;
00026         FixedSizeSecBlock<byte, 256> m_outputBuffer;
00027 };
00028 
00029 //! Huffman Encoder
00030 class HuffmanEncoder
00031 {
00032 public:
00033         typedef unsigned int code_t;
00034         typedef unsigned int value_t;
00035 
00036         HuffmanEncoder() {}
00037         HuffmanEncoder(const unsigned int *codeBits, unsigned int nCodes);
00038         void Initialize(const unsigned int *codeBits, unsigned int nCodes);
00039 
00040         static void GenerateCodeLengths(unsigned int *codeBits, unsigned int maxCodeBits, const unsigned int *codeCounts, unsigned int nCodes);
00041 
00042         void Encode(LowFirstBitWriter &writer, value_t value) const;
00043 
00044         struct Code
00045         {
00046                 unsigned int code;
00047                 unsigned int len;
00048         };
00049 
00050         SecBlock<Code> m_valueToCode;
00051 };
00052 
00053 //! DEFLATE (RFC 1951) compressor
00054 
00055 class Deflator : public LowFirstBitWriter
00056 {
00057 public:
00058         enum {MIN_DEFLATE_LEVEL = 0, DEFAULT_DEFLATE_LEVEL = 6, MAX_DEFLATE_LEVEL = 9};
00059         enum {MIN_LOG2_WINDOW_SIZE = 9, DEFAULT_LOG2_WINDOW_SIZE = 15, MAX_LOG2_WINDOW_SIZE = 15};
00060         Deflator(BufferedTransformation *attachment=NULL, int deflateLevel=DEFAULT_DEFLATE_LEVEL, int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE);
00061         //! possible parameter names: Log2WindowSize, DeflateLevel
00062         Deflator(const NameValuePairs &parameters, BufferedTransformation *attachment=NULL);
00063 
00064         //! this function can be used to set the deflate level in the middle of compression
00065         void SetDeflateLevel(int deflateLevel);
00066         int GetDeflateLevel() const {return m_deflateLevel;}
00067         int GetLog2WindowSize() const {return m_log2WindowSize;}
00068 
00069         void IsolatedInitialize(const NameValuePairs &parameters);
00070         unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking);
00071         bool IsolatedFlush(bool hardFlush, bool blocking);
00072 
00073 private:
00074         virtual void WritePrestreamHeader() {}
00075         virtual void ProcessUncompressedData(const byte *string, unsigned int length) {}
00076         virtual void WritePoststreamTail() {}
00077 
00078         enum {STORED = 0, STATIC = 1, DYNAMIC = 2};
00079         enum {MIN_MATCH = 3, MAX_MATCH = 258};
00080 
00081         void InitializeStaticEncoders();
00082         void Reset(bool forceReset = false);
00083         unsigned int FillWindow(const byte *str, unsigned int length);
00084         unsigned int ComputeHash(const byte *str) const;
00085         unsigned int LongestMatch(unsigned int &bestMatch) const;
00086         void InsertString(unsigned int start);
00087         void ProcessBuffer();
00088 
00089         void LiteralByte(byte b);
00090         void MatchFound(unsigned int distance, unsigned int length);
00091         void EncodeBlock(bool eof, unsigned int blockType);
00092         void EndBlock(bool eof);
00093 
00094         struct EncodedMatch
00095         {
00096                 unsigned literalCode : 9;
00097                 unsigned literalExtra : 5;
00098                 unsigned distanceCode : 5;
00099                 unsigned distanceExtra : 13;
00100         };
00101 
00102         int m_deflateLevel, m_log2WindowSize;
00103         unsigned int DSIZE, DMASK, HSIZE, HMASK, GOOD_MATCH, MAX_LAZYLENGTH, MAX_CHAIN_LENGTH;
00104         bool m_headerWritten, m_matchAvailable;
00105         unsigned int m_dictionaryEnd, m_stringStart, m_lookahead, m_minLookahead, m_previousMatch, m_previousLength;
00106         HuffmanEncoder m_staticLiteralEncoder, m_staticDistanceEncoder, m_dynamicLiteralEncoder, m_dynamicDistanceEncoder;
00107         SecByteBlock m_byteBuffer;
00108         SecBlock<word16> m_head, m_prev;
00109         FixedSizeSecBlock<unsigned int, 286> m_literalCounts;
00110         FixedSizeSecBlock<unsigned int, 30> m_distanceCounts;
00111         SecBlock<EncodedMatch> m_matchBuffer;
00112         unsigned int m_matchBufferEnd, m_blockStart, m_blockLength;
00113 };
00114 
00115 NAMESPACE_END
00116 
00117 #endif

Generated on Mon Apr 19 18:12:34 2004 for Crypto++ by doxygen 1.3.6-20040222