LZNV SDK Sample Buffer Encode/Decode Function


   LZNV SDK Sample Buffer Encode/Decode Function


_test_encode_decode_buffer is a sample function to perform buffer compression/decompression using LZNV SDK DLL


/// \brief      _test_encode_decode_buffer is a sample function that performs simple buffer compression / decompression.

/// \details    The function needs prior LZNV Dll loading and export function "LZNV_Main_Buffer_Compress" to work. See _loadDLL() function

/// \pre        The function needs prior LZNV Dll loading and export function "LZNV_Main_Buffer_Compress" to work. See _loadDLL() function

/// \return     returns _LZNV_NO_ERROR_ (0x00) in case of NO ERRORS or one of the error codes defined in LZNV_Defines.h header file

///             other possible values:

///             - _LZNV_ERROR_INVALID_COMPRESSION_METHOD      

///             - _LZNV_ERROR_INVALID_BUFFER_SIZE            

///             - _LZNV_ERROR_EXPORTED_MAIN_FUNC_NOT_FOUND    

///


int32_t _test_encode_decode_buffer()

{

    int32_t _res = _LZNV_NO_ERROR_;


    const char* _wchr_test_string = "text_to_be_compressed_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

   

    int32_t _chunk_to_process = (int32_t)strlen(_wchr_test_string); // size of the text to be compressed


    uint8_t* __restrict _uint8_buffer_to_encode       = (uint8_t * __restrict)malloc(_chunk_to_process + 1);

    uint8_t* __restrict _uint8_output_buffer          = (uint8_t * __restrict)malloc(_chunk_to_process + 1);  

    uint8_t* __restrict _uint8_output_buffer_original = (uint8_t * __restrict)malloc(_chunk_to_process + 1);  


    ASSERT(_uint8_buffer_to_encode);

    ASSERT(_uint8_output_buffer);

    ASSERT(_uint8_output_buffer_original);


    if (!_uint8_buffer_to_encode) {

        wprintf(L"\n Not enough memory for chunk size %d... Exiting!\n\n", _chunk_to_process);

        return _LZNV_ERROR_MEMORY_ALLOCATION_ERROR_OR_SIZE;

    }


    if (!_uint8_output_buffer) {

        wprintf(L"\n Not enough memory for chunk size %d... Exiting!\n\n", _chunk_to_process);

        return _LZNV_ERROR_MEMORY_ALLOCATION_ERROR_OR_SIZE;

    }

    if (!_uint8_output_buffer_original) {

        wprintf(L"\n Not enough memory for chunk size %d... Exiting!\n\n", _chunk_to_process);

        return _LZNV_ERROR_MEMORY_ALLOCATION_ERROR_OR_SIZE;

    }


    int32_t _output_bytes = 0;

    uint8_t _encoding_method =  _LZNV_HUFFMAN_COMPRESSION;


    memcpy(_uint8_buffer_to_encode, _wchr_test_string, _chunk_to_process); // construct a more complex "test string" to compress


    //

    // Prepare buffer compression command structure

    //

    {

        LZNV_BUFFER_COMMAND_PARAMS _lznv_buffer_compression_commands = { 0 };


        _lznv_buffer_compression_commands._command_to_execute = _LZNV_COMMAND_ENCODE;

        _lznv_buffer_compression_commands._cpu_priority_level = _LZNV_NORMAL_PRIORITY_CLASS;


        _lznv_buffer_compression_commands._encoding_method = _encoding_method; // LZ or ENTROPY

        _lznv_buffer_compression_commands._memory_level = _LZNV_BUFFER_SIZE_256KB_INDX;


        _lznv_buffer_compression_commands._p_source_buffer = (uint8_t*)_wchr_test_string; // to use _uint8_buffer_to_encode for more complex examples

        _lznv_buffer_compression_commands._i_source_buffer_size = _chunk_to_process;


        _lznv_buffer_compression_commands._p_destination_buffer = _uint8_output_buffer;

        _lznv_buffer_compression_commands._i_destination_buffer_size = _chunk_to_process;


        uint64_t dwS = GetTickCount64();


        _res = (*_p_LZNV_DLL_Main_Buffer_funct)(_lznv_buffer_compression_commands);


        uint64_t dwE = GetTickCount64();

       

        // ...


        _output_bytes = _lznv_buffer_compression_commands._i_destination_buffer_size;


        wprintf(L"\n\n Encoding: %d --> %d [%lldms] [res = %d, %s]", _chunk_to_process, _output_bytes, dwE - dwS, _res, _res == 0 ? L"Ok!" : L"NOk!");


        if(NO_ERROR != _res)

        {

            wprintf(L"-> Error Detected: ");

            _print_extended_error_information(_res);

        }

    }


    //

    // Prepare buffer decompression command structure

    //

    {

        LZNV_BUFFER_COMMAND_PARAMS _lznv_buffer_decompression_commands = { 0 };


        _lznv_buffer_decompression_commands._encoding_method = _encoding_method;                     // Required

        _lznv_buffer_decompression_commands._command_to_execute = _LZNV_COMMAND_DECODE;              // Required

        _lznv_buffer_decompression_commands._cpu_priority_level = _LZNV_NORMAL_PRIORITY_CLASS;      


        _lznv_buffer_decompression_commands._p_source_buffer = _uint8_output_buffer;                 // Required, Preallocate

        _lznv_buffer_decompression_commands._i_source_buffer_size = _output_bytes;                   // Required, Non-zero value


        _lznv_buffer_decompression_commands._p_destination_buffer = _uint8_output_buffer_original;   // Required, Preallocate

        _lznv_buffer_decompression_commands._i_destination_buffer_size = _chunk_to_process;          // Required, Non-zero value


        uint64_t dwS = GetTickCount64();


        _res = (*_p_LZNV_DLL_Main_Buffer_funct)(_lznv_buffer_decompression_commands);


        uint64_t dwE = GetTickCount64();


        _output_bytes = _lznv_buffer_decompression_commands._i_destination_buffer_size;


        wprintf(L"\n\n Decoding: %d --> %d [%lldms] [res = %d, %s]", _chunk_to_process, _output_bytes, dwE - dwS, _res, _res == 0 ? L"Ok!" : L"NOk!");


        if (NO_ERROR != _res)

        {

            wprintf(L"-> Error Detected: ");

            _print_extended_error_information(_res);

        }


    }


    ASSERT(0x00 == _res);

       

    _res = memcmp(_uint8_output_buffer_original, _wchr_test_string, _chunk_to_process);


    wprintf(L"\n Final res memcp = %d", _res);

   

    ASSERT(_uint8_buffer_to_encode);

    ASSERT(_uint8_output_buffer);

    ASSERT(_uint8_output_buffer_original);

   

    if(_uint8_buffer_to_encode)

      free(_uint8_buffer_to_encode);

 

    if(_uint8_output_buffer)

        free(_uint8_output_buffer);


    if(_uint8_output_buffer_original)

        free(_uint8_output_buffer_original);


    return _res;

}