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 
WritePrestreamHeader()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 
ProcessUncompressedData(const byte * inString,size_t length)31 void ZlibCompressor::ProcessUncompressedData(const byte *inString, size_t length)
32 {
33 	m_adler32.Update(inString, length);
34 }
35 
WritePoststreamTail()36 void ZlibCompressor::WritePoststreamTail()
37 {
38 	FixedSizeSecBlock<byte, 4> adler32;
39 	m_adler32.Final(adler32);
40 	AttachedTransformation()->Put(adler32, 4);
41 }
42 
GetCompressionLevel() const43 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 
ZlibDecompressor(BufferedTransformation * attachment,bool repeat,int propagation)51 ZlibDecompressor::ZlibDecompressor(BufferedTransformation *attachment, bool repeat, int propagation)
52 	: Inflator(attachment, repeat, propagation), m_log2WindowSize(0)
53 {
54 }
55 
ProcessPrestreamHeader()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 
ProcessDecompressedData(const byte * inString,size_t length)78 void ZlibDecompressor::ProcessDecompressedData(const byte *inString, size_t length)
79 {
80 	AttachedTransformation()->Put(inString, length);
81 	m_adler32.Update(inString, length);
82 }
83 
ProcessPoststreamTail()84 void ZlibDecompressor::ProcessPoststreamTail()
85 {
86 	FixedSizeSecBlock<byte, 4> adler32;
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
94