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


  • Please help me

    Posted by Legacy on 03/24/2003 12:00am

    Originally posted by: mrNghia

    When I runed WinCompr in VC++ 6.0:Command line error D2016 : '/ZI' and '/Ob2' command-line options are incompatible.Please help me.


  • nihao

    Posted by Legacy on 07/11/2001 12:00am

    Originally posted by: hjwang


  • some words to the compression-formats

    Posted by Legacy on 04/09/2001 12:00am

    Originally posted by: M.Rissler

    hello, can someone tell me which of these used compress formats can be included into commercial applications for free ;-) .

    thanks a lot!

    M. Ri�ler

  • Where is the comprlib.lib file in your demo source?

    Posted by Legacy on 04/20/2000 12:00am

    Originally posted by: Jayson

    I cannot find the comprlib.lib file in your demo source, nor do I know how to use the source code available to make my own. Please help


  • LZW Patent

    Posted by Legacy on 02/22/2000 12:00am

    Originally posted by: Greg Heck

    Be careful when using the LZW encode in any software you plan to release outside your home. LZW is patented by Unisys and anyone implementing the decoder or encoder in their software must obtain a license.

  • Can you tell a word where's "lzw_decode"?

    Posted by Legacy on 10/25/1999 12:00am

    Originally posted by: Victor

    I could not find function lzw_decode... :o(
    Can I decode data, encoded with LZW method(used here)?

    (And one more comment:
    I needed to compress data from source to destination memory block, but lz-huffman algorithm described in this project use its' own memory cycle buffer. It copies source mem buffer through its' own to desination block(with compression, of course).
    ( There is no problem if You have PII... :)
    I'll get rid of this buffer...

    Any way Thanks for "WinCompr". It was interesting to decrypt it ;-)

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Microsoft® Office 365 is a top choice for enterprises that want a cloud–based suite of productivity collaboration applications. With Office 365, you get access to Microsoft™ Office solutions practically anytime, anywhere, on virtually any device. It's a great option for current Microsoft users who can now build on their experience with Microsoft™ solutions while enjoying the flexibility of a cloud-based delivery. But even organizations with no previous investment in Microsoft will find that …

  • Entire organizations suffer when their networks can't keep up and new opportunities are put on hold. Waiting on service providers isn't good business. In these examples, learn how to simplify network management so that your organization can better manage costs, adapt quickly to business demands, and seize market opportunities when they arise.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date