ComprLib - A Data Compression Library

Friday Jul 30th 1999 by Jeff Connelly

ComprLib - A Data Compression Library


There's not much to see

Environment: VC4

ComprLib is a data compression, encryption, plain text conversion (UUEncoding ect.), and checksum library I made a while ago for DOS. I ported it to Windows recently.

To use ComprLib, first you set up the I/O function pointers, and then you call a function that encodes or decodes (or computes the checksum) the data.

The following function pointers are used for I/O in ComprLib (defined in ComprLib.h):

extern bool (*end_of_data)();              // Returns true if no more input data left
extern unsigned char (*read_byte)();       // Reads a byte from the input data
extern void (*write_byte)(unsigned char);  // Writes a byte to output data
extern void (*beginning_of_data)();        // Resets position of input data
extern long (*stream_size)();              // Returns the size of the input data
end_of_data() returns true if there is no more input data left, read_byte() reads a byte from the input data and returns it, write_byte() writes a byte to the output data, beginning_of_data() resets the position of the input file back to the beginning, and finally stream_size() returns the size of the input data. All of these function pointers should be set to functions that do the things described above.

These functions that are pointed to by the global I/O function pointers may be grouped in a class. You can derive the I/O class from CComprLibIOBase, which is defined as follows:

class ComprLibIOBase
    static bool end_of_data();
    static unsigned char read_byte();
    static void write_byte(unsigned char);
    static void beginning_of_data();
    static long stream_size();
    static void write_array(void*, int);
    static void write_block(unsigned char, int);
The derived class should provide implementation for each of these functions. Classes should also provide a Set() method that sets all of the global I/O function pointers to the ones in the class. Optionally it may also provide member functions that set only the input or output function pointers, this way applications can combine multiple I/O routines.

Note that all of the functions are static. This is because one should never instanate a I/O class, but rather call the static functions in it.

Two I/O classes are provided with ComprLib. These are CComprLibFileIO, CComprLibMemIO.

CComprLibFileIO handles file I/O. By setting the two FILE* variables CComprLibFileIO::source_file and CComprLibFileIO::dest_file, you can control what files are used for reading and writing. Setting CComprLibFileIO::file_limit allows you to consider a certain position end the input file the end of the file (i.e., no more data will be read beyond this point).

static char* source_ptr; static char* dest_ptr; static long source_len; static long dest_len; static long grow_by;

CComprLibMemIO handles memory "I/O". It is much faster than the file I/O class. The following member variables are in CComprLibMemIO:

static char* source_ptr Pointer to where source data starts
static char* dest_ptr Pointer to where data should be written
static long source_len Length of source data
static long dest_len Length of destination, may be grown during encoding/decoding
static long grow_by How many bytes the destination length should grow when it runs out of room. Higher values may be faster but waste memory. The C function realloc() will be used to re-allocate the memory. If you don't want to use realloc(), derive a new class.

It is recommended that you use the memory I/O if you want speed.

After you have set all the variables in the I/O class of your choice to the correct values, call the Set() method in the class. It will set the global I/O function pointers to point to the ones in the class.

After all the I/O stuff is set, you can call a function that compresses, encrypts, converts to plain text, or figures out the checksum of a file. ComprLib has over 45 functions for doing this, but beware - not all of them work. Some crash. Sorry about that.

Stable functions include:

unsigned int UpdateCRC (unsigned int crc, int c);
unsigned int UpdateCRCReverse (unsigned int crc, int c);
unsigned long adler32();
unsigned long crc32();
void addsub_decode (char* pw);
void addsub_encode (char* pw);
void adler32 (char c, char* buf, long len);
void crc32 (unsigned char c, unsigned long* crc);
void hex_decode();
void hex_encode();
void invert_decode();
void invert_encode();
void lzari_decode ();
void lzari_encode ();
void lzhuf_decode ();
void lzhuf_encode ();
void lzss_decode ();
void lzss_encode ();
void lzw_encode ();
void rle1_decode ();
void rle1_encode ();
void rle2_decode ();
void rle2_encode ();
void uu_decode();
void uu_encode();
void xor_decode (char*);
void xor_encode (char*);
void xx_decode();
void xx_encode();
void spin(int speed /*= 10*/, char chars[] /*= "|/-\\\0"*/);

Now for an example program:

#include "comprlib.h"

int main()
    // Set the functions in 'ComprLibFileIO' class to the global I/O ones

    // Set the source and dest files to the standard input and output
    // streams.  To compress a file, use 'fopen'.
    ComprLibFileIO::source_file = stdin;
    ComprLibFileIO::dest_file = stdout;

    fprintf (stderr, "Compressing stdin to stdout...\n");

    // Compress the streams using LZSS method.
This example will compress standard input to standard output. But this is not MFC - see WinCompr for an MFC example. WinCompr is pretty easy to use, so I won't explain it here.

Hope you like it!


Download demo project - 79KB
Download source - 372KB
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved