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.

93 lines
2.5 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. #include "secblock.h"
  11. NAMESPACE_BEGIN(CryptoPP)
  12. static const byte DEFLATE_METHOD = 8;
  13. static const byte FDICT_FLAG = (1 << 5);
  14. // *************************************************************
  15. void ZlibCompressor::WritePrestreamHeader()
  16. {
  17. m_adler32.Restart();
  18. assert(((GetLog2WindowSize()-8) << 4) <= 255);
  19. byte cmf = byte(DEFLATE_METHOD | ((GetLog2WindowSize()-8) << 4));
  20. assert((GetCompressionLevel() << 6) <= 255);
  21. byte flags = byte(GetCompressionLevel() << 6);
  22. AttachedTransformation()->PutWord16(RoundUpToMultipleOf(word16(cmf*256+flags), word16(31)));
  23. }
  24. void ZlibCompressor::ProcessUncompressedData(const byte *inString, size_t length)
  25. {
  26. m_adler32.Update(inString, length);
  27. }
  28. void ZlibCompressor::WritePoststreamTail()
  29. {
  30. FixedSizeSecBlock<byte, 4> adler32;
  31. m_adler32.Final(adler32);
  32. AttachedTransformation()->Put(adler32, 4);
  33. }
  34. unsigned int ZlibCompressor::GetCompressionLevel() const
  35. {
  36. static const unsigned int deflateToCompressionLevel[] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3};
  37. return deflateToCompressionLevel[GetDeflateLevel()];
  38. }
  39. // *************************************************************
  40. ZlibDecompressor::ZlibDecompressor(BufferedTransformation *attachment, bool repeat, int propagation)
  41. : Inflator(attachment, repeat, propagation), m_log2WindowSize(0)
  42. {
  43. }
  44. void ZlibDecompressor::ProcessPrestreamHeader()
  45. {
  46. m_adler32.Restart();
  47. byte cmf;
  48. byte flags;
  49. if (!m_inQueue.Get(cmf) || !m_inQueue.Get(flags))
  50. throw HeaderErr();
  51. if ((cmf*256+flags) % 31 != 0)
  52. throw HeaderErr(); // if you hit this exception, you're probably trying to decompress invalid data
  53. if ((cmf & 0xf) != DEFLATE_METHOD)
  54. throw UnsupportedAlgorithm();
  55. if (flags & FDICT_FLAG)
  56. throw UnsupportedPresetDictionary();
  57. m_log2WindowSize = 8 + (cmf >> 4);
  58. }
  59. void ZlibDecompressor::ProcessDecompressedData(const byte *inString, size_t length)
  60. {
  61. AttachedTransformation()->Put(inString, length);
  62. m_adler32.Update(inString, length);
  63. }
  64. void ZlibDecompressor::ProcessPoststreamTail()
  65. {
  66. FixedSizeSecBlock<byte, 4> adler32;
  67. if (m_inQueue.Get(adler32, 4) != 4)
  68. throw Adler32Err();
  69. if (!m_adler32.Verify(adler32))
  70. throw Adler32Err();
  71. }
  72. NAMESPACE_END