1
// zlib.cpp - written and placed in the public domain by Wei Dai
3
// "zlib" is the name of a well known C language compression library
4
// (http://www.zlib.org) and also the name of a compression format
5
// (RFC 1950) that the library implements. This file is part of a
6
// complete reimplementation of the zlib compression format.
13
NAMESPACE_BEGIN(CryptoPP)
15
static const byte DEFLATE_METHOD = 8;
16
static const byte FDICT_FLAG = 1 << 5;
18
// *************************************************************
20
void ZlibCompressor::WritePrestreamHeader()
23
byte cmf = DEFLATE_METHOD | ((GetLog2WindowSize()-8) << 4);
24
byte flags = GetCompressionLevel() << 6;
25
AttachedTransformation()->PutWord16(RoundUpToMultipleOf(cmf*256+flags, 31));
28
void ZlibCompressor::ProcessUncompressedData(const byte *inString, size_t length)
30
m_adler32.Update(inString, length);
33
void ZlibCompressor::WritePoststreamTail()
35
FixedSizeSecBlock<byte, 4> adler32;
36
m_adler32.Final(adler32);
37
AttachedTransformation()->Put(adler32, 4);
40
unsigned int ZlibCompressor::GetCompressionLevel() const
42
static const unsigned int deflateToCompressionLevel[] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3};
43
return deflateToCompressionLevel[GetDeflateLevel()];
46
// *************************************************************
48
ZlibDecompressor::ZlibDecompressor(BufferedTransformation *attachment, bool repeat, int propagation)
49
: Inflator(attachment, repeat, propagation)
53
void ZlibDecompressor::ProcessPrestreamHeader()
60
if (!m_inQueue.Get(cmf) || !m_inQueue.Get(flags))
63
if ((cmf*256+flags) % 31 != 0)
64
throw HeaderErr(); // if you hit this exception, you're probably trying to decompress invalid data
66
if ((cmf & 0xf) != DEFLATE_METHOD)
67
throw UnsupportedAlgorithm();
69
if (flags & FDICT_FLAG)
70
throw UnsupportedPresetDictionary();
72
m_log2WindowSize = 8 + (cmf >> 4);
75
void ZlibDecompressor::ProcessDecompressedData(const byte *inString, size_t length)
77
AttachedTransformation()->Put(inString, length);
78
m_adler32.Update(inString, length);
81
void ZlibDecompressor::ProcessPoststreamTail()
83
FixedSizeSecBlock<byte, 4> adler32;
84
if (m_inQueue.Get(adler32, 4) != 4)
86
if (!m_adler32.Verify(adler32))