/* 31/01/01 Libtiny compression/decompression library v1.0 source file Decompression routine using zlib inflate Inflate code: inflate 1.1.3 Copyright 1995-1998 Mark Adler */ /* includes */ #include #include "stdio.h" #include "zlib.h" #include "zconf.h" #include "tiny.h" /* Decompress zlib compressed data, *source_p - pointer to data to decompress, destination_p - pointer to buffer to store decompressed data Return values >0 decompressed data size in bytes, -1(T_ID_ERROR) bad header id value, should be ID_ZLIB, -2(T_MEM_ERROR) out of memory, workspace couldn't be allocated, -3(T_BUF_ERROR) destination buffer too small, should never be returned unless header has become corrupted, -4(T_DATA_ERROR) header or compressed data has been corrupted */ int z_uncompress(u_char *source_p, u_char *destination_p) { S_HDR header; S_HDR *header_p = (S_HDR *) source_p; u_int sourcelen,destlen; int status = T_ID_ERROR; header = *header_p; /* get header info */ if(header.id == ID_ZLIB) { sourcelen = header.comp_size; destlen = header.orig_size; /*destination buffer size is the same as the original data length */ source_p += sizeof(S_HDR); /* bump pointer past header */ status = uncompress((Bytef *)destination_p,(uLongf *)&destlen,(Bytef *)source_p,(uLong)sourcelen); switch(status) { case Z_OK: if(destlen == header.orig_size) status = (int) destlen; /* success, return decompressed data length */ else status = T_DATA_ERROR; /* header data corrupt */ break; case Z_MEM_ERROR: status = T_MEM_ERROR; /* malloc failure */ break; case Z_BUF_ERROR: /* destination buffer too small */ status = T_BUF_ERROR; break; case Z_DATA_ERROR: status = T_DATA_ERROR; /* compressed data corrupt */ } } return status; }