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