zdeflate.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // zdeflate.h - originally written and placed in the public domain by Wei Dai
  2. /// \file zdeflate.h
  3. /// \brief DEFLATE compression and decompression (RFC 1951)
  4. #ifndef CRYPTOPP_ZDEFLATE_H
  5. #define CRYPTOPP_ZDEFLATE_H
  6. #include "cryptlib.h"
  7. #include "filters.h"
  8. #include "misc.h"
  9. NAMESPACE_BEGIN(CryptoPP)
  10. /// \brief Encoding table writer
  11. /// \since Crypto++ 1.0
  12. class LowFirstBitWriter : public Filter
  13. {
  14. public:
  15. /// \brief Construct a LowFirstBitWriter
  16. /// \param attachment an attached transformation
  17. LowFirstBitWriter(BufferedTransformation *attachment);
  18. void PutBits(unsigned long value, unsigned int length);
  19. void FlushBitBuffer();
  20. void ClearBitBuffer();
  21. void StartCounting();
  22. unsigned long FinishCounting();
  23. protected:
  24. bool m_counting;
  25. unsigned long m_bitCount;
  26. unsigned long m_buffer;
  27. unsigned int m_bitsBuffered, m_bytesBuffered;
  28. FixedSizeSecBlock<byte, 256> m_outputBuffer;
  29. };
  30. /// \brief Huffman Encoder
  31. /// \since Crypto++ 1.0
  32. class HuffmanEncoder
  33. {
  34. public:
  35. typedef unsigned int code_t;
  36. typedef unsigned int value_t;
  37. /// \brief Construct a HuffmanEncoder
  38. HuffmanEncoder() {}
  39. /// \brief Construct a HuffmanEncoder
  40. /// \param codeBits a table of code bits
  41. /// \param nCodes the number of codes in the table
  42. HuffmanEncoder(const unsigned int *codeBits, unsigned int nCodes);
  43. /// \brief Initialize or reinitialize this object
  44. /// \param codeBits a table of code bits
  45. /// \param nCodes the number of codes in the table
  46. void Initialize(const unsigned int *codeBits, unsigned int nCodes);
  47. static void GenerateCodeLengths(unsigned int *codeBits, unsigned int maxCodeBits, const unsigned int *codeCounts, size_t nCodes);
  48. void Encode(LowFirstBitWriter &writer, value_t value) const;
  49. struct Code
  50. {
  51. unsigned int code;
  52. unsigned int len;
  53. };
  54. SecBlock<Code> m_valueToCode;
  55. };
  56. /// \brief DEFLATE compressor (RFC 1951)
  57. /// \since Crypto++ 1.0
  58. class Deflator : public LowFirstBitWriter
  59. {
  60. public:
  61. /// \brief Deflate level as enumerated values.
  62. enum {
  63. /// \brief Minimum deflation level, fastest speed (0)
  64. MIN_DEFLATE_LEVEL = 0,
  65. /// \brief Default deflation level, compromise between speed (6)
  66. DEFAULT_DEFLATE_LEVEL = 6,
  67. /// \brief Minimum deflation level, slowest speed (9)
  68. MAX_DEFLATE_LEVEL = 9};
  69. /// \brief Windows size as enumerated values.
  70. enum {
  71. /// \brief Minimum window size, smallest table (9)
  72. MIN_LOG2_WINDOW_SIZE = 9,
  73. /// \brief Default window size (15)
  74. DEFAULT_LOG2_WINDOW_SIZE = 15,
  75. /// \brief Maximum window size, largest table (15)
  76. MAX_LOG2_WINDOW_SIZE = 15};
  77. /// \brief Construct a Deflator compressor
  78. /// \param attachment an attached transformation
  79. /// \param deflateLevel the deflate level
  80. /// \param log2WindowSize the window size
  81. /// \param detectUncompressible flag to detect if data is compressible
  82. /// \details detectUncompressible makes it faster to process uncompressible files, but
  83. /// if a file has both compressible and uncompressible parts, it may fail to compress
  84. /// some of the compressible parts.
  85. Deflator(BufferedTransformation *attachment=NULLPTR, int deflateLevel=DEFAULT_DEFLATE_LEVEL, int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE, bool detectUncompressible=true);
  86. /// \brief Construct a Deflator compressor
  87. /// \param parameters a set of NameValuePairs to initialize this object
  88. /// \param attachment an attached transformation
  89. /// \details Possible parameter names: Log2WindowSize, DeflateLevel, DetectUncompressible
  90. Deflator(const NameValuePairs &parameters, BufferedTransformation *attachment=NULLPTR);
  91. /// \brief Sets the deflation level
  92. /// \param deflateLevel the level of deflation
  93. /// \details SetDeflateLevel can be used to set the deflate level in the middle of compression
  94. void SetDeflateLevel(int deflateLevel);
  95. /// \brief Retrieves the deflation level
  96. /// \return the level of deflation
  97. int GetDeflateLevel() const {return m_deflateLevel;}
  98. /// \brief Retrieves the window size
  99. /// \return the windows size
  100. int GetLog2WindowSize() const {return m_log2WindowSize;}
  101. void IsolatedInitialize(const NameValuePairs &parameters);
  102. size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
  103. bool IsolatedFlush(bool hardFlush, bool blocking);
  104. protected:
  105. virtual void WritePrestreamHeader() {}
  106. virtual void ProcessUncompressedData(const byte *string, size_t length)
  107. {CRYPTOPP_UNUSED(string), CRYPTOPP_UNUSED(length);}
  108. virtual void WritePoststreamTail() {}
  109. enum {STORED = 0, STATIC = 1, DYNAMIC = 2};
  110. enum {MIN_MATCH = 3, MAX_MATCH = 258};
  111. void InitializeStaticEncoders();
  112. void Reset(bool forceReset = false);
  113. unsigned int FillWindow(const byte *str, size_t length);
  114. unsigned int ComputeHash(const byte *str) const;
  115. unsigned int LongestMatch(unsigned int &bestMatch) const;
  116. void InsertString(unsigned int start);
  117. void ProcessBuffer();
  118. void LiteralByte(byte b);
  119. void MatchFound(unsigned int distance, unsigned int length);
  120. void EncodeBlock(bool eof, unsigned int blockType);
  121. void EndBlock(bool eof);
  122. struct EncodedMatch
  123. {
  124. unsigned literalCode : 9;
  125. unsigned literalExtra : 5;
  126. unsigned distanceCode : 5;
  127. unsigned distanceExtra : 13;
  128. };
  129. int m_deflateLevel, m_log2WindowSize, m_compressibleDeflateLevel;
  130. unsigned int m_detectSkip, m_detectCount;
  131. unsigned int DSIZE, DMASK, HSIZE, HMASK, GOOD_MATCH, MAX_LAZYLENGTH, MAX_CHAIN_LENGTH;
  132. bool m_headerWritten, m_matchAvailable;
  133. unsigned int m_dictionaryEnd, m_stringStart, m_lookahead, m_minLookahead, m_previousMatch, m_previousLength;
  134. HuffmanEncoder m_staticLiteralEncoder, m_staticDistanceEncoder, m_dynamicLiteralEncoder, m_dynamicDistanceEncoder;
  135. SecByteBlock m_byteBuffer;
  136. SecBlock<word16> m_head, m_prev;
  137. FixedSizeSecBlock<unsigned int, 286> m_literalCounts;
  138. FixedSizeSecBlock<unsigned int, 30> m_distanceCounts;
  139. SecBlock<EncodedMatch> m_matchBuffer;
  140. unsigned int m_matchBufferEnd, m_blockStart, m_blockLength;
  141. };
  142. NAMESPACE_END
  143. #endif