473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

zlib question (compression/uncompression fails) - demo atatched

Slightly OT, but can't find a zlib specific ng - so hopefuly, someone
can point out why uncompressed strings are not matching the original
strings (lots of strange characters at end of string).

Heres a little prog that demonstrates the problem:

//include zlib header etc

int main(int argc, char * argv[])
{
unsigned long compressed_str_len, srclen ;
void * src_buffer = 0, * dest_buffer = 0;
void * compressed_str = 0, * uncompressed_str = 0 ;
char * test_string = 0;
uLongf temp, dest_buffer_len, uncompressed_str_len ;

if (argc < 1)
return -1 ;

test_string = argv[1];

// COMPRESSION

//get the size of the input
srclen = strlen(test_string) ;

//zlib states that the source buffer must be at least 0.1
//times larger than the source buffer plus 12 bytes
//to cope with the overhead of zlib data streams
temp = (uLongf)(srclen + (srclen * 0.1) + 12);
dest_buffer = calloc((size_t)(temp), sizeof(char));

src_buffer = test_string ;

//now compress the data
compress2((Bytef*)dest_buffer, (uLongf*)&dest_buffer_len,
(const Bytef*)src_buffer, (uLongf)srclen, Z_BEST_COMPRESSION);

//write the compressed data to disk
//fwrite(dest_buffer, dest_buffer_len, 1, FileOut);
printf("String to compress: '%s'\n", test_string );
printf("Length of string to compress: %d\n", strlen(test_string) ) ;

printf("Compressed string: '%s'\n", dest_buffer );
printf("Length of compressed string: %d\n", dest_buffer_len ) ;
// DECOMPRESSION
compressed_str_len = dest_buffer_len ;

//read in the contents of the file into the source buffer
compressed_str = dest_buffer ;

//allocate a buffer big enough to hold the uncompressed data, we can
cheat here
//because we know the size of the original
uncompressed_str_len = strlen(test_string);
uncompressed_str = calloc(uncompressed_str_len, sizeof(char));
//all data we require is ready so compress it into the source buffer,
the exact
//size will be stored in uncompressed_str_len
uncompress((Bytef*)uncompressed_str, &uncompressed_str_len, (const
Bytef*)compressed_str, compressed_str_len );

printf("\nString to uncompress: '%s'\n", compressed_str);
printf("Length of string to uncompress: %d\n", compressed_str_len ) ;

printf("UnCompressed string: '%s'\n", uncompressed_str );
printf("Length of Uncompressed string: %d\n", uncompressed_str_len ) ;

printf("\nCompression/Uncompression result: %s\n", strcmp(test_string,
(char*)uncompressed_str) == 0 ? "SUCCESS" : "FAIL" );

free(dest_buffer);
free(uncompressed_str);

return 0;
}
This always returns FAIL - because the strings don't match (uncompressed
string contains wierd chars after original string - why does this
happen, and how can I get rid of it without resorting to memcpy or strncpy?
Aug 19 '07 #1
4 2853
Anonymous wrote:
>
Slightly OT, but can't find a zlib specific ng - so hopefuly, someone
can point out why uncompressed strings are not matching the original
strings (lots of strange characters at end of string).

Heres a little prog that demonstrates the problem:

//include zlib header etc
I can't compile your code.
Is it possible to post the relevant parts of zlib.h ?
unsigned long compressed_str_len, srclen ;
uLongf temp, dest_buffer_len, uncompressed_str_len ;
//zlib states that the source buffer must be at least 0.1
//times larger than the source buffer plus 12 bytes
//to cope with the overhead of zlib data streams
temp = (uLongf)(srclen + (srclen * 0.1) + 12);
If temp is an integer type,
then your code does not do what your comment says it must do.

((int)(9 + 9 * 0.1)) is not greater than 9.

--
pete
Aug 19 '07 #2
Anonymous wrote:
Slightly OT, but can't find a zlib specific ng - so hopefuly, someone
can point out why uncompressed strings are not matching the original
strings (lots of strange characters at end of string).
(fx:big-snip)
This always returns FAIL - because the strings don't match (uncompressed
string contains wierd chars after original string - why does this
happen, and how can I get rid of it without resorting to memcpy or
strncpy?
I'd lay odds that the result from zlib decompression /is not a string/,
ie, it's not null-terminated. (How could it be? zlib surely isn't
limited to compressing text data, and non-text data could easily have
null bytes in it. That's why you have to pass in a length on compressing,
and provide a length result parameter when uncompressing, yes?) So
your checking that the result is OK is wrong: you should not use
`strcmp`.
how can I get rid of it without resorting to memcpy or strncpy?
How about using `memcmp`?

--
Zedgehog
"I just wonder when we're going to have to sit down and re-evaluate
our decision-making paradigm." /Sahara/

Aug 19 '07 #3

"Chris Dollin" <eh@electrichedgehog.netwrote in message
news:8p*******************@fe1.news.blueyonder.co. uk...
I'd lay odds that the result from zlib decompression /is not a string/,
ie, it's not null-terminated.
He's calling zlib with the string address, correctly, but then with the
length of the string, incorrectly. It should be length plus one.
zlib decompresses, but isn't clever enough to add a nul on the end. Hnece
the string is unterminated and C sees string plus random garbage.

Crashingly simple, like most bugs.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 19 '07 #4
"Malcolm McLean" <re*******@btinternet.comwrites:
"Chris Dollin" <eh@electrichedgehog.netwrote in message
news:8p*******************@fe1.news.blueyonder.co. uk...
>I'd lay odds that the result from zlib decompression /is not a string/,
ie, it's not null-terminated.
He's calling zlib with the string address, correctly, but then with
the length of the string, incorrectly. It should be length plus one.
zlib decompresses, but isn't clever enough to add a nul on the
end. Hnece the string is unterminated and C sees string plus random
garbage.
Either that, or he needs to pass the length of the string and then
re-append the '\0' after decompression. It's hard to tell which
approach makes more sense.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 19 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alan Toppen | last post by:
I was unable to use the ZipFile class in the zipfile module in Python2.4. I got an error that zlib could not be found. Comparing my Python 2.2 installation I noticed Python 2.4 was missing a...
1
by: MuZZy | last post by:
Hello, I am pretty new to .NET programming and probably my question has an obvious answer: I am starting to port an existing c++ application to c#.NET The first problem i am facing is that the...
1
by: Dennis Powell | last post by:
Does anyone have a successful implementaion of the zlib.dll in VB. Net they can show me. I'm writting a class encaplsulating zlib functionality and I keep getting a System.NullReferenceException...
1
by: Leif Wessman | last post by:
I enabled automatic gzip compression with the following lines in ..htaccess: php_value zlib.output_compression On php_value zlib.output_compression_level 5 The problem is that the...
1
by: chris.atlee | last post by:
I'm writing a program in python that creates tar files of a certain maximum size (to fit onto CD/DVD). One of the problems I'm running into is that when using compression, it's pretty much...
0
by: Jan Prochazka | last post by:
Hi, I need to decompress zip archive. I wrote a parser of zip file, i obtain the compressed data, but when i call zlib.decompress(data) on them, it throws this error: decbuf =...
0
by: Jan Prochazka | last post by:
Hi, I need to decompress zip archive. I wrote a parser of zip file, i obtain the compressed data, but when i call zlib.decompress(data) on them, it throws this error: decbuf =...
1
by: DLPnet | last post by:
Hello all, I m not sure if it is the good newsgroup, please fu to the correct one if you know (already tried comp.compression with no answer) I m really knew to the use of the zlib in C++ to...
5
by: tombrogan3 | last post by:
Hi, I need to implement in-memory zlib compression in c# to replace an old c++ app. Pre-requisites.. 1) The performance must be FAST (with source memory sizes from a few k to a meg). 2) The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.