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