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.

99 lines
2.5 KiB

  1. // gzip.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #include "gzip.h"
  4. NAMESPACE_BEGIN(CryptoPP)
  5. void Gzip::WritePrestreamHeader()
  6. {
  7. m_totalLen = 0;
  8. m_crc.Restart();
  9. AttachedTransformation()->Put(MAGIC1);
  10. AttachedTransformation()->Put(MAGIC2);
  11. AttachedTransformation()->Put(DEFLATED);
  12. AttachedTransformation()->Put(0); // general flag
  13. AttachedTransformation()->PutWord32(0); // time stamp
  14. byte extra = (GetDeflateLevel() == 1) ? FAST : ((GetDeflateLevel() == 9) ? SLOW : 0);
  15. AttachedTransformation()->Put(extra);
  16. AttachedTransformation()->Put(GZIP_OS_CODE);
  17. }
  18. void Gzip::ProcessUncompressedData(const byte *inString, size_t length)
  19. {
  20. m_crc.Update(inString, length);
  21. m_totalLen += (word32)length;
  22. }
  23. void Gzip::WritePoststreamTail()
  24. {
  25. SecByteBlock crc(4);
  26. m_crc.Final(crc);
  27. AttachedTransformation()->Put(crc, 4);
  28. AttachedTransformation()->PutWord32(m_totalLen, LITTLE_ENDIAN_ORDER);
  29. }
  30. // *************************************************************
  31. Gunzip::Gunzip(BufferedTransformation *attachment, bool repeat, int propagation)
  32. : Inflator(attachment, repeat, propagation)
  33. {
  34. }
  35. void Gunzip::ProcessPrestreamHeader()
  36. {
  37. m_length = 0;
  38. m_crc.Restart();
  39. byte buf[6];
  40. byte b, flags;
  41. if (m_inQueue.Get(buf, 2)!=2) throw HeaderErr();
  42. if (buf[0] != MAGIC1 || buf[1] != MAGIC2) throw HeaderErr();
  43. if (!m_inQueue.Skip(1)) throw HeaderErr(); // skip extra flags
  44. if (!m_inQueue.Get(flags)) throw HeaderErr();
  45. if (flags & (ENCRYPTED | CONTINUED)) throw HeaderErr();
  46. if (m_inQueue.Skip(6)!=6) throw HeaderErr(); // Skip file time, extra flags and OS type
  47. if (flags & EXTRA_FIELDS) // skip extra fields
  48. {
  49. word16 length;
  50. if (m_inQueue.GetWord16(length, LITTLE_ENDIAN_ORDER) != 2) throw HeaderErr();
  51. if (m_inQueue.Skip(length)!=length) throw HeaderErr();
  52. }
  53. if (flags & FILENAME) // skip filename
  54. do
  55. if(!m_inQueue.Get(b)) throw HeaderErr();
  56. while (b);
  57. if (flags & COMMENTS) // skip comments
  58. do
  59. if(!m_inQueue.Get(b)) throw HeaderErr();
  60. while (b);
  61. }
  62. void Gunzip::ProcessDecompressedData(const byte *inString, size_t length)
  63. {
  64. AttachedTransformation()->Put(inString, length);
  65. m_crc.Update(inString, length);
  66. m_length += (word32)length;
  67. }
  68. void Gunzip::ProcessPoststreamTail()
  69. {
  70. SecByteBlock crc(4);
  71. if (m_inQueue.Get(crc, 4) != 4)
  72. throw TailErr();
  73. if (!m_crc.Verify(crc))
  74. throw CrcErr();
  75. word32 lengthCheck;
  76. if (m_inQueue.GetWord32(lengthCheck, LITTLE_ENDIAN_ORDER) != 4)
  77. throw TailErr();
  78. if (lengthCheck != m_length)
  79. throw LengthErr();
  80. }
  81. NAMESPACE_END