gzipstream.cpp revision 18529e48f4766aa72dc3166bd14d768b79d48c1a
/*
* Zlib-enabled input and output streams
*
* This is a thin wrapper of libz calls, in order
* to provide a simple interface to our developers
* for gzip input and output.
*
* Authors:
* Bob Jamison <rjamison@titan.com>
*
* Copyright (C) 2004
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "gzipstream.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
namespace Inkscape
{
namespace IO
{
//#########################################################################
//# G Z I P I N P U T S T R E A M
//#########################################################################
#define OUT_SIZE 4000
/**
*
*/
loaded(false),
totalIn(0),
totalOut(0),
crc(0),
srcCrc(0),
srcSiz(0),
srcConsumed(0),
srcLen(0),
outputBufPos(0),
outputBufLen(0)
{
}
/**
*
*/
{
close();
if ( srcBuf ) {
}
if ( outputBuf ) {
}
}
/**
* Returns the number of bytes that can be read (or skipped over) from
* this input stream without blocking by the next caller of a method for
* this input stream.
*/
int GzipInputStream::available()
{
return 0;
return outputBufLen - outputBufPos;
}
/**
* Closes this input stream and releases any system resources
* associated with the stream.
*/
void GzipInputStream::close()
{
if (closed)
return;
}
if ( srcBuf ) {
}
if ( outputBuf ) {
}
closed = true;
}
/**
* Reads the next byte of data from the input stream. -1 if EOF
*/
int GzipInputStream::get()
{
int ch = -1;
if (closed) {
// leave return value -1
}
closed=true;
} else {
loaded = true;
if ( outputBufPos >= outputBufLen ) {
// time to read more, if we can
fetchMore();
}
if ( outputBufPos < outputBufLen ) {
}
}
return ch;
}
#define FTEXT 0x01
#define FHCRC 0x02
#define FEXTRA 0x04
#define FNAME 0x08
#define FCOMMENT 0x10
bool GzipInputStream::load()
{
while (true)
{
if (ch<0)
break;
}
{
return false;
}
if (!srcBuf) {
return false;
}
if ( !outputBuf ) {
srcBuf = 0;
return false;
}
outputBufLen = 0; // Not filled in yet
{
*p++ = *iter;
}
int headerLen = 10;
//Magic
//int val = (int)srcBuf[0];
////printf("val:%x\n", val);
//val = (int)srcBuf[1];
////printf("val:%x\n", val);
////Method
//val = (int)srcBuf[2];
////printf("val:%x\n", val);
//flags
////time
//val = (int)srcBuf[4];
//val = (int)srcBuf[5];
//val = (int)srcBuf[6];
//val = (int)srcBuf[7];
////xflags
//val = (int)srcBuf[8];
////OS
//val = (int)srcBuf[9];
// if ( flags & FEXTRA ) {
// headerLen += 2;
// int xlen =
// TODO deal with optional header parts
// }
int cur = 10;
{
cur++;
headerLen++;
}
headerLen++;
}
//printf("srcCrc:%lx\n", srcCrc);
//printf("srcSiz:%lx/%ld\n", srcSiz, srcSiz);
//outputBufLen = srcSiz + srcSiz/100 + 14;
//printf("%x %x\n", data[0], data[dataLen-1]);
{
} else {
}
}
int GzipInputStream::fetchMore()
{
// TODO assumes we aren't called till the buffer is empty
outputBufLen = 0;
outputBufPos = 0;
if ( outputBufLen ) {
}
//printf("crc:%lx\n", crc);
// } else if ( zerr != Z_STREAM_END ) {
// // TODO check to be sure this won't happen for partial end reads
// printf("inflate: Some kind of problem: %d\n", zerr);
}
return zerr;
}
//#########################################################################
//# G Z I P O U T P U T S T R E A M
//#########################################################################
/**
*
*/
{
totalIn = 0;
totalOut = 0;
//Gzip header
//Say it is compressed
//flags
destination.put(0);
//time
destination.put(0);
destination.put(0);
destination.put(0);
destination.put(0);
//xflags
destination.put(0);
//OS code - from zutil.h
//destination.put(OS_CODE);
//apparently, we should not explicitly include zutil.h
destination.put(0);
}
/**
*
*/
{
close();
}
/**
* Closes this output stream and releases any system resources
* associated with this stream.
*/
void GzipOutputStream::close()
{
if (closed)
return;
flush();
//# Send the CRC
for (int n = 0; n < 4; n++)
{
outlong >>= 8;
}
//# send the file length
for (int n = 0; n < 4; n++)
{
outlong >>= 8;
}
destination.close();
closed = true;
}
/**
* Flushes this output stream and forces any buffered output
* bytes to be written out.
*/
void GzipOutputStream::flush()
{
{
return;
}
if (!srcbuf)
{
return;
}
if (!destbuf)
{
return;
}
*p++ = *iter;
{
printf("Some kind of problem\n");
}
//skip the redundant zlib header and checksum
{
}
destination.flush();
}
/**
* Writes the specified byte to this output stream.
*/
{
if (closed)
{
//probably throw an exception here
return -1;
}
//Add char to buffer
totalIn++;
return 1;
}
} // namespace IO
} // namespace Inkscape
//#########################################################################
//# E N D O F F I L E
//#########################################################################
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :