Crypto++  5.6.4
Free C++ class library of cryptographic schemes
gzip.h
Go to the documentation of this file.
1 // gzip.h - 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=NULL, 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  //! \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=NULL)
36  : Deflator(parameters, attachment), m_totalLen(0) {}
37 
38 protected:
39  enum {MAGIC1=0x1f, MAGIC2=0x8b, // flags for the header
40  DEFLATED=8, FAST=4, SLOW=2};
41 
42  void WritePrestreamHeader();
43  void ProcessUncompressedData(const byte *string, size_t length);
44  void WritePoststreamTail();
45 
46  word32 m_totalLen;
47  CRC32 m_crc;
48 };
49 
50 //! \class Gunzip
51 //! \brief GZIP Decompression (RFC 1952)
52 class Gunzip : public Inflator
53 {
54 public:
55  typedef Inflator::Err Err;
56 
57  //! \class HeaderErr
58  //! \brief Exception thrown when a header decoding error occurs
59  class HeaderErr : public Err {public: HeaderErr() : Err(INVALID_DATA_FORMAT, "Gunzip: header decoding error") {}};
60  //! \class TailErr
61  //! \brief Exception thrown when the tail is too short
62  class TailErr : public Err {public: TailErr() : Err(INVALID_DATA_FORMAT, "Gunzip: tail too short") {}};
63  //! \class CrcErr
64  //! \brief Exception thrown when a CRC error occurs
65  class CrcErr : public Err {public: CrcErr() : Err(DATA_INTEGRITY_CHECK_FAILED, "Gunzip: CRC check error") {}};
66  //! \class LengthErr
67  //! \brief Exception thrown when a length error occurs
68  class LengthErr : public Err {public: LengthErr() : Err(DATA_INTEGRITY_CHECK_FAILED, "Gunzip: length check error") {}};
69 
70  //! \brief Construct a Gunzip decompressor
71  //! \param attachment an attached transformation
72  //! \param repeat decompress multiple compressed streams in series
73  //! \param autoSignalPropagation 0 to turn off MessageEnd signal
74  Gunzip(BufferedTransformation *attachment = NULL, bool repeat = false, int autoSignalPropagation = -1);
75 
76 protected:
77  enum {
78  //! \brief First header magic value
79  MAGIC1=0x1f,
80  //! \brief Second header magic value
81  MAGIC2=0x8b,
82  //! \brief Deflated flag
83  DEFLATED=8
84  };
85 
86  enum FLAG_MASKS {
87  CONTINUED=2, EXTRA_FIELDS=4, FILENAME=8, COMMENTS=16, ENCRYPTED=32};
88 
89  unsigned int MaxPrestreamHeaderSize() const {return 1024;}
90  void ProcessPrestreamHeader();
91  void ProcessDecompressedData(const byte *string, size_t length);
92  unsigned int MaxPoststreamTailSize() const {return 8;}
93  void ProcessPoststreamTail();
94 
95  word32 m_length;
96  CRC32 m_crc;
97 };
98 
99 NAMESPACE_END
100 
101 #endif
GZIP Decompression (RFC 1952)
Definition: gzip.h:52
Abstract base classes that provide a uniform interface to this library.
Exception thrown when a CRC error occurs.
Definition: gzip.h:65
DEFLATE compressor (RFC 1951)
Definition: zdeflate.h:73
Exception thrown when a length error occurs.
Definition: gzip.h:68
Classes for CRC-32 and CRC-32C checksum algorithm.
Interface for buffered transformations.
Definition: cryptlib.h:1359
DEFLATE compression and decompression (RFC 1951)
Data integerity check, such as CRC or MAC, failed.
Definition: cryptlib.h:157
Gunzip(BufferedTransformation *attachment=NULL, bool repeat=false, int autoSignalPropagation=-1)
Construct a Gunzip decompressor.
Definition: gzip.cpp:39
Input data was received that did not conform to expected format.
Definition: cryptlib.h:159
Exception thrown when the tail is too short.
Definition: gzip.h:62
Exception thrown when a header decoding error occurs.
Definition: gzip.h:59
Gzip(const NameValuePairs &parameters, BufferedTransformation *attachment=NULL)
Construct a Gzip compressor.
Definition: gzip.h:35
DEFLATE (RFC 1951) decompressor.
Definition: zinflate.h:88
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:282