Merge branch 'master' into allin

update
This commit is contained in:
tharexde 2020-10-03 23:34:23 +02:00
commit b0cfb28d40
232 changed files with 711543 additions and 328556 deletions

View file

@ -161,12 +161,12 @@ void htole24(uint32_t val, uint8_t data[3]) {
// ROL on u32
uint32_t rotl(uint32_t a, uint8_t n) {
n &= 31;
return (a << n) | (a >> (32 - n));
n &= 31;
return (a << n) | (a >> (32 - n));
}
// ROR on u32
uint32_t rotr(uint32_t a, uint8_t n) {
n &= 31;
return (a >> n) | (a << (32 - n));
n &= 31;
return (a >> n) | (a << (32 - n));
}

View file

@ -419,7 +419,7 @@ uint32_t lf_t55xx_white_pwdgen(uint32_t id) {
uint32_t r1 = rotl(id & 0x000000ec, 8);
uint32_t r2 = rotl(id & 0x86000000, 16);
uint32_t pwd = 0x10303;
pwd += ((id & 0x86ee00ec) ^ r1 ^ r2 );
pwd += ((id & 0x86ee00ec) ^ r1 ^ r2);
return pwd;
}
@ -483,7 +483,7 @@ int generator_selftest(void) {
if (success)
testresult++;
PrintAndLogEx(success ? SUCCESS : WARNING, "ID | 0x00000080 | %08"PRIx32 " - %s", lf_id, success ? "OK" : "->00018383<--");
PrintAndLogEx(SUCCESS, "------------------- Selftest %s", (testresult == NUM_OF_TEST) ? "OK" : "fail");
return PM3_SUCCESS;
}

View file

@ -2168,26 +2168,6 @@ int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32
return (int)start_idx;
}
// Find IDTEC PSK1, RF Preamble == 0x4944544B, Demodsize 64bits
// by iceman
int detectIdteck(uint8_t *dest, size_t *size) {
//make sure buffer has data
if (*size < 64 * 2) return -1;
if (signalprop.isnoise) return -2;
size_t start_idx = 0;
uint8_t preamble[] = {0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1};
//preamble not found
if (!preambleSearch(dest, preamble, sizeof(preamble), size, &start_idx))
return -3;
// wrong demoded size
if (*size != 64) return -4;
return (int)start_idx;
}
int detectIOProx(uint8_t *dest, size_t *size, int *waveStartIdx) {
//make sure buffer has data
if (*size < 66 * 64) return -1;

View file

@ -78,7 +78,6 @@ size_t removeParity(uint8_t *bits, size_t startIdx, uint8_t pLen, uint8_t pTyp
int detectAWID(uint8_t *dest, size_t *size, int *waveStartIdx);
int Em410xDecode(uint8_t *bits, size_t *size, size_t *start_idx, uint32_t *hi, uint64_t *lo);
int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo, int *waveStartIdx);
int detectIdteck(uint8_t *dest, size_t *size);
int detectIOProx(uint8_t *dest, size_t *size, int *waveStartIdx);
#endif

View file

@ -45,10 +45,16 @@
#endif
/*
* ACCELERATION_DEFAULT :
* LZ4_ACCELERATION_DEFAULT :
* Select "acceleration" for LZ4_compress_fast() when parameter value <= 0
*/
#define ACCELERATION_DEFAULT 1
#define LZ4_ACCELERATION_DEFAULT 1
/*
* LZ4_ACCELERATION_MAX :
* Any "acceleration" value higher than this threshold
* get treated as LZ4_ACCELERATION_MAX instead (fix #876)
*/
#define LZ4_ACCELERATION_MAX 65537
/*-************************************
@ -82,6 +88,7 @@
* Define this parameter if your target system or compiler does not support hardware bit count
*/
#if defined(_MSC_VER) && defined(_WIN32_WCE) /* Visual Studio for WinCE doesn't support Hardware bit count */
# undef LZ4_FORCE_SW_BITCOUNT /* avoid double def */
# define LZ4_FORCE_SW_BITCOUNT
#endif
@ -226,14 +233,14 @@ static const int LZ4_minLength = (MFLIMIT + 1);
#if defined(LZ4_DEBUG) && (LZ4_DEBUG>=2)
# include <stdio.h>
static int g_debuglog_enable = 1;
# define DEBUGLOG(l, ...) { \
if ((g_debuglog_enable) && (l<=LZ4_DEBUG)) { \
fprintf(stderr, __FILE__ ": "); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, " \n"); \
} }
# define DEBUGLOG(l, ...) { \
if ((g_debuglog_enable) && (l<=LZ4_DEBUG)) { \
fprintf(stderr, __FILE__ ": "); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, " \n"); \
} }
#else
# define DEBUGLOG(l, ...) {} /* disabled */
# define DEBUGLOG(l, ...) {} /* disabled */
#endif
@ -249,6 +256,10 @@ typedef int32_t S32;
typedef uint64_t U64;
typedef uintptr_t uptrval;
#else
# include <limits.h>
# if UINT_MAX != 4294967295UL
# error "LZ4 code (when not C++ or C99) assumes that sizeof(int) == 4"
# endif
typedef unsigned char BYTE;
typedef unsigned short U16;
typedef unsigned int U32;
@ -273,6 +284,21 @@ typedef enum {
/*-************************************
* Reading and writing into memory
**************************************/
/**
* LZ4 relies on memcpy with a constant size being inlined. In freestanding
* environments, the compiler can't assume the implementation of memcpy() is
* standard compliant, so it can't apply its specialized memcpy() inlining
* logic. When possible, use __builtin_memcpy() to tell the compiler to analyze
* memcpy() as if it were standard compliant, so it can inline it in freestanding
* environments. This is needed when decompressing the Linux Kernel, for example.
*/
#if defined(__GNUC__) && (__GNUC__ >= 4)
#define LZ4_memcpy(dst, src, size) __builtin_memcpy(dst, src, size)
#else
#define LZ4_memcpy(dst, src, size) memcpy(dst, src, size)
#endif
static unsigned LZ4_isLittleEndian(void) {
const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
return one.c[0];
@ -306,28 +332,28 @@ static void LZ4_write32(void *memPtr, U32 value) { ((unalign *)memPtr)->u32 = va
static U16 LZ4_read16(const void *memPtr) {
U16 val;
memcpy(&val, memPtr, sizeof(val));
LZ4_memcpy(&val, memPtr, sizeof(val));
return val;
}
static U32 LZ4_read32(const void *memPtr) {
U32 val;
memcpy(&val, memPtr, sizeof(val));
LZ4_memcpy(&val, memPtr, sizeof(val));
return val;
}
static reg_t LZ4_read_ARCH(const void *memPtr) {
reg_t val;
memcpy(&val, memPtr, sizeof(val));
LZ4_memcpy(&val, memPtr, sizeof(val));
return val;
}
static void LZ4_write16(void *memPtr, U16 value) {
memcpy(memPtr, &value, sizeof(value));
LZ4_memcpy(memPtr, &value, sizeof(value));
}
static void LZ4_write32(void *memPtr, U32 value) {
memcpy(memPtr, &value, sizeof(value));
LZ4_memcpy(memPtr, &value, sizeof(value));
}
#endif /* LZ4_FORCE_MEMORY_ACCESS */
@ -359,7 +385,7 @@ void LZ4_wildCopy8(void *dstPtr, const void *srcPtr, void *dstEnd) {
const BYTE *s = (const BYTE *)srcPtr;
BYTE *const e = (BYTE *)dstEnd;
do { memcpy(d, s, 8); d += 8; s += 8; }
do { LZ4_memcpy(d, s, 8); d += 8; s += 8; }
while (d < e);
}
@ -368,12 +394,12 @@ static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3};
#ifndef LZ4_FAST_DEC_LOOP
# if defined(__i386__) || defined(__x86_64__)
# if defined __i386__ || defined _M_IX86 || defined __x86_64__ || defined _M_X64
# define LZ4_FAST_DEC_LOOP 1
# elif defined(__aarch64__) && !defined(__clang__)
/* On aarch64, we disable this optimization for clang because on certain
* mobile chipsets and clang, it reduces performance. For more information
* refer to https://github.com/lz4/lz4/pull/707. */
* mobile chipsets, performance is reduced with clang. For information
* refer to https://github.com/lz4/lz4/pull/707 */
# define LZ4_FAST_DEC_LOOP 1
# else
# define LZ4_FAST_DEC_LOOP 0
@ -390,11 +416,11 @@ LZ4_memcpy_using_offset_base(BYTE *dstPtr, const BYTE *srcPtr, BYTE *dstEnd, con
dstPtr[2] = srcPtr[2];
dstPtr[3] = srcPtr[3];
srcPtr += inc32table[offset];
memcpy(dstPtr + 4, srcPtr, 4);
LZ4_memcpy(dstPtr + 4, srcPtr, 4);
srcPtr -= dec64table[offset];
dstPtr += 8;
} else {
memcpy(dstPtr, srcPtr, 8);
LZ4_memcpy(dstPtr, srcPtr, 8);
dstPtr += 8;
srcPtr += 8;
}
@ -411,7 +437,7 @@ LZ4_wildCopy32(void *dstPtr, const void *srcPtr, void *dstEnd) {
const BYTE *s = (const BYTE *)srcPtr;
BYTE *const e = (BYTE *)dstEnd;
do { memcpy(d, s, 16); memcpy(d + 16, s + 16, 16); d += 32; s += 32; }
do { LZ4_memcpy(d, s, 16); LZ4_memcpy(d + 16, s + 16, 16); d += 32; s += 32; }
while (d < e);
}
@ -430,23 +456,23 @@ LZ4_memcpy_using_offset(BYTE *dstPtr, const BYTE *srcPtr, BYTE *dstEnd, const si
memset(v, *srcPtr, 8);
break;
case 2:
memcpy(v, srcPtr, 2);
memcpy(&v[2], srcPtr, 2);
memcpy(&v[4], &v[0], 4);
LZ4_memcpy(v, srcPtr, 2);
LZ4_memcpy(&v[2], srcPtr, 2);
LZ4_memcpy(&v[4], &v[0], 4);
break;
case 4:
memcpy(v, srcPtr, 4);
memcpy(&v[4], srcPtr, 4);
LZ4_memcpy(v, srcPtr, 4);
LZ4_memcpy(&v[4], srcPtr, 4);
break;
default:
LZ4_memcpy_using_offset_base(dstPtr, srcPtr, dstEnd, offset);
return;
}
memcpy(dstPtr, v, 8);
LZ4_memcpy(dstPtr, v, 8);
dstPtr += 8;
while (dstPtr < dstEnd) {
memcpy(dstPtr, v, 8);
LZ4_memcpy(dstPtr, v, 8);
dstPtr += 8;
}
}
@ -457,54 +483,69 @@ LZ4_memcpy_using_offset(BYTE *dstPtr, const BYTE *srcPtr, BYTE *dstEnd, const si
* Common functions
**************************************/
static unsigned LZ4_NbCommonBytes(reg_t val) {
assert(val != 0);
if (LZ4_isLittleEndian()) {
if (sizeof(val) == 8) {
# if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
# if defined(_MSC_VER) && (_MSC_VER >= 1800) && defined(_M_AMD64) && !defined(LZ4_FORCE_SW_BITCOUNT)
/* x64 CPUS without BMI support interpret `TZCNT` as `REP BSF` */
return (unsigned)_tzcnt_u64(val) >> 3;
# elif defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
unsigned long r = 0;
_BitScanForward64(&r, (U64)val);
return (int)(r >> 3);
# elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT)
return (unsigned)r >> 3;
# elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
!defined(LZ4_FORCE_SW_BITCOUNT)
return (unsigned)__builtin_ctzll((U64)val) >> 3;
# else
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];
const U64 m = 0x0101010101010101ULL;
val ^= val - 1;
return (unsigned)(((U64)((val & (m - 1)) * m)) >> 56);
# endif
} else /* 32 bits */ {
# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
unsigned long r;
_BitScanForward(&r, (U32)val);
return (int)(r >> 3);
# elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT)
return (unsigned)r >> 3;
# elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
!defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT)
return (unsigned)__builtin_ctz((U32)val) >> 3;
# else
static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0,
3, 2, 2, 1, 3, 2, 0, 1,
3, 3, 1, 2, 2, 2, 2, 0,
3, 1, 2, 0, 1, 0, 1, 1
};
return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
const U32 m = 0x01010101;
return (unsigned)((((val - 1) ^ val) & (m - 1)) * m) >> 24;
# endif
}
} else { /* Big Endian CPU */
if (sizeof(val) == 8) { /* 64-bits */
# if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
unsigned long r = 0;
_BitScanReverse64(&r, val);
return (unsigned)(r >> 3);
# elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT)
if (sizeof(val) == 8) {
# if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
!defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT)
return (unsigned)__builtin_clzll((U64)val) >> 3;
# else
#if 1
/* this method is probably faster,
* but adds a 128 bytes lookup table */
static const unsigned char ctz7_tab[128] = {
7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
};
U64 const mask = 0x0101010101010101ULL;
U64 const t = (((val >> 8) - mask) | val) & mask;
return ctz7_tab[(t * 0x0080402010080402ULL) >> 57];
#else
/* this method doesn't consume memory space like the previous one,
* but it contains several branches,
* that may end up slowing execution */
static const U32 by32 = sizeof(val) * 4; /* 32 on 64 bits (goal), 16 on 32 bits.
Just to avoid some static analyzer complaining about shift by 32 on 32-bits target.
Note that this code path is never triggered in 32-bits mode. */
Just to avoid some static analyzer complaining about shift by 32 on 32-bits target.
Note that this code path is never triggered in 32-bits mode. */
unsigned r;
if (!(val >> by32)) { r = 4; }
else { r = 0; val >>= by32; }
@ -512,25 +553,24 @@ static unsigned LZ4_NbCommonBytes(reg_t val) {
else { val >>= 24; }
r += (!val);
return r;
#endif
# endif
} else /* 32 bits */ {
# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
unsigned long r = 0;
_BitScanReverse(&r, (unsigned long)val);
return (unsigned)(r >> 3);
# elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT)
# if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
!defined(LZ4_FORCE_SW_BITCOUNT)
return (unsigned)__builtin_clz((U32)val) >> 3;
# else
unsigned r;
if (!(val >> 16)) { r = 2; val >>= 8; }
else { r = 0; val >>= 24; }
r += (!val);
return r;
val >>= 8;
val = ((((val + 0x00FFFF00) | 0x00FFFFFF) + val) |
(val + 0x00FF0000)) >> 24;
return (unsigned)val ^ 3;
# endif
}
}
}
#define STEPSIZE sizeof(reg_t)
LZ4_FORCE_INLINE
unsigned LZ4_count(const BYTE *pIn, const BYTE *pMatch, const BYTE *pInLimit) {
@ -606,7 +646,7 @@ typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive;
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; }
int LZ4_sizeofState(void) { return LZ4_STREAMSIZE; }
/*-************************************
@ -629,14 +669,14 @@ int LZ4_decompress_safe_forceExtDict(const char *source, char *dest,
/*-******************************
* Compression functions
********************************/
static U32 LZ4_hash4(U32 sequence, tableType_t const tableType) {
LZ4_FORCE_INLINE U32 LZ4_hash4(U32 sequence, tableType_t const tableType) {
if (tableType == byU16)
return ((sequence * 2654435761U) >> ((MINMATCH * 8) - (LZ4_HASHLOG + 1)));
else
return ((sequence * 2654435761U) >> ((MINMATCH * 8) - LZ4_HASHLOG));
}
static U32 LZ4_hash5(U64 sequence, tableType_t const tableType) {
LZ4_FORCE_INLINE U32 LZ4_hash5(U64 sequence, tableType_t const tableType) {
const U32 hashLog = (tableType == byU16) ? LZ4_HASHLOG + 1 : LZ4_HASHLOG;
if (LZ4_isLittleEndian()) {
const U64 prime5bytes = 889523592379ULL;
@ -652,7 +692,7 @@ LZ4_FORCE_INLINE U32 LZ4_hashPosition(const void *const p, tableType_t const tab
return LZ4_hash4(LZ4_read32(p), tableType);
}
static void LZ4_clearHash(U32 h, void *tableBase, tableType_t const tableType) {
LZ4_FORCE_INLINE void LZ4_clearHash(U32 h, void *tableBase, tableType_t const tableType) {
switch (tableType) {
default: /* fallthrough */
case clearedTable: { /* illegal! */ assert(0); return; }
@ -665,7 +705,7 @@ static void LZ4_clearHash(U32 h, void *tableBase, tableType_t const tableType) {
}
}
static void LZ4_putIndexOnHash(U32 idx, U32 h, void *tableBase, tableType_t const tableType) {
LZ4_FORCE_INLINE void LZ4_putIndexOnHash(U32 idx, U32 h, void *tableBase, tableType_t const tableType) {
switch (tableType) {
default: /* fallthrough */
case clearedTable: /* fallthrough */
@ -677,9 +717,9 @@ static void LZ4_putIndexOnHash(U32 idx, U32 h, void *tableBase, tableType_t cons
}
}
static void LZ4_putPositionOnHash(const BYTE *p, U32 h,
void *tableBase, tableType_t const tableType,
const BYTE *srcBase) {
LZ4_FORCE_INLINE void LZ4_putPositionOnHash(const BYTE *p, U32 h,
void *tableBase, tableType_t const tableType,
const BYTE *srcBase) {
switch (tableType) {
case clearedTable: { /* illegal! */ assert(0); return; }
case byPtr:
@ -702,7 +742,7 @@ LZ4_FORCE_INLINE void LZ4_putPosition(const BYTE *p, void *tableBase, tableType_
* Assumption 1 : only valid if tableType == byU32 or byU16.
* Assumption 2 : h is presumed valid (within limits of hash table)
*/
static U32 LZ4_getIndexOnHash(U32 h, const void *tableBase, tableType_t tableType) {
LZ4_FORCE_INLINE U32 LZ4_getIndexOnHash(U32 h, const void *tableBase, tableType_t tableType) {
LZ4_STATIC_ASSERT(LZ4_MEMORY_USAGE > 2);
if (tableType == byU32) {
const U32 *const hashTable = (const U32 *) tableBase;
@ -736,22 +776,13 @@ LZ4_FORCE_INLINE void
LZ4_prepareTable(LZ4_stream_t_internal *const cctx,
const int inputSize,
const tableType_t tableType) {
/* If compression failed during the previous step, then the context
* is marked as dirty, therefore, it has to be fully reset.
*/
if (cctx->dirty) {
DEBUGLOG(5, "LZ4_prepareTable: Full reset for %p", cctx);
MEM_INIT(cctx, 0, sizeof(LZ4_stream_t_internal));
return;
}
/* If the table hasn't been used, it's guaranteed to be zeroed out, and is
* therefore safe to use no matter what mode we're in. Otherwise, we figure
* out if it's safe to leave as is or whether it needs to be reset.
*/
if (cctx->tableType != clearedTable) {
if ((tableType_t)cctx->tableType != clearedTable) {
assert(inputSize >= 0);
if (cctx->tableType != tableType
if ((tableType_t)cctx->tableType != tableType
|| ((tableType == byU16) && cctx->currentOffset + (unsigned)inputSize >= 0xFFFFU)
|| ((tableType == byU32) && cctx->currentOffset > 1 GB)
|| tableType == byPtr
@ -759,7 +790,7 @@ LZ4_prepareTable(LZ4_stream_t_internal *const cctx,
DEBUGLOG(4, "LZ4_prepareTable: Resetting table in %p", cctx);
MEM_INIT(cctx->hashTable, 0, LZ4_HASHTABLESIZE);
cctx->currentOffset = 0;
cctx->tableType = clearedTable;
cctx->tableType = (U32)clearedTable;
} else {
DEBUGLOG(4, "LZ4_prepareTable: Re-use hash table (no reset)");
}
@ -781,8 +812,12 @@ LZ4_prepareTable(LZ4_stream_t_internal *const cctx,
}
/** LZ4_compress_generic() :
inlined, to ensure branches are decided at compilation time */
LZ4_FORCE_INLINE int LZ4_compress_generic(
* inlined, to ensure branches are decided at compilation time.
* Presumed already validated at this stage:
* - source != NULL
* - inputSize > 0
*/
LZ4_FORCE_INLINE int LZ4_compress_generic_validated(
LZ4_stream_t_internal *const cctx,
const char *const source,
char *const dest,
@ -810,7 +845,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic(
int const maybe_extMem = (dictDirective == usingExtDict) || (dictDirective == usingDictCtx);
U32 const prefixIdxLimit = startIndex - dictSize; /* used when dictDirective == dictSmall */
const BYTE *const dictEnd = dictionary + dictSize;
const BYTE *const dictEnd = dictionary ? dictionary + dictSize : dictionary;
const BYTE *anchor = (const BYTE *) source;
const BYTE *const iend = ip + inputSize;
const BYTE *const mflimitPlusOne = iend - MFLIMIT + 1;
@ -818,7 +853,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic(
/* the dictCtx currentOffset is indexed on the start of the dictionary,
* while a dictionary in the current context precedes the currentOffset */
const BYTE *dictBase = (dictDirective == usingDictCtx) ?
const BYTE *dictBase = !dictionary ? NULL : (dictDirective == usingDictCtx) ?
dictionary + dictSize - dictCtx->currentOffset :
dictionary + dictSize - startIndex;
@ -828,11 +863,11 @@ LZ4_FORCE_INLINE int LZ4_compress_generic(
U32 offset = 0;
U32 forwardH;
DEBUGLOG(5, "LZ4_compress_generic: srcSize=%i, tableType=%u", inputSize, tableType);
DEBUGLOG(5, "LZ4_compress_generic_validated: srcSize=%i, tableType=%u", inputSize, tableType);
assert(ip != NULL);
/* If init conditions are not met, we don't have to mark stream
* as having dirty context, since no action was taken yet */
if (outputDirective == fillOutput && maxOutputSize < 1) { return 0; } /* Impossible to store anything */
if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) { return 0; } /* Unsupported inputSize, too large (or negative) */
if ((tableType == byU16) && (inputSize >= LZ4_64Klimit)) { return 0; } /* Size too large (not within 64K limit) */
if (tableType == byPtr) assert(dictDirective == noDict); /* only supported use case with byPtr */
assert(acceleration >= 1);
@ -849,7 +884,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic(
cctx->dictSize += (U32)inputSize;
}
cctx->currentOffset += (U32)inputSize;
cctx->tableType = (U16)tableType;
cctx->tableType = (U32)tableType;
if (inputSize < LZ4_minLength) goto _last_literals; /* Input too small, no compression (all literals) */
@ -1162,7 +1197,7 @@ _last_literals:
} else {
*op++ = (BYTE)(lastRun << ML_BITS);
}
memcpy(op, anchor, lastRun);
LZ4_memcpy(op, anchor, lastRun);
ip = anchor + lastRun;
op += lastRun;
}
@ -1170,17 +1205,58 @@ _last_literals:
if (outputDirective == fillOutput) {
*inputConsumed = (int)(((const char *)ip) - source);
}
DEBUGLOG(5, "LZ4_compress_generic: compressed %i bytes into %i bytes", inputSize, (int)(((char *)op) - dest));
result = (int)(((char *)op) - dest);
assert(result > 0);
DEBUGLOG(5, "LZ4_compress_generic: compressed %i bytes into %i bytes", inputSize, result);
return result;
}
/** LZ4_compress_generic() :
* inlined, to ensure branches are decided at compilation time;
* takes care of src == (NULL, 0)
* and forward the rest to LZ4_compress_generic_validated */
LZ4_FORCE_INLINE int LZ4_compress_generic(
LZ4_stream_t_internal *const cctx,
const char *const src,
char *const dst,
const int srcSize,
int *inputConsumed, /* only written when outputDirective == fillOutput */
const int dstCapacity,
const limitedOutput_directive outputDirective,
const tableType_t tableType,
const dict_directive dictDirective,
const dictIssue_directive dictIssue,
const int acceleration) {
DEBUGLOG(5, "LZ4_compress_generic: srcSize=%i, dstCapacity=%i",
srcSize, dstCapacity);
if ((U32)srcSize > (U32)LZ4_MAX_INPUT_SIZE) { return 0; } /* Unsupported srcSize, too large (or negative) */
if (srcSize == 0) { /* src == NULL supported if srcSize == 0 */
if (outputDirective != notLimited && dstCapacity <= 0) return 0; /* no output, can't write anything */
DEBUGLOG(5, "Generating an empty block");
assert(outputDirective == notLimited || dstCapacity >= 1);
assert(dst != NULL);
dst[0] = 0;
if (outputDirective == fillOutput) {
assert(inputConsumed != NULL);
*inputConsumed = 0;
}
return 1;
}
assert(src != NULL);
return LZ4_compress_generic_validated(cctx, src, dst, srcSize,
inputConsumed, /* only written into if outputDirective == fillOutput */
dstCapacity, outputDirective,
tableType, dictDirective, dictIssue, acceleration);
}
int LZ4_compress_fast_extState(void *state, const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration) {
LZ4_stream_t_internal *const ctx = & LZ4_initStream(state, sizeof(LZ4_stream_t)) -> internal_donotuse;
assert(ctx != NULL);
if (acceleration < 1) acceleration = ACCELERATION_DEFAULT;
if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT;
if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX;
if (maxOutputSize >= LZ4_compressBound(inputSize)) {
if (inputSize < LZ4_64Klimit) {
return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, 0, notLimited, byU16, noDict, noDictIssue, acceleration);
@ -1209,7 +1285,8 @@ int LZ4_compress_fast_extState(void *state, const char *source, char *dest, int
*/
int LZ4_compress_fast_extState_fastReset(void *state, const char *src, char *dst, int srcSize, int dstCapacity, int acceleration) {
LZ4_stream_t_internal *ctx = &((LZ4_stream_t *)state)->internal_donotuse;
if (acceleration < 1) acceleration = ACCELERATION_DEFAULT;
if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT;
if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX;
if (dstCapacity >= LZ4_compressBound(srcSize)) {
if (srcSize < LZ4_64Klimit) {
@ -1266,22 +1343,6 @@ int LZ4_compress_default(const char *src, char *dst, int srcSize, int maxOutputS
}
int LZ4_compress_fast_force(const char *src, char *dst, int srcSize, int dstCapacity, int acceleration);
/* hidden debug function */
/* strangely enough, gcc generates faster code when this function is uncommented, even if unused */
int LZ4_compress_fast_force(const char *src, char *dst, int srcSize, int dstCapacity, int acceleration) {
LZ4_stream_t ctx;
LZ4_initStream(&ctx, sizeof(ctx));
if (srcSize < LZ4_64Klimit) {
return LZ4_compress_generic(&ctx.internal_donotuse, src, dst, srcSize, NULL, dstCapacity, limitedOutput, byU16, noDict, noDictIssue, acceleration);
} else {
tableType_t const addrMode = (sizeof(void *) > 4) ? byU32 : byPtr;
return LZ4_compress_generic(&ctx.internal_donotuse, src, dst, srcSize, NULL, dstCapacity, limitedOutput, addrMode, noDict, noDictIssue, acceleration);
}
}
/* Note!: This function leaves the stream in an unclean/broken state!
* It is not safe to subsequently use the same state with a _fastReset() or
* _continue() call without resetting it. */
@ -1339,8 +1400,8 @@ LZ4_stream_t *LZ4_createStream(void) {
it reports an aligment of 8-bytes,
while actually aligning LZ4_stream_t on 4 bytes. */
static size_t LZ4_stream_t_alignment(void) {
struct { char c; LZ4_stream_t t; } t_a;
return sizeof(t_a) - sizeof(t_a.t);
typedef struct { char c; LZ4_stream_t t; } t_a;
return sizeof(t_a) - sizeof(LZ4_stream_t);
}
#endif
@ -1408,7 +1469,7 @@ int LZ4_loadDict(LZ4_stream_t *LZ4_dict, const char *dictionary, int dictSize) {
base = dictEnd - dict->currentOffset;
dict->dictionary = p;
dict->dictSize = (U32)(dictEnd - p);
dict->tableType = tableType;
dict->tableType = (U32)tableType;
while (p <= dictEnd - HASH_UNIT) {
LZ4_putPosition(p, dict->hashTable, tableType, base);
@ -1426,12 +1487,6 @@ void LZ4_attach_dictionary(LZ4_stream_t *workingStream, const LZ4_stream_t *dict
workingStream, dictionaryStream,
dictCtx != NULL ? dictCtx->dictSize : 0);
/* Calling LZ4_resetStream_fast() here makes sure that changes will not be
* erased by subsequent calls to LZ4_resetStream_fast() in case stream was
* marked as having dirty context, e.g. requiring full reset.
*/
LZ4_resetStream_fast(workingStream);
if (dictCtx != NULL) {
/* If the current offset is zero, we will never look in the
* external dictionary context, since there is no value a table
@ -1481,9 +1536,9 @@ int LZ4_compress_fast_continue(LZ4_stream_t *LZ4_stream,
DEBUGLOG(5, "LZ4_compress_fast_continue (inputSize=%i)", inputSize);
if (streamPtr->dirty) { return 0; } /* Uninitialized structure detected */
LZ4_renormDictT(streamPtr, inputSize); /* avoid index overflow */
if (acceleration < 1) acceleration = ACCELERATION_DEFAULT;
if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT;
if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX;
/* invalidate tiny dictionaries */
if ((streamPtr->dictSize - 1 < 4 - 1) /* intentional underflow */
@ -1528,7 +1583,7 @@ int LZ4_compress_fast_continue(LZ4_stream_t *LZ4_stream,
* cost to copy the dictionary's tables into the active context,
* so that the compression loop is only looking into one table.
*/
memcpy(streamPtr, streamPtr->dictCtx, sizeof(LZ4_stream_t));
LZ4_memcpy(streamPtr, streamPtr->dictCtx, sizeof(LZ4_stream_t));
result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, noDictIssue, acceleration);
} else {
result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingDictCtx, noDictIssue, acceleration);
@ -1612,8 +1667,8 @@ typedef enum { decode_full_block = 0, partial_decode = 1 } earlyEnd_directive;
typedef enum { loop_error = -2, initial_error = -1, ok = 0 } variable_length_error;
LZ4_FORCE_INLINE unsigned
read_variable_length(const BYTE **ip, const BYTE *lencheck, int loop_check, int initial_check, variable_length_error *error) {
unsigned length = 0;
unsigned s;
U32 length = 0;
U32 s;
if (initial_check && unlikely((*ip) >= lencheck)) { /* overflow detection */
*error = initial_error;
return length;
@ -1737,12 +1792,12 @@ LZ4_decompress_generic(
/* We don't need to check oend, since we check it once for each loop below */
if (ip > iend - (16 + 1/*max lit + offset + nextToken*/)) { goto safe_literal_copy; }
/* Literals can only be 14, but hope compilers optimize if we copy by a register size */
memcpy(op, ip, 16);
LZ4_memcpy(op, ip, 16);
} else { /* LZ4_decompress_fast() */
/* LZ4_decompress_fast() cannot copy more than 8 bytes at a time :
* it doesn't know input length, and relies on end-of-block properties */
memcpy(op, ip, 8);
if (length > 8) { memcpy(op + 8, ip + 8, 8); }
LZ4_memcpy(op, ip, 8);
if (length > 8) { LZ4_memcpy(op + 8, ip + 8, 8); }
}
ip += length;
op = cpy;
@ -1780,9 +1835,9 @@ LZ4_decompress_generic(
assert(match <= op);
assert(op + 18 <= oend);
memcpy(op, match, 8);
memcpy(op + 8, match + 8, 8);
memcpy(op + 16, match + 16, 2);
LZ4_memcpy(op, match, 8);
LZ4_memcpy(op + 8, match + 8, 8);
LZ4_memcpy(op + 16, match + 16, 2);
op += length;
continue;
}
@ -1794,7 +1849,8 @@ LZ4_decompress_generic(
if ((dict == usingExtDict) && (match < lowPrefix)) {
if (unlikely(op + length > oend - LASTLITERALS)) {
if (partialDecoding) {
length = MIN(length, (size_t)(oend - op)); /* reach end of buffer */
DEBUGLOG(7, "partialDecoding: dictionary match, close to dstEnd");
length = MIN(length, (size_t)(oend - op));
} else {
goto _output_error; /* end-of-block condition violated */
}
@ -1808,14 +1864,14 @@ LZ4_decompress_generic(
/* match stretches into both external dictionary and current block */
size_t const copySize = (size_t)(lowPrefix - match);
size_t const restSize = length - copySize;
memcpy(op, dictEnd - copySize, copySize);
LZ4_memcpy(op, dictEnd - copySize, copySize);
op += copySize;
if (restSize > (size_t)(op - lowPrefix)) { /* overlap copy */
BYTE *const endOfMatch = op + restSize;
const BYTE *copyFrom = lowPrefix;
while (op < endOfMatch) { *op++ = *copyFrom++; }
} else {
memcpy(op, lowPrefix, restSize);
LZ4_memcpy(op, lowPrefix, restSize);
op += restSize;
}
}
@ -1857,7 +1913,7 @@ safe_decode:
/* strictly "less than" on input, to re-enter the loop with at least one byte */
&& likely((endOnInput ? ip < shortiend : 1) & (op <= shortoend))) {
/* Copy the literals */
memcpy(op, ip, endOnInput ? 16 : 8);
LZ4_memcpy(op, ip, endOnInput ? 16 : 8);
op += length;
ip += length;
@ -1874,9 +1930,9 @@ safe_decode:
&& (offset >= 8)
&& (dict == withPrefix64k || match >= lowPrefix)) {
/* Copy the match. */
memcpy(op + 0, match + 0, 8);
memcpy(op + 8, match + 8, 8);
memcpy(op + 16, match + 16, 2);
LZ4_memcpy(op + 0, match + 0, 8);
LZ4_memcpy(op + 8, match + 8, 8);
LZ4_memcpy(op + 16, match + 16, 2);
op += length + MINMATCH;
/* Both stages worked, load the next token. */
continue;
@ -1905,29 +1961,34 @@ safe_literal_copy:
if (((endOnInput) && ((cpy > oend - MFLIMIT) || (ip + length > iend - (2 + 1 + LASTLITERALS))))
|| ((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) {
/* We've either hit the input parsing restriction or the output parsing restriction.
* If we've hit the input parsing condition then this must be the last sequence.
* If we've hit the output parsing condition then we are either using partialDecoding
* or we've hit the output parsing condition.
* In the normal scenario, decoding a full block, it must be the last sequence,
* otherwise it's an error (invalid input or dimensions).
* In partialDecoding scenario, it's necessary to ensure there is no buffer overflow.
*/
if (partialDecoding) {
/* Since we are partial decoding we may be in this block because of the output parsing
* restriction, which is not valid since the output buffer is allowed to be undersized.
*/
assert(endOnInput);
/* If we're in this block because of the input parsing condition, then we must be on the
* last sequence (or invalid), so we must check that we exactly consume the input.
DEBUGLOG(7, "partialDecoding: copying literals, close to input or output end")
DEBUGLOG(7, "partialDecoding: literal length = %u", (unsigned)length);
DEBUGLOG(7, "partialDecoding: remaining space in dstBuffer : %i", (int)(oend - op));
DEBUGLOG(7, "partialDecoding: remaining space in srcBuffer : %i", (int)(iend - ip));
/* Finishing in the middle of a literals segment,
* due to lack of input.
*/
if ((ip + length > iend - (2 + 1 + LASTLITERALS)) && (ip + length != iend)) { goto _output_error; }
assert(ip + length <= iend);
/* We are finishing in the middle of a literals segment.
* Break after the copy.
if (ip + length > iend) {
length = (size_t)(iend - ip);
cpy = op + length;
}
/* Finishing in the middle of a literals segment,
* due to lack of output space.
*/
if (cpy > oend) {
cpy = oend;
assert(op <= oend);
length = (size_t)(oend - op);
}
assert(ip + length <= iend);
} else {
/* We must be on the last sequence because of the parsing limitations so check
* that we exactly regenerate the original size (must be exact when !endOnInput).
@ -1938,14 +1999,15 @@ safe_literal_copy:
*/
if ((endOnInput) && ((ip + length != iend) || (cpy > oend))) { goto _output_error; }
}
memmove(op, ip, length); /* supports overlapping memory regions, which only matters for in-place decompression scenarios */
memmove(op, ip, length); /* supports overlapping memory regions; only matters for in-place decompression scenarios */
ip += length;
op += length;
/* Necessarily EOF when !partialDecoding. When partialDecoding
* it is EOF if we've either filled the output buffer or hit
* the input parsing restriction.
/* Necessarily EOF when !partialDecoding.
* When partialDecoding, it is EOF if we've either
* filled the output buffer or
* can't proceed with reading an offset for following match.
*/
if (!partialDecoding || (cpy == oend) || (ip == iend)) {
if (!partialDecoding || (cpy == oend) || (ip >= (iend - 2))) {
break;
}
} else {
@ -1990,14 +2052,14 @@ safe_match_copy:
/* match stretches into both external dictionary and current block */
size_t const copySize = (size_t)(lowPrefix - match);
size_t const restSize = length - copySize;
memcpy(op, dictEnd - copySize, copySize);
LZ4_memcpy(op, dictEnd - copySize, copySize);
op += copySize;
if (restSize > (size_t)(op - lowPrefix)) { /* overlap copy */
BYTE *const endOfMatch = op + restSize;
const BYTE *copyFrom = lowPrefix;
while (op < endOfMatch) *op++ = *copyFrom++;
} else {
memcpy(op, lowPrefix, restSize);
LZ4_memcpy(op, lowPrefix, restSize);
op += restSize;
}
}
@ -2017,7 +2079,7 @@ safe_match_copy:
if (matchEnd > op) { /* overlap copy */
while (op < copyEnd) { *op++ = *match++; }
} else {
memcpy(op, match, mlen);
LZ4_memcpy(op, match, mlen);
}
op = copyEnd;
if (op == oend) { break; }
@ -2031,10 +2093,10 @@ safe_match_copy:
op[2] = match[2];
op[3] = match[3];
match += inc32table[offset];
memcpy(op + 4, match, 4);
LZ4_memcpy(op + 4, match, 4);
match -= dec64table[offset];
} else {
memcpy(op, match, 8);
LZ4_memcpy(op, match, 8);
match += 8;
}
op += 8;
@ -2049,7 +2111,7 @@ safe_match_copy:
}
while (op < cpy) { *op++ = *match++; }
} else {
memcpy(op, match, 8);
LZ4_memcpy(op, match, 8);
if (length > 16) { LZ4_wildCopy8(op + 8, match + 8, cpy); }
}
op = cpy; /* wildcopy correction */
@ -2057,6 +2119,7 @@ safe_match_copy:
/* end of decoding */
if (endOnInput) {
DEBUGLOG(5, "decoded %i bytes", (int)(((char *)op) - dst));
return (int)(((char *)op) - dst); /* Nb of output bytes decoded */
} else {
return (int)(((const char *)ip) - src); /* Nb of input bytes read */
@ -2349,7 +2412,7 @@ int LZ4_uncompress_unknownOutputSize(const char *source, char *dest, int isize,
/* Obsolete Streaming functions */
int LZ4_sizeofStreamState() { return LZ4_STREAMSIZE; }
int LZ4_sizeofStreamState(void) { return LZ4_STREAMSIZE; }
int LZ4_resetStreamState(void *state, char *inputBuffer) {
(void)inputBuffer;

View file

@ -186,7 +186,8 @@ LZ4LIB_API int LZ4_compressBound(int inputSize);
The larger the acceleration value, the faster the algorithm, but also the lesser the compression.
It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed.
An acceleration value of "1" is the same as regular LZ4_compress_default()
Values <= 0 will be replaced by ACCELERATION_DEFAULT (currently == 1, see lz4.c).
Values <= 0 will be replaced by LZ4_ACCELERATION_DEFAULT (currently == 1, see lz4.c).
Values > LZ4_ACCELERATION_MAX will be replaced by LZ4_ACCELERATION_MAX (currently == 65537, see lz4.c).
*/
LZ4LIB_API int LZ4_compress_fast(const char *src, char *dst, int srcSize, int dstCapacity, int acceleration);
@ -212,7 +213,18 @@ LZ4LIB_API int LZ4_compress_fast_extState(void *state, const char *src, char *ds
* New value is necessarily <= input value.
* @return : Nb bytes written into 'dst' (necessarily <= targetDestSize)
* or 0 if compression fails.
*/
*
* Note : from v1.8.2 to v1.9.1, this function had a bug (fixed un v1.9.2+):
* the produced compressed content could, in specific circumstances,
* require to be decompressed into a destination buffer larger
* by at least 1 byte than the content to decompress.
* If an application uses `LZ4_compress_destSize()`,
* it's highly recommended to update liblz4 to v1.9.2 or better.
* If this can't be done or ensured,
* the receiving decompression function should provide
* a dstCapacity which is > decompressedSize, by at least 1 byte.
* See https://github.com/lz4/lz4/issues/859 for details
*/
LZ4LIB_API int LZ4_compress_destSize(const char *src, char *dst, int *srcSizePtr, int targetDstSize);
@ -220,25 +232,35 @@ LZ4LIB_API int LZ4_compress_destSize(const char *src, char *dst, int *srcSizePtr
* Decompress an LZ4 compressed block, of size 'srcSize' at position 'src',
* into destination buffer 'dst' of size 'dstCapacity'.
* Up to 'targetOutputSize' bytes will be decoded.
* The function stops decoding on reaching this objective,
* which can boost performance when only the beginning of a block is required.
* The function stops decoding on reaching this objective.
* This can be useful to boost performance
* whenever only the beginning of a block is required.
*
* @return : the number of bytes decoded in `dst` (necessarily <= dstCapacity)
* @return : the number of bytes decoded in `dst` (necessarily <= targetOutputSize)
* If source stream is detected malformed, function returns a negative result.
*
* Note : @return can be < targetOutputSize, if compressed block contains less data.
* Note 1 : @return can be < targetOutputSize, if compressed block contains less data.
*
* Note 2 : this function features 2 parameters, targetOutputSize and dstCapacity,
* and expects targetOutputSize <= dstCapacity.
* It effectively stops decoding on reaching targetOutputSize,
* Note 2 : targetOutputSize must be <= dstCapacity
*
* Note 3 : this function effectively stops decoding on reaching targetOutputSize,
* so dstCapacity is kind of redundant.
* This is because in a previous version of this function,
* decoding operation would not "break" a sequence in the middle.
* As a consequence, there was no guarantee that decoding would stop at exactly targetOutputSize,
* This is because in older versions of this function,
* decoding operation would still write complete sequences.
* Therefore, there was no guarantee that it would stop writing at exactly targetOutputSize,
* it could write more bytes, though only up to dstCapacity.
* Some "margin" used to be required for this operation to work properly.
* This is no longer necessary.
* The function nonetheless keeps its signature, in an effort to not break API.
* Thankfully, this is no longer necessary.
* The function nonetheless keeps the same signature, in an effort to preserve API compatibility.
*
* Note 4 : If srcSize is the exact size of the block,
* then targetOutputSize can be any value,
* including larger than the block's decompressed size.
* The function will, at most, generate block's decompressed size.
*
* Note 5 : If srcSize is _larger_ than block's compressed size,
* then targetOutputSize **MUST** be <= block's decompressed size.
* Otherwise, *silent corruption will occur*.
*/
LZ4LIB_API int LZ4_decompress_safe_partial(const char *src, char *dst, int srcSize, int targetOutputSize, int dstCapacity);
@ -564,8 +586,7 @@ typedef struct LZ4_stream_t_internal LZ4_stream_t_internal;
struct LZ4_stream_t_internal {
uint32_t hashTable[LZ4_HASH_SIZE_U32];
uint32_t currentOffset;
uint16_t dirty;
uint16_t tableType;
uint32_t tableType;
const uint8_t *dictionary;
const LZ4_stream_t_internal *dictCtx;
uint32_t dictSize;
@ -584,8 +605,7 @@ typedef struct LZ4_stream_t_internal LZ4_stream_t_internal;
struct LZ4_stream_t_internal {
unsigned int hashTable[LZ4_HASH_SIZE_U32];
unsigned int currentOffset;
unsigned short dirty;
unsigned short tableType;
unsigned int tableType;
const unsigned char *dictionary;
const LZ4_stream_t_internal *dictCtx;
unsigned int dictSize;
@ -667,18 +687,17 @@ union LZ4_streamDecode_u {
#ifdef LZ4_DISABLE_DEPRECATE_WARNINGS
# define LZ4_DEPRECATED(message) /* disable deprecation warnings */
#else
# define LZ4_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
# if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */
# define LZ4_DEPRECATED(message) [[deprecated(message)]]
# elif (LZ4_GCC_VERSION >= 405) || defined(__clang__)
# define LZ4_DEPRECATED(message) __attribute__((deprecated(message)))
# elif (LZ4_GCC_VERSION >= 301)
# define LZ4_DEPRECATED(message) __attribute__((deprecated))
# elif defined(_MSC_VER)
# define LZ4_DEPRECATED(message) __declspec(deprecated(message))
# elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 45))
# define LZ4_DEPRECATED(message) __attribute__((deprecated(message)))
# elif defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 31)
# define LZ4_DEPRECATED(message) __attribute__((deprecated))
# else
# pragma message("WARNING: You need to implement LZ4_DEPRECATED for this compiler")
# define LZ4_DEPRECATED(message)
# pragma message("WARNING: LZ4_DEPRECATED needs custom implementation for this compiler")
# define LZ4_DEPRECATED(message) /* disabled */
# endif
#endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */

View file

@ -396,8 +396,8 @@ LZ4HC_InsertAndGetWiderMatch(
if (lookBackLength == 0) { /* no back possible */
size_t const maxML = MIN(currentSegmentLength, srcPatternLength);
if ((size_t)longest < maxML) {
assert(base + matchIndex < ip);
if (ip - (base + matchIndex) > LZ4_DISTANCE_MAX) break;
assert(base + matchIndex != ip);
if ((size_t)(ip - base) - matchIndex > LZ4_DISTANCE_MAX) break;
assert(maxML < 2 GB);
longest = (int)maxML;
*matchpos = base + matchIndex; /* virtual pos, relative to ip, to retrieve offset */
@ -908,8 +908,8 @@ int LZ4_sizeofStateHC(void) { return (int)sizeof(LZ4_streamHC_t); }
* it reports an aligment of 8-bytes,
* while actually aligning LZ4_streamHC_t on 4 bytes. */
static size_t LZ4_streamHC_t_alignment(void) {
struct { char c; LZ4_streamHC_t t; } t_a;
return sizeof(t_a) - sizeof(t_a.t);
typedef struct { char c; LZ4_streamHC_t t; } t_a;
return sizeof(t_a) - sizeof(LZ4_streamHC_t);
}
#endif
@ -1038,7 +1038,7 @@ void LZ4_favorDecompressionSpeed(LZ4_streamHC_t *LZ4_streamHCPtr, int favor) {
int LZ4_loadDictHC(LZ4_streamHC_t *LZ4_streamHCPtr,
const char *dictionary, int dictSize) {
LZ4HC_CCtx_internal *const ctxPtr = &LZ4_streamHCPtr->internal_donotuse;
DEBUGLOG(4, "LZ4_loadDictHC(%p, %p, %d)", LZ4_streamHCPtr, dictionary, dictSize);
DEBUGLOG(4, "LZ4_loadDictHC(ctx:%p, dict:%p, dictSize:%d)", LZ4_streamHCPtr, dictionary, dictSize);
assert(LZ4_streamHCPtr != NULL);
if (dictSize > 64 KB) {
dictionary += (size_t)dictSize - 64 KB;
@ -1285,8 +1285,13 @@ static int LZ4HC_compress_optimal(LZ4HC_CCtx_internal *ctx,
int const fullUpdate,
const dictCtx_directive dict,
const HCfavor_e favorDecSpeed) {
int retval = 0;
#define TRAILING_LITERALS 3
#ifdef LZ4HC_HEAPMODE
LZ4HC_optimal_t *const opt = (LZ4HC_optimal_t *)malloc(sizeof(LZ4HC_optimal_t) * (LZ4_OPT_NUM + TRAILING_LITERALS));
#else
LZ4HC_optimal_t opt[LZ4_OPT_NUM + TRAILING_LITERALS]; /* ~64 KB, which is a bit large for stack... */
#endif
const BYTE *ip = (const BYTE *) source;
const BYTE *anchor = ip;
@ -1298,6 +1303,9 @@ static int LZ4HC_compress_optimal(LZ4HC_CCtx_internal *ctx,
BYTE *oend = op + dstCapacity;
/* init */
#ifdef LZ4HC_HEAPMODE
if (opt == NULL) goto _return_label;
#endif
DEBUGLOG(5, "LZ4HC_compress_optimal(dst=%p, dstCapa=%u)", dst, (unsigned)dstCapacity);
*srcSizePtr = 0;
if (limit == fillOutput) oend -= LASTLITERALS; /* Hack for support LZ4 format restriction */
@ -1522,7 +1530,10 @@ _last_literals:
size_t const totalSize = 1 + litLength + lastRunSize;
if (limit == fillOutput) oend += LASTLITERALS; /* restore correct value */
if (limit && (op + totalSize > oend)) {
if (limit == limitedOutput) return 0; /* Check output limit */
if (limit == limitedOutput) { /* Check output limit */
retval = 0;
goto _return_label;
}
/* adapt lastRunSize to fill 'dst' */
lastRunSize = (size_t)(oend - op) - 1;
litLength = (lastRunSize + 255 - RUN_MASK) / 255;
@ -1544,12 +1555,17 @@ _last_literals:
/* End */
*srcSizePtr = (int)(((const char *)ip) - source);
return (int)((char *)op - dst);
retval = (int)((char *)op - dst);
goto _return_label;
_dest_overflow:
if (limit == fillOutput) {
op = opSaved; /* restore correct out pointer */
goto _last_literals;
}
return 0;
_return_label:
#ifdef LZ4HC_HEAPMODE
free(opt);
#endif
return retval;
}