gzip_wrapper.c revision b552f0092251f472920f73e701bfd7fffe954327
1/* 2 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 3 * Phillip Lougher <phillip@lougher.demon.co.uk> 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License 7 * as published by the Free Software Foundation; either version 2, 8 * or (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 * 19 * gzip_wrapper.c 20 */ 21 22#include <stdlib.h> 23#include <zlib.h> 24 25int gzip_compress(void **strm, char *d, char *s, int size, int block_size, 26 int *error) 27{ 28 int res = 0; 29 z_stream *stream = *strm; 30 31 if(stream == NULL) { 32 if((stream = *strm = malloc(sizeof(z_stream))) == NULL) 33 goto failed; 34 35 stream->zalloc = Z_NULL; 36 stream->zfree = Z_NULL; 37 stream->opaque = 0; 38 39 if((res = deflateInit(stream, 9)) != Z_OK) 40 goto failed; 41 } else if((res = deflateReset(stream)) != Z_OK) 42 goto failed; 43 44 stream->next_in = (unsigned char *) s; 45 stream->avail_in = size; 46 stream->next_out = (unsigned char *) d; 47 stream->avail_out = block_size; 48 49 res = deflate(stream, Z_FINISH); 50 if(res == Z_STREAM_END) 51 /* 52 * Success, return the compressed size. 53 */ 54 return (int) stream->total_out; 55 if(res == Z_OK) 56 /* 57 * Output buffer overflow. Return out of buffer space 58 */ 59 return 0; 60failed: 61 /* 62 * All other errors return failure, with the compressor 63 * specific error code in *error 64 */ 65 *error = res; 66 return -1; 67} 68 69 70int gzip_uncompress(char *d, char *s, int size, int block_size, int *error) 71{ 72 int res; 73 unsigned long bytes = block_size; 74 75 res = uncompress((unsigned char *) d, &bytes, 76 (const unsigned char *) s, size); 77 78 *error = res; 79 return res == Z_OK ? (int) bytes : -1; 80} 81