zlib.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // zlib.h - originally written and placed in the public domain by Wei Dai
  2. /// \file zlib.h
  3. /// \brief ZLIB compression and decompression (RFC 1950)
  4. #ifndef CRYPTOPP_ZLIB_H
  5. #define CRYPTOPP_ZLIB_H
  6. #include "cryptlib.h"
  7. #include "adler32.h"
  8. #include "zdeflate.h"
  9. #include "zinflate.h"
  10. NAMESPACE_BEGIN(CryptoPP)
  11. /// ZLIB Compressor (RFC 1950)
  12. class ZlibCompressor : public Deflator
  13. {
  14. public:
  15. ZlibCompressor(BufferedTransformation *attachment=NULLPTR, unsigned int deflateLevel=DEFAULT_DEFLATE_LEVEL, unsigned int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE, bool detectUncompressible=true)
  16. : Deflator(attachment, deflateLevel, log2WindowSize, detectUncompressible) {}
  17. ZlibCompressor(const NameValuePairs &parameters, BufferedTransformation *attachment=NULLPTR)
  18. : Deflator(parameters, attachment) {}
  19. unsigned int GetCompressionLevel() const;
  20. protected:
  21. void WritePrestreamHeader();
  22. void ProcessUncompressedData(const byte *string, size_t length);
  23. void WritePoststreamTail();
  24. Adler32 m_adler32;
  25. };
  26. /// ZLIB Decompressor (RFC 1950)
  27. class ZlibDecompressor : public Inflator
  28. {
  29. public:
  30. typedef Inflator::Err Err;
  31. class HeaderErr : public Err {public: HeaderErr() : Err(INVALID_DATA_FORMAT, "ZlibDecompressor: header decoding error") {}};
  32. class Adler32Err : public Err {public: Adler32Err() : Err(DATA_INTEGRITY_CHECK_FAILED, "ZlibDecompressor: ADLER32 check error") {}};
  33. class UnsupportedAlgorithm : public Err {public: UnsupportedAlgorithm() : Err(INVALID_DATA_FORMAT, "ZlibDecompressor: unsupported algorithm") {}};
  34. class UnsupportedPresetDictionary : public Err {public: UnsupportedPresetDictionary() : Err(INVALID_DATA_FORMAT, "ZlibDecompressor: unsupported preset dictionary") {}};
  35. /// \brief Construct a ZlibDecompressor
  36. /// \param attachment a \ BufferedTransformation to attach to this object
  37. /// \param repeat decompress multiple compressed streams in series
  38. /// \param autoSignalPropagation 0 to turn off MessageEnd signal
  39. ZlibDecompressor(BufferedTransformation *attachment = NULLPTR, bool repeat = false, int autoSignalPropagation = -1);
  40. unsigned int GetLog2WindowSize() const {return m_log2WindowSize;}
  41. private:
  42. unsigned int MaxPrestreamHeaderSize() const {return 2;}
  43. void ProcessPrestreamHeader();
  44. void ProcessDecompressedData(const byte *string, size_t length);
  45. unsigned int MaxPoststreamTailSize() const {return 4;}
  46. void ProcessPoststreamTail();
  47. unsigned int m_log2WindowSize;
  48. Adler32 m_adler32;
  49. };
  50. NAMESPACE_END
  51. #endif