1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * Copyright (c) 2020 iXsystems, Inc.
3eda14cbcSMatt Macy  * All rights reserved.
4eda14cbcSMatt Macy  *
5eda14cbcSMatt Macy  * Redistribution and use in source and binary forms, with or without
6eda14cbcSMatt Macy  * modification, are permitted provided that the following conditions
7eda14cbcSMatt Macy  * are met:
8eda14cbcSMatt Macy  * 1. Redistributions of source code must retain the above copyright
9eda14cbcSMatt Macy  *    notice, this list of conditions and the following disclaimer.
10eda14cbcSMatt Macy  * 2. Redistributions in binary form must reproduce the above copyright
11eda14cbcSMatt Macy  *    notice, this list of conditions and the following disclaimer in the
12eda14cbcSMatt Macy  *    documentation and/or other materials provided with the distribution.
13eda14cbcSMatt Macy  *
14eda14cbcSMatt Macy  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15eda14cbcSMatt Macy  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16eda14cbcSMatt Macy  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17eda14cbcSMatt Macy  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18eda14cbcSMatt Macy  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19eda14cbcSMatt Macy  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20eda14cbcSMatt Macy  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21eda14cbcSMatt Macy  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22eda14cbcSMatt Macy  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23eda14cbcSMatt Macy  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24eda14cbcSMatt Macy  * SUCH DAMAGE.
25eda14cbcSMatt Macy  *
26eda14cbcSMatt Macy  */
27eda14cbcSMatt Macy 
28eda14cbcSMatt Macy #include <sys/types.h>
29eda14cbcSMatt Macy #include <sys/kmem.h>
30eda14cbcSMatt Macy #include <sys/kmem_cache.h>
31eda14cbcSMatt Macy #include <sys/zmod.h>
32eda14cbcSMatt Macy #include <contrib/zlib/zlib.h>
33eda14cbcSMatt Macy #include <sys/kobj.h>
34eda14cbcSMatt Macy 
35eda14cbcSMatt Macy 
36eda14cbcSMatt Macy static void *
zcalloc(void * opaque,uint_t items,uint_t size)37eda14cbcSMatt Macy zcalloc(void *opaque, uint_t items, uint_t size)
38eda14cbcSMatt Macy {
39c03c5b1cSMartin Matuska 	(void) opaque;
40eda14cbcSMatt Macy 	return (malloc((size_t)items*size, M_SOLARIS, M_NOWAIT));
41eda14cbcSMatt Macy }
42eda14cbcSMatt Macy 
43eda14cbcSMatt Macy static void
zcfree(void * opaque,void * ptr)44eda14cbcSMatt Macy zcfree(void *opaque, void *ptr)
45eda14cbcSMatt Macy {
46c03c5b1cSMartin Matuska 	(void) opaque;
47eda14cbcSMatt Macy 	free(ptr, M_SOLARIS);
48eda14cbcSMatt Macy }
49eda14cbcSMatt Macy 
50eda14cbcSMatt Macy static int
zlib_deflateInit(z_stream * stream,int level)51eda14cbcSMatt Macy zlib_deflateInit(z_stream *stream, int level)
52eda14cbcSMatt Macy {
53eda14cbcSMatt Macy 
54eda14cbcSMatt Macy 	stream->zalloc = zcalloc;
55eda14cbcSMatt Macy 	stream->opaque = NULL;
56eda14cbcSMatt Macy 	stream->zfree = zcfree;
57eda14cbcSMatt Macy 
58eda14cbcSMatt Macy 	return (deflateInit(stream, level));
59eda14cbcSMatt Macy }
60eda14cbcSMatt Macy 
61eda14cbcSMatt Macy static int
zlib_deflate(z_stream * stream,int flush)62eda14cbcSMatt Macy zlib_deflate(z_stream *stream, int flush)
63eda14cbcSMatt Macy {
64eda14cbcSMatt Macy 	return (deflate(stream, flush));
65eda14cbcSMatt Macy }
66eda14cbcSMatt Macy 
67eda14cbcSMatt Macy static int
zlib_deflateEnd(z_stream * stream)68eda14cbcSMatt Macy zlib_deflateEnd(z_stream *stream)
69eda14cbcSMatt Macy {
70eda14cbcSMatt Macy 	return (deflateEnd(stream));
71eda14cbcSMatt Macy }
72eda14cbcSMatt Macy 
73eda14cbcSMatt Macy static int
zlib_inflateInit(z_stream * stream)74eda14cbcSMatt Macy zlib_inflateInit(z_stream *stream)
75eda14cbcSMatt Macy {
76eda14cbcSMatt Macy 	stream->zalloc = zcalloc;
77eda14cbcSMatt Macy 	stream->opaque = NULL;
78eda14cbcSMatt Macy 	stream->zfree = zcfree;
79eda14cbcSMatt Macy 
80eda14cbcSMatt Macy 	return (inflateInit(stream));
81eda14cbcSMatt Macy }
82eda14cbcSMatt Macy 
83eda14cbcSMatt Macy static int
zlib_inflate(z_stream * stream,int finish)84eda14cbcSMatt Macy zlib_inflate(z_stream *stream, int finish)
85eda14cbcSMatt Macy {
86eda14cbcSMatt Macy 	return (inflate(stream, finish));
87eda14cbcSMatt Macy }
88eda14cbcSMatt Macy 
89eda14cbcSMatt Macy 
90eda14cbcSMatt Macy static int
zlib_inflateEnd(z_stream * stream)91eda14cbcSMatt Macy zlib_inflateEnd(z_stream *stream)
92eda14cbcSMatt Macy {
93eda14cbcSMatt Macy 	return (inflateEnd(stream));
94eda14cbcSMatt Macy }
95eda14cbcSMatt Macy 
96eda14cbcSMatt Macy /*
97eda14cbcSMatt Macy  * A kmem_cache is used for the zlib workspaces to avoid having to vmalloc
98eda14cbcSMatt Macy  * and vfree for every call.  Using a kmem_cache also has the advantage
99eda14cbcSMatt Macy  * that improves the odds that the memory used will be local to this cpu.
100eda14cbcSMatt Macy  * To further improve things it might be wise to create a dedicated per-cpu
101eda14cbcSMatt Macy  * workspace for use.  This would take some additional care because we then
102eda14cbcSMatt Macy  * must disable preemption around the critical section, and verify that
103eda14cbcSMatt Macy  * zlib_deflate* and zlib_inflate* never internally call schedule().
104eda14cbcSMatt Macy  */
105eda14cbcSMatt Macy static void *
zlib_workspace_alloc(int flags)106eda14cbcSMatt Macy zlib_workspace_alloc(int flags)
107eda14cbcSMatt Macy {
108eda14cbcSMatt Macy 	// return (kmem_cache_alloc(zlib_workspace_cache, flags));
109eda14cbcSMatt Macy 	return (NULL);
110eda14cbcSMatt Macy }
111eda14cbcSMatt Macy 
112eda14cbcSMatt Macy static void
zlib_workspace_free(void * workspace)113eda14cbcSMatt Macy zlib_workspace_free(void *workspace)
114eda14cbcSMatt Macy {
115eda14cbcSMatt Macy 	// kmem_cache_free(zlib_workspace_cache, workspace);
116eda14cbcSMatt Macy }
117eda14cbcSMatt Macy 
118eda14cbcSMatt Macy /*
119eda14cbcSMatt Macy  * Compresses the source buffer into the destination buffer. The level
120eda14cbcSMatt Macy  * parameter has the same meaning as in deflateInit.  sourceLen is the byte
121eda14cbcSMatt Macy  * length of the source buffer. Upon entry, destLen is the total size of the
122eda14cbcSMatt Macy  * destination buffer, which must be at least 0.1% larger than sourceLen plus
123eda14cbcSMatt Macy  * 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
124eda14cbcSMatt Macy  *
125eda14cbcSMatt Macy  * compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
126eda14cbcSMatt Macy  * memory, Z_BUF_ERROR if there was not enough room in the output buffer,
127eda14cbcSMatt Macy  * Z_STREAM_ERROR if the level parameter is invalid.
128eda14cbcSMatt Macy  */
129eda14cbcSMatt Macy int
z_compress_level(void * dest,size_t * destLen,const void * source,size_t sourceLen,int level)130eda14cbcSMatt Macy z_compress_level(void *dest, size_t *destLen, const void *source,
131eda14cbcSMatt Macy     size_t sourceLen, int level)
132eda14cbcSMatt Macy {
133*da5137abSMartin Matuska 	z_stream stream = {0};
134eda14cbcSMatt Macy 	int err;
135eda14cbcSMatt Macy 
136eda14cbcSMatt Macy 	stream.next_in = (Byte *)source;
137eda14cbcSMatt Macy 	stream.avail_in = (uInt)sourceLen;
138eda14cbcSMatt Macy 	stream.next_out = dest;
139eda14cbcSMatt Macy 	stream.avail_out = (uInt)*destLen;
140eda14cbcSMatt Macy 	stream.opaque = NULL;
141eda14cbcSMatt Macy 
142eda14cbcSMatt Macy 	if ((size_t)stream.avail_out != *destLen)
143eda14cbcSMatt Macy 		return (Z_BUF_ERROR);
144eda14cbcSMatt Macy 
145eda14cbcSMatt Macy 	stream.opaque = zlib_workspace_alloc(KM_SLEEP);
146eda14cbcSMatt Macy #if 0
147eda14cbcSMatt Macy 	if (!stream.opaque)
148eda14cbcSMatt Macy 		return (Z_MEM_ERROR);
149eda14cbcSMatt Macy #endif
150eda14cbcSMatt Macy 	err = zlib_deflateInit(&stream, level);
151eda14cbcSMatt Macy 	if (err != Z_OK) {
152eda14cbcSMatt Macy 		zlib_workspace_free(stream.opaque);
153eda14cbcSMatt Macy 		return (err);
154eda14cbcSMatt Macy 	}
155eda14cbcSMatt Macy 
156eda14cbcSMatt Macy 	err = zlib_deflate(&stream, Z_FINISH);
157eda14cbcSMatt Macy 	if (err != Z_STREAM_END) {
158eda14cbcSMatt Macy 		zlib_deflateEnd(&stream);
159eda14cbcSMatt Macy 		zlib_workspace_free(stream.opaque);
160eda14cbcSMatt Macy 		return (err == Z_OK ? Z_BUF_ERROR : err);
161eda14cbcSMatt Macy 	}
162eda14cbcSMatt Macy 	*destLen = stream.total_out;
163eda14cbcSMatt Macy 
164eda14cbcSMatt Macy 	err = zlib_deflateEnd(&stream);
165eda14cbcSMatt Macy 	zlib_workspace_free(stream.opaque);
166eda14cbcSMatt Macy 	return (err);
167eda14cbcSMatt Macy }
168eda14cbcSMatt Macy 
169eda14cbcSMatt Macy /*
170eda14cbcSMatt Macy  * Decompresses the source buffer into the destination buffer.  sourceLen is
171eda14cbcSMatt Macy  * the byte length of the source buffer. Upon entry, destLen is the total
172eda14cbcSMatt Macy  * size of the destination buffer, which must be large enough to hold the
173eda14cbcSMatt Macy  * entire uncompressed data. (The size of the uncompressed data must have
174eda14cbcSMatt Macy  * been saved previously by the compressor and transmitted to the decompressor
175eda14cbcSMatt Macy  * by some mechanism outside the scope of this compression library.)
176eda14cbcSMatt Macy  * Upon exit, destLen is the actual size of the compressed buffer.
177eda14cbcSMatt Macy  * This function can be used to decompress a whole file at once if the
178eda14cbcSMatt Macy  * input file is mmap'ed.
179eda14cbcSMatt Macy  *
180eda14cbcSMatt Macy  * uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
181eda14cbcSMatt Macy  * enough memory, Z_BUF_ERROR if there was not enough room in the output
182eda14cbcSMatt Macy  * buffer, or Z_DATA_ERROR if the input data was corrupted.
183eda14cbcSMatt Macy  */
184eda14cbcSMatt Macy int
z_uncompress(void * dest,size_t * destLen,const void * source,size_t sourceLen)185eda14cbcSMatt Macy z_uncompress(void *dest, size_t *destLen, const void *source, size_t sourceLen)
186eda14cbcSMatt Macy {
187*da5137abSMartin Matuska 	z_stream stream = {0};
188eda14cbcSMatt Macy 	int err;
189eda14cbcSMatt Macy 
190eda14cbcSMatt Macy 	stream.next_in = (Byte *)source;
191eda14cbcSMatt Macy 	stream.avail_in = (uInt)sourceLen;
192eda14cbcSMatt Macy 	stream.next_out = dest;
193eda14cbcSMatt Macy 	stream.avail_out = (uInt)*destLen;
194eda14cbcSMatt Macy 
195eda14cbcSMatt Macy 	if ((size_t)stream.avail_out != *destLen)
196eda14cbcSMatt Macy 		return (Z_BUF_ERROR);
197eda14cbcSMatt Macy 
198eda14cbcSMatt Macy 	stream.opaque = zlib_workspace_alloc(KM_SLEEP);
199eda14cbcSMatt Macy #if 0
200eda14cbcSMatt Macy 	if (!stream.opaque)
201eda14cbcSMatt Macy 		return (Z_MEM_ERROR);
202eda14cbcSMatt Macy #endif
203eda14cbcSMatt Macy 	err = zlib_inflateInit(&stream);
204eda14cbcSMatt Macy 	if (err != Z_OK) {
205eda14cbcSMatt Macy 		zlib_workspace_free(stream.opaque);
206eda14cbcSMatt Macy 		return (err);
207eda14cbcSMatt Macy 	}
208eda14cbcSMatt Macy 
209eda14cbcSMatt Macy 	err = zlib_inflate(&stream, Z_FINISH);
210eda14cbcSMatt Macy 	if (err != Z_STREAM_END) {
211eda14cbcSMatt Macy 		zlib_inflateEnd(&stream);
212eda14cbcSMatt Macy 		zlib_workspace_free(stream.opaque);
213eda14cbcSMatt Macy 
214eda14cbcSMatt Macy 		if (err == Z_NEED_DICT ||
215eda14cbcSMatt Macy 		    (err == Z_BUF_ERROR && stream.avail_in == 0))
216eda14cbcSMatt Macy 			return (Z_DATA_ERROR);
217eda14cbcSMatt Macy 
218eda14cbcSMatt Macy 		return (err);
219eda14cbcSMatt Macy 	}
220eda14cbcSMatt Macy 	*destLen = stream.total_out;
221eda14cbcSMatt Macy 
222eda14cbcSMatt Macy 	err = zlib_inflateEnd(&stream);
223eda14cbcSMatt Macy 	zlib_workspace_free(stream.opaque);
224eda14cbcSMatt Macy 
225eda14cbcSMatt Macy 	return (err);
226eda14cbcSMatt Macy }
227