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/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/types.h>
32 #include <sys/kmem.h>
33 #include <sys/kmem_cache.h>
34 #include <sys/zmod.h>
35 #if __FreeBSD_version >= 1300041
36 #include <contrib/zlib/zlib.h>
37 #else
38 #include <sys/zlib.h>
39 #endif
40 #include <sys/kobj.h>
41 
42 
43 static void *
44 zcalloc(void *opaque, uint_t items, uint_t size)
45 {
46 	(void) opaque;
47 	return (malloc((size_t)items*size, M_SOLARIS, M_NOWAIT));
48 }
49 
50 static void
51 zcfree(void *opaque, void *ptr)
52 {
53 	(void) opaque;
54 	free(ptr, M_SOLARIS);
55 }
56 
57 static int
58 zlib_deflateInit(z_stream *stream, int level)
59 {
60 
61 	stream->zalloc = zcalloc;
62 	stream->opaque = NULL;
63 	stream->zfree = zcfree;
64 
65 	return (deflateInit(stream, level));
66 }
67 
68 static int
69 zlib_deflate(z_stream *stream, int flush)
70 {
71 	return (deflate(stream, flush));
72 }
73 
74 static int
75 zlib_deflateEnd(z_stream *stream)
76 {
77 	return (deflateEnd(stream));
78 }
79 
80 static int
81 zlib_inflateInit(z_stream *stream)
82 {
83 	stream->zalloc = zcalloc;
84 	stream->opaque = NULL;
85 	stream->zfree = zcfree;
86 
87 	return (inflateInit(stream));
88 }
89 
90 static int
91 zlib_inflate(z_stream *stream, int finish)
92 {
93 #if __FreeBSD_version >= 1300024
94 	return (inflate(stream, finish));
95 #else
96 	return (_zlib104_inflate(stream, finish));
97 #endif
98 }
99 
100 
101 static int
102 zlib_inflateEnd(z_stream *stream)
103 {
104 	return (inflateEnd(stream));
105 }
106 
107 /*
108  * A kmem_cache is used for the zlib workspaces to avoid having to vmalloc
109  * and vfree for every call.  Using a kmem_cache also has the advantage
110  * that improves the odds that the memory used will be local to this cpu.
111  * To further improve things it might be wise to create a dedicated per-cpu
112  * workspace for use.  This would take some additional care because we then
113  * must disable preemption around the critical section, and verify that
114  * zlib_deflate* and zlib_inflate* never internally call schedule().
115  */
116 static void *
117 zlib_workspace_alloc(int flags)
118 {
119 	// return (kmem_cache_alloc(zlib_workspace_cache, flags));
120 	return (NULL);
121 }
122 
123 static void
124 zlib_workspace_free(void *workspace)
125 {
126 	// kmem_cache_free(zlib_workspace_cache, workspace);
127 }
128 
129 /*
130  * Compresses the source buffer into the destination buffer. The level
131  * parameter has the same meaning as in deflateInit.  sourceLen is the byte
132  * length of the source buffer. Upon entry, destLen is the total size of the
133  * destination buffer, which must be at least 0.1% larger than sourceLen plus
134  * 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
135  *
136  * compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
137  * memory, Z_BUF_ERROR if there was not enough room in the output buffer,
138  * Z_STREAM_ERROR if the level parameter is invalid.
139  */
140 int
141 z_compress_level(void *dest, size_t *destLen, const void *source,
142     size_t sourceLen, int level)
143 {
144 	z_stream stream = {0};
145 	int err;
146 
147 	stream.next_in = (Byte *)source;
148 	stream.avail_in = (uInt)sourceLen;
149 	stream.next_out = dest;
150 	stream.avail_out = (uInt)*destLen;
151 	stream.opaque = NULL;
152 
153 	if ((size_t)stream.avail_out != *destLen)
154 		return (Z_BUF_ERROR);
155 
156 	stream.opaque = zlib_workspace_alloc(KM_SLEEP);
157 #if 0
158 	if (!stream.opaque)
159 		return (Z_MEM_ERROR);
160 #endif
161 	err = zlib_deflateInit(&stream, level);
162 	if (err != Z_OK) {
163 		zlib_workspace_free(stream.opaque);
164 		return (err);
165 	}
166 
167 	err = zlib_deflate(&stream, Z_FINISH);
168 	if (err != Z_STREAM_END) {
169 		zlib_deflateEnd(&stream);
170 		zlib_workspace_free(stream.opaque);
171 		return (err == Z_OK ? Z_BUF_ERROR : err);
172 	}
173 	*destLen = stream.total_out;
174 
175 	err = zlib_deflateEnd(&stream);
176 	zlib_workspace_free(stream.opaque);
177 	return (err);
178 }
179 
180 /*
181  * Decompresses the source buffer into the destination buffer.  sourceLen is
182  * the byte length of the source buffer. Upon entry, destLen is the total
183  * size of the destination buffer, which must be large enough to hold the
184  * entire uncompressed data. (The size of the uncompressed data must have
185  * been saved previously by the compressor and transmitted to the decompressor
186  * by some mechanism outside the scope of this compression library.)
187  * Upon exit, destLen is the actual size of the compressed buffer.
188  * This function can be used to decompress a whole file at once if the
189  * input file is mmap'ed.
190  *
191  * uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
192  * enough memory, Z_BUF_ERROR if there was not enough room in the output
193  * buffer, or Z_DATA_ERROR if the input data was corrupted.
194  */
195 int
196 z_uncompress(void *dest, size_t *destLen, const void *source, size_t sourceLen)
197 {
198 	z_stream stream = {0};
199 	int err;
200 
201 	stream.next_in = (Byte *)source;
202 	stream.avail_in = (uInt)sourceLen;
203 	stream.next_out = dest;
204 	stream.avail_out = (uInt)*destLen;
205 
206 	if ((size_t)stream.avail_out != *destLen)
207 		return (Z_BUF_ERROR);
208 
209 	stream.opaque = zlib_workspace_alloc(KM_SLEEP);
210 #if 0
211 	if (!stream.opaque)
212 		return (Z_MEM_ERROR);
213 #endif
214 	err = zlib_inflateInit(&stream);
215 	if (err != Z_OK) {
216 		zlib_workspace_free(stream.opaque);
217 		return (err);
218 	}
219 
220 	err = zlib_inflate(&stream, Z_FINISH);
221 	if (err != Z_STREAM_END) {
222 		zlib_inflateEnd(&stream);
223 		zlib_workspace_free(stream.opaque);
224 
225 		if (err == Z_NEED_DICT ||
226 		    (err == Z_BUF_ERROR && stream.avail_in == 0))
227 			return (Z_DATA_ERROR);
228 
229 		return (err);
230 	}
231 	*destLen = stream.total_out;
232 
233 	err = zlib_inflateEnd(&stream);
234 	zlib_workspace_free(stream.opaque);
235 
236 	return (err);
237 }
238