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

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;
}