/*
// Copyright 2017 Intel Corporation All Rights Reserved.
//
// The source code, information and material ("Material") contained herein is
// owned by Intel Corporation or its suppliers or licensors, and title
// to such Material remains with Intel Corporation or its suppliers or
// licensors. The Material contains proprietary information of Intel
// or its suppliers and licensors. The Material is protected by worldwide
// copyright laws and treaty provisions. No part of the Material may be used,
// copied, reproduced, modified, published, uploaded, posted, transmitted,
// distributed or disclosed in any way without Intel's prior express written
// permission. No license under any patent, copyright or other intellectual
// property rights in the Material is granted to or conferred upon you,
// either expressly, by implication, inducement, estoppel or otherwise.
// Any license under such intellectual property rights must be express and
// approved by Intel in writing.
//
// Unless otherwise agreed by Intel in writing,
// you may not remove or alter this notice or any other notice embedded in
// Materials by Intel or Intel's suppliers or licensors in any way.
//
*/

/*
 The example below shows how to use the functions:
     ippsEncodeLZ4HashTableGetSize_8u
     ippsEncodeLZ4HashTableInit_8u
     ippsEncodeLZ4_8u
     ippsDecodeLZ4_8u
*/

#include <stdio.h>
#include <string.h>

#include <ippdc.h>
#include <ipps.h>

/* Next two defines are created to simplify code reading and understanding */
#define EXIT_MAIN       exitLine:                              /* Label for Exit */
#define check_sts(st)   if((st) != ippStsNoErr) goto exitLine; /* Go to Exit if
                Intel(R) IPP function returned status different from ippStsNoErr */

#define TEST_SIZE (1024)

int main(void)
{
    Ipp8u       *srcBuf = NULL, *comprBuf = NULL, *decomprBuf = NULL, *hashTable = NULL;
    IppStatus   st = ippStsNoErr;
    int         hashSize = 0, 
                comprLen = TEST_SIZE + TEST_SIZE / 255 + 16,    /* Extra bytes for
                                                                    uncompressible data */
                decomprLen = TEST_SIZE + 33;
    int         i;

    srcBuf      = ippsMalloc_8u(TEST_SIZE);
    decomprBuf  = ippsMalloc_8u(decomprLen);                    /* Spare bytes for "wild"
                                                            (non-safe) decompression */
    comprBuf    = ippsMalloc_8u(comprLen);
    /* Initialize source buffer */
    check_sts(      st = ippsVectorJaehne_8u(srcBuf, TEST_SIZE, IPP_MAX_8U) )
    for(i = 0; i < TEST_SIZE; i++)
        srcBuf[i] >>= 6;                  /* Decrease source data entropy */
    /* Init and allocate hash table */
    check_sts(  st = ippsEncodeLZ4HashTableGetSize_8u(&hashSize)    )
    hashTable   = ippsMalloc_8u(hashSize);
    check_sts(  st = ippsEncodeLZ4HashTableInit_8u(hashTable, TEST_SIZE)    )
    /* Compress source data */
    check_sts(  st = ippsEncodeLZ4_8u((const Ipp8u*)srcBuf, TEST_SIZE, comprBuf,
                                      &comprLen, hashTable) )
    /* Print compression result */
    printf("Compression: %d bytes compressed into %d bytes\n", TEST_SIZE, comprLen);
    /* Decompression */
    decomprLen  = TEST_SIZE + 33;
    check_sts(  st = ippsDecodeLZ4_8u((const Ipp8u*)comprBuf, comprLen, decomprBuf,
                                      &decomprLen)  )
    /* Check */
    if(decomprLen == TEST_SIZE)/* Decompressed size must be equal to source data size */
    {
        if(memcmp(srcBuf, decomprBuf, TEST_SIZE) != 0)
        {
            printf("Wrong decompression!\n");
            st = ippStsErr;
        }
        else
            printf("Decompressed by ippsDecodeLZ4_8u OK\n");
    }
    else
        printf("Invalid decompressed length %d\n", decomprLen);

EXIT_MAIN
    ippsFree(srcBuf);
    ippsFree(comprBuf);
    ippsFree(decomprBuf);
    ippsFree(hashTable);
    return (int)st;
}