Hi,

I am trying to use zlib library to use it in a string compression application. Look at the piece of code --

uLongf DestBuffSize=0;
	compress2((Bytef*)CompDataBuff, (uLongf*)&DestBuffSize,(const Bytef*)d1, (uLongf)length, Z_BEST_COMPRESSION);

In this code the variable DestBuffSize will contain the length of the compressed buffer.

But the variable DestBuffSize is not containing the size of the compressed buffer. when looking at the value of DestBuffSize after the execution of this function.

And we're supposed to guess(believe) that you declared AND allocated ALL your buffers correctly?

And we're supposed to guess(believe) that you declared AND allocated ALL your buffers correctly?

I think I have allocated all the buffers properly.
The code is --

char *CompDataBuff = NULL;
	char *final_buffer=NULL;

	int length=strlen(d1);
	uLongf j=0;
	uLongf CompBuffSize = (uLongf)(length + (length * 0.1));
	CompDataBuff = (char *)malloc((CompBuffSize));

After allocating the buffer I do this ---

uLongf DestBuffSize;
	compress2((Bytef*)CompDataBuff, &DestBuffSize,(const Bytef*)d1, (uLongf)length, Z_BEST_COMPRESSION);
	printf("\n compressed string size is(original value) --- %ld\n" ,DestBuffSize);
	j=DestBuffSize;

So I think DestBuffSize will contain the size of the compressed buffer in bytes.

http://www.gzip.org/zlib/manual.html#compress2

int compress2 (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level);
Compresses the source buffer into the destination buffer. The level parameter has the same meaning as in deflateInit. sourceLen is the byte length of the source buffer. Upon entry, destLen is the total size of the destination buffer, which must be at least 0.1% larger than sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.

compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if there was not enough room in the output buffer, Z_STREAM_ERROR if the level parameter is invalid.

a) you don't initialise DestBuffSize (it's an IN and OUT parameter).
b) your calculation is off (for small values, it seems dangerously short)
c) you ignore the return result.

http://www.gzip.org/zlib/manual.html#compress2

a) you don't initialise DestBuffSize (it's an IN and OUT parameter).
b) your calculation is off (for small values, it seems dangerously short)
c) you ignore the return result.

I think what is happening here. string compression is not useful for a small amout of data. It is only useful if the string is repetetive.

And what does that to do with your original question?

Did you fix the bugs I pointed out?
What results did you get?
Can you post your latest code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.