diff --git a/lib/lz4.c b/lib/lz4.c index 7b8de77..45cc7c9 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -738,10 +738,18 @@ _clean_up: int LZ4_compress_fast_extState(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration) +{ + LZ4_resetStream((LZ4_stream_t*)state); + return LZ4_compress_fast_safeExtState(state, source, dest, inputSize, maxOutputSize, acceleration); +} + + +int LZ4_compress_fast_safeExtState(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration) { LZ4_stream_t_internal* ctx = &((LZ4_stream_t*)state)->internal_donotuse; - LZ4_resetStream((LZ4_stream_t*)state); if (acceleration < 1) acceleration = ACCELERATION_DEFAULT; + ctx->dictionary = NULL; + ctx->dictSize = 0; if (maxOutputSize >= LZ4_compressBound(inputSize)) { if (inputSize < LZ4_64Klimit) diff --git a/lib/lz4.h b/lib/lz4.h index d2e2103..0013ffe 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -179,13 +179,21 @@ LZ4LIB_API int LZ4_compress_fast (const char* src, char* dst, int srcSize, int d /*! +LZ4_compress_fast_safeExtState() : LZ4_compress_fast_extState() : Same compression function, just using an externally allocated memory space to store compression state. Use LZ4_sizeofState() to know how much memory must be allocated, and allocate it on 8-bytes boundaries (using malloc() typically). Then, provide it as 'void* state' to compression function. + + Use _safeExtState variant if LZ4_resetStream() was called on the state + buffer before being used for the first time (calls to this function leave + the state in a safe state, so zeroing is not required between calls). + Otherwise, using legacy _extState requires LZ4 to reinitialize the state + internally for every call. */ LZ4LIB_API int LZ4_sizeofState(void); +LZ4LIB_API int LZ4_compress_fast_safeExtState (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); LZ4LIB_API int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);