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.

90 lines
2.3 KiB

  1. // zlib.cpp - written and placed in the public domain by Wei Dai
  2. // "zlib" is the name of a well known C language compression library
  3. // (http://www.zlib.org) and also the name of a compression format
  4. // (RFC 1950) that the library implements. This file is part of a
  5. // complete reimplementation of the zlib compression format.
  6. #include "pch.h"
  7. #include "zlib.h"
  8. #include "zdeflate.h"
  9. #include "zinflate.h"
  10. NAMESPACE_BEGIN(CryptoPP)
  11. static const byte DEFLATE_METHOD = 8;
  12. static const byte FDICT_FLAG = 1 << 5;
  13. // *************************************************************
  14. void ZlibCompressor::WritePrestreamHeader()
  15. {
  16. m_adler32.Restart();
  17. byte cmf = DEFLATE_METHOD | ((GetLog2WindowSize()-8) << 4);
  18. byte flags = GetCompressionLevel() << 6;
  19. AttachedTransformation()->PutWord16(RoundUpToMultipleOf(cmf*256+flags, 31));
  20. }
  21. void ZlibCompressor::ProcessUncompressedData(const byte *inString, size_t length)
  22. {
  23. m_adler32.Update(inString, length);
  24. }
  25. void ZlibCompressor::WritePoststreamTail()
  26. {
  27. FixedSizeSecBlock<byte, 4> adler32;
  28. m_adler32.Final(adler32);
  29. AttachedTransformation()->Put(adler32, 4);
  30. }
  31. unsigned int ZlibCompressor::GetCompressionLevel() const
  32. {
  33. static const unsigned int deflateToCompressionLevel[] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3};
  34. return deflateToCompressionLevel[GetDeflateLevel()];
  35. }
  36. // *************************************************************
  37. ZlibDecompressor::ZlibDecompressor(BufferedTransformation *attachment, bool repeat, int propagation)
  38. : Inflator(attachment, repeat, propagation)
  39. {
  40. }
  41. void ZlibDecompressor::ProcessPrestreamHeader()
  42. {
  43. m_adler32.Restart();
  44. byte cmf;
  45. byte flags;
  46. if (!m_inQueue.Get(cmf) || !m_inQueue.Get(flags))
  47. throw HeaderErr();
  48. if ((cmf*256+flags) % 31 != 0)
  49. throw HeaderErr(); // if you hit this exception, you're probably trying to decompress invalid data
  50. if ((cmf & 0xf) != DEFLATE_METHOD)
  51. throw UnsupportedAlgorithm();
  52. if (flags & FDICT_FLAG)
  53. throw UnsupportedPresetDictionary();
  54. m_log2WindowSize = 8 + (cmf >> 4);
  55. }
  56. void ZlibDecompressor::ProcessDecompressedData(const byte *inString, size_t length)
  57. {
  58. AttachedTransformation()->Put(inString, length);
  59. m_adler32.Update(inString, length);
  60. }
  61. void ZlibDecompressor::ProcessPoststreamTail()
  62. {
  63. FixedSizeSecBlock<byte, 4> adler32;
  64. if (m_inQueue.Get(adler32, 4) != 4)
  65. throw Adler32Err();
  66. if (!m_adler32.Verify(adler32))
  67. throw Adler32Err();
  68. }
  69. NAMESPACE_END