Crypto++  8.8
Free C++ class library of cryptographic schemes
zlib.cpp
1 // zlib.cpp - originally written and placed in the public domain by Wei Dai
2 
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.
7 
8 #include "pch.h"
9 #include "zlib.h"
10 #include "zdeflate.h"
11 #include "zinflate.h"
12 #include "secblock.h"
13 
14 NAMESPACE_BEGIN(CryptoPP)
15 
16 static const byte DEFLATE_METHOD = 8;
17 static const byte FDICT_FLAG = (1 << 5);
18 
19 // *************************************************************
20 
21 void ZlibCompressor::WritePrestreamHeader()
22 {
23  m_adler32.Restart();
24  CRYPTOPP_ASSERT(((GetLog2WindowSize()-8) << 4) <= 255);
25  byte cmf = byte(DEFLATE_METHOD | ((GetLog2WindowSize()-8) << 4));
26  CRYPTOPP_ASSERT((GetCompressionLevel() << 6) <= 255);
27  byte flags = byte(GetCompressionLevel() << 6);
29 }
30 
31 void ZlibCompressor::ProcessUncompressedData(const byte *inString, size_t length)
32 {
33  m_adler32.Update(inString, length);
34 }
35 
36 void ZlibCompressor::WritePoststreamTail()
37 {
39  m_adler32.Final(adler32);
40  AttachedTransformation()->Put(adler32, 4);
41 }
42 
43 unsigned int ZlibCompressor::GetCompressionLevel() const
44 {
45  static const unsigned int deflateToCompressionLevel[] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3};
46  return deflateToCompressionLevel[GetDeflateLevel()];
47 }
48 
49 // *************************************************************
50 
51 ZlibDecompressor::ZlibDecompressor(BufferedTransformation *attachment, bool repeat, int propagation)
52  : Inflator(attachment, repeat, propagation), m_log2WindowSize(0)
53 {
54 }
55 
56 void ZlibDecompressor::ProcessPrestreamHeader()
57 {
58  m_adler32.Restart();
59 
60  byte cmf;
61  byte flags;
62 
63  if (!m_inQueue.Get(cmf) || !m_inQueue.Get(flags))
64  throw HeaderErr();
65 
66  if ((cmf*256+flags) % 31 != 0)
67  throw HeaderErr(); // if you hit this exception, you're probably trying to decompress invalid data
68 
69  if ((cmf & 0xf) != DEFLATE_METHOD)
70  throw UnsupportedAlgorithm();
71 
72  if (flags & FDICT_FLAG)
73  throw UnsupportedPresetDictionary();
74 
75  m_log2WindowSize = 8 + (cmf >> 4);
76 }
77 
78 void ZlibDecompressor::ProcessDecompressedData(const byte *inString, size_t length)
79 {
80  AttachedTransformation()->Put(inString, length);
81  m_adler32.Update(inString, length);
82 }
83 
84 void ZlibDecompressor::ProcessPoststreamTail()
85 {
87  if (m_inQueue.Get(adler32, 4) != 4)
88  throw Adler32Err();
89  if (!m_adler32.Verify(adler32))
90  throw Adler32Err();
91 }
92 
93 NAMESPACE_END
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: adler32.cpp:8
Interface for buffered transformations.
Definition: cryptlib.h:1657
size_t PutWord16(word16 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true)
Input a 16-bit word for processing.
size_t Put(byte inByte, bool blocking=true)
Input a byte for processing.
Definition: cryptlib.h:1678
int GetDeflateLevel() const
Retrieves the deflation level.
Definition: zdeflate.h:118
int GetLog2WindowSize() const
Retrieves the window size.
Definition: zdeflate.h:122
BufferedTransformation * AttachedTransformation()
Retrieve attached transformation.
Fixed size stack-based SecBlock.
Definition: secblock.h:1246
virtual bool Verify(const byte *digest)
Verifies the hash of the current message.
Definition: cryptlib.h:1205
virtual void Restart()
Restart the hash.
Definition: cryptlib.h:1152
virtual void Final(byte *digest)
Computes the hash of the current message.
Definition: cryptlib.h:1147
DEFLATE decompressor (RFC 1951)
Definition: zinflate.h:95
ZlibDecompressor(BufferedTransformation *attachment=NULL, bool repeat=false, int autoSignalPropagation=-1)
Construct a ZlibDecompressor.
Definition: zlib.cpp:51
unsigned char byte
8-bit unsigned datatype
Definition: config_int.h:66
unsigned short word16
16-bit unsigned datatype
Definition: config_int.h:69
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
Definition: misc.h:1384
Crypto++ library namespace.
Precompiled header file.
Classes and functions for secure memory allocations.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:68
DEFLATE compression and decompression (RFC 1951)
DEFLATE compression and decompression (RFC 1951)
ZLIB compression and decompression (RFC 1950)