Crypto++  5.6.3
Free C++ class library of cryptographic schemes
zdeflate.h
Go to the documentation of this file.
1 // zdeflate.h - written and placed in the public domain by Wei Dai
2 
3 //! \file zdeflate.h
4 //! \brief DEFLATE compression and decompression (RFC 1951)
5 
6 #ifndef CRYPTOPP_ZDEFLATE_H
7 #define CRYPTOPP_ZDEFLATE_H
8 
9 #include "cryptlib.h"
10 #include "filters.h"
11 #include "misc.h"
12 
13 NAMESPACE_BEGIN(CryptoPP)
14 
15 //! \brief Encoding table writer
16 class LowFirstBitWriter : public Filter
17 {
18 public:
19  //! \brief Construct a LowFirstBitWriter
20  //! \param attachment an attached transformation
22 
23  void PutBits(unsigned long value, unsigned int length);
24  void FlushBitBuffer();
25  void ClearBitBuffer();
26 
27  void StartCounting();
28  unsigned long FinishCounting();
29 
30 protected:
31  bool m_counting;
32  unsigned long m_bitCount;
33  unsigned long m_buffer;
34  unsigned int m_bitsBuffered, m_bytesBuffered;
35  FixedSizeSecBlock<byte, 256> m_outputBuffer;
36 };
37 
38 //! \class HuffmanEncoder
40 {
41 public:
42  typedef unsigned int code_t;
43  typedef unsigned int value_t;
44 
45  //! \brief Construct a HuffmanEncoder
47 
48  //! \brief Construct a HuffmanEncoder
49  //! \param codeBits a table of code bits
50  //! \param nCodes the number of codes in the table
51  HuffmanEncoder(const unsigned int *codeBits, unsigned int nCodes);
52 
53  //! \brief Initialize or reinitialize this object
54  //! \param codeBits a table of code bits
55  //! \param nCodes the number of codes in the table
56  void Initialize(const unsigned int *codeBits, unsigned int nCodes);
57 
58  static void GenerateCodeLengths(unsigned int *codeBits, unsigned int maxCodeBits, const unsigned int *codeCounts, size_t nCodes);
59 
60  void Encode(LowFirstBitWriter &writer, value_t value) const;
61 
62  struct Code
63  {
64  unsigned int code;
65  unsigned int len;
66  };
67 
68  SecBlock<Code> m_valueToCode;
69 };
70 
71 //! \class Deflator
72 //! \brief DEFLATE compressor (RFC 1951)
74 {
75 public:
76  //! \brief Deflate level as enumerated values.
77  enum {
78  //! \brief Minimum deflation level, fastest speed (0)
80  //! \brief Default deflation level, compromise between speed (6)
82  //! \brief Minimum deflation level, slowest speed (9)
84 
85  //! \brief Windows size as enumerated values.
86  enum {
87  //! \brief Minimum window size, smallest table (9)
89  //! \brief Default window size (15)
91  //! \brief Maximum window size, largest table (15)
93 
94  //! \brief Construct a Deflator compressor
95  //! \param attachment an attached transformation
96  //! \param deflateLevel the deflate level
97  //! \param log2WindowSize the window size
98  //! \param detectUncompressible flag to detect if data is compressible
99  //! \details detectUncompressible makes it faster to process uncompressible files, but
100  //! if a file has both compressible and uncompressible parts, it may fail to compress
101  //! some of the compressible parts.
102  Deflator(BufferedTransformation *attachment=NULL, int deflateLevel=DEFAULT_DEFLATE_LEVEL, int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE, bool detectUncompressible=true);
103  //! \brief Construct a Deflator compressor
104  //! \param parameters a set of NameValuePairs to initialize this object
105  //! \param attachment an attached transformation
106  //! \details Possible parameter names: Log2WindowSize, DeflateLevel, DetectUncompressible
107  Deflator(const NameValuePairs &parameters, BufferedTransformation *attachment=NULL);
108 
109  //! \brief Sets the deflation level
110  //! \param deflateLevel the level of deflation
111  //! \details SetDeflateLevel can be used to set the deflate level in the middle of compression
112  void SetDeflateLevel(int deflateLevel);
113 
114  //! \brief Retrieves the deflation level
115  //! \returns the level of deflation
116  int GetDeflateLevel() const {return m_deflateLevel;}
117 
118  //! \brief Retrieves the window size
119  //! \returns the windows size
120  int GetLog2WindowSize() const {return m_log2WindowSize;}
121 
122  void IsolatedInitialize(const NameValuePairs &parameters);
123  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
124  bool IsolatedFlush(bool hardFlush, bool blocking);
125 
126 protected:
127  virtual void WritePrestreamHeader() {}
128  virtual void ProcessUncompressedData(const byte *string, size_t length)
129  {CRYPTOPP_UNUSED(string), CRYPTOPP_UNUSED(length);}
130  virtual void WritePoststreamTail() {}
131 
132  enum {STORED = 0, STATIC = 1, DYNAMIC = 2};
133  enum {MIN_MATCH = 3, MAX_MATCH = 258};
134 
135  void InitializeStaticEncoders();
136  void Reset(bool forceReset = false);
137  unsigned int FillWindow(const byte *str, size_t length);
138  unsigned int ComputeHash(const byte *str) const;
139  unsigned int LongestMatch(unsigned int &bestMatch) const;
140  void InsertString(unsigned int start);
141  void ProcessBuffer();
142 
143  void LiteralByte(byte b);
144  void MatchFound(unsigned int distance, unsigned int length);
145  void EncodeBlock(bool eof, unsigned int blockType);
146  void EndBlock(bool eof);
147 
148  struct EncodedMatch
149  {
150  unsigned literalCode : 9;
151  unsigned literalExtra : 5;
152  unsigned distanceCode : 5;
153  unsigned distanceExtra : 13;
154  };
155 
156  int m_deflateLevel, m_log2WindowSize, m_compressibleDeflateLevel;
157  unsigned int m_detectSkip, m_detectCount;
158  unsigned int DSIZE, DMASK, HSIZE, HMASK, GOOD_MATCH, MAX_LAZYLENGTH, MAX_CHAIN_LENGTH;
159  bool m_headerWritten, m_matchAvailable;
160  unsigned int m_dictionaryEnd, m_stringStart, m_lookahead, m_minLookahead, m_previousMatch, m_previousLength;
161  HuffmanEncoder m_staticLiteralEncoder, m_staticDistanceEncoder, m_dynamicLiteralEncoder, m_dynamicDistanceEncoder;
162  SecByteBlock m_byteBuffer;
163  SecBlock<word16> m_head, m_prev;
164  FixedSizeSecBlock<unsigned int, 286> m_literalCounts;
165  FixedSizeSecBlock<unsigned int, 30> m_distanceCounts;
166  SecBlock<EncodedMatch> m_matchBuffer;
167  unsigned int m_matchBufferEnd, m_blockStart, m_blockLength;
168 };
169 
170 NAMESPACE_END
171 
172 #endif
Utility functions for the Crypto++ library.
int GetLog2WindowSize() const
Retrieves the window size.
Definition: zdeflate.h:120
Secure memory block with allocator and cleanup.
Definition: secblock.h:440
Abstract base classes that provide a uniform interface to this library.
Encoding table writer.
Definition: zdeflate.h:16
DEFLATE compressor (RFC 1951)
Definition: zdeflate.h:73
SecBlock typedef.
Definition: secblock.h:734
bool IsolatedFlush(bool hardFlush, bool blocking)
Flushes data buffered by this object, without signal propagation.
Definition: zdeflate.cpp:536
Interface for buffered transformations.
Definition: cryptlib.h:1352
int GetDeflateLevel() const
Retrieves the deflation level.
Definition: zdeflate.h:116
Minimum deflation level, fastest speed (0)
Definition: zdeflate.h:79
Maximum window size, largest table (15)
Definition: zdeflate.h:92
HuffmanEncoder()
Construct a HuffmanEncoder.
Definition: zdeflate.h:46
void SetDeflateLevel(int deflateLevel)
Sets the deflation level.
Definition: zdeflate.cpp:301
Implementation of BufferedTransformation's attachment interface.
Default deflation level, compromise between speed (6)
Definition: zdeflate.h:81
Deflator(BufferedTransformation *attachment=NULL, int deflateLevel=DEFAULT_DEFLATE_LEVEL, int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE, bool detectUncompressible=true)
Construct a Deflator compressor.
Definition: zdeflate.cpp:222
void Initialize(const unsigned int *codeBits, unsigned int nCodes)
Initialize or reinitialize this object.
Definition: zdeflate.cpp:184
Implementation of BufferedTransformation's attachment interface.
Definition: filters.h:36
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: zdeflate.cpp:506
Minimum deflation level, slowest speed (9)
Definition: zdeflate.h:83
Crypto++ library namespace.
Minimum window size, smallest table (9)
Definition: zdeflate.h:88
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: zdeflate.cpp:250
Default window size (15)
Definition: zdeflate.h:90
Interface for retrieving values given their names.
Definition: cryptlib.h:277