1*e50e4068SToomas Soome /*
2*e50e4068SToomas Soome * CDDL HEADER START
3*e50e4068SToomas Soome *
4*e50e4068SToomas Soome * The contents of this file are subject to the terms of the
5*e50e4068SToomas Soome * Common Development and Distribution License (the "License").
6*e50e4068SToomas Soome * You may not use this file except in compliance with the License.
7*e50e4068SToomas Soome *
8*e50e4068SToomas Soome * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*e50e4068SToomas Soome * or http://www.opensolaris.org/os/licensing.
10*e50e4068SToomas Soome * See the License for the specific language governing permissions
11*e50e4068SToomas Soome * and limitations under the License.
12*e50e4068SToomas Soome *
13*e50e4068SToomas Soome * When distributing Covered Code, include this CDDL HEADER in each
14*e50e4068SToomas Soome * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*e50e4068SToomas Soome * If applicable, add the following below this CDDL HEADER, with the
16*e50e4068SToomas Soome * fields enclosed by brackets "[]" replaced with your own identifying
17*e50e4068SToomas Soome * information: Portions Copyright [yyyy] [name of copyright owner]
18*e50e4068SToomas Soome *
19*e50e4068SToomas Soome * CDDL HEADER END
20*e50e4068SToomas Soome */
21*e50e4068SToomas Soome
22*e50e4068SToomas Soome /*
23*e50e4068SToomas Soome * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24*e50e4068SToomas Soome * Use is subject to license terms.
25*e50e4068SToomas Soome */
26*e50e4068SToomas Soome
27*e50e4068SToomas Soome /*
28*e50e4068SToomas Soome * Portions Copyright 2022 Mikhail Zakharov <zmey20000@yahoo.com>
29*e50e4068SToomas Soome */
30*e50e4068SToomas Soome
31*e50e4068SToomas Soome #include <contrib/zlib/zlib.h>
32*e50e4068SToomas Soome #include <contrib/zlib/zutil.h>
33*e50e4068SToomas Soome
34*e50e4068SToomas Soome static void *
zfs_zcalloc(void * opaque __unused,uint_t items,uint_t size)35*e50e4068SToomas Soome zfs_zcalloc(void *opaque __unused, uint_t items, uint_t size)
36*e50e4068SToomas Soome {
37*e50e4068SToomas Soome
38*e50e4068SToomas Soome return (calloc(items, size));
39*e50e4068SToomas Soome }
40*e50e4068SToomas Soome
41*e50e4068SToomas Soome static void
zfs_zcfree(void * opaque __unused,void * ptr)42*e50e4068SToomas Soome zfs_zcfree(void *opaque __unused, void *ptr)
43*e50e4068SToomas Soome {
44*e50e4068SToomas Soome free(ptr);
45*e50e4068SToomas Soome }
46*e50e4068SToomas Soome
47*e50e4068SToomas Soome /*
48*e50e4068SToomas Soome * Uncompress the buffer 'src' into the buffer 'dst'. The caller must store
49*e50e4068SToomas Soome * the expected decompressed data size externally so it can be passed in.
50*e50e4068SToomas Soome * The resulting decompressed size is then returned through dstlen. This
51*e50e4068SToomas Soome * function return Z_OK on success, or another error code on failure.
52*e50e4068SToomas Soome */
53*e50e4068SToomas Soome static inline int
z_uncompress(void * dst,size_t * dstlen,const void * src,size_t srclen)54*e50e4068SToomas Soome z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
55*e50e4068SToomas Soome {
56*e50e4068SToomas Soome z_stream zs;
57*e50e4068SToomas Soome int err;
58*e50e4068SToomas Soome
59*e50e4068SToomas Soome bzero(&zs, sizeof (zs));
60*e50e4068SToomas Soome zs.next_in = (unsigned char *)src;
61*e50e4068SToomas Soome zs.avail_in = srclen;
62*e50e4068SToomas Soome zs.next_out = dst;
63*e50e4068SToomas Soome zs.avail_out = *dstlen;
64*e50e4068SToomas Soome zs.zalloc = zfs_zcalloc;
65*e50e4068SToomas Soome zs.zfree = zfs_zcfree;
66*e50e4068SToomas Soome
67*e50e4068SToomas Soome /*
68*e50e4068SToomas Soome * Call inflateInit2() specifying a window size of DEF_WBITS
69*e50e4068SToomas Soome * with the 6th bit set to indicate that the compression format
70*e50e4068SToomas Soome * type (zlib or gzip) should be automatically detected.
71*e50e4068SToomas Soome */
72*e50e4068SToomas Soome if ((err = inflateInit2(&zs, DEF_WBITS | 0x20)) != Z_OK)
73*e50e4068SToomas Soome return (err);
74*e50e4068SToomas Soome
75*e50e4068SToomas Soome if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
76*e50e4068SToomas Soome (void) inflateEnd(&zs);
77*e50e4068SToomas Soome return (err == Z_OK ? Z_BUF_ERROR : err);
78*e50e4068SToomas Soome }
79*e50e4068SToomas Soome
80*e50e4068SToomas Soome *dstlen = zs.total_out;
81*e50e4068SToomas Soome return (inflateEnd(&zs));
82*e50e4068SToomas Soome }
83*e50e4068SToomas Soome
84*e50e4068SToomas Soome static int
gzip_decompress(void * s_start,void * d_start,size_t s_len,size_t d_len,int n __unused)85*e50e4068SToomas Soome gzip_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len,
86*e50e4068SToomas Soome int n __unused)
87*e50e4068SToomas Soome {
88*e50e4068SToomas Soome size_t dstlen = d_len;
89*e50e4068SToomas Soome
90*e50e4068SToomas Soome ASSERT(d_len >= s_len);
91*e50e4068SToomas Soome
92*e50e4068SToomas Soome if (z_uncompress(d_start, &dstlen, s_start, s_len) != Z_OK)
93*e50e4068SToomas Soome return (-1);
94*e50e4068SToomas Soome
95*e50e4068SToomas Soome return (0);
96*e50e4068SToomas Soome }
97