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