Example 3 - 5.2 English - 68552

AOCL API Guide (68552)

Document ID
68552
Release Date
2025-12-29
Version
5.2 English

The following test program shows the sample usage and calling sequence of aocl - compression APIs for multi-threaded gzip compression :

Note

Build AOCL-Compression library with AOCL_ENABLE_THREADS.

 1#include <stdio.h>
 2#include <stdlib.h>
 3#include "zlib.h"
 4
 5int main(int argc, char** argv)
 6{
 7    FILE* inFp = NULL;
 8    uLong inpSize = 0;
 9    Bytef* inPtr = NULL, * compPtr = NULL, * decompPtr = NULL;
10    uLong outSize = 0;
11    int resultComp, resultDecomp;
12    int level = 6;
13
14    printf("Running example_compress2_gzip\n");
15    printf("Demonstrates using native APIs for GZIP compression and decompression\n");
16    if (argc < 2)
17    {
18        printf("Provide input test file path\n");
19        return -1;
20    }
21    inFp = fopen(argv[1], "rb");
22    fseek(inFp, 0L, SEEK_END);
23    inpSize = ftell(inFp);
24    rewind(inFp);
25
26    // 1. allocate buffers
27    outSize = compressBound_gzip(inpSize);
28    if (outSize == 0)
29    {
30        printf("CompressBound_gzip: failed\n");
31        goto error_exit;
32    }
33    inPtr     = (Bytef*)calloc(1, inpSize);
34    compPtr   = (Bytef*)calloc(1, outSize);
35    decompPtr = (Bytef*)calloc(1, inpSize);
36    inpSize = fread(inPtr, 1, inpSize, inFp);
37
38    // 2. compress
39    resultComp = compress2_gzip(compPtr, &outSize, inPtr, inpSize, level);
40    if (resultComp != Z_OK)
41    {
42        printf("Compression: failed\n");
43        goto error_exit;
44    }
45    printf("Compression: done\n");
46
47    // 3. decompress
48    resultDecomp = uncompress2_gzip(decompPtr, &inpSize, compPtr, &outSize);
49    if (resultDecomp != Z_OK)
50    {
51        printf("Decompression Failure\n");
52        goto error_exit;
53    }
54    printf("Decompression: done\n");
55
56    // 4. cleanup
57error_exit:
58    if (inPtr)
59        free(inPtr);
60    if (compPtr)
61        free(compPtr);
62    if (decompPtr)
63        free(decompPtr);
64    if (inFp)
65        fclose(inFp);
66    return 0;
67}

To build this example test program on a Linux system using GCC or AOCC, you must specify path to aocl_compression.h header file and link with libaocl_compression.so file as follows:

gcc test.c -I <aocl_compression.h file path> -L <libaocl_compression.so directory path> -laocl_compression -Wl,-rpath=<libaocl_compression.so directory path>

Before running the example program, ensure it points to the right library dependencies for openMP, etc.