Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:03

0001 // gzip.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file gzip.h

0004 /// \brief GZIP compression and decompression (RFC 1952)

0005 
0006 #ifndef CRYPTOPP_GZIP_H
0007 #define CRYPTOPP_GZIP_H
0008 
0009 #include "cryptlib.h"
0010 #include "zdeflate.h"
0011 #include "zinflate.h"
0012 #include "crc.h"
0013 
0014 NAMESPACE_BEGIN(CryptoPP)
0015 
0016 /// \brief GZIP Compression (RFC 1952)

0017 class Gzip : public Deflator
0018 {
0019 public:
0020     /// \brief Construct a Gzip compressor

0021     /// \param attachment an attached transformation

0022     /// \param deflateLevel the deflate level

0023     /// \param log2WindowSize the window size

0024     /// \param detectUncompressible flag to detect if data is compressible

0025     /// \details detectUncompressible makes it faster to process uncompressible files, but

0026     ///   if a file has both compressible and uncompressible parts, it may fail to compress

0027     ///   some of the compressible parts.

0028     Gzip(BufferedTransformation *attachment=NULLPTR, unsigned int deflateLevel=DEFAULT_DEFLATE_LEVEL, unsigned int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE, bool detectUncompressible=true)
0029         : Deflator(attachment, deflateLevel, log2WindowSize, detectUncompressible), m_totalLen(0), m_filetime(0) { }
0030 
0031     /// \brief Construct a Gzip compressor

0032     /// \param parameters a set of NameValuePairs to initialize this object

0033     /// \param attachment an attached transformation

0034     /// \details Possible parameter names: Log2WindowSize, DeflateLevel, DetectUncompressible

0035     Gzip(const NameValuePairs &parameters, BufferedTransformation *attachment=NULLPTR)
0036         : Deflator(parameters, attachment), m_totalLen(0), m_filetime(0)
0037     {
0038         IsolatedInitialize(parameters);
0039     }
0040 
0041     /// \param filetime the filetime to set in the header. The application is responsible for setting it.

0042     void SetFiletime(word32 filetime) { m_filetime = filetime; }
0043 
0044     /// \param filename the original filename to set in the header. The application is responsible for setting it.

0045     ///        RFC 1952 requires a ISO/IEC 8859-1 encoding.

0046     /// \param throwOnEncodingError if throwOnEncodingError is true, then the filename is checked to ensure it is

0047     ///        ISO/IEC 8859-1 encoded. If the filename does not adhere to ISO/IEC 8859-1, then a InvalidDataFormat

0048     ///        is thrown. If throwOnEncodingError is false then the filename is not checked.

0049     void SetFilename(const std::string& filename, bool throwOnEncodingError = false);
0050 
0051     /// \param comment the comment to set in the header. The application is responsible for setting it.

0052     ///        RFC 1952 requires a ISO/IEC 8859-1 encoding.

0053     /// \param throwOnEncodingError if throwOnEncodingError is true, then the comment is checked to ensure it is

0054     ///        ISO/IEC 8859-1 encoded. If the comment does not adhere to ISO/IEC 8859-1, then a InvalidDataFormat

0055     ///        is thrown. If throwOnEncodingError is false then the comment is not checked.

0056     void SetComment(const std::string& comment, bool throwOnEncodingError = false);
0057 
0058     void IsolatedInitialize(const NameValuePairs &parameters);
0059 
0060 protected:
0061     enum {MAGIC1=0x1f, MAGIC2=0x8b,   // flags for the header

0062           DEFLATED=8, FAST=4, SLOW=2};
0063 
0064     enum FLAG_MASKS {
0065         FILENAME=8, COMMENTS=16};
0066 
0067     void WritePrestreamHeader();
0068     void ProcessUncompressedData(const byte *string, size_t length);
0069     void WritePoststreamTail();
0070 
0071     word32 m_totalLen;
0072     CRC32 m_crc;
0073 
0074     word32 m_filetime;
0075     std::string m_filename;
0076     std::string m_comment;
0077 };
0078 
0079 /// \brief GZIP Decompression (RFC 1952)

0080 class Gunzip : public Inflator
0081 {
0082 public:
0083     typedef Inflator::Err Err;
0084 
0085     /// \brief Exception thrown when a header decoding error occurs

0086     class HeaderErr : public Err {public: HeaderErr() : Err(INVALID_DATA_FORMAT, "Gunzip: header decoding error") {}};
0087     /// \brief Exception thrown when the tail is too short

0088     class TailErr : public Err {public: TailErr() : Err(INVALID_DATA_FORMAT, "Gunzip: tail too short") {}};
0089     /// \brief Exception thrown when a CRC error occurs

0090     class CrcErr : public Err {public: CrcErr() : Err(DATA_INTEGRITY_CHECK_FAILED, "Gunzip: CRC check error") {}};
0091     /// \brief Exception thrown when a length error occurs

0092     class LengthErr : public Err {public: LengthErr() : Err(DATA_INTEGRITY_CHECK_FAILED, "Gunzip: length check error") {}};
0093 
0094     /// \brief Construct a Gunzip decompressor

0095     /// \param attachment an attached transformation

0096     /// \param repeat decompress multiple compressed streams in series

0097     /// \param autoSignalPropagation 0 to turn off MessageEnd signal

0098     Gunzip(BufferedTransformation *attachment = NULLPTR, bool repeat = false, int autoSignalPropagation = -1);
0099 
0100     /// \return the filetime of the stream as set in the header. The application is responsible for setting it on the decompressed file.

0101     word32 GetFiletime() const { return m_filetime; }
0102 
0103     /// \return the filename of the stream as set in the header. The application is responsible for setting it on the decompressed file.

0104     /// \param throwOnEncodingError if throwOnEncodingError is true, then the filename is checked to ensure it is

0105     ///        ISO/IEC 8859-1 encoded. If the filename does not adhere to ISO/IEC 8859-1, then a InvalidDataFormat is thrown.

0106     ///        If throwOnEncodingError is false then the filename is not checked.

0107     const std::string& GetFilename(bool throwOnEncodingError = false) const;
0108 
0109     /// \return the comment of the stream as set in the header.

0110     /// \param throwOnEncodingError if throwOnEncodingError is true, then the comment is checked to ensure it is

0111     ///        ISO/IEC 8859-1 encoded. If the comment does not adhere to ISO/IEC 8859-1, then a InvalidDataFormat is thrown.

0112     ///        If throwOnEncodingError is false then the comment is not checked.

0113     const std::string& GetComment(bool throwOnEncodingError = false) const;
0114 
0115 protected:
0116     enum {
0117         /// \brief First header magic value

0118         MAGIC1=0x1f,
0119         /// \brief Second header magic value

0120         MAGIC2=0x8b,
0121         /// \brief Deflated flag

0122         DEFLATED=8
0123     };
0124 
0125     enum FLAG_MASKS {
0126         CONTINUED=2, EXTRA_FIELDS=4, FILENAME=8, COMMENTS=16, ENCRYPTED=32};
0127 
0128     unsigned int MaxPrestreamHeaderSize() const {return 1024;}
0129     void ProcessPrestreamHeader();
0130     void ProcessDecompressedData(const byte *string, size_t length);
0131     unsigned int MaxPoststreamTailSize() const {return 8;}
0132     void ProcessPoststreamTail();
0133 
0134     word32 m_length;
0135     CRC32 m_crc;
0136 
0137     word32 m_filetime;
0138     std::string m_filename;
0139     std::string m_comment;
0140 };
0141 
0142 NAMESPACE_END
0143 
0144 #endif