xref: /titanic_44/usr/src/uts/common/zmod/zmod.c (revision ee5416c9d7e449233197d5d20bc6b81e4ff091b2)
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 
32 #include "zlib.h"
33 
34 /*
35  * Uncompress the buffer 'src' into the buffer 'dst'.  The caller must store
36  * the expected decompressed data size externally so it can be passed in.
37  * The resulting decompressed size is then returned through dstlen.  This
38  * function return Z_OK on success, or another error code on failure.
39  */
40 int
41 z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
42 {
43 	z_stream zs;
44 	int err;
45 
46 	bzero(&zs, sizeof (zs));
47 	zs.next_in = (uchar_t *)src;
48 	zs.avail_in = srclen;
49 	zs.next_out = dst;
50 	zs.avail_out = *dstlen;
51 
52 	if ((err = inflateInit(&zs)) != Z_OK)
53 		return (err);
54 
55 	if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
56 		(void) inflateEnd(&zs);
57 		return (err == Z_OK ? Z_BUF_ERROR : err);
58 	}
59 
60 	*dstlen = zs.total_out;
61 	return (inflateEnd(&zs));
62 }
63 
64 int
65 z_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen,
66     int level)
67 {
68 
69 	z_stream zs;
70 	int err;
71 
72 	bzero(&zs, sizeof (zs));
73 	zs.next_in = (uchar_t *)src;
74 	zs.avail_in = srclen;
75 	zs.next_out = dst;
76 	zs.avail_out = *dstlen;
77 
78 	if ((err = deflateInit(&zs, level)) != Z_OK)
79 		return (err);
80 
81 	if ((err = deflate(&zs, Z_FINISH)) != Z_STREAM_END) {
82 		(void) deflateEnd(&zs);
83 		return (err == Z_OK ? Z_BUF_ERROR : err);
84 	}
85 
86 	*dstlen = zs.total_out;
87 	return (deflateEnd(&zs));
88 }
89 
90 int
91 z_compress(void *dst, size_t *dstlen, const void *src, size_t srclen)
92 {
93 	return (z_compress_level(dst, dstlen, src, srclen,
94 	    Z_DEFAULT_COMPRESSION));
95 }
96 
97 /*
98  * Convert a zlib error code into a string error message.
99  */
100 const char *
101 z_strerror(int err)
102 {
103 	int i = Z_NEED_DICT - err;
104 
105 	if (i < 0 || i > Z_NEED_DICT - Z_VERSION_ERROR)
106 		return ("unknown error");
107 
108 	return (zError(err));
109 }
110