/* 31/01/01 Libtiny compression/decompression library v1.0 Compression routine using zlib deflate Deflate code: deflate 1.1.3 Copyright 1995-1998 Jean-loup Gailly */ /* includes */ #include #include "stdio.h" #include "zlib.h" #include "zconf.h" #include "tiny.h" /* globals */ /* Compress data using zlib deflate routines, *source_p - pointer to data to compress, sourcelen - number of bytes to compress, destination_p - pointer to buffer to store compressed data Return values >0 compressed data size in bytes, including header, 0(T_LEN_ERROR) sourcelen is zero, -2(T_MEM_ERROR) out of memory, workspace couldn't be allocated, -3(T_BUF_ERROR) destination buffer too small, this error is returned if the data cannot be compressed, The destination buffer length should be 0.1% plus 32 bytes larger than the source data length*/ int z_compress(u_char *source_p, u_int sourcelen, u_char *destination_p) { S_HDR *header_p = (S_HDR *)destination_p; u_int destlen; int status = T_LEN_ERROR; if(sourcelen) { header_p->id = ID_ZLIB; header_p->orig_size = sourcelen; destination_p += sizeof(S_HDR); /* start of compression data after header */ destlen = sourcelen + (sourcelen * 0.001) + 12; /* minimum destination buffer size */ status = compress((Bytef *)destination_p,(uLongf *)&destlen,(Bytef *)source_p,(uLong)sourcelen); switch(status) { case Z_OK: header_p->comp_size = destlen; /* store compressed data size in header */ status = (int) destlen + sizeof(S_HDR); /* add header size to total */ break; case Z_MEM_ERROR: /* work space could not be allocated */ status = T_MEM_ERROR; break; case Z_BUF_ERROR: /* compressed data buffer too small */ status = T_BUF_ERROR; /* probably trying to compress data that's already compressed */ } } return status; }