Home | History | Annotate | Line # | Download | only in opencrypto
deflate.c revision 1.4.10.1
      1  1.4.10.1      elad /*	$NetBSD: deflate.c,v 1.4.10.1 2006/04/19 04:33:31 elad 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.4.10.1      elad __KERNEL_RCSID(0, "$NetBSD: deflate.c,v 1.4.10.1 2006/04/19 04:33:31 elad 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.2   thorpej 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.2   thorpej 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.1  jonathan 	struct deflate_buf buf[ZBUF];
     86       1.1  jonathan 
     87       1.1  jonathan 	bzero(&zbuf, sizeof(z_stream));
     88       1.1  jonathan 	for (j = 0; j < ZBUF; j++)
     89       1.1  jonathan 		buf[j].flag = 0;
     90       1.1  jonathan 
     91       1.1  jonathan 	zbuf.next_in = data;	/* data that is going to be processed */
     92       1.2   thorpej 	zbuf.zalloc = ocf_zalloc;
     93       1.2   thorpej 	zbuf.zfree = ocf_zfree;
     94       1.1  jonathan 	zbuf.opaque = Z_NULL;
     95       1.1  jonathan 	zbuf.avail_in = size;	/* Total length of data to be processed */
     96       1.1  jonathan 
     97       1.1  jonathan 	if (!decomp) {
     98  1.4.10.1      elad 		buf[i].out = malloc(size, M_CRYPTO_DATA, M_NOWAIT);
     99       1.1  jonathan 		if (buf[i].out == NULL)
    100       1.1  jonathan 			goto bad;
    101       1.1  jonathan 		buf[i].size = size;
    102       1.1  jonathan 		buf[i].flag = 1;
    103       1.1  jonathan 		i++;
    104       1.1  jonathan 	} else {
    105       1.1  jonathan 		/*
    106       1.1  jonathan 	 	 * Choose a buffer with 4x the size of the input buffer
    107       1.1  jonathan 	 	 * for the size of the output buffer in the case of
    108       1.1  jonathan 	 	 * decompression. If it's not sufficient, it will need to be
    109       1.1  jonathan 	 	 * updated while the decompression is going on
    110       1.1  jonathan 	 	 */
    111       1.1  jonathan 
    112  1.4.10.1      elad 		buf[i].size = size * 4;
    113  1.4.10.1      elad 		buf[i].out = malloc(buf[i].size, M_CRYPTO_DATA, M_NOWAIT);
    114       1.1  jonathan 		if (buf[i].out == NULL)
    115       1.1  jonathan 			goto bad;
    116       1.1  jonathan 		buf[i].flag = 1;
    117       1.1  jonathan 		i++;
    118       1.1  jonathan 	}
    119       1.1  jonathan 
    120       1.1  jonathan 	zbuf.next_out = buf[0].out;
    121       1.1  jonathan 	zbuf.avail_out = buf[0].size;
    122       1.1  jonathan 
    123       1.1  jonathan 	error = decomp ? inflateInit2(&zbuf, window_inflate) :
    124       1.1  jonathan 	    deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
    125       1.1  jonathan 		    window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
    126       1.1  jonathan 
    127       1.1  jonathan 	if (error != Z_OK)
    128       1.1  jonathan 		goto bad;
    129       1.1  jonathan 	for (;;) {
    130       1.1  jonathan 		error = decomp ? inflate(&zbuf, Z_PARTIAL_FLUSH) :
    131       1.1  jonathan 				 deflate(&zbuf, Z_PARTIAL_FLUSH);
    132       1.1  jonathan 		if (error != Z_OK && error != Z_STREAM_END)
    133       1.1  jonathan 			goto bad;
    134       1.1  jonathan 		else if (zbuf.avail_in == 0 && zbuf.avail_out != 0)
    135       1.1  jonathan 			goto end;
    136       1.1  jonathan 		else if (zbuf.avail_out == 0 && i < (ZBUF - 1)) {
    137       1.1  jonathan 			/* we need more output space, allocate size */
    138  1.4.10.1      elad 			buf[i].out = malloc(size, M_CRYPTO_DATA, M_NOWAIT);
    139       1.1  jonathan 			if (buf[i].out == NULL)
    140       1.1  jonathan 				goto bad;
    141       1.1  jonathan 			zbuf.next_out = buf[i].out;
    142       1.1  jonathan 			buf[i].size = size;
    143       1.1  jonathan 			buf[i].flag = 1;
    144       1.1  jonathan 			zbuf.avail_out = buf[i].size;
    145       1.1  jonathan 			i++;
    146       1.1  jonathan 		} else
    147       1.1  jonathan 			goto bad;
    148       1.1  jonathan 	}
    149       1.1  jonathan 
    150       1.1  jonathan end:
    151       1.1  jonathan 	result = count = zbuf.total_out;
    152       1.1  jonathan 
    153  1.4.10.1      elad 	*out = malloc(result, M_CRYPTO_DATA, M_NOWAIT);
    154       1.1  jonathan 	if (*out == NULL)
    155       1.1  jonathan 		goto bad;
    156       1.1  jonathan 	if (decomp)
    157       1.1  jonathan 		inflateEnd(&zbuf);
    158       1.1  jonathan 	else
    159       1.1  jonathan 		deflateEnd(&zbuf);
    160       1.1  jonathan 	output = *out;
    161       1.1  jonathan 	for (j = 0; buf[j].flag != 0; j++) {
    162       1.1  jonathan 		if (count > buf[j].size) {
    163       1.1  jonathan 			bcopy(buf[j].out, *out, buf[j].size);
    164       1.1  jonathan 			*out += buf[j].size;
    165       1.1  jonathan 			FREE(buf[j].out, M_CRYPTO_DATA);
    166       1.1  jonathan 			count -= buf[j].size;
    167       1.1  jonathan 		} else {
    168       1.1  jonathan 			/* it should be the last buffer */
    169       1.1  jonathan 			bcopy(buf[j].out, *out, count);
    170       1.1  jonathan 			*out += count;
    171       1.1  jonathan 			FREE(buf[j].out, M_CRYPTO_DATA);
    172       1.1  jonathan 			count = 0;
    173       1.1  jonathan 		}
    174       1.1  jonathan 	}
    175       1.1  jonathan 	*out = output;
    176       1.1  jonathan 	return result;
    177       1.1  jonathan 
    178       1.1  jonathan bad:
    179       1.1  jonathan 	*out = NULL;
    180       1.1  jonathan 	for (j = 0; buf[j].flag != 0; j++)
    181       1.1  jonathan 		FREE(buf[j].out, M_CRYPTO_DATA);
    182       1.1  jonathan 	if (decomp)
    183       1.1  jonathan 		inflateEnd(&zbuf);
    184       1.1  jonathan 	else
    185       1.1  jonathan 		deflateEnd(&zbuf);
    186       1.1  jonathan 	return 0;
    187       1.1  jonathan }
    188