Mark Adler wrote:
Vinay Nagrik wrote:
However, the call to first
inflate gives no error but subsequent calls give data error (-3).
You should look at http://zlib.net/zlib_how.html for an example of how
to use the zlib functions correctly. You are not updating avail_in and
next_in with the newly read input.
mark
Mark,
That site and code over there helped me a lot, but my job remains
unfinished. The code works find if the whole file is to be inflated in
a single function. However, my http stack
implementatin requires me to make function calls. Here is how the
function calls look
like.
main (void).// input/output files and decide the chunk size.
Call fn1(char *buf, FILE* fptr, int size).
fn1(buf, fptr, size) // initializes z_stream and calls fn2(buf, fptr,
size) to read from the
destinatin file so that it could then inflate the data and return to
main.
fn2(buf, fptr, size) // read required number of bytes from fptr in buf
and return.
This code works fine for first and only first inflate. Since the data
is being read in chunk
sizes the second function call fails in inflate with Z_DATA_ERRR.
I have differenct combinations.
1. Initializing everytime/(only one time) when I enter the function.
2. Calling/(not calling) inflateReset/inflateEnd after each call to
function.
None of the combination seems to work.
please help. It is very important to me.
Here is my complete code.I must be making some stupid mistake, which
you can point
out.
For read file you can create any gzip file and kindly select any small
chunk size to read
the file so that many function calls are made from main function.
Thanks.
I look forward to hear from you.
arun
code
#include <stdio.h>
#include "zlib.h"
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int chunksize = 2048;
int readFn(char* inbuf, FILE* fPtr, int size);
int inflateData(char* inbuf, FILE* fPtr, int size);
int main(void)
{
FILE* inFile;
FILE* outFile;
char readBuf[chunksize];
int readLen;
inFile = fopen("gzipImg.jpg.gz", "rb");
if(inFile == NULL) {
printf("Input file could not be opened \n");
exit(1);
}
outFile = fopen("phoneImg.jpg", "w");
if(outFile == NULL) {
printf(" file could not be opened \n");
exit(1);
}
while (( readLen = inflateData(readBuf, inFile, chunksize)) != 0) {
fwrite(readBuf, 1, readLen, outFile);
}
}
int readFn(char* inbuf, FILE* fPtr, int size) {
int len = fread(inbuf, 1, size, fPtr);
return len;
}
int inflateData(char* inbuf, FILE* fPtr, int size) {
int decompressionError;
z_stream c_stream;
int dataLen;
static bool firsttime = true;
int outsize = size/4;
char outbuf[outsize];
if ( firsttime) {
c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
c_stream.opaque = (voidpf)0;
c_stream.next_in = Z_NULL;
c_stream.avail_in = 0;
decompressionError = inflateInit2(&c_stream, 47);
if ( decompressionError != ZK)
return Z_ERRN;
firsttime = false;
}
else {
c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
c_stream.opaque = (voidpf)0;
c_stream.next_in = Z_NULL;
c_stream.avail_in = 0;
decompressionError = inflateInit(&c_stream);
if ( decompressionError != ZK)
return Z_ERRN;
firsttime = false;
}
c_stream.avail_in = readFn(outbuf, fPtr, outsize);
cout << "avail in is " << c_stream.avail_in << endl;
if (c_stream.avail_in == 0)
return 0;
c_stream.next_in = (Byte *)outbuf;
do {
c_stream.next_out = (Byte*)inbuf;
c_stream.avail_out = size;
cout << "avail out is " << c_stream.avail_out << endl;
decompressionError = inflate(&c_stream, Z_NFLUSH);
cout << "error is " << decompressionError << endl; //
Z_DATA_ERRR after first time
dataLen = size - c_stream.avail_out;
cout << "datalen is " << dataLen << endl;
cout << "avail out is " << c_stream.avail_out << endl;
decompressionError = inflateEnd(&c_stream);
cout << "error is " << decompressionError << endl;
} while (c_stream.avail_out ==0);
return dataLen;
}