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