1210008Srdivacky/*
2210008Srdivacky * CDDL HEADER START
3210008Srdivacky *
4210008Srdivacky * The contents of this file are subject to the terms of the
5210008Srdivacky * Common Development and Distribution License (the "License").
6210008Srdivacky * You may not use this file except in compliance with the License.
7210008Srdivacky *
8210008Srdivacky * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9210008Srdivacky * or http://www.opensolaris.org/os/licensing.
10221345Sdim * See the License for the specific language governing permissions
11210008Srdivacky * and limitations under the License.
12210008Srdivacky *
13210008Srdivacky * When distributing Covered Code, include this CDDL HEADER in each
14210008Srdivacky * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15210008Srdivacky * If applicable, add the following below this CDDL HEADER, with the
16210008Srdivacky * fields enclosed by brackets "[]" replaced with your own identifying
17210008Srdivacky * information: Portions Copyright [yyyy] [name of copyright owner]
18210008Srdivacky *
19263508Sdim * CDDL HEADER END
20263508Sdim */
21210008Srdivacky
22210008Srdivacky/*
23263508Sdim * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24263508Sdim * Use is subject to license terms.
25210008Srdivacky */
26210008Srdivacky
27210008Srdivacky/*
28210008Srdivacky * Portions Copyright 2022 Mikhail Zakharov <zmey20000@yahoo.com>
29210008Srdivacky */
30210008Srdivacky
31212904Sdim#include <contrib/zlib/zlib.h>
32210008Srdivacky#include <contrib/zlib/zutil.h>
33218893Sdim
34210008Srdivackystatic void *
35263508Sdimzfs_zcalloc(void *opaque __unused, uint_t items, uint_t size)
36263508Sdim{
37251662Sdim
38251662Sdim	return (calloc(items, size));
39251662Sdim}
40251662Sdim
41251662Sdimstatic void
42251662Sdimzfs_zcfree(void *opaque __unused, void *ptr)
43263508Sdim{
44251662Sdim	free(ptr);
45251662Sdim}
46251662Sdim
47251662Sdim/*
48239462Sdim * Uncompress the buffer 'src' into the buffer 'dst'.  The caller must store
49243830Sdim * the expected decompressed data size externally so it can be passed in.
50243830Sdim * The resulting decompressed size is then returned through dstlen.  This
51243830Sdim * function return Z_OK on success, or another error code on failure.
52239462Sdim */
53263508Sdimstatic inline int
54263508Sdimz_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
55243830Sdim{
56243830Sdim	z_stream zs;
57243830Sdim	int err;
58243830Sdim
59263508Sdim	bzero(&zs, sizeof (zs));
60263508Sdim	zs.next_in = (unsigned char *)src;
61263508Sdim	zs.avail_in = srclen;
62263508Sdim	zs.next_out = dst;
63263508Sdim	zs.avail_out = *dstlen;
64212904Sdim	zs.zalloc = zfs_zcalloc;
65212904Sdim	zs.zfree = zfs_zcfree;
66212904Sdim
67243830Sdim	/*
68212904Sdim	 * Call inflateInit2() specifying a window size of DEF_WBITS
69263508Sdim	 * with the 6th bit set to indicate that the compression format
70263508Sdim	 * type (zlib or gzip) should be automatically detected.
71249423Sdim	 */
72263508Sdim	if ((err = inflateInit2(&zs, DEF_WBITS | 0x20)) != Z_OK)
73263508Sdim		return (err);
74263508Sdim
75263508Sdim	if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
76263508Sdim		(void) inflateEnd(&zs);
77263508Sdim		return (err == Z_OK ? Z_BUF_ERROR : err);
78263508Sdim	}
79263508Sdim
80263508Sdim	*dstlen = zs.total_out;
81263508Sdim	return (inflateEnd(&zs));
82263508Sdim}
83263508Sdim
84263508Sdimstatic int
85263508Sdimgzip_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len,
86263508Sdim    int n __unused)
87263508Sdim{
88263508Sdim	size_t dstlen = d_len;
89263508Sdim
90263508Sdim	ASSERT(d_len >= s_len);
91263508Sdim
92263508Sdim	if (z_uncompress(d_start, &dstlen, s_start, s_len) != Z_OK)
93263508Sdim		return (-1);
94263508Sdim
95263508Sdim	return (0);
96263508Sdim}
97263508Sdim