File indexing completed on 2025-01-18 09:55:10
0001
0002
0003
0004
0005
0006 #ifndef CRYPTOPP_ZDEFLATE_H
0007 #define CRYPTOPP_ZDEFLATE_H
0008
0009 #include "cryptlib.h"
0010 #include "filters.h"
0011 #include "misc.h"
0012
0013 NAMESPACE_BEGIN(CryptoPP)
0014
0015
0016
0017 class LowFirstBitWriter : public Filter
0018 {
0019 public:
0020
0021
0022 LowFirstBitWriter(BufferedTransformation *attachment);
0023
0024 void PutBits(unsigned long value, unsigned int length);
0025 void FlushBitBuffer();
0026 void ClearBitBuffer();
0027
0028 void StartCounting();
0029 unsigned long FinishCounting();
0030
0031 protected:
0032 bool m_counting;
0033 unsigned long m_bitCount;
0034 unsigned long m_buffer;
0035 unsigned int m_bitsBuffered, m_bytesBuffered;
0036 FixedSizeSecBlock<byte, 256> m_outputBuffer;
0037 };
0038
0039
0040
0041 class HuffmanEncoder
0042 {
0043 public:
0044 typedef unsigned int code_t;
0045 typedef unsigned int value_t;
0046
0047
0048 HuffmanEncoder() {}
0049
0050
0051
0052
0053 HuffmanEncoder(const unsigned int *codeBits, unsigned int nCodes);
0054
0055
0056
0057
0058 void Initialize(const unsigned int *codeBits, unsigned int nCodes);
0059
0060 static void GenerateCodeLengths(unsigned int *codeBits, unsigned int maxCodeBits, const unsigned int *codeCounts, size_t nCodes);
0061
0062 void Encode(LowFirstBitWriter &writer, value_t value) const;
0063
0064 struct Code
0065 {
0066 unsigned int code;
0067 unsigned int len;
0068 };
0069
0070 SecBlock<Code> m_valueToCode;
0071 };
0072
0073
0074
0075 class Deflator : public LowFirstBitWriter
0076 {
0077 public:
0078
0079 enum {
0080
0081 MIN_DEFLATE_LEVEL = 0,
0082
0083 DEFAULT_DEFLATE_LEVEL = 6,
0084
0085 MAX_DEFLATE_LEVEL = 9};
0086
0087
0088 enum {
0089
0090 MIN_LOG2_WINDOW_SIZE = 9,
0091
0092 DEFAULT_LOG2_WINDOW_SIZE = 15,
0093
0094 MAX_LOG2_WINDOW_SIZE = 15};
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104 Deflator(BufferedTransformation *attachment=NULLPTR, int deflateLevel=DEFAULT_DEFLATE_LEVEL, int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE, bool detectUncompressible=true);
0105
0106
0107
0108
0109 Deflator(const NameValuePairs ¶meters, BufferedTransformation *attachment=NULLPTR);
0110
0111
0112
0113
0114 void SetDeflateLevel(int deflateLevel);
0115
0116
0117
0118 int GetDeflateLevel() const {return m_deflateLevel;}
0119
0120
0121
0122 int GetLog2WindowSize() const {return m_log2WindowSize;}
0123
0124 void IsolatedInitialize(const NameValuePairs ¶meters);
0125 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
0126 bool IsolatedFlush(bool hardFlush, bool blocking);
0127
0128 protected:
0129 virtual void WritePrestreamHeader() {}
0130 virtual void ProcessUncompressedData(const byte *string, size_t length)
0131 {CRYPTOPP_UNUSED(string), CRYPTOPP_UNUSED(length);}
0132 virtual void WritePoststreamTail() {}
0133
0134 enum {STORED = 0, STATIC = 1, DYNAMIC = 2};
0135 enum {MIN_MATCH = 3, MAX_MATCH = 258};
0136
0137 void InitializeStaticEncoders();
0138 void Reset(bool forceReset = false);
0139 unsigned int FillWindow(const byte *str, size_t length);
0140 unsigned int ComputeHash(const byte *str) const;
0141 unsigned int LongestMatch(unsigned int &bestMatch) const;
0142 void InsertString(unsigned int start);
0143 void ProcessBuffer();
0144
0145 void LiteralByte(byte b);
0146 void MatchFound(unsigned int distance, unsigned int length);
0147 void EncodeBlock(bool eof, unsigned int blockType);
0148 void EndBlock(bool eof);
0149
0150 struct EncodedMatch
0151 {
0152 unsigned literalCode : 9;
0153 unsigned literalExtra : 5;
0154 unsigned distanceCode : 5;
0155 unsigned distanceExtra : 13;
0156 };
0157
0158 int m_deflateLevel, m_log2WindowSize, m_compressibleDeflateLevel;
0159 unsigned int m_detectSkip, m_detectCount;
0160 unsigned int DSIZE, DMASK, HSIZE, HMASK, GOOD_MATCH, MAX_LAZYLENGTH, MAX_CHAIN_LENGTH;
0161 bool m_headerWritten, m_matchAvailable;
0162 unsigned int m_dictionaryEnd, m_stringStart, m_lookahead, m_minLookahead, m_previousMatch, m_previousLength;
0163 HuffmanEncoder m_staticLiteralEncoder, m_staticDistanceEncoder, m_dynamicLiteralEncoder, m_dynamicDistanceEncoder;
0164 SecByteBlock m_byteBuffer;
0165 SecBlock<word16> m_head, m_prev;
0166 FixedSizeSecBlock<unsigned int, 286> m_literalCounts;
0167 FixedSizeSecBlock<unsigned int, 30> m_distanceCounts;
0168 SecBlock<EncodedMatch> m_matchBuffer;
0169 unsigned int m_matchBufferEnd, m_blockStart, m_blockLength;
0170 };
0171
0172 NAMESPACE_END
0173
0174 #endif