Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / sys / opencrypto / deflate.c
1 /*      $FreeBSD: src/sys/opencrypto/deflate.c,v 1.5 2008/10/23 15:53:51 des Exp $      */
2 /* $OpenBSD: deflate.c,v 1.3 2001/08/20 02:45:22 hugh Exp $ */
3
4 /*-
5  * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *   notice, this list of conditions and the following disclaimer in the
15  *   documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *   derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 /*
32  * This file contains a wrapper around the deflate algo compression
33  * functions using the zlib library (see net/zlib.{c,h})
34  */
35
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/malloc.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <net/zlib.h>
42
43 #include <opencrypto/cryptodev.h>
44 #include <opencrypto/deflate.h>
45
46 int window_inflate = -1 * MAX_WBITS;
47 int window_deflate = -12;
48
49 /*
50  * This function takes a block of data and (de)compress it using the deflate
51  * algorithm
52  */
53
54 u_int32_t
55 deflate_global(u_int8_t *data, u_int32_t size, int decomp, u_int8_t **out)
56 {
57         /* decomp indicates whether we compress (0) or decompress (1) */
58
59         z_stream zbuf;
60         u_int8_t *output;
61         u_int32_t count, result;
62         int error, i = 0, j;
63         struct deflate_buf buf[ZBUF];
64
65         bzero(&zbuf, sizeof(z_stream));
66         for (j = 0; j < ZBUF; j++)
67                 buf[j].flag = 0;
68
69         zbuf.next_in = data;    /* data that is going to be processed */
70         zbuf.zalloc = z_alloc;
71         zbuf.zfree = z_free;
72         zbuf.opaque = Z_NULL;
73         zbuf.avail_in = size;   /* Total length of data to be processed */
74
75         if (!decomp) {
76                 buf[i].out = kmalloc((u_long) size, M_CRYPTO_DATA, M_WAITOK);
77                 if (buf[i].out == NULL)
78                         goto bad;
79                 buf[i].size = size;
80                 buf[i].flag = 1;
81                 i++;
82         } else {
83                 /*
84                  * Choose a buffer with 4x the size of the input buffer
85                  * for the size of the output buffer in the case of
86                  * decompression. If it's not sufficient, it will need to be
87                  * updated while the decompression is going on
88                  */
89
90                 buf[i].out = kmalloc((u_long) (size * 4),
91                                      M_CRYPTO_DATA, M_WAITOK);
92                 if (buf[i].out == NULL)
93                         goto bad;
94                 buf[i].size = size * 4;
95                 buf[i].flag = 1;
96                 i++;
97         }
98
99         zbuf.next_out = buf[0].out;
100         zbuf.avail_out = buf[0].size;
101
102         error = decomp ? inflateInit2(&zbuf, window_inflate) :
103             deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
104                     window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
105
106         if (error != Z_OK)
107                 goto bad;
108         for (;;) {
109                 error = decomp ? inflate(&zbuf, Z_PARTIAL_FLUSH) :
110                                  deflate(&zbuf, Z_PARTIAL_FLUSH);
111                 if (error != Z_OK && error != Z_STREAM_END)
112                         goto bad;
113                 else if (zbuf.avail_in == 0 && zbuf.avail_out != 0)
114                         goto end;
115                 else if (zbuf.avail_out == 0 && i < (ZBUF - 1)) {
116                         /* we need more output space, allocate size */
117                         buf[i].out = kmalloc((u_long) size,
118                                              M_CRYPTO_DATA, M_WAITOK);
119                         if (buf[i].out == NULL)
120                                 goto bad;
121                         zbuf.next_out = buf[i].out;
122                         buf[i].size = size;
123                         buf[i].flag = 1;
124                         zbuf.avail_out = buf[i].size;
125                         i++;
126                 } else
127                         goto bad;
128         }
129
130 end:
131         result = count = zbuf.total_out;
132
133         *out = kmalloc((u_long) result, M_CRYPTO_DATA, M_WAITOK);
134         if (*out == NULL)
135                 goto bad;
136         if (decomp)
137                 inflateEnd(&zbuf);
138         else
139                 deflateEnd(&zbuf);
140         output = *out;
141         for (j = 0; buf[j].flag != 0; j++) {
142                 if (count > buf[j].size) {
143                         bcopy(buf[j].out, *out, buf[j].size);
144                         *out += buf[j].size;
145                         kfree(buf[j].out, M_CRYPTO_DATA);
146                         count -= buf[j].size;
147                 } else {
148                         /* it should be the last buffer */
149                         bcopy(buf[j].out, *out, count);
150                         *out += count;
151                         kfree(buf[j].out, M_CRYPTO_DATA);
152                         count = 0;
153                 }
154         }
155         *out = output;
156         return result;
157
158 bad:
159         *out = NULL;
160         for (j = 0; buf[j].flag != 0; j++)
161                 kfree(buf[j].out, M_CRYPTO_DATA);
162         if (decomp)
163                 inflateEnd(&zbuf);
164         else
165                 deflateEnd(&zbuf);
166         return 0;
167 }
168
169 void *
170 z_alloc(void *nil, u_int type, u_int size)
171 {
172         void *ptr;
173
174         ptr = kmalloc(type *size, M_CRYPTO_DATA, M_WAITOK);
175         return ptr;
176 }
177
178 void
179 z_free(void *nil, void *ptr)
180 {
181         kfree(ptr, M_CRYPTO_DATA);
182 }