Security Scol plugin
zdeflate.h
Go to the documentation of this file.
1// zdeflate.h - originally 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
13NAMESPACE_BEGIN(CryptoPP)
14
15
18{
19public:
23
24 void PutBits(unsigned long value, unsigned int length);
25 void FlushBitBuffer();
26 void ClearBitBuffer();
27
28 void StartCounting();
29 unsigned long FinishCounting();
30
31protected:
32 bool m_counting;
33 unsigned long m_bitCount;
34 unsigned long m_buffer;
35 unsigned int m_bitsBuffered, m_bytesBuffered;
36 FixedSizeSecBlock<byte, 256> m_outputBuffer;
37};
38
42{
43public:
44 typedef unsigned int code_t;
45 typedef unsigned int value_t;
46
49
53 HuffmanEncoder(const unsigned int *codeBits, unsigned int nCodes);
54
58 void Initialize(const unsigned int *codeBits, unsigned int nCodes);
59
60 static void GenerateCodeLengths(unsigned int *codeBits, unsigned int maxCodeBits, const unsigned int *codeCounts, size_t nCodes);
61
62 void Encode(LowFirstBitWriter &writer, value_t value) const;
63
64 struct Code
65 {
66 unsigned int code;
67 unsigned int len;
68 };
69
70 SecBlock<Code> m_valueToCode;
71};
72
76{
77public:
79 enum {
86
88 enum {
95
104 Deflator(BufferedTransformation *attachment=NULLPTR, int deflateLevel=DEFAULT_DEFLATE_LEVEL, int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE, bool detectUncompressible=true);
109 Deflator(const NameValuePairs &parameters, BufferedTransformation *attachment=NULLPTR);
110
114 void SetDeflateLevel(int deflateLevel);
115
118 int GetDeflateLevel() const {return m_deflateLevel;}
119
122 int GetLog2WindowSize() const {return m_log2WindowSize;}
123
124 void IsolatedInitialize(const NameValuePairs &parameters);
125 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
126 bool IsolatedFlush(bool hardFlush, bool blocking);
127
128protected:
129 virtual void WritePrestreamHeader() {}
130 virtual void ProcessUncompressedData(const byte *string, size_t length)
131 {CRYPTOPP_UNUSED(string), CRYPTOPP_UNUSED(length);}
132 virtual void WritePoststreamTail() {}
133
134 enum {STORED = 0, STATIC = 1, DYNAMIC = 2};
135 enum {MIN_MATCH = 3, MAX_MATCH = 258};
136
137 void InitializeStaticEncoders();
138 void Reset(bool forceReset = false);
139 unsigned int FillWindow(const byte *str, size_t length);
140 unsigned int ComputeHash(const byte *str) const;
141 unsigned int LongestMatch(unsigned int &bestMatch) const;
142 void InsertString(unsigned int start);
143 void ProcessBuffer();
144
145 void LiteralByte(byte b);
146 void MatchFound(unsigned int distance, unsigned int length);
147 void EncodeBlock(bool eof, unsigned int blockType);
148 void EndBlock(bool eof);
149
151 {
152 unsigned literalCode : 9;
153 unsigned literalExtra : 5;
154 unsigned distanceCode : 5;
155 unsigned distanceExtra : 13;
156 };
157
158 int m_deflateLevel, m_log2WindowSize, m_compressibleDeflateLevel;
159 unsigned int m_detectSkip, m_detectCount;
160 unsigned int DSIZE, DMASK, HSIZE, HMASK, GOOD_MATCH, MAX_LAZYLENGTH, MAX_CHAIN_LENGTH;
161 bool m_headerWritten, m_matchAvailable;
162 unsigned int m_dictionaryEnd, m_stringStart, m_lookahead, m_minLookahead, m_previousMatch, m_previousLength;
163 HuffmanEncoder m_staticLiteralEncoder, m_staticDistanceEncoder, m_dynamicLiteralEncoder, m_dynamicDistanceEncoder;
164 SecByteBlock m_byteBuffer;
165 SecBlock<word16> m_head, m_prev;
168 SecBlock<EncodedMatch> m_matchBuffer;
169 unsigned int m_matchBufferEnd, m_blockStart, m_blockLength;
170};
171
172NAMESPACE_END
173
174#endif
Interface for buffered transformations.
Definition cryptlib.h:1652
DEFLATE compressor (RFC 1951)
Definition zdeflate.h:76
@ DEFAULT_LOG2_WINDOW_SIZE
Default window size (15)
Definition zdeflate.h:92
@ MAX_LOG2_WINDOW_SIZE
Maximum window size, largest table (15)
Definition zdeflate.h:94
@ MIN_LOG2_WINDOW_SIZE
Minimum window size, smallest table (9)
Definition zdeflate.h:90
bool IsolatedFlush(bool hardFlush, bool blocking)
Flushes data buffered by this object, without signal propagation.
Definition zdeflate.cpp:551
void SetDeflateLevel(int deflateLevel)
Sets the deflation level.
Definition zdeflate.cpp:316
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition zdeflate.cpp:521
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition zdeflate.cpp:265
int GetDeflateLevel() const
Retrieves the deflation level.
Definition zdeflate.h:118
@ MIN_DEFLATE_LEVEL
Minimum deflation level, fastest speed (0)
Definition zdeflate.h:81
@ DEFAULT_DEFLATE_LEVEL
Default deflation level, compromise between speed (6)
Definition zdeflate.h:83
@ MAX_DEFLATE_LEVEL
Minimum deflation level, slowest speed (9)
Definition zdeflate.h:85
int GetLog2WindowSize() const
Retrieves the window size.
Definition zdeflate.h:122
Implementation of BufferedTransformation's attachment interface.
Definition filters.h:36
Fixed size stack-based SecBlock.
Definition secblock.h:1246
Huffman Encoder.
Definition zdeflate.h:42
void Initialize(const unsigned int *codeBits, unsigned int nCodes)
Initialize or reinitialize this object.
Definition zdeflate.cpp:199
HuffmanEncoder()
Construct a HuffmanEncoder.
Definition zdeflate.h:48
Encoding table writer.
Definition zdeflate.h:18
Interface for retrieving values given their names.
Definition cryptlib.h:322
Secure memory block with allocator and cleanup.
Definition secblock.h:731
Abstract base classes that provide a uniform interface to this library.
Implementation of BufferedTransformation's attachment interface.
Utility functions for the Crypto++ library.