Crypto++  5.6.3
Free C++ class library of cryptographic schemes
zdeflate.h
1 #ifndef CRYPTOPP_ZDEFLATE_H
2 #define CRYPTOPP_ZDEFLATE_H
3 
4 #include "cryptlib.h"
5 #include "filters.h"
6 #include "misc.h"
7 
8 NAMESPACE_BEGIN(CryptoPP)
9 
10 //! _
11 class LowFirstBitWriter : public Filter
12 {
13 public:
15  void PutBits(unsigned long value, unsigned int length);
16  void FlushBitBuffer();
17  void ClearBitBuffer();
18 
19  void StartCounting();
20  unsigned long FinishCounting();
21 
22 protected:
23  bool m_counting;
24  unsigned long m_bitCount;
25  unsigned long m_buffer;
26  unsigned int m_bitsBuffered, m_bytesBuffered;
27  FixedSizeSecBlock<byte, 256> m_outputBuffer;
28 };
29 
30 //! \class HuffmanEncoder
32 {
33 public:
34  typedef unsigned int code_t;
35  typedef unsigned int value_t;
36 
37  //! \brief Construct a HuffmanEncoder
39 
40  //! \brief Construct a HuffmanEncoder
41  //! \param codeBits a table of code bits
42  //! \param nCodes the number of codes in the table
43  HuffmanEncoder(const unsigned int *codeBits, unsigned int nCodes);
44 
45  //! \brief Initialize or reinitialize this object
46  //! \param codeBits a table of code bits
47  //! \param nCodes the number of codes in the table
48  void Initialize(const unsigned int *codeBits, unsigned int nCodes);
49 
50  static void GenerateCodeLengths(unsigned int *codeBits, unsigned int maxCodeBits, const unsigned int *codeCounts, size_t nCodes);
51 
52  void Encode(LowFirstBitWriter &writer, value_t value) const;
53 
54  struct Code
55  {
56  unsigned int code;
57  unsigned int len;
58  };
59 
60  SecBlock<Code> m_valueToCode;
61 };
62 
63 //! DEFLATE (RFC 1951) compressor
64 
66 {
67 public:
68  enum {MIN_DEFLATE_LEVEL = 0, DEFAULT_DEFLATE_LEVEL = 6, MAX_DEFLATE_LEVEL = 9};
69  enum {MIN_LOG2_WINDOW_SIZE = 9, DEFAULT_LOG2_WINDOW_SIZE = 15, MAX_LOG2_WINDOW_SIZE = 15};
70  /*! \note detectUncompressible makes it faster to process uncompressible files, but
71  if a file has both compressible and uncompressible parts, it may fail to compress some of the
72  compressible parts. */
73  Deflator(BufferedTransformation *attachment=NULL, int deflateLevel=DEFAULT_DEFLATE_LEVEL, int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE, bool detectUncompressible=true);
74  //! possible parameter names: Log2WindowSize, DeflateLevel, DetectUncompressible
75  Deflator(const NameValuePairs &parameters, BufferedTransformation *attachment=NULL);
76 
77  //! this function can be used to set the deflate level in the middle of compression
78  void SetDeflateLevel(int deflateLevel);
79  int GetDeflateLevel() const {return m_deflateLevel;}
80  int GetLog2WindowSize() const {return m_log2WindowSize;}
81 
82  void IsolatedInitialize(const NameValuePairs &parameters);
83  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
84  bool IsolatedFlush(bool hardFlush, bool blocking);
85 
86 protected:
87  virtual void WritePrestreamHeader() {}
88  virtual void ProcessUncompressedData(const byte *string, size_t length)
89  {CRYPTOPP_UNUSED(string), CRYPTOPP_UNUSED(length);}
90  virtual void WritePoststreamTail() {}
91 
92  enum {STORED = 0, STATIC = 1, DYNAMIC = 2};
93  enum {MIN_MATCH = 3, MAX_MATCH = 258};
94 
95  void InitializeStaticEncoders();
96  void Reset(bool forceReset = false);
97  unsigned int FillWindow(const byte *str, size_t length);
98  unsigned int ComputeHash(const byte *str) const;
99  unsigned int LongestMatch(unsigned int &bestMatch) const;
100  void InsertString(unsigned int start);
101  void ProcessBuffer();
102 
103  void LiteralByte(byte b);
104  void MatchFound(unsigned int distance, unsigned int length);
105  void EncodeBlock(bool eof, unsigned int blockType);
106  void EndBlock(bool eof);
107 
108  struct EncodedMatch
109  {
110  unsigned literalCode : 9;
111  unsigned literalExtra : 5;
112  unsigned distanceCode : 5;
113  unsigned distanceExtra : 13;
114  };
115 
116  int m_deflateLevel, m_log2WindowSize, m_compressibleDeflateLevel;
117  unsigned int m_detectSkip, m_detectCount;
118  unsigned int DSIZE, DMASK, HSIZE, HMASK, GOOD_MATCH, MAX_LAZYLENGTH, MAX_CHAIN_LENGTH;
119  bool m_headerWritten, m_matchAvailable;
120  unsigned int m_dictionaryEnd, m_stringStart, m_lookahead, m_minLookahead, m_previousMatch, m_previousLength;
121  HuffmanEncoder m_staticLiteralEncoder, m_staticDistanceEncoder, m_dynamicLiteralEncoder, m_dynamicDistanceEncoder;
122  SecByteBlock m_byteBuffer;
123  SecBlock<word16> m_head, m_prev;
124  FixedSizeSecBlock<unsigned int, 286> m_literalCounts;
125  FixedSizeSecBlock<unsigned int, 30> m_distanceCounts;
126  SecBlock<EncodedMatch> m_matchBuffer;
127  unsigned int m_matchBufferEnd, m_blockStart, m_blockLength;
128 };
129 
130 NAMESPACE_END
131 
132 #endif
Utility functions for the Crypto++ library.
Secure memory block with allocator and cleanup.
Definition: secblock.h:429
Abstract base classes that provide a uniform interface to this library.
DEFLATE (RFC 1951) compressor.
Definition: zdeflate.h:65
SecBlock typedef.
Definition: secblock.h:723
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:1342
HuffmanEncoder()
Construct a HuffmanEncoder.
Definition: zdeflate.h:38
void SetDeflateLevel(int deflateLevel)
this function can be used to set the deflate level in the middle of compression
Definition: zdeflate.cpp:301
Implementation of BufferedTransformation's attachment interface.
Deflator(BufferedTransformation *attachment=NULL, int deflateLevel=DEFAULT_DEFLATE_LEVEL, int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE, bool detectUncompressible=true)
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
Crypto++ library namespace.
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: zdeflate.cpp:250
Interface for retrieving values given their names.
Definition: cryptlib.h:277