I am trying to implement the "zlib.h" deflate and inflate functions to compress and decompress streams in PDF-file. Input: compressed stream from PDF-file. I implemented inflate function -- it's all right, I have uncopressed stream, after that I try to compress this stream again with deflate function, as output I have compressed stream, but it is not equal to input compressed stream and they are not equal to the length. What I'm doing wrong? This is a part of my code:
size_t outsize = (streamend - streamstart) * 10;
char* output = new char[outsize]; ZeroMemory(output, outsize);
z_stream zstrm; ZeroMemory(&zstrm, sizeof(zstrm));
zstrm.avail_in = streamend - streamstart + 1;
zstrm.avail_out = outsize;
zstrm.next_in = (Bytef*)(buffer + streamstart);//block of date to infalte
zstrm.next_out = (Bytef*)output;
int rsti = inflateInit(&zstrm);
if (rsti == Z_OK)
{
int rst2 = inflate(&zstrm, Z_FINISH);
if (rst2 >= 0)
{
cout << output << endl;//inflated data
}
}
char* deflate_output = new char[streamend - streamstart];
ZeroMemory(deflate_output, streamend - streamstart);
z_stream d_zstrm; ZeroMemory(&d_zstrm, sizeof(d_zstrm));
d_zstrm.avail_in = (uInt) (strlen(output)+1);
d_zstrm.avail_out = (uInt) (streamend - streamstart);
d_zstrm.next_in = (Bytef*)(output);
d_zstrm.next_out = (Bytef*)(deflate_output);
int rsti1 = deflateInit(&d_zstrm, Z_DEFAULT_COMPRESSION);
if (rsti1 == Z_OK)
{
int rst22 = deflate(&d_zstrm, Z_FINISH);
out << deflate_output << endl << "**********************" << endl;
//I try to write deflated stream to file
printf("New size of stream: %lu\n", (char*)d_zstrm.next_out - deflate_output);
}
"aaaa123456789aaa"
. There are two possible pattern matches for the final"aaa"
and either is correct. – Weather Vane