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