Home | History | Annotate | Line # | Download | only in opencrypto
deflate.c revision 1.12
      1 /*	$NetBSD: deflate.c,v 1.12 2009/03/14 15:36:24 dsl Exp $ */
      2 /*	$FreeBSD: src/sys/opencrypto/deflate.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $	*/
      3 /* $OpenBSD: deflate.c,v 1.3 2001/08/20 02:45:22 hugh Exp $ */
      4 
      5 /*
      6  * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj (at) wabbitt.org)
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  *
     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.
     19  *
     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.
     30  */
     31 
     32 /*
     33  * This file contains a wrapper around the deflate algo compression
     34  * functions using the zlib library (see net/zlib.{c,h})
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: deflate.c,v 1.12 2009/03/14 15:36:24 dsl Exp $");
     39 
     40 #include <sys/types.h>
     41 #include <sys/malloc.h>
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <net/zlib.h>
     45 
     46 #include <opencrypto/cryptodev.h>
     47 #include <opencrypto/deflate.h>
     48 
     49 int window_inflate = -1 * MAX_WBITS;
     50 int window_deflate = -12;
     51 
     52 /*
     53  * This function takes a block of data and (de)compress it using the deflate
     54  * algorithm
     55  */
     56 
     57 static void *
     58 ocf_zalloc(void *nil, u_int type, u_int size)
     59 {
     60 	void *ptr;
     61 
     62 	ptr = malloc(type *size, M_CRYPTO_DATA, M_NOWAIT);
     63 	return ptr;
     64 }
     65 
     66 static void
     67 ocf_zfree(void *nil, void *ptr)
     68 {
     69 	free(ptr, M_CRYPTO_DATA);
     70 }
     71 
     72 u_int32_t
     73 deflate_global(u_int8_t *data, u_int32_t size, int decomp, u_int8_t **out)
     74 {
     75 	/* decomp indicates whether we compress (0) or decompress (1) */
     76 
     77 	z_stream zbuf;
     78 	u_int8_t *output;
     79 	u_int32_t count, result;
     80 	int error, i = 0, j;
     81 	struct deflate_buf *buf, *tmp;
     82 	size_t len, old_len;
     83 
     84 	len = ZBUF;
     85 	buf = malloc(len*sizeof(struct deflate_buf), M_CRYPTO_DATA, M_NOWAIT);
     86 	if (buf == NULL)
     87 		return 0;
     88 
     89 	memset(&zbuf, 0, sizeof(z_stream));
     90 	for (j = 0; j < len; j++)
     91 		buf[j].flag = 0;
     92 
     93 	zbuf.next_in = data;	/* data that is going to be processed */
     94 	zbuf.zalloc = ocf_zalloc;
     95 	zbuf.zfree = ocf_zfree;
     96 	zbuf.opaque = Z_NULL;
     97 	zbuf.avail_in = size;	/* Total length of data to be processed */
     98 
     99 	if (!decomp) {
    100 		buf[i].out = malloc(size, M_CRYPTO_DATA, M_NOWAIT);
    101 		if (buf[i].out == NULL)
    102 			goto bad;
    103 		buf[i].size = size;
    104 		buf[i].flag = 1;
    105 		i++;
    106 	} else {
    107 		/*
    108 	 	 * Choose a buffer with 4x the size of the input buffer
    109 	 	 * for the size of the output buffer in the case of
    110 	 	 * decompression. If it's not sufficient, it will need to be
    111 	 	 * updated while the decompression is going on
    112 	 	 */
    113 
    114 		buf[i].size = size * 4;
    115 		buf[i].out = malloc(buf[i].size, M_CRYPTO_DATA, M_NOWAIT);
    116 		if (buf[i].out == NULL)
    117 			goto bad;
    118 		buf[i].flag = 1;
    119 		i++;
    120 	}
    121 
    122 	zbuf.next_out = buf[0].out;
    123 	zbuf.avail_out = buf[0].size;
    124 
    125 	error = decomp ? inflateInit2(&zbuf, window_inflate) :
    126 	    deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
    127 		    window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
    128 
    129 	if (error != Z_OK)
    130 		goto bad;
    131 	for (;;) {
    132 		error = decomp ? inflate(&zbuf, Z_PARTIAL_FLUSH) :
    133 				 deflate(&zbuf, Z_PARTIAL_FLUSH);
    134 		if (error != Z_OK && error != Z_STREAM_END)
    135 			goto bad;
    136 		else if (zbuf.avail_in == 0 && zbuf.avail_out != 0)
    137 			goto end;
    138 		else if (zbuf.avail_out == 0) {
    139 			if (i == (len-1)) {
    140 				old_len = i;
    141 				len += ZBUF;
    142 				tmp = realloc(buf,len*sizeof(struct deflate_buf),
    143 							  M_CRYPTO_DATA, M_NOWAIT);
    144 				if (tmp == NULL)
    145 					goto bad;
    146 				buf = tmp;
    147 				for (j = old_len; j < len; j++)
    148 					buf[j].flag = 0;
    149 			}
    150 			/* we need more output space, allocate size */
    151 			buf[i].out = malloc(size, M_CRYPTO_DATA, M_NOWAIT);
    152 			if (buf[i].out == NULL)
    153 				goto bad;
    154 			zbuf.next_out = buf[i].out;
    155 			buf[i].size = size;
    156 			buf[i].flag = 1;
    157 			zbuf.avail_out = buf[i].size;
    158 			i++;
    159 		} else
    160 			goto bad;
    161 	}
    162 
    163 end:
    164 	result = count = zbuf.total_out;
    165 
    166 	*out = malloc(result, M_CRYPTO_DATA, M_NOWAIT);
    167 	if (*out == NULL)
    168 		goto bad;
    169 	if (decomp)
    170 		inflateEnd(&zbuf);
    171 	else
    172 		deflateEnd(&zbuf);
    173 	output = *out;
    174 	for (j = 0; buf[j].flag != 0; j++) {
    175 		if (count > buf[j].size) {
    176 			memcpy(*out, buf[j].out, buf[j].size);
    177 			*out += buf[j].size;
    178 			free(buf[j].out, M_CRYPTO_DATA);
    179 			count -= buf[j].size;
    180 		} else {
    181 			/* it should be the last buffer */
    182 			memcpy(*out, buf[j].out, count);
    183 			*out += count;
    184 			free(buf[j].out, M_CRYPTO_DATA);
    185 			count = 0;
    186 		}
    187 	}
    188 	free(buf, M_CRYPTO_DATA);
    189 	*out = output;
    190 	return result;
    191 
    192 bad:
    193 	*out = NULL;
    194 	for (j = 0; buf[j].flag != 0; j++)
    195 		free(buf[j].out, M_CRYPTO_DATA);
    196 	free(buf, M_CRYPTO_DATA);
    197 	if (decomp)
    198 		inflateEnd(&zbuf);
    199 	else
    200 		deflateEnd(&zbuf);
    201 	return 0;
    202 }
    203