1/* $OpenBSD: deflate.c,v 1.3 2001/08/20 02:45:22 hugh Exp $ */
2
3/*-
4 * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
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.
17 *
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.
28 */
29
30/*
31 * This file contains a wrapper around the deflate algo compression
32 * functions using the zlib library (see libkern/zlib.c and sys/zlib.h})
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD$");
37
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>
43#include <sys/sdt.h>
44#include <sys/systm.h>
45#include <sys/zlib.h>
46
47#include <opencrypto/cryptodev.h>
48#include <opencrypto/deflate.h>
49
50SDT_PROVIDER_DECLARE(opencrypto);
51SDT_PROBE_DEFINE2(opencrypto, deflate, deflate_global, entry,
52    "int", "u_int32_t");
53SDT_PROBE_DEFINE5(opencrypto, deflate, deflate_global, bad,
54    "int", "int", "int", "int", "int");
55SDT_PROBE_DEFINE5(opencrypto, deflate, deflate_global, iter,
56    "int", "int", "int", "int", "int");
57SDT_PROBE_DEFINE2(opencrypto, deflate, deflate_global, return,
58    "int", "u_int32_t");
59
60int window_inflate = -1 * MAX_WBITS;
61int window_deflate = -12;
62
63/*
64 * This function takes a block of data and (de)compress it using the deflate
65 * algorithm
66 */
67
68u_int32_t
69deflate_global(data, size, decomp, out)
70	u_int8_t *data;
71	u_int32_t size;
72	int decomp;
73	u_int8_t **out;
74{
75	/* decomp indicates whether we compress (0) or decompress (1) */
76
77	z_stream zbuf;
78	u_int8_t *output;
79	u_int32_t count, result;
80	int error, i;
81	struct deflate_buf *bufh, *bufp;
82
83	SDT_PROBE2(opencrypto, deflate, deflate_global, entry, decomp, size);
84
85	bufh = bufp = NULL;
86	if (!decomp) {
87		i = 1;
88	} else {
89		/*
90	 	 * Choose a buffer with 4x the size of the input buffer
91	 	 * for the size of the output buffer in the case of
92	 	 * decompression. If it's not sufficient, it will need to be
93	 	 * updated while the decompression is going on.
94	 	 */
95		i = 4;
96	}
97	/*
98	 * Make sure we do have enough output space.  Repeated calls to
99	 * deflate need at least 6 bytes of output buffer space to avoid
100	 * repeated markers.  We will always provide at least 16 bytes.
101	 */
102	while ((size * i) < 16)
103		i++;
104
105	bufh = bufp = malloc(sizeof(*bufp) + (size_t)(size * i),
106	    M_CRYPTO_DATA, M_NOWAIT);
107	if (bufp == NULL) {
108		SDT_PROBE5(opencrypto, deflate, deflate_global, bad,
109		    decomp, 0, __LINE__, 0, 0);
110		goto bad2;
111	}
112	bufp->next = NULL;
113	bufp->size = size * i;
114
115	bzero(&zbuf, sizeof(z_stream));
116	zbuf.zalloc = z_alloc;
117	zbuf.zfree = z_free;
118	zbuf.opaque = Z_NULL;
119	zbuf.next_in = data;	/* Data that is going to be processed. */
120	zbuf.avail_in = size;	/* Total length of data to be processed. */
121	zbuf.next_out = bufp->data;
122	zbuf.avail_out = bufp->size;
123
124	error = decomp ? inflateInit2(&zbuf, window_inflate) :
125	    deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
126		    window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
127	if (error != Z_OK) {
128		SDT_PROBE5(opencrypto, deflate, deflate_global, bad,
129		    decomp, error, __LINE__, 0, 0);
130		goto bad;
131	}
132
133	for (;;) {
134		error = decomp ? inflate(&zbuf, Z_SYNC_FLUSH) :
135				 deflate(&zbuf, Z_FINISH);
136		if (error != Z_OK && error != Z_STREAM_END) {
137			/*
138			 * Unfortunately we are limited to 5 arguments,
139			 * thus use two probes.
140			 */
141			SDT_PROBE5(opencrypto, deflate, deflate_global, bad,
142			    decomp, error, __LINE__,
143			    zbuf.avail_in, zbuf.avail_out);
144			SDT_PROBE5(opencrypto, deflate, deflate_global, bad,
145			    decomp, error, __LINE__,
146			    zbuf.state->dummy, zbuf.total_out);
147			goto bad;
148		}
149		SDT_PROBE5(opencrypto, deflate, deflate_global, iter,
150		    decomp, error, __LINE__,
151		    zbuf.avail_in, zbuf.avail_out);
152		SDT_PROBE5(opencrypto, deflate, deflate_global, iter,
153		    decomp, error, __LINE__,
154		    zbuf.state->dummy, zbuf.total_out);
155		if (decomp && zbuf.avail_in == 0 && error == Z_STREAM_END) {
156			/* Done. */
157			break;
158		} else if (!decomp && error == Z_STREAM_END) {
159			/* Done. */
160			break;
161		} else if (zbuf.avail_out == 0) {
162			struct deflate_buf *p;
163
164			/* We need more output space for another iteration. */
165			p = malloc(sizeof(*p) + (size_t)(size * i),
166			    M_CRYPTO_DATA, M_NOWAIT);
167			if (p == NULL) {
168				SDT_PROBE5(opencrypto, deflate, deflate_global,
169				    bad, decomp, 0, __LINE__, 0, 0);
170				goto bad;
171			}
172			p->next = NULL;
173			p->size = size * i;
174			bufp->next = p;
175			bufp = p;
176			zbuf.next_out = bufp->data;
177			zbuf.avail_out = bufp->size;
178		} else {
179			/* Unexpect result. */
180			/*
181			 * Unfortunately we are limited to 5 arguments,
182			 * thus, again, use two probes.
183			 */
184			SDT_PROBE5(opencrypto, deflate, deflate_global, bad,
185			    decomp, error, __LINE__,
186			    zbuf.avail_in, zbuf.avail_out);
187			SDT_PROBE5(opencrypto, deflate, deflate_global, bad,
188			    decomp, error, __LINE__,
189			    zbuf.state->dummy, zbuf.total_out);
190			goto bad;
191		}
192	}
193
194	result = count = zbuf.total_out;
195
196	*out = malloc(result, M_CRYPTO_DATA, M_NOWAIT);
197	if (*out == NULL) {
198		SDT_PROBE5(opencrypto, deflate, deflate_global, bad,
199		    decomp, 0, __LINE__, 0, 0);
200		goto bad;
201	}
202	if (decomp)
203		inflateEnd(&zbuf);
204	else
205		deflateEnd(&zbuf);
206	output = *out;
207	for (bufp = bufh; bufp != NULL; ) {
208		if (count > bufp->size) {
209			struct deflate_buf *p;
210
211			bcopy(bufp->data, *out, bufp->size);
212			*out += bufp->size;
213			count -= bufp->size;
214			p = bufp;
215			bufp = bufp->next;
216			free(p, M_CRYPTO_DATA);
217		} else {
218			/* It should be the last buffer. */
219			bcopy(bufp->data, *out, count);
220			*out += count;
221			free(bufp, M_CRYPTO_DATA);
222			bufp = NULL;
223			count = 0;
224		}
225	}
226	*out = output;
227	SDT_PROBE2(opencrypto, deflate, deflate_global, return, decomp, result);
228	return result;
229
230bad:
231	if (decomp)
232		inflateEnd(&zbuf);
233	else
234		deflateEnd(&zbuf);
235	for (bufp = bufh; bufp != NULL; ) {
236		struct deflate_buf *p;
237
238		p = bufp;
239		bufp = bufp->next;
240		free(p, M_CRYPTO_DATA);
241	}
242bad2:
243	*out = NULL;
244	return 0;
245}
246
247void *
248z_alloc(nil, type, size)
249	void *nil;
250	u_int type, size;
251{
252	void *ptr;
253
254	ptr = malloc(type *size, M_CRYPTO_DATA, M_NOWAIT);
255	return ptr;
256}
257
258void
259z_free(nil, ptr)
260	void *nil, *ptr;
261{
262	free(ptr, M_CRYPTO_DATA);
263}
264