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