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 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #include <sys/types.h>
28 #include <sys/cmn_err.h>
29 #include <sys/systm.h>
30 #include <sys/kmem.h>
31 #include <sys/zmod.h>
32
33 #include <contrib/zlib/zlib.h>
34 #include <contrib/zlib/zutil.h>
35
36 /*ARGSUSED*/
37 static void *
zfs_zcalloc(void * opaque,uint_t items,uint_t size)38 zfs_zcalloc(void *opaque, uint_t items, uint_t size)
39 {
40 void *ptr;
41
42 ptr = malloc((size_t)items * size, M_SOLARIS, M_NOWAIT);
43 return ptr;
44 }
45
46 /*ARGSUSED*/
47 static void
zfs_zcfree(void * opaque,void * ptr)48 zfs_zcfree(void *opaque, void *ptr)
49 {
50
51 free(ptr, M_SOLARIS);
52 }
53
54 /*
55 * Uncompress the buffer 'src' into the buffer 'dst'. The caller must store
56 * the expected decompressed data size externally so it can be passed in.
57 * The resulting decompressed size is then returned through dstlen. This
58 * function return Z_OK on success, or another error code on failure.
59 */
60 int
z_uncompress(void * dst,size_t * dstlen,const void * src,size_t srclen)61 z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
62 {
63 z_stream zs;
64 int err;
65
66 bzero(&zs, sizeof (zs));
67 zs.next_in = (uchar_t *)src;
68 zs.avail_in = srclen;
69 zs.next_out = dst;
70 zs.avail_out = *dstlen;
71 zs.zalloc = zfs_zcalloc;
72 zs.zfree = zfs_zcfree;
73
74 /*
75 * Call inflateInit2() specifying a window size of DEF_WBITS
76 * with the 6th bit set to indicate that the compression format
77 * type (zlib or gzip) should be automatically detected.
78 */
79 if ((err = inflateInit2(&zs, DEF_WBITS | 0x20)) != Z_OK)
80 return (err);
81
82 if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
83 (void) inflateEnd(&zs);
84 return (err == Z_OK ? Z_BUF_ERROR : err);
85 }
86
87 *dstlen = zs.total_out;
88 return (inflateEnd(&zs));
89 }
90
91 int
z_compress_level(void * dst,size_t * dstlen,const void * src,size_t srclen,int level)92 z_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen,
93 int level)
94 {
95
96 z_stream zs;
97 int err;
98
99 bzero(&zs, sizeof (zs));
100 zs.next_in = (uchar_t *)src;
101 zs.avail_in = srclen;
102 zs.next_out = dst;
103 zs.avail_out = *dstlen;
104 zs.zalloc = zfs_zcalloc;
105 zs.zfree = zfs_zcfree;
106
107 if ((err = deflateInit(&zs, level)) != Z_OK)
108 return (err);
109
110 if ((err = deflate(&zs, Z_FINISH)) != Z_STREAM_END) {
111 (void) deflateEnd(&zs);
112 return (err == Z_OK ? Z_BUF_ERROR : err);
113 }
114
115 *dstlen = zs.total_out;
116 return (deflateEnd(&zs));
117 }
118
119 int
z_compress(void * dst,size_t * dstlen,const void * src,size_t srclen)120 z_compress(void *dst, size_t *dstlen, const void *src, size_t srclen)
121 {
122 return (z_compress_level(dst, dstlen, src, srclen,
123 Z_DEFAULT_COMPRESSION));
124 }
125
126 /*
127 * Convert a zlib error code into a string error message.
128 */
129 const char *
z_strerror(int err)130 z_strerror(int err)
131 {
132 int i = Z_NEED_DICT - err;
133
134 if (i < 0 || i > Z_NEED_DICT - Z_VERSION_ERROR)
135 return ("unknown error");
136
137 return (zError(err));
138 }
139