Sponsored Content
Top Forums Programming problem about using zlib to uncompress gzip in memory Post 302112052 by iwishfine on Sunday 25th of March 2007 01:56:14 AM
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;
}

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
ZLIB(3) 						     Library Functions Manual							   ZLIB(3)

NAME
zlib - compression/decompression library SYNOPSIS
[see zlib.h for full description] DESCRIPTION
The zlib library is a general purpose data compression library. The code is thread safe, assuming that the standard library functions used are thread safe, such as memory allocation routines. It provides in-memory compression and decompression functions, including integrity checks of the uncompressed data. This version of the library supports only one compression method (deflation) but other algorithms may be added later with the same stream interface. Compression can be done in a single step if the buffers are large enough or can be done by repeated calls of the compression function. In the latter case, the application must provide more input and/or consume the output (providing more output space) before each call. The library also supports reading and writing files in gzip(1) (.gz) format with an interface similar to that of stdio. The library does not install any signal handler. The decoder checks the consistency of the compressed data, so the library should never crash even in the case of corrupted input. All functions of the compression library are documented in the file zlib.h. The distribution source includes examples of use of the library in the files example.c and minigzip.c, as well as other examples in the examples/ directory. Changes to this version are documented in the file ChangeLog that accompanies the source. zlib is available in Java using the java.util.zip package: http://java.sun.com/developer/technicalArticles/Programming/compression/ A Perl interface to zlib, written by Paul Marquess (pmqs@cpan.org), is available at CPAN (Comprehensive Perl Archive Network) sites, including: http://search.cpan.org/~pmqs/IO-Compress-Zlib/ A Python interface to zlib, written by A.M. Kuchling (amk@magnet.com), is available in Python 1.5 and later versions: http://www.python.org/doc/lib/module-zlib.html zlib is built into tcl: http://wiki.tcl.tk/4610 An experimental package to read and write files in .zip format, written on top of zlib by Gilles Vollant (info@winimage.com), is available at: http://www.winimage.com/zLibDll/minizip.html and also in the contrib/minizip directory of the main zlib source distribution. SEE ALSO
The zlib web site can be found at: http://zlib.net/ The data format used by the zlib library is described by RFC (Request for Comments) 1950 to 1952 in the files: http://www.ietf.org/rfc/rfc1950.txt (for the zlib header and trailer format) http://www.ietf.org/rfc/rfc1951.txt (for the deflate compressed data format) http://www.ietf.org/rfc/rfc1952.txt (for the gzip header and trailer format) Mark Nelson wrote an article about zlib for the Jan. 1997 issue of Dr. Dobb's Journal; a copy of the article is available at: http://marknelson.us/1997/01/01/zlib-engine/ REPORTING PROBLEMS
Before reporting a problem, please check the zlib web site to verify that you have the latest version of zlib; otherwise, obtain the latest version and see if the problem still exists. Please read the zlib FAQ at: http://zlib.net/zlib_faq.html before asking for help. Send questions and/or comments to zlib@gzip.org, or (for the Windows DLL version) to Gilles Vollant (info@winim- age.com). AUTHORS
Version 1.2.5 Copyright (C) 1995-2010 Jean-loup Gailly (jloup@gzip.org) and Mark Adler (madler@alumni.caltech.edu). This software is provided "as-is," without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. See the distribution directory with respect to requirements governing redistribution. The deflate format used by zlib was defined by Phil Katz. The deflate and zlib specifications were written by L. Peter Deutsch. Thanks to all the people who reported problems and suggested various improvements in zlib; who are too numerous to cite here. UNIX manual page by R. P. C. Rodgers, U.S. National Library of Medicine (rodgers@nlm.nih.gov). 19 Apr 2010 ZLIB(3)
All times are GMT -4. The time now is 03:11 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy