Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.3 KiB

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