Skip to content
This repository has been archived by the owner on Sep 5, 2020. It is now read-only.

Commit

Permalink
(#18) Started wrapping the LZ4 API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
leandor committed Jul 1, 2017
1 parent 27e270c commit 1c0d854
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/papi/lz4/api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "api.h"

using namespace lz4::api;
using namespace gsl;

namespace
{
#include <lz4.h>
};

int compressBound(int inputSize)
{
return LZ4_compressBound(inputSize);
};

int compressBound(const span<char> input)
{
return LZ4_compressBound(input.length());
};

int compress_default(const span<char> source, span<char> dest)
{
return LZ4_compress_default(source.data(), dest.data(), source.length(), dest.length());
};

int decompress_safe (const span<char> source, span<char> dest)
{
return LZ4_decompress_safe(source.data(), dest.data(), source.length(), dest.length());
};

int sizeofState()
{
return LZ4_sizeofState();
};

int compress_fast_extState(span<byte> state, const span<char> source, span<char> dest, int acceleration)
{
return LZ4_compress_fast_extState(state.data(), source.data(), dest.data(), source.length(), dest.length(), acceleration);
};

int compress_destSize (span<char>& source, span<char> 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<int*>(&sourceUsed), dest.length());
};

int decompress_fast (const span<char> source, span<char> dest, int uncompressedSize)
{
return LZ4_decompress_fast(source.data(), dest.data(), uncompressedSize);
};

int decompress_safe_partial (const span<char> source, span<char> dest, int targetOutputSize)
{
return LZ4_decompress_safe_partial (source.data(), dest.data(), source.length(), targetOutputSize, dest.length());
};
60 changes: 60 additions & 0 deletions src/papi/lz4/api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#ifndef __CINT_LZ4_API__
#define __CINT_LZ4_API__
#pragma once

#include <gsl/gsl>
#include "module.h"

namespace lz4 { namespace api {

using namespace gsl;

constexpr auto MAX_INPUT_SIZE = 0x7E000000;

template<int Size>
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<char> 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<char> source, span<char> dest);

int decompress_safe (const span<char> source, span<char> dest);
int sizeofState();
int compress_fast_extState(span<byte> state, const span<char> source, span<char> dest, int acceleration);
int compress_destSize (span<char>& source, span<char> dest);
int decompress_fast (const span<char> source, span<char> dest, int uncompressedSize);
int decompress_safe_partial (const span<char> source, span<char> dest, int targetOutputSize);
};
};

#endif // __CINT_LZ4_API__

0 comments on commit 1c0d854

Please sign in to comment.