jigdo API
Last update by Admin on 2010-05-23
zstream-bz.hh
Go to the documentation of this file.00001 /* $Id: zstream-bz.hh,v 1.3 2005/04/09 23:09:52 atterer Exp $ -*- C++ -*- 00002 __ _ 00003 |_) /| Copyright (C) 2004-2005 | richard@ 00004 | \/¯| Richard Atterer | atterer.net 00005 ¯ '` ¯ 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License, version 2. See 00008 the file COPYING for details. 00009 00010 */ 00016 #ifndef ZSTREAM_BZ_HH 00017 #define ZSTREAM_BZ_HH 00018 00019 #include <config.h> 00020 00021 #include <bzlib.h> 00022 00023 #include <log.hh> 00024 #include <zstream.hh> 00025 //______________________________________________________________________ 00026 00027 struct ZerrorBz : public Zerror { 00028 ZerrorBz(int s, const string& m) : Zerror(s, m) { } 00029 int status; 00030 }; 00031 //______________________________________________________________________ 00032 00033 class ZobstreamBz : public Zobstream { 00034 public: 00035 inline ZobstreamBz(bostream& s, int level /*= 6*/, 00036 unsigned todoBufSz /*= 256U*/, MD5Sum* md /*= 0*/); 00037 ~ZobstreamBz() { Assert(memReleased); } 00038 00043 void open(bostream& s, int level /*= 6*/, unsigned todoBufSz/* = 256U*/); 00044 00045 protected: 00046 virtual unsigned partId(); 00047 virtual void deflateEnd(); 00048 virtual void deflateReset(); 00049 virtual unsigned totalOut() const { return z.total_out_lo32; } 00050 virtual unsigned totalIn() const { return z.total_in_lo32; } 00051 virtual unsigned availOut() const { return z.avail_out; } 00052 virtual unsigned availIn() const { return z.avail_in; } 00053 virtual byte* nextOut() const { return reinterpret_cast<byte*>(z.next_out); } 00054 virtual byte* nextIn() const { return reinterpret_cast<byte*>(z.next_in); } 00055 virtual void setTotalOut(unsigned n) { 00056 z.total_out_lo32 = n; z.total_out_hi32 = 0; } 00057 virtual void setTotalIn(unsigned n) { 00058 z.total_in_lo32 = n; z.total_in_hi32 = 0; } 00059 virtual void setAvailOut(unsigned n) { z.avail_out = n; } 00060 virtual void setAvailIn(unsigned n) { z.avail_in = n; } 00061 virtual void setNextOut(byte* n) { 00062 z.next_out = reinterpret_cast<char*>(n); } 00063 virtual void setNextIn(byte* n) { 00064 z.next_in = reinterpret_cast<char*>(n); } 00065 virtual void zip2(byte* start, unsigned len, bool finish = false); 00066 00067 private: 00068 bz_stream z; 00069 int compressLevel; 00070 bool memReleased; 00071 }; 00072 //______________________________________________________________________ 00073 00074 class ZibstreamBz : public Zibstream::Impl { 00075 public: 00076 00077 class ZibstreamBzError : public Zerror { 00078 public: 00079 ZibstreamBzError(int s, const string& m) : Zerror(s, m) { } 00080 }; 00081 00082 ZibstreamBz() : status(0), memReleased(true) { } 00083 ~ZibstreamBz() { Assert(memReleased); } 00084 00085 virtual unsigned totalOut() const { return z.total_out_lo32; } 00086 virtual unsigned totalIn() const { return z.total_in_lo32; } 00087 virtual unsigned availOut() const { return z.avail_out; } 00088 virtual unsigned availIn() const { return z.avail_in; } 00089 virtual byte* nextOut() const { return reinterpret_cast<byte*>(z.next_out); } 00090 virtual byte* nextIn() const { return reinterpret_cast<byte*>(z.next_in); } 00091 virtual void setTotalOut(unsigned n) { 00092 z.total_out_lo32 = n; z.total_out_hi32 = 0; } 00093 virtual void setTotalIn(unsigned n) { 00094 z.total_in_lo32 = n; z.total_in_hi32 = 0; } 00095 virtual void setAvailIn(unsigned n) { z.avail_in = n; } 00096 virtual void setNextIn(byte* n) { 00097 z.next_in = reinterpret_cast<char*>(n); } 00098 00099 virtual void init() { 00100 z.bzalloc = 0; 00101 z.bzfree = 0; 00102 z.opaque = 0; 00103 status = BZ2_bzDecompressInit(&z, 0/*silent*/, 0/*fast*/); 00104 if (ok()) memReleased = false; 00105 } 00106 virtual void end() { status = BZ2_bzDecompressEnd(&z); memReleased = true; } 00107 virtual void reset() { 00108 end(); 00109 if (status == BZ_OK) init(); 00110 } 00111 00112 virtual void inflate(byte** nextOut, unsigned* availOut) { 00113 z.next_out = reinterpret_cast<char*>(*nextOut); z.avail_out = *availOut; 00114 status = BZ2_bzDecompress(&z); 00115 *nextOut = reinterpret_cast<byte*>(z.next_out); *availOut = z.avail_out; 00116 } 00117 virtual bool streamEnd() const { return status == BZ_STREAM_END; } 00118 virtual bool ok() const { return status == BZ_OK; } 00119 00120 static const char* bzerrorstrings[]; 00121 virtual void throwError() const { 00122 int s = status; 00123 if (s > 0) s = 0; 00124 throw ZibstreamBzError(status, bzerrorstrings[s*-1]); 00125 } 00126 private: 00127 int status; 00128 bz_stream z; 00129 bool memReleased; 00130 }; 00131 //====================================================================== 00132 00133 ZobstreamBz::ZobstreamBz(bostream& s, int level, unsigned todoBufSz, 00134 MD5Sum* md) 00135 : Zobstream(md), memReleased(true) { 00136 z.bzalloc = 0; 00137 z.bzfree = 0; 00138 z.opaque = 0; 00139 open(s, level, todoBufSz); 00140 } 00141 00142 #endif
Generated on Tue Sep 23 14:27:42 2008 for jigdo by
