xref: /onnv-gate/usr/src/uts/common/zmod/zmod.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate #include <sys/modctl.h>
9*0Sstevel@tonic-gate #include <sys/zmod.h>
10*0Sstevel@tonic-gate #include <sys/systm.h>
11*0Sstevel@tonic-gate 
12*0Sstevel@tonic-gate #include "zlib.h"
13*0Sstevel@tonic-gate 
14*0Sstevel@tonic-gate /*
15*0Sstevel@tonic-gate  * Uncompress the buffer 'src' into the buffer 'dst'.  The caller must store
16*0Sstevel@tonic-gate  * the expected decompressed data size externally so it can be passed in.
17*0Sstevel@tonic-gate  * The resulting decompressed size is then returned through dstlen.  This
18*0Sstevel@tonic-gate  * function return Z_OK on success, or another error code on failure.
19*0Sstevel@tonic-gate  */
20*0Sstevel@tonic-gate int
21*0Sstevel@tonic-gate z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
22*0Sstevel@tonic-gate {
23*0Sstevel@tonic-gate 	z_stream zs;
24*0Sstevel@tonic-gate 	int err;
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate 	bzero(&zs, sizeof (zs));
27*0Sstevel@tonic-gate 	zs.next_in = (uchar_t *)src;
28*0Sstevel@tonic-gate 	zs.avail_in = srclen;
29*0Sstevel@tonic-gate 	zs.next_out = dst;
30*0Sstevel@tonic-gate 	zs.avail_out = *dstlen;
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate 	if ((err = inflateInit(&zs)) != Z_OK)
33*0Sstevel@tonic-gate 		return (err);
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate 	if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
36*0Sstevel@tonic-gate 		(void) inflateEnd(&zs);
37*0Sstevel@tonic-gate 		return (err == Z_OK ? Z_BUF_ERROR : err);
38*0Sstevel@tonic-gate 	}
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate 	*dstlen = zs.total_out;
41*0Sstevel@tonic-gate 	return (inflateEnd(&zs));
42*0Sstevel@tonic-gate }
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate static const char *const z_errmsg[] = {
45*0Sstevel@tonic-gate 	"need dictionary",	/* Z_NEED_DICT		2  */
46*0Sstevel@tonic-gate 	"stream end",		/* Z_STREAM_END		1  */
47*0Sstevel@tonic-gate 	"",			/* Z_OK			0  */
48*0Sstevel@tonic-gate 	"file error",		/* Z_ERRNO		(-1) */
49*0Sstevel@tonic-gate 	"stream error",		/* Z_STREAM_ERROR	(-2) */
50*0Sstevel@tonic-gate 	"data error",		/* Z_DATA_ERROR		(-3) */
51*0Sstevel@tonic-gate 	"insufficient memory",	/* Z_MEM_ERROR		(-4) */
52*0Sstevel@tonic-gate 	"buffer error",		/* Z_BUF_ERROR		(-5) */
53*0Sstevel@tonic-gate 	"incompatible version"	/* Z_VERSION_ERROR	(-6) */
54*0Sstevel@tonic-gate };
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate /*
57*0Sstevel@tonic-gate  * Convert a zlib error code into a string error message.
58*0Sstevel@tonic-gate  */
59*0Sstevel@tonic-gate const char *
60*0Sstevel@tonic-gate z_strerror(int err)
61*0Sstevel@tonic-gate {
62*0Sstevel@tonic-gate 	int i = Z_NEED_DICT - err;
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	if (i < 0 || i >= sizeof (z_errmsg) / sizeof (z_errmsg[0]))
65*0Sstevel@tonic-gate 		return ("unknown error");
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	return (z_errmsg[i]);
68*0Sstevel@tonic-gate }
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate static struct modlmisc modlmisc = {
71*0Sstevel@tonic-gate 	&mod_miscops, "RFC 1950 decompression routines"
72*0Sstevel@tonic-gate };
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
75*0Sstevel@tonic-gate 	MODREV_1, &modlmisc, NULL
76*0Sstevel@tonic-gate };
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate int
79*0Sstevel@tonic-gate _init(void)
80*0Sstevel@tonic-gate {
81*0Sstevel@tonic-gate 	return (mod_install(&modlinkage));
82*0Sstevel@tonic-gate }
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate int
85*0Sstevel@tonic-gate _info(struct modinfo *mip)
86*0Sstevel@tonic-gate {
87*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, mip));
88*0Sstevel@tonic-gate }
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate int
91*0Sstevel@tonic-gate _fini(void)
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
94*0Sstevel@tonic-gate }
95