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.

149 lines
4.4 KiB

  1. #ifndef CRYPTOPP_ZINFLATE_H
  2. #define CRYPTOPP_ZINFLATE_H
  3. #include "filters.h"
  4. #include <vector>
  5. NAMESPACE_BEGIN(CryptoPP)
  6. //! _
  7. class LowFirstBitReader
  8. {
  9. public:
  10. LowFirstBitReader(BufferedTransformation &store)
  11. : m_store(store), m_buffer(0), m_bitsBuffered(0) {}
  12. // unsigned long BitsLeft() const {return m_store.MaxRetrievable() * 8 + m_bitsBuffered;}
  13. unsigned int BitsBuffered() const {return m_bitsBuffered;}
  14. unsigned long PeekBuffer() const {return m_buffer;}
  15. bool FillBuffer(unsigned int length);
  16. unsigned long PeekBits(unsigned int length);
  17. void SkipBits(unsigned int length);
  18. unsigned long GetBits(unsigned int length);
  19. private:
  20. BufferedTransformation &m_store;
  21. unsigned long m_buffer;
  22. unsigned int m_bitsBuffered;
  23. };
  24. struct CodeLessThan;
  25. //! Huffman Decoder
  26. class HuffmanDecoder
  27. {
  28. public:
  29. typedef unsigned int code_t;
  30. typedef unsigned int value_t;
  31. enum {MAX_CODE_BITS = sizeof(code_t)*8};
  32. class Err : public Exception {public: Err(const std::string &what) : Exception(INVALID_DATA_FORMAT, "HuffmanDecoder: " + what) {}};
  33. HuffmanDecoder() {}
  34. HuffmanDecoder(const unsigned int *codeBitLengths, unsigned int nCodes) {Initialize(codeBitLengths, nCodes);}
  35. void Initialize(const unsigned int *codeBitLengths, unsigned int nCodes);
  36. unsigned int Decode(code_t code, /* out */ value_t &value) const;
  37. bool Decode(LowFirstBitReader &reader, value_t &value) const;
  38. private:
  39. friend struct CodeLessThan;
  40. struct CodeInfo
  41. {
  42. CodeInfo(code_t code=0, unsigned int len=0, value_t value=0) : code(code), len(len), value(value) {}
  43. inline bool operator<(const CodeInfo &rhs) const {return code < rhs.code;}
  44. code_t code;
  45. unsigned int len;
  46. value_t value;
  47. };
  48. struct LookupEntry
  49. {
  50. unsigned int type;
  51. union
  52. {
  53. value_t value;
  54. const CodeInfo *begin;
  55. };
  56. union
  57. {
  58. unsigned int len;
  59. const CodeInfo *end;
  60. };
  61. };
  62. static code_t NormalizeCode(code_t code, unsigned int codeBits);
  63. void FillCacheEntry(LookupEntry &entry, code_t normalizedCode) const;
  64. unsigned int m_maxCodeBits, m_cacheBits, m_cacheMask, m_normalizedCacheMask;
  65. std::vector<CodeInfo, AllocatorWithCleanup<CodeInfo> > m_codeToValue;
  66. mutable std::vector<LookupEntry, AllocatorWithCleanup<LookupEntry> > m_cache;
  67. };
  68. //! DEFLATE (RFC 1951) decompressor
  69. class Inflator : public AutoSignaling<Filter>
  70. {
  71. public:
  72. class Err : public Exception
  73. {
  74. public:
  75. Err(ErrorType e, const std::string &s)
  76. : Exception(e, s) {}
  77. };
  78. class UnexpectedEndErr : public Err {public: UnexpectedEndErr() : Err(INVALID_DATA_FORMAT, "Inflator: unexpected end of compressed block") {}};
  79. class BadBlockErr : public Err {public: BadBlockErr() : Err(INVALID_DATA_FORMAT, "Inflator: error in compressed block") {}};
  80. /*! \param repeat decompress multiple compressed streams in series
  81. \param autoSignalPropagation 0 to turn off MessageEnd signal
  82. */
  83. Inflator(BufferedTransformation *attachment = NULL, bool repeat = false, int autoSignalPropagation = -1);
  84. void IsolatedInitialize(const NameValuePairs &parameters);
  85. size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
  86. bool IsolatedFlush(bool hardFlush, bool blocking);
  87. virtual unsigned int GetLog2WindowSize() const {return 15;}
  88. protected:
  89. ByteQueue m_inQueue;
  90. private:
  91. virtual unsigned int MaxPrestreamHeaderSize() const {return 0;}
  92. virtual void ProcessPrestreamHeader() {}
  93. virtual void ProcessDecompressedData(const byte *string, size_t length)
  94. {AttachedTransformation()->Put(string, length);}
  95. virtual unsigned int MaxPoststreamTailSize() const {return 0;}
  96. virtual void ProcessPoststreamTail() {}
  97. void ProcessInput(bool flush);
  98. void DecodeHeader();
  99. bool DecodeBody();
  100. void FlushOutput();
  101. void OutputByte(byte b);
  102. void OutputString(const byte *string, size_t length);
  103. void OutputPast(unsigned int length, unsigned int distance);
  104. static const HuffmanDecoder *FixedLiteralDecoder();
  105. static const HuffmanDecoder *FixedDistanceDecoder();
  106. const HuffmanDecoder& GetLiteralDecoder() const;
  107. const HuffmanDecoder& GetDistanceDecoder() const;
  108. enum State {PRE_STREAM, WAIT_HEADER, DECODING_BODY, POST_STREAM, AFTER_END};
  109. State m_state;
  110. bool m_repeat, m_eof, m_wrappedAround;
  111. byte m_blockType;
  112. word16 m_storedLen;
  113. enum NextDecode {LITERAL, LENGTH_BITS, DISTANCE, DISTANCE_BITS};
  114. NextDecode m_nextDecode;
  115. unsigned int m_literal, m_distance; // for LENGTH_BITS or DISTANCE_BITS
  116. HuffmanDecoder m_dynamicLiteralDecoder, m_dynamicDistanceDecoder;
  117. LowFirstBitReader m_reader;
  118. SecByteBlock m_window;
  119. size_t m_current, m_lastFlush;
  120. };
  121. NAMESPACE_END
  122. #endif