strigi/src/streams
gzipcompressstream.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "gzipcompressstream.h"
00021 #include <zlib.h>
00022 #include <iostream>
00023
00024 using namespace Strigi;
00025 using namespace std;
00026
00027 GZipCompressInputStream::GZipCompressInputStream(InputStream* input, int level) {
00028
00029 m_status = Ok;
00030 zstream = 0;
00031 if ( level < 0 || level > 9 ) {
00032 level = Z_DEFAULT_COMPRESSION;
00033 }
00034
00035 this->input = input;
00036
00037
00038 zstream = (z_stream_s*)malloc(sizeof(z_stream_s));
00039 zstream->zalloc = Z_NULL;
00040 zstream->zfree = Z_NULL;
00041 zstream->opaque = Z_NULL;
00042 zstream->avail_in = 0;
00043
00044
00045 int r = deflateInit(zstream, level);
00046 if (r != Z_OK) {
00047 m_error = "Error initializing GZipCompressInputStream.";
00048 dealloc();
00049 m_status = Error;
00050 return;
00051 }
00052
00053
00054 zstream->avail_out = 1;
00055 }
00056 GZipCompressInputStream::~GZipCompressInputStream() {
00057 dealloc();
00058 }
00059 void
00060 GZipCompressInputStream::dealloc() {
00061 if (zstream) {
00062 deflateEnd(zstream);
00063 free(zstream);
00064 zstream = 0;
00065 }
00066 }
00067 void
00068 GZipCompressInputStream::readFromStream() {
00069
00070 const char* inStart;
00071 int32_t nread;
00072 nread = input->read(inStart, 1, 0);
00073 if (nread < -1) {
00074 m_status = Error;
00075 m_error = input->error();
00076 } else if (nread < 1) {
00077 zstream->avail_in = 0;
00078 } else {
00079 zstream->next_in = (Bytef*)inStart;
00080 zstream->avail_in = nread;
00081 }
00082 }
00083 int32_t
00084 GZipCompressInputStream::fillBuffer(char* start, int32_t space) {
00085 cerr << "GZCI " << this << " " << zstream << endl;
00086 if (zstream == 0) return -1;
00087
00088
00089 zstream->avail_out = space;
00090 zstream->next_out = (Bytef*)start;
00091
00092 int r;
00093
00094 if (zstream->avail_in == 0) {
00095 readFromStream();
00096 if (m_status == Error) {
00097 cerr << "error " << endl;
00098
00099 return -1;
00100 } else if (zstream->avail_in == 0) {
00101 r = deflate(zstream, Z_FINISH);
00102 int32_t nwritten = space - zstream->avail_out;
00103 cerr << "GZCI end " << this << " " << nwritten << " " << m_status
00104 << endl;
00105 if (r != Z_OK) {
00106 cerr << "GZCI streamend " << r << endl;
00107 dealloc();
00108 if (r != Z_STREAM_END) {
00109 fprintf(stderr, "deflate should report Z_STREAM_END\n");
00110 return -1;
00111 }
00112 }
00113 return nwritten;
00114 }
00115 }
00116 r = deflate(zstream, Z_NO_FLUSH);
00117
00118 int32_t nwritten = space - zstream->avail_out;
00119 switch (r) {
00120 case Z_NEED_DICT:
00121 m_error = "Z_NEED_DICT while inflating stream.";
00122 m_status = Error;
00123 break;
00124 case Z_DATA_ERROR:
00125 m_error = "Z_DATA_ERROR while inflating stream.";
00126 m_status = Error;
00127 break;
00128 case Z_MEM_ERROR:
00129 m_error = "Z_MEM_ERROR while inflating stream.";
00130 m_status = Error;
00131 break;
00132 }
00133 cerr << "GZCI more " << this << " " << nwritten << endl;
00134 return nwritten;
00135 }