<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>Crypto++: gzip.cpp Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.1 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <h1>gzip.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// gzip.cpp - written and placed in the public domain by Wei Dai</span> <a name="l00002"></a>00002 <a name="l00003"></a>00003 <span class="preprocessor">#include "pch.h"</span> <a name="l00004"></a>00004 <span class="preprocessor">#include "gzip.h"</span> <a name="l00005"></a>00005 <a name="l00006"></a>00006 NAMESPACE_BEGIN(CryptoPP) <a name="l00007"></a>00007 <a name="l00008"></a>00008 void <a class="code" href="class_gzip.html" title="GZIP Compression (RFC 1952).">Gzip</a>::WritePrestreamHeader() <a name="l00009"></a>00009 { <a name="l00010"></a>00010 m_totalLen = 0; <a name="l00011"></a>00011 m_crc.Restart(); <a name="l00012"></a>00012 <a name="l00013"></a>00013 AttachedTransformation()->Put(MAGIC1); <a name="l00014"></a>00014 AttachedTransformation()->Put(MAGIC2); <a name="l00015"></a>00015 AttachedTransformation()->Put(DEFLATED); <a name="l00016"></a>00016 AttachedTransformation()->Put(0); <span class="comment">// general flag</span> <a name="l00017"></a>00017 AttachedTransformation()->PutWord32(0); <span class="comment">// time stamp</span> <a name="l00018"></a>00018 byte extra = (GetDeflateLevel() == 1) ? FAST : ((GetDeflateLevel() == 9) ? SLOW : 0); <a name="l00019"></a>00019 AttachedTransformation()->Put(extra); <a name="l00020"></a>00020 AttachedTransformation()->Put(GZIP_OS_CODE); <a name="l00021"></a>00021 } <a name="l00022"></a>00022 <a name="l00023"></a>00023 <span class="keywordtype">void</span> Gzip::ProcessUncompressedData(<span class="keyword">const</span> byte *inString, <span class="keywordtype">size_t</span> length) <a name="l00024"></a>00024 { <a name="l00025"></a>00025 m_crc.<a class="code" href="class_c_r_c32.html#abd642abba82cf63edb025461bc1af8c0" title="process more input">Update</a>(inString, length); <a name="l00026"></a>00026 m_totalLen += (word32)length; <a name="l00027"></a>00027 } <a name="l00028"></a>00028 <a name="l00029"></a>00029 <span class="keywordtype">void</span> Gzip::WritePoststreamTail() <a name="l00030"></a>00030 { <a name="l00031"></a>00031 <a class="code" href="class_sec_block.html" title="a block of memory allocated using A">SecByteBlock</a> crc(4); <a name="l00032"></a>00032 m_crc.<a class="code" href="class_hash_transformation.html#aa0b8c7a110d8968268fd02ec32b9a8e8" title="compute hash for current message, then restart for a new message">Final</a>(crc); <a name="l00033"></a>00033 <a class="code" href="class_filter.html#ac8fc888888b2dad88bf64fc703044a93" title="returns the object immediately attached to this object or NULL for no attachment">AttachedTransformation</a>()-><a class="code" href="class_buffered_transformation.html#ae70658b0d271f8e114ac6c3cc9774ede" title="input a byte for processing">Put</a>(crc, 4); <a name="l00034"></a>00034 <a class="code" href="class_filter.html#ac8fc888888b2dad88bf64fc703044a93" title="returns the object immediately attached to this object or NULL for no attachment">AttachedTransformation</a>()-><a class="code" href="class_buffered_transformation.html#affcc574867d51528afe3c96721ded075" title="input a 32-bit word">PutWord32</a>(m_totalLen, LITTLE_ENDIAN_ORDER); <a name="l00035"></a>00035 } <a name="l00036"></a>00036 <a name="l00037"></a>00037 <span class="comment">// *************************************************************</span> <a name="l00038"></a>00038 <a name="l00039"></a><a class="code" href="class_gunzip.html#a30ad6514a475206a6b60ccf81693323b">00039</a> <a class="code" href="class_gunzip.html#a30ad6514a475206a6b60ccf81693323b">Gunzip::Gunzip</a>(<a class="code" href="class_buffered_transformation.html" title="interface for buffered transformations">BufferedTransformation</a> *attachment, <span class="keywordtype">bool</span> repeat, <span class="keywordtype">int</span> propagation) <a name="l00040"></a>00040 : <a class="code" href="class_inflator.html" title="DEFLATE (RFC 1951) decompressor.">Inflator</a>(attachment, repeat, propagation) <a name="l00041"></a>00041 { <a name="l00042"></a>00042 } <a name="l00043"></a>00043 <a name="l00044"></a>00044 <span class="keywordtype">void</span> Gunzip::ProcessPrestreamHeader() <a name="l00045"></a>00045 { <a name="l00046"></a>00046 m_length = 0; <a name="l00047"></a>00047 m_crc.<a class="code" href="class_hash_transformation.html#a92752eebac10497211d7a065faba8725" title="discard the current state, and restart with a new message">Restart</a>(); <a name="l00048"></a>00048 <a name="l00049"></a>00049 byte buf[6]; <a name="l00050"></a>00050 byte b, flags; <a name="l00051"></a>00051 <a name="l00052"></a>00052 <span class="keywordflow">if</span> (m_inQueue.<a class="code" href="class_byte_queue.html#a19413e3aa72b749e3771d617e8eca632" title="try to retrieve a single byte">Get</a>(buf, 2)!=2) <span class="keywordflow">throw</span> HeaderErr(); <a name="l00053"></a>00053 <span class="keywordflow">if</span> (buf[0] != MAGIC1 || buf[1] != MAGIC2) <span class="keywordflow">throw</span> HeaderErr(); <a name="l00054"></a>00054 <span class="keywordflow">if</span> (!m_inQueue.<a class="code" href="class_buffered_transformation.html#a0c25529ded99db20ad35ccef3f7234e6" title="discard skipMax bytes from the output buffer">Skip</a>(1)) <span class="keywordflow">throw</span> HeaderErr(); <span class="comment">// skip extra flags</span> <a name="l00055"></a>00055 <span class="keywordflow">if</span> (!m_inQueue.<a class="code" href="class_byte_queue.html#a19413e3aa72b749e3771d617e8eca632" title="try to retrieve a single byte">Get</a>(flags)) <span class="keywordflow">throw</span> HeaderErr(); <a name="l00056"></a>00056 <span class="keywordflow">if</span> (flags & (ENCRYPTED | CONTINUED)) <span class="keywordflow">throw</span> HeaderErr(); <a name="l00057"></a>00057 <span class="keywordflow">if</span> (m_inQueue.<a class="code" href="class_buffered_transformation.html#a0c25529ded99db20ad35ccef3f7234e6" title="discard skipMax bytes from the output buffer">Skip</a>(6)!=6) <span class="keywordflow">throw</span> HeaderErr(); <span class="comment">// Skip file time, extra flags and OS type</span> <a name="l00058"></a>00058 <a name="l00059"></a>00059 <span class="keywordflow">if</span> (flags & EXTRA_FIELDS) <span class="comment">// skip extra fields</span> <a name="l00060"></a>00060 { <a name="l00061"></a>00061 word16 length; <a name="l00062"></a>00062 <span class="keywordflow">if</span> (m_inQueue.<a class="code" href="class_buffered_transformation.html#a1ac50bcb2d279c3c6aef6858dab3eeb7" title="try to retrieve a 16-bit word">GetWord16</a>(length, LITTLE_ENDIAN_ORDER) != 2) <span class="keywordflow">throw</span> HeaderErr(); <a name="l00063"></a>00063 <span class="keywordflow">if</span> (m_inQueue.<a class="code" href="class_buffered_transformation.html#a0c25529ded99db20ad35ccef3f7234e6" title="discard skipMax bytes from the output buffer">Skip</a>(length)!=length) <span class="keywordflow">throw</span> HeaderErr(); <a name="l00064"></a>00064 } <a name="l00065"></a>00065 <a name="l00066"></a>00066 <span class="keywordflow">if</span> (flags & FILENAME) <span class="comment">// skip filename</span> <a name="l00067"></a>00067 <span class="keywordflow">do</span> <a name="l00068"></a>00068 <span class="keywordflow">if</span>(!m_inQueue.<a class="code" href="class_byte_queue.html#a19413e3aa72b749e3771d617e8eca632" title="try to retrieve a single byte">Get</a>(b)) <span class="keywordflow">throw</span> HeaderErr(); <a name="l00069"></a>00069 <span class="keywordflow">while</span> (b); <a name="l00070"></a>00070 <a name="l00071"></a>00071 <span class="keywordflow">if</span> (flags & COMMENTS) <span class="comment">// skip comments</span> <a name="l00072"></a>00072 <span class="keywordflow">do</span> <a name="l00073"></a>00073 <span class="keywordflow">if</span>(!m_inQueue.<a class="code" href="class_byte_queue.html#a19413e3aa72b749e3771d617e8eca632" title="try to retrieve a single byte">Get</a>(b)) <span class="keywordflow">throw</span> HeaderErr(); <a name="l00074"></a>00074 <span class="keywordflow">while</span> (b); <a name="l00075"></a>00075 } <a name="l00076"></a>00076 <a name="l00077"></a>00077 <span class="keywordtype">void</span> Gunzip::ProcessDecompressedData(<span class="keyword">const</span> byte *inString, <span class="keywordtype">size_t</span> length) <a name="l00078"></a>00078 { <a name="l00079"></a>00079 <a class="code" href="class_filter.html#ac8fc888888b2dad88bf64fc703044a93" title="returns the object immediately attached to this object or NULL for no attachment">AttachedTransformation</a>()-><a class="code" href="class_buffered_transformation.html#ae70658b0d271f8e114ac6c3cc9774ede" title="input a byte for processing">Put</a>(inString, length); <a name="l00080"></a>00080 m_crc.<a class="code" href="class_c_r_c32.html#abd642abba82cf63edb025461bc1af8c0" title="process more input">Update</a>(inString, length); <a name="l00081"></a>00081 m_length += (word32)length; <a name="l00082"></a>00082 } <a name="l00083"></a>00083 <a name="l00084"></a>00084 <span class="keywordtype">void</span> Gunzip::ProcessPoststreamTail() <a name="l00085"></a>00085 { <a name="l00086"></a>00086 <a class="code" href="class_sec_block.html" title="a block of memory allocated using A">SecByteBlock</a> crc(4); <a name="l00087"></a>00087 <span class="keywordflow">if</span> (m_inQueue.<a class="code" href="class_byte_queue.html#a19413e3aa72b749e3771d617e8eca632" title="try to retrieve a single byte">Get</a>(crc, 4) != 4) <a name="l00088"></a>00088 <span class="keywordflow">throw</span> TailErr(); <a name="l00089"></a>00089 <span class="keywordflow">if</span> (!m_crc.<a class="code" href="class_hash_transformation.html#a16c4aecd047015d513feef4ed5b929ba" title="verify that digest is a valid digest for the current message, then reinitialize the...">Verify</a>(crc)) <a name="l00090"></a>00090 <span class="keywordflow">throw</span> CrcErr(); <a name="l00091"></a>00091 <a name="l00092"></a>00092 word32 lengthCheck; <a name="l00093"></a>00093 <span class="keywordflow">if</span> (m_inQueue.<a class="code" href="class_buffered_transformation.html#a69c064d931db3a8631c1138827d67e2f" title="try to retrieve a 32-bit word">GetWord32</a>(lengthCheck, LITTLE_ENDIAN_ORDER) != 4) <a name="l00094"></a>00094 <span class="keywordflow">throw</span> TailErr(); <a name="l00095"></a>00095 <span class="keywordflow">if</span> (lengthCheck != m_length) <a name="l00096"></a>00096 <span class="keywordflow">throw</span> LengthErr(); <a name="l00097"></a>00097 } <a name="l00098"></a>00098 <a name="l00099"></a>00099 NAMESPACE_END </pre></div></div> <hr size="1"/><address style="text-align: right;"><small>Generated on 9 Dec 2009 for Crypto++ by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address> </body> </html>