Example


Example 1 contains the sample program compressexample.c (located in the Examples folder), which shows how to use the Compression folio. This program loads itself into a memory buffer, compresses the data, decompresses it, and finally compares the original data with the decompressed data to make sure the compression process was successful.

Example 1: Example of compressing and decompressing data (compressexample.c).

#include "types.h"
#include "filestream.h"
#include "filestreamfunctions.h"
#include "stdio.h"
#include "mem.h"
#include "compression.h"


/*****************************************************************************/
int main(int argc, char **argv)
{
Stream       *stream;
Err           err;
bool          same;
uint32        i;
int32         fileSize;
uint32       *originalData;
uint32       *compressedData;
uint32       *finalData;
int32         numFinalWords;
int32         numCompWords;

    err = OpenCompressionFolio();
    if (err >= 0)
    {
        stream = OpenDiskStream(argv[0],0);
        if (stream)
        {
            fileSize       = stream->st_FileLength & 0xfffffffc;
            originalData   = (uint32 *)malloc(fileSize);
            compressedData = (uint32 *)malloc(fileSize);
            finalData      = (uint32 *)malloc(fileSize);

            if (originalData && compressedData && finalData)
            {
                if (ReadDiskStream(stream,(char *)originalData,fileSize) == 

                   fileSize)
                {
                    err = Compress(originalData, fileSize / sizeof(uint32),
                                   compressedData, fileSize / sizeof(uint32));
                    if (err >= 0)
                    {
                        numCompWords = err;
                        err = Decompress(compressedData, numCompWords,
                                         finalData, fileSize / sizeof(uint32));
                        if (err >= 0)
                        {
                            numFinalWords = err;
                            printf("Original data size    : %d\n",fileSize / 

                                sizeof(uint32));
                            printf("Compressed data size  : %d\n",numCompWords);
                            printf("Uncompressed data size: %d\n",numFinalWords);

                            same = TRUE;
                            for (i = 0; i < fileSize / sizeof(uint32); i++)
                            {
                                if (originalData[i] != finalData[i])
                                {
                                    same = FALSE;
                                    break;
                                }
                            }

                            if (same)
                            {
                                printf("Uncompressed data matched original\n");
                            }
                            else
                            {
                                printf("Uncompressed data differed with 

                                        original!\n");
                                for (i = 0; i < 10; i++)
                                {
                                    printf("orig $%08x, final $%08x, comp 

                                          $%08x\n",
                                           originalData[i],
                                           finalData[i],
                                           compressedData[i]);
                                }
                            }
                        }
                        else
                        {
                            PrintError(NULL,"decompress data",NULL,err);
                        }
                    }
                    else
                    {
                        PrintError(NULL,"compress data",NULL,err);
                    }
                }
                else
                {
                    printf("Could not read whole file\n");
                }
            }
            else
            {
                printf("Could not allocate memory buffers\n");
            }

            free(originalData);
            free(compressedData);
            free(finalData);

            CloseDiskStream(stream);
        }
        else
        {
            printf("Could not open '%s' as an input file\n",argv[0]);
        }
        CloseCompressionFolio();
    }
    else
    {
        PrintError(NULL,"open the compression folio",NULL,err);
    }

    return (0);
}