Crypto++
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 
13 NAMESPACE_BEGIN(CryptoPP)
14 
15 static const byte DEFLATE_METHOD = 8;
16 static const byte FDICT_FLAG = 1 << 5;
17 
18 // *************************************************************
19 
20 void ZlibCompressor::WritePrestreamHeader()
21 {
22  m_adler32.Restart();
23  byte cmf = DEFLATE_METHOD | ((GetLog2WindowSize()-8) << 4);
24  byte flags = GetCompressionLevel() << 6;
25  AttachedTransformation()->PutWord16(RoundUpToMultipleOf(cmf*256+flags, 31));
26 }
27 
28 void ZlibCompressor::ProcessUncompressedData(const byte *inString, size_t length)
29 {
30  m_adler32.Update(inString, length);
31 }
32 
33 void ZlibCompressor::WritePoststreamTail()
34 {
36  m_adler32.Final(adler32);
37  AttachedTransformation()->Put(adler32, 4);
38 }
39 
40 unsigned int ZlibCompressor::GetCompressionLevel() const
41 {
42  static const unsigned int deflateToCompressionLevel[] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3};
43  return deflateToCompressionLevel[GetDeflateLevel()];
44 }
45 
46 // *************************************************************
47 
48 ZlibDecompressor::ZlibDecompressor(BufferedTransformation *attachment, bool repeat, int propagation)
49  : Inflator(attachment, repeat, propagation)
50 {
51 }
52 
53 void ZlibDecompressor::ProcessPrestreamHeader()
54 {
55  m_adler32.Restart();
56 
57  byte cmf;
58  byte flags;
59 
60  if (!m_inQueue.Get(cmf) || !m_inQueue.Get(flags))
61  throw HeaderErr();
62 
63  if ((cmf*256+flags) % 31 != 0)
64  throw HeaderErr(); // if you hit this exception, you're probably trying to decompress invalid data
65 
66  if ((cmf & 0xf) != DEFLATE_METHOD)
67  throw UnsupportedAlgorithm();
68 
69  if (flags & FDICT_FLAG)
70  throw UnsupportedPresetDictionary();
71 
72  m_log2WindowSize = 8 + (cmf >> 4);
73 }
74 
75 void ZlibDecompressor::ProcessDecompressedData(const byte *inString, size_t length)
76 {
77  AttachedTransformation()->Put(inString, length);
78  m_adler32.Update(inString, length);
79 }
80 
81 void ZlibDecompressor::ProcessPoststreamTail()
82 {
84  if (m_inQueue.Get(adler32, 4) != 4)
85  throw Adler32Err();
86  if (!m_adler32.Verify(adler32))
87  throw Adler32Err();
88 }
89 
90 NAMESPACE_END
ZlibDecompressor(BufferedTransformation *attachment=NULL, bool repeat=false, int autoSignalPropagation=-1)
Definition: zlib.cpp:48
interface for buffered transformations
Definition: cryptlib.h:771
ZLIB Compressor (RFC 1950)
Definition: zlib.h:11
size_t Put(byte inByte, bool blocking=true)
input a byte for processing
Definition: cryptlib.h:785
virtual void Restart()
discard the current state, and restart with a new message
Definition: cryptlib.h:549
BufferedTransformation * AttachedTransformation()
returns the object immediately attached to this object or NULL for no attachment
Definition: filters.cpp:26
void Update(const byte *input, size_t length)
process more input
Definition: adler32.cpp:8
a SecBlock with fixed size, allocated statically
Definition: secblock.h:422
virtual bool Verify(const byte *digest)
verify that digest is a valid digest for the current message, then reinitialize the object ...
Definition: cryptlib.h:575
DEFLATE (RFC 1951) decompressor.
Definition: zinflate.h:85
size_t PutWord16(word16 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true)
input a 16-bit word
Definition: cryptlib.cpp:604
virtual void Final(byte *digest)
compute hash for current message, then restart for a new message
Definition: cryptlib.h:545
size_t Get(byte &outByte)
try to retrieve a single byte
Definition: queue.cpp:293