1 /* $Id: zstream-gz.hh,v 1.5 2005/04/09 23:09:52 atterer Exp $ -*- C++ -*-
3 |_) /| Copyright (C) 2004-2005 | richard@
4 | \/¯| Richard Atterer | atterer.org
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2. See
8 the file COPYING for details.
12 zlib (gzip-style) compression and decompression for zstream
25 //______________________________________________________________________
27 struct ZerrorGz : public Zerror {
28 ZerrorGz(int s, const string& m) : Zerror(s, m) { }
31 //______________________________________________________________________
33 class ZobstreamGz : public Zobstream {
35 inline ZobstreamGz(bostream& s, unsigned chunkLimit,
36 int level = Z_DEFAULT_COMPRESSION, int windowBits = 15,
37 int memLevel = 8, unsigned todoBufSz = 256U,
39 ~ZobstreamGz() { Assert(memReleased); }
41 /** @param s Output stream
42 @param chunkLimit Size limit for output data, will buffer this much
44 @param windowBits zlib param
45 @param memLevel zlib param
46 @param todoBufSz Size of mini buffer, which holds data sent to
47 the stream with single put() calls or << statements */
48 void open(bostream& s, unsigned chunkLimit, int level =Z_DEFAULT_COMPRESSION,
49 int windowBits = 15, int memLevel = 8, unsigned todoBufSz = 256U);
52 virtual unsigned partId();
53 virtual void deflateEnd();
54 virtual void deflateReset();
55 virtual unsigned totalOut() const { return z.total_out; }
56 virtual unsigned totalIn() const { return z.total_in; }
57 virtual unsigned availOut() const { return z.avail_out; }
58 virtual unsigned availIn() const { return z.avail_in; }
59 virtual byte* nextOut() const { return z.next_out; }
60 virtual byte* nextIn() const { return z.next_in; }
61 virtual void setTotalOut(unsigned n) { z.total_out = n; }
62 virtual void setTotalIn(unsigned n) { z.total_in = n; }
63 virtual void setAvailOut(unsigned n) { z.avail_out = n; }
64 virtual void setAvailIn(unsigned n) { z.avail_in = n; }
65 virtual void setNextOut(byte* n) { z.next_out = n; }
66 virtual void setNextIn(byte* n) { z.next_in = n; }
67 virtual void zip2(byte* start, unsigned len, bool finish = false);
70 // Throw a Zerror exception, or bad_alloc() for status==Z_MEM_ERROR
71 // inline void throwZerror(int status, const char* zmsg);
74 // To keep track in the dtor whether deflateEnd() has been called
77 //______________________________________________________________________
79 class ZibstreamGz : public Zibstream::Impl {
82 class ZibstreamGzError : public Zerror {
84 ZibstreamGzError(int s, const string& m) : Zerror(s, m) { }
87 ZibstreamGz() : status(0), memReleased(true) { }
88 ~ZibstreamGz() { Assert(memReleased); }
90 virtual unsigned totalOut() const { return z.total_out; }
91 virtual unsigned totalIn() const { return z.total_in; }
92 virtual unsigned availOut() const { return z.avail_out; }
93 virtual unsigned availIn() const { return z.avail_in; }
94 virtual byte* nextOut() const { return z.next_out; }
95 virtual byte* nextIn() const { return z.next_in; }
96 virtual void setTotalOut(unsigned n) { z.total_out = n; }
97 virtual void setTotalIn(unsigned n) { z.total_in = n; }
98 virtual void setAvailIn(unsigned n) { z.avail_in = n; }
99 virtual void setNextIn(byte* n) { z.next_in = n; }
101 virtual void init() {
102 //memset(&z, 0, sizeof(z));
103 z.zalloc = (alloc_func)0;
104 z.zfree = (free_func)0;
106 status = inflateInit(&z);
107 if (ok()) memReleased = false;
109 virtual void end() { status = inflateEnd(&z); memReleased = true; }
110 virtual void reset() { status = inflateReset(&z); }
112 virtual void inflate(byte** nextOut, unsigned* availOut) {
113 z.next_out = *nextOut; z.avail_out = *availOut;
114 status = ::inflate(&z, Z_NO_FLUSH);
115 *nextOut = z.next_out; *availOut = z.avail_out;
117 virtual bool streamEnd() const { return status == Z_STREAM_END; }
118 virtual bool ok() const { return status == Z_OK; }
120 virtual void throwError() const { throw ZibstreamGzError(status, z.msg); }
124 // To keep track in the dtor whether deflateEnd() has been called
127 //======================================================================
129 ZobstreamGz::ZobstreamGz(bostream& s, unsigned chunkLimit, int level,
130 int windowBits, int memLevel, unsigned todoBufSz,
131 MD5Sum* md) : Zobstream(md), memReleased(true) {
132 z.zalloc = (alloc_func)0;
133 z.zfree = (free_func)0;
135 open(s, chunkLimit, level, windowBits, memLevel, todoBufSz);