From 1c0d854e61ff3392e25fcd30814ca118b5fcbf39 Mon Sep 17 00:00:00 2001 From: leandor Date: Sat, 5 Nov 2016 00:44:00 -0300 Subject: [PATCH] (#18) Started wrapping the LZ4 API Started definition of internal redirection functions under namespace lz4::api that have a safer interface in general by using constructs taken from the CPP guidelines. --- src/papi/lz4/api.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++ src/papi/lz4/api.h | 60 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 src/papi/lz4/api.cpp create mode 100644 src/papi/lz4/api.h diff --git a/src/papi/lz4/api.cpp b/src/papi/lz4/api.cpp new file mode 100644 index 0000000..2f97459 --- /dev/null +++ b/src/papi/lz4/api.cpp @@ -0,0 +1,60 @@ +#include "api.h" + +using namespace lz4::api; +using namespace gsl; + +namespace +{ + #include +}; + +int compressBound(int inputSize) + { + return LZ4_compressBound(inputSize); + }; + +int compressBound(const span input) + { + return LZ4_compressBound(input.length()); + }; + +int compress_default(const span source, span dest) + { + return LZ4_compress_default(source.data(), dest.data(), source.length(), dest.length()); + }; + +int decompress_safe (const span source, span dest) + { + return LZ4_decompress_safe(source.data(), dest.data(), source.length(), dest.length()); + }; + +int sizeofState() + { + return LZ4_sizeofState(); + }; + +int compress_fast_extState(span state, const span source, span dest, int acceleration) + { + return LZ4_compress_fast_extState(state.data(), source.data(), dest.data(), source.length(), dest.length(), acceleration); + }; + +int compress_destSize (span& source, span dest) + { + auto sourceSize = source.length(); + volatile int sourceUsed = sourceSize; + auto atExit = finally([&](){ + source = source.subspan(sourceSize - sourceUsed); + }); + + return LZ4_compress_destSize(source.data(), dest.data(), const_cast(&sourceUsed), dest.length()); + }; + +int decompress_fast (const span source, span dest, int uncompressedSize) + { + return LZ4_decompress_fast(source.data(), dest.data(), uncompressedSize); + }; + +int decompress_safe_partial (const span source, span dest, int targetOutputSize) + { + return LZ4_decompress_safe_partial (source.data(), dest.data(), source.length(), targetOutputSize, dest.length()); + }; \ No newline at end of file diff --git a/src/papi/lz4/api.h b/src/papi/lz4/api.h new file mode 100644 index 0000000..3cb79c7 --- /dev/null +++ b/src/papi/lz4/api.h @@ -0,0 +1,60 @@ +#ifndef __CINT_LZ4_API__ +#define __CINT_LZ4_API__ +#pragma once + +#include +#include "module.h" + +namespace lz4 { namespace api { + + using namespace gsl; + + constexpr auto MAX_INPUT_SIZE = 0x7E000000; + + template + constexpr auto COMPRESSBOUND = ((unsigned)(Size) > (unsigned)MAX_INPUT_SIZE ? 0 : (Size) + ((Size)/255) + 16) + + /** + Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible) + This function is primarily useful for memory allocation purposes (destination buffer size). + + Macro COMPRESSBOUND is also provided for compilation-time evaluation (stack memory allocation for example). + Note that compress_default() compress faster when dest buffer size is >= compressBound(srcSize) + + @param inputSize max supported value is MAX_INPUT_SIZE + @return maximum output size in a "worst case" scenario + or 0, if input size is too large ( > MAX_INPUT_SIZE) + */ + int compressBound(int inputSize); + int compressBound(const span input); + + /** + Compresses buffer 'source' into already allocated 'dest' buffer. + + Compression is guaranteed to succeed if 'dest.lenght()' >= compressBound(source.length()). + It also runs faster, so it's a recommended setting. + + If the function cannot compress 'source' into a more limited 'dest' budget, + compression stops *immediately*, and the function result is zero. + As a consequence, 'dest' content is not valid. + + This function never writes outside 'dest' buffer, nor read outside 'source' buffer. + + @param source The input data buffer. Max supported input size is LZ4_MAX_INPUT_VALUE + @param dest The output data buffer for compressed data. + + @return The number of bytes written into buffer 'dest' (necessarily <= dest.length()) + or 0 if compression fails + */ + int compress_default(const span source, span dest); + + int decompress_safe (const span source, span dest); + int sizeofState(); + int compress_fast_extState(span state, const span source, span dest, int acceleration); + int compress_destSize (span& source, span dest); + int decompress_fast (const span source, span dest, int uncompressedSize); + int decompress_safe_partial (const span source, span dest, int targetOutputSize); +}; +}; + +#endif // __CINT_LZ4_API__