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