From d3c43d3251057da84f036c1e43fe015d2731cdd1 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 5 Jul 2014 16:48:49 +0100 Subject: Modified : streaming API (fast compression) --- lz4.c | 27 +++++++++++++++++++-------- lz4.h | 34 ++++++++++++++++++++-------------- programs/lz4io.c | 7 ++++++- 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/lz4.c b/lz4.c index 1a864e9..5ce6c71 100644 --- a/lz4.c +++ b/lz4.c @@ -684,10 +684,21 @@ int LZ4_compress_limitedOutput(const char* source, char* dest, int inputSize, in Experimental : Streaming functions *****************************************/ -void* LZ4_createStream() +/* + * LZ4_initStream + * Use this function once, to init a newly allocated LZ4_stream_t structure + * Return : 1 if OK, 0 if error + */ +void LZ4_resetStream (LZ4_stream_t* LZ4_stream) +{ + MEM_INIT(LZ4_stream, 0, sizeof(LZ4_stream_t)); +} + + +void* LZ4_createStream(void) { void* lz4s = ALLOCATOR(4, LZ4_STREAMSIZE_U32); - MEM_INIT(lz4s, 0, LZ4_STREAMSIZE); + LZ4_resetStream(lz4s); return lz4s; } @@ -698,15 +709,15 @@ int LZ4_free (void* LZ4_stream) } -int LZ4_loadDict (void* LZ4_dict, const char* dictionary, int dictSize) +int LZ4_loadDict (LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize) { LZ4_stream_t_internal* dict = (LZ4_stream_t_internal*) LZ4_dict; const BYTE* p = (const BYTE*)dictionary; const BYTE* const dictEnd = p + dictSize; const BYTE* base; - LZ4_STATIC_ASSERT(LZ4_STREAMSIZE >= sizeof(LZ4_stream_t_internal)); /* A compilation error here means LZ4_STREAMSIZE is not large enough */ - if (dict->initCheck) MEM_INIT(dict, 0, sizeof(LZ4_stream_t_internal)); /* Uninitialized structure detected */ + LZ4_STATIC_ASSERT(LZ4_STREAMSIZE >= sizeof(LZ4_stream_t_internal)); /* A compilation error here means LZ4_STREAMSIZE is not large enough */ + if (dict->initCheck) LZ4_resetStream(LZ4_dict); /* Uninitialized structure detected */ if (dictSize < MINMATCH) { @@ -803,12 +814,12 @@ FORCE_INLINE int LZ4_compress_continue_generic (void* LZ4_stream, const char* so } -int LZ4_compress_continue (void* LZ4_stream, const char* source, char* dest, int inputSize) +int LZ4_compress_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize) { return LZ4_compress_continue_generic(LZ4_stream, source, dest, inputSize, 0, notLimited); } -int LZ4_compress_limitedOutput_continue (void* LZ4_stream, const char* source, char* dest, int inputSize, int maxOutputSize) +int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize, int maxOutputSize) { return LZ4_compress_continue_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limitedOutput); } @@ -835,7 +846,7 @@ int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* } -int LZ4_saveDict (void* LZ4_dict, char* safeBuffer, int dictSize) +int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) { LZ4_stream_t_internal* dict = (LZ4_stream_t_internal*) LZ4_dict; const BYTE* previousDictEnd = dict->dictionary + dict->dictSize; diff --git a/lz4.h b/lz4.h index 1064fa1..275655d 100644 --- a/lz4.h +++ b/lz4.h @@ -166,53 +166,50 @@ int LZ4_decompress_safe_partial (const char* source, char* dest, int compressedS /* * LZ4_stream_t * information structure to track an LZ4 stream. - * important : set this structure content to zero before first use ! + * important : init this structure content before first use ! */ typedef struct { unsigned int table[LZ4_STREAMSIZE_U32]; } LZ4_stream_t; /* - * If you prefer dynamic allocation methods, - * LZ4_createStream - * provides a pointer (void*) towards an initialized LZ4_stream_t structure. - * LZ4_free just frees it. + * LZ4_resetStream + * Use this function to init a newly allocated LZ4_stream_t structure + * You can also reset an existing LZ4_stream_t structure */ -void* LZ4_createStream(); -int LZ4_free (void* LZ4_stream); - +void LZ4_resetStream (LZ4_stream_t* LZ4_stream); /* * LZ4_loadDict * Use this function to load a static dictionary into LZ4_stream. * Any previous data will be forgotten, only 'dictionary' will remain in memory. - * Loading a size of 0 is allowed (same effect as init). + * Loading a size of 0 is allowed. * Return : 1 if OK, 0 if error */ -int LZ4_loadDict (void* LZ4_stream, const char* dictionary, int dictSize); +int LZ4_loadDict (LZ4_stream_t* LZ4_stream, const char* dictionary, int dictSize); /* * LZ4_compress_continue * Compress data block 'source', using blocks compressed before as dictionary to improve compression ratio * Previous data blocks are assumed to still be present at their previous location. */ -int LZ4_compress_continue (void* LZ4_stream, const char* source, char* dest, int inputSize); +int LZ4_compress_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize); /* * LZ4_compress_limitedOutput_continue * Same as before, but also specify a maximum target compressed size (maxOutputSize) * If objective cannot be met, compression exits, and returns a zero. */ -int LZ4_compress_limitedOutput_continue (void* LZ4_stream, const char* source, char* dest, int inputSize, int maxOutputSize); +int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize, int maxOutputSize); /* * LZ4_saveDict - * If previously compressed data block is not guaranteed to remain at its previous memory location + * If previously compressed data block is not guaranteed to remain available at its memory location * save it into a safe place (char* safeBuffer) * Note : you don't need to call LZ4_loadDict() afterwards, * dictionary is immediately usable, you can therefore call again LZ4_compress_continue() * Return : 1 if OK, 0 if error * Note : any dictSize > 64 KB will be interpreted as 64KB. */ -int LZ4_saveDict (void* LZ4_stream, char* safeBuffer, int dictSize); +int LZ4_saveDict (LZ4_stream_t* LZ4_stream, char* safeBuffer, int dictSize); /************************************************ @@ -290,6 +287,15 @@ int LZ4_sizeofState(void); int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize); int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize); +/* + * If you prefer dynamic allocation methods, + * LZ4_createStream + * provides a pointer (void*) towards an initialized LZ4_stream_t structure. + * LZ4_free just frees it. + */ +void* LZ4_createStream(void); +int LZ4_free (void* LZ4_stream); + /* Obsolete streaming functions; use new streaming interface whenever possible */ void* LZ4_create (const char* inputBuffer); int LZ4_sizeofStreamState(void); diff --git a/programs/lz4io.c b/programs/lz4io.c index 65d1445..7617cff 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -376,6 +376,11 @@ static int LZ4IO_LZ4_compress_limitedOutput_continue (void* ctx, const char* sou return LZ4_compress_limitedOutput_continue(ctx, source, dest, inputSize, maxOutputSize); } +static int LZ4IO_LZ4_saveDict (void* LZ4_stream, char* safeBuffer, int dictSize) +{ + return LZ4_saveDict ((LZ4_stream_t*) LZ4_stream, safeBuffer, dictSize); +} + static int LZ4IO_LZ4_slideInputBufferHC (void* ctx, char* buffer, int size) { (void)size; (void)buffer; @@ -411,7 +416,7 @@ static int compress_file_blockDependency(char* input_filename, char* output_file { initFunction = LZ4IO_LZ4_createStream; compressionFunction = LZ4IO_LZ4_compress_limitedOutput_continue; - nextBlockFunction = LZ4_saveDict; + nextBlockFunction = LZ4IO_LZ4_saveDict; freeFunction = LZ4_free; } else -- cgit v0.12