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/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/kernel.h>
44 #include <sys/systm.h>
45 #include <contrib/zlib/zlib.h>
47 #include <opencrypto/cryptodev.h>
48 #include <opencrypto/deflate.h>
50 SDT_PROVIDER_DECLARE(opencrypto);
51 SDT_PROBE_DEFINE2(opencrypto, deflate, deflate_global, entry,
53 SDT_PROBE_DEFINE6(opencrypto, deflate, deflate_global, bad,
54 "int", "int", "int", "int", "int", "int");
55 SDT_PROBE_DEFINE6(opencrypto, deflate, deflate_global, iter,
56 "int", "int", "int", "int", "int", "int");
57 SDT_PROBE_DEFINE2(opencrypto, deflate, deflate_global, return,
60 int window_inflate = -1 * MAX_WBITS;
61 int window_deflate = -12;
64 crypto_zalloc(void *nil, u_int type, u_int size)
68 ptr = malloc(type *size, M_CRYPTO_DATA, M_NOWAIT);
73 crypto_zfree(void *nil, void *ptr)
76 free(ptr, M_CRYPTO_DATA);
80 * This function takes a block of data and (de)compress it using the deflate
85 deflate_global(data, size, decomp, out)
91 /* decomp indicates whether we compress (0) or decompress (1) */
95 u_int32_t count, result;
97 struct deflate_buf *bufh, *bufp;
99 SDT_PROBE2(opencrypto, deflate, deflate_global, entry, decomp, size);
106 * Choose a buffer with 4x the size of the input buffer
107 * for the size of the output buffer in the case of
108 * decompression. If it's not sufficient, it will need to be
109 * updated while the decompression is going on.
114 * Make sure we do have enough output space. Repeated calls to
115 * deflate need at least 6 bytes of output buffer space to avoid
116 * repeated markers. We will always provide at least 16 bytes.
118 while ((size * i) < 16)
121 bufh = bufp = malloc(sizeof(*bufp) + (size_t)(size * i),
122 M_CRYPTO_DATA, M_NOWAIT);
124 SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
125 decomp, 0, __LINE__, 0, 0, 0);
129 bufp->size = size * i;
131 bzero(&zbuf, sizeof(z_stream));
132 zbuf.zalloc = crypto_zalloc;
133 zbuf.zfree = crypto_zfree;
134 zbuf.opaque = Z_NULL;
135 zbuf.next_in = data; /* Data that is going to be processed. */
136 zbuf.avail_in = size; /* Total length of data to be processed. */
137 zbuf.next_out = bufp->data;
138 zbuf.avail_out = bufp->size;
140 error = decomp ? inflateInit2(&zbuf, window_inflate) :
141 deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
142 window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
144 SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
145 decomp, error, __LINE__, 0, 0, 0);
150 error = decomp ? inflate(&zbuf, Z_SYNC_FLUSH) :
151 deflate(&zbuf, Z_FINISH);
152 if (error != Z_OK && error != Z_STREAM_END) {
153 SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
154 decomp, error, __LINE__,
155 zbuf.avail_in, zbuf.avail_out, zbuf.total_out);
158 SDT_PROBE6(opencrypto, deflate, deflate_global, iter,
159 decomp, error, __LINE__,
160 zbuf.avail_in, zbuf.avail_out, zbuf.total_out);
161 if (decomp && zbuf.avail_in == 0 && error == Z_STREAM_END) {
164 } else if (!decomp && error == Z_STREAM_END) {
167 } else if (zbuf.avail_out == 0) {
168 struct deflate_buf *p;
170 /* We need more output space for another iteration. */
171 p = malloc(sizeof(*p) + (size_t)(size * i),
172 M_CRYPTO_DATA, M_NOWAIT);
174 SDT_PROBE6(opencrypto, deflate, deflate_global,
175 bad, decomp, 0, __LINE__, 0, 0, 0);
182 zbuf.next_out = bufp->data;
183 zbuf.avail_out = bufp->size;
185 /* Unexpect result. */
186 SDT_PROBE6(opencrypto, deflate, deflate_global,
187 bad, decomp, error, __LINE__,
188 zbuf.avail_in, zbuf.avail_out, zbuf.total_out);
193 result = count = zbuf.total_out;
195 *out = malloc(result, M_CRYPTO_DATA, M_NOWAIT);
197 SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
198 decomp, 0, __LINE__, 0, 0, 0);
206 for (bufp = bufh; bufp != NULL; ) {
207 if (count > bufp->size) {
208 struct deflate_buf *p;
210 bcopy(bufp->data, *out, bufp->size);
215 free(p, M_CRYPTO_DATA);
217 /* It should be the last buffer. */
218 bcopy(bufp->data, *out, count);
220 free(bufp, M_CRYPTO_DATA);
226 SDT_PROBE2(opencrypto, deflate, deflate_global, return, decomp, result);
234 for (bufp = bufh; bufp != NULL; ) {
235 struct deflate_buf *p;
239 free(p, M_CRYPTO_DATA);