xref: /titanic_50/usr/src/uts/common/zmod/zmod.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <sys/modctl.h>
31 #include <sys/zmod.h>
32 #include <sys/systm.h>
33 
34 #include "zlib.h"
35 
36 /*
37  * Uncompress the buffer 'src' into the buffer 'dst'.  The caller must store
38  * the expected decompressed data size externally so it can be passed in.
39  * The resulting decompressed size is then returned through dstlen.  This
40  * function return Z_OK on success, or another error code on failure.
41  */
42 int
43 z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
44 {
45 	z_stream zs;
46 	int err;
47 
48 	bzero(&zs, sizeof (zs));
49 	zs.next_in = (uchar_t *)src;
50 	zs.avail_in = srclen;
51 	zs.next_out = dst;
52 	zs.avail_out = *dstlen;
53 
54 	if ((err = inflateInit(&zs)) != Z_OK)
55 		return (err);
56 
57 	if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
58 		(void) inflateEnd(&zs);
59 		return (err == Z_OK ? Z_BUF_ERROR : err);
60 	}
61 
62 	*dstlen = zs.total_out;
63 	return (inflateEnd(&zs));
64 }
65 
66 static const char *const z_errmsg[] = {
67 	"need dictionary",	/* Z_NEED_DICT		2  */
68 	"stream end",		/* Z_STREAM_END		1  */
69 	"",			/* Z_OK			0  */
70 	"file error",		/* Z_ERRNO		(-1) */
71 	"stream error",		/* Z_STREAM_ERROR	(-2) */
72 	"data error",		/* Z_DATA_ERROR		(-3) */
73 	"insufficient memory",	/* Z_MEM_ERROR		(-4) */
74 	"buffer error",		/* Z_BUF_ERROR		(-5) */
75 	"incompatible version"	/* Z_VERSION_ERROR	(-6) */
76 };
77 
78 /*
79  * Convert a zlib error code into a string error message.
80  */
81 const char *
82 z_strerror(int err)
83 {
84 	int i = Z_NEED_DICT - err;
85 
86 	if (i < 0 || i >= sizeof (z_errmsg) / sizeof (z_errmsg[0]))
87 		return ("unknown error");
88 
89 	return (z_errmsg[i]);
90 }
91 
92 static struct modlmisc modlmisc = {
93 	&mod_miscops, "RFC 1950 decompression routines"
94 };
95 
96 static struct modlinkage modlinkage = {
97 	MODREV_1, &modlmisc, NULL
98 };
99 
100 int
101 _init(void)
102 {
103 	return (mod_install(&modlinkage));
104 }
105 
106 int
107 _info(struct modinfo *mip)
108 {
109 	return (mod_info(&modlinkage, mip));
110 }
111 
112 int
113 _fini(void)
114 {
115 	return (mod_remove(&modlinkage));
116 }
117