From 3c0332600490003b1267a9f0a51a6a315b52891e Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 29 Jun 2016 12:54:23 +0200 Subject: [PATCH] minor refactoring (coding style) --- lib/lz4.c | 222 +++++++++++++++++++--------------------------------- programs/.gitignore | 6 +- programs/lz4cli.c | 79 +++++++------------ 3 files changed, 116 insertions(+), 191 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index ec32a12..a8d3bce 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -33,7 +33,7 @@ */ -/************************************** +/*-************************************ * Tuning parameters **************************************/ /* @@ -50,7 +50,7 @@ #define ACCELERATION_DEFAULT 1 -/************************************** +/*-************************************ * CPU Feature Detection **************************************/ /* LZ4_FORCE_MEMORY_ACCESS @@ -84,13 +84,13 @@ #endif -/************************************** +/*-************************************ * Includes **************************************/ #include "lz4.h" -/************************************** +/*-************************************ * Compiler Options **************************************/ #ifdef _MSC_VER /* Visual Studio */ @@ -121,7 +121,7 @@ #define unlikely(expr) expect((expr) != 0, 0) -/************************************** +/*-************************************ * Memory routines **************************************/ #include /* malloc, calloc, free */ @@ -131,7 +131,7 @@ #define MEM_INIT memset -/************************************** +/*-************************************ * Basic Types **************************************/ #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ @@ -150,7 +150,7 @@ #endif -/************************************** +/*-************************************ * Reading and writing into memory **************************************/ #define STEPSIZE sizeof(size_t) @@ -159,7 +159,7 @@ static unsigned LZ4_64bits(void) { return sizeof(void*)==8; } static unsigned LZ4_isLittleEndian(void) { - const union { U32 i; BYTE c[4]; } one = { 1 }; // don't use static : performance detrimental + const union { U32 i; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ return one.c[0]; } @@ -206,17 +206,14 @@ static void LZ4_write16(void* memPtr, U16 value) memcpy(memPtr, &value, sizeof(value)); } -#endif // LZ4_FORCE_MEMORY_ACCESS +#endif /* LZ4_FORCE_MEMORY_ACCESS */ static U16 LZ4_readLE16(const void* memPtr) { - if (LZ4_isLittleEndian()) - { + if (LZ4_isLittleEndian()) { return LZ4_read16(memPtr); - } - else - { + } else { const BYTE* p = (const BYTE*)memPtr; return (U16)((U16)p[0] + (p[1]<<8)); } @@ -224,12 +221,9 @@ static U16 LZ4_readLE16(const void* memPtr) static void LZ4_writeLE16(void* memPtr, U16 value) { - if (LZ4_isLittleEndian()) - { + if (LZ4_isLittleEndian()) { LZ4_write16(memPtr, value); - } - else - { + } else { BYTE* p = (BYTE*)memPtr; p[0] = (BYTE) value; p[1] = (BYTE)(value>>8); @@ -258,7 +252,7 @@ static void LZ4_wildCopy(void* dstPtr, const void* srcPtr, void* dstEnd) } -/************************************** +/*-************************************ * Common Constants **************************************/ #define MINMATCH 4 @@ -281,21 +275,19 @@ static const int LZ4_minLength = (MFLIMIT+1); #define RUN_MASK ((1U<> 58]; # endif - } - else /* 32 bits */ - { + } else /* 32 bits */ { # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) unsigned long r; _BitScanForward( &r, (U32)val ); @@ -320,11 +310,8 @@ static unsigned LZ4_NbCommonBytes (register size_t val) return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27]; # endif } - } - else /* Big Endian CPU */ - { - if (LZ4_64bits()) - { + } else /* Big Endian CPU */ { + if (LZ4_64bits()) { # if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT) unsigned long r = 0; _BitScanReverse64( &r, val ); @@ -338,9 +325,7 @@ static unsigned LZ4_NbCommonBytes (register size_t val) r += (!val); return r; # endif - } - else /* 32 bits */ - { + } else /* 32 bits */ { # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) unsigned long r = 0; _BitScanReverse( &r, (unsigned long)val ); @@ -361,8 +346,7 @@ static unsigned LZ4_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* pInLi { const BYTE* const pStart = pIn; - while (likely(pIn compression run slower on incompressible data */ -/************************************** +/*-************************************ * Local Structures and types **************************************/ typedef struct { @@ -410,20 +394,17 @@ typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive; typedef enum { full = 0, partial = 1 } earlyEnd_directive; -/************************************** +/*-************************************ * Local Utils **************************************/ int LZ4_versionNumber (void) { return LZ4_VERSION_NUMBER; } -const char* LZ4_versionString (void) { return LZ4_VERSION_STRING; } int LZ4_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); } int LZ4_sizeofState() { return LZ4_STREAMSIZE; } - -/******************************** +/*-****************************** * Compression functions ********************************/ - static U32 LZ4_hashSequence(U32 sequence, tableType_t const tableType) { if (tableType == byU16) @@ -461,23 +442,26 @@ static void LZ4_putPositionOnHash(const BYTE* p, U32 h, void* tableBase, tableTy static void LZ4_putPosition(const BYTE* p, void* tableBase, tableType_t tableType, const BYTE* srcBase) { - U32 h = LZ4_hashPosition(p, tableType); + U32 const h = LZ4_hashPosition(p, tableType); LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase); } static const BYTE* LZ4_getPositionOnHash(U32 h, void* tableBase, tableType_t tableType, const BYTE* srcBase) { if (tableType == byPtr) { const BYTE** hashTable = (const BYTE**) tableBase; return hashTable[h]; } - if (tableType == byU32) { U32* hashTable = (U32*) tableBase; return hashTable[h] + srcBase; } - { U16* hashTable = (U16*) tableBase; return hashTable[h] + srcBase; } /* default, to ensure a return */ + if (tableType == byU32) { const U32* const hashTable = (U32*) tableBase; return hashTable[h] + srcBase; } + { const U16* const hashTable = (U16*) tableBase; return hashTable[h] + srcBase; } /* default, to ensure a return */ } static const BYTE* LZ4_getPosition(const BYTE* p, void* tableBase, tableType_t tableType, const BYTE* srcBase) { - U32 h = LZ4_hashPosition(p, tableType); + U32 const h = LZ4_hashPosition(p, tableType); return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase); } + +/** LZ4_compress_generic() : + inlined, to ensure branches are decided at compilation time */ FORCE_INLINE int LZ4_compress_generic( void* const ctx, const char* const source, @@ -536,18 +520,16 @@ FORCE_INLINE int LZ4_compress_generic( ip++; forwardH = LZ4_hashPosition(ip, tableType); /* Main Loop */ - for ( ; ; ) - { + for ( ; ; ) { const BYTE* match; BYTE* token; - { - const BYTE* forwardIp = ip; + + /* Find a match */ + { const BYTE* forwardIp = ip; unsigned step = 1; unsigned searchMatchNb = acceleration << LZ4_skipTrigger; - - /* Find a match */ do { - U32 h = forwardH; + U32 const h = forwardH; ip = forwardIp; forwardIp += step; step = (searchMatchNb++ >> LZ4_skipTrigger); @@ -555,19 +537,14 @@ FORCE_INLINE int LZ4_compress_generic( if (unlikely(forwardIp > mflimit)) goto _last_literals; match = LZ4_getPositionOnHash(h, ctx, tableType, base); - if (dict==usingExtDict) - { - if (match<(const BYTE*)source) - { + if (dict==usingExtDict) { + if (match < (const BYTE*)source) { refDelta = dictDelta; lowLimit = dictionary; - } - else - { + } else { refDelta = 0; lowLimit = (const BYTE*)source; - } - } + } } forwardH = LZ4_hashPosition(forwardIp, tableType); LZ4_putPositionOnHash(ip, h, ctx, tableType, base); @@ -577,16 +554,14 @@ FORCE_INLINE int LZ4_compress_generic( } /* Catch up */ - while ((ip>anchor) && (match+refDelta > lowLimit) && (unlikely(ip[-1]==match[refDelta-1]))) { ip--; match--; } + while (((ip>anchor) & (match+refDelta > lowLimit)) && (unlikely(ip[-1]==match[refDelta-1]))) { ip--; match--; } - { - /* Encode Literal length */ - unsigned litLength = (unsigned)(ip - anchor); + /* Encode Literals */ + { unsigned const litLength = (unsigned)(ip - anchor); token = op++; if ((outputLimited) && (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit))) return 0; /* Check output limit */ - if (litLength>=RUN_MASK) - { + if (litLength>=RUN_MASK) { int len = (int)litLength-RUN_MASK; *token=(RUN_MASK<= 255 ; len-=255) *op++ = 255; @@ -604,41 +579,35 @@ _next_match: LZ4_writeLE16(op, (U16)(ip-match)); op+=2; /* Encode MatchLength */ - { - unsigned matchLength; + { unsigned matchCode; - if ((dict==usingExtDict) && (lowLimit==dictionary)) - { + if ((dict==usingExtDict) && (lowLimit==dictionary)) { const BYTE* limit; match += refDelta; limit = ip + (dictEnd-match); if (limit > matchlimit) limit = matchlimit; - matchLength = LZ4_count(ip+MINMATCH, match+MINMATCH, limit); - ip += MINMATCH + matchLength; - if (ip==limit) - { - unsigned more = LZ4_count(ip, (const BYTE*)source, matchlimit); - matchLength += more; + matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, limit); + ip += MINMATCH + matchCode; + if (ip==limit) { + unsigned const more = LZ4_count(ip, (const BYTE*)source, matchlimit); + matchCode += more; ip += more; } - } - else - { - matchLength = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit); - ip += MINMATCH + matchLength; + } else { + matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit); + ip += MINMATCH + matchCode; } - if ((outputLimited) && (unlikely(op + (1 + LASTLITERALS) + (matchLength>>8) > olimit))) + if ((outputLimited) && (unlikely(op + (1 + LASTLITERALS) + (matchCode>>8) > olimit))) return 0; /* Check output limit */ - if (matchLength>=ML_MASK) - { + if (matchCode>=ML_MASK) { *token += ML_MASK; - matchLength -= ML_MASK; - for (; matchLength >= 510 ; matchLength-=510) { *op++ = 255; *op++ = 255; } - if (matchLength >= 255) { matchLength-=255; *op++ = 255; } - *op++ = (BYTE)matchLength; - } - else *token += (BYTE)(matchLength); + matchCode -= ML_MASK; + for (; matchCode >= 510 ; matchCode-=510) { *op++ = 255; *op++ = 255; } + if (matchCode >= 255) { matchCode-=255; *op++ = 255; } + *op++ = (BYTE)matchCode; + } else + *token += (BYTE)(matchCode); } anchor = ip; @@ -651,19 +620,14 @@ _next_match: /* Test next position */ match = LZ4_getPosition(ip, ctx, tableType, base); - if (dict==usingExtDict) - { - if (match<(const BYTE*)source) - { + if (dict==usingExtDict) { + if (match < (const BYTE*)source) { refDelta = dictDelta; lowLimit = dictionary; - } - else - { + } else { refDelta = 0; lowLimit = (const BYTE*)source; - } - } + } } LZ4_putPosition(ip, ctx, tableType, base); if ( ((dictIssue==dictSmall) ? (match>=lowRefLimit) : 1) && (match+MAX_DISTANCE>=ip) @@ -676,19 +640,16 @@ _next_match: _last_literals: /* Encode Last Literals */ - { - const size_t lastRun = (size_t)(iend - anchor); - if ((outputLimited) && ((op - (BYTE*)dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize)) - return 0; /* Check output limit */ - if (lastRun >= RUN_MASK) - { + { const size_t lastRun = (size_t)(iend - anchor); + if ( (outputLimited) && /* Check output buffer overflow */ + ((op - (BYTE*)dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize) ) + return 0; + if (lastRun >= RUN_MASK) { size_t accumulator = lastRun - RUN_MASK; *op++ = RUN_MASK << ML_BITS; for(; accumulator >= 255 ; accumulator-=255) *op++ = 255; *op++ = (BYTE) accumulator; - } - else - { + } else { *op++ = (BYTE)(lastRun<= LZ4_compressBound(inputSize)) - { + if (maxOutputSize >= LZ4_compressBound(inputSize)) { if (inputSize < LZ4_64Klimit) return LZ4_compress_generic(state, source, dest, inputSize, 0, notLimited, byU16, noDict, noDictIssue, acceleration); else return LZ4_compress_generic(state, source, dest, inputSize, 0, notLimited, LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue, acceleration); - } - else - { + } else { if (inputSize < LZ4_64Klimit) return LZ4_compress_generic(state, source, dest, inputSize, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration); else @@ -730,13 +688,8 @@ int LZ4_compress_fast(const char* source, char* dest, int inputSize, int maxOutp LZ4_stream_t ctx; void* ctxPtr = &ctx; #endif - int result; -#if (HEAPMODE) - if (!ctxPtr) { return 0; } -#endif - - result = LZ4_compress_fast_extState(ctxPtr, source, dest, inputSize, maxOutputSize, acceleration); + int result = LZ4_compress_fast_extState(ctxPtr, source, dest, inputSize, maxOutputSize, acceleration); #if (HEAPMODE) FREEMEM(ctxPtr); @@ -756,7 +709,6 @@ int LZ4_compress_default(const char* source, char* dest, int inputSize, int maxO int LZ4_compress_fast_force(const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration) { LZ4_stream_t ctx; - LZ4_resetStream(&ctx); if (inputSize < LZ4_64Klimit) @@ -766,8 +718,8 @@ int LZ4_compress_fast_force(const char* source, char* dest, int inputSize, int m } -/******************************** -* destSize variant +/*-****************************** +* *_destSize() variant ********************************/ static int LZ4_compress_destSize_generic( @@ -967,13 +919,8 @@ int LZ4_compress_destSize(const char* src, char* dst, int* srcSizePtr, int targe LZ4_stream_t ctxBody; void* ctx = &ctxBody; #endif - int result; -#if (HEAPMODE) - if (!ctx) { return 0; } -#endif - - result = LZ4_compress_destSize_extState(ctx, src, dst, srcSizePtr, targetDstSize); + int result = LZ4_compress_destSize_extState(ctx, src, dst, srcSizePtr, targetDstSize); #if (HEAPMODE) FREEMEM(ctx); @@ -990,7 +937,6 @@ int LZ4_compress_destSize(const char* src, char* dst, int* srcSizePtr, int targe LZ4_stream_t* LZ4_createStream(void) { LZ4_stream_t* lz4s = (LZ4_stream_t*)ALLOCATOR(8, LZ4_STREAMSIZE_U64); - if (!lz4s) { return NULL; } LZ4_STATIC_ASSERT(LZ4_STREAMSIZE >= sizeof(LZ4_stream_t_internal)); /* A compilation error here means LZ4_STREAMSIZE is not large enough */ LZ4_resetStream(lz4s); return lz4s; @@ -1194,7 +1140,6 @@ FORCE_INLINE int LZ4_decompress_generic( const int safeDecode = (endOnInput==endOnInputSize); const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB))); - const int inPlaceDecode = ((ip >= op) && (ip < oend)); /* Special cases */ @@ -1211,8 +1156,6 @@ FORCE_INLINE int LZ4_decompress_generic( const BYTE* match; size_t offset; - if (unlikely((inPlaceDecode) && (op + WILDCOPYLENGTH > ip))) goto _output_error; /* output stream ran over input stream */ - /* get literal length */ token = *ip++; if ((length=(token>>ML_BITS)) == RUN_MASK) @@ -1243,7 +1186,7 @@ FORCE_INLINE int LZ4_decompress_generic( if ((!endOnInput) && (cpy != oend)) goto _output_error; /* Error : block decoding must stop exactly there */ if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) goto _output_error; /* Error : input must be consumed */ } - memmove(op, ip, length); + memcpy(op, ip, length); ip += length; op += length; break; /* Necessarily EOF, due to parsing restrictions */ @@ -1552,7 +1495,6 @@ int LZ4_resetStreamState(void* state, char* inputBuffer) void* LZ4_create (char* inputBuffer) { void* lz4ds = ALLOCATOR(8, LZ4_STREAMSIZE_U64); - if (!lz4ds) { return NULL; } LZ4_init ((LZ4_stream_t_internal*)lz4ds, (BYTE*)inputBuffer); return lz4ds; } diff --git a/programs/.gitignore b/programs/.gitignore index f7fc991..bba3110 100644 --- a/programs/.gitignore +++ b/programs/.gitignore @@ -1,2 +1,4 @@ -/lz4 -/*.exe +# local binary (Makefile) +lz4 +lz4c32 +*.exe diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 2d491df..b5b244e 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -59,7 +59,6 @@ #include /* exit, calloc, free */ #include /* strcmp, strlen */ #include "bench.h" /* BMK_benchFile, BMK_SetNbIterations, BMK_SetBlocksize, BMK_SetPause */ -#include "lz4.h" /* LZ4_versionString */ #include "lz4io.h" /* LZ4IO_compressFilename, LZ4IO_decompressFilename, LZ4IO_compressMultipleFilenames */ @@ -86,14 +85,12 @@ /***************************** * Constants ******************************/ -#define COMPRESSOR_NAME "LZ4 CLI" +#define COMPRESSOR_NAME "LZ4 command line interface" #ifndef LZ4_VERSION # define LZ4_VERSION "r128" #endif #define AUTHOR "Yann Collet" -#define WELCOME_MESSAGE "*** %s %i-bits %s (lib %s), by %s ***\n", \ - COMPRESSOR_NAME, (int)(sizeof(void*)*8), LZ4_VERSION, \ - LZ4_versionString(), AUTHOR +#define WELCOME_MESSAGE "*** %s %i-bits %s, by %s (%s) ***\n", COMPRESSOR_NAME, (int)(sizeof(void*)*8), LZ4_VERSION, AUTHOR, __DATE__ #define LZ4_EXTENSION ".lz4" #define LZ4CAT "lz4cat" #define UNLZ4 "unlz4" @@ -105,7 +102,7 @@ #define LZ4_BLOCKSIZEID_DEFAULT 7 -/************************************** +/*-************************************ * Macros ***************************************/ #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) @@ -113,13 +110,13 @@ static unsigned displayLevel = 2; /* 0 : no display ; 1: errors only ; 2 : downgradable normal ; 3 : non-downgradable normal; 4 : + information */ -/************************************** +/*-************************************ * Local Variables ***************************************/ -static char* programName; +static const char* programName; -/************************************** +/*-************************************ * Exceptions ***************************************/ #define DEBUG 0 @@ -134,7 +131,7 @@ static char* programName; } -/************************************** +/*-************************************ * Version modifiers ***************************************/ #define EXTENDED_ARGUMENTS @@ -145,7 +142,7 @@ static char* programName; int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output_filename, int compressionlevel); /* hidden function */ -/***************************** +/*-*************************** * Functions *****************************/ static int usage(void) @@ -274,7 +271,7 @@ static void waitEnter(void) } -int main(int argc, char** argv) +int main(int argc, const char** argv) { int i, cLevel=0, @@ -305,9 +302,8 @@ int main(int argc, char** argv) if (!strcmp(programName, UNLZ4)) { decode=1; } /* command switches */ - for(i=1; i='0') && (*argument<='9')) - { + if ((*argument>='0') && (*argument<='9')) { cLevel = 0; - while ((*argument >= '0') && (*argument <= '9')) - { + while ((*argument >= '0') && (*argument <= '9')) { cLevel *= 10; cLevel += *argument - '0'; argument++; @@ -413,13 +404,12 @@ int main(int argc, char** argv) case '5': case '6': case '7': - { - int B = argument[1] - '0'; - blockSize = LZ4IO_setBlockSizeID(B); - BMK_setBlocksize(blockSize); - argument++; - break; - } + { int B = argument[1] - '0'; + blockSize = LZ4IO_setBlockSizeID(B); + BMK_setBlocksize(blockSize); + argument++; + break; + } case 'D': LZ4IO_setBlockMode(LZ4IO_blockLinked); argument++; break; case 'X': LZ4IO_setBlockChecksumMode(1); argument ++; break; /* currently disabled */ default : exitBlockProperties=1; @@ -442,8 +432,7 @@ int main(int argc, char** argv) /* Modify Nb Iterations (benchmark only) */ case 'i': - { - unsigned iters = 0; + { unsigned iters = 0; while ((argument[1] >='0') && (argument[1] <='9')) { iters *= 10; @@ -513,12 +502,12 @@ int main(int argc, char** argv) if (!IS_CONSOLE(stdout)) { output_filename=stdoutmark; break; } /* Default to stdout whenever possible (i.e. not a console) */ if ((!decode) && !(forceCompress)) /* auto-determine compression or decompression, based on file extension */ { - size_t l = strlen(input_filename); + size_t const l = strlen(input_filename); if (!strcmp(input_filename+(l-4), LZ4_EXTENSION)) decode=1; } if (!decode) /* compression to file */ { - size_t l = strlen(input_filename); + size_t const l = strlen(input_filename); dynNameSpace = (char*)calloc(1,l+5); if (dynNameSpace==NULL) exit(1); strcpy(dynNameSpace, input_filename); @@ -543,8 +532,7 @@ int main(int argc, char** argv) } /* Check if output is defined as console; trigger an error in this case */ - if (!strcmp(output_filename,stdoutmark) && IS_CONSOLE(stdout) && !forceStdout) - { + if (!strcmp(output_filename,stdoutmark) && IS_CONSOLE(stdout) && !forceStdout) { DISPLAYLEVEL(1, "refusing to write to console without -c\n"); exit(1); } @@ -553,26 +541,19 @@ int main(int argc, char** argv) if (!strcmp(input_filename, stdinmark) && !strcmp(output_filename,stdoutmark) && (displayLevel==2)) displayLevel=1; if ((multiple_inputs) && (displayLevel==2)) displayLevel=1; - /* IO Stream/File */ LZ4IO_setNotificationLevel(displayLevel); - if (decode) - { + if (decode) { if (multiple_inputs) operationResult = LZ4IO_decompressMultipleFilenames(inFileNames, ifnIdx, LZ4_EXTENSION); else DEFAULT_DECOMPRESSOR(input_filename, output_filename); - } - else - { + } else { /* compression is default action */ - if (legacy_format) - { + if (legacy_format) { DISPLAYLEVEL(3, "! Generating compressed LZ4 using Legacy format (deprecated) ! \n"); LZ4IO_compressFilename_Legacy(input_filename, output_filename, cLevel); - } - else - { + } else { if (multiple_inputs) operationResult = LZ4IO_compressMultipleFilenames(inFileNames, ifnIdx, LZ4_EXTENSION, cLevel); else -- 2.7.4