xref: /onnv-gate/usr/src/uts/common/fs/zfs/gzip.c (revision 3886:3291401d66a6)
1*3886Sahl /*
2*3886Sahl  * CDDL HEADER START
3*3886Sahl  *
4*3886Sahl  * The contents of this file are subject to the terms of the
5*3886Sahl  * Common Development and Distribution License (the "License").
6*3886Sahl  * You may not use this file except in compliance with the License.
7*3886Sahl  *
8*3886Sahl  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*3886Sahl  * or http://www.opensolaris.org/os/licensing.
10*3886Sahl  * See the License for the specific language governing permissions
11*3886Sahl  * and limitations under the License.
12*3886Sahl  *
13*3886Sahl  * When distributing Covered Code, include this CDDL HEADER in each
14*3886Sahl  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*3886Sahl  * If applicable, add the following below this CDDL HEADER, with the
16*3886Sahl  * fields enclosed by brackets "[]" replaced with your own identifying
17*3886Sahl  * information: Portions Copyright [yyyy] [name of copyright owner]
18*3886Sahl  *
19*3886Sahl  * CDDL HEADER END
20*3886Sahl  */
21*3886Sahl 
22*3886Sahl /*
23*3886Sahl  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*3886Sahl  * Use is subject to license terms.
25*3886Sahl  */
26*3886Sahl 
27*3886Sahl #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*3886Sahl 
29*3886Sahl #include <sys/debug.h>
30*3886Sahl #include <sys/types.h>
31*3886Sahl #include <sys/zmod.h>
32*3886Sahl 
33*3886Sahl #ifdef _KERNEL
34*3886Sahl #include <sys/systm.h>
35*3886Sahl #else
36*3886Sahl #include <strings.h>
37*3886Sahl #endif
38*3886Sahl 
39*3886Sahl size_t
gzip_compress(void * s_start,void * d_start,size_t s_len,size_t d_len,int n)40*3886Sahl gzip_compress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
41*3886Sahl {
42*3886Sahl 	size_t dstlen = d_len;
43*3886Sahl 
44*3886Sahl 	ASSERT(d_len <= s_len);
45*3886Sahl 
46*3886Sahl 	if (z_compress_level(d_start, &dstlen, s_start, s_len, n) != Z_OK) {
47*3886Sahl 		if (d_len != s_len)
48*3886Sahl 			return (s_len);
49*3886Sahl 
50*3886Sahl 		bcopy(s_start, d_start, s_len);
51*3886Sahl 		return (s_len);
52*3886Sahl 	}
53*3886Sahl 
54*3886Sahl 	return (dstlen);
55*3886Sahl }
56*3886Sahl 
57*3886Sahl /*ARGSUSED*/
58*3886Sahl int
gzip_decompress(void * s_start,void * d_start,size_t s_len,size_t d_len,int n)59*3886Sahl gzip_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
60*3886Sahl {
61*3886Sahl 	size_t dstlen = d_len;
62*3886Sahl 
63*3886Sahl 	ASSERT(d_len >= s_len);
64*3886Sahl 
65*3886Sahl 	if (z_uncompress(d_start, &dstlen, s_start, s_len) != Z_OK)
66*3886Sahl 		return (-1);
67*3886Sahl 
68*3886Sahl 	return (0);
69*3886Sahl }
70