xref: /titanic_44/usr/src/uts/common/zmod/zmod.c (revision ae115bc77f6fcde83175c75b4206dc2e50747966)
17c478bd9Sstevel@tonic-gate /*
2a0b85df4Sstevel  * CDDL HEADER START
3a0b85df4Sstevel  *
4a0b85df4Sstevel  * The contents of this file are subject to the terms of the
5*ae115bc7Smrj  * Common Development and Distribution License (the "License").
6*ae115bc7Smrj  * You may not use this file except in compliance with the License.
7a0b85df4Sstevel  *
8a0b85df4Sstevel  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9a0b85df4Sstevel  * or http://www.opensolaris.org/os/licensing.
10a0b85df4Sstevel  * See the License for the specific language governing permissions
11a0b85df4Sstevel  * and limitations under the License.
12a0b85df4Sstevel  *
13a0b85df4Sstevel  * When distributing Covered Code, include this CDDL HEADER in each
14a0b85df4Sstevel  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15a0b85df4Sstevel  * If applicable, add the following below this CDDL HEADER, with the
16a0b85df4Sstevel  * fields enclosed by brackets "[]" replaced with your own identifying
17a0b85df4Sstevel  * information: Portions Copyright [yyyy] [name of copyright owner]
18a0b85df4Sstevel  *
19a0b85df4Sstevel  * CDDL HEADER END
20a0b85df4Sstevel  */
21a0b85df4Sstevel 
22a0b85df4Sstevel /*
23*ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
307c478bd9Sstevel@tonic-gate #include <sys/zmod.h>
317c478bd9Sstevel@tonic-gate #include <sys/systm.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "zlib.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * Uncompress the buffer 'src' into the buffer 'dst'.  The caller must store
377c478bd9Sstevel@tonic-gate  * the expected decompressed data size externally so it can be passed in.
387c478bd9Sstevel@tonic-gate  * The resulting decompressed size is then returned through dstlen.  This
397c478bd9Sstevel@tonic-gate  * function return Z_OK on success, or another error code on failure.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate int
427c478bd9Sstevel@tonic-gate z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
437c478bd9Sstevel@tonic-gate {
447c478bd9Sstevel@tonic-gate 	z_stream zs;
457c478bd9Sstevel@tonic-gate 	int err;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	bzero(&zs, sizeof (zs));
487c478bd9Sstevel@tonic-gate 	zs.next_in = (uchar_t *)src;
497c478bd9Sstevel@tonic-gate 	zs.avail_in = srclen;
507c478bd9Sstevel@tonic-gate 	zs.next_out = dst;
517c478bd9Sstevel@tonic-gate 	zs.avail_out = *dstlen;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	if ((err = inflateInit(&zs)) != Z_OK)
547c478bd9Sstevel@tonic-gate 		return (err);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
577c478bd9Sstevel@tonic-gate 		(void) inflateEnd(&zs);
587c478bd9Sstevel@tonic-gate 		return (err == Z_OK ? Z_BUF_ERROR : err);
597c478bd9Sstevel@tonic-gate 	}
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	*dstlen = zs.total_out;
627c478bd9Sstevel@tonic-gate 	return (inflateEnd(&zs));
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static const char *const z_errmsg[] = {
667c478bd9Sstevel@tonic-gate 	"need dictionary",	/* Z_NEED_DICT		2  */
677c478bd9Sstevel@tonic-gate 	"stream end",		/* Z_STREAM_END		1  */
687c478bd9Sstevel@tonic-gate 	"",			/* Z_OK			0  */
697c478bd9Sstevel@tonic-gate 	"file error",		/* Z_ERRNO		(-1) */
707c478bd9Sstevel@tonic-gate 	"stream error",		/* Z_STREAM_ERROR	(-2) */
717c478bd9Sstevel@tonic-gate 	"data error",		/* Z_DATA_ERROR		(-3) */
727c478bd9Sstevel@tonic-gate 	"insufficient memory",	/* Z_MEM_ERROR		(-4) */
737c478bd9Sstevel@tonic-gate 	"buffer error",		/* Z_BUF_ERROR		(-5) */
747c478bd9Sstevel@tonic-gate 	"incompatible version"	/* Z_VERSION_ERROR	(-6) */
757c478bd9Sstevel@tonic-gate };
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * Convert a zlib error code into a string error message.
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate const char *
817c478bd9Sstevel@tonic-gate z_strerror(int err)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	int i = Z_NEED_DICT - err;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if (i < 0 || i >= sizeof (z_errmsg) / sizeof (z_errmsg[0]))
867c478bd9Sstevel@tonic-gate 		return ("unknown error");
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	return (z_errmsg[i]);
897c478bd9Sstevel@tonic-gate }
90