Home | History | Annotate | Line # | Download | only in opencrypto
deflate.c revision 1.8
      1  1.8  degroote /*	$NetBSD: deflate.c,v 1.8 2007/05/21 11:35:16 degroote 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.8  degroote __KERNEL_RCSID(0, "$NetBSD: deflate.c,v 1.8 2007/05/21 11:35:16 degroote 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.2   thorpej static void *
     58  1.7  christos ocf_zalloc(void *nil, u_int type, u_int size)
     59  1.2   thorpej {
     60  1.2   thorpej 	void *ptr;
     61  1.2   thorpej 
     62  1.2   thorpej 	ptr = malloc(type *size, M_CRYPTO_DATA, M_NOWAIT);
     63  1.2   thorpej 	return ptr;
     64  1.2   thorpej }
     65  1.2   thorpej 
     66  1.2   thorpej static void
     67  1.7  christos ocf_zfree(void *nil, void *ptr)
     68  1.2   thorpej {
     69  1.2   thorpej 	free(ptr, M_CRYPTO_DATA);
     70  1.2   thorpej }
     71  1.2   thorpej 
     72  1.1  jonathan u_int32_t
     73  1.1  jonathan deflate_global(data, size, decomp, out)
     74  1.1  jonathan 	u_int8_t *data;
     75  1.1  jonathan 	u_int32_t size;
     76  1.1  jonathan 	int decomp;
     77  1.1  jonathan 	u_int8_t **out;
     78  1.1  jonathan {
     79  1.1  jonathan 	/* decomp indicates whether we compress (0) or decompress (1) */
     80  1.1  jonathan 
     81  1.1  jonathan 	z_stream zbuf;
     82  1.1  jonathan 	u_int8_t *output;
     83  1.1  jonathan 	u_int32_t count, result;
     84  1.1  jonathan 	int error, i = 0, j;
     85  1.8  degroote 	struct deflate_buf *buf, *tmp;
     86  1.8  degroote 	size_t len, old_len;
     87  1.1  jonathan 
     88  1.8  degroote 	len = ZBUF;
     89  1.8  degroote 	buf = malloc(len*sizeof(struct deflate_buf), M_CRYPTO_DATA, M_NOWAIT);
     90  1.8  degroote 	if (buf == NULL)
     91  1.8  degroote 		return 0;
     92  1.8  degroote 
     93  1.8  degroote 	memset(&zbuf, 0, sizeof(z_stream));
     94  1.8  degroote 	for (j = 0; j < len; j++)
     95  1.1  jonathan 		buf[j].flag = 0;
     96  1.1  jonathan 
     97  1.1  jonathan 	zbuf.next_in = data;	/* data that is going to be processed */
     98  1.2   thorpej 	zbuf.zalloc = ocf_zalloc;
     99  1.2   thorpej 	zbuf.zfree = ocf_zfree;
    100  1.1  jonathan 	zbuf.opaque = Z_NULL;
    101  1.1  jonathan 	zbuf.avail_in = size;	/* Total length of data to be processed */
    102  1.1  jonathan 
    103  1.1  jonathan 	if (!decomp) {
    104  1.5  christos 		buf[i].out = malloc(size, M_CRYPTO_DATA, M_NOWAIT);
    105  1.1  jonathan 		if (buf[i].out == NULL)
    106  1.1  jonathan 			goto bad;
    107  1.1  jonathan 		buf[i].size = size;
    108  1.1  jonathan 		buf[i].flag = 1;
    109  1.1  jonathan 		i++;
    110  1.1  jonathan 	} else {
    111  1.1  jonathan 		/*
    112  1.1  jonathan 	 	 * Choose a buffer with 4x the size of the input buffer
    113  1.1  jonathan 	 	 * for the size of the output buffer in the case of
    114  1.1  jonathan 	 	 * decompression. If it's not sufficient, it will need to be
    115  1.1  jonathan 	 	 * updated while the decompression is going on
    116  1.1  jonathan 	 	 */
    117  1.1  jonathan 
    118  1.5  christos 		buf[i].size = size * 4;
    119  1.5  christos 		buf[i].out = malloc(buf[i].size, M_CRYPTO_DATA, M_NOWAIT);
    120  1.1  jonathan 		if (buf[i].out == NULL)
    121  1.1  jonathan 			goto bad;
    122  1.1  jonathan 		buf[i].flag = 1;
    123  1.1  jonathan 		i++;
    124  1.1  jonathan 	}
    125  1.1  jonathan 
    126  1.1  jonathan 	zbuf.next_out = buf[0].out;
    127  1.1  jonathan 	zbuf.avail_out = buf[0].size;
    128  1.1  jonathan 
    129  1.1  jonathan 	error = decomp ? inflateInit2(&zbuf, window_inflate) :
    130  1.1  jonathan 	    deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
    131  1.1  jonathan 		    window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
    132  1.1  jonathan 
    133  1.1  jonathan 	if (error != Z_OK)
    134  1.1  jonathan 		goto bad;
    135  1.1  jonathan 	for (;;) {
    136  1.1  jonathan 		error = decomp ? inflate(&zbuf, Z_PARTIAL_FLUSH) :
    137  1.1  jonathan 				 deflate(&zbuf, Z_PARTIAL_FLUSH);
    138  1.1  jonathan 		if (error != Z_OK && error != Z_STREAM_END)
    139  1.1  jonathan 			goto bad;
    140  1.1  jonathan 		else if (zbuf.avail_in == 0 && zbuf.avail_out != 0)
    141  1.1  jonathan 			goto end;
    142  1.8  degroote 		else if (zbuf.avail_out == 0) {
    143  1.8  degroote 			if (i == (len-1)) {
    144  1.8  degroote 				old_len = i;
    145  1.8  degroote 				len += ZBUF;
    146  1.8  degroote 				tmp = realloc(buf,len*sizeof(struct deflate_buf),
    147  1.8  degroote 							  M_CRYPTO_DATA, M_NOWAIT);
    148  1.8  degroote 				if (tmp == NULL)
    149  1.8  degroote 					goto bad;
    150  1.8  degroote 				buf = tmp;
    151  1.8  degroote 				for (j = old_len; j < len; j++)
    152  1.8  degroote 					buf[j].flag = 0;
    153  1.8  degroote 			}
    154  1.1  jonathan 			/* we need more output space, allocate size */
    155  1.5  christos 			buf[i].out = malloc(size, M_CRYPTO_DATA, M_NOWAIT);
    156  1.1  jonathan 			if (buf[i].out == NULL)
    157  1.1  jonathan 				goto bad;
    158  1.1  jonathan 			zbuf.next_out = buf[i].out;
    159  1.1  jonathan 			buf[i].size = size;
    160  1.1  jonathan 			buf[i].flag = 1;
    161  1.1  jonathan 			zbuf.avail_out = buf[i].size;
    162  1.1  jonathan 			i++;
    163  1.1  jonathan 		} else
    164  1.1  jonathan 			goto bad;
    165  1.1  jonathan 	}
    166  1.1  jonathan 
    167  1.1  jonathan end:
    168  1.1  jonathan 	result = count = zbuf.total_out;
    169  1.1  jonathan 
    170  1.5  christos 	*out = malloc(result, M_CRYPTO_DATA, M_NOWAIT);
    171  1.1  jonathan 	if (*out == NULL)
    172  1.1  jonathan 		goto bad;
    173  1.1  jonathan 	if (decomp)
    174  1.1  jonathan 		inflateEnd(&zbuf);
    175  1.1  jonathan 	else
    176  1.1  jonathan 		deflateEnd(&zbuf);
    177  1.1  jonathan 	output = *out;
    178  1.1  jonathan 	for (j = 0; buf[j].flag != 0; j++) {
    179  1.1  jonathan 		if (count > buf[j].size) {
    180  1.8  degroote 			memcpy(buf[j].out, *out, buf[j].size);
    181  1.1  jonathan 			*out += buf[j].size;
    182  1.8  degroote 			free(buf[j].out, M_CRYPTO_DATA);
    183  1.1  jonathan 			count -= buf[j].size;
    184  1.1  jonathan 		} else {
    185  1.1  jonathan 			/* it should be the last buffer */
    186  1.8  degroote 			memcpy(buf[j].out, *out, count);
    187  1.1  jonathan 			*out += count;
    188  1.8  degroote 			free(buf[j].out, M_CRYPTO_DATA);
    189  1.1  jonathan 			count = 0;
    190  1.1  jonathan 		}
    191  1.1  jonathan 	}
    192  1.8  degroote 	free(buf, M_CRYPTO_DATA);
    193  1.1  jonathan 	*out = output;
    194  1.1  jonathan 	return result;
    195  1.1  jonathan 
    196  1.1  jonathan bad:
    197  1.1  jonathan 	*out = NULL;
    198  1.1  jonathan 	for (j = 0; buf[j].flag != 0; j++)
    199  1.8  degroote 		free(buf[j].out, M_CRYPTO_DATA);
    200  1.8  degroote 	free(buf, M_CRYPTO_DATA);
    201  1.1  jonathan 	if (decomp)
    202  1.1  jonathan 		inflateEnd(&zbuf);
    203  1.1  jonathan 	else
    204  1.1  jonathan 		deflateEnd(&zbuf);
    205  1.1  jonathan 	return 0;
    206  1.1  jonathan }
    207