problem about using zlib to uncompress gzip in memory


 
Thread Tools Search this Thread
Top Forums Programming problem about using zlib to uncompress gzip in memory
# 1  
Old 03-25-2007
problem about using zlib to uncompress gzip in memory

I wrote a function which for uncompressing data for gzip or deflate format using zlib,see followed code;
source param is pointed to the compressed data,len param is the size of compressed data,
dest param is for returning the address which pointed to the uncompressed data;the last gzip param tell the function
if the data is compressed by deflate format or gzip format;

currently,the function only work for uncompressing data which is compressed by deflate format;
a Z_DATA_ERROR would be returned if uncompressing gzip data;

anything did I miss? what's wrong?
any help is appreciated!

debian:~# uname -a
Linux debian 2.6.18-3-686 #1 SMP Thu Nov 23 20:49:23 UTC 2006 i686 GNU/Linux


debian:~# apt-cache show zlib1g-dev
Package: zlib1g-dev
Priority: optional
Section: libdevel
Installed-Size: 580
Maintainer: Mark Brown <broonie@debian.org>
Architecture: i386
Source: zlib
Version: 1:1.2.3-13
Provides: libz-dev
Depends: zlib1g (= 1:1.2.3-13), libc6-dev | libc-dev
Conflicts: zlib1-dev
Filename: pool/main/z/zlib/zlib1g-dev_1.2.3-13_i386.deb
Size: 406222
MD5sum: 144bb30c39b0dfde9c82986eeee27d14
SHA1: 3e1603e995f9fdf432e64bbc850ba8d59f18a4ed
SHA256: cdf5272f6c40abd23ab807135cf2d9a3fe0fc4fc3398861e7aa59287e72450c7
Description: compression library - development
zlib is a library implementing the deflate compression method found
in gzip and PKZIP. This package includes the development support
files.
Tag: devel::library, role::devel-lib, use::compressing


Code:
int inflate_read(char *source,int len,char **dest,int gzip) {
	int ret;
	unsigned have;
	z_stream strm;
	unsigned char out[CHUNK];
	int totalsize = 0;

	/* allocate inflate state */
	strm.zalloc = Z_NULL;
	strm.zfree = Z_NULL;
	strm.opaque = Z_NULL;
	strm.avail_in = 0;
	strm.next_in = Z_NULL;
	
	if(gzip)
		ret = inflateInit2(&strm, 15);
	else
		ret = inflateInit(&strm);
		
	if (ret != Z_OK)
		return ret;

	strm.avail_in = len;
	strm.next_in = source;

	/* run inflate() on input until output buffer not full */
	do {
		strm.avail_out = CHUNK;
		strm.next_out = out;
		ret = inflate(&strm, Z_NO_FLUSH);
		assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
		switch (ret) {
		case Z_NEED_DICT:
			ret = Z_DATA_ERROR;     /* and fall through */
		case Z_DATA_ERROR:
		case Z_MEM_ERROR:
			inflateEnd(&strm);
			return ret;
		}
		have = CHUNK - strm.avail_out;
		totalsize += have;
		*dest = realloc(*dest,totalsize);
		memcpy(*dest + totalsize - have,out,have);
	} while (strm.avail_out == 0);

	/* clean up and return */
	(void)inflateEnd(&strm);
	return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Problem compiling glib using static zlib

Hello I have been trying to compile glib 2.28 with needs Zlib. During the compilation process I received these messages Then giving a look at the configure of Zlib, there was an option to static link it and I obviously used it. So I think I can solve it by compiling a shared zlib, or by... (4 Replies)
Discussion started by: colt
4 Replies

2. Programming

Uncompress a gzip and bzip file using java on unix solaris environment

Hi, I need to uncompress a gzip and bzip file using java on unix solaris environment. I also need to retreive the header information of the file inorder to differentiate between gzip and bzip file. Please help Pooja (0 Replies)
Discussion started by: wadhwa.pooja
0 Replies

3. Shell Programming and Scripting

tar and gzip problem

Hi Guys, I have a few files. i want to tar these files and zip it using gzip it. -rw-r----- 1 magesh magesh 12940369 Jul 27 09:26 dcx_imds_c.asc -rw-r----- 1 magesh magesh 1221391 Jul 27 09:27 dcx_imds_h.asc -rw-r----- 1 magesh magesh 1105673 Jul 27 09:27... (6 Replies)
Discussion started by: mac4rfree
6 Replies

4. Shell Programming and Scripting

gzip problem

Hi, I am quite new to unix. I need to do the following using gzip. a file suppose abc.gz is in the directory. I need to uncompress it and rename the uncompressed file to ecoes.dat. can anyon please help me in this regard?? (4 Replies)
Discussion started by: UBD
4 Replies

5. UNIX for Advanced & Expert Users

How to reduce GZIP memory usage

I am using the ZLIB_VERSION "1.2.3" . The memory requirement for Zlib/GZIP compression is stated as /* The memory requirements for deflate are (in bytes): (1 << (windowBits+2)) + (1 << (memLevel+9)) that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) ... (0 Replies)
Discussion started by: Parmod Garg
0 Replies

6. UNIX for Advanced & Expert Users

GZIP memory constraints

Currently I am using the ZLIB_VERSION "1.2.3" . The memory requirement for Zlib/GZIP compression is stated as /* The memory requirements for deflate are (in bytes): (1 << (windowBits+2)) + (1 << (memLevel+9)) that is: 128K for windowBits=15 + 128K for memLevel = 8 (default... (0 Replies)
Discussion started by: Parmod Garg
0 Replies

7. Shell Programming and Scripting

gzip problem

Hi All, I am zipping a file using gzip -cq XYZ.txt > XYZ.txt.gz command. file got created with name XYZ.txt.gz but when i execute the following command i am getting errors, $ gzip -t XYZ.txt.gz gzip: XYZ.txt.gz: invalid compressed data--crc error gzip: XYZ.txt.gz: invalid... (6 Replies)
Discussion started by: rkrgarlapati
6 Replies

8. Programming

uncompress of zlib

When I gzopen & gzread from a gzip file, it works OK. But I when I try to uncompress the same data from memory (either by reading to memory with fread or mmap()ing) using decompress, I get Z_DATA_ERROR. Is it because gzip file has some kind of headers that uncompress doesn't want? How can I get... (3 Replies)
Discussion started by: rayne
3 Replies

9. Programming

how to round up a memory address(memory alignment problem)

Hi, I try to marshal a unsigned int and a char * into a buffer, and then unmarshal them later to get them out. I need to put the char * in the front and unsigned int at the end of the buffer. However, my system always give me "BUS ERROR". I am using Sun Sparcs Sloris 2.10. My code to marshal... (6 Replies)
Discussion started by: nj302
6 Replies

10. UNIX for Advanced & Expert Users

gzip or FTP problem?

I hope someone can help me on this little problem. Bit of background first. I've dumped an Oracle database to a plain text file that I wish to gzip then transfer via ftp to another machine. The dump and gzip runs fine and if I gunzip the file while still in unix the file looks fine. The... (2 Replies)
Discussion started by: DGM
2 Replies
Login or Register to Ask a Question