All of the interesting technological, artistic or just plain fun subjects I'd investigate if I had an infinite number of lifetimes. In other words, a dumping ground...

Monday 12 July 2010

Using zlib in C programming

g++ -Wall -Wextra comp.cpp -lz -o comp

#include <iostream>
#include <cstring>
#include <zlib.h>

using namespace std;

/*
ZEXTERN int ZEXPORT compress OF((Bytef *dest,   uLongf *destLen,
                                 const Bytef *source, uLong sourceLen));
     Compresses the source buffer into the destination buffer.  sourceLen is
   the byte length of the source buffer. Upon entry, destLen is the total
   size of the destination buffer, which must be at least the value returned
   by compressBound(sourceLen). Upon exit, destLen is the actual size of the
   compressed buffer.
     This function can be used to compress a whole file at once if the
   input file is mmap'ed.
     compress returns Z_OK if success, Z_MEM_ERROR if there was not
   enough memory, Z_BUF_ERROR if there was not enough room in the output
   buffer.

ZEXTERN int ZEXPORT uncompress OF((Bytef *dest,   uLongf *destLen,
                                   const Bytef *source, uLong sourceLen));
     Decompresses the source buffer into the destination buffer.  sourceLen is
   the byte length of the source buffer. Upon entry, destLen is the total
   size of the destination buffer, which must be large enough to hold the
   entire uncompressed data. (The size of the uncompressed data must have
   been saved previously by the compressor and transmitted to the decompressor
   by some mechanism outside the scope of this compression library.)
   Upon exit, destLen is the actual size of the compressed buffer.
     This function can be used to decompress a whole file at once if the
   input file is mmap'ed.

     uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
   enough memory, Z_BUF_ERROR if there was not enough room in the output
   buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.

*/

int main()
{
    char d[] =
    "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
    cout << "sizeof(d): " << sizeof(d) << endl;
    const unsigned int DESTLEN = 256;
    char dest[DESTLEN];
    if (sizeof(d) > DESTLEN) {
        cerr << "Dest size " << DESTLEN << " too small : " << sizeof(d) << endl;
        return 0;
    }
    uLongf destlen = DESTLEN;
    unsigned int compress_bound = compressBound(sizeof(d));
    cout << "Source len: " << sizeof(d) << ", size of compress bound: " << compress_bound << ", size of destlen: " <<
    DESTLEN << endl;
    if (compress_bound > DESTLEN) {
        cerr << "Dest size " << DESTLEN << " too small for compress_bound: " << compress_bound << endl;
        return 0;
    }
    int rtn = compress((Bytef*)dest, &destlen, (Bytef*)d, sizeof(d));
    if (rtn != Z_OK) {
        cerr << "Compress failed" << rtn << endl;
    } else {
        cout << "Compress suceeded: destlen: " << destlen << endl;
        //cout << "Compressed: " << dest << endl;
    }

    char dest2[DESTLEN] = {0};
    uLongf destlen2 = DESTLEN;
    rtn = uncompress((Bytef*)dest2, &destlen2, (Bytef*)dest, destlen);
    if (rtn != Z_OK) {
        cerr << "Decompress failed" << rtn << endl;
    } else {
        cout << "Decompress suceeded: destlen: " << destlen2 << endl;
        cout << "Decompressed: " << dest2 << endl;
    }
    rtn = memcmp(d, dest2, sizeof(d));
    cout << "Comparison: " << rtn << endl;
    return 0;
}

No comments:

tim's shared items

Add to Google Reader or Homepage