Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > by-pkgid > a74ec78bdb789d910d054e3918f3f007 > files > 591

libsword1-devel-1.5.5-2mdk.ppc.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>zipcomprs.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.2.15 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="namespaces.html">Namespace List</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="classes.html">Alphabetical List</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; </center>
<hr><h1>zipcomprs.cpp</h1><div class="fragment"><pre>00001 <font class="comment">/******************************************************************************</font>
00002 <font class="comment"> *  swcomprs.cpp   - code for class 'ZipCompress'- a driver class that provides</font>
00003 <font class="comment"> *                              compression utilities. - using zlib</font>
00004 <font class="comment"> */</font>
00005 
00006 <font class="preprocessor">#include &lt;string.h&gt;</font>
00007 <font class="preprocessor">#include &lt;string&gt;</font>
00008 <font class="preprocessor">#include &lt;stdlib.h&gt;</font>
00009 <font class="preprocessor">#include &lt;stdio.h&gt;</font>
00010 <font class="preprocessor">#include &lt;zipcomprs.h&gt;</font>
00011 <font class="preprocessor">#include &lt;zlib.h&gt;</font>
00012 
00013 <font class="comment">/******************************************************************************</font>
00014 <font class="comment"> * ZipCompress Constructor - Initializes data for instance of ZipCompress</font>
00015 <font class="comment"> *</font>
00016 <font class="comment"> */</font>
00017 
00018 ZipCompress::ZipCompress() : SWCompress()
00019 {
00020 <font class="comment">//      fprintf(stderr, "init compress\n");</font>
00021 }
00022 
00023 
00024 <font class="comment">/******************************************************************************</font>
00025 <font class="comment"> * ZipCompress Destructor - Cleans up instance of ZipCompress</font>
00026 <font class="comment"> */</font>
00027 
00028 ZipCompress::~ZipCompress() {
00029 }
00030 
00031 
00032 <font class="comment">/******************************************************************************</font>
00033 <font class="comment"> * ZipCompress::Encode  - This function "encodes" the input stream into the</font>
00034 <font class="comment"> *                                              output stream.</font>
00035 <font class="comment"> *                                              The GetChars() and SendChars() functions are</font>
00036 <font class="comment"> *                                              used to separate this method from the actual</font>
00037 <font class="comment"> *                                              i/o.</font>
00038 <font class="comment"> *              NOTE:                   must set zlen for parent class to know length of</font>
00039 <font class="comment"> *                                              compressed buffer.</font>
00040 <font class="comment"> */</font>
00041 
00042 <font class="keywordtype">void</font> ZipCompress::Encode(<font class="keywordtype">void</font>)
00043 {
00044 <font class="comment">/*</font>
00045 <font class="comment">ZEXTERN int ZEXPORT compress OF((Bytef *dest,   uLongf *destLen,</font>
00046 <font class="comment">                                 const Bytef *source, uLong sourceLen));</font>
00047 <font class="comment">     Compresses the source buffer into the destination buffer.  sourceLen is</font>
00048 <font class="comment">   the byte length of the source buffer. Upon entry, destLen is the total</font>
00049 <font class="comment">   size of the destination buffer, which must be at least 0.1% larger than</font>
00050 <font class="comment">   sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the</font>
00051 <font class="comment">   compressed buffer.</font>
00052 <font class="comment">     This function can be used to compress a whole file at once if the</font>
00053 <font class="comment">   input file is mmap'ed.</font>
00054 <font class="comment">     compress returns Z_OK if success, Z_MEM_ERROR if there was not</font>
00055 <font class="comment">   enough memory, Z_BUF_ERROR if there was not enough room in the output</font>
00056 <font class="comment">   buffer.</font>
00057 <font class="comment">*/</font>
00058         direct = 0;     <font class="comment">// set direction needed by parent [Get|Send]Chars()</font>
00059 
00060         <font class="comment">// get buffer</font>
00061         <font class="keywordtype">char</font> chunk[1024];
00062         <font class="keywordtype">char</font> *buf = (<font class="keywordtype">char</font> *)calloc(1, 1024);
00063         <font class="keywordtype">char</font> *chunkbuf = buf;
00064         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> chunklen;
00065         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> len = 0;
00066         <font class="keywordflow">while</font>((chunklen = GetChars(chunk, 1023))) {
00067                 memcpy(chunkbuf, chunk, chunklen);
00068                 len += chunklen;
00069                 <font class="keywordflow">if</font> (chunklen &lt; 1023)
00070                         <font class="keywordflow">break</font>;
00071                 <font class="keywordflow">else</font>    buf = (<font class="keywordtype">char</font> *)realloc(buf, len + 1024);
00072                 chunkbuf = buf+len;
00073         }
00074 
00075 
00076         zlen = (long) (len*1.001)+15;
00077         <font class="keywordtype">char</font> *zbuf = <font class="keyword">new</font> <font class="keywordtype">char</font>[zlen+1];
00078         <font class="keywordflow">if</font> (len)
00079         {
00080                 <font class="comment">//printf("Doing compress\n");</font>
00081                 <font class="keywordflow">if</font> (compress((Bytef*)zbuf, &amp;zlen, (<font class="keyword">const</font> Bytef*)buf, len)!=Z_OK)
00082                 {
00083                         printf(<font class="stringliteral">"ERROR in compression\n"</font>);
00084                 }
00085                 <font class="keywordflow">else</font> {
00086                         SendChars(zbuf, zlen);
00087                 }
00088         }
00089         <font class="keywordflow">else</font>
00090         {
00091                 fprintf(stderr, <font class="stringliteral">"No buffer to compress\n"</font>);
00092         }
00093         <font class="keyword">delete</font> [] zbuf;
00094         free (buf);
00095 }
00096 
00097 
00098 <font class="comment">/******************************************************************************</font>
00099 <font class="comment"> * ZipCompress::Decode  - This function "decodes" the input stream into the</font>
00100 <font class="comment"> *                                              output stream.</font>
00101 <font class="comment"> *                                              The GetChars() and SendChars() functions are</font>
00102 <font class="comment"> *                                              used to separate this method from the actual</font>
00103 <font class="comment"> *                                              i/o.</font>
00104 <font class="comment"> */</font>
00105 
00106 <font class="keywordtype">void</font> ZipCompress::Decode(<font class="keywordtype">void</font>)
00107 {
00108 <font class="comment">/*</font>
00109 <font class="comment">ZEXTERN int ZEXPORT uncompress OF((Bytef *dest,   uLongf *destLen,</font>
00110 <font class="comment">                                   const Bytef *source, uLong sourceLen));</font>
00111 <font class="comment">     Decompresses the source buffer into the destination buffer.  sourceLen is</font>
00112 <font class="comment">   the byte length of the source buffer. Upon entry, destLen is the total</font>
00113 <font class="comment">   size of the destination buffer, which must be large enough to hold the</font>
00114 <font class="comment">   entire uncompressed data. (The size of the uncompressed data must have</font>
00115 <font class="comment">   been saved previously by the compressor and transmitted to the decompressor</font>
00116 <font class="comment">   by some mechanism outside the scope of this compression library.)</font>
00117 <font class="comment">   Upon exit, destLen is the actual size of the compressed buffer.</font>
00118 <font class="comment">     This function can be used to decompress a whole file at once if the</font>
00119 <font class="comment">   input file is mmap'ed.</font>
00120 <font class="comment"></font>
00121 <font class="comment">     uncompress returns Z_OK if success, Z_MEM_ERROR if there was not</font>
00122 <font class="comment">   enough memory, Z_BUF_ERROR if there was not enough room in the output</font>
00123 <font class="comment">   buffer, or Z_DATA_ERROR if the input data was corrupted.</font>
00124 <font class="comment">*/</font>
00125 
00126         <font class="comment">// get buffer</font>
00127         <font class="keywordtype">char</font> chunk[1024];
00128         <font class="keywordtype">char</font> *zbuf = (<font class="keywordtype">char</font> *)calloc(1, 1024);
00129         <font class="keywordtype">char</font> *chunkbuf = zbuf;
00130         <font class="keywordtype">int</font> chunklen;
00131         <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> zlen = 0;
00132         <font class="keywordflow">while</font>((chunklen = GetChars(chunk, 1023))) {
00133                 memcpy(chunkbuf, chunk, chunklen);
00134                 zlen += chunklen;
00135                 <font class="keywordflow">if</font> (chunklen &lt; 1023)
00136                         <font class="keywordflow">break</font>;
00137                 <font class="keywordflow">else</font>    zbuf = (<font class="keywordtype">char</font> *)realloc(zbuf, zlen + 1024);
00138                 chunkbuf = zbuf + zlen;
00139         }
00140 
00141         <font class="comment">//printf("Decoding complength{%ld} uncomp{%ld}\n", zlen, blen);</font>
00142         <font class="keywordflow">if</font> (zlen) {
00143                 <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> blen = zlen*20;   <font class="comment">// trust compression is less than 1000%</font>
00144                 <font class="keywordtype">char</font> *buf = <font class="keyword">new</font> <font class="keywordtype">char</font>[blen]; 
00145                 <font class="comment">//printf("Doing decompress {%s}\n", zbuf);</font>
00146                 <font class="keywordflow">if</font> (uncompress((Bytef*)buf, &amp;blen, (Bytef*)zbuf, zlen) != Z_OK) {
00147                         fprintf(stderr, <font class="stringliteral">"no room in outbuffer to during decompression. see zipcomp.cpp\n"</font>);
00148                 }
00149                 SendChars(buf, blen);
00150                 <font class="keyword">delete</font> [] buf;
00151                 slen = blen;
00152         }
00153         <font class="keywordflow">else</font> {
00154                 fprintf(stderr, <font class="stringliteral">"No buffer to decompress!\n"</font>);
00155         }
00156         <font class="comment">//printf("Finished decoding\n");</font>
00157         free (zbuf);
00158 }
</pre></div><hr><address align="right"><small>Generated on Thu Jun 20 22:13:01 2002 for The Sword Project by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 
width=110 height=53></a>1.2.15 </small></address>
</body>
</html>