Fabcoin Core  0.16.2
P2P Digital Currency
zlib.cpp
Go to the documentation of this file.
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 
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 
37 {
39  m_adler32.Final(adler32);
40  AttachedTransformation()->Put(adler32, 4);
41 }
42 
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 
57 {
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)
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 
85 {
87  if (m_inQueue.Get(adler32, 4) != 4)
88  throw Adler32Err();
89  if (!m_adler32.Verify(adler32))
90  throw Adler32Err();
91 }
92 
ZlibDecompressor(BufferedTransformation *attachment=NULL, bool repeat=false, int autoSignalPropagation=-1)
Construct a ZlibDecompressor.
Definition: zlib.cpp:51
uint8_t byte
Definition: Common.h:57
void ProcessPrestreamHeader()
Definition: zlib.cpp:56
int m_log2WindowSize
Definition: zdeflate.h:161
unsigned short word16
Definition: config.h:230
ByteQueue m_inQueue
Definition: zinflate.h:117
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
Interface for buffered transformations.
Definition: cryptlib.h:1352
DEFLATE compression and decompression (RFC 1951)
Classes and functions for secure memory allocations.
void WritePoststreamTail()
Definition: zlib.cpp:36
int GetDeflateLevel() const
Retrieves the deflation level.
Definition: zdeflate.h:121
ZLIB Compressor (RFC 1950)
Definition: zlib.h:12
size_t Put(byte inByte, bool blocking=true)
Input a byte for processing.
Definition: cryptlib.h:1376
virtual void Restart()
Restart the hash.
Definition: cryptlib.h:965
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:1015
#define CRYPTOPP_ASSERT(exp)
Definition: trap.h:92
Adler32 m_adler32
Definition: zlib.h:27
DEFLATE decompressor (RFC 1951)
Definition: zinflate.h:92
uint8_t byte
Definition: Common.h:10
void ProcessDecompressedData(const byte *string, size_t length)
Definition: zlib.cpp:78
unsigned int m_log2WindowSize
Definition: zlib.h:54
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
Definition: misc.h:905
#define NAMESPACE_END
Definition: config.h:201
void ProcessUncompressedData(const byte *string, size_t length)
Definition: zlib.cpp:31
virtual void Final(byte *digest)
Computes the hash of the current message.
Definition: cryptlib.h:960
unsigned int GetCompressionLevel() const
Definition: zlib.cpp:43
size_t Get(byte &outByte)
Retrieve a 8-bit byte.
Definition: queue.cpp:300
Adler32 m_adler32
Definition: zlib.h:55
void ProcessPoststreamTail()
Definition: zlib.cpp:84