xref: /onnv-gate/usr/src/uts/common/zmod/zmod.c (revision 3446:5903aece022d)
1420Sstevel /*
2420Sstevel  * CDDL HEADER START
3420Sstevel  *
4420Sstevel  * The contents of this file are subject to the terms of the
5*3446Smrj  * Common Development and Distribution License (the "License").
6*3446Smrj  * You may not use this file except in compliance with the License.
7420Sstevel  *
8420Sstevel  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9420Sstevel  * or http://www.opensolaris.org/os/licensing.
10420Sstevel  * See the License for the specific language governing permissions
11420Sstevel  * and limitations under the License.
12420Sstevel  *
13420Sstevel  * When distributing Covered Code, include this CDDL HEADER in each
14420Sstevel  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15420Sstevel  * If applicable, add the following below this CDDL HEADER, with the
16420Sstevel  * fields enclosed by brackets "[]" replaced with your own identifying
17420Sstevel  * information: Portions Copyright [yyyy] [name of copyright owner]
18420Sstevel  *
19420Sstevel  * CDDL HEADER END
20420Sstevel  */
21420Sstevel 
220Sstevel@tonic-gate /*
23*3446Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/modctl.h>
300Sstevel@tonic-gate #include <sys/zmod.h>
310Sstevel@tonic-gate #include <sys/systm.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include "zlib.h"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate /*
360Sstevel@tonic-gate  * Uncompress the buffer 'src' into the buffer 'dst'.  The caller must store
370Sstevel@tonic-gate  * the expected decompressed data size externally so it can be passed in.
380Sstevel@tonic-gate  * The resulting decompressed size is then returned through dstlen.  This
390Sstevel@tonic-gate  * function return Z_OK on success, or another error code on failure.
400Sstevel@tonic-gate  */
410Sstevel@tonic-gate int
420Sstevel@tonic-gate z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
430Sstevel@tonic-gate {
440Sstevel@tonic-gate 	z_stream zs;
450Sstevel@tonic-gate 	int err;
460Sstevel@tonic-gate 
470Sstevel@tonic-gate 	bzero(&zs, sizeof (zs));
480Sstevel@tonic-gate 	zs.next_in = (uchar_t *)src;
490Sstevel@tonic-gate 	zs.avail_in = srclen;
500Sstevel@tonic-gate 	zs.next_out = dst;
510Sstevel@tonic-gate 	zs.avail_out = *dstlen;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	if ((err = inflateInit(&zs)) != Z_OK)
540Sstevel@tonic-gate 		return (err);
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 	if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
570Sstevel@tonic-gate 		(void) inflateEnd(&zs);
580Sstevel@tonic-gate 		return (err == Z_OK ? Z_BUF_ERROR : err);
590Sstevel@tonic-gate 	}
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	*dstlen = zs.total_out;
620Sstevel@tonic-gate 	return (inflateEnd(&zs));
630Sstevel@tonic-gate }
640Sstevel@tonic-gate 
650Sstevel@tonic-gate static const char *const z_errmsg[] = {
660Sstevel@tonic-gate 	"need dictionary",	/* Z_NEED_DICT		2  */
670Sstevel@tonic-gate 	"stream end",		/* Z_STREAM_END		1  */
680Sstevel@tonic-gate 	"",			/* Z_OK			0  */
690Sstevel@tonic-gate 	"file error",		/* Z_ERRNO		(-1) */
700Sstevel@tonic-gate 	"stream error",		/* Z_STREAM_ERROR	(-2) */
710Sstevel@tonic-gate 	"data error",		/* Z_DATA_ERROR		(-3) */
720Sstevel@tonic-gate 	"insufficient memory",	/* Z_MEM_ERROR		(-4) */
730Sstevel@tonic-gate 	"buffer error",		/* Z_BUF_ERROR		(-5) */
740Sstevel@tonic-gate 	"incompatible version"	/* Z_VERSION_ERROR	(-6) */
750Sstevel@tonic-gate };
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * Convert a zlib error code into a string error message.
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate const char *
810Sstevel@tonic-gate z_strerror(int err)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	int i = Z_NEED_DICT - err;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	if (i < 0 || i >= sizeof (z_errmsg) / sizeof (z_errmsg[0]))
860Sstevel@tonic-gate 		return ("unknown error");
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	return (z_errmsg[i]);
890Sstevel@tonic-gate }
90