00001
00002
00003
00004
00005
00006
00007
00008 #include "pch.h"
00009 #include "zlib.h"
00010 #include "zdeflate.h"
00011 #include "zinflate.h"
00012 #include "secblock.h"
00013
00014 NAMESPACE_BEGIN(CryptoPP)
00015
00016 static const byte DEFLATE_METHOD = 8;
00017 static const byte FDICT_FLAG = (1 << 5);
00018
00019
00020
00021 void ZlibCompressor::WritePrestreamHeader()
00022 {
00023 m_adler32.Restart();
00024 assert(((GetLog2WindowSize()-8) << 4) <= 255);
00025 byte cmf = byte(DEFLATE_METHOD | ((GetLog2WindowSize()-8) << 4));
00026 assert((GetCompressionLevel() << 6) <= 255);
00027 byte flags = byte(GetCompressionLevel() << 6);
00028 AttachedTransformation()->PutWord16(RoundUpToMultipleOf(word16(cmf*256+flags), word16(31)));
00029 }
00030
00031 void ZlibCompressor::ProcessUncompressedData(const byte *inString, size_t length)
00032 {
00033 m_adler32.Update(inString, length);
00034 }
00035
00036 void ZlibCompressor::WritePoststreamTail()
00037 {
00038 FixedSizeSecBlock<byte, 4> adler32;
00039 m_adler32.Final(adler32);
00040 AttachedTransformation()->Put(adler32, 4);
00041 }
00042
00043 unsigned int ZlibCompressor::GetCompressionLevel() const
00044 {
00045 static const unsigned int deflateToCompressionLevel[] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3};
00046 return deflateToCompressionLevel[GetDeflateLevel()];
00047 }
00048
00049
00050
00051 ZlibDecompressor::ZlibDecompressor(BufferedTransformation *attachment, bool repeat, int propagation)
00052 : Inflator(attachment, repeat, propagation), m_log2WindowSize(0)
00053 {
00054 }
00055
00056 void ZlibDecompressor::ProcessPrestreamHeader()
00057 {
00058 m_adler32.Restart();
00059
00060 byte cmf;
00061 byte flags;
00062
00063 if (!m_inQueue.Get(cmf) || !m_inQueue.Get(flags))
00064 throw HeaderErr();
00065
00066 if ((cmf*256+flags) % 31 != 0)
00067 throw HeaderErr();
00068
00069 if ((cmf & 0xf) != DEFLATE_METHOD)
00070 throw UnsupportedAlgorithm();
00071
00072 if (flags & FDICT_FLAG)
00073 throw UnsupportedPresetDictionary();
00074
00075 m_log2WindowSize = 8 + (cmf >> 4);
00076 }
00077
00078 void ZlibDecompressor::ProcessDecompressedData(const byte *inString, size_t length)
00079 {
00080 AttachedTransformation()->Put(inString, length);
00081 m_adler32.Update(inString, length);
00082 }
00083
00084 void ZlibDecompressor::ProcessPoststreamTail()
00085 {
00086 FixedSizeSecBlock<byte, 4> adler32;
00087 if (m_inQueue.Get(adler32, 4) != 4)
00088 throw Adler32Err();
00089 if (!m_adler32.Verify(adler32))
00090 throw Adler32Err();
00091 }
00092
00093 NAMESPACE_END