deflate.c revision 1.16 1 1.16 drochner /* $NetBSD: deflate.c,v 1.16 2011/02/17 17:10:18 drochner Exp $ */
2 1.1 jonathan /* $FreeBSD: src/sys/opencrypto/deflate.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $ */
3 1.1 jonathan /* $OpenBSD: deflate.c,v 1.3 2001/08/20 02:45:22 hugh Exp $ */
4 1.1 jonathan
5 1.1 jonathan /*
6 1.1 jonathan * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj (at) wabbitt.org)
7 1.1 jonathan *
8 1.1 jonathan * Redistribution and use in source and binary forms, with or without
9 1.1 jonathan * modification, are permitted provided that the following conditions
10 1.1 jonathan * are met:
11 1.1 jonathan *
12 1.1 jonathan * 1. Redistributions of source code must retain the above copyright
13 1.1 jonathan * notice, this list of conditions and the following disclaimer.
14 1.1 jonathan * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 jonathan * notice, this list of conditions and the following disclaimer in the
16 1.1 jonathan * documentation and/or other materials provided with the distribution.
17 1.1 jonathan * 3. The name of the author may not be used to endorse or promote products
18 1.1 jonathan * derived from this software without specific prior written permission.
19 1.1 jonathan *
20 1.1 jonathan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 jonathan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 jonathan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 jonathan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 jonathan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 jonathan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 jonathan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 jonathan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 jonathan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1 jonathan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 jonathan */
31 1.1 jonathan
32 1.1 jonathan /*
33 1.1 jonathan * This file contains a wrapper around the deflate algo compression
34 1.11 ad * functions using the zlib library (see net/zlib.{c,h})
35 1.1 jonathan */
36 1.1 jonathan
37 1.1 jonathan #include <sys/cdefs.h>
38 1.16 drochner __KERNEL_RCSID(0, "$NetBSD: deflate.c,v 1.16 2011/02/17 17:10:18 drochner Exp $");
39 1.1 jonathan
40 1.1 jonathan #include <sys/types.h>
41 1.1 jonathan #include <sys/malloc.h>
42 1.1 jonathan #include <sys/param.h>
43 1.1 jonathan #include <sys/systm.h>
44 1.11 ad #include <net/zlib.h>
45 1.1 jonathan
46 1.1 jonathan #include <opencrypto/cryptodev.h>
47 1.1 jonathan #include <opencrypto/deflate.h>
48 1.1 jonathan
49 1.13 darran
50 1.1 jonathan int window_inflate = -1 * MAX_WBITS;
51 1.1 jonathan int window_deflate = -12;
52 1.1 jonathan
53 1.1 jonathan /*
54 1.1 jonathan * This function takes a block of data and (de)compress it using the deflate
55 1.1 jonathan * algorithm
56 1.1 jonathan */
57 1.1 jonathan
58 1.2 thorpej static void *
59 1.7 christos ocf_zalloc(void *nil, u_int type, u_int size)
60 1.2 thorpej {
61 1.2 thorpej void *ptr;
62 1.2 thorpej
63 1.2 thorpej ptr = malloc(type *size, M_CRYPTO_DATA, M_NOWAIT);
64 1.2 thorpej return ptr;
65 1.2 thorpej }
66 1.2 thorpej
67 1.2 thorpej static void
68 1.7 christos ocf_zfree(void *nil, void *ptr)
69 1.2 thorpej {
70 1.2 thorpej free(ptr, M_CRYPTO_DATA);
71 1.2 thorpej }
72 1.2 thorpej
73 1.1 jonathan u_int32_t
74 1.12 dsl deflate_global(u_int8_t *data, u_int32_t size, int decomp, u_int8_t **out)
75 1.1 jonathan {
76 1.1 jonathan /* decomp indicates whether we compress (0) or decompress (1) */
77 1.1 jonathan
78 1.1 jonathan z_stream zbuf;
79 1.1 jonathan u_int8_t *output;
80 1.15 drochner u_int32_t count, result, tocopy;
81 1.15 drochner int error, i, j;
82 1.8 degroote struct deflate_buf *buf, *tmp;
83 1.15 drochner size_t len;
84 1.1 jonathan
85 1.13 darran DPRINTF(("deflate_global: size %d\n", size));
86 1.13 darran
87 1.8 degroote len = ZBUF;
88 1.8 degroote buf = malloc(len*sizeof(struct deflate_buf), M_CRYPTO_DATA, M_NOWAIT);
89 1.8 degroote if (buf == NULL)
90 1.8 degroote return 0;
91 1.8 degroote
92 1.8 degroote memset(&zbuf, 0, sizeof(z_stream));
93 1.1 jonathan zbuf.next_in = data; /* data that is going to be processed */
94 1.2 thorpej zbuf.zalloc = ocf_zalloc;
95 1.2 thorpej zbuf.zfree = ocf_zfree;
96 1.1 jonathan zbuf.opaque = Z_NULL;
97 1.1 jonathan zbuf.avail_in = size; /* Total length of data to be processed */
98 1.1 jonathan
99 1.1 jonathan if (!decomp) {
100 1.15 drochner buf[0].size = size;
101 1.1 jonathan } else {
102 1.1 jonathan /*
103 1.1 jonathan * Choose a buffer with 4x the size of the input buffer
104 1.1 jonathan * for the size of the output buffer in the case of
105 1.1 jonathan * decompression. If it's not sufficient, it will need to be
106 1.1 jonathan * updated while the decompression is going on
107 1.1 jonathan */
108 1.1 jonathan
109 1.15 drochner buf[0].size = size * 4;
110 1.1 jonathan }
111 1.15 drochner buf[0].out = malloc(buf[0].size, M_CRYPTO_DATA, M_NOWAIT);
112 1.15 drochner if (buf[0].out == NULL)
113 1.15 drochner goto bad3;
114 1.15 drochner i = 1;
115 1.1 jonathan
116 1.1 jonathan zbuf.next_out = buf[0].out;
117 1.1 jonathan zbuf.avail_out = buf[0].size;
118 1.1 jonathan
119 1.1 jonathan error = decomp ? inflateInit2(&zbuf, window_inflate) :
120 1.1 jonathan deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
121 1.1 jonathan window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
122 1.1 jonathan
123 1.1 jonathan if (error != Z_OK)
124 1.15 drochner goto bad2;
125 1.1 jonathan for (;;) {
126 1.14 drochner error = decomp ? inflate(&zbuf, Z_SYNC_FLUSH) :
127 1.14 drochner deflate(&zbuf, Z_FINISH);
128 1.16 drochner if (error == Z_STREAM_END) /* success */
129 1.16 drochner break;
130 1.16 drochner /*
131 1.16 drochner * XXX compensate for two problems:
132 1.16 drochner * -Former versions of this code didn't set Z_FINISH
133 1.16 drochner * on compression, so the compressed data are not correctly
134 1.16 drochner * terminated and the decompressor doesn't get Z_STREAM_END.
135 1.16 drochner * Accept such packets for interoperability.
136 1.16 drochner * -sys/net/zlib.c has a bug which makes that Z_BUF_ERROR is
137 1.16 drochner * set after successful decompression under rare conditions.
138 1.16 drochner */
139 1.16 drochner else if (decomp && (error == Z_OK || error == Z_BUF_ERROR)
140 1.16 drochner && zbuf.avail_in == 0 && zbuf.avail_out != 0)
141 1.16 drochner break;
142 1.16 drochner else if (error != Z_OK)
143 1.1 jonathan goto bad;
144 1.8 degroote else if (zbuf.avail_out == 0) {
145 1.15 drochner if (i == len) {
146 1.8 degroote len += ZBUF;
147 1.8 degroote tmp = realloc(buf,len*sizeof(struct deflate_buf),
148 1.8 degroote M_CRYPTO_DATA, M_NOWAIT);
149 1.8 degroote if (tmp == NULL)
150 1.8 degroote goto bad;
151 1.8 degroote buf = tmp;
152 1.8 degroote }
153 1.1 jonathan /* we need more output space, allocate size */
154 1.5 christos buf[i].out = malloc(size, M_CRYPTO_DATA, M_NOWAIT);
155 1.1 jonathan if (buf[i].out == NULL)
156 1.1 jonathan goto bad;
157 1.1 jonathan zbuf.next_out = buf[i].out;
158 1.1 jonathan buf[i].size = size;
159 1.1 jonathan zbuf.avail_out = buf[i].size;
160 1.1 jonathan i++;
161 1.16 drochner }
162 1.1 jonathan }
163 1.1 jonathan
164 1.1 jonathan result = count = zbuf.total_out;
165 1.1 jonathan
166 1.15 drochner if (i != 1) { /* copy everything into one buffer */
167 1.15 drochner output = malloc(result, M_CRYPTO_DATA, M_NOWAIT);
168 1.15 drochner if (output == NULL)
169 1.15 drochner goto bad;
170 1.15 drochner *out = output;
171 1.15 drochner for (j = 0; j < i; j++) {
172 1.15 drochner tocopy = MIN(count, buf[j].size);
173 1.15 drochner /* XXX the last buf can be empty */
174 1.15 drochner KASSERT(tocopy || j == (i - 1));
175 1.15 drochner memcpy(output, buf[j].out, tocopy);
176 1.15 drochner output += tocopy;
177 1.15 drochner free(buf[j].out, M_CRYPTO_DATA);
178 1.15 drochner count -= tocopy;
179 1.15 drochner }
180 1.15 drochner KASSERT(count == 0);
181 1.15 drochner } else {
182 1.15 drochner *out = buf[0].out;
183 1.15 drochner }
184 1.15 drochner free(buf, M_CRYPTO_DATA);
185 1.1 jonathan if (decomp)
186 1.1 jonathan inflateEnd(&zbuf);
187 1.1 jonathan else
188 1.1 jonathan deflateEnd(&zbuf);
189 1.1 jonathan return result;
190 1.1 jonathan
191 1.1 jonathan bad:
192 1.1 jonathan if (decomp)
193 1.1 jonathan inflateEnd(&zbuf);
194 1.1 jonathan else
195 1.1 jonathan deflateEnd(&zbuf);
196 1.15 drochner bad2:
197 1.15 drochner for (j = 0; j < i; j++)
198 1.15 drochner free(buf[j].out, M_CRYPTO_DATA);
199 1.15 drochner bad3:
200 1.15 drochner free(buf, M_CRYPTO_DATA);
201 1.1 jonathan return 0;
202 1.1 jonathan }
203 1.13 darran
204 1.13 darran /*
205 1.13 darran * Initial version will perform a single gzip encapsulation,
206 1.13 darran * filling in the header,
207 1.13 darran * and appending the crc and uncompressed length.
208 1.13 darran *
209 1.13 darran * Later version will support multiple buffers with
210 1.13 darran * a flag indication final buffer. The crc is maintained
211 1.13 darran * over all buffers and appended to the output along with
212 1.13 darran * the uncompressed length after the final data buffer
213 1.13 darran * has been compressed and output.
214 1.13 darran *
215 1.13 darran * Ditto for uncompress - CRC is extracted from the final packed
216 1.13 darran * and compared against CRC of uncompressed data.
217 1.13 darran *
218 1.13 darran */
219 1.13 darran
220 1.13 darran /* constant header for the gzip */
221 1.13 darran static const char gzip_header[10] = {
222 1.13 darran 0x1f, 0x8b, /* ID1 ID2 */
223 1.13 darran Z_DEFLATED, /* CM */
224 1.13 darran 0, /* FLG */
225 1.13 darran 0, 0, 0, 0, /* MTIME */
226 1.13 darran 0, /* XFL */
227 1.13 darran 0x03 /* OS (Unix) */
228 1.13 darran };
229 1.13 darran
230 1.13 darran /* Followed by compressed payload */
231 1.13 darran /* Followed by uint32_t CRC32 and uint32_t ISIZE */
232 1.13 darran #define GZIP_TAIL_SIZE 8
233 1.13 darran
234 1.13 darran u_int32_t
235 1.13 darran gzip_global(u_int8_t *data, u_int32_t size,
236 1.13 darran int decomp, u_int8_t **out)
237 1.13 darran {
238 1.13 darran /* decomp indicates whether we compress (0) or decompress (1) */
239 1.13 darran z_stream zbuf;
240 1.13 darran u_int8_t *output;
241 1.13 darran u_int32_t count, result;
242 1.13 darran int error, i = 0, j;
243 1.13 darran struct deflate_buf *buf, *tmp;
244 1.15 drochner size_t nbufs;
245 1.13 darran u_int32_t crc;
246 1.13 darran u_int32_t isize;
247 1.13 darran
248 1.13 darran DPRINTF(("gzip_global: decomp %d, size %d\n", decomp, size));
249 1.13 darran
250 1.13 darran nbufs = ZBUF;
251 1.13 darran buf = malloc(nbufs*sizeof(struct deflate_buf), M_CRYPTO_DATA, M_NOWAIT);
252 1.13 darran if (buf == NULL) {
253 1.13 darran DPRINTF(("gzip_global.%d: failed to malloc %d\n",
254 1.13 darran __LINE__, nbufs*sizeof(struct deflate_buf)));
255 1.13 darran return 0;
256 1.13 darran }
257 1.13 darran
258 1.13 darran memset(&zbuf, 0, sizeof(z_stream));
259 1.13 darran zbuf.zalloc = ocf_zalloc;
260 1.13 darran zbuf.zfree = ocf_zfree;
261 1.13 darran zbuf.opaque = Z_NULL;
262 1.13 darran
263 1.13 darran crc = crc32(0, NULL, 0); /* get initial crc value */
264 1.13 darran
265 1.13 darran zbuf.avail_in = size; /* Total length of data to be processed */
266 1.13 darran zbuf.next_in = data; /* data that is going to be processed */
267 1.13 darran
268 1.13 darran if (!decomp) {
269 1.13 darran /* compress */
270 1.13 darran DPRINTF(("gzip_global: compress[%d] malloc %d + %d + %d = %d\n",
271 1.13 darran i, size, sizeof(gzip_header), GZIP_TAIL_SIZE,
272 1.13 darran size + sizeof(gzip_header) + GZIP_TAIL_SIZE));
273 1.13 darran
274 1.15 drochner buf[0].size = size;
275 1.13 darran
276 1.13 darran crc = crc32(crc, data, size);
277 1.13 darran DPRINTF(("gzip_compress: size %d, crc 0x%x\n", size, crc));
278 1.13 darran } else {
279 1.13 darran /* decompress */
280 1.13 darran /* check the gzip header */
281 1.13 darran if (zbuf.avail_in <= 0) {
282 1.13 darran /* Not enough data for the header & tail */
283 1.13 darran DPRINTF(("gzip_global: not enough data (%d)\n",
284 1.13 darran size));
285 1.13 darran goto bad2;
286 1.13 darran }
287 1.13 darran
288 1.13 darran /* XXX this is pretty basic,
289 1.13 darran * needs to be expanded to ignore MTIME, OS,
290 1.13 darran * but still ensure flags are 0.
291 1.13 darran * Q. Do we need to support the flags and
292 1.13 darran * optional header fields? Likely.
293 1.13 darran * XXX add flag and field support too.
294 1.13 darran */
295 1.13 darran if (memcmp(data, gzip_header, sizeof(gzip_header)) != 0) {
296 1.13 darran DPRINTF(("gzip_global: unsupported gzip header (%02x%02x)\n",
297 1.13 darran data[0], data[1]));
298 1.13 darran goto bad2;
299 1.13 darran } else {
300 1.13 darran DPRINTF(("gzip_global.%d: gzip header ok\n",__LINE__));
301 1.13 darran }
302 1.13 darran
303 1.13 darran isize = *((uint32_t *)&data[size-sizeof(uint32_t)]);
304 1.13 darran
305 1.13 darran DPRINTF(("gzip_global: isize = %d (%02x %02x %02x %02x)\n",
306 1.13 darran isize,
307 1.13 darran data[size-4],
308 1.13 darran data[size-3],
309 1.13 darran data[size-2],
310 1.13 darran data[size-1]));
311 1.13 darran
312 1.15 drochner buf[0].size = isize;
313 1.13 darran
314 1.13 darran /* skip over the gzip header */
315 1.13 darran zbuf.next_in = data + sizeof(gzip_header);
316 1.13 darran
317 1.13 darran /* actual payload size stripped of gzip header and tail */
318 1.13 darran zbuf.avail_in = size - sizeof(gzip_header) - GZIP_TAIL_SIZE;
319 1.13 darran }
320 1.13 darran
321 1.15 drochner buf[0].out = malloc(buf[0].size, M_CRYPTO_DATA, M_NOWAIT);
322 1.15 drochner if (buf[0].out == NULL)
323 1.15 drochner goto bad2;
324 1.15 drochner zbuf.next_out = buf[0].out;
325 1.15 drochner zbuf.avail_out = buf[0].size;
326 1.15 drochner DPRINTF(("zbuf avail_in %d, avail_out %d\n",
327 1.15 drochner zbuf.avail_in, zbuf.avail_out));
328 1.15 drochner i = 1;
329 1.13 darran
330 1.13 darran error = decomp ? inflateInit2(&zbuf, window_inflate) :
331 1.13 darran deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
332 1.13 darran window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
333 1.13 darran
334 1.13 darran if (error != Z_OK) {
335 1.13 darran printf("deflateInit2() failed\n");
336 1.13 darran goto bad;
337 1.13 darran }
338 1.13 darran for (;;) {
339 1.13 darran DPRINTF(("pre: %s in:%d out:%d\n", decomp ? "deflate()" : "inflate()",
340 1.13 darran zbuf.avail_in, zbuf.avail_out));
341 1.14 drochner error = decomp ? inflate(&zbuf, Z_SYNC_FLUSH) :
342 1.14 drochner deflate(&zbuf, Z_FINISH);
343 1.13 darran DPRINTF(("post: %s in:%d out:%d\n", decomp ? "deflate()" : "inflate()",
344 1.13 darran zbuf.avail_in, zbuf.avail_out));
345 1.13 darran if (error != Z_OK && error != Z_STREAM_END) {
346 1.13 darran printf("deflate() or inflate() failed, error=%d\n", error);
347 1.13 darran goto bad;
348 1.13 darran } else if (zbuf.avail_in == 0 && zbuf.avail_out != 0) {
349 1.13 darran DPRINTF(("gzip_global: avail_in == 0, ending\n"));
350 1.13 darran goto end;
351 1.13 darran } else if (zbuf.avail_in == 0 && zbuf.avail_out == 0) {
352 1.13 darran DPRINTF(("gzip_global: avail_in == 0, avail_out == 0, ending\n"));
353 1.13 darran goto end;
354 1.13 darran } else if (zbuf.avail_out == 0) {
355 1.15 drochner if (i == nbufs) {
356 1.13 darran nbufs += ZBUF;
357 1.13 darran tmp = realloc(buf,nbufs*sizeof(struct deflate_buf),
358 1.13 darran M_CRYPTO_DATA, M_NOWAIT);
359 1.13 darran if (tmp == NULL)
360 1.13 darran goto bad;
361 1.13 darran buf = tmp;
362 1.13 darran }
363 1.13 darran /* we need more output space, allocate size */
364 1.13 darran buf[i].out = malloc(size, M_CRYPTO_DATA, M_NOWAIT);
365 1.13 darran if (buf[i].out == NULL)
366 1.13 darran goto bad;
367 1.13 darran zbuf.next_out = buf[i].out;
368 1.13 darran buf[i].size = size;
369 1.13 darran zbuf.avail_out = buf[i].size;
370 1.13 darran i++;
371 1.13 darran } else
372 1.13 darran goto bad;
373 1.13 darran }
374 1.13 darran
375 1.13 darran end:
376 1.13 darran if (decomp) {
377 1.13 darran count = result = zbuf.total_out;
378 1.13 darran } else {
379 1.13 darran /* need room for header, CRC, and ISIZE */
380 1.13 darran result = zbuf.total_out + sizeof(gzip_header) + GZIP_TAIL_SIZE;
381 1.13 darran count = zbuf.total_out;
382 1.13 darran }
383 1.13 darran
384 1.13 darran DPRINTF(("gzip_global: in %d -> out %d\n", size, result));
385 1.13 darran
386 1.13 darran *out = malloc(result, M_CRYPTO_DATA, M_NOWAIT);
387 1.13 darran if (*out == NULL)
388 1.13 darran goto bad;
389 1.13 darran output = *out;
390 1.13 darran if (decomp)
391 1.13 darran inflateEnd(&zbuf);
392 1.13 darran else {
393 1.13 darran deflateEnd(&zbuf);
394 1.13 darran
395 1.13 darran /* fill in gzip header */
396 1.13 darran memcpy(output, gzip_header, sizeof(gzip_header));
397 1.13 darran output += sizeof(gzip_header);
398 1.13 darran }
399 1.15 drochner for (j = 0; j < i; j++) {
400 1.13 darran if (decomp) {
401 1.13 darran /* update crc for decompressed data */
402 1.13 darran crc = crc32(crc, buf[j].out, buf[j].size);
403 1.13 darran }
404 1.13 darran if (count > buf[j].size) {
405 1.13 darran memcpy(output, buf[j].out, buf[j].size);
406 1.13 darran output += buf[j].size;
407 1.13 darran free(buf[j].out, M_CRYPTO_DATA);
408 1.13 darran count -= buf[j].size;
409 1.13 darran } else {
410 1.13 darran /* it should be the last buffer */
411 1.13 darran memcpy(output, buf[j].out, count);
412 1.13 darran output += count;
413 1.13 darran free(buf[j].out, M_CRYPTO_DATA);
414 1.13 darran count = 0;
415 1.13 darran }
416 1.13 darran }
417 1.13 darran free(buf, M_CRYPTO_DATA);
418 1.13 darran
419 1.13 darran if (!decomp) {
420 1.13 darran /* fill in CRC and ISIZE */
421 1.13 darran ((uint32_t *)output)[0] = crc;
422 1.13 darran ((uint32_t *)output)[1] = size;
423 1.13 darran
424 1.13 darran DPRINTF(("gzip_global: size = 0x%x (%02x %02x %02x %02x)\n",
425 1.13 darran size,
426 1.13 darran output[7],
427 1.13 darran output[3],
428 1.13 darran output[5],
429 1.13 darran output[4]));
430 1.13 darran }
431 1.13 darran
432 1.13 darran return result;
433 1.13 darran
434 1.13 darran bad:
435 1.13 darran if (decomp)
436 1.13 darran inflateEnd(&zbuf);
437 1.13 darran else
438 1.13 darran deflateEnd(&zbuf);
439 1.13 darran bad2:
440 1.13 darran *out = NULL;
441 1.15 drochner for (j = 0; j < i; j++)
442 1.13 darran free(buf[j].out, M_CRYPTO_DATA);
443 1.13 darran free(buf, M_CRYPTO_DATA);
444 1.13 darran return 0;
445 1.13 darran }
446