Fabcoin Core  0.16.2
P2P Digital Currency
zdeflate.h
Go to the documentation of this file.
1 // zdeflate.h - written and placed in the public domain by Wei Dai
2 
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 
14 
15 class LowFirstBitWriter : public Filter
19 {
20 public:
24 
25  void PutBits(unsigned long value, unsigned int length);
26  void FlushBitBuffer();
27  void ClearBitBuffer();
28 
29  void StartCounting();
30  unsigned long FinishCounting();
31 
32 protected:
33  bool m_counting;
34  unsigned long m_bitCount;
35  unsigned long m_buffer;
36  unsigned int m_bitsBuffered, m_bytesBuffered;
38 };
39 
44 {
45 public:
46  typedef unsigned int code_t;
47  typedef unsigned int value_t;
48 
51 
55  HuffmanEncoder(const unsigned int *codeBits, unsigned int nCodes);
56 
60  void Initialize(const unsigned int *codeBits, unsigned int nCodes);
61 
62  static void GenerateCodeLengths(unsigned int *codeBits, unsigned int maxCodeBits, const unsigned int *codeCounts, size_t nCodes);
63 
64  void Encode(LowFirstBitWriter &writer, value_t value) const;
65 
66  struct Code
67  {
68  unsigned int code;
69  unsigned int len;
70  };
71 
73 };
74 
79 {
80 public:
82  enum {
84  MIN_DEFLATE_LEVEL = 0,
86  DEFAULT_DEFLATE_LEVEL = 6,
88  MAX_DEFLATE_LEVEL = 9};
89 
91  enum {
93  MIN_LOG2_WINDOW_SIZE = 9,
95  DEFAULT_LOG2_WINDOW_SIZE = 15,
97  MAX_LOG2_WINDOW_SIZE = 15};
98 
107  Deflator(BufferedTransformation *attachment=NULL, int deflateLevel=DEFAULT_DEFLATE_LEVEL, int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE, bool detectUncompressible=true);
112  Deflator(const NameValuePairs &parameters, BufferedTransformation *attachment=NULL);
113 
117  void SetDeflateLevel(int deflateLevel);
118 
121  int GetDeflateLevel() const {return m_deflateLevel;}
122 
125  int GetLog2WindowSize() const {return m_log2WindowSize;}
126 
127  void IsolatedInitialize(const NameValuePairs &parameters);
128  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
129  bool IsolatedFlush(bool hardFlush, bool blocking);
130 
131 protected:
132  virtual void WritePrestreamHeader() {}
133  virtual void ProcessUncompressedData(const byte *string, size_t length)
134  {CRYPTOPP_UNUSED(string), CRYPTOPP_UNUSED(length);}
135  virtual void WritePoststreamTail() {}
136 
137  enum {STORED = 0, STATIC = 1, DYNAMIC = 2};
138  enum {MIN_MATCH = 3, MAX_MATCH = 258};
139 
140  void InitializeStaticEncoders();
141  void Reset(bool forceReset = false);
142  unsigned int FillWindow(const byte *str, size_t length);
143  unsigned int ComputeHash(const byte *str) const;
144  unsigned int LongestMatch(unsigned int &bestMatch) const;
145  void InsertString(unsigned int start);
146  void ProcessBuffer();
147 
148  void LiteralByte(byte b);
149  void MatchFound(unsigned int distance, unsigned int length);
150  void EncodeBlock(bool eof, unsigned int blockType);
151  void EndBlock(bool eof);
152 
154  {
155  unsigned literalCode : 9;
156  unsigned literalExtra : 5;
157  unsigned distanceCode : 5;
158  unsigned distanceExtra : 13;
159  };
160 
161  int m_deflateLevel, m_log2WindowSize, m_compressibleDeflateLevel;
162  unsigned int m_detectSkip, m_detectCount;
163  unsigned int DSIZE, DMASK, HSIZE, HMASK, GOOD_MATCH, MAX_LAZYLENGTH, MAX_CHAIN_LENGTH;
164  bool m_headerWritten, m_matchAvailable;
165  unsigned int m_dictionaryEnd, m_stringStart, m_lookahead, m_minLookahead, m_previousMatch, m_previousLength;
166  HuffmanEncoder m_staticLiteralEncoder, m_staticDistanceEncoder, m_dynamicLiteralEncoder, m_dynamicDistanceEncoder;
172  unsigned int m_matchBufferEnd, m_blockStart, m_blockLength;
173 };
174 
176 
177 #endif
unsigned int code_t
Definition: zdeflate.h:46
uint8_t byte
Definition: Common.h:57
Utility functions for the Crypto++ library.
int m_log2WindowSize
Definition: zdeflate.h:161
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
SecByteBlock m_byteBuffer
Definition: zdeflate.h:167
int GetLog2WindowSize() const
Retrieves the window size.
Definition: zdeflate.h:125
Secure memory block with allocator and cleanup.
Definition: secblock.h:437
unsigned int len
Definition: zdeflate.h:69
Abstract base classes that provide a uniform interface to this library.
Encoding table writer.
Definition: zdeflate.h:18
SecBlock< EncodedMatch > m_matchBuffer
Definition: zdeflate.h:171
virtual void ProcessUncompressedData(const byte *string, size_t length)
Definition: zdeflate.h:133
DEFLATE compressor (RFC 1951)
Definition: zdeflate.h:78
SecBlock< Code > m_valueToCode
Definition: zdeflate.h:72
Interface for buffered transformations.
Definition: cryptlib.h:1352
unsigned int m_matchBufferEnd
Definition: zdeflate.h:172
int GetDeflateLevel() const
Retrieves the deflation level.
Definition: zdeflate.h:121
bool m_matchAvailable
Definition: zdeflate.h:164
unsigned int code
Definition: zdeflate.h:68
void Encode(LowFirstBitWriter &writer, value_t value) const
Definition: zdeflate.cpp:225
FixedSizeSecBlock< unsigned int, 30 > m_distanceCounts
Definition: zdeflate.h:170
static void GenerateCodeLengths(unsigned int *codeBits, unsigned int maxCodeBits, const unsigned int *codeCounts, size_t nCodes)
Definition: zdeflate.cpp:119
unsigned long m_buffer
Definition: zdeflate.h:35
unsigned int m_bytesBuffered
Definition: zdeflate.h:36
unsigned int m_detectSkip
Definition: zdeflate.h:162
#define b(i, j)
HuffmanEncoder()
Construct a HuffmanEncoder.
Definition: zdeflate.h:50
unsigned int MAX_LAZYLENGTH
Definition: zdeflate.h:163
Implementation of BufferedTransformation&#39;s attachment interface.
virtual void WritePrestreamHeader()
Definition: zdeflate.h:132
unsigned int value_t
Definition: zdeflate.h:47
FixedSizeSecBlock< unsigned int, 286 > m_literalCounts
Definition: zdeflate.h:169
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
void Initialize(const unsigned int *codeBits, unsigned int nCodes)
Initialize or reinitialize this object.
Definition: zdeflate.cpp:193
Implementation of BufferedTransformation&#39;s attachment interface.
Definition: filters.h:36
SecBlock< word16 > m_prev
Definition: zdeflate.h:168
unsigned int m_stringStart
Definition: zdeflate.h:165
#define NAMESPACE_END
Definition: config.h:201
std::vector< char * > parameters
Definition: boostTest.cpp:46
unsigned long m_bitCount
Definition: zdeflate.h:34
FixedSizeSecBlock< byte, 256 > m_outputBuffer
Definition: zdeflate.h:37
virtual void WritePoststreamTail()
Definition: zdeflate.h:135
HuffmanEncoder m_staticLiteralEncoder
Definition: zdeflate.h:166
Huffman Encoder.
Definition: zdeflate.h:43
Interface for retrieving values given their names.
Definition: cryptlib.h:279