00001
00002
00003
00004
00005
00006
00007 #include "wvgzip.h"
00008 #include <zlib.h>
00009
00010 #include <assert.h>
00011
00012 #define ZBUFSIZE 10240
00013
00014 WvGzip::WvGzip(GzipMode _mode) : tmpbuf(ZBUFSIZE)
00015 {
00016 int retval;
00017
00018 okay = true;
00019 mode = _mode;
00020 zstr = new z_stream;
00021
00022 memset(zstr, 0, sizeof(*zstr));
00023 zstr->zalloc = Z_NULL;
00024 zstr->zfree = Z_NULL;
00025 zstr->opaque = NULL;
00026
00027 if (mode == Compress)
00028 retval = deflateInit(zstr, Z_DEFAULT_COMPRESSION);
00029 else
00030 retval = inflateInit(zstr);
00031
00032 if (retval != Z_OK)
00033 {
00034 okay = false;
00035 return;
00036 }
00037
00038 zstr->next_in = zstr->next_out = NULL;
00039 zstr->avail_in = zstr->avail_out = 0;
00040 }
00041
00042
00043 WvGzip::~WvGzip()
00044 {
00045 deflateEnd(zstr);
00046 delete zstr;
00047 }
00048
00049
00050 bool WvGzip::isok() const
00051 {
00052 return okay;
00053 }
00054
00055
00056 size_t WvGzip::do_encode(const unsigned char *in, size_t insize, bool flush)
00057 {
00058 assert(!zstr->avail_in && (insize || flush));
00059
00060 int retval;
00061 size_t taken = 0, tmpused;
00062
00063 if (in && !zstr->avail_in)
00064 {
00065 zstr->avail_in = insize;
00066 zstr->next_in = (unsigned char *)in;
00067 }
00068
00069 do
00070 {
00071 if (!zstr->avail_out)
00072 {
00073 tmpbuf.zap();
00074 assert(tmpbuf.free() == ZBUFSIZE);
00075 zstr->avail_out = tmpbuf.free();
00076 zstr->next_out = tmpbuf.alloc(tmpbuf.free());
00077 }
00078
00079 tmpbuf.alloc(tmpbuf.free());
00080 if (mode == Compress)
00081 retval = deflate(zstr, flush ? Z_SYNC_FLUSH : Z_NO_FLUSH);
00082 else
00083 retval = inflate(zstr, flush ? Z_SYNC_FLUSH : Z_NO_FLUSH);
00084 tmpbuf.unalloc(zstr->avail_out);
00085
00086
00087
00088
00089 taken = insize - zstr->avail_in;
00090
00091 tmpused = tmpbuf.used();
00092 outbuf.put(tmpbuf.get(tmpused), tmpused);
00093
00094 fprintf(stderr, "obu: %d\n", outbuf.used());
00095 } while (retval == Z_OK && !zstr->avail_out);
00096
00097 if (retval != Z_OK && retval != Z_BUF_ERROR)
00098 {
00099 fprintf(stderr, "gzip: retval was %d!\n", retval);
00100 okay = false;
00101 }
00102 return taken;
00103 }