1 /* $OpenBSD: deflate.c,v 1.3 2001/08/20 02:45:22 hugh Exp $ */
4 * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * This file contains a wrapper around the deflate algo compression
32 * functions using the zlib library (see net/zlib.{c,h})
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/malloc.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
45 #include <opencrypto/cryptodev.h>
46 #include <opencrypto/deflate.h>
48 int window_inflate = -1 * MAX_WBITS;
49 int window_deflate = -12;
52 * This function takes a block of data and (de)compress it using the deflate
57 deflate_global(data, size, decomp, out)
63 /* decomp indicates whether we compress (0) or decompress (1) */
67 u_int32_t count, result;
69 struct deflate_buf buf[ZBUF];
71 bzero(&zbuf, sizeof(z_stream));
72 for (j = 0; j < ZBUF; j++)
75 zbuf.next_in = data; /* data that is going to be processed */
76 zbuf.zalloc = z_alloc;
79 zbuf.avail_in = size; /* Total length of data to be processed */
82 MALLOC(buf[i].out, u_int8_t *, (u_long) size, M_CRYPTO_DATA,
84 if (buf[i].out == NULL)
91 * Choose a buffer with 4x the size of the input buffer
92 * for the size of the output buffer in the case of
93 * decompression. If it's not sufficient, it will need to be
94 * updated while the decompression is going on
97 MALLOC(buf[i].out, u_int8_t *, (u_long) (size * 4),
98 M_CRYPTO_DATA, M_NOWAIT);
99 if (buf[i].out == NULL)
101 buf[i].size = size * 4;
106 zbuf.next_out = buf[0].out;
107 zbuf.avail_out = buf[0].size;
109 error = decomp ? inflateInit2(&zbuf, window_inflate) :
110 deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
111 window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
116 error = decomp ? inflate(&zbuf, Z_PARTIAL_FLUSH) :
117 deflate(&zbuf, Z_PARTIAL_FLUSH);
118 if (error != Z_OK && error != Z_STREAM_END)
120 else if (zbuf.avail_in == 0 && zbuf.avail_out != 0)
122 else if (zbuf.avail_out == 0 && i < (ZBUF - 1)) {
123 /* we need more output space, allocate size */
124 MALLOC(buf[i].out, u_int8_t *, (u_long) size,
125 M_CRYPTO_DATA, M_NOWAIT);
126 if (buf[i].out == NULL)
128 zbuf.next_out = buf[i].out;
131 zbuf.avail_out = buf[i].size;
138 result = count = zbuf.total_out;
140 MALLOC(*out, u_int8_t *, (u_long) result, M_CRYPTO_DATA, M_NOWAIT);
148 for (j = 0; buf[j].flag != 0; j++) {
149 if (count > buf[j].size) {
150 bcopy(buf[j].out, *out, buf[j].size);
152 FREE(buf[j].out, M_CRYPTO_DATA);
153 count -= buf[j].size;
155 /* it should be the last buffer */
156 bcopy(buf[j].out, *out, count);
158 FREE(buf[j].out, M_CRYPTO_DATA);
167 for (j = 0; buf[j].flag != 0; j++)
168 FREE(buf[j].out, M_CRYPTO_DATA);
177 z_alloc(nil, type, size)
183 ptr = malloc(type *size, M_CRYPTO_DATA, M_NOWAIT);
191 free(ptr, M_CRYPTO_DATA);