zlib.cpp
// zlib.cpp - originally written and placed in the public domain by Wei Dai
// "zlib" is the name of a well known C language compression library
// (http://www.zlib.org) and also the name of a compression format
// (RFC 1950) that the library implements. This file is part of a
// complete reimplementation of the zlib compression format.
#include "pch.h"
#include "zlib.h"
#include "zdeflate.h"
#include "zinflate.h"
#include "secblock.h"
NAMESPACE_BEGIN(CryptoPP)
static const byte DEFLATE_METHOD = 8;
static const byte FDICT_FLAG = (1 << 5);
// *************************************************************
void ZlibCompressor::WritePrestreamHeader()
{
m_adler32.Restart();
CRYPTOPP_ASSERT(((GetLog2WindowSize()-8) << 4) <= 255);
byte cmf = byte(DEFLATE_METHOD | ((GetLog2WindowSize()-8) << 4));
CRYPTOPP_ASSERT((GetCompressionLevel() << 6) <= 255);
byte flags = byte(GetCompressionLevel() << 6);
AttachedTransformation()->PutWord16(RoundUpToMultipleOf(word16(cmf*256+flags), word16(31)));
}
void ZlibCompressor::ProcessUncompressedData(const byte *inString, size_t length)
{
m_adler32.Update(inString, length);
}
void ZlibCompressor::WritePoststreamTail()
{
FixedSizeSecBlock<byte, 4> adler32;
m_adler32.Final(adler32);
AttachedTransformation()->Put(adler32, 4);
}
unsigned int ZlibCompressor::GetCompressionLevel() const
{
static const unsigned int deflateToCompressionLevel[] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3};
return deflateToCompressionLevel[GetDeflateLevel()];
}
// *************************************************************
ZlibDecompressor::ZlibDecompressor(BufferedTransformation *attachment, bool repeat, int propagation)
: Inflator(attachment, repeat, propagation), m_log2WindowSize(0)
{
}
void ZlibDecompressor::ProcessPrestreamHeader()
{
m_adler32.Restart();
byte cmf;
byte flags;
if (!m_inQueue.Get(cmf) || !m_inQueue.Get(flags))
throw HeaderErr();
if ((cmf*256+flags) % 31 != 0)
throw HeaderErr(); // if you hit this exception, you're probably trying to decompress invalid data
if ((cmf & 0xf) != DEFLATE_METHOD)
throw UnsupportedAlgorithm();
if (flags & FDICT_FLAG)
throw UnsupportedPresetDictionary();
m_log2WindowSize = 8 + (cmf >> 4);
}
void ZlibDecompressor::ProcessDecompressedData(const byte *inString, size_t length)
{
AttachedTransformation()->Put(inString, length);
m_adler32.Update(inString, length);
}
void ZlibDecompressor::ProcessPoststreamTail()
{
FixedSizeSecBlock<byte, 4> adler32;
if (m_inQueue.Get(adler32, 4) != 4)
throw Adler32Err();
if (!m_adler32.Verify(adler32))
throw Adler32Err();
}
NAMESPACE_END
Adler32::Update
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition adler32.cpp:8
BufferedTransformation
Interface for buffered transformations.
Definition cryptlib.h:1657
BufferedTransformation::PutWord16
size_t PutWord16(word16 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true)
Input a 16-bit word for processing.
BufferedTransformation::Put
size_t Put(byte inByte, bool blocking=true)
Input a byte for processing.
Definition cryptlib.h:1678
Deflator::GetDeflateLevel
int GetDeflateLevel() const
Retrieves the deflation level.
Definition zdeflate.h:118
Deflator::GetLog2WindowSize
int GetLog2WindowSize() const
Retrieves the window size.
Definition zdeflate.h:122
Filter::AttachedTransformation
BufferedTransformation * AttachedTransformation()
Retrieve attached transformation.
FixedSizeSecBlock
Fixed size stack-based SecBlock.
Definition secblock.h:1246
HashTransformation::Verify
virtual bool Verify(const byte *digest)
Verifies the hash of the current message.
Definition cryptlib.h:1205
HashTransformation::Restart
virtual void Restart()
Restart the hash.
Definition cryptlib.h:1152
HashTransformation::Final
virtual void Final(byte *digest)
Computes the hash of the current message.
Definition cryptlib.h:1147
Inflator
DEFLATE decompressor (RFC 1951)
Definition zinflate.h:95
ZlibDecompressor::ZlibDecompressor
ZlibDecompressor(BufferedTransformation *attachment=NULL, bool repeat=false, int autoSignalPropagation=-1)
Construct a ZlibDecompressor.
Definition zlib.cpp:51
byte
unsigned char byte
8-bit unsigned datatype
Definition config_int.h:66
word16
unsigned short word16
16-bit unsigned datatype
Definition config_int.h:69
RoundUpToMultipleOf
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
Definition misc.h:1384
CryptoPP
Crypto++ library namespace.
pch.h
Precompiled header file.
secblock.h
Classes and functions for secure memory allocations.
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition trap.h:68
zdeflate.h
DEFLATE compression and decompression (RFC 1951)
zinflate.h
DEFLATE compression and decompression (RFC 1951)
zlib.h
ZLIB compression and decompression (RFC 1950)