Crypto++  5.6.5
Free C++ class library of cryptographic schemes
gzip.h
Go to the documentation of this file.
1 // gzip.h - originally written and placed in the public domain by Wei Dai
2 
3 //! \file gzip.h
4 //! \brief GZIP compression and decompression (RFC 1952)
5 
6 #ifndef CRYPTOPP_GZIP_H
7 #define CRYPTOPP_GZIP_H
8 
9 #include "cryptlib.h"
10 #include "zdeflate.h"
11 #include "zinflate.h"
12 #include "crc.h"
13 
14 NAMESPACE_BEGIN(CryptoPP)
15 
16 //! \class Gzip
17 //! \brief GZIP Compression (RFC 1952)
18 class Gzip : public Deflator
19 {
20 public:
21  //! \brief Construct a Gzip compressor
22  //! \param attachment an attached transformation
23  //! \param deflateLevel the deflate level
24  //! \param log2WindowSize the window size
25  //! \param detectUncompressible flag to detect if data is compressible
26  //! \details detectUncompressible makes it faster to process uncompressible files, but
27  //! if a file has both compressible and uncompressible parts, it may fail to compress
28  //! some of the compressible parts.
29  Gzip(BufferedTransformation *attachment=NULLPTR, unsigned int deflateLevel=DEFAULT_DEFLATE_LEVEL, unsigned int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE, bool detectUncompressible=true)
30  : Deflator(attachment, deflateLevel, log2WindowSize, detectUncompressible), m_totalLen(0) { }
31 
32  //! \brief Construct a Gzip compressor
33  //! \param parameters a set of NameValuePairs to initialize this object
34  //! \param attachment an attached transformation
35  //! \details Possible parameter names: Log2WindowSize, DeflateLevel, DetectUncompressible
36  Gzip(const NameValuePairs &parameters, BufferedTransformation *attachment=NULLPTR)
37  : Deflator(parameters, attachment), m_totalLen(0)
38  {
39  IsolatedInitialize(parameters);
40  }
41 
42  //! \param filetime the filetime to set in the header. The application is responsible for setting it.
43  void SetFiletime(word32 filetime) { m_filetime = filetime; }
44 
45  //! \param filename the original filename to set in the header. The application is responsible for setting it.
46  //! RFC 1952 requires a ISO/IEC 8859-1 encoding.
47  //! \param throwOnEncodingError if throwOnEncodingError is true, then the filename is checked to ensure it is
48  //! ISO/IEC 8859-1 encoded. If the filename does not adhere to ISO/IEC 8859-1, then a InvalidDataFormat
49  //! is thrown. If throwOnEncodingError is false then the filename is not checked.
50  void SetFilename(const std::string& filename, bool throwOnEncodingError = false);
51 
52  //! \param comment the comment to set in the header. The application is responsible for setting it.
53  //! RFC 1952 requires a ISO/IEC 8859-1 encoding.
54  //! \param throwOnEncodingError if throwOnEncodingError is true, then the comment is checked to ensure it is
55  //! ISO/IEC 8859-1 encoded. If the comment does not adhere to ISO/IEC 8859-1, then a InvalidDataFormat
56  //! is thrown. If throwOnEncodingError is false then the comment is not checked.
57  void SetComment(const std::string& comment, bool throwOnEncodingError = false);
58 
59  void IsolatedInitialize(const NameValuePairs &parameters);
60 
61 protected:
62  enum {MAGIC1=0x1f, MAGIC2=0x8b, // flags for the header
63  DEFLATED=8, FAST=4, SLOW=2};
64 
65  enum FLAG_MASKS {
66  FILENAME=8, COMMENTS=16};
67 
68  void WritePrestreamHeader();
69  void ProcessUncompressedData(const byte *string, size_t length);
70  void WritePoststreamTail();
71 
72  word32 m_totalLen;
73  CRC32 m_crc;
74 
75  word32 m_filetime;
76  std::string m_filename;
77  std::string m_comment;
78 };
79 
80 //! \class Gunzip
81 //! \brief GZIP Decompression (RFC 1952)
82 class Gunzip : public Inflator
83 {
84 public:
85  typedef Inflator::Err Err;
86 
87  //! \class HeaderErr
88  //! \brief Exception thrown when a header decoding error occurs
89  class HeaderErr : public Err {public: HeaderErr() : Err(INVALID_DATA_FORMAT, "Gunzip: header decoding error") {}};
90  //! \class TailErr
91  //! \brief Exception thrown when the tail is too short
92  class TailErr : public Err {public: TailErr() : Err(INVALID_DATA_FORMAT, "Gunzip: tail too short") {}};
93  //! \class CrcErr
94  //! \brief Exception thrown when a CRC error occurs
95  class CrcErr : public Err {public: CrcErr() : Err(DATA_INTEGRITY_CHECK_FAILED, "Gunzip: CRC check error") {}};
96  //! \class LengthErr
97  //! \brief Exception thrown when a length error occurs
98  class LengthErr : public Err {public: LengthErr() : Err(DATA_INTEGRITY_CHECK_FAILED, "Gunzip: length check error") {}};
99 
100  //! \brief Construct a Gunzip decompressor
101  //! \param attachment an attached transformation
102  //! \param repeat decompress multiple compressed streams in series
103  //! \param autoSignalPropagation 0 to turn off MessageEnd signal
104  Gunzip(BufferedTransformation *attachment = NULLPTR, bool repeat = false, int autoSignalPropagation = -1);
105 
106  //! \return the filetime of the stream as set in the header. The application is responsible for setting it on the decompressed file.
107  word32 GetFiletime() const { return m_filetime; }
108 
109  //! \return the filename of the stream as set in the header. The application is responsible for setting it on the decompressed file.
110  //! \param throwOnEncodingError if throwOnEncodingError is true, then the filename is checked to ensure it is
111  //! ISO/IEC 8859-1 encoded. If the filename does not adhere to ISO/IEC 8859-1, then a InvalidDataFormat is thrown.
112  //! If throwOnEncodingError is false then the filename is not checked.
113  const std::string& GetFilename(bool throwOnEncodingError = false) const;
114 
115  //! \return the comment of the stream as set in the header.
116  //! \param throwOnEncodingError if throwOnEncodingError is true, then the comment is checked to ensure it is
117  //! ISO/IEC 8859-1 encoded. If the comment does not adhere to ISO/IEC 8859-1, then a InvalidDataFormat is thrown.
118  //! If throwOnEncodingError is false then the comment is not checked.
119  const std::string& GetComment(bool throwOnEncodingError = false) const;
120 
121 protected:
122  enum {
123  //! \brief First header magic value
124  MAGIC1=0x1f,
125  //! \brief Second header magic value
126  MAGIC2=0x8b,
127  //! \brief Deflated flag
128  DEFLATED=8
129  };
130 
131  enum FLAG_MASKS {
132  CONTINUED=2, EXTRA_FIELDS=4, FILENAME=8, COMMENTS=16, ENCRYPTED=32};
133 
134  unsigned int MaxPrestreamHeaderSize() const {return 1024;}
135  void ProcessPrestreamHeader();
136  void ProcessDecompressedData(const byte *string, size_t length);
137  unsigned int MaxPoststreamTailSize() const {return 8;}
138  void ProcessPoststreamTail();
139 
140  word32 m_length;
141  CRC32 m_crc;
142 
143  word32 m_filetime;
144  std::string m_filename;
145  std::string m_comment;
146 };
147 
148 NAMESPACE_END
149 
150 #endif
void SetFiletime(word32 filetime)
Definition: gzip.h:43
GZIP Decompression (RFC 1952)
Definition: gzip.h:82
Abstract base classes that provide a uniform interface to this library.
Exception thrown when a CRC error occurs.
Definition: gzip.h:95
DEFLATE compressor (RFC 1951)
Definition: zdeflate.h:78
Exception thrown when a length error occurs.
Definition: gzip.h:98
Classes for CRC-32 and CRC-32C checksum algorithm.
Interface for buffered transformations.
Definition: cryptlib.h:1343
DEFLATE compression and decompression (RFC 1951)
Data integerity check, such as CRC or MAC, failed.
Definition: cryptlib.h:163
Gunzip(BufferedTransformation *attachment=NULL, bool repeat=false, int autoSignalPropagation=-1)
Construct a Gunzip decompressor.
Definition: gzip.cpp:102
Input data was received that did not conform to expected format.
Definition: cryptlib.h:165
word32 GetFiletime() const
Definition: gzip.h:107
Exception thrown when the tail is too short.
Definition: gzip.h:92
Exception thrown when a header decoding error occurs.
Definition: gzip.h:89
Gzip(const NameValuePairs &parameters, BufferedTransformation *attachment=NULL)
Construct a Gzip compressor.
Definition: gzip.h:36
const std::string & GetComment(bool throwOnEncodingError=false) const
Definition: gzip.cpp:177
const std::string & GetFilename(bool throwOnEncodingError=false) const
Definition: gzip.cpp:191
DEFLATE decompressor (RFC 1951)
Definition: zinflate.h:92
Gzip(BufferedTransformation *attachment=NULL, unsigned int deflateLevel=DEFAULT_DEFLATE_LEVEL, unsigned int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE, bool detectUncompressible=true)
Construct a Gzip compressor.
Definition: gzip.h:29
Crypto++ library namespace.
GZIP Compression (RFC 1952)
Definition: gzip.h:18
CRC-32 Checksum Calculation.
Definition: crc.h:26
Interface for retrieving values given their names.
Definition: cryptlib.h:285