gzip.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // gzip.h - originally written and placed in the public domain by Wei Dai
  2. /// \file gzip.h
  3. /// \brief GZIP compression and decompression (RFC 1952)
  4. #ifndef CRYPTOPP_GZIP_H
  5. #define CRYPTOPP_GZIP_H
  6. #include "cryptlib.h"
  7. #include "zdeflate.h"
  8. #include "zinflate.h"
  9. #include "crc.h"
  10. NAMESPACE_BEGIN(CryptoPP)
  11. /// \brief GZIP Compression (RFC 1952)
  12. class Gzip : public Deflator
  13. {
  14. public:
  15. /// \brief Construct a Gzip compressor
  16. /// \param attachment an attached transformation
  17. /// \param deflateLevel the deflate level
  18. /// \param log2WindowSize the window size
  19. /// \param detectUncompressible flag to detect if data is compressible
  20. /// \details detectUncompressible makes it faster to process uncompressible files, but
  21. /// if a file has both compressible and uncompressible parts, it may fail to compress
  22. /// some of the compressible parts.
  23. Gzip(BufferedTransformation *attachment=NULLPTR, unsigned int deflateLevel=DEFAULT_DEFLATE_LEVEL, unsigned int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE, bool detectUncompressible=true)
  24. : Deflator(attachment, deflateLevel, log2WindowSize, detectUncompressible), m_totalLen(0), m_filetime(0) { }
  25. /// \brief Construct a Gzip compressor
  26. /// \param parameters a set of NameValuePairs to initialize this object
  27. /// \param attachment an attached transformation
  28. /// \details Possible parameter names: Log2WindowSize, DeflateLevel, DetectUncompressible
  29. Gzip(const NameValuePairs &parameters, BufferedTransformation *attachment=NULLPTR)
  30. : Deflator(parameters, attachment), m_totalLen(0), m_filetime(0)
  31. {
  32. IsolatedInitialize(parameters);
  33. }
  34. /// \param filetime the filetime to set in the header. The application is responsible for setting it.
  35. void SetFiletime(word32 filetime) { m_filetime = filetime; }
  36. /// \param filename the original filename to set in the header. The application is responsible for setting it.
  37. /// RFC 1952 requires a ISO/IEC 8859-1 encoding.
  38. /// \param throwOnEncodingError if throwOnEncodingError is true, then the filename is checked to ensure it is
  39. /// ISO/IEC 8859-1 encoded. If the filename does not adhere to ISO/IEC 8859-1, then a InvalidDataFormat
  40. /// is thrown. If throwOnEncodingError is false then the filename is not checked.
  41. void SetFilename(const std::string& filename, bool throwOnEncodingError = false);
  42. /// \param comment the comment to set in the header. The application is responsible for setting it.
  43. /// RFC 1952 requires a ISO/IEC 8859-1 encoding.
  44. /// \param throwOnEncodingError if throwOnEncodingError is true, then the comment is checked to ensure it is
  45. /// ISO/IEC 8859-1 encoded. If the comment does not adhere to ISO/IEC 8859-1, then a InvalidDataFormat
  46. /// is thrown. If throwOnEncodingError is false then the comment is not checked.
  47. void SetComment(const std::string& comment, bool throwOnEncodingError = false);
  48. void IsolatedInitialize(const NameValuePairs &parameters);
  49. protected:
  50. enum {MAGIC1=0x1f, MAGIC2=0x8b, // flags for the header
  51. DEFLATED=8, FAST=4, SLOW=2};
  52. enum FLAG_MASKS {
  53. FILENAME=8, COMMENTS=16};
  54. void WritePrestreamHeader();
  55. void ProcessUncompressedData(const byte *string, size_t length);
  56. void WritePoststreamTail();
  57. word32 m_totalLen;
  58. CRC32 m_crc;
  59. word32 m_filetime;
  60. std::string m_filename;
  61. std::string m_comment;
  62. };
  63. /// \brief GZIP Decompression (RFC 1952)
  64. class Gunzip : public Inflator
  65. {
  66. public:
  67. typedef Inflator::Err Err;
  68. /// \brief Exception thrown when a header decoding error occurs
  69. class HeaderErr : public Err {public: HeaderErr() : Err(INVALID_DATA_FORMAT, "Gunzip: header decoding error") {}};
  70. /// \brief Exception thrown when the tail is too short
  71. class TailErr : public Err {public: TailErr() : Err(INVALID_DATA_FORMAT, "Gunzip: tail too short") {}};
  72. /// \brief Exception thrown when a CRC error occurs
  73. class CrcErr : public Err {public: CrcErr() : Err(DATA_INTEGRITY_CHECK_FAILED, "Gunzip: CRC check error") {}};
  74. /// \brief Exception thrown when a length error occurs
  75. class LengthErr : public Err {public: LengthErr() : Err(DATA_INTEGRITY_CHECK_FAILED, "Gunzip: length check error") {}};
  76. /// \brief Construct a Gunzip decompressor
  77. /// \param attachment an attached transformation
  78. /// \param repeat decompress multiple compressed streams in series
  79. /// \param autoSignalPropagation 0 to turn off MessageEnd signal
  80. Gunzip(BufferedTransformation *attachment = NULLPTR, bool repeat = false, int autoSignalPropagation = -1);
  81. /// \return the filetime of the stream as set in the header. The application is responsible for setting it on the decompressed file.
  82. word32 GetFiletime() const { return m_filetime; }
  83. /// \return the filename of the stream as set in the header. The application is responsible for setting it on the decompressed file.
  84. /// \param throwOnEncodingError if throwOnEncodingError is true, then the filename is checked to ensure it is
  85. /// ISO/IEC 8859-1 encoded. If the filename does not adhere to ISO/IEC 8859-1, then a InvalidDataFormat is thrown.
  86. /// If throwOnEncodingError is false then the filename is not checked.
  87. const std::string& GetFilename(bool throwOnEncodingError = false) const;
  88. /// \return the comment of the stream as set in the header.
  89. /// \param throwOnEncodingError if throwOnEncodingError is true, then the comment is checked to ensure it is
  90. /// ISO/IEC 8859-1 encoded. If the comment does not adhere to ISO/IEC 8859-1, then a InvalidDataFormat is thrown.
  91. /// If throwOnEncodingError is false then the comment is not checked.
  92. const std::string& GetComment(bool throwOnEncodingError = false) const;
  93. protected:
  94. enum {
  95. /// \brief First header magic value
  96. MAGIC1=0x1f,
  97. /// \brief Second header magic value
  98. MAGIC2=0x8b,
  99. /// \brief Deflated flag
  100. DEFLATED=8
  101. };
  102. enum FLAG_MASKS {
  103. CONTINUED=2, EXTRA_FIELDS=4, FILENAME=8, COMMENTS=16, ENCRYPTED=32};
  104. unsigned int MaxPrestreamHeaderSize() const {return 1024;}
  105. void ProcessPrestreamHeader();
  106. void ProcessDecompressedData(const byte *string, size_t length);
  107. unsigned int MaxPoststreamTailSize() const {return 8;}
  108. void ProcessPoststreamTail();
  109. word32 m_length;
  110. CRC32 m_crc;
  111. word32 m_filetime;
  112. std::string m_filename;
  113. std::string m_comment;
  114. };
  115. NAMESPACE_END
  116. #endif