xref: /freebsd/sys/opencrypto/cryptodeflate.c (revision 5405b282e1f319b6f3597bb77f68be903e7f248c)
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 
50 SDT_PROVIDER_DECLARE(opencrypto);
51 SDT_PROBE_DEFINE2(opencrypto, deflate, deflate_global, entry,
52     "int", "u_int32_t");
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,
58     "int", "u_int32_t");
59 
60 int window_inflate = -1 * MAX_WBITS;
61 int window_deflate = -12;
62 
63 /*
64  * This function takes a block of data and (de)compress it using the deflate
65  * algorithm
66  */
67 
68 u_int32_t
69 deflate_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_PROBE6(opencrypto, deflate, deflate_global, bad,
109 		    decomp, 0, __LINE__, 0, 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_PROBE6(opencrypto, deflate, deflate_global, bad,
129 		    decomp, error, __LINE__, 0, 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 			SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
138 			    decomp, error, __LINE__,
139 			    zbuf.avail_in, zbuf.avail_out, zbuf.total_out);
140 			goto bad;
141 		}
142 		SDT_PROBE6(opencrypto, deflate, deflate_global, iter,
143 		    decomp, error, __LINE__,
144 		    zbuf.avail_in, zbuf.avail_out, zbuf.total_out);
145 		if (decomp && zbuf.avail_in == 0 && error == Z_STREAM_END) {
146 			/* Done. */
147 			break;
148 		} else if (!decomp && error == Z_STREAM_END) {
149 			/* Done. */
150 			break;
151 		} else if (zbuf.avail_out == 0) {
152 			struct deflate_buf *p;
153 
154 			/* We need more output space for another iteration. */
155 			p = malloc(sizeof(*p) + (size_t)(size * i),
156 			    M_CRYPTO_DATA, M_NOWAIT);
157 			if (p == NULL) {
158 				SDT_PROBE6(opencrypto, deflate, deflate_global,
159 				    bad, decomp, 0, __LINE__, 0, 0, 0);
160 				goto bad;
161 			}
162 			p->next = NULL;
163 			p->size = size * i;
164 			bufp->next = p;
165 			bufp = p;
166 			zbuf.next_out = bufp->data;
167 			zbuf.avail_out = bufp->size;
168 		} else {
169 			/* Unexpect result. */
170 			SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
171 			    decomp, error, __LINE__,
172 			    zbuf.avail_in, zbuf.avail_out, zbuf.total_out);
173 			goto bad;
174 		}
175 	}
176 
177 	result = count = zbuf.total_out;
178 
179 	*out = malloc(result, M_CRYPTO_DATA, M_NOWAIT);
180 	if (*out == NULL) {
181 		SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
182 		    decomp, 0, __LINE__, 0, 0, 0);
183 		goto bad;
184 	}
185 	if (decomp)
186 		inflateEnd(&zbuf);
187 	else
188 		deflateEnd(&zbuf);
189 	output = *out;
190 	for (bufp = bufh; bufp != NULL; ) {
191 		if (count > bufp->size) {
192 			struct deflate_buf *p;
193 
194 			bcopy(bufp->data, *out, bufp->size);
195 			*out += bufp->size;
196 			count -= bufp->size;
197 			p = bufp;
198 			bufp = bufp->next;
199 			free(p, M_CRYPTO_DATA);
200 		} else {
201 			/* It should be the last buffer. */
202 			bcopy(bufp->data, *out, count);
203 			*out += count;
204 			free(bufp, M_CRYPTO_DATA);
205 			bufp = NULL;
206 			count = 0;
207 		}
208 	}
209 	*out = output;
210 	SDT_PROBE2(opencrypto, deflate, deflate_global, return, decomp, result);
211 	return result;
212 
213 bad:
214 	if (decomp)
215 		inflateEnd(&zbuf);
216 	else
217 		deflateEnd(&zbuf);
218 	for (bufp = bufh; bufp != NULL; ) {
219 		struct deflate_buf *p;
220 
221 		p = bufp;
222 		bufp = bufp->next;
223 		free(p, M_CRYPTO_DATA);
224 	}
225 bad2:
226 	*out = NULL;
227 	return 0;
228 }
229 
230 void *
231 z_alloc(nil, type, size)
232 	void *nil;
233 	u_int type, size;
234 {
235 	void *ptr;
236 
237 	ptr = malloc(type *size, M_CRYPTO_DATA, M_NOWAIT);
238 	return ptr;
239 }
240 
241 void
242 z_free(nil, ptr)
243 	void *nil, *ptr;
244 {
245 	free(ptr, M_CRYPTO_DATA);
246 }
247