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
00010 class LowFirstBitWriter : public Filter
00011 {
00012 public:
00013 LowFirstBitWriter(BufferedTransformation *outQ);
00014 void PutBits(unsigned long value, unsigned int length);
00015 void FlushBitBuffer();
00016
00017 void StartCounting();
00018 unsigned long FinishCounting();
00019
00020 protected:
00021 bool m_counting;
00022 unsigned long m_bitCount;
00023 unsigned long m_buffer;
00024 unsigned int m_bitsBuffered, m_bytesBuffered;
00025 SecByteBlock m_outputBuffer;
00026 };
00027
00029 class HuffmanEncoder
00030 {
00031 public:
00032 typedef unsigned int code_t;
00033 typedef unsigned int value_t;
00034
00035 HuffmanEncoder() {}
00036 HuffmanEncoder(const unsigned int *codeBits, unsigned int nCodes);
00037 void Initialize(const unsigned int *codeBits, unsigned int nCodes);
00038
00039 static void GenerateCodeLengths(unsigned int *codeBits, unsigned int maxCodeBits, const unsigned int *codeCounts, unsigned int nCodes);
00040
00041 void Encode(LowFirstBitWriter &writer, value_t value) const;
00042
00043 struct Code
00044 {
00045 unsigned int code;
00046 unsigned int len;
00047 };
00048
00049 SecBlock<Code> m_valueToCode;
00050 };
00051
00053
00054 class Deflator : public LowFirstBitWriter
00055 {
00056 public:
00057 enum {DEFAULT_DEFLATE_LEVEL = 6, DEFAULT_LOG2_WINDOW_SIZE = 15};
00058 Deflator(BufferedTransformation *outQ=NULL, unsigned int deflateLevel=DEFAULT_DEFLATE_LEVEL, unsigned int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE);
00059
00060 void SetDeflateLevel(unsigned int deflateLevel);
00061 unsigned int GetDeflateLevel() const {return m_deflateLevel;}
00062 unsigned int GetLog2WindowSize() const {return m_log2WindowSize;}
00063
00064 void Put(byte inByte)
00065 {Deflator::Put(&inByte, 1);}
00066 void Put(const byte *str, unsigned int length);
00067 void Flush(bool completeFlush, int propagation=-1);
00068 void MessageEnd(int propagation=-1);
00069
00070 private:
00071 virtual void WritePrestreamHeader() {}
00072 virtual void ProcessUncompressedData(const byte *string, unsigned int length) {}
00073 virtual void WritePoststreamTail() {}
00074
00075 enum {STORED = 0, STATIC = 1, DYNAMIC = 2};
00076 enum {MIN_MATCH = 3, MAX_MATCH = 258};
00077
00078 void Reset();
00079 unsigned int FillWindow(const byte *str, unsigned int length);
00080 unsigned int ComputeHash(const byte *str) const;
00081 unsigned int LongestMatch(unsigned int &bestMatch) const;
00082 void InsertString(unsigned int start);
00083 void ProcessBuffer();
00084
00085 void LiteralByte(byte b);
00086 void MatchFound(unsigned int distance, unsigned int length);
00087 void EncodeBlock(bool eof, unsigned int blockType);
00088 void EndBlock(bool eof);
00089
00090 struct EncodedMatch
00091 {
00092 unsigned literalCode : 9;
00093 unsigned literalExtra : 5;
00094 unsigned distanceCode : 5;
00095 unsigned distanceExtra : 13;
00096 };
00097
00098 unsigned int m_deflateLevel, m_log2WindowSize;
00099 unsigned int DSIZE, DMASK, HSIZE, HMASK, GOOD_MATCH, MAX_LAZYLENGTH, MAX_CHAIN_LENGTH;
00100 bool m_headerWritten, m_matchAvailable;
00101 unsigned int m_dictionaryEnd, m_stringStart, m_lookahead, m_minLookahead, m_previousMatch, m_previousLength;
00102 HuffmanEncoder m_staticLiteralEncoder, m_staticDistanceEncoder, m_dynamicLiteralEncoder, m_dynamicDistanceEncoder;
00103 SecByteBlock m_byteBuffer;
00104 SecBlock<word16> m_head, m_prev;
00105 SecBlock<unsigned int> m_literalCounts, m_distanceCounts;
00106 SecBlock<EncodedMatch> m_matchBuffer;
00107 unsigned int m_matchBufferEnd, m_blockStart, m_blockLength;
00108 };
00109
00110 NAMESPACE_END
00111
00112 #endif