xref: /freebsd/sys/opencrypto/cryptodeflate.c (revision 315ee00f)
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 sys/contrib/zlib)
33  */
34 
35 #include <sys/cdefs.h>
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/malloc.h>
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/sdt.h>
42 #include <sys/systm.h>
43 #include <contrib/zlib/zlib.h>
44 
45 #include <opencrypto/cryptodev.h>
46 #include <opencrypto/deflate.h>
47 
48 SDT_PROVIDER_DECLARE(opencrypto);
49 SDT_PROBE_DEFINE2(opencrypto, deflate, deflate_global, entry,
50     "int", "uint32_t");
51 SDT_PROBE_DEFINE6(opencrypto, deflate, deflate_global, bad,
52     "int", "int", "int", "int", "int", "int");
53 SDT_PROBE_DEFINE6(opencrypto, deflate, deflate_global, iter,
54     "int", "int", "int", "int", "int", "int");
55 SDT_PROBE_DEFINE2(opencrypto, deflate, deflate_global, return,
56     "int", "uint32_t");
57 
58 int window_inflate = -1 * MAX_WBITS;
59 int window_deflate = -12;
60 
61 static void *
62 crypto_zalloc(void *nil, u_int type, u_int size)
63 {
64 	void *ptr;
65 
66 	ptr = malloc(type *size, M_CRYPTO_DATA, M_NOWAIT);
67 	return ptr;
68 }
69 
70 static void
71 crypto_zfree(void *nil, void *ptr)
72 {
73 
74 	free(ptr, M_CRYPTO_DATA);
75 }
76 
77 /*
78  * This function takes a block of data and (de)compress it using the deflate
79  * algorithm
80  */
81 
82 uint32_t
83 deflate_global(uint8_t *data, uint32_t size, int decomp, uint8_t **out)
84 {
85 	/* decomp indicates whether we compress (0) or decompress (1) */
86 
87 	z_stream zbuf;
88 	uint8_t *output;
89 	uint32_t count, result;
90 	int error, i;
91 	struct deflate_buf *bufh, *bufp;
92 
93 	SDT_PROBE2(opencrypto, deflate, deflate_global, entry, decomp, size);
94 
95 	bufh = bufp = NULL;
96 	if (!decomp) {
97 		i = 1;
98 	} else {
99 		/*
100 	 	 * Choose a buffer with 4x the size of the input buffer
101 	 	 * for the size of the output buffer in the case of
102 	 	 * decompression. If it's not sufficient, it will need to be
103 	 	 * updated while the decompression is going on.
104 	 	 */
105 		i = 4;
106 	}
107 	/*
108 	 * Make sure we do have enough output space.  Repeated calls to
109 	 * deflate need at least 6 bytes of output buffer space to avoid
110 	 * repeated markers.  We will always provide at least 16 bytes.
111 	 */
112 	while ((size * i) < 16)
113 		i++;
114 
115 	bufh = bufp = malloc(sizeof(*bufp) + (size_t)(size * i),
116 	    M_CRYPTO_DATA, M_NOWAIT);
117 	if (bufp == NULL) {
118 		SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
119 		    decomp, 0, __LINE__, 0, 0, 0);
120 		goto bad2;
121 	}
122 	bufp->next = NULL;
123 	bufp->size = size * i;
124 
125 	bzero(&zbuf, sizeof(z_stream));
126 	zbuf.zalloc = crypto_zalloc;
127 	zbuf.zfree = crypto_zfree;
128 	zbuf.opaque = Z_NULL;
129 	zbuf.next_in = data;	/* Data that is going to be processed. */
130 	zbuf.avail_in = size;	/* Total length of data to be processed. */
131 	zbuf.next_out = bufp->data;
132 	zbuf.avail_out = bufp->size;
133 
134 	error = decomp ? inflateInit2(&zbuf, window_inflate) :
135 	    deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
136 		    window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
137 	if (error != Z_OK) {
138 		SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
139 		    decomp, error, __LINE__, 0, 0, 0);
140 		goto bad;
141 	}
142 
143 	for (;;) {
144 		error = decomp ? inflate(&zbuf, Z_SYNC_FLUSH) :
145 				 deflate(&zbuf, Z_FINISH);
146 		if (error != Z_OK && error != Z_STREAM_END) {
147 			SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
148 			    decomp, error, __LINE__,
149 			    zbuf.avail_in, zbuf.avail_out, zbuf.total_out);
150 			goto bad;
151 		}
152 		SDT_PROBE6(opencrypto, deflate, deflate_global, iter,
153 		    decomp, error, __LINE__,
154 		    zbuf.avail_in, zbuf.avail_out, 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_PROBE6(opencrypto, deflate, deflate_global,
169 				    bad, decomp, 0, __LINE__, 0, 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 			SDT_PROBE6(opencrypto, deflate, deflate_global,
181 			    bad, decomp, error, __LINE__,
182 			    zbuf.avail_in, zbuf.avail_out, zbuf.total_out);
183 			goto bad;
184 		}
185 	}
186 
187 	result = count = zbuf.total_out;
188 
189 	*out = malloc(result, M_CRYPTO_DATA, M_NOWAIT);
190 	if (*out == NULL) {
191 		SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
192 		    decomp, 0, __LINE__, 0, 0, 0);
193 		goto bad;
194 	}
195 	if (decomp)
196 		inflateEnd(&zbuf);
197 	else
198 		deflateEnd(&zbuf);
199 	output = *out;
200 	for (bufp = bufh; bufp != NULL; ) {
201 		if (count > bufp->size) {
202 			struct deflate_buf *p;
203 
204 			bcopy(bufp->data, *out, bufp->size);
205 			*out += bufp->size;
206 			count -= bufp->size;
207 			p = bufp;
208 			bufp = bufp->next;
209 			free(p, M_CRYPTO_DATA);
210 		} else {
211 			/* It should be the last buffer. */
212 			bcopy(bufp->data, *out, count);
213 			*out += count;
214 			free(bufp, M_CRYPTO_DATA);
215 			bufp = NULL;
216 			count = 0;
217 		}
218 	}
219 	*out = output;
220 	SDT_PROBE2(opencrypto, deflate, deflate_global, return, decomp, result);
221 	return result;
222 
223 bad:
224 	if (decomp)
225 		inflateEnd(&zbuf);
226 	else
227 		deflateEnd(&zbuf);
228 	for (bufp = bufh; bufp != NULL; ) {
229 		struct deflate_buf *p;
230 
231 		p = bufp;
232 		bufp = bufp->next;
233 		free(p, M_CRYPTO_DATA);
234 	}
235 bad2:
236 	*out = NULL;
237 	return 0;
238 }
239