/* 31/01/01 Libtiny compression/decompression library v1.0 source file Huffman decompression routine Huffman code: (C) David Bourgin */ /* includes */ #include #include "stdio.h" #include "tiny.h" /* externs */ /* huffman variables */ extern u_char bit_counter_to_read; extern u_int val_to_read; extern u_char *srce2_p; extern u_char *end2_p; extern u_char *dest2_p; extern int buflen2; extern int bytecount2; extern int errval2; /* Decompress huffman encoded 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_HUFF, -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 or data has become corrupted, -4(T_DATA_ERROR) compressed data has been corrupted */ int huf_uncompress(u_char *source_p, u_char *destination_p) { S_HDR header; S_HDR *header_p = (S_HDR *) source_p; int status = T_ID_ERROR; header = *header_p; /* copy header info */ /* identify block as huffman encoded data */ if(header.id == ID_HUFF) { source_p += sizeof(S_HDR); /* bump up ptr to start of compressed data */ /* set required variables in dcodhuff.c */ srce2_p = source_p; /* ptr to compressed data */ end2_p = (srce2_p + header.comp_size) -1; /* ptr to last byte of compressed data */ dest2_p = destination_p; /* mem space for uncompressed data */ errval2 = bytecount2 = 0; val_to_read = bit_counter_to_read = 0; buflen2 = header.orig_size; /* to check for buffer overrun */ status = huffmandecoding(); if(status >= 0) /* check decompressed data length is correct */ { if(status != header.orig_size) status = T_DATA_ERROR; /* if there's a mismatch then data is corrupt */ } } return status; }