/* 31/01/01 Libtiny compression/decompression library v1.0 source file Huffman compression routine Huffman code: (C) David Bourgin */ /* includes */ #include #include "stdio.h" #include "tiny.h" /* extern */ /* huffman variables */ extern u_char bit_counter_to_write; extern u_int val_to_write; extern u_char *srce_p; extern u_char *start_p; extern u_char *end_p; extern u_char *dest_p; extern int buflen; extern int bytecount; extern int errval; /* Compress data using huffman encoding, *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, returned if the source data cannot be compressed, The destination buffer length should be 0.1% plus 32 bytes larger than the source data length*/ int huf_compress(u_char *source_p, u_int sourcelen, u_char *destination_p) { S_HDR *hdr_p = (S_HDR *)destination_p; int status = T_LEN_ERROR; if(sourcelen) { hdr_p->id = ID_HUFF; hdr_p->orig_size = sourcelen; destination_p += sizeof(S_HDR); /* start of compression data after header */ /* set required variables in dcodhuff.c */ start_p = srce_p = source_p; /* ptr to data to compress */ end_p = (srce_p + sourcelen) -1; /* ptr at end of data */ dest_p = destination_p; /* ptr to compression buffer */ errval = bytecount = 0; bit_counter_to_write = val_to_write = 0; buflen = sourcelen + (int) (sourcelen * 0.001) + 24; /* used to watch for a buffer overrun */ status = huffmanencoding(); if(status >= 0) { hdr_p->comp_size = status; /* compression successful, place compressed data length in header */ status += sizeof(S_HDR); /* add header to compression data length */ } /* else error value returned */ } return status; }