5.2 Decompressing a buffer with LZNV DLL


   5.2 Decompressing (decoding) a buffer using the LZNV DLL


The code is available in function _loadDLL from LZNV_DLL_Tester.cpp



#include <LZNV_Defines.h> // load header file from predefined include paths


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

/// \details    The function needs prior LZNV Dll loading and export function "LZNV_Main_Buffer_Compress" to work

/// \pre        The function needs prior LZNV Dll loading and export function "LZNV_Main_Buffer_Compress" to work

/// \return     returns NO_ERROR (0) 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


    // [...] encode buffer


    //

    // Prepare buffer decoding/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


        int64_t s = _get_ms(); // use a high performance counter

        _res = (*_p_LZNV_DLL_Main_Buffer_funct)(_lznv_buffer_decompression_commands);

        int64_t e = _get_ms();


        _output_bytes = _lznv_buffer_decompression_commands._i_destination_buffer_size;

        double speed_decode = (e - s) > 0 ? (double)(_output_bytes / 1024) / (e - s) : 1000;

       

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


        if (NO_ERROR != _res)

        {

            wprintf(L"-> Error Detected: %s",  _get_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;

}