00001
00002
00003
00004
00005
00006
00007 #include "wvgzip.h"
00008 #include <zlib.h>
00009 #include <assert.h>
00010
00011 #define ZBUFSIZE 10240
00012
00013
00014 WvGzipEncoder::WvGzipEncoder(Mode _mode, size_t _out_limit) :
00015 out_limit(_out_limit), tmpbuf(ZBUFSIZE), mode(_mode)
00016 {
00017 ignore_decompression_errors = false;
00018 full_flush = false;
00019 init();
00020 }
00021
00022
00023 WvGzipEncoder::~WvGzipEncoder()
00024 {
00025 close();
00026 }
00027
00028
00029 void WvGzipEncoder::init()
00030 {
00031 zstr = new z_stream;
00032 memset(zstr, 0, sizeof(*zstr));
00033 zstr->zalloc = Z_NULL;
00034 zstr->zfree = Z_NULL;
00035 zstr->opaque = NULL;
00036 zstr->msg = NULL;
00037
00038 int retval;
00039 if (mode == Deflate)
00040 retval = deflateInit(zstr, Z_DEFAULT_COMPRESSION);
00041 else
00042 retval = inflateInit(zstr);
00043
00044 if (retval != Z_OK)
00045 {
00046 seterror("error %s initializing gzip %s: %s", retval,
00047 mode == Deflate ? "compressor" : "decompressor",
00048 zstr->msg ? zstr->msg : "unknown");
00049 return;
00050 }
00051 zstr->next_in = zstr->next_out = NULL;
00052 zstr->avail_in = zstr->avail_out = 0;
00053 }
00054
00055 void WvGzipEncoder::close()
00056 {
00057 if (mode == Deflate)
00058 deflateEnd(zstr);
00059 else
00060 inflateEnd(zstr);
00061
00062 delete zstr;
00063
00064 }
00065
00066 bool WvGzipEncoder::_encode(WvBuf &inbuf, WvBuf &outbuf, bool flush)
00067 {
00068 bool success;
00069 output = 0;
00070 for (;;)
00071 {
00072 prepare(& inbuf);
00073 bool alldata = inbuf.used() == 0;
00074 success = process(outbuf, flush && alldata, false);
00075 if (zstr->avail_in != 0)
00076 {
00077
00078 inbuf.unget(zstr->avail_in);
00079 zstr->avail_in = 0;
00080 }
00081 if (! success)
00082 return false;
00083 if (alldata || (out_limit && (output == out_limit)))
00084 return true;
00085 }
00086 }
00087
00088
00089 bool WvGzipEncoder::_finish(WvBuf &outbuf)
00090 {
00091 prepare(NULL);
00092 return process(outbuf, false, true);
00093 }
00094
00095
00096 bool WvGzipEncoder::_reset()
00097 {
00098 close();
00099 init();
00100 return true;
00101 }
00102
00103
00104 void WvGzipEncoder::prepare(WvBuf *inbuf)
00105 {
00106 assert(zstr->avail_in == 0);
00107 if (inbuf && inbuf->used() != 0)
00108 {
00109 size_t avail = inbuf->optgettable();
00110 zstr->avail_in = avail;
00111 zstr->next_in = const_cast<Bytef*>(
00112 (const Bytef*)inbuf->get(avail));
00113 }
00114 else
00115 {
00116 zstr->avail_in = 0;
00117 zstr->next_in = (Bytef*)"";
00118 }
00119 }
00120
00121
00122 bool WvGzipEncoder::process(WvBuf &outbuf, bool flush, bool finish)
00123 {
00124 int flushmode = finish ? Z_FINISH :
00125 flush ? (full_flush ? Z_FULL_FLUSH : Z_SYNC_FLUSH) : Z_NO_FLUSH;
00126 int retval;
00127 do
00128 {
00129
00130 tmpbuf.zap();
00131 size_t avail_out = tmpbuf.free();
00132 if (out_limit)
00133 avail_out = tmpbuf.free() < (out_limit - output) ? tmpbuf.free()
00134 : (out_limit - output);
00135
00136 zstr->avail_out = avail_out;
00137 zstr->next_out = tmpbuf.alloc(avail_out);
00138 if (mode == Deflate)
00139 retval = deflate(zstr, flushmode);
00140 else
00141 retval = inflate(zstr, flushmode);
00142 tmpbuf.unalloc(zstr->avail_out);
00143
00144 output += avail_out - zstr->avail_out;
00145
00146
00147 outbuf.merge(tmpbuf);
00148
00149 if (retval == Z_DATA_ERROR && mode == Inflate
00150 && ignore_decompression_errors)
00151 retval = inflateSync(zstr);
00152 } while (retval == Z_OK && (!out_limit || (out_limit == output)));
00153
00154 if (retval == Z_STREAM_END)
00155 setfinished();
00156 else if (retval != Z_OK && retval != Z_BUF_ERROR &&
00157 !(retval == Z_DATA_ERROR && mode == Inflate
00158 && ignore_decompression_errors))
00159 {
00160 seterror("error %s during gzip %s: %s", retval,
00161 mode == Deflate ? "compression" : "decompression",
00162 zstr->msg ? zstr->msg : "unknown");
00163 return false;
00164 }
00165
00166 return true;
00167 }
00168