minor refactoring (coding style)

This commit is contained in:
Yann Collet 2016-06-29 12:54:23 +02:00
parent 1f47f3f747
commit 3c03326004
3 changed files with 116 additions and 191 deletions

222
lib/lz4.c
View File

@ -33,7 +33,7 @@
*/ */
/************************************** /*-************************************
* Tuning parameters * Tuning parameters
**************************************/ **************************************/
/* /*
@ -50,7 +50,7 @@
#define ACCELERATION_DEFAULT 1 #define ACCELERATION_DEFAULT 1
/************************************** /*-************************************
* CPU Feature Detection * CPU Feature Detection
**************************************/ **************************************/
/* LZ4_FORCE_MEMORY_ACCESS /* LZ4_FORCE_MEMORY_ACCESS
@ -84,13 +84,13 @@
#endif #endif
/************************************** /*-************************************
* Includes * Includes
**************************************/ **************************************/
#include "lz4.h" #include "lz4.h"
/************************************** /*-************************************
* Compiler Options * Compiler Options
**************************************/ **************************************/
#ifdef _MSC_VER /* Visual Studio */ #ifdef _MSC_VER /* Visual Studio */
@ -121,7 +121,7 @@
#define unlikely(expr) expect((expr) != 0, 0) #define unlikely(expr) expect((expr) != 0, 0)
/************************************** /*-************************************
* Memory routines * Memory routines
**************************************/ **************************************/
#include <stdlib.h> /* malloc, calloc, free */ #include <stdlib.h> /* malloc, calloc, free */
@ -131,7 +131,7 @@
#define MEM_INIT memset #define MEM_INIT memset
/************************************** /*-************************************
* Basic Types * Basic Types
**************************************/ **************************************/
#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */
@ -150,7 +150,7 @@
#endif #endif
/************************************** /*-************************************
* Reading and writing into memory * Reading and writing into memory
**************************************/ **************************************/
#define STEPSIZE sizeof(size_t) #define STEPSIZE sizeof(size_t)
@ -159,7 +159,7 @@ static unsigned LZ4_64bits(void) { return sizeof(void*)==8; }
static unsigned LZ4_isLittleEndian(void) 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]; return one.c[0];
} }
@ -206,17 +206,14 @@ static void LZ4_write16(void* memPtr, U16 value)
memcpy(memPtr, &value, sizeof(value)); memcpy(memPtr, &value, sizeof(value));
} }
#endif // LZ4_FORCE_MEMORY_ACCESS #endif /* LZ4_FORCE_MEMORY_ACCESS */
static U16 LZ4_readLE16(const void* memPtr) static U16 LZ4_readLE16(const void* memPtr)
{ {
if (LZ4_isLittleEndian()) if (LZ4_isLittleEndian()) {
{
return LZ4_read16(memPtr); return LZ4_read16(memPtr);
} } else {
else
{
const BYTE* p = (const BYTE*)memPtr; const BYTE* p = (const BYTE*)memPtr;
return (U16)((U16)p[0] + (p[1]<<8)); 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) static void LZ4_writeLE16(void* memPtr, U16 value)
{ {
if (LZ4_isLittleEndian()) if (LZ4_isLittleEndian()) {
{
LZ4_write16(memPtr, value); LZ4_write16(memPtr, value);
} } else {
else
{
BYTE* p = (BYTE*)memPtr; BYTE* p = (BYTE*)memPtr;
p[0] = (BYTE) value; p[0] = (BYTE) value;
p[1] = (BYTE)(value>>8); p[1] = (BYTE)(value>>8);
@ -258,7 +252,7 @@ static void LZ4_wildCopy(void* dstPtr, const void* srcPtr, void* dstEnd)
} }
/************************************** /*-************************************
* Common Constants * Common Constants
**************************************/ **************************************/
#define MINMATCH 4 #define MINMATCH 4
@ -281,21 +275,19 @@ static const int LZ4_minLength = (MFLIMIT+1);
#define RUN_MASK ((1U<<RUN_BITS)-1) #define RUN_MASK ((1U<<RUN_BITS)-1)
/************************************** /*-************************************
* Common Utils * Common Utils
**************************************/ **************************************/
#define LZ4_STATIC_ASSERT(c) { enum { LZ4_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */ #define LZ4_STATIC_ASSERT(c) { enum { LZ4_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
/************************************** /*-************************************
* Common functions * Common functions
**************************************/ **************************************/
static unsigned LZ4_NbCommonBytes (register size_t val) static unsigned LZ4_NbCommonBytes (register size_t val)
{ {
if (LZ4_isLittleEndian()) if (LZ4_isLittleEndian()) {
{ if (LZ4_64bits()) {
if (LZ4_64bits())
{
# if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT) # if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
unsigned long r = 0; unsigned long r = 0;
_BitScanForward64( &r, (U64)val ); _BitScanForward64( &r, (U64)val );
@ -306,9 +298,7 @@ static unsigned LZ4_NbCommonBytes (register size_t val)
static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 }; static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 };
return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58]; return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58];
# endif # endif
} } else /* 32 bits */ {
else /* 32 bits */
{
# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
unsigned long r; unsigned long r;
_BitScanForward( &r, (U32)val ); _BitScanForward( &r, (U32)val );
@ -320,11 +310,8 @@ static unsigned LZ4_NbCommonBytes (register size_t val)
return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27]; return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
# endif # endif
} }
} } else /* Big Endian CPU */ {
else /* Big Endian CPU */ if (LZ4_64bits()) {
{
if (LZ4_64bits())
{
# if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT) # if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
unsigned long r = 0; unsigned long r = 0;
_BitScanReverse64( &r, val ); _BitScanReverse64( &r, val );
@ -338,9 +325,7 @@ static unsigned LZ4_NbCommonBytes (register size_t val)
r += (!val); r += (!val);
return r; return r;
# endif # endif
} } else /* 32 bits */ {
else /* 32 bits */
{
# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
unsigned long r = 0; unsigned long r = 0;
_BitScanReverse( &r, (unsigned long)val ); _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; const BYTE* const pStart = pIn;
while (likely(pIn<pInLimit-(STEPSIZE-1))) while (likely(pIn<pInLimit-(STEPSIZE-1))) {
{
size_t diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn); size_t diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
if (!diff) { pIn+=STEPSIZE; pMatch+=STEPSIZE; continue; } if (!diff) { pIn+=STEPSIZE; pMatch+=STEPSIZE; continue; }
pIn += LZ4_NbCommonBytes(diff); pIn += LZ4_NbCommonBytes(diff);
@ -377,7 +361,7 @@ static unsigned LZ4_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* pInLi
#ifndef LZ4_COMMONDEFS_ONLY #ifndef LZ4_COMMONDEFS_ONLY
/************************************** /*-************************************
* Local Constants * Local Constants
**************************************/ **************************************/
#define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2) #define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2)
@ -388,7 +372,7 @@ static const int LZ4_64Klimit = ((64 KB) + (MFLIMIT-1));
static const U32 LZ4_skipTrigger = 6; /* Increase this value ==> compression run slower on incompressible data */ static const U32 LZ4_skipTrigger = 6; /* Increase this value ==> compression run slower on incompressible data */
/************************************** /*-************************************
* Local Structures and types * Local Structures and types
**************************************/ **************************************/
typedef struct { typedef struct {
@ -410,20 +394,17 @@ typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive;
typedef enum { full = 0, partial = 1 } earlyEnd_directive; typedef enum { full = 0, partial = 1 } earlyEnd_directive;
/************************************** /*-************************************
* Local Utils * Local Utils
**************************************/ **************************************/
int LZ4_versionNumber (void) { return LZ4_VERSION_NUMBER; } 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_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); }
int LZ4_sizeofState() { return LZ4_STREAMSIZE; } int LZ4_sizeofState() { return LZ4_STREAMSIZE; }
/*-******************************
/********************************
* Compression functions * Compression functions
********************************/ ********************************/
static U32 LZ4_hashSequence(U32 sequence, tableType_t const tableType) static U32 LZ4_hashSequence(U32 sequence, tableType_t const tableType)
{ {
if (tableType == byU16) 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) 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); LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase);
} }
static const BYTE* LZ4_getPositionOnHash(U32 h, void* tableBase, tableType_t tableType, const BYTE* 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 == byPtr) { const BYTE** hashTable = (const BYTE**) tableBase; return hashTable[h]; }
if (tableType == byU32) { U32* hashTable = (U32*) tableBase; return hashTable[h] + srcBase; } if (tableType == byU32) { const U32* const hashTable = (U32*) tableBase; return hashTable[h] + srcBase; }
{ U16* hashTable = (U16*) tableBase; return hashTable[h] + srcBase; } /* default, to ensure a return */ { 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) 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); 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( FORCE_INLINE int LZ4_compress_generic(
void* const ctx, void* const ctx,
const char* const source, const char* const source,
@ -536,18 +520,16 @@ FORCE_INLINE int LZ4_compress_generic(
ip++; forwardH = LZ4_hashPosition(ip, tableType); ip++; forwardH = LZ4_hashPosition(ip, tableType);
/* Main Loop */ /* Main Loop */
for ( ; ; ) for ( ; ; ) {
{
const BYTE* match; const BYTE* match;
BYTE* token; BYTE* token;
{
const BYTE* forwardIp = ip;
unsigned step = 1;
unsigned searchMatchNb = acceleration << LZ4_skipTrigger;
/* Find a match */ /* Find a match */
{ const BYTE* forwardIp = ip;
unsigned step = 1;
unsigned searchMatchNb = acceleration << LZ4_skipTrigger;
do { do {
U32 h = forwardH; U32 const h = forwardH;
ip = forwardIp; ip = forwardIp;
forwardIp += step; forwardIp += step;
step = (searchMatchNb++ >> LZ4_skipTrigger); step = (searchMatchNb++ >> LZ4_skipTrigger);
@ -555,19 +537,14 @@ FORCE_INLINE int LZ4_compress_generic(
if (unlikely(forwardIp > mflimit)) goto _last_literals; if (unlikely(forwardIp > mflimit)) goto _last_literals;
match = LZ4_getPositionOnHash(h, ctx, tableType, base); match = LZ4_getPositionOnHash(h, ctx, tableType, base);
if (dict==usingExtDict) if (dict==usingExtDict) {
{ if (match < (const BYTE*)source) {
if (match<(const BYTE*)source)
{
refDelta = dictDelta; refDelta = dictDelta;
lowLimit = dictionary; lowLimit = dictionary;
} } else {
else
{
refDelta = 0; refDelta = 0;
lowLimit = (const BYTE*)source; lowLimit = (const BYTE*)source;
} } }
}
forwardH = LZ4_hashPosition(forwardIp, tableType); forwardH = LZ4_hashPosition(forwardIp, tableType);
LZ4_putPositionOnHash(ip, h, ctx, tableType, base); LZ4_putPositionOnHash(ip, h, ctx, tableType, base);
@ -577,16 +554,14 @@ FORCE_INLINE int LZ4_compress_generic(
} }
/* Catch up */ /* 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 Literals */
/* Encode Literal length */ { unsigned const litLength = (unsigned)(ip - anchor);
unsigned litLength = (unsigned)(ip - anchor);
token = op++; token = op++;
if ((outputLimited) && (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit))) if ((outputLimited) && (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit)))
return 0; /* Check output limit */ return 0; /* Check output limit */
if (litLength>=RUN_MASK) if (litLength>=RUN_MASK) {
{
int len = (int)litLength-RUN_MASK; int len = (int)litLength-RUN_MASK;
*token=(RUN_MASK<<ML_BITS); *token=(RUN_MASK<<ML_BITS);
for(; len >= 255 ; len-=255) *op++ = 255; for(; len >= 255 ; len-=255) *op++ = 255;
@ -604,41 +579,35 @@ _next_match:
LZ4_writeLE16(op, (U16)(ip-match)); op+=2; LZ4_writeLE16(op, (U16)(ip-match)); op+=2;
/* Encode MatchLength */ /* Encode MatchLength */
{ { unsigned matchCode;
unsigned matchLength;
if ((dict==usingExtDict) && (lowLimit==dictionary)) if ((dict==usingExtDict) && (lowLimit==dictionary)) {
{
const BYTE* limit; const BYTE* limit;
match += refDelta; match += refDelta;
limit = ip + (dictEnd-match); limit = ip + (dictEnd-match);
if (limit > matchlimit) limit = matchlimit; if (limit > matchlimit) limit = matchlimit;
matchLength = LZ4_count(ip+MINMATCH, match+MINMATCH, limit); matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, limit);
ip += MINMATCH + matchLength; ip += MINMATCH + matchCode;
if (ip==limit) if (ip==limit) {
{ unsigned const more = LZ4_count(ip, (const BYTE*)source, matchlimit);
unsigned more = LZ4_count(ip, (const BYTE*)source, matchlimit); matchCode += more;
matchLength += more;
ip += more; ip += more;
} }
} } else {
else matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit);
{ ip += MINMATCH + matchCode;
matchLength = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit);
ip += MINMATCH + matchLength;
} }
if ((outputLimited) && (unlikely(op + (1 + LASTLITERALS) + (matchLength>>8) > olimit))) if ((outputLimited) && (unlikely(op + (1 + LASTLITERALS) + (matchCode>>8) > olimit)))
return 0; /* Check output limit */ return 0; /* Check output limit */
if (matchLength>=ML_MASK) if (matchCode>=ML_MASK) {
{
*token += ML_MASK; *token += ML_MASK;
matchLength -= ML_MASK; matchCode -= ML_MASK;
for (; matchLength >= 510 ; matchLength-=510) { *op++ = 255; *op++ = 255; } for (; matchCode >= 510 ; matchCode-=510) { *op++ = 255; *op++ = 255; }
if (matchLength >= 255) { matchLength-=255; *op++ = 255; } if (matchCode >= 255) { matchCode-=255; *op++ = 255; }
*op++ = (BYTE)matchLength; *op++ = (BYTE)matchCode;
} } else
else *token += (BYTE)(matchLength); *token += (BYTE)(matchCode);
} }
anchor = ip; anchor = ip;
@ -651,19 +620,14 @@ _next_match:
/* Test next position */ /* Test next position */
match = LZ4_getPosition(ip, ctx, tableType, base); match = LZ4_getPosition(ip, ctx, tableType, base);
if (dict==usingExtDict) if (dict==usingExtDict) {
{ if (match < (const BYTE*)source) {
if (match<(const BYTE*)source)
{
refDelta = dictDelta; refDelta = dictDelta;
lowLimit = dictionary; lowLimit = dictionary;
} } else {
else
{
refDelta = 0; refDelta = 0;
lowLimit = (const BYTE*)source; lowLimit = (const BYTE*)source;
} } }
}
LZ4_putPosition(ip, ctx, tableType, base); LZ4_putPosition(ip, ctx, tableType, base);
if ( ((dictIssue==dictSmall) ? (match>=lowRefLimit) : 1) if ( ((dictIssue==dictSmall) ? (match>=lowRefLimit) : 1)
&& (match+MAX_DISTANCE>=ip) && (match+MAX_DISTANCE>=ip)
@ -676,19 +640,16 @@ _next_match:
_last_literals: _last_literals:
/* Encode Last Literals */ /* Encode Last Literals */
{ { const size_t lastRun = (size_t)(iend - anchor);
const size_t lastRun = (size_t)(iend - anchor); if ( (outputLimited) && /* Check output buffer overflow */
if ((outputLimited) && ((op - (BYTE*)dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize)) ((op - (BYTE*)dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize) )
return 0; /* Check output limit */ return 0;
if (lastRun >= RUN_MASK) if (lastRun >= RUN_MASK) {
{
size_t accumulator = lastRun - RUN_MASK; size_t accumulator = lastRun - RUN_MASK;
*op++ = RUN_MASK << ML_BITS; *op++ = RUN_MASK << ML_BITS;
for(; accumulator >= 255 ; accumulator-=255) *op++ = 255; for(; accumulator >= 255 ; accumulator-=255) *op++ = 255;
*op++ = (BYTE) accumulator; *op++ = (BYTE) accumulator;
} } else {
else
{
*op++ = (BYTE)(lastRun<<ML_BITS); *op++ = (BYTE)(lastRun<<ML_BITS);
} }
memcpy(op, anchor, lastRun); memcpy(op, anchor, lastRun);
@ -705,15 +666,12 @@ int LZ4_compress_fast_extState(void* state, const char* source, char* dest, int
LZ4_resetStream((LZ4_stream_t*)state); LZ4_resetStream((LZ4_stream_t*)state);
if (acceleration < 1) acceleration = ACCELERATION_DEFAULT; if (acceleration < 1) acceleration = ACCELERATION_DEFAULT;
if (maxOutputSize >= LZ4_compressBound(inputSize)) if (maxOutputSize >= LZ4_compressBound(inputSize)) {
{
if (inputSize < LZ4_64Klimit) if (inputSize < LZ4_64Klimit)
return LZ4_compress_generic(state, source, dest, inputSize, 0, notLimited, byU16, noDict, noDictIssue, acceleration); return LZ4_compress_generic(state, source, dest, inputSize, 0, notLimited, byU16, noDict, noDictIssue, acceleration);
else else
return LZ4_compress_generic(state, source, dest, inputSize, 0, notLimited, LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue, acceleration); return LZ4_compress_generic(state, source, dest, inputSize, 0, notLimited, LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue, acceleration);
} } else {
else
{
if (inputSize < LZ4_64Klimit) if (inputSize < LZ4_64Klimit)
return LZ4_compress_generic(state, source, dest, inputSize, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration); return LZ4_compress_generic(state, source, dest, inputSize, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration);
else else
@ -730,13 +688,8 @@ int LZ4_compress_fast(const char* source, char* dest, int inputSize, int maxOutp
LZ4_stream_t ctx; LZ4_stream_t ctx;
void* ctxPtr = &ctx; void* ctxPtr = &ctx;
#endif #endif
int result;
#if (HEAPMODE) int result = LZ4_compress_fast_extState(ctxPtr, source, dest, inputSize, maxOutputSize, acceleration);
if (!ctxPtr) { return 0; }
#endif
result = LZ4_compress_fast_extState(ctxPtr, source, dest, inputSize, maxOutputSize, acceleration);
#if (HEAPMODE) #if (HEAPMODE)
FREEMEM(ctxPtr); 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) int LZ4_compress_fast_force(const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration)
{ {
LZ4_stream_t ctx; LZ4_stream_t ctx;
LZ4_resetStream(&ctx); LZ4_resetStream(&ctx);
if (inputSize < LZ4_64Klimit) 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( 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; LZ4_stream_t ctxBody;
void* ctx = &ctxBody; void* ctx = &ctxBody;
#endif #endif
int result;
#if (HEAPMODE) int result = LZ4_compress_destSize_extState(ctx, src, dst, srcSizePtr, targetDstSize);
if (!ctx) { return 0; }
#endif
result = LZ4_compress_destSize_extState(ctx, src, dst, srcSizePtr, targetDstSize);
#if (HEAPMODE) #if (HEAPMODE)
FREEMEM(ctx); 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* LZ4_createStream(void)
{ {
LZ4_stream_t* lz4s = (LZ4_stream_t*)ALLOCATOR(8, LZ4_STREAMSIZE_U64); 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_STATIC_ASSERT(LZ4_STREAMSIZE >= sizeof(LZ4_stream_t_internal)); /* A compilation error here means LZ4_STREAMSIZE is not large enough */
LZ4_resetStream(lz4s); LZ4_resetStream(lz4s);
return lz4s; return lz4s;
@ -1194,7 +1140,6 @@ FORCE_INLINE int LZ4_decompress_generic(
const int safeDecode = (endOnInput==endOnInputSize); const int safeDecode = (endOnInput==endOnInputSize);
const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB))); const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB)));
const int inPlaceDecode = ((ip >= op) && (ip < oend));
/* Special cases */ /* Special cases */
@ -1211,8 +1156,6 @@ FORCE_INLINE int LZ4_decompress_generic(
const BYTE* match; const BYTE* match;
size_t offset; size_t offset;
if (unlikely((inPlaceDecode) && (op + WILDCOPYLENGTH > ip))) goto _output_error; /* output stream ran over input stream */
/* get literal length */ /* get literal length */
token = *ip++; token = *ip++;
if ((length=(token>>ML_BITS)) == RUN_MASK) 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) && (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 */ 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; ip += length;
op += length; op += length;
break; /* Necessarily EOF, due to parsing restrictions */ break; /* Necessarily EOF, due to parsing restrictions */
@ -1552,7 +1495,6 @@ int LZ4_resetStreamState(void* state, char* inputBuffer)
void* LZ4_create (char* inputBuffer) void* LZ4_create (char* inputBuffer)
{ {
void* lz4ds = ALLOCATOR(8, LZ4_STREAMSIZE_U64); void* lz4ds = ALLOCATOR(8, LZ4_STREAMSIZE_U64);
if (!lz4ds) { return NULL; }
LZ4_init ((LZ4_stream_t_internal*)lz4ds, (BYTE*)inputBuffer); LZ4_init ((LZ4_stream_t_internal*)lz4ds, (BYTE*)inputBuffer);
return lz4ds; return lz4ds;
} }

6
programs/.gitignore vendored
View File

@ -1,2 +1,4 @@
/lz4 # local binary (Makefile)
/*.exe lz4
lz4c32
*.exe

View File

@ -59,7 +59,6 @@
#include <stdlib.h> /* exit, calloc, free */ #include <stdlib.h> /* exit, calloc, free */
#include <string.h> /* strcmp, strlen */ #include <string.h> /* strcmp, strlen */
#include "bench.h" /* BMK_benchFile, BMK_SetNbIterations, BMK_SetBlocksize, BMK_SetPause */ #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 */ #include "lz4io.h" /* LZ4IO_compressFilename, LZ4IO_decompressFilename, LZ4IO_compressMultipleFilenames */
@ -86,14 +85,12 @@
/***************************** /*****************************
* Constants * Constants
******************************/ ******************************/
#define COMPRESSOR_NAME "LZ4 CLI" #define COMPRESSOR_NAME "LZ4 command line interface"
#ifndef LZ4_VERSION #ifndef LZ4_VERSION
# define LZ4_VERSION "r128" # define LZ4_VERSION "r128"
#endif #endif
#define AUTHOR "Yann Collet" #define AUTHOR "Yann Collet"
#define WELCOME_MESSAGE "*** %s %i-bits %s (lib %s), by %s ***\n", \ #define WELCOME_MESSAGE "*** %s %i-bits %s, by %s (%s) ***\n", COMPRESSOR_NAME, (int)(sizeof(void*)*8), LZ4_VERSION, AUTHOR, __DATE__
COMPRESSOR_NAME, (int)(sizeof(void*)*8), LZ4_VERSION, \
LZ4_versionString(), AUTHOR
#define LZ4_EXTENSION ".lz4" #define LZ4_EXTENSION ".lz4"
#define LZ4CAT "lz4cat" #define LZ4CAT "lz4cat"
#define UNLZ4 "unlz4" #define UNLZ4 "unlz4"
@ -105,7 +102,7 @@
#define LZ4_BLOCKSIZEID_DEFAULT 7 #define LZ4_BLOCKSIZEID_DEFAULT 7
/************************************** /*-************************************
* Macros * Macros
***************************************/ ***************************************/
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) #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 */ static unsigned displayLevel = 2; /* 0 : no display ; 1: errors only ; 2 : downgradable normal ; 3 : non-downgradable normal; 4 : + information */
/************************************** /*-************************************
* Local Variables * Local Variables
***************************************/ ***************************************/
static char* programName; static const char* programName;
/************************************** /*-************************************
* Exceptions * Exceptions
***************************************/ ***************************************/
#define DEBUG 0 #define DEBUG 0
@ -134,7 +131,7 @@ static char* programName;
} }
/************************************** /*-************************************
* Version modifiers * Version modifiers
***************************************/ ***************************************/
#define EXTENDED_ARGUMENTS #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 */ int LZ4IO_compressFilename_Legacy(const char* input_filename, const char* output_filename, int compressionlevel); /* hidden function */
/***************************** /*-***************************
* Functions * Functions
*****************************/ *****************************/
static int usage(void) 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, int i,
cLevel=0, cLevel=0,
@ -305,9 +302,8 @@ int main(int argc, char** argv)
if (!strcmp(programName, UNLZ4)) { decode=1; } if (!strcmp(programName, UNLZ4)) { decode=1; }
/* command switches */ /* command switches */
for(i=1; i<argc; i++) for(i=1; i<argc; i++) {
{ const char* argument = argv[i];
char* argument = argv[i];
if(!argument) continue; /* Protection if argument empty */ if(!argument) continue; /* Protection if argument empty */
@ -334,17 +330,14 @@ int main(int argc, char** argv)
/* Short commands (note : aggregated short commands are allowed) */ /* Short commands (note : aggregated short commands are allowed) */
if (argument[0]=='-') if (argument[0]=='-') {
{
/* '-' means stdin/stdout */ /* '-' means stdin/stdout */
if (argument[1]==0) if (argument[1]==0) {
{
if (!input_filename) input_filename=stdinmark; if (!input_filename) input_filename=stdinmark;
else output_filename=stdoutmark; else output_filename=stdoutmark;
} }
while (argument[1]!=0) while (argument[1]!=0) {
{
argument ++; argument ++;
#if defined(ENABLE_LZ4C_LEGACY_OPTIONS) #if defined(ENABLE_LZ4C_LEGACY_OPTIONS)
@ -355,11 +348,9 @@ int main(int argc, char** argv)
if (*argument=='y') { LZ4IO_setOverwrite(1); continue; } /* -y (answer 'yes' to overwrite permission) */ if (*argument=='y') { LZ4IO_setOverwrite(1); continue; } /* -y (answer 'yes' to overwrite permission) */
#endif /* ENABLE_LZ4C_LEGACY_OPTIONS */ #endif /* ENABLE_LZ4C_LEGACY_OPTIONS */
if ((*argument>='0') && (*argument<='9')) if ((*argument>='0') && (*argument<='9')) {
{
cLevel = 0; cLevel = 0;
while ((*argument >= '0') && (*argument <= '9')) while ((*argument >= '0') && (*argument <= '9')) {
{
cLevel *= 10; cLevel *= 10;
cLevel += *argument - '0'; cLevel += *argument - '0';
argument++; argument++;
@ -413,8 +404,7 @@ int main(int argc, char** argv)
case '5': case '5':
case '6': case '6':
case '7': case '7':
{ { int B = argument[1] - '0';
int B = argument[1] - '0';
blockSize = LZ4IO_setBlockSizeID(B); blockSize = LZ4IO_setBlockSizeID(B);
BMK_setBlocksize(blockSize); BMK_setBlocksize(blockSize);
argument++; argument++;
@ -442,8 +432,7 @@ int main(int argc, char** argv)
/* Modify Nb Iterations (benchmark only) */ /* Modify Nb Iterations (benchmark only) */
case 'i': case 'i':
{ { unsigned iters = 0;
unsigned iters = 0;
while ((argument[1] >='0') && (argument[1] <='9')) while ((argument[1] >='0') && (argument[1] <='9'))
{ {
iters *= 10; 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 (!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 */ 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 (!strcmp(input_filename+(l-4), LZ4_EXTENSION)) decode=1;
} }
if (!decode) /* compression to file */ if (!decode) /* compression to file */
{ {
size_t l = strlen(input_filename); size_t const l = strlen(input_filename);
dynNameSpace = (char*)calloc(1,l+5); dynNameSpace = (char*)calloc(1,l+5);
if (dynNameSpace==NULL) exit(1); if (dynNameSpace==NULL) exit(1);
strcpy(dynNameSpace, input_filename); 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 */ /* 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"); DISPLAYLEVEL(1, "refusing to write to console without -c\n");
exit(1); 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 (!strcmp(input_filename, stdinmark) && !strcmp(output_filename,stdoutmark) && (displayLevel==2)) displayLevel=1;
if ((multiple_inputs) && (displayLevel==2)) displayLevel=1; if ((multiple_inputs) && (displayLevel==2)) displayLevel=1;
/* IO Stream/File */ /* IO Stream/File */
LZ4IO_setNotificationLevel(displayLevel); LZ4IO_setNotificationLevel(displayLevel);
if (decode) if (decode) {
{
if (multiple_inputs) if (multiple_inputs)
operationResult = LZ4IO_decompressMultipleFilenames(inFileNames, ifnIdx, LZ4_EXTENSION); operationResult = LZ4IO_decompressMultipleFilenames(inFileNames, ifnIdx, LZ4_EXTENSION);
else else
DEFAULT_DECOMPRESSOR(input_filename, output_filename); DEFAULT_DECOMPRESSOR(input_filename, output_filename);
} } else {
else
{
/* compression is default action */ /* compression is default action */
if (legacy_format) if (legacy_format) {
{
DISPLAYLEVEL(3, "! Generating compressed LZ4 using Legacy format (deprecated) ! \n"); DISPLAYLEVEL(3, "! Generating compressed LZ4 using Legacy format (deprecated) ! \n");
LZ4IO_compressFilename_Legacy(input_filename, output_filename, cLevel); LZ4IO_compressFilename_Legacy(input_filename, output_filename, cLevel);
} } else {
else
{
if (multiple_inputs) if (multiple_inputs)
operationResult = LZ4IO_compressMultipleFilenames(inFileNames, ifnIdx, LZ4_EXTENSION, cLevel); operationResult = LZ4IO_compressMultipleFilenames(inFileNames, ifnIdx, LZ4_EXTENSION, cLevel);
else else