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 sys/contrib/zlib)
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/malloc.h>
38 #include <sys/param.h>
39 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <contrib/zlib/zlib.h>
44 #include <opencrypto/cryptodev.h>
45 #include <opencrypto/deflate.h>
47 SDT_PROVIDER_DECLARE(opencrypto);
48 SDT_PROBE_DEFINE2(opencrypto, deflate, deflate_global, entry,
50 SDT_PROBE_DEFINE6(opencrypto, deflate, deflate_global, bad,
51 "int", "int", "int", "int", "int", "int");
52 SDT_PROBE_DEFINE6(opencrypto, deflate, deflate_global, iter,
53 "int", "int", "int", "int", "int", "int");
54 SDT_PROBE_DEFINE2(opencrypto, deflate, deflate_global, return,
57 int window_inflate = -1 * MAX_WBITS;
58 int window_deflate = -12;
61 crypto_zalloc(void *nil, u_int type, u_int size)
65 ptr = malloc(type *size, M_CRYPTO_DATA, M_NOWAIT);
70 crypto_zfree(void *nil, void *ptr)
73 free(ptr, M_CRYPTO_DATA);
77 * This function takes a block of data and (de)compress it using the deflate
82 deflate_global(uint8_t *data, uint32_t size, int decomp, uint8_t **out)
84 /* decomp indicates whether we compress (0) or decompress (1) */
88 uint32_t count, result;
90 struct deflate_buf *bufh, *bufp;
92 SDT_PROBE2(opencrypto, deflate, deflate_global, entry, decomp, size);
99 * Choose a buffer with 4x the size of the input buffer
100 * for the size of the output buffer in the case of
101 * decompression. If it's not sufficient, it will need to be
102 * updated while the decompression is going on.
107 * Make sure we do have enough output space. Repeated calls to
108 * deflate need at least 6 bytes of output buffer space to avoid
109 * repeated markers. We will always provide at least 16 bytes.
111 while ((size * i) < 16)
114 bufh = bufp = malloc(sizeof(*bufp) + (size_t)(size * i),
115 M_CRYPTO_DATA, M_NOWAIT);
117 SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
118 decomp, 0, __LINE__, 0, 0, 0);
122 bufp->size = size * i;
124 bzero(&zbuf, sizeof(z_stream));
125 zbuf.zalloc = crypto_zalloc;
126 zbuf.zfree = crypto_zfree;
127 zbuf.opaque = Z_NULL;
128 zbuf.next_in = data; /* Data that is going to be processed. */
129 zbuf.avail_in = size; /* Total length of data to be processed. */
130 zbuf.next_out = bufp->data;
131 zbuf.avail_out = bufp->size;
133 error = decomp ? inflateInit2(&zbuf, window_inflate) :
134 deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
135 window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
137 SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
138 decomp, error, __LINE__, 0, 0, 0);
143 error = decomp ? inflate(&zbuf, Z_SYNC_FLUSH) :
144 deflate(&zbuf, Z_FINISH);
145 if (error != Z_OK && error != Z_STREAM_END) {
146 SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
147 decomp, error, __LINE__,
148 zbuf.avail_in, zbuf.avail_out, zbuf.total_out);
151 SDT_PROBE6(opencrypto, deflate, deflate_global, iter,
152 decomp, error, __LINE__,
153 zbuf.avail_in, zbuf.avail_out, zbuf.total_out);
154 if (decomp && zbuf.avail_in == 0 && error == Z_STREAM_END) {
157 } else if (!decomp && error == Z_STREAM_END) {
160 } else if (zbuf.avail_out == 0) {
161 struct deflate_buf *p;
163 /* We need more output space for another iteration. */
164 p = malloc(sizeof(*p) + (size_t)(size * i),
165 M_CRYPTO_DATA, M_NOWAIT);
167 SDT_PROBE6(opencrypto, deflate, deflate_global,
168 bad, decomp, 0, __LINE__, 0, 0, 0);
175 zbuf.next_out = bufp->data;
176 zbuf.avail_out = bufp->size;
178 /* Unexpect result. */
179 SDT_PROBE6(opencrypto, deflate, deflate_global,
180 bad, decomp, error, __LINE__,
181 zbuf.avail_in, zbuf.avail_out, zbuf.total_out);
186 result = count = zbuf.total_out;
188 *out = malloc(result, M_CRYPTO_DATA, M_NOWAIT);
190 SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
191 decomp, 0, __LINE__, 0, 0, 0);
199 for (bufp = bufh; bufp != NULL; ) {
200 if (count > bufp->size) {
201 struct deflate_buf *p;
203 bcopy(bufp->data, *out, bufp->size);
208 free(p, M_CRYPTO_DATA);
210 /* It should be the last buffer. */
211 bcopy(bufp->data, *out, count);
213 free(bufp, M_CRYPTO_DATA);
219 SDT_PROBE2(opencrypto, deflate, deflate_global, return, decomp, result);
227 for (bufp = bufh; bufp != NULL; ) {
228 struct deflate_buf *p;
232 free(p, M_CRYPTO_DATA);