monotone

monotone Mtn Source Tree

Root/cryptopp/gzip.cpp

1// gzip.cpp - written and placed in the public domain by Wei Dai
2
3#include "pch.h"
4#include "gzip.h"
5
6NAMESPACE_BEGIN(CryptoPP)
7
8void Gzip::WritePrestreamHeader()
9{
10m_totalLen = 0;
11m_crc.Restart();
12
13AttachedTransformation()->Put(MAGIC1);
14AttachedTransformation()->Put(MAGIC2);
15AttachedTransformation()->Put(DEFLATED);
16AttachedTransformation()->Put(0);// general flag
17AttachedTransformation()->PutWord32(0);// time stamp
18byte extra = (GetDeflateLevel() == 1) ? FAST : ((GetDeflateLevel() == 9) ? SLOW : 0);
19AttachedTransformation()->Put(extra);
20AttachedTransformation()->Put(GZIP_OS_CODE);
21}
22
23void Gzip::ProcessUncompressedData(const byte *inString, unsigned int length)
24{
25m_crc.Update(inString, length);
26m_totalLen += length;
27}
28
29void Gzip::WritePoststreamTail()
30{
31SecByteBlock crc(4);
32m_crc.Final(crc);
33AttachedTransformation()->Put(crc, 4);
34AttachedTransformation()->PutWord32(m_totalLen, LITTLE_ENDIAN_ORDER);
35}
36
37// *************************************************************
38
39Gunzip::Gunzip(BufferedTransformation *attachment, bool repeat, int propagation)
40: Inflator(attachment, repeat, propagation)
41{
42}
43
44void Gunzip::ProcessPrestreamHeader()
45{
46m_length = 0;
47m_crc.Restart();
48
49byte buf[6];
50byte b, flags;
51
52if (m_inQueue.Get(buf, 2)!=2) throw HeaderErr();
53if (buf[0] != MAGIC1 || buf[1] != MAGIC2) throw HeaderErr();
54if (!m_inQueue.Skip(1)) throw HeaderErr(); // skip extra flags
55if (!m_inQueue.Get(flags)) throw HeaderErr();
56if (flags & (ENCRYPTED | CONTINUED)) throw HeaderErr();
57if (m_inQueue.Skip(6)!=6) throw HeaderErr(); // Skip file time, extra flags and OS type
58
59if (flags & EXTRA_FIELDS)// skip extra fields
60{
61word16 length;
62if (m_inQueue.GetWord16(length, LITTLE_ENDIAN_ORDER) != 2) throw HeaderErr();
63if (m_inQueue.Skip(length)!=length) throw HeaderErr();
64}
65
66if (flags & FILENAME)// skip filename
67do
68if(!m_inQueue.Get(b)) throw HeaderErr();
69while (b);
70
71if (flags & COMMENTS)// skip comments
72do
73if(!m_inQueue.Get(b)) throw HeaderErr();
74while (b);
75}
76
77void Gunzip::ProcessDecompressedData(const byte *inString, unsigned int length)
78{
79AttachedTransformation()->Put(inString, length);
80m_crc.Update(inString, length);
81m_length += length;
82}
83
84void Gunzip::ProcessPoststreamTail()
85{
86SecByteBlock crc(4);
87if (m_inQueue.Get(crc, 4) != 4)
88throw TailErr();
89if (!m_crc.Verify(crc))
90throw CrcErr();
91
92word32 lengthCheck;
93if (m_inQueue.GetWord32(lengthCheck, LITTLE_ENDIAN_ORDER) != 4)
94throw TailErr();
95if (lengthCheck != m_length)
96throw LengthErr();
97}
98
99NAMESPACE_END

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status