Crypto++  5.6.4
Free C++ class library of cryptographic schemes
zlib.cpp
1 // zlib.cpp - 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);
28  AttachedTransformation()->PutWord16(RoundUpToMultipleOf(word16(cmf*256+flags), word16(31)));
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
ZlibDecompressor(BufferedTransformation *attachment=NULL, bool repeat=false, int autoSignalPropagation=-1)
Construct a ZlibDecompressor.
Definition: zlib.cpp:51
Interface for buffered transformations.
Definition: cryptlib.h:1359
DEFLATE compression and decompression (RFC 1951)
Classes and functions for secure memory allocations.
int GetDeflateLevel() const
Retrieves the deflation level.
Definition: zdeflate.h:116
ZLIB Compressor (RFC 1950)
Definition: zlib.h:12
size_t Put(byte inByte, bool blocking=true)
Input a byte for processing.
Definition: cryptlib.h:1385
virtual void Restart()
Restart the hash.
Definition: cryptlib.h:968
BufferedTransformation * AttachedTransformation()
Retrieve attached transformation.
Definition: filters.cpp:36
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: adler32.cpp:8
Fixed size stack-based SecBlock.
Definition: secblock.h:753
virtual bool Verify(const byte *digest)
Verifies the hash of the current message.
Definition: cryptlib.h:1018
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:62
DEFLATE (RFC 1951) decompressor.
Definition: zinflate.h:88
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
Definition: misc.h:866
Crypto++ library namespace.
virtual void Final(byte *digest)
Computes the hash of the current message.
Definition: cryptlib.h:963