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