xref: /onnv-gate/usr/src/uts/common/zmod/zmod.c (revision 420:45e68e7aede3)
1*420Sstevel /*
2*420Sstevel  * CDDL HEADER START
3*420Sstevel  *
4*420Sstevel  * The contents of this file are subject to the terms of the
5*420Sstevel  * Common Development and Distribution License, Version 1.0 only
6*420Sstevel  * (the "License").  You may not use this file except in compliance
7*420Sstevel  * with the License.
8*420Sstevel  *
9*420Sstevel  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*420Sstevel  * or http://www.opensolaris.org/os/licensing.
11*420Sstevel  * See the License for the specific language governing permissions
12*420Sstevel  * and limitations under the License.
13*420Sstevel  *
14*420Sstevel  * When distributing Covered Code, include this CDDL HEADER in each
15*420Sstevel  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*420Sstevel  * If applicable, add the following below this CDDL HEADER, with the
17*420Sstevel  * fields enclosed by brackets "[]" replaced with your own identifying
18*420Sstevel  * information: Portions Copyright [yyyy] [name of copyright owner]
19*420Sstevel  *
20*420Sstevel  * CDDL HEADER END
21*420Sstevel  */
22*420Sstevel 
230Sstevel@tonic-gate /*
240Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/modctl.h>
310Sstevel@tonic-gate #include <sys/zmod.h>
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include "zlib.h"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * Uncompress the buffer 'src' into the buffer 'dst'.  The caller must store
380Sstevel@tonic-gate  * the expected decompressed data size externally so it can be passed in.
390Sstevel@tonic-gate  * The resulting decompressed size is then returned through dstlen.  This
400Sstevel@tonic-gate  * function return Z_OK on success, or another error code on failure.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate int
430Sstevel@tonic-gate z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
440Sstevel@tonic-gate {
450Sstevel@tonic-gate 	z_stream zs;
460Sstevel@tonic-gate 	int err;
470Sstevel@tonic-gate 
480Sstevel@tonic-gate 	bzero(&zs, sizeof (zs));
490Sstevel@tonic-gate 	zs.next_in = (uchar_t *)src;
500Sstevel@tonic-gate 	zs.avail_in = srclen;
510Sstevel@tonic-gate 	zs.next_out = dst;
520Sstevel@tonic-gate 	zs.avail_out = *dstlen;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	if ((err = inflateInit(&zs)) != Z_OK)
550Sstevel@tonic-gate 		return (err);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
580Sstevel@tonic-gate 		(void) inflateEnd(&zs);
590Sstevel@tonic-gate 		return (err == Z_OK ? Z_BUF_ERROR : err);
600Sstevel@tonic-gate 	}
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	*dstlen = zs.total_out;
630Sstevel@tonic-gate 	return (inflateEnd(&zs));
640Sstevel@tonic-gate }
650Sstevel@tonic-gate 
660Sstevel@tonic-gate static const char *const z_errmsg[] = {
670Sstevel@tonic-gate 	"need dictionary",	/* Z_NEED_DICT		2  */
680Sstevel@tonic-gate 	"stream end",		/* Z_STREAM_END		1  */
690Sstevel@tonic-gate 	"",			/* Z_OK			0  */
700Sstevel@tonic-gate 	"file error",		/* Z_ERRNO		(-1) */
710Sstevel@tonic-gate 	"stream error",		/* Z_STREAM_ERROR	(-2) */
720Sstevel@tonic-gate 	"data error",		/* Z_DATA_ERROR		(-3) */
730Sstevel@tonic-gate 	"insufficient memory",	/* Z_MEM_ERROR		(-4) */
740Sstevel@tonic-gate 	"buffer error",		/* Z_BUF_ERROR		(-5) */
750Sstevel@tonic-gate 	"incompatible version"	/* Z_VERSION_ERROR	(-6) */
760Sstevel@tonic-gate };
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * Convert a zlib error code into a string error message.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate const char *
820Sstevel@tonic-gate z_strerror(int err)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	int i = Z_NEED_DICT - err;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (i < 0 || i >= sizeof (z_errmsg) / sizeof (z_errmsg[0]))
870Sstevel@tonic-gate 		return ("unknown error");
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	return (z_errmsg[i]);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static struct modlmisc modlmisc = {
930Sstevel@tonic-gate 	&mod_miscops, "RFC 1950 decompression routines"
940Sstevel@tonic-gate };
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static struct modlinkage modlinkage = {
970Sstevel@tonic-gate 	MODREV_1, &modlmisc, NULL
980Sstevel@tonic-gate };
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate int
1010Sstevel@tonic-gate _init(void)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate int
1070Sstevel@tonic-gate _info(struct modinfo *mip)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	return (mod_info(&modlinkage, mip));
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate int
1130Sstevel@tonic-gate _fini(void)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
1160Sstevel@tonic-gate }
117