Counter Strike : Global Offensive Source Code
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.

121 lines
3.9 KiB

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