Home | History | Annotate | Line # | Download | only in opencrypto
deflate.c revision 1.14
      1  1.14  drochner /*	$NetBSD: deflate.c,v 1.14 2011/02/10 21:17:49 drochner 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.11        ad  * 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.14  drochner __KERNEL_RCSID(0, "$NetBSD: deflate.c,v 1.14 2011/02/10 21:17:49 drochner 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.11        ad #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.13    darran 
     50   1.1  jonathan int window_inflate = -1 * MAX_WBITS;
     51   1.1  jonathan int window_deflate = -12;
     52   1.1  jonathan 
     53   1.1  jonathan /*
     54   1.1  jonathan  * This function takes a block of data and (de)compress it using the deflate
     55   1.1  jonathan  * algorithm
     56   1.1  jonathan  */
     57   1.1  jonathan 
     58   1.2   thorpej static void *
     59   1.7  christos ocf_zalloc(void *nil, u_int type, u_int size)
     60   1.2   thorpej {
     61   1.2   thorpej 	void *ptr;
     62   1.2   thorpej 
     63   1.2   thorpej 	ptr = malloc(type *size, M_CRYPTO_DATA, M_NOWAIT);
     64   1.2   thorpej 	return ptr;
     65   1.2   thorpej }
     66   1.2   thorpej 
     67   1.2   thorpej static void
     68   1.7  christos ocf_zfree(void *nil, void *ptr)
     69   1.2   thorpej {
     70   1.2   thorpej 	free(ptr, M_CRYPTO_DATA);
     71   1.2   thorpej }
     72   1.2   thorpej 
     73   1.1  jonathan u_int32_t
     74  1.12       dsl deflate_global(u_int8_t *data, u_int32_t size, int decomp, u_int8_t **out)
     75   1.1  jonathan {
     76   1.1  jonathan 	/* decomp indicates whether we compress (0) or decompress (1) */
     77   1.1  jonathan 
     78   1.1  jonathan 	z_stream zbuf;
     79   1.1  jonathan 	u_int8_t *output;
     80   1.1  jonathan 	u_int32_t count, result;
     81   1.1  jonathan 	int error, i = 0, j;
     82   1.8  degroote 	struct deflate_buf *buf, *tmp;
     83   1.8  degroote 	size_t len, old_len;
     84   1.1  jonathan 
     85  1.13    darran 	DPRINTF(("deflate_global: size %d\n", size));
     86  1.13    darran 
     87   1.8  degroote 	len = ZBUF;
     88   1.8  degroote 	buf = malloc(len*sizeof(struct deflate_buf), M_CRYPTO_DATA, M_NOWAIT);
     89   1.8  degroote 	if (buf == NULL)
     90   1.8  degroote 		return 0;
     91   1.8  degroote 
     92   1.8  degroote 	memset(&zbuf, 0, sizeof(z_stream));
     93   1.8  degroote 	for (j = 0; j < len; j++)
     94   1.1  jonathan 		buf[j].flag = 0;
     95   1.1  jonathan 
     96   1.1  jonathan 	zbuf.next_in = data;	/* data that is going to be processed */
     97   1.2   thorpej 	zbuf.zalloc = ocf_zalloc;
     98   1.2   thorpej 	zbuf.zfree = ocf_zfree;
     99   1.1  jonathan 	zbuf.opaque = Z_NULL;
    100   1.1  jonathan 	zbuf.avail_in = size;	/* Total length of data to be processed */
    101   1.1  jonathan 
    102   1.1  jonathan 	if (!decomp) {
    103   1.5  christos 		buf[i].out = malloc(size, M_CRYPTO_DATA, M_NOWAIT);
    104   1.1  jonathan 		if (buf[i].out == NULL)
    105   1.1  jonathan 			goto bad;
    106   1.1  jonathan 		buf[i].size = size;
    107   1.1  jonathan 		buf[i].flag = 1;
    108   1.1  jonathan 		i++;
    109   1.1  jonathan 	} else {
    110   1.1  jonathan 		/*
    111   1.1  jonathan 	 	 * Choose a buffer with 4x the size of the input buffer
    112   1.1  jonathan 	 	 * for the size of the output buffer in the case of
    113   1.1  jonathan 	 	 * decompression. If it's not sufficient, it will need to be
    114   1.1  jonathan 	 	 * updated while the decompression is going on
    115   1.1  jonathan 	 	 */
    116   1.1  jonathan 
    117   1.5  christos 		buf[i].size = size * 4;
    118   1.5  christos 		buf[i].out = malloc(buf[i].size, M_CRYPTO_DATA, M_NOWAIT);
    119   1.1  jonathan 		if (buf[i].out == NULL)
    120   1.1  jonathan 			goto bad;
    121   1.1  jonathan 		buf[i].flag = 1;
    122   1.1  jonathan 		i++;
    123   1.1  jonathan 	}
    124   1.1  jonathan 
    125   1.1  jonathan 	zbuf.next_out = buf[0].out;
    126   1.1  jonathan 	zbuf.avail_out = buf[0].size;
    127   1.1  jonathan 
    128   1.1  jonathan 	error = decomp ? inflateInit2(&zbuf, window_inflate) :
    129   1.1  jonathan 	    deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
    130   1.1  jonathan 		    window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
    131   1.1  jonathan 
    132   1.1  jonathan 	if (error != Z_OK)
    133   1.1  jonathan 		goto bad;
    134   1.1  jonathan 	for (;;) {
    135  1.14  drochner 		error = decomp ? inflate(&zbuf, Z_SYNC_FLUSH) :
    136  1.14  drochner 				 deflate(&zbuf, Z_FINISH);
    137   1.1  jonathan 		if (error != Z_OK && error != Z_STREAM_END)
    138   1.1  jonathan 			goto bad;
    139   1.1  jonathan 		else if (zbuf.avail_in == 0 && zbuf.avail_out != 0)
    140   1.1  jonathan 			goto end;
    141   1.8  degroote 		else if (zbuf.avail_out == 0) {
    142   1.8  degroote 			if (i == (len-1)) {
    143   1.8  degroote 				old_len = i;
    144   1.8  degroote 				len += ZBUF;
    145   1.8  degroote 				tmp = realloc(buf,len*sizeof(struct deflate_buf),
    146   1.8  degroote 							  M_CRYPTO_DATA, M_NOWAIT);
    147   1.8  degroote 				if (tmp == NULL)
    148   1.8  degroote 					goto bad;
    149   1.8  degroote 				buf = tmp;
    150   1.8  degroote 				for (j = old_len; j < len; j++)
    151   1.8  degroote 					buf[j].flag = 0;
    152   1.8  degroote 			}
    153   1.1  jonathan 			/* we need more output space, allocate size */
    154   1.5  christos 			buf[i].out = malloc(size, M_CRYPTO_DATA, M_NOWAIT);
    155   1.1  jonathan 			if (buf[i].out == NULL)
    156   1.1  jonathan 				goto bad;
    157   1.1  jonathan 			zbuf.next_out = buf[i].out;
    158   1.1  jonathan 			buf[i].size = size;
    159   1.1  jonathan 			buf[i].flag = 1;
    160   1.1  jonathan 			zbuf.avail_out = buf[i].size;
    161   1.1  jonathan 			i++;
    162   1.1  jonathan 		} else
    163   1.1  jonathan 			goto bad;
    164   1.1  jonathan 	}
    165   1.1  jonathan 
    166   1.1  jonathan end:
    167   1.1  jonathan 	result = count = zbuf.total_out;
    168   1.1  jonathan 
    169   1.5  christos 	*out = malloc(result, M_CRYPTO_DATA, M_NOWAIT);
    170   1.1  jonathan 	if (*out == NULL)
    171   1.1  jonathan 		goto bad;
    172   1.1  jonathan 	if (decomp)
    173   1.1  jonathan 		inflateEnd(&zbuf);
    174   1.1  jonathan 	else
    175   1.1  jonathan 		deflateEnd(&zbuf);
    176   1.1  jonathan 	output = *out;
    177   1.1  jonathan 	for (j = 0; buf[j].flag != 0; j++) {
    178   1.1  jonathan 		if (count > buf[j].size) {
    179   1.9  degroote 			memcpy(*out, buf[j].out, buf[j].size);
    180   1.1  jonathan 			*out += buf[j].size;
    181   1.8  degroote 			free(buf[j].out, M_CRYPTO_DATA);
    182   1.1  jonathan 			count -= buf[j].size;
    183   1.1  jonathan 		} else {
    184   1.1  jonathan 			/* it should be the last buffer */
    185   1.9  degroote 			memcpy(*out, buf[j].out, count);
    186   1.1  jonathan 			*out += count;
    187   1.8  degroote 			free(buf[j].out, M_CRYPTO_DATA);
    188   1.1  jonathan 			count = 0;
    189   1.1  jonathan 		}
    190   1.1  jonathan 	}
    191   1.8  degroote 	free(buf, M_CRYPTO_DATA);
    192   1.1  jonathan 	*out = output;
    193   1.1  jonathan 	return result;
    194   1.1  jonathan 
    195   1.1  jonathan bad:
    196   1.1  jonathan 	*out = NULL;
    197   1.1  jonathan 	for (j = 0; buf[j].flag != 0; j++)
    198   1.8  degroote 		free(buf[j].out, M_CRYPTO_DATA);
    199   1.8  degroote 	free(buf, M_CRYPTO_DATA);
    200   1.1  jonathan 	if (decomp)
    201   1.1  jonathan 		inflateEnd(&zbuf);
    202   1.1  jonathan 	else
    203   1.1  jonathan 		deflateEnd(&zbuf);
    204   1.1  jonathan 	return 0;
    205   1.1  jonathan }
    206  1.13    darran 
    207  1.13    darran /*
    208  1.13    darran  * Initial version will perform a single gzip encapsulation,
    209  1.13    darran  * filling in the header,
    210  1.13    darran  * and appending the crc and uncompressed length.
    211  1.13    darran  *
    212  1.13    darran  * Later version will support multiple buffers with
    213  1.13    darran  * a flag indication final buffer.  The crc is maintained
    214  1.13    darran  * over all buffers and appended to the output along with
    215  1.13    darran  * the uncompressed length after the final data buffer
    216  1.13    darran  * has been compressed and output.
    217  1.13    darran  *
    218  1.13    darran  * Ditto for uncompress - CRC is extracted from the final packed
    219  1.13    darran  * and compared against CRC of uncompressed data.
    220  1.13    darran  *
    221  1.13    darran  */
    222  1.13    darran 
    223  1.13    darran /* constant header for the gzip */
    224  1.13    darran static const char gzip_header[10] = {
    225  1.13    darran 	0x1f, 0x8b, 	/* ID1 ID2	*/
    226  1.13    darran 	Z_DEFLATED, 	/* CM		*/
    227  1.13    darran 	0,		/* FLG		*/
    228  1.13    darran 	0, 0, 0, 0,	/* MTIME	*/
    229  1.13    darran 	0,		/* XFL		*/
    230  1.13    darran 	0x03		/* OS (Unix)	*/
    231  1.13    darran };
    232  1.13    darran 
    233  1.13    darran /* Followed by compressed payload */
    234  1.13    darran /* Followed by uint32_t CRC32 and uint32_t ISIZE */
    235  1.13    darran #define GZIP_TAIL_SIZE	8
    236  1.13    darran 
    237  1.13    darran u_int32_t
    238  1.13    darran gzip_global(u_int8_t *data, u_int32_t size,
    239  1.13    darran 	int decomp, u_int8_t **out)
    240  1.13    darran {
    241  1.13    darran 	/* decomp indicates whether we compress (0) or decompress (1) */
    242  1.13    darran 	z_stream zbuf;
    243  1.13    darran 	u_int8_t *output;
    244  1.13    darran 	u_int32_t count, result;
    245  1.13    darran 	int error, i = 0, j;
    246  1.13    darran 	struct deflate_buf *buf, *tmp;
    247  1.13    darran 	size_t nbufs, old_nbufs;
    248  1.13    darran 	u_int32_t crc;
    249  1.13    darran 	u_int32_t isize;
    250  1.13    darran 
    251  1.13    darran 	DPRINTF(("gzip_global: decomp %d, size %d\n", decomp, size));
    252  1.13    darran 
    253  1.13    darran 	nbufs = ZBUF;
    254  1.13    darran 	buf = malloc(nbufs*sizeof(struct deflate_buf), M_CRYPTO_DATA, M_NOWAIT);
    255  1.13    darran 	if (buf == NULL) {
    256  1.13    darran 		DPRINTF(("gzip_global.%d: failed to malloc %d\n",
    257  1.13    darran 				__LINE__, nbufs*sizeof(struct deflate_buf)));
    258  1.13    darran 		return 0;
    259  1.13    darran 	}
    260  1.13    darran 
    261  1.13    darran 	memset(&zbuf, 0, sizeof(z_stream));
    262  1.13    darran 	for (j = 0; j < nbufs; j++)
    263  1.13    darran 		buf[j].flag = 0;
    264  1.13    darran 
    265  1.13    darran 	zbuf.zalloc = ocf_zalloc;
    266  1.13    darran 	zbuf.zfree = ocf_zfree;
    267  1.13    darran 	zbuf.opaque = Z_NULL;
    268  1.13    darran 
    269  1.13    darran 	crc = crc32(0, NULL, 0);	/* get initial crc value */
    270  1.13    darran 
    271  1.13    darran 	zbuf.avail_in = size;	/* Total length of data to be processed */
    272  1.13    darran 	zbuf.next_in = data;	/* data that is going to be processed */
    273  1.13    darran 
    274  1.13    darran 	if (!decomp) {
    275  1.13    darran 		/* compress */
    276  1.13    darran 		DPRINTF(("gzip_global: compress[%d] malloc %d + %d + %d = %d\n",
    277  1.13    darran 				i, size, sizeof(gzip_header), GZIP_TAIL_SIZE,
    278  1.13    darran 				size + sizeof(gzip_header) + GZIP_TAIL_SIZE));
    279  1.13    darran 
    280  1.13    darran 		buf[i].out = malloc(size, M_CRYPTO_DATA, M_NOWAIT);
    281  1.13    darran 		if (buf[i].out == NULL)
    282  1.13    darran 			goto bad2;
    283  1.13    darran 		buf[i].size = size;
    284  1.13    darran 		buf[i].flag = 1;
    285  1.13    darran 
    286  1.13    darran 		zbuf.next_out = buf[i].out;
    287  1.13    darran 		zbuf.avail_out = buf[i].size;
    288  1.13    darran 		i++;
    289  1.13    darran 
    290  1.13    darran 		crc = crc32(crc, data, size);
    291  1.13    darran 		DPRINTF(("gzip_compress: size %d, crc 0x%x\n", size, crc));
    292  1.13    darran 	} else {
    293  1.13    darran 		/* decompress */
    294  1.13    darran 		/* check the gzip header */
    295  1.13    darran 		if (zbuf.avail_in <= 0) {
    296  1.13    darran 			/* Not enough data for the header & tail */
    297  1.13    darran 			DPRINTF(("gzip_global: not enough data (%d)\n",
    298  1.13    darran 					size));
    299  1.13    darran 			goto bad2;
    300  1.13    darran 		}
    301  1.13    darran 
    302  1.13    darran 		/* XXX this is pretty basic,
    303  1.13    darran 		 * needs to be expanded to ignore MTIME, OS,
    304  1.13    darran 		 * but still ensure flags are 0.
    305  1.13    darran 		 * Q. Do we need to support the flags and
    306  1.13    darran 		 * optional header fields? Likely.
    307  1.13    darran 		 * XXX add flag and field support too.
    308  1.13    darran 		 */
    309  1.13    darran 		if (memcmp(data, gzip_header, sizeof(gzip_header)) != 0) {
    310  1.13    darran 			DPRINTF(("gzip_global: unsupported gzip header (%02x%02x)\n",
    311  1.13    darran 					data[0], data[1]));
    312  1.13    darran 			goto bad2;
    313  1.13    darran 		} else {
    314  1.13    darran 			DPRINTF(("gzip_global.%d: gzip header ok\n",__LINE__));
    315  1.13    darran 		}
    316  1.13    darran 
    317  1.13    darran 		isize = *((uint32_t *)&data[size-sizeof(uint32_t)]);
    318  1.13    darran 
    319  1.13    darran 		DPRINTF(("gzip_global: isize = %d (%02x %02x %02x %02x)\n",
    320  1.13    darran 				isize,
    321  1.13    darran 				data[size-4],
    322  1.13    darran 				data[size-3],
    323  1.13    darran 				data[size-2],
    324  1.13    darran 				data[size-1]));
    325  1.13    darran 
    326  1.13    darran 		buf[i].size = isize;
    327  1.13    darran 		buf[i].out = malloc(buf[i].size, M_CRYPTO_DATA, M_NOWAIT);
    328  1.13    darran 		if (buf[i].out == NULL)
    329  1.13    darran 			goto bad2;
    330  1.13    darran 		buf[i].flag = 1;
    331  1.13    darran 		zbuf.next_out = buf[i].out;
    332  1.13    darran 		zbuf.avail_out = buf[i].size;
    333  1.13    darran 		i++;
    334  1.13    darran 
    335  1.13    darran 		/* skip over the gzip header */
    336  1.13    darran 		zbuf.next_in = data + sizeof(gzip_header);
    337  1.13    darran 
    338  1.13    darran 		/* actual payload size stripped of gzip header and tail */
    339  1.13    darran 		zbuf.avail_in = size - sizeof(gzip_header) - GZIP_TAIL_SIZE;
    340  1.13    darran 		DPRINTF(("zbuf avail_in %d, avail_out %d\n",
    341  1.13    darran 					zbuf.avail_in, zbuf.avail_out));
    342  1.13    darran 
    343  1.13    darran 	}
    344  1.13    darran 
    345  1.13    darran 
    346  1.13    darran 	error = decomp ? inflateInit2(&zbuf, window_inflate) :
    347  1.13    darran 	    deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
    348  1.13    darran 		    window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
    349  1.13    darran 
    350  1.13    darran 	if (error != Z_OK) {
    351  1.13    darran 		printf("deflateInit2() failed\n");
    352  1.13    darran 		goto bad;
    353  1.13    darran 	}
    354  1.13    darran 	for (;;) {
    355  1.13    darran 		DPRINTF(("pre: %s in:%d out:%d\n", decomp ? "deflate()" : "inflate()",
    356  1.13    darran 				zbuf.avail_in, zbuf.avail_out));
    357  1.14  drochner 		error = decomp ? inflate(&zbuf, Z_SYNC_FLUSH) :
    358  1.14  drochner 				 deflate(&zbuf, Z_FINISH);
    359  1.13    darran 		DPRINTF(("post: %s in:%d out:%d\n", decomp ? "deflate()" : "inflate()",
    360  1.13    darran 				zbuf.avail_in, zbuf.avail_out));
    361  1.13    darran 		if (error != Z_OK && error != Z_STREAM_END) {
    362  1.13    darran 			printf("deflate() or inflate() failed, error=%d\n", error);
    363  1.13    darran 			goto bad;
    364  1.13    darran 		} else if (zbuf.avail_in == 0 && zbuf.avail_out != 0) {
    365  1.13    darran 			DPRINTF(("gzip_global: avail_in == 0, ending\n"));
    366  1.13    darran 			goto end;
    367  1.13    darran 		} else if (zbuf.avail_in == 0 && zbuf.avail_out == 0) {
    368  1.13    darran 			DPRINTF(("gzip_global: avail_in == 0, avail_out == 0, ending\n"));
    369  1.13    darran 			goto end;
    370  1.13    darran 		} else if (zbuf.avail_out == 0) {
    371  1.13    darran 			if (i == (nbufs-1)) {
    372  1.13    darran 				old_nbufs = i;
    373  1.13    darran 				nbufs += ZBUF;
    374  1.13    darran 				tmp = realloc(buf,nbufs*sizeof(struct deflate_buf),
    375  1.13    darran 							  M_CRYPTO_DATA, M_NOWAIT);
    376  1.13    darran 				if (tmp == NULL)
    377  1.13    darran 					goto bad;
    378  1.13    darran 				buf = tmp;
    379  1.13    darran 				for (j = old_nbufs; j < nbufs; j++)
    380  1.13    darran 					buf[j].flag = 0;
    381  1.13    darran 			}
    382  1.13    darran 			/* we need more output space, allocate size */
    383  1.13    darran 			buf[i].out = malloc(size, M_CRYPTO_DATA, M_NOWAIT);
    384  1.13    darran 			if (buf[i].out == NULL)
    385  1.13    darran 				goto bad;
    386  1.13    darran 			zbuf.next_out = buf[i].out;
    387  1.13    darran 			buf[i].size = size;
    388  1.13    darran 			buf[i].flag = 1;
    389  1.13    darran 			zbuf.avail_out = buf[i].size;
    390  1.13    darran 			i++;
    391  1.13    darran 		} else
    392  1.13    darran 			goto bad;
    393  1.13    darran 	}
    394  1.13    darran 
    395  1.13    darran end:
    396  1.13    darran 	if (decomp) {
    397  1.13    darran 		count = result = zbuf.total_out;
    398  1.13    darran 	} else {
    399  1.13    darran 		/* need room for header, CRC, and ISIZE */
    400  1.13    darran 		result = zbuf.total_out + sizeof(gzip_header) + GZIP_TAIL_SIZE;
    401  1.13    darran 		count = zbuf.total_out;
    402  1.13    darran 	}
    403  1.13    darran 
    404  1.13    darran 	DPRINTF(("gzip_global: in %d -> out %d\n", size, result));
    405  1.13    darran 
    406  1.13    darran 	*out = malloc(result, M_CRYPTO_DATA, M_NOWAIT);
    407  1.13    darran 	if (*out == NULL)
    408  1.13    darran 		goto bad;
    409  1.13    darran 	output = *out;
    410  1.13    darran 	if (decomp)
    411  1.13    darran 		inflateEnd(&zbuf);
    412  1.13    darran 	else {
    413  1.13    darran 		deflateEnd(&zbuf);
    414  1.13    darran 
    415  1.13    darran 		/* fill in gzip header */
    416  1.13    darran 		memcpy(output, gzip_header, sizeof(gzip_header));
    417  1.13    darran 		output += sizeof(gzip_header);
    418  1.13    darran 	}
    419  1.13    darran 	for (j = 0; buf[j].flag != 0; j++) {
    420  1.13    darran 		if (decomp) {
    421  1.13    darran 			/* update crc for decompressed data */
    422  1.13    darran 			crc = crc32(crc, buf[j].out, buf[j].size);
    423  1.13    darran 		}
    424  1.13    darran 		if (count > buf[j].size) {
    425  1.13    darran 			memcpy(output, buf[j].out, buf[j].size);
    426  1.13    darran 			output += buf[j].size;
    427  1.13    darran 			free(buf[j].out, M_CRYPTO_DATA);
    428  1.13    darran 			count -= buf[j].size;
    429  1.13    darran 		} else {
    430  1.13    darran 			/* it should be the last buffer */
    431  1.13    darran 			memcpy(output, buf[j].out, count);
    432  1.13    darran 			output += count;
    433  1.13    darran 			free(buf[j].out, M_CRYPTO_DATA);
    434  1.13    darran 			count = 0;
    435  1.13    darran 		}
    436  1.13    darran 	}
    437  1.13    darran 	free(buf, M_CRYPTO_DATA);
    438  1.13    darran 
    439  1.13    darran 	if (!decomp) {
    440  1.13    darran 		/* fill in CRC and ISIZE */
    441  1.13    darran 		((uint32_t *)output)[0] = crc;
    442  1.13    darran 		((uint32_t *)output)[1] = size;
    443  1.13    darran 
    444  1.13    darran 		DPRINTF(("gzip_global: size = 0x%x (%02x %02x %02x %02x)\n",
    445  1.13    darran 				size,
    446  1.13    darran 				output[7],
    447  1.13    darran 				output[3],
    448  1.13    darran 				output[5],
    449  1.13    darran 				output[4]));
    450  1.13    darran 	}
    451  1.13    darran 
    452  1.13    darran 	return result;
    453  1.13    darran 
    454  1.13    darran bad:
    455  1.13    darran 	if (decomp)
    456  1.13    darran 		inflateEnd(&zbuf);
    457  1.13    darran 	else
    458  1.13    darran 		deflateEnd(&zbuf);
    459  1.13    darran bad2:
    460  1.13    darran 	*out = NULL;
    461  1.13    darran 	for (j = 0; buf[j].flag != 0; j++)
    462  1.13    darran 		free(buf[j].out, M_CRYPTO_DATA);
    463  1.13    darran 	free(buf, M_CRYPTO_DATA);
    464  1.13    darran 	return 0;
    465  1.13    darran }
    466