xref: /freebsd/sys/cddl/contrib/opensolaris/uts/common/zmod/zmod.c (revision a4d5fcadd8285146ee9f46626cc3ab6a23c39e78)
1f0a75d27SPawel Jakub Dawidek /*
2f0a75d27SPawel Jakub Dawidek  * CDDL HEADER START
3f0a75d27SPawel Jakub Dawidek  *
4f0a75d27SPawel Jakub Dawidek  * The contents of this file are subject to the terms of the
5f0a75d27SPawel Jakub Dawidek  * Common Development and Distribution License (the "License").
6f0a75d27SPawel Jakub Dawidek  * You may not use this file except in compliance with the License.
7f0a75d27SPawel Jakub Dawidek  *
8f0a75d27SPawel Jakub Dawidek  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9f0a75d27SPawel Jakub Dawidek  * or http://www.opensolaris.org/os/licensing.
10f0a75d27SPawel Jakub Dawidek  * See the License for the specific language governing permissions
11f0a75d27SPawel Jakub Dawidek  * and limitations under the License.
12f0a75d27SPawel Jakub Dawidek  *
13f0a75d27SPawel Jakub Dawidek  * When distributing Covered Code, include this CDDL HEADER in each
14f0a75d27SPawel Jakub Dawidek  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15f0a75d27SPawel Jakub Dawidek  * If applicable, add the following below this CDDL HEADER, with the
16f0a75d27SPawel Jakub Dawidek  * fields enclosed by brackets "[]" replaced with your own identifying
17f0a75d27SPawel Jakub Dawidek  * information: Portions Copyright [yyyy] [name of copyright owner]
18f0a75d27SPawel Jakub Dawidek  *
19f0a75d27SPawel Jakub Dawidek  * CDDL HEADER END
20f0a75d27SPawel Jakub Dawidek  */
21f0a75d27SPawel Jakub Dawidek 
22f0a75d27SPawel Jakub Dawidek /*
231ba4a712SPawel Jakub Dawidek  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24f0a75d27SPawel Jakub Dawidek  * Use is subject to license terms.
25f0a75d27SPawel Jakub Dawidek  */
26f0a75d27SPawel Jakub Dawidek 
27f0a75d27SPawel Jakub Dawidek #include <sys/types.h>
280ed1d6fbSXin LI #include <sys/cmn_err.h>
290ed1d6fbSXin LI #include <sys/systm.h>
30*a4d5fcadSAlexander Motin #include <sys/kmem.h>
31f0a75d27SPawel Jakub Dawidek #include <sys/zmod.h>
32f0a75d27SPawel Jakub Dawidek 
330ed1d6fbSXin LI #include <contrib/zlib/zlib.h>
340ed1d6fbSXin LI #include <contrib/zlib/zutil.h>
350ed1d6fbSXin LI 
360ed1d6fbSXin LI /*ARGSUSED*/
370ed1d6fbSXin LI static void *
zfs_zcalloc(void * opaque,uint_t items,uint_t size)380ed1d6fbSXin LI zfs_zcalloc(void *opaque, uint_t items, uint_t size)
390ed1d6fbSXin LI {
40*a4d5fcadSAlexander Motin 	void *ptr;
410ed1d6fbSXin LI 
42*a4d5fcadSAlexander Motin 	ptr = malloc((size_t)items * size, M_SOLARIS, M_NOWAIT);
43*a4d5fcadSAlexander Motin 	return ptr;
440ed1d6fbSXin LI }
450ed1d6fbSXin LI 
460ed1d6fbSXin LI /*ARGSUSED*/
470ed1d6fbSXin LI static void
zfs_zcfree(void * opaque,void * ptr)480ed1d6fbSXin LI zfs_zcfree(void *opaque, void *ptr)
490ed1d6fbSXin LI {
500ed1d6fbSXin LI 
51*a4d5fcadSAlexander Motin 	free(ptr, M_SOLARIS);
520ed1d6fbSXin LI }
53f0a75d27SPawel Jakub Dawidek 
54f0a75d27SPawel Jakub Dawidek /*
55f0a75d27SPawel Jakub Dawidek  * Uncompress the buffer 'src' into the buffer 'dst'.  The caller must store
56f0a75d27SPawel Jakub Dawidek  * the expected decompressed data size externally so it can be passed in.
57f0a75d27SPawel Jakub Dawidek  * The resulting decompressed size is then returned through dstlen.  This
58f0a75d27SPawel Jakub Dawidek  * function return Z_OK on success, or another error code on failure.
59f0a75d27SPawel Jakub Dawidek  */
60f0a75d27SPawel Jakub Dawidek int
z_uncompress(void * dst,size_t * dstlen,const void * src,size_t srclen)61f0a75d27SPawel Jakub Dawidek z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
62f0a75d27SPawel Jakub Dawidek {
63f0a75d27SPawel Jakub Dawidek 	z_stream zs;
64f0a75d27SPawel Jakub Dawidek 	int err;
65f0a75d27SPawel Jakub Dawidek 
66f0a75d27SPawel Jakub Dawidek 	bzero(&zs, sizeof (zs));
67f0a75d27SPawel Jakub Dawidek 	zs.next_in = (uchar_t *)src;
68f0a75d27SPawel Jakub Dawidek 	zs.avail_in = srclen;
69f0a75d27SPawel Jakub Dawidek 	zs.next_out = dst;
70f0a75d27SPawel Jakub Dawidek 	zs.avail_out = *dstlen;
710ed1d6fbSXin LI 	zs.zalloc = zfs_zcalloc;
720ed1d6fbSXin LI 	zs.zfree = zfs_zcfree;
73f0a75d27SPawel Jakub Dawidek 
741ba4a712SPawel Jakub Dawidek 	/*
751ba4a712SPawel Jakub Dawidek 	 * Call inflateInit2() specifying a window size of DEF_WBITS
761ba4a712SPawel Jakub Dawidek 	 * with the 6th bit set to indicate that the compression format
771ba4a712SPawel Jakub Dawidek 	 * type (zlib or gzip) should be automatically detected.
781ba4a712SPawel Jakub Dawidek 	 */
791ba4a712SPawel Jakub Dawidek 	if ((err = inflateInit2(&zs, DEF_WBITS | 0x20)) != Z_OK)
80f0a75d27SPawel Jakub Dawidek 		return (err);
81f0a75d27SPawel Jakub Dawidek 
82f0a75d27SPawel Jakub Dawidek 	if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
83f0a75d27SPawel Jakub Dawidek 		(void) inflateEnd(&zs);
84f0a75d27SPawel Jakub Dawidek 		return (err == Z_OK ? Z_BUF_ERROR : err);
85f0a75d27SPawel Jakub Dawidek 	}
86f0a75d27SPawel Jakub Dawidek 
87f0a75d27SPawel Jakub Dawidek 	*dstlen = zs.total_out;
88f0a75d27SPawel Jakub Dawidek 	return (inflateEnd(&zs));
89f0a75d27SPawel Jakub Dawidek }
90f0a75d27SPawel Jakub Dawidek 
91f0a75d27SPawel Jakub Dawidek int
z_compress_level(void * dst,size_t * dstlen,const void * src,size_t srclen,int level)92f0a75d27SPawel Jakub Dawidek z_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen,
93f0a75d27SPawel Jakub Dawidek     int level)
94f0a75d27SPawel Jakub Dawidek {
95f0a75d27SPawel Jakub Dawidek 
96f0a75d27SPawel Jakub Dawidek 	z_stream zs;
97f0a75d27SPawel Jakub Dawidek 	int err;
98f0a75d27SPawel Jakub Dawidek 
99f0a75d27SPawel Jakub Dawidek 	bzero(&zs, sizeof (zs));
100f0a75d27SPawel Jakub Dawidek 	zs.next_in = (uchar_t *)src;
101f0a75d27SPawel Jakub Dawidek 	zs.avail_in = srclen;
102f0a75d27SPawel Jakub Dawidek 	zs.next_out = dst;
103f0a75d27SPawel Jakub Dawidek 	zs.avail_out = *dstlen;
1040ed1d6fbSXin LI 	zs.zalloc = zfs_zcalloc;
1050ed1d6fbSXin LI 	zs.zfree = zfs_zcfree;
106f0a75d27SPawel Jakub Dawidek 
107f0a75d27SPawel Jakub Dawidek 	if ((err = deflateInit(&zs, level)) != Z_OK)
108f0a75d27SPawel Jakub Dawidek 		return (err);
109f0a75d27SPawel Jakub Dawidek 
110f0a75d27SPawel Jakub Dawidek 	if ((err = deflate(&zs, Z_FINISH)) != Z_STREAM_END) {
111f0a75d27SPawel Jakub Dawidek 		(void) deflateEnd(&zs);
112f0a75d27SPawel Jakub Dawidek 		return (err == Z_OK ? Z_BUF_ERROR : err);
113f0a75d27SPawel Jakub Dawidek 	}
114f0a75d27SPawel Jakub Dawidek 
115f0a75d27SPawel Jakub Dawidek 	*dstlen = zs.total_out;
116f0a75d27SPawel Jakub Dawidek 	return (deflateEnd(&zs));
117f0a75d27SPawel Jakub Dawidek }
118f0a75d27SPawel Jakub Dawidek 
119f0a75d27SPawel Jakub Dawidek int
z_compress(void * dst,size_t * dstlen,const void * src,size_t srclen)120f0a75d27SPawel Jakub Dawidek z_compress(void *dst, size_t *dstlen, const void *src, size_t srclen)
121f0a75d27SPawel Jakub Dawidek {
122f0a75d27SPawel Jakub Dawidek 	return (z_compress_level(dst, dstlen, src, srclen,
123f0a75d27SPawel Jakub Dawidek 	    Z_DEFAULT_COMPRESSION));
124f0a75d27SPawel Jakub Dawidek }
125f0a75d27SPawel Jakub Dawidek 
126f0a75d27SPawel Jakub Dawidek /*
127f0a75d27SPawel Jakub Dawidek  * Convert a zlib error code into a string error message.
128f0a75d27SPawel Jakub Dawidek  */
129f0a75d27SPawel Jakub Dawidek const char *
z_strerror(int err)130f0a75d27SPawel Jakub Dawidek z_strerror(int err)
131f0a75d27SPawel Jakub Dawidek {
132f0a75d27SPawel Jakub Dawidek 	int i = Z_NEED_DICT - err;
133f0a75d27SPawel Jakub Dawidek 
134f0a75d27SPawel Jakub Dawidek 	if (i < 0 || i > Z_NEED_DICT - Z_VERSION_ERROR)
135f0a75d27SPawel Jakub Dawidek 		return ("unknown error");
136f0a75d27SPawel Jakub Dawidek 
137f0a75d27SPawel Jakub Dawidek 	return (zError(err));
138f0a75d27SPawel Jakub Dawidek }
139