Encoding a buffer with LZNV DLL
5.1 Compressing a buffer with LZNV DLL
5.1 Compressing (encoding) 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
//
// 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;
int64_t s = _get_ms(); // use a high performance counter
_res = (*_p_LZNV_DLL_Main_Buffer_funct)(_lznv_buffer_compression_commands);
int64_t e = _get_ms(); // use a high performance counter
_output_bytes = _lznv_buffer_compression_commands._i_destination_buffer_size;
double speed_encode = (e - s) > 0 ? (double)(_chunk_to_process / 1024) / (e - s) : 1000;
wprintf(L"\n Encoding: %7d --> %7d [%3lldms] | Speed: %4.02fMB/s | res = %s ", _chunk_to_process, _output_bytes, e - s, speed_encode, _res == 0 ? L"Ok!" : L"NOk!");
if(NO_ERROR != _res)
{
const wchar_t* _wch_error_string = _get_extended_error_information(_res);
wprintf(L"-> Error Detected: %s", _wch_error_string);
return _res;
}
}
// [...] de-allocated buffers, compare output, etc
return _res;
}