1 /* $OpenBSD: xform_ipcomp.c,v 1.4 2014/07/12 18:50:00 tedu Exp $ */ 2 3 /* 4 * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org) 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 /* 31 * This file contains a wrapper around the deflate algo compression 32 * functions using the zlib library (see net/zlib.{c,h}) 33 */ 34 35 #include <sys/types.h> 36 #include <sys/malloc.h> 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <lib/libz/zutil.h> 40 41 #define Z_METHOD 8 42 #define Z_MEMLEVEL 8 43 #define ZBUF 10 44 45 u_int32_t deflate_global(u_int8_t *, u_int32_t, int, u_int8_t **); 46 47 struct deflate_buf { 48 u_int8_t *out; 49 u_int32_t size; 50 int flag; 51 }; 52 53 int window_inflate = -1 * MAX_WBITS; 54 int window_deflate = -12; 55 56 /* 57 * This function takes a block of data and (de)compress it using the deflate 58 * algorithm 59 */ 60 61 u_int32_t 62 deflate_global(u_int8_t *data, u_int32_t size, int decomp, u_int8_t **out) 63 { 64 z_stream zbuf; 65 u_int8_t *output; 66 u_int32_t count, result; 67 int error, i = 0, j; 68 struct deflate_buf buf[ZBUF]; 69 70 bzero(&zbuf, sizeof(z_stream)); 71 for (j = 0; j < ZBUF; j++) 72 buf[j].flag = 0; 73 74 zbuf.next_in = data; /* data that is going to be processed */ 75 zbuf.zalloc = zcalloc; 76 zbuf.zfree = zcfree; 77 zbuf.opaque = Z_NULL; 78 zbuf.avail_in = size; /* Total length of data to be processed */ 79 80 if (decomp) { 81 /* 82 * Choose a buffer with 4x the size of the input buffer 83 * for the size of the output buffer in the case of 84 * decompression. If it's not sufficient, it will need to be 85 * updated while the decompression is going on 86 */ 87 if (size < 32 * 1024) 88 size *= 4; 89 } 90 buf[i].out = malloc((u_long)size, M_CRYPTO_DATA, M_NOWAIT); 91 if (buf[i].out == NULL) 92 goto bad; 93 buf[i].size = size; 94 buf[i].flag = 1; 95 i++; 96 97 zbuf.next_out = buf[0].out; 98 zbuf.avail_out = buf[0].size; 99 100 error = decomp ? 101 inflateInit2(&zbuf, window_inflate) : 102 deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD, 103 window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY); 104 105 if (error != Z_OK) 106 goto bad; 107 for (;;) { 108 error = decomp ? 109 inflate(&zbuf, Z_PARTIAL_FLUSH) : 110 deflate(&zbuf, Z_FINISH); 111 if (error == Z_STREAM_END) 112 break; 113 if (error != Z_OK) 114 goto bad; 115 if (zbuf.avail_out == 0 && i < (ZBUF - 1)) { 116 /* we need more output space, allocate size */ 117 if (size < 32 * 1024) 118 size *= 2; 119 buf[i].out = malloc((u_long)size, M_CRYPTO_DATA, 120 M_NOWAIT); 121 if (buf[i].out == NULL) 122 goto bad; 123 zbuf.next_out = buf[i].out; 124 buf[i].size = size; 125 buf[i].flag = 1; 126 zbuf.avail_out = buf[i].size; 127 i++; 128 } else 129 goto bad; /* out of buffers */ 130 } 131 result = count = zbuf.total_out; 132 133 *out = malloc((u_long)result, M_CRYPTO_DATA, M_NOWAIT); 134 if (*out == NULL) 135 goto bad; 136 if (decomp) 137 inflateEnd(&zbuf); 138 else 139 deflateEnd(&zbuf); 140 output = *out; 141 for (j = 0; buf[j].flag != 0; j++) { 142 if (count > buf[j].size) { 143 bcopy(buf[j].out, *out, buf[j].size); 144 *out += buf[j].size; 145 free(buf[j].out, M_CRYPTO_DATA, 0); 146 count -= buf[j].size; 147 } else { 148 /* it should be the last buffer */ 149 bcopy(buf[j].out, *out, count); 150 *out += count; 151 free(buf[j].out, M_CRYPTO_DATA, 0); 152 count = 0; 153 } 154 } 155 *out = output; 156 return result; 157 158 bad: 159 *out = NULL; 160 for (j = 0; buf[j].flag != 0; j++) 161 free(buf[j].out, M_CRYPTO_DATA, 0); 162 if (decomp) 163 inflateEnd(&zbuf); 164 else 165 deflateEnd(&zbuf); 166 return 0; 167 } 168