]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/opencrypto/deflate.c
MFC r199885:
[FreeBSD/stable/8.git] / sys / opencrypto / deflate.c
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 net/zlib.{c,h})
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include "opt_kdtrace.h"
39
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/malloc.h>
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/sdt.h>
46 #include <sys/systm.h>
47 #include <net/zlib.h>
48
49 #include <opencrypto/cryptodev.h>
50 #include <opencrypto/deflate.h>
51
52 SDT_PROVIDER_DECLARE(opencrypto);
53 SDT_PROBE_DEFINE2(opencrypto, deflate, deflate_global, entry,
54     "int", "u_int32_t");
55 SDT_PROBE_DEFINE5(opencrypto, deflate, deflate_global, bad,
56     "int", "int", "int", "int", "int");
57 SDT_PROBE_DEFINE5(opencrypto, deflate, deflate_global, iter,
58     "int", "int", "int", "int", "int");
59 SDT_PROBE_DEFINE2(opencrypto, deflate, deflate_global, return,
60     "int", "u_int32_t");
61
62 int window_inflate = -1 * MAX_WBITS;
63 int window_deflate = -12;
64
65 /*
66  * This function takes a block of data and (de)compress it using the deflate
67  * algorithm
68  */
69
70 u_int32_t
71 deflate_global(data, size, decomp, out)
72         u_int8_t *data;
73         u_int32_t size;
74         int decomp;
75         u_int8_t **out;
76 {
77         /* decomp indicates whether we compress (0) or decompress (1) */
78
79         z_stream zbuf;
80         u_int8_t *output;
81         u_int32_t count, result;
82         int error, i = 0, j;
83         struct deflate_buf buf[ZBUF];
84
85         SDT_PROBE2(opencrypto, deflate, deflate_global, entry, decomp, size);
86
87         bzero(&zbuf, sizeof(z_stream));
88         for (j = 0; j < ZBUF; j++)
89                 buf[j].flag = 0;
90
91         zbuf.next_in = data;    /* data that is going to be processed */
92         zbuf.zalloc = z_alloc;
93         zbuf.zfree = z_free;
94         zbuf.opaque = Z_NULL;
95         zbuf.avail_in = size;   /* Total length of data to be processed */
96
97         if (!decomp) {
98                 buf[i].out = malloc((u_long) size, M_CRYPTO_DATA, 
99                     M_NOWAIT);
100                 if (buf[i].out == NULL) {
101                         SDT_PROBE3(opencrypto, deflate, deflate_global, bad,
102                             decomp, 0, __LINE__);
103                         goto bad;
104                 }
105                 buf[i].size = size;
106                 buf[i].flag = 1;
107                 i++;
108         } else {
109                 /*
110                  * Choose a buffer with 4x the size of the input buffer
111                  * for the size of the output buffer in the case of
112                  * decompression. If it's not sufficient, it will need to be
113                  * updated while the decompression is going on
114                  */
115
116                 buf[i].out = malloc((u_long) (size * 4), 
117                     M_CRYPTO_DATA, M_NOWAIT);
118                 if (buf[i].out == NULL) {
119                         SDT_PROBE3(opencrypto, deflate, deflate_global, bad,
120                             decomp, 0, __LINE__);
121                         goto bad;
122                 }
123                 buf[i].size = size * 4;
124                 buf[i].flag = 1;
125                 i++;
126         }
127
128         zbuf.next_out = buf[0].out;
129         zbuf.avail_out = buf[0].size;
130
131         error = decomp ? inflateInit2(&zbuf, window_inflate) :
132             deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
133                     window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
134
135         if (error != Z_OK) {
136                 SDT_PROBE3(opencrypto, deflate, deflate_global, bad,
137                     decomp, error, __LINE__);
138                 goto bad;
139         }
140         for (;;) {
141                 error = decomp ? inflate(&zbuf, Z_PARTIAL_FLUSH) :
142                                  deflate(&zbuf, Z_PARTIAL_FLUSH);
143                 if (error != Z_OK && error != Z_STREAM_END) {
144                         /*
145                          * Unfortunately we are limited to 5 arguments,
146                          * thus use two probes.
147                          */
148                         SDT_PROBE5(opencrypto, deflate, deflate_global, bad,
149                             decomp, error, __LINE__,
150                             zbuf.avail_in, zbuf.avail_out);
151                         SDT_PROBE5(opencrypto, deflate, deflate_global, bad,
152                             decomp, error, __LINE__,
153                             zbuf.state->dummy, zbuf.total_out);
154                         goto bad;
155                 }
156                 else if (zbuf.avail_in == 0 && zbuf.avail_out != 0)
157                         goto end;
158                 else if (zbuf.avail_out == 0 && i < (ZBUF - 1)) {
159                         /* we need more output space, allocate size */
160                         buf[i].out = malloc((u_long) size,
161                             M_CRYPTO_DATA, M_NOWAIT);
162                         if (buf[i].out == NULL) {
163                                 SDT_PROBE3(opencrypto, deflate, deflate_global,
164                                     bad, decomp, 0, __LINE__);
165                                 goto bad;
166                         }
167                         zbuf.next_out = buf[i].out;
168                         buf[i].size = size;
169                         buf[i].flag = 1;
170                         zbuf.avail_out = buf[i].size;
171                         i++;
172                 } else {
173                         /*
174                          * Unfortunately we are limited to 5 arguments,
175                          * thus, again, use two probes.
176                          */
177                         SDT_PROBE5(opencrypto, deflate, deflate_global, bad,
178                             decomp, error, __LINE__,
179                             zbuf.avail_in, zbuf.avail_out);
180                         SDT_PROBE5(opencrypto, deflate, deflate_global, bad,
181                             decomp, error, __LINE__,
182                             zbuf.state->dummy, zbuf.total_out);
183                         goto bad;
184                 }
185         }
186
187 end:
188         result = count = zbuf.total_out;
189
190         *out = malloc((u_long) result, M_CRYPTO_DATA, M_NOWAIT);
191         if (*out == NULL) {
192                 SDT_PROBE3(opencrypto, deflate, deflate_global, bad,
193                     decomp, 0, __LINE__);
194                 goto bad;
195         }
196         if (decomp)
197                 inflateEnd(&zbuf);
198         else
199                 deflateEnd(&zbuf);
200         output = *out;
201         for (j = 0; buf[j].flag != 0; j++) {
202                 if (count > buf[j].size) {
203                         bcopy(buf[j].out, *out, buf[j].size);
204                         *out += buf[j].size;
205                         free(buf[j].out, M_CRYPTO_DATA);
206                         count -= buf[j].size;
207                 } else {
208                         /* it should be the last buffer */
209                         bcopy(buf[j].out, *out, count);
210                         *out += count;
211                         free(buf[j].out, M_CRYPTO_DATA);
212                         count = 0;
213                 }
214         }
215         *out = output;
216         SDT_PROBE2(opencrypto, deflate, deflate_global, return, decomp, result);
217         return result;
218
219 bad:
220         *out = NULL;
221         for (j = 0; buf[j].flag != 0; j++)
222                 free(buf[j].out, M_CRYPTO_DATA);
223         if (decomp)
224                 inflateEnd(&zbuf);
225         else
226                 deflateEnd(&zbuf);
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 }