Rizin
unix-like reverse engineering framework and cli tools
lz4.c File Reference
#include "lz4.h"
#include <stdlib.h>
#include <string.h>
#include <limits.h>

Go to the source code of this file.

Macros

#define LZ4_HEAPMODE   0
 
#define LZ4_ACCELERATION_DEFAULT   1
 
#define LZ4_ACCELERATION_MAX   65537
 
#define LZ4_SRC_INCLUDED   1
 
#define LZ4_STATIC_LINKING_ONLY
 
#define LZ4_DISABLE_DEPRECATE_WARNINGS   /* due to LZ4_decompress_safe_withPrefix64k */
 
#define LZ4_STATIC_LINKING_ONLY   /* LZ4_DISTANCE_MAX */
 
#define LZ4_FORCE_INLINE   static
 
#define LZ4_FORCE_O2
 
#define expect(expr, value)   (expr)
 
#define likely(expr)   expect((expr) != 0, 1)
 
#define unlikely(expr)   expect((expr) != 0, 0)
 
#define LZ4_ALIGN_TEST   1
 
#define ALLOC(s)   malloc(s)
 
#define ALLOC_AND_ZERO(s)   calloc(1,s)
 
#define FREEMEM(p)   free(p)
 
#define MEM_INIT(p, v, s)   memset((p),(v),(s))
 
#define MINMATCH   4
 
#define WILDCOPYLENGTH   8
 
#define LASTLITERALS   5 /* see ../doc/lz4_Block_format.md#parsing-restrictions */
 
#define MFLIMIT   12 /* see ../doc/lz4_Block_format.md#parsing-restrictions */
 
#define MATCH_SAFEGUARD_DISTANCE   ((2*WILDCOPYLENGTH) - MINMATCH) /* ensure it's possible to write 2 x wildcopyLength without overflowing output buffer */
 
#define FASTLOOP_SAFE_DISTANCE   64
 
#define KB   *(1 <<10)
 
#define MB   *(1 <<20)
 
#define GB   *(1U<<30)
 
#define LZ4_DISTANCE_ABSOLUTE_MAX   65535
 
#define ML_BITS   4
 
#define ML_MASK   ((1U<<ML_BITS)-1)
 
#define RUN_BITS   (8-ML_BITS)
 
#define RUN_MASK   ((1U<<RUN_BITS)-1)
 
#define assert(condition)   ((void)0)
 
#define LZ4_STATIC_ASSERT(c)   { enum { LZ4_static_assert = 1/(int)(!!(c)) }; } /* use after variable declarations */
 
#define DEBUGLOG(l, ...)   {} /* disabled */
 
#define LZ4_memcpy(dst, src, size)   memcpy(dst, src, size)
 
#define LZ4_FAST_DEC_LOOP   0
 
#define STEPSIZE   sizeof(reg_t)
 
#define HASH_UNIT   sizeof(reg_t)
 
#define MIN(a, b)   ( (a) < (b) ? (a) : (b) )
 

Typedefs

typedef unsigned char BYTE
 
typedef unsigned short U16
 
typedef unsigned int U32
 
typedef signed int S32
 
typedef unsigned long long U64
 
typedef size_t uptrval
 
typedef size_t reg_t
 

Enumerations

enum  limitedOutput_directive { notLimited = 0 , limitedOutput = 1 , fillOutput = 2 }
 
enum  tableType_t { clearedTable = 0 , byPtr , byU32 , byU16 }
 
enum  dict_directive { noDict = 0 , withPrefix64k , usingExtDict , usingDictCtx }
 
enum  dictIssue_directive { noDictIssue = 0 , dictSmall }
 
enum  endCondition_directive { endOnOutputSize = 0 , endOnInputSize = 1 }
 
enum  earlyEnd_directive { decode_full_block = 0 , partial_decode = 1 }
 
enum  variable_length_error { loop_error = -2 , initial_error = -1 , ok = 0 }
 

Functions

static int LZ4_isAligned (const void *ptr, size_t alignment)
 
static unsigned LZ4_isLittleEndian (void)
 
static U16 LZ4_read16 (const void *memPtr)
 
static U32 LZ4_read32 (const void *memPtr)
 
static reg_t LZ4_read_ARCH (const void *memPtr)
 
static void LZ4_write16 (void *memPtr, U16 value)
 
static void LZ4_write32 (void *memPtr, U32 value)
 
static U16 LZ4_readLE16 (const void *memPtr)
 
static void LZ4_writeLE16 (void *memPtr, U16 value)
 
LZ4_FORCE_INLINE void LZ4_wildCopy8 (void *dstPtr, const void *srcPtr, void *dstEnd)
 
static unsigned LZ4_NbCommonBytes (reg_t val)
 
LZ4_FORCE_INLINE unsigned LZ4_count (const BYTE *pIn, const BYTE *pMatch, const BYTE *pInLimit)
 
int LZ4_versionNumber (void)
 
const char * LZ4_versionString (void)
 
int LZ4_compressBound (int isize)
 
int LZ4_sizeofState (void)
 
int LZ4_compress_forceExtDict (LZ4_stream_t *LZ4_dict, const char *source, char *dest, int srcSize)
 
int LZ4_decompress_safe_forceExtDict (const char *source, char *dest, int compressedSize, int maxOutputSize, const void *dictStart, size_t dictSize)
 
LZ4_FORCE_INLINE U32 LZ4_hash4 (U32 sequence, tableType_t const tableType)
 
LZ4_FORCE_INLINE U32 LZ4_hash5 (U64 sequence, tableType_t const tableType)
 
LZ4_FORCE_INLINE U32 LZ4_hashPosition (const void *const p, tableType_t const tableType)
 
LZ4_FORCE_INLINE void LZ4_clearHash (U32 h, void *tableBase, tableType_t const tableType)
 
LZ4_FORCE_INLINE void LZ4_putIndexOnHash (U32 idx, U32 h, void *tableBase, tableType_t const tableType)
 
LZ4_FORCE_INLINE void LZ4_putPositionOnHash (const BYTE *p, U32 h, void *tableBase, tableType_t const tableType, const BYTE *srcBase)
 
LZ4_FORCE_INLINE void LZ4_putPosition (const BYTE *p, void *tableBase, tableType_t tableType, const BYTE *srcBase)
 
LZ4_FORCE_INLINE U32 LZ4_getIndexOnHash (U32 h, const void *tableBase, tableType_t tableType)
 
static const BYTELZ4_getPositionOnHash (U32 h, const void *tableBase, tableType_t tableType, const BYTE *srcBase)
 
LZ4_FORCE_INLINE const BYTELZ4_getPosition (const BYTE *p, const void *tableBase, tableType_t tableType, const BYTE *srcBase)
 
LZ4_FORCE_INLINE void LZ4_prepareTable (LZ4_stream_t_internal *const cctx, const int inputSize, const tableType_t tableType)
 
LZ4_FORCE_INLINE int LZ4_compress_generic_validated (LZ4_stream_t_internal *const cctx, const char *const source, char *const dest, const int inputSize, int *inputConsumed, const int maxOutputSize, const limitedOutput_directive outputDirective, const tableType_t tableType, const dict_directive dictDirective, const dictIssue_directive dictIssue, const int acceleration)
 
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, const int dstCapacity, const limitedOutput_directive outputDirective, const tableType_t tableType, const dict_directive dictDirective, const dictIssue_directive dictIssue, const int acceleration)
 
int LZ4_compress_fast_extState (void *state, const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration)
 
int LZ4_compress_fast_extState_fastReset (void *state, const char *src, char *dst, int srcSize, int dstCapacity, int acceleration)
 
int LZ4_compress_fast (const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration)
 
int LZ4_compress_default (const char *src, char *dst, int srcSize, int maxOutputSize)
 
static int LZ4_compress_destSize_extState (LZ4_stream_t *state, const char *src, char *dst, int *srcSizePtr, int targetDstSize)
 
int LZ4_compress_destSize (const char *src, char *dst, int *srcSizePtr, int targetDstSize)
 
LZ4_stream_tLZ4_createStream (void)
 
static size_t LZ4_stream_t_alignment (void)
 
LZ4_stream_tLZ4_initStream (void *buffer, size_t size)
 
void LZ4_resetStream (LZ4_stream_t *LZ4_stream)
 
void LZ4_resetStream_fast (LZ4_stream_t *ctx)
 
int LZ4_freeStream (LZ4_stream_t *LZ4_stream)
 
int LZ4_loadDict (LZ4_stream_t *LZ4_dict, const char *dictionary, int dictSize)
 
void LZ4_attach_dictionary (LZ4_stream_t *workingStream, const LZ4_stream_t *dictionaryStream)
 
static void LZ4_renormDictT (LZ4_stream_t_internal *LZ4_dict, int nextSize)
 
int LZ4_compress_fast_continue (LZ4_stream_t *LZ4_stream, const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration)
 
int LZ4_saveDict (LZ4_stream_t *LZ4_dict, char *safeBuffer, int dictSize)
 
LZ4_FORCE_INLINE unsigned read_variable_length (const BYTE **ip, const BYTE *lencheck, int loop_check, int initial_check, variable_length_error *error)
 
LZ4_FORCE_INLINE int LZ4_decompress_generic (const char *const src, char *const dst, int srcSize, int outputSize, endCondition_directive endOnInput, earlyEnd_directive partialDecoding, dict_directive dict, const BYTE *const lowPrefix, const BYTE *const dictStart, const size_t dictSize)
 
LZ4_FORCE_O2 int LZ4_decompress_safe (const char *source, char *dest, int compressedSize, int maxDecompressedSize)
 
LZ4_FORCE_O2 int LZ4_decompress_safe_partial (const char *src, char *dst, int compressedSize, int targetOutputSize, int dstCapacity)
 
LZ4_FORCE_O2 int LZ4_decompress_fast (const char *source, char *dest, int originalSize)
 
LZ4_FORCE_O2 int LZ4_decompress_safe_withPrefix64k (const char *source, char *dest, int compressedSize, int maxOutputSize)
 
int LZ4_decompress_fast_withPrefix64k (const char *source, char *dest, int originalSize)
 
static LZ4_FORCE_O2 int LZ4_decompress_safe_withSmallPrefix (const char *source, char *dest, int compressedSize, int maxOutputSize, size_t prefixSize)
 
static LZ4_FORCE_O2 int LZ4_decompress_fast_extDict (const char *source, char *dest, int originalSize, const void *dictStart, size_t dictSize)
 
LZ4_FORCE_INLINE int LZ4_decompress_safe_doubleDict (const char *source, char *dest, int compressedSize, int maxOutputSize, size_t prefixSize, const void *dictStart, size_t dictSize)
 
LZ4_FORCE_INLINE int LZ4_decompress_fast_doubleDict (const char *source, char *dest, int originalSize, size_t prefixSize, const void *dictStart, size_t dictSize)
 
LZ4_streamDecode_tLZ4_createStreamDecode (void)
 
int LZ4_freeStreamDecode (LZ4_streamDecode_t *LZ4_stream)
 
int LZ4_setStreamDecode (LZ4_streamDecode_t *LZ4_streamDecode, const char *dictionary, int dictSize)
 
int LZ4_decoderRingBufferSize (int maxBlockSize)
 
LZ4_FORCE_O2 int LZ4_decompress_safe_continue (LZ4_streamDecode_t *LZ4_streamDecode, const char *source, char *dest, int compressedSize, int maxOutputSize)
 
LZ4_FORCE_O2 int LZ4_decompress_fast_continue (LZ4_streamDecode_t *LZ4_streamDecode, const char *source, char *dest, int originalSize)
 
int LZ4_decompress_safe_usingDict (const char *source, char *dest, int compressedSize, int maxOutputSize, const char *dictStart, int dictSize)
 
int LZ4_decompress_fast_usingDict (const char *source, char *dest, int originalSize, const char *dictStart, int dictSize)
 
int LZ4_compress_limitedOutput (const char *source, char *dest, int inputSize, int maxOutputSize)
 
int LZ4_compress (const char *src, char *dest, int srcSize)
 
int LZ4_compress_limitedOutput_withState (void *state, const char *src, char *dst, int srcSize, int dstSize)
 
int LZ4_compress_withState (void *state, const char *src, char *dst, int srcSize)
 
int LZ4_compress_limitedOutput_continue (LZ4_stream_t *LZ4_stream, const char *src, char *dst, int srcSize, int dstCapacity)
 
int LZ4_compress_continue (LZ4_stream_t *LZ4_stream, const char *source, char *dest, int inputSize)
 
int LZ4_uncompress (const char *source, char *dest, int outputSize)
 
int LZ4_uncompress_unknownOutputSize (const char *source, char *dest, int isize, int maxOutputSize)
 
int LZ4_sizeofStreamState (void)
 
int LZ4_resetStreamState (void *state, char *inputBuffer)
 
void * LZ4_create (char *inputBuffer)
 
char * LZ4_slideInputBuffer (void *state)
 

Variables

static const int LZ4_minLength = (MFLIMIT+1)
 
static const unsigned inc32table [8] = {0, 1, 2, 1, 0, 4, 4, 4}
 
static const int dec64table [8] = {0, 0, 0, -1, -4, 1, 2, 3}
 
static const int LZ4_64Klimit = ((64 KB) + (MFLIMIT-1))
 
static const U32 LZ4_skipTrigger = 6
 

Macro Definition Documentation

◆ ALLOC

#define ALLOC (   s)    malloc(s)

Definition at line 202 of file lz4.c.

◆ ALLOC_AND_ZERO

#define ALLOC_AND_ZERO (   s)    calloc(1,s)

Definition at line 203 of file lz4.c.

◆ assert

#define assert (   condition)    ((void)0)

Definition at line 245 of file lz4.c.

◆ DEBUGLOG

#define DEBUGLOG (   l,
  ... 
)    {} /* disabled */

Definition at line 261 of file lz4.c.

◆ expect

#define expect (   expr,
  value 
)    (expr)

Definition at line 170 of file lz4.c.

◆ FASTLOOP_SAFE_DISTANCE

#define FASTLOOP_SAFE_DISTANCE   64

Definition at line 220 of file lz4.c.

◆ FREEMEM

#define FREEMEM (   p)    free(p)

Definition at line 204 of file lz4.c.

◆ GB

#define GB   *(1U<<30)

Definition at line 225 of file lz4.c.

◆ HASH_UNIT

#define HASH_UNIT   sizeof(reg_t)

Definition at line 1474 of file lz4.c.

◆ KB

#define KB   *(1 <<10)

Definition at line 223 of file lz4.c.

◆ LASTLITERALS

#define LASTLITERALS   5 /* see ../doc/lz4_Block_format.md#parsing-restrictions */

Definition at line 217 of file lz4.c.

◆ likely

#define likely (   expr)    expect((expr) != 0, 1)

Definition at line 174 of file lz4.c.

◆ LZ4_ACCELERATION_DEFAULT

#define LZ4_ACCELERATION_DEFAULT   1

Definition at line 51 of file lz4.c.

◆ LZ4_ACCELERATION_MAX

#define LZ4_ACCELERATION_MAX   65537

Definition at line 57 of file lz4.c.

◆ LZ4_ALIGN_TEST

#define LZ4_ALIGN_TEST   1

Definition at line 183 of file lz4.c.

◆ LZ4_DISABLE_DEPRECATE_WARNINGS

#define LZ4_DISABLE_DEPRECATE_WARNINGS   /* due to LZ4_decompress_safe_withPrefix64k */

Definition at line 113 of file lz4.c.

◆ LZ4_DISTANCE_ABSOLUTE_MAX

#define LZ4_DISTANCE_ABSOLUTE_MAX   65535

Definition at line 227 of file lz4.c.

◆ LZ4_FAST_DEC_LOOP

#define LZ4_FAST_DEC_LOOP   0

Definition at line 430 of file lz4.c.

◆ LZ4_FORCE_INLINE

#define LZ4_FORCE_INLINE   static

Definition at line 140 of file lz4.c.

◆ LZ4_FORCE_O2

#define LZ4_FORCE_O2

Definition at line 164 of file lz4.c.

◆ LZ4_HEAPMODE

#define LZ4_HEAPMODE   0

Definition at line 44 of file lz4.c.

◆ LZ4_memcpy

#define LZ4_memcpy (   dst,
  src,
  size 
)    memcpy(dst, src, size)

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.

Definition at line 322 of file lz4.c.

◆ LZ4_SRC_INCLUDED

#define LZ4_SRC_INCLUDED   1

Definition at line 105 of file lz4.c.

◆ LZ4_STATIC_ASSERT

#define LZ4_STATIC_ASSERT (   c)    { enum { LZ4_static_assert = 1/(int)(!!(c)) }; } /* use after variable declarations */

Definition at line 249 of file lz4.c.

◆ LZ4_STATIC_LINKING_ONLY [1/2]

#define LZ4_STATIC_LINKING_ONLY

Definition at line 116 of file lz4.c.

◆ LZ4_STATIC_LINKING_ONLY [2/2]

#define LZ4_STATIC_LINKING_ONLY   /* LZ4_DISTANCE_MAX */

Definition at line 116 of file lz4.c.

◆ MATCH_SAFEGUARD_DISTANCE

#define MATCH_SAFEGUARD_DISTANCE   ((2*WILDCOPYLENGTH) - MINMATCH) /* ensure it's possible to write 2 x wildcopyLength without overflowing output buffer */

Definition at line 219 of file lz4.c.

◆ MB

#define MB   *(1 <<20)

Definition at line 224 of file lz4.c.

◆ MEM_INIT

#define MEM_INIT (   p,
  v,
  s 
)    memset((p),(v),(s))

Definition at line 208 of file lz4.c.

◆ MFLIMIT

#define MFLIMIT   12 /* see ../doc/lz4_Block_format.md#parsing-restrictions */

Definition at line 218 of file lz4.c.

◆ MIN

#define MIN (   a,
  b 
)    ( (a) < (b) ? (a) : (b) )

Definition at line 1696 of file lz4.c.

◆ MINMATCH

#define MINMATCH   4

Definition at line 214 of file lz4.c.

◆ ML_BITS

#define ML_BITS   4

Definition at line 232 of file lz4.c.

◆ ML_MASK

#define ML_MASK   ((1U<<ML_BITS)-1)

Definition at line 233 of file lz4.c.

◆ RUN_BITS

#define RUN_BITS   (8-ML_BITS)

Definition at line 234 of file lz4.c.

◆ RUN_MASK

#define RUN_MASK   ((1U<<RUN_BITS)-1)

Definition at line 235 of file lz4.c.

◆ STEPSIZE

#define STEPSIZE   sizeof(reg_t)

Definition at line 601 of file lz4.c.

◆ unlikely

#define unlikely (   expr)    expect((expr) != 0, 0)

Definition at line 177 of file lz4.c.

◆ WILDCOPYLENGTH

#define WILDCOPYLENGTH   8

Definition at line 216 of file lz4.c.

Typedef Documentation

◆ BYTE

typedef unsigned char BYTE

Definition at line 286 of file lz4.c.

◆ reg_t

typedef size_t reg_t

Definition at line 297 of file lz4.c.

◆ S32

typedef signed int S32

Definition at line 289 of file lz4.c.

◆ U16

typedef unsigned short U16

Definition at line 287 of file lz4.c.

◆ U32

typedef unsigned int U32

Definition at line 288 of file lz4.c.

◆ U64

typedef unsigned long long U64

Definition at line 290 of file lz4.c.

◆ uptrval

typedef size_t uptrval

Definition at line 291 of file lz4.c.

Enumeration Type Documentation

◆ dict_directive

This enum distinguishes several different modes of accessing previous content in the stream.

  • noDict : There is no preceding content.
  • withPrefix64k : Table entries up to ctx->dictSize before the current blob blob being compressed are valid and refer to the preceding content (of length ctx->dictSize), which is available contiguously preceding in memory the content currently being compressed.
  • usingExtDict : Like withPrefix64k, but the preceding content is somewhere else in memory, starting at ctx->dictionary with length ctx->dictSize.
  • usingDictCtx : Like usingExtDict, but everything concerning the preceding content is in a separate context, pointed to by ctx->dictCtx. ctx->dictionary, ctx->dictSize, and table entries in the current context that refer to positions preceding the beginning of the current compression are ignored. Instead, ctx->dictCtx->dictionary and ctx->dictCtx ->dictSize describe the location and size of the preceding content, and matches are found by looking in the ctx ->dictCtx->hashTable.
Enumerator
noDict 
withPrefix64k 
usingExtDict 
usingDictCtx 

Definition at line 665 of file lz4.c.

dict_directive
Definition: lz4.c:665
@ noDict
Definition: lz4.c:665
@ withPrefix64k
Definition: lz4.c:665
@ usingExtDict
Definition: lz4.c:665
@ usingDictCtx
Definition: lz4.c:665

◆ dictIssue_directive

Enumerator
noDictIssue 
dictSmall 

Definition at line 666 of file lz4.c.

dictIssue_directive
Definition: lz4.c:666
@ noDictIssue
Definition: lz4.c:666
@ dictSmall
Definition: lz4.c:666

◆ earlyEnd_directive

Enumerator
decode_full_block 
partial_decode 

Definition at line 1693 of file lz4.c.

earlyEnd_directive
Definition: lz4.c:1693
@ partial_decode
Definition: lz4.c:1693
@ decode_full_block
Definition: lz4.c:1693

◆ endCondition_directive

Enumerator
endOnOutputSize 
endOnInputSize 

Definition at line 1692 of file lz4.c.

endCondition_directive
Definition: lz4.c:1692
@ endOnInputSize
Definition: lz4.c:1692
@ endOnOutputSize
Definition: lz4.c:1692

◆ limitedOutput_directive

Enumerator
notLimited 
limitedOutput 
fillOutput 

Definition at line 300 of file lz4.c.

300  {
301  notLimited = 0,
302  limitedOutput = 1,
303  fillOutput = 2
limitedOutput_directive
Definition: lz4.c:300
@ limitedOutput
Definition: lz4.c:302
@ fillOutput
Definition: lz4.c:303
@ notLimited
Definition: lz4.c:301

◆ tableType_t

Enumerator
clearedTable 
byPtr 
byU32 
byU16 

Definition at line 640 of file lz4.c.

tableType_t
Definition: lz4.c:640
@ clearedTable
Definition: lz4.c:640
@ byU16
Definition: lz4.c:640
@ byPtr
Definition: lz4.c:640
@ byU32
Definition: lz4.c:640

◆ variable_length_error

Enumerator
loop_error 
initial_error 
ok 

Definition at line 1706 of file lz4.c.

1706 { loop_error = -2, initial_error = -1, ok = 0 } variable_length_error;
variable_length_error
Definition: lz4.c:1706
@ loop_error
Definition: lz4.c:1706
@ initial_error
Definition: lz4.c:1706
@ ok
Definition: lz4.c:1706

Function Documentation

◆ LZ4_attach_dictionary()

void LZ4_attach_dictionary ( LZ4_stream_t workingStream,
const LZ4_stream_t dictionaryStream 
)

Definition at line 1517 of file lz4.c.

1517  {
1518  const LZ4_stream_t_internal* dictCtx = dictionaryStream == NULL ? NULL :
1519  &(dictionaryStream->internal_donotuse);
1520 
1521  DEBUGLOG(4, "LZ4_attach_dictionary (%p, %p, size %u)",
1522  workingStream, dictionaryStream,
1523  dictCtx != NULL ? dictCtx->dictSize : 0);
1524 
1525  if (dictCtx != NULL) {
1526  /* If the current offset is zero, we will never look in the
1527  * external dictionary context, since there is no value a table
1528  * entry can take that indicate a miss. In that case, we need
1529  * to bump the offset to something non-zero.
1530  */
1531  if (workingStream->internal_donotuse.currentOffset == 0) {
1532  workingStream->internal_donotuse.currentOffset = 64 KB;
1533  }
1534 
1535  /* Don't actually attach an empty dictionary.
1536  */
1537  if (dictCtx->dictSize == 0) {
1538  dictCtx = NULL;
1539  }
1540  }
1541  workingStream->internal_donotuse.dictCtx = dictCtx;
1542 }
#define NULL
Definition: cris-opc.c:27
#define KB
Definition: lz4.c:223
#define DEBUGLOG(l,...)
Definition: lz4.c:261
const LZ4_stream_t_internal * dictCtx
Definition: lz4.h:601
LZ4_u32 currentOffset
Definition: lz4.h:598
LZ4_u32 dictSize
Definition: lz4.h:602
LZ4_stream_t_internal internal_donotuse
Definition: lz4.h:627

References LZ4_stream_t_internal::currentOffset, DEBUGLOG, LZ4_stream_t_internal::dictCtx, LZ4_stream_t_internal::dictSize, LZ4_stream_u::internal_donotuse, KB, and NULL.

Referenced by FUZ_test(), LZ4_compressResetStream(), LZ4F_initStream(), and state_attachDictRoundTrip().

◆ LZ4_clearHash()

LZ4_FORCE_INLINE void LZ4_clearHash ( U32  h,
void *  tableBase,
tableType_t const  tableType 
)

Definition at line 724 of file lz4.c.

725 {
726  switch (tableType)
727  {
728  default: /* fallthrough */
729  case clearedTable: { /* illegal! */ assert(0); return; }
730  case byPtr: { const BYTE** hashTable = (const BYTE**)tableBase; hashTable[h] = NULL; return; }
731  case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = 0; return; }
732  case byU16: { U16* hashTable = (U16*) tableBase; hashTable[h] = 0; return; }
733  }
734 }
unsigned char BYTE
Definition: lz4.c:286
unsigned int U32
Definition: lz4.c:288
#define assert(condition)
Definition: lz4.c:245
unsigned short U16
Definition: lz4.c:287
#define h(i)
Definition: sha256.c:48

References assert, byPtr, byU16, byU32, clearedTable, h, and NULL.

Referenced by LZ4_compress_generic_validated().

◆ LZ4_compress()

int LZ4_compress ( const char *  src,
char *  dest,
int  srcSize 
)

Definition at line 2436 of file lz4.c.

2437 {
2439 }
lzma_index * src
Definition: index.h:567
int LZ4_compressBound(int isize)
Definition: lz4.c:674
int LZ4_compress_default(const char *src, char *dst, int srcSize, int maxOutputSize)
Definition: lz4.c:1373
char int srcSize
Definition: lz4.h:697
char * dest
Definition: lz4.h:697

References dest, LZ4_compress_default(), LZ4_compressBound(), src, and srcSize.

◆ LZ4_compress_continue()

int LZ4_compress_continue ( LZ4_stream_t LZ4_stream,
const char *  source,
char *  dest,
int  inputSize 
)

Definition at line 2452 of file lz4.c.

2453 {
2455 }
static LZ4_stream_t LZ4_stream
Definition: fullbench.c:169
int LZ4_compress_fast_continue(LZ4_stream_t *LZ4_stream, const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration)
Definition: lz4.c:1565
const char * source
Definition: lz4.h:699
const char char int inputSize
Definition: lz4.h:699

References dest, inputSize, LZ4_compress_fast_continue(), LZ4_compressBound(), LZ4_stream, and source.

◆ LZ4_compress_default()

int LZ4_compress_default ( const char *  src,
char *  dst,
int  srcSize,
int  dstCapacity 
)

LZ4_compress_default() : Compresses 'srcSize' bytes from buffer 'src' into already allocated 'dst' buffer of size 'dstCapacity'. Compression is guaranteed to succeed if 'dstCapacity' >= LZ4_compressBound(srcSize). It also runs faster, so it's a recommended setting. If the function cannot compress 'src' into a more limited 'dst' budget, compression stops immediately, and the function result is zero. In which case, 'dst' content is undefined (invalid). srcSize : max supported value is LZ4_MAX_INPUT_SIZE. dstCapacity : size of buffer 'dst' (which must be already allocated)

Returns
: the number of bytes written into buffer 'dst' (necessarily <= dstCapacity) or 0 if compression fails Note : This function is protected against buffer overflow scenarios (never writes outside 'dst' buffer, nor read outside 'source' buffer).

Definition at line 1373 of file lz4.c.

1374 {
1376 }
int LZ4_compress_fast(const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration)
Definition: lz4.c:1354
char int int maxOutputSize
Definition: lz4.h:698
char * dst
Definition: lz4.h:724

References dst, LZ4_compress_fast(), maxOutputSize, src, and srcSize.

Referenced by bench(), fullSpeedBench(), FUZ_test(), FUZ_unitTests(), LLVMFuzzerTestOneInput(), local_LZ4_compress_default_large(), local_LZ4_compress_default_small(), LZ4_compress(), LZ4_compress_limitedOutput(), and main().

◆ LZ4_compress_destSize()

int LZ4_compress_destSize ( const char *  src,
char *  dst,
int srcSizePtr,
int  targetDstSize 
)

LZ4_compress_destSize() : Reverse the logic : compresses as much data as possible from 'src' buffer into already allocated buffer 'dst', of size >= 'targetDestSize'. This function either compresses the entire 'src' content into 'dst' if it's large enough, or fill 'dst' buffer completely with as much data as possible from 'src'. note: acceleration parameter is fixed to "default".

*srcSizePtr : will be modified to indicate how many bytes where read from 'src' to fill 'dst'. New value is necessarily <= input value.

Returns
: 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

Definition at line 1399 of file lz4.c.

1400 {
1401 #if (LZ4_HEAPMODE)
1402  LZ4_stream_t* ctx = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */
1403  if (ctx == NULL) return 0;
1404 #else
1405  LZ4_stream_t ctxBody;
1406  LZ4_stream_t* ctx = &ctxBody;
1407 #endif
1408 
1409  int result = LZ4_compress_destSize_extState(ctx, src, dst, srcSizePtr, targetDstSize);
1410 
1411 #if (LZ4_HEAPMODE)
1412  FREEMEM(ctx);
1413 #endif
1414  return result;
1415 }
#define ALLOC(s)
Definition: lz4.c:202
#define FREEMEM(p)
Definition: lz4.c:204
static int LZ4_compress_destSize_extState(LZ4_stream_t *state, const char *src, char *dst, int *srcSizePtr, int targetDstSize)
Definition: lz4.c:1382

References ALLOC, dst, FREEMEM, LZ4_compress_destSize_extState(), NULL, and src.

Referenced by FUZ_test(), FUZ_unitTests(), LLVMFuzzerTestOneInput(), and local_LZ4_compress_destSize().

◆ LZ4_compress_destSize_extState()

static int LZ4_compress_destSize_extState ( LZ4_stream_t state,
const char *  src,
char *  dst,
int srcSizePtr,
int  targetDstSize 
)
static

Definition at line 1382 of file lz4.c.

1383 {
1384  void* const s = LZ4_initStream(state, sizeof (*state));
1385  assert(s != NULL); (void)s;
1386 
1387  if (targetDstSize >= LZ4_compressBound(*srcSizePtr)) { /* compression success is guaranteed */
1388  return LZ4_compress_fast_extState(state, src, dst, *srcSizePtr, targetDstSize, 1);
1389  } else {
1390  if (*srcSizePtr < LZ4_64Klimit) {
1391  return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, byU16, noDict, noDictIssue, 1);
1392  } else {
1393  tableType_t const addrMode = ((sizeof(void*)==4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32;
1394  return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, addrMode, noDict, noDictIssue, 1);
1395  } }
1396 }
int LZ4_compress_fast_extState(void *state, const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration)
Definition: lz4.c:1284
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, const int dstCapacity, const limitedOutput_directive outputDirective, const tableType_t tableType, const dict_directive dictDirective, const dictIssue_directive dictIssue, const int acceleration)
Definition: lz4.c:1246
LZ4_stream_t * LZ4_initStream(void *buffer, size_t size)
Definition: lz4.c:1443
size_t uptrval
Definition: lz4.c:291
static const int LZ4_64Klimit
Definition: lz4.c:633
static RzSocket * s
Definition: rtr.c:28
Definition: dis.h:43

References assert, byPtr, byU16, byU32, dst, fillOutput, LZ4_64Klimit, LZ4_compress_fast_extState(), LZ4_compress_generic(), LZ4_compressBound(), LZ4_initStream(), noDict, noDictIssue, NULL, s, and src.

Referenced by LZ4_compress_destSize().

◆ LZ4_compress_fast()

int LZ4_compress_fast ( const char *  src,
char *  dst,
int  srcSize,
int  dstCapacity,
int  acceleration 
)

LZ4_compress_fast() : Same as LZ4_compress_default(), but allows selection of "acceleration" factor. 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 LZ4_ACCELERATION_DEFAULT (currently == 1, see lz4.c). Values > LZ4_ACCELERATION_MAX will be replaced by LZ4_ACCELERATION_MAX (currently == 65537, see lz4.c).

Definition at line 1354 of file lz4.c.

1355 {
1356  int result;
1357 #if (LZ4_HEAPMODE)
1358  LZ4_stream_t* ctxPtr = ALLOC(sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */
1359  if (ctxPtr == NULL) return 0;
1360 #else
1361  LZ4_stream_t ctx;
1362  LZ4_stream_t* const ctxPtr = &ctx;
1363 #endif
1364  result = LZ4_compress_fast_extState(ctxPtr, source, dest, inputSize, maxOutputSize, acceleration);
1365 
1366 #if (LZ4_HEAPMODE)
1367  FREEMEM(ctxPtr);
1368 #endif
1369  return result;
1370 }

References ALLOC, dest, FREEMEM, inputSize, LZ4_compress_fast_extState(), maxOutputSize, NULL, and source.

Referenced by bench(), local_LZ4_compress_fast0(), local_LZ4_compress_fast1(), local_LZ4_compress_fast17(), local_LZ4_compress_fast2(), LZ4_compress_default(), LZ4_compressBlockNoStream(), LZ4IO_LZ4_compress(), main(), and roundTripTest().

◆ LZ4_compress_fast_continue()

int LZ4_compress_fast_continue ( LZ4_stream_t streamPtr,
const char *  src,
char *  dst,
int  srcSize,
int  dstCapacity,
int  acceleration 
)

LZ4_compress_fast_continue() : Compress 'src' content using data from previously compressed blocks, for better compression ratio. 'dst' buffer must be already allocated. If dstCapacity >= LZ4_compressBound(srcSize), compression is guaranteed to succeed, and runs faster.

Returns
: size of compressed block or 0 if there is an error (typically, cannot fit into 'dst').

Note 1 : Each invocation to LZ4_compress_fast_continue() generates a new block. Each block has precise boundaries. Each block must be decompressed separately, calling LZ4_decompress_*() with relevant metadata. It's not possible to append blocks together and expect a single invocation of LZ4_decompress_*() to decompress them together.

Note 2 : The previous 64KB of source data is assumed to remain present, unmodified, at same address in memory !

Note 3 : When input is structured as a double-buffer, each buffer can have any size, including < 64 KB. Make sure that buffers are separated, by at least one byte. This construction ensures that each block only depends on previous block.

Note 4 : If input buffer is a ring-buffer, it can have any size, including < 64 KB.

Note 5 : After an error, the stream status is undefined (invalid), it can only be reset or freed.

Definition at line 1565 of file lz4.c.

1569 {
1570  const tableType_t tableType = byU32;
1572  const BYTE* dictEnd = streamPtr->dictionary + streamPtr->dictSize;
1573 
1574  DEBUGLOG(5, "LZ4_compress_fast_continue (inputSize=%i)", inputSize);
1575 
1576  LZ4_renormDictT(streamPtr, inputSize); /* avoid index overflow */
1577  if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT;
1578  if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX;
1579 
1580  /* invalidate tiny dictionaries */
1581  if ( (streamPtr->dictSize-1 < 4-1) /* intentional underflow */
1582  && (dictEnd != (const BYTE*)source) ) {
1583  DEBUGLOG(5, "LZ4_compress_fast_continue: dictSize(%u) at addr:%p is too small", streamPtr->dictSize, streamPtr->dictionary);
1584  streamPtr->dictSize = 0;
1585  streamPtr->dictionary = (const BYTE*)source;
1586  dictEnd = (const BYTE*)source;
1587  }
1588 
1589  /* Check overlapping input/dictionary space */
1590  { const BYTE* sourceEnd = (const BYTE*) source + inputSize;
1591  if ((sourceEnd > streamPtr->dictionary) && (sourceEnd < dictEnd)) {
1592  streamPtr->dictSize = (U32)(dictEnd - sourceEnd);
1593  if (streamPtr->dictSize > 64 KB) streamPtr->dictSize = 64 KB;
1594  if (streamPtr->dictSize < 4) streamPtr->dictSize = 0;
1595  streamPtr->dictionary = dictEnd - streamPtr->dictSize;
1596  }
1597  }
1598 
1599  /* prefix mode : source data follows dictionary */
1600  if (dictEnd == (const BYTE*)source) {
1601  if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset))
1602  return LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, withPrefix64k, dictSmall, acceleration);
1603  else
1604  return LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, withPrefix64k, noDictIssue, acceleration);
1605  }
1606 
1607  /* external dictionary mode */
1608  { int result;
1609  if (streamPtr->dictCtx) {
1610  /* We depend here on the fact that dictCtx'es (produced by
1611  * LZ4_loadDict) guarantee that their tables contain no references
1612  * to offsets between dictCtx->currentOffset - 64 KB and
1613  * dictCtx->currentOffset - dictCtx->dictSize. This makes it safe
1614  * to use noDictIssue even when the dict isn't a full 64 KB.
1615  */
1616  if (inputSize > 4 KB) {
1617  /* For compressing large blobs, it is faster to pay the setup
1618  * cost to copy the dictionary's tables into the active context,
1619  * so that the compression loop is only looking into one table.
1620  */
1621  LZ4_memcpy(streamPtr, streamPtr->dictCtx, sizeof(*streamPtr));
1622  result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, noDictIssue, acceleration);
1623  } else {
1624  result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingDictCtx, noDictIssue, acceleration);
1625  }
1626  } else {
1627  if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) {
1628  result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, dictSmall, acceleration);
1629  } else {
1630  result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, noDictIssue, acceleration);
1631  }
1632  }
1633  streamPtr->dictionary = (const BYTE*)source;
1634  streamPtr->dictSize = (U32)inputSize;
1635  return result;
1636  }
1637 }
#define LZ4_memcpy(dst, src, size)
Definition: lz4.c:322
#define LZ4_ACCELERATION_MAX
Definition: lz4.c:57
#define LZ4_ACCELERATION_DEFAULT
Definition: lz4.c:51
static void LZ4_renormDictT(LZ4_stream_t_internal *LZ4_dict, int nextSize)
Definition: lz4.c:1545
const LZ4_byte * dictionary
Definition: lz4.h:600

References byU32, LZ4_stream_t_internal::currentOffset, DEBUGLOG, dest, LZ4_stream_t_internal::dictCtx, LZ4_stream_t_internal::dictionary, LZ4_stream_t_internal::dictSize, dictSmall, inputSize, LZ4_stream_u::internal_donotuse, KB, limitedOutput, LZ4_ACCELERATION_DEFAULT, LZ4_ACCELERATION_MAX, LZ4_compress_generic(), LZ4_memcpy, LZ4_renormDictT(), LZ4_stream, maxOutputSize, noDictIssue, NULL, source, usingDictCtx, usingExtDict, and withPrefix64k.

Referenced by FUZ_test(), FUZ_unitTests(), local_LZ4_compress_fast_continue0(), LZ4_compress_continue(), LZ4_compress_limitedOutput_continue(), LZ4_compressBlockStream(), LZ4F_compressBlock(), LZ4F_compressBlock_continue(), state_extDictRoundTrip(), state_prefixRoundTrip(), and test_compress().

◆ LZ4_compress_fast_extState()

int LZ4_compress_fast_extState ( void *  state,
const char *  source,
char *  dest,
int  inputSize,
int  maxOutputSize,
int  acceleration 
)

Definition at line 1284 of file lz4.c.

1285 {
1286  LZ4_stream_t_internal* const ctx = & LZ4_initStream(state, sizeof(LZ4_stream_t)) -> internal_donotuse;
1287  assert(ctx != NULL);
1288  if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT;
1289  if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX;
1291  if (inputSize < LZ4_64Klimit) {
1293  } else {
1294  const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)source > LZ4_DISTANCE_MAX)) ? byPtr : byU32;
1295  return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration);
1296  }
1297  } else {
1298  if (inputSize < LZ4_64Klimit) {
1300  } else {
1301  const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)source > LZ4_DISTANCE_MAX)) ? byPtr : byU32;
1303  }
1304  }
1305 }

References assert, byPtr, byU16, byU32, dest, inputSize, limitedOutput, LZ4_64Klimit, LZ4_ACCELERATION_DEFAULT, LZ4_ACCELERATION_MAX, LZ4_compress_generic(), LZ4_compressBound(), LZ4_initStream(), maxOutputSize, noDict, noDictIssue, notLimited, NULL, and source.

Referenced by bench(), FUZ_test(), local_LZ4_compress_fast_extState0(), LZ4_compress_destSize_extState(), LZ4_compress_fast(), LZ4_compress_limitedOutput_withState(), LZ4_compress_withState(), and main().

◆ LZ4_compress_fast_extState_fastReset()

int LZ4_compress_fast_extState_fastReset ( void *  state,
const char *  src,
char *  dst,
int  srcSize,
int  dstCapacity,
int  acceleration 
)

LZ4_compress_fast_extState_fastReset() : A variant of LZ4_compress_fast_extState().

Using this variant avoids an expensive initialization step. It is only safe to call if the state buffer is known to be correctly initialized already (see comment in lz4.h on LZ4_resetStream_fast() for a definition of "correctly initialized").

Definition at line 1316 of file lz4.c.

1317 {
1318  LZ4_stream_t_internal* ctx = &((LZ4_stream_t*)state)->internal_donotuse;
1319  if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT;
1320  if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX;
1321 
1322  if (dstCapacity >= LZ4_compressBound(srcSize)) {
1323  if (srcSize < LZ4_64Klimit) {
1324  const tableType_t tableType = byU16;
1325  LZ4_prepareTable(ctx, srcSize, tableType);
1326  if (ctx->currentOffset) {
1327  return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, dictSmall, acceleration);
1328  } else {
1329  return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration);
1330  }
1331  } else {
1332  const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32;
1333  LZ4_prepareTable(ctx, srcSize, tableType);
1334  return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration);
1335  }
1336  } else {
1337  if (srcSize < LZ4_64Klimit) {
1338  const tableType_t tableType = byU16;
1339  LZ4_prepareTable(ctx, srcSize, tableType);
1340  if (ctx->currentOffset) {
1341  return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, dictSmall, acceleration);
1342  } else {
1343  return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, noDictIssue, acceleration);
1344  }
1345  } else {
1346  const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32;
1347  LZ4_prepareTable(ctx, srcSize, tableType);
1348  return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, noDictIssue, acceleration);
1349  }
1350  }
1351 }
LZ4_FORCE_INLINE void LZ4_prepareTable(LZ4_stream_t_internal *const cctx, const int inputSize, const tableType_t tableType)
Definition: lz4.c:806

References byPtr, byU16, byU32, dictSmall, dst, limitedOutput, LZ4_64Klimit, LZ4_ACCELERATION_DEFAULT, LZ4_ACCELERATION_MAX, LZ4_compress_generic(), LZ4_compressBound(), LZ4_prepareTable(), noDict, noDictIssue, notLimited, NULL, src, and srcSize.

Referenced by FUZ_test(), and LZ4F_compressBlock().

◆ LZ4_compress_forceExtDict()

int LZ4_compress_forceExtDict ( LZ4_stream_t LZ4_dict,
const char *  source,
char *  dest,
int  srcSize 
)

Definition at line 1641 of file lz4.c.

1642 {
1643  LZ4_stream_t_internal* streamPtr = &LZ4_dict->internal_donotuse;
1644  int result;
1645 
1646  LZ4_renormDictT(streamPtr, srcSize);
1647 
1648  if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) {
1650  } else {
1652  }
1653 
1654  streamPtr->dictionary = (const BYTE*)source;
1655  streamPtr->dictSize = (U32)srcSize;
1656 
1657  return result;
1658 }

References byU32, LZ4_stream_t_internal::currentOffset, dest, LZ4_stream_t_internal::dictionary, LZ4_stream_t_internal::dictSize, dictSmall, LZ4_stream_u::internal_donotuse, KB, LZ4_compress_generic(), LZ4_renormDictT(), noDictIssue, notLimited, NULL, source, srcSize, and usingExtDict.

Referenced by local_LZ4_compress_forceDict().

◆ LZ4_compress_generic()

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,
const int  dstCapacity,
const limitedOutput_directive  outputDirective,
const tableType_t  tableType,
const dict_directive  dictDirective,
const dictIssue_directive  dictIssue,
const int  acceleration 
)

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

Definition at line 1246 of file lz4.c.

1258 {
1259  DEBUGLOG(5, "LZ4_compress_generic: srcSize=%i, dstCapacity=%i",
1260  srcSize, dstCapacity);
1261 
1262  if ((U32)srcSize > (U32)LZ4_MAX_INPUT_SIZE) { return 0; } /* Unsupported srcSize, too large (or negative) */
1263  if (srcSize == 0) { /* src == NULL supported if srcSize == 0 */
1264  if (outputDirective != notLimited && dstCapacity <= 0) return 0; /* no output, can't write anything */
1265  DEBUGLOG(5, "Generating an empty block");
1266  assert(outputDirective == notLimited || dstCapacity >= 1);
1267  assert(dst != NULL);
1268  dst[0] = 0;
1269  if (outputDirective == fillOutput) {
1270  assert (inputConsumed != NULL);
1271  *inputConsumed = 0;
1272  }
1273  return 1;
1274  }
1275  assert(src != NULL);
1276 
1278  inputConsumed, /* only written into if outputDirective == fillOutput */
1279  dstCapacity, outputDirective,
1280  tableType, dictDirective, dictIssue, acceleration);
1281 }
LZ4_FORCE_INLINE int LZ4_compress_generic_validated(LZ4_stream_t_internal *const cctx, const char *const source, char *const dest, const int inputSize, int *inputConsumed, const int maxOutputSize, const limitedOutput_directive outputDirective, const tableType_t tableType, const dict_directive dictDirective, const dictIssue_directive dictIssue, const int acceleration)
Definition: lz4.c:851
#define LZ4_MAX_INPUT_SIZE
Definition: lz4.h:170

References assert, DEBUGLOG, dst, fillOutput, LZ4_compress_generic_validated(), LZ4_MAX_INPUT_SIZE, notLimited, NULL, src, and srcSize.

Referenced by LZ4_compress_destSize_extState(), LZ4_compress_fast_continue(), LZ4_compress_fast_extState(), LZ4_compress_fast_extState_fastReset(), and LZ4_compress_forceExtDict().

◆ LZ4_compress_generic_validated()

LZ4_FORCE_INLINE int LZ4_compress_generic_validated ( LZ4_stream_t_internal *const  cctx,
const char *const  source,
char *const  dest,
const int  inputSize,
int inputConsumed,
const int  maxOutputSize,
const limitedOutput_directive  outputDirective,
const tableType_t  tableType,
const dict_directive  dictDirective,
const dictIssue_directive  dictIssue,
const int  acceleration 
)

LZ4_compress_generic() : inlined, to ensure branches are decided at compilation time. Presumed already validated at this stage:

  • source != NULL
  • inputSize > 0

Definition at line 851 of file lz4.c.

863 {
864  int result;
865  const BYTE* ip = (const BYTE*) source;
866 
867  U32 const startIndex = cctx->currentOffset;
868  const BYTE* base = (const BYTE*) source - startIndex;
869  const BYTE* lowLimit;
870 
871  const LZ4_stream_t_internal* dictCtx = (const LZ4_stream_t_internal*) cctx->dictCtx;
872  const BYTE* const dictionary =
873  dictDirective == usingDictCtx ? dictCtx->dictionary : cctx->dictionary;
874  const U32 dictSize =
875  dictDirective == usingDictCtx ? dictCtx->dictSize : cctx->dictSize;
876  const U32 dictDelta = (dictDirective == usingDictCtx) ? startIndex - dictCtx->currentOffset : 0; /* make indexes in dictCtx comparable with index in current context */
877 
878  int const maybe_extMem = (dictDirective == usingExtDict) || (dictDirective == usingDictCtx);
879  U32 const prefixIdxLimit = startIndex - dictSize; /* used when dictDirective == dictSmall */
880  const BYTE* const dictEnd = dictionary ? dictionary + dictSize : dictionary;
881  const BYTE* anchor = (const BYTE*) source;
882  const BYTE* const iend = ip + inputSize;
883  const BYTE* const mflimitPlusOne = iend - MFLIMIT + 1;
884  const BYTE* const matchlimit = iend - LASTLITERALS;
885 
886  /* the dictCtx currentOffset is indexed on the start of the dictionary,
887  * while a dictionary in the current context precedes the currentOffset */
888  const BYTE* dictBase = !dictionary ? NULL : (dictDirective == usingDictCtx) ?
889  dictionary + dictSize - dictCtx->currentOffset :
890  dictionary + dictSize - startIndex;
891 
892  BYTE* op = (BYTE*) dest;
893  BYTE* const olimit = op + maxOutputSize;
894 
895  U32 offset = 0;
896  U32 forwardH;
897 
898  DEBUGLOG(5, "LZ4_compress_generic_validated: srcSize=%i, tableType=%u", inputSize, tableType);
899  assert(ip != NULL);
900  /* If init conditions are not met, we don't have to mark stream
901  * as having dirty context, since no action was taken yet */
902  if (outputDirective == fillOutput && maxOutputSize < 1) { return 0; } /* Impossible to store anything */
903  if ((tableType == byU16) && (inputSize>=LZ4_64Klimit)) { return 0; } /* Size too large (not within 64K limit) */
904  if (tableType==byPtr) assert(dictDirective==noDict); /* only supported use case with byPtr */
905  assert(acceleration >= 1);
906 
907  lowLimit = (const BYTE*)source - (dictDirective == withPrefix64k ? dictSize : 0);
908 
909  /* Update context state */
910  if (dictDirective == usingDictCtx) {
911  /* Subsequent linked blocks can't use the dictionary. */
912  /* Instead, they use the block we just compressed. */
913  cctx->dictCtx = NULL;
914  cctx->dictSize = (U32)inputSize;
915  } else {
916  cctx->dictSize += (U32)inputSize;
917  }
918  cctx->currentOffset += (U32)inputSize;
919  cctx->tableType = (U32)tableType;
920 
921  if (inputSize<LZ4_minLength) goto _last_literals; /* Input too small, no compression (all literals) */
922 
923  /* First Byte */
924  LZ4_putPosition(ip, cctx->hashTable, tableType, base);
925  ip++; forwardH = LZ4_hashPosition(ip, tableType);
926 
927  /* Main Loop */
928  for ( ; ; ) {
929  const BYTE* match;
930  BYTE* token;
931  const BYTE* filledIp;
932 
933  /* Find a match */
934  if (tableType == byPtr) {
935  const BYTE* forwardIp = ip;
936  int step = 1;
937  int searchMatchNb = acceleration << LZ4_skipTrigger;
938  do {
939  U32 const h = forwardH;
940  ip = forwardIp;
941  forwardIp += step;
942  step = (searchMatchNb++ >> LZ4_skipTrigger);
943 
944  if (unlikely(forwardIp > mflimitPlusOne)) goto _last_literals;
945  assert(ip < mflimitPlusOne);
946 
947  match = LZ4_getPositionOnHash(h, cctx->hashTable, tableType, base);
948  forwardH = LZ4_hashPosition(forwardIp, tableType);
949  LZ4_putPositionOnHash(ip, h, cctx->hashTable, tableType, base);
950 
951  } while ( (match+LZ4_DISTANCE_MAX < ip)
952  || (LZ4_read32(match) != LZ4_read32(ip)) );
953 
954  } else { /* byU32, byU16 */
955 
956  const BYTE* forwardIp = ip;
957  int step = 1;
958  int searchMatchNb = acceleration << LZ4_skipTrigger;
959  do {
960  U32 const h = forwardH;
961  U32 const current = (U32)(forwardIp - base);
962  U32 matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType);
963  assert(matchIndex <= current);
964  assert(forwardIp - base < (ptrdiff_t)(2 GB - 1));
965  ip = forwardIp;
966  forwardIp += step;
967  step = (searchMatchNb++ >> LZ4_skipTrigger);
968 
969  if (unlikely(forwardIp > mflimitPlusOne)) goto _last_literals;
970  assert(ip < mflimitPlusOne);
971 
972  if (dictDirective == usingDictCtx) {
973  if (matchIndex < startIndex) {
974  /* there was no match, try the dictionary */
975  assert(tableType == byU32);
976  matchIndex = LZ4_getIndexOnHash(h, dictCtx->hashTable, byU32);
977  match = dictBase + matchIndex;
978  matchIndex += dictDelta; /* make dictCtx index comparable with current context */
979  lowLimit = dictionary;
980  } else {
981  match = base + matchIndex;
982  lowLimit = (const BYTE*)source;
983  }
984  } else if (dictDirective==usingExtDict) {
985  if (matchIndex < startIndex) {
986  DEBUGLOG(7, "extDict candidate: matchIndex=%5u < startIndex=%5u", matchIndex, startIndex);
987  assert(startIndex - matchIndex >= MINMATCH);
988  match = dictBase + matchIndex;
989  lowLimit = dictionary;
990  } else {
991  match = base + matchIndex;
992  lowLimit = (const BYTE*)source;
993  }
994  } else { /* single continuous memory segment */
995  match = base + matchIndex;
996  }
997  forwardH = LZ4_hashPosition(forwardIp, tableType);
998  LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType);
999 
1000  DEBUGLOG(7, "candidate at pos=%u (offset=%u \n", matchIndex, current - matchIndex);
1001  if ((dictIssue == dictSmall) && (matchIndex < prefixIdxLimit)) { continue; } /* match outside of valid area */
1002  assert(matchIndex < current);
1003  if ( ((tableType != byU16) || (LZ4_DISTANCE_MAX < LZ4_DISTANCE_ABSOLUTE_MAX))
1004  && (matchIndex+LZ4_DISTANCE_MAX < current)) {
1005  continue;
1006  } /* too far */
1007  assert((current - matchIndex) <= LZ4_DISTANCE_MAX); /* match now expected within distance */
1008 
1009  if (LZ4_read32(match) == LZ4_read32(ip)) {
1010  if (maybe_extMem) offset = current - matchIndex;
1011  break; /* match found */
1012  }
1013 
1014  } while(1);
1015  }
1016 
1017  /* Catch up */
1018  filledIp = ip;
1019  while (((ip>anchor) & (match > lowLimit)) && (unlikely(ip[-1]==match[-1]))) { ip--; match--; }
1020 
1021  /* Encode Literals */
1022  { unsigned const litLength = (unsigned)(ip - anchor);
1023  token = op++;
1024  if ((outputDirective == limitedOutput) && /* Check output buffer overflow */
1025  (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit)) ) {
1026  return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */
1027  }
1028  if ((outputDirective == fillOutput) &&
1029  (unlikely(op + (litLength+240)/255 /* litlen */ + litLength /* literals */ + 2 /* offset */ + 1 /* token */ + MFLIMIT - MINMATCH /* min last literals so last match is <= end - MFLIMIT */ > olimit))) {
1030  op--;
1031  goto _last_literals;
1032  }
1033  if (litLength >= RUN_MASK) {
1034  int len = (int)(litLength - RUN_MASK);
1035  *token = (RUN_MASK<<ML_BITS);
1036  for(; len >= 255 ; len-=255) *op++ = 255;
1037  *op++ = (BYTE)len;
1038  }
1039  else *token = (BYTE)(litLength<<ML_BITS);
1040 
1041  /* Copy Literals */
1042  LZ4_wildCopy8(op, anchor, op+litLength);
1043  op+=litLength;
1044  DEBUGLOG(6, "seq.start:%i, literals=%u, match.start:%i",
1045  (int)(anchor-(const BYTE*)source), litLength, (int)(ip-(const BYTE*)source));
1046  }
1047 
1048 _next_match:
1049  /* at this stage, the following variables must be correctly set :
1050  * - ip : at start of LZ operation
1051  * - match : at start of previous pattern occurence; can be within current prefix, or within extDict
1052  * - offset : if maybe_ext_memSegment==1 (constant)
1053  * - lowLimit : must be == dictionary to mean "match is within extDict"; must be == source otherwise
1054  * - token and *token : position to write 4-bits for match length; higher 4-bits for literal length supposed already written
1055  */
1056 
1057  if ((outputDirective == fillOutput) &&
1058  (op + 2 /* offset */ + 1 /* token */ + MFLIMIT - MINMATCH /* min last literals so last match is <= end - MFLIMIT */ > olimit)) {
1059  /* the match was too close to the end, rewind and go to last literals */
1060  op = token;
1061  goto _last_literals;
1062  }
1063 
1064  /* Encode Offset */
1065  if (maybe_extMem) { /* static test */
1066  DEBUGLOG(6, " with offset=%u (ext if > %i)", offset, (int)(ip - (const BYTE*)source));
1068  LZ4_writeLE16(op, (U16)offset); op+=2;
1069  } else {
1070  DEBUGLOG(6, " with offset=%u (same segment)", (U32)(ip - match));
1071  assert(ip-match <= LZ4_DISTANCE_MAX);
1072  LZ4_writeLE16(op, (U16)(ip - match)); op+=2;
1073  }
1074 
1075  /* Encode MatchLength */
1076  { unsigned matchCode;
1077 
1078  if ( (dictDirective==usingExtDict || dictDirective==usingDictCtx)
1079  && (lowLimit==dictionary) /* match within extDict */ ) {
1080  const BYTE* limit = ip + (dictEnd-match);
1081  assert(dictEnd > match);
1082  if (limit > matchlimit) limit = matchlimit;
1083  matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, limit);
1084  ip += (size_t)matchCode + MINMATCH;
1085  if (ip==limit) {
1086  unsigned const more = LZ4_count(limit, (const BYTE*)source, matchlimit);
1087  matchCode += more;
1088  ip += more;
1089  }
1090  DEBUGLOG(6, " with matchLength=%u starting in extDict", matchCode+MINMATCH);
1091  } else {
1092  matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit);
1093  ip += (size_t)matchCode + MINMATCH;
1094  DEBUGLOG(6, " with matchLength=%u", matchCode+MINMATCH);
1095  }
1096 
1097  if ((outputDirective) && /* Check output buffer overflow */
1098  (unlikely(op + (1 + LASTLITERALS) + (matchCode+240)/255 > olimit)) ) {
1099  if (outputDirective == fillOutput) {
1100  /* Match description too long : reduce it */
1101  U32 newMatchCode = 15 /* in token */ - 1 /* to avoid needing a zero byte */ + ((U32)(olimit - op) - 1 - LASTLITERALS) * 255;
1102  ip -= matchCode - newMatchCode;
1103  assert(newMatchCode < matchCode);
1104  matchCode = newMatchCode;
1105  if (unlikely(ip <= filledIp)) {
1106  /* We have already filled up to filledIp so if ip ends up less than filledIp
1107  * we have positions in the hash table beyond the current position. This is
1108  * a problem if we reuse the hash table. So we have to remove these positions
1109  * from the hash table.
1110  */
1111  const BYTE* ptr;
1112  DEBUGLOG(5, "Clearing %u positions", (U32)(filledIp - ip));
1113  for (ptr = ip; ptr <= filledIp; ++ptr) {
1114  U32 const h = LZ4_hashPosition(ptr, tableType);
1115  LZ4_clearHash(h, cctx->hashTable, tableType);
1116  }
1117  }
1118  } else {
1119  assert(outputDirective == limitedOutput);
1120  return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */
1121  }
1122  }
1123  if (matchCode >= ML_MASK) {
1124  *token += ML_MASK;
1125  matchCode -= ML_MASK;
1126  LZ4_write32(op, 0xFFFFFFFF);
1127  while (matchCode >= 4*255) {
1128  op+=4;
1129  LZ4_write32(op, 0xFFFFFFFF);
1130  matchCode -= 4*255;
1131  }
1132  op += matchCode / 255;
1133  *op++ = (BYTE)(matchCode % 255);
1134  } else
1135  *token += (BYTE)(matchCode);
1136  }
1137  /* Ensure we have enough space for the last literals. */
1138  assert(!(outputDirective == fillOutput && op + 1 + LASTLITERALS > olimit));
1139 
1140  anchor = ip;
1141 
1142  /* Test end of chunk */
1143  if (ip >= mflimitPlusOne) break;
1144 
1145  /* Fill table */
1146  LZ4_putPosition(ip-2, cctx->hashTable, tableType, base);
1147 
1148  /* Test next position */
1149  if (tableType == byPtr) {
1150 
1151  match = LZ4_getPosition(ip, cctx->hashTable, tableType, base);
1152  LZ4_putPosition(ip, cctx->hashTable, tableType, base);
1153  if ( (match+LZ4_DISTANCE_MAX >= ip)
1154  && (LZ4_read32(match) == LZ4_read32(ip)) )
1155  { token=op++; *token=0; goto _next_match; }
1156 
1157  } else { /* byU32, byU16 */
1158 
1159  U32 const h = LZ4_hashPosition(ip, tableType);
1160  U32 const current = (U32)(ip-base);
1161  U32 matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType);
1162  assert(matchIndex < current);
1163  if (dictDirective == usingDictCtx) {
1164  if (matchIndex < startIndex) {
1165  /* there was no match, try the dictionary */
1166  matchIndex = LZ4_getIndexOnHash(h, dictCtx->hashTable, byU32);
1167  match = dictBase + matchIndex;
1168  lowLimit = dictionary; /* required for match length counter */
1169  matchIndex += dictDelta;
1170  } else {
1171  match = base + matchIndex;
1172  lowLimit = (const BYTE*)source; /* required for match length counter */
1173  }
1174  } else if (dictDirective==usingExtDict) {
1175  if (matchIndex < startIndex) {
1176  match = dictBase + matchIndex;
1177  lowLimit = dictionary; /* required for match length counter */
1178  } else {
1179  match = base + matchIndex;
1180  lowLimit = (const BYTE*)source; /* required for match length counter */
1181  }
1182  } else { /* single memory segment */
1183  match = base + matchIndex;
1184  }
1185  LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType);
1186  assert(matchIndex < current);
1187  if ( ((dictIssue==dictSmall) ? (matchIndex >= prefixIdxLimit) : 1)
1188  && (((tableType==byU16) && (LZ4_DISTANCE_MAX == LZ4_DISTANCE_ABSOLUTE_MAX)) ? 1 : (matchIndex+LZ4_DISTANCE_MAX >= current))
1189  && (LZ4_read32(match) == LZ4_read32(ip)) ) {
1190  token=op++;
1191  *token=0;
1192  if (maybe_extMem) offset = current - matchIndex;
1193  DEBUGLOG(6, "seq.start:%i, literals=%u, match.start:%i",
1194  (int)(anchor-(const BYTE*)source), 0, (int)(ip-(const BYTE*)source));
1195  goto _next_match;
1196  }
1197  }
1198 
1199  /* Prepare next loop */
1200  forwardH = LZ4_hashPosition(++ip, tableType);
1201 
1202  }
1203 
1204 _last_literals:
1205  /* Encode Last Literals */
1206  { size_t lastRun = (size_t)(iend - anchor);
1207  if ( (outputDirective) && /* Check output buffer overflow */
1208  (op + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > olimit)) {
1209  if (outputDirective == fillOutput) {
1210  /* adapt lastRun to fill 'dst' */
1211  assert(olimit >= op);
1212  lastRun = (size_t)(olimit-op) - 1/*token*/;
1213  lastRun -= (lastRun + 256 - RUN_MASK) / 256; /*additional length tokens*/
1214  } else {
1215  assert(outputDirective == limitedOutput);
1216  return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */
1217  }
1218  }
1219  DEBUGLOG(6, "Final literal run : %i literals", (int)lastRun);
1220  if (lastRun >= RUN_MASK) {
1221  size_t accumulator = lastRun - RUN_MASK;
1222  *op++ = RUN_MASK << ML_BITS;
1223  for(; accumulator >= 255 ; accumulator-=255) *op++ = 255;
1224  *op++ = (BYTE) accumulator;
1225  } else {
1226  *op++ = (BYTE)(lastRun<<ML_BITS);
1227  }
1228  LZ4_memcpy(op, anchor, lastRun);
1229  ip = anchor + lastRun;
1230  op += lastRun;
1231  }
1232 
1233  if (outputDirective == fillOutput) {
1234  *inputConsumed = (int) (((const char*)ip)-source);
1235  }
1236  result = (int)(((char*)op) - dest);
1237  assert(result > 0);
1238  DEBUGLOG(5, "LZ4_compress_generic: compressed %i bytes into %i bytes", inputSize, result);
1239  return result;
1240 }
size_t len
Definition: 6502dis.c:15
ut8 op
Definition: 6502dis.c:13
static states step(struct re_guts *, sopno, sopno, states, int, states)
Definition: engine.c:888
unsigned char match[65280+2]
Definition: gun.c:165
voidpf uLong offset
Definition: ioapi.h:144
static void struct sockaddr socklen_t static fromlen static backlog static fork char char char static envp int struct rusage static rusage struct utsname static buf struct sembuf unsigned
Definition: sflib.h:97
static const int LZ4_minLength
Definition: lz4.c:221
LZ4_FORCE_INLINE void LZ4_wildCopy8(void *dstPtr, const void *srcPtr, void *dstEnd)
Definition: lz4.c:408
LZ4_FORCE_INLINE void LZ4_putPositionOnHash(const BYTE *p, U32 h, void *tableBase, tableType_t const tableType, const BYTE *srcBase)
Definition: lz4.c:748
#define MINMATCH
Definition: lz4.c:214
static void LZ4_write32(void *memPtr, U32 value)
Definition: lz4.c:377
static void LZ4_writeLE16(void *memPtr, U16 value)
Definition: lz4.c:395
#define GB
Definition: lz4.c:225
LZ4_FORCE_INLINE const BYTE * LZ4_getPosition(const BYTE *p, const void *tableBase, tableType_t tableType, const BYTE *srcBase)
Definition: lz4.c:797
LZ4_FORCE_INLINE U32 LZ4_getIndexOnHash(U32 h, const void *tableBase, tableType_t tableType)
Definition: lz4.c:773
#define LZ4_DISTANCE_ABSOLUTE_MAX
Definition: lz4.c:227
#define MFLIMIT
Definition: lz4.c:218
LZ4_FORCE_INLINE void LZ4_putPosition(const BYTE *p, void *tableBase, tableType_t tableType, const BYTE *srcBase)
Definition: lz4.c:761
#define LASTLITERALS
Definition: lz4.c:217
static U32 LZ4_read32(const void *memPtr)
Definition: lz4.c:362
LZ4_FORCE_INLINE unsigned LZ4_count(const BYTE *pIn, const BYTE *pMatch, const BYTE *pInLimit)
Definition: lz4.c:603
static const U32 LZ4_skipTrigger
Definition: lz4.c:634
LZ4_FORCE_INLINE void LZ4_putIndexOnHash(U32 idx, U32 h, void *tableBase, tableType_t const tableType)
Definition: lz4.c:736
LZ4_FORCE_INLINE void LZ4_clearHash(U32 h, void *tableBase, tableType_t const tableType)
Definition: lz4.c:724
#define ML_BITS
Definition: lz4.c:232
#define ML_MASK
Definition: lz4.c:233
static const BYTE * LZ4_getPositionOnHash(U32 h, const void *tableBase, tableType_t tableType, const BYTE *srcBase)
Definition: lz4.c:789
LZ4_FORCE_INLINE U32 LZ4_hashPosition(const void *const p, tableType_t const tableType)
Definition: lz4.c:718
#define unlikely(expr)
Definition: lz4.c:177
#define RUN_MASK
Definition: lz4.c:235
#define anchor
#define ip
static uint32_t const uint8_t uint32_t uint32_t limit
Definition: memcmplen.h:45
static int
Definition: sfsocketcall.h:114
int size_t
Definition: sftypes.h:40
int ptrdiff_t
Definition: sftypes.h:68
LZ4_u32 tableType
Definition: lz4.h:599
LZ4_u32 hashTable[LZ4_HASH_SIZE_U32]
Definition: lz4.h:597
Definition: engine.c:71
Definition: dis.c:32

References anchor, assert, byPtr, byU16, byU32, LZ4_stream_t_internal::currentOffset, DEBUGLOG, dest, LZ4_stream_t_internal::dictCtx, LZ4_stream_t_internal::dictionary, LZ4_stream_t_internal::dictSize, dictSmall, fillOutput, GB, h, LZ4_stream_t_internal::hashTable, inputSize, int, ip, LASTLITERALS, len, limit, limitedOutput, LZ4_64Klimit, LZ4_clearHash(), LZ4_count(), LZ4_DISTANCE_ABSOLUTE_MAX, LZ4_getIndexOnHash(), LZ4_getPosition(), LZ4_getPositionOnHash(), LZ4_hashPosition(), LZ4_memcpy, LZ4_minLength, LZ4_putIndexOnHash(), LZ4_putPosition(), LZ4_putPositionOnHash(), LZ4_read32(), LZ4_skipTrigger, LZ4_wildCopy8(), LZ4_write32(), LZ4_writeLE16(), match, maxOutputSize, MFLIMIT, MINMATCH, ML_BITS, ML_MASK, noDict, NULL, op, RUN_MASK, source, step(), LZ4_stream_t_internal::tableType, unlikely, unsigned, usingDictCtx, usingExtDict, and withPrefix64k.

Referenced by LZ4_compress_generic().

◆ LZ4_compress_limitedOutput()

int LZ4_compress_limitedOutput ( const char *  source,
char *  dest,
int  inputSize,
int  maxOutputSize 
)

Definition at line 2432 of file lz4.c.

2433 {
2435 }

References dest, inputSize, LZ4_compress_default(), maxOutputSize, and source.

◆ LZ4_compress_limitedOutput_continue()

int LZ4_compress_limitedOutput_continue ( LZ4_stream_t LZ4_stream,
const char *  src,
char *  dst,
int  srcSize,
int  dstCapacity 
)

Definition at line 2448 of file lz4.c.

2449 {
2450  return LZ4_compress_fast_continue(LZ4_stream, src, dst, srcSize, dstCapacity, 1);
2451 }

References dst, LZ4_compress_fast_continue(), LZ4_stream, src, and srcSize.

◆ LZ4_compress_limitedOutput_withState()

int LZ4_compress_limitedOutput_withState ( void *  state,
const char *  src,
char *  dst,
int  srcSize,
int  dstSize 
)

Definition at line 2440 of file lz4.c.

2441 {
2442  return LZ4_compress_fast_extState(state, src, dst, srcSize, dstSize, 1);
2443 }

References dst, LZ4_compress_fast_extState(), src, and srcSize.

◆ LZ4_compress_withState()

int LZ4_compress_withState ( void *  state,
const char *  src,
char *  dst,
int  srcSize 
)

Definition at line 2444 of file lz4.c.

2445 {
2447 }

References dst, LZ4_compress_fast_extState(), LZ4_compressBound(), src, and srcSize.

◆ LZ4_compressBound()

int LZ4_compressBound ( int  inputSize)

LZ4_compressBound() : Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible) This function is primarily useful for memory allocation purposes (destination buffer size). Macro LZ4_COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example). Note that LZ4_compress_default() compresses faster when dstCapacity is >= LZ4_compressBound(srcSize) inputSize : max supported value is LZ4_MAX_INPUT_SIZE return : maximum output size in a "worst case" scenario or 0, if input size is incorrect (too large or negative)

Definition at line 674 of file lz4.c.

674 { return LZ4_COMPRESSBOUND(isize); }
#define LZ4_COMPRESSBOUND(isize)
Definition: lz4.h:171
char int isize
Definition: lz4.h:706

References isize, and LZ4_COMPRESSBOUND.

Referenced by BMK_benchMem(), fullSpeedBench(), FUZ_test(), FUZ_unitTests(), LLVMFuzzerTestOneInput(), local_LZ4_compress_default_large(), local_LZ4_compress_default_small(), local_LZ4_compress_destSize(), local_LZ4_compress_fast0(), local_LZ4_compress_fast1(), local_LZ4_compress_fast17(), local_LZ4_compress_fast2(), local_LZ4_compress_fast_continue0(), local_LZ4_compress_fast_extState0(), local_LZ4_compress_HC(), local_LZ4_compress_HC_continue(), local_LZ4_compress_HC_extStateHC(), LZ4_compress(), LZ4_compress_continue(), LZ4_compress_destSize_extState(), LZ4_compress_fast_extState(), LZ4_compress_fast_extState_fastReset(), LZ4_compress_HC_continue(), LZ4_compress_HC_extStateHC_fastReset(), LZ4_compress_withState(), LZ4_compressHC(), LZ4_compressHC2(), LZ4_compressHC2_withStateHC(), LZ4_compressHC_continue(), LZ4_compressHC_withStateHC(), LZ4IO_compressFilename_Legacy(), LZ4IO_decodeLegacyStream(), main(), roundTripCheck(), and state_create().

◆ LZ4_count()

LZ4_FORCE_INLINE unsigned LZ4_count ( const BYTE pIn,
const BYTE pMatch,
const BYTE pInLimit 
)

Definition at line 603 of file lz4.c.

604 {
605  const BYTE* const pStart = pIn;
606 
607  if (likely(pIn < pInLimit-(STEPSIZE-1))) {
608  reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
609  if (!diff) {
610  pIn+=STEPSIZE; pMatch+=STEPSIZE;
611  } else {
612  return LZ4_NbCommonBytes(diff);
613  } }
614 
615  while (likely(pIn < pInLimit-(STEPSIZE-1))) {
616  reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
617  if (!diff) { pIn+=STEPSIZE; pMatch+=STEPSIZE; continue; }
618  pIn += LZ4_NbCommonBytes(diff);
619  return (unsigned)(pIn - pStart);
620  }
621 
622  if ((STEPSIZE==8) && (pIn<(pInLimit-3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) { pIn+=4; pMatch+=4; }
623  if ((pIn<(pInLimit-1)) && (LZ4_read16(pMatch) == LZ4_read16(pIn))) { pIn+=2; pMatch+=2; }
624  if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++;
625  return (unsigned)(pIn - pStart);
626 }
#define STEPSIZE
Definition: lz4.c:601
size_t reg_t
Definition: lz4.c:297
static unsigned LZ4_NbCommonBytes(reg_t val)
Definition: lz4.c:513
static reg_t LZ4_read_ARCH(const void *memPtr)
Definition: lz4.c:367
#define likely(expr)
Definition: lz4.c:174
static U16 LZ4_read16(const void *memPtr)
Definition: lz4.c:357

References likely, LZ4_NbCommonBytes(), LZ4_read16(), LZ4_read32(), LZ4_read_ARCH(), and STEPSIZE.

Referenced by LZ4_compress_generic_validated(), and LZ4HC_InsertAndGetWiderMatch().

◆ LZ4_create()

void* LZ4_create ( char *  inputBuffer)

Definition at line 2483 of file lz4.c.

2484 {
2485  (void)inputBuffer;
2486  return LZ4_createStream();
2487 }
LZ4_stream_t * LZ4_createStream(void)
Definition: lz4.c:1423
char * inputBuffer
Definition: lz4.h:720

References inputBuffer, and LZ4_createStream().

◆ LZ4_createStream()

LZ4_stream_t* LZ4_createStream ( void  )

Definition at line 1423 of file lz4.c.

1424 {
1425  LZ4_stream_t* const lz4s = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t));
1426  LZ4_STATIC_ASSERT(LZ4_STREAMSIZE >= sizeof(LZ4_stream_t_internal)); /* A compilation error here means LZ4_STREAMSIZE is not large enough */
1427  DEBUGLOG(4, "LZ4_createStream %p", lz4s);
1428  if (lz4s == NULL) return NULL;
1429  LZ4_initStream(lz4s, sizeof(*lz4s));
1430  return lz4s;
1431 }
#define LZ4_STATIC_ASSERT(c)
Definition: lz4.c:249
#define LZ4_STREAMSIZE
Definition: lz4.h:623

References ALLOC, DEBUGLOG, LZ4_initStream(), LZ4_STATIC_ASSERT, LZ4_STREAMSIZE, test-lz4-versions::lz4s, and NULL.

Referenced by FUZ_unitTests(), LZ4_compressInitStream(), LZ4_create(), LZ4F_compressBegin_usingCDict(), LZ4F_createCDict(), state_attachDictRoundTrip(), state_create(), and test_compress().

◆ LZ4_createStreamDecode()

LZ4_streamDecode_t* LZ4_createStreamDecode ( void  )

LZ4_createStreamDecode() and LZ4_freeStreamDecode() : creation / destruction of streaming decompression tracking context. A tracking context can be re-used multiple times.

Definition at line 2265 of file lz4.c.

2266 {
2268  LZ4_STATIC_ASSERT(LZ4_STREAMDECODESIZE >= sizeof(LZ4_streamDecode_t_internal)); /* A compilation error here means LZ4_STREAMDECODESIZE is not large enough */
2269  return lz4s;
2270 }
#define ALLOC_AND_ZERO(s)
Definition: lz4.c:203
#define LZ4_STREAMDECODESIZE
Definition: lz4.h:656

References ALLOC_AND_ZERO, LZ4_STATIC_ASSERT, LZ4_STREAMDECODESIZE, and test-lz4-versions::lz4s.

Referenced by state_create(), and test_decompress().

◆ LZ4_decoderRingBufferSize()

int LZ4_decoderRingBufferSize ( int  maxBlockSize)

LZ4_decoderRingBufferSize() : when setting a ring buffer for streaming decompression (optional scenario), provides the minimum size of this ring buffer to be compatible with any source respecting maxBlockSize condition. Note : in a ring buffer scenario, blocks are presumed decompressed next to each other. When not enough space remains for next block (remainingSize < maxBlockSize), decoding resumes from beginning of ring buffer.

Returns
: minimum ring buffer size, or 0 if there is an error (invalid maxBlockSize).

Definition at line 2306 of file lz4.c.

2307 {
2308  if (maxBlockSize < 0) return 0;
2309  if (maxBlockSize > LZ4_MAX_INPUT_SIZE) return 0;
2310  if (maxBlockSize < 16) maxBlockSize = 16;
2311  return LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize);
2312 }
#define LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize)
Definition: lz4.h:382

References LZ4_DECODER_RING_BUFFER_SIZE, and LZ4_MAX_INPUT_SIZE.

Referenced by FUZ_unitTests().

◆ LZ4_decompress_fast()

LZ4_FORCE_O2 int LZ4_decompress_fast ( const char *  src,
char *  dst,
int  originalSize 
)

Obsolete LZ4_decompress_fast variants (since v1.9.0) : These functions used to be faster than LZ4_decompress_safe(), but this is no longer the case. They are now slower. This is because LZ4_decompress_fast() doesn't know the input size, and therefore must progress more cautiously into the input buffer to not read beyond the end of block. On top of that LZ4_decompress_fast() is not protected vs malformed or malicious inputs, making it a security liability. As a consequence, LZ4_decompress_fast() is strongly discouraged, and deprecated.

The last remaining LZ4_decompress_fast() specificity is that it can decompress a block without knowing its compressed size. Such functionality can be achieved in a more secure manner by employing LZ4_decompress_safe_partial().

Parameters: originalSize : is the uncompressed size to regenerate. dst must be already allocated, its size must be >= 'originalSize' bytes.

Returns
: number of bytes read from source buffer (== compressed size). The function expects to finish at block's end exactly. If the source stream is detected malformed, the function stops decoding and returns a negative result. note : LZ4_decompress_fast*() requires originalSize. Thanks to this information, it never writes past the output buffer. However, since it doesn't know its 'src' size, it may read an unknown amount of input, past input buffer bounds. Also, since match offsets are not validated, match reads from 'src' may underflow too. These issues never happen if input (compressed) data is correct. But they may happen if input data is invalid (error or intentional tampering). As a consequence, use these functions in trusted environments with trusted data only.

Definition at line 2188 of file lz4.c.

2189 {
2192  (BYTE*)dest - 64 KB, NULL, 0);
2193 }
LZ4_FORCE_INLINE int LZ4_decompress_generic(const char *const src, char *const dst, int srcSize, int outputSize, endCondition_directive endOnInput, earlyEnd_directive partialDecoding, dict_directive dict, const BYTE *const lowPrefix, const BYTE *const dictStart, const size_t dictSize)
Definition: lz4.c:1738
char int originalSize
Definition: lz4.h:725

References decode_full_block, dest, endOnOutputSize, KB, LZ4_decompress_generic(), NULL, originalSize, source, and withPrefix64k.

Referenced by bench(), FUZ_test(), local_LZ4_decompress_fast(), LZ4_decompress_fast_continue(), LZ4_decompress_fast_usingDict(), LZ4_decompress_fast_withPrefix64k(), and LZ4_uncompress().

◆ LZ4_decompress_fast_continue()

LZ4_FORCE_O2 int LZ4_decompress_fast_continue ( LZ4_streamDecode_t LZ4_streamDecode,
const char *  source,
char *  dest,
int  originalSize 
)

Definition at line 2362 of file lz4.c.

2363 {
2364  LZ4_streamDecode_t_internal* lz4sd = &LZ4_streamDecode->internal_donotuse;
2365  int result;
2366  assert(originalSize >= 0);
2367 
2368  if (lz4sd->prefixSize == 0) {
2369  assert(lz4sd->extDictSize == 0);
2371  if (result <= 0) return result;
2372  lz4sd->prefixSize = (size_t)originalSize;
2373  lz4sd->prefixEnd = (BYTE*)dest + originalSize;
2374  } else if (lz4sd->prefixEnd == (BYTE*)dest) {
2375  if (lz4sd->prefixSize >= 64 KB - 1 || lz4sd->extDictSize == 0)
2377  else
2379  lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize);
2380  if (result <= 0) return result;
2381  lz4sd->prefixSize += (size_t)originalSize;
2382  lz4sd->prefixEnd += originalSize;
2383  } else {
2384  lz4sd->extDictSize = lz4sd->prefixSize;
2385  lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
2387  lz4sd->externalDict, lz4sd->extDictSize);
2388  if (result <= 0) return result;
2389  lz4sd->prefixSize = (size_t)originalSize;
2390  lz4sd->prefixEnd = (BYTE*)dest + originalSize;
2391  }
2392 
2393  return result;
2394 }
LZ4_FORCE_INLINE int LZ4_decompress_fast_doubleDict(const char *source, char *dest, int originalSize, size_t prefixSize, const void *dictStart, size_t dictSize)
Definition: lz4.c:2255
LZ4_FORCE_O2 int LZ4_decompress_fast(const char *source, char *dest, int originalSize)
Definition: lz4.c:2188
static LZ4_FORCE_O2 int LZ4_decompress_fast_extDict(const char *source, char *dest, int originalSize, const void *dictStart, size_t dictSize)
Definition: lz4.c:2233
const LZ4_byte * prefixEnd
Definition: lz4.h:608
const LZ4_byte * externalDict
Definition: lz4.h:606
LZ4_streamDecode_t_internal internal_donotuse
Definition: lz4.h:659

References assert, dest, LZ4_streamDecode_t_internal::extDictSize, LZ4_streamDecode_t_internal::externalDict, LZ4_streamDecode_u::internal_donotuse, KB, LZ4_decompress_fast(), LZ4_decompress_fast_doubleDict(), LZ4_decompress_fast_extDict(), originalSize, LZ4_streamDecode_t_internal::prefixEnd, LZ4_streamDecode_t_internal::prefixSize, and source.

Referenced by FUZ_unitTests().

◆ LZ4_decompress_fast_doubleDict()

LZ4_FORCE_INLINE int LZ4_decompress_fast_doubleDict ( const char *  source,
char *  dest,
int  originalSize,
size_t  prefixSize,
const void *  dictStart,
size_t  dictSize 
)

Definition at line 2255 of file lz4.c.

2257 {
2260  (BYTE*)dest-prefixSize, (const BYTE*)dictStart, dictSize);
2261 }

References decode_full_block, dest, endOnOutputSize, LZ4_decompress_generic(), originalSize, source, and usingExtDict.

Referenced by LZ4_decompress_fast_continue().

◆ LZ4_decompress_fast_extDict()

static LZ4_FORCE_O2 int LZ4_decompress_fast_extDict ( const char *  source,
char *  dest,
int  originalSize,
const void *  dictStart,
size_t  dictSize 
)
static

Definition at line 2233 of file lz4.c.

2235 {
2238  (BYTE*)dest, (const BYTE*)dictStart, dictSize);
2239 }

References decode_full_block, dest, endOnOutputSize, LZ4_decompress_generic(), originalSize, source, and usingExtDict.

Referenced by LZ4_decompress_fast_continue(), and LZ4_decompress_fast_usingDict().

◆ LZ4_decompress_fast_usingDict()

int LZ4_decompress_fast_usingDict ( const char *  source,
char *  dest,
int  originalSize,
const char *  dictStart,
int  dictSize 
)

Definition at line 2419 of file lz4.c.

2420 {
2421  if (dictSize==0 || dictStart+dictSize == dest)
2423  assert(dictSize >= 0);
2424  return LZ4_decompress_fast_extDict(source, dest, originalSize, dictStart, (size_t)dictSize);
2425 }

References assert, dest, LZ4_decompress_fast(), LZ4_decompress_fast_extDict(), originalSize, and source.

Referenced by FUZ_test(), local_LZ4_decompress_fast_usingDict_prefix(), and local_LZ4_decompress_fast_usingExtDict().

◆ LZ4_decompress_fast_withPrefix64k()

int LZ4_decompress_fast_withPrefix64k ( const char *  source,
char *  dest,
int  originalSize 
)

Definition at line 2206 of file lz4.c.

2207 {
2208  /* LZ4_decompress_fast doesn't validate match offsets,
2209  * and thus serves well with any prefixed dictionary. */
2211 }

References dest, LZ4_decompress_fast(), originalSize, and source.

◆ LZ4_decompress_generic()

LZ4_FORCE_INLINE int LZ4_decompress_generic ( const char *const  src,
char *const  dst,
int  srcSize,
int  outputSize,
endCondition_directive  endOnInput,
earlyEnd_directive  partialDecoding,
dict_directive  dict,
const BYTE *const  lowPrefix,
const BYTE *const  dictStart,
const size_t  dictSize 
)

LZ4_decompress_generic() : This generic decompression function covers all use cases. It shall be instantiated several times, using different sets of directives. Note that it is important for performance that this function really get inlined, in order to remove useless branches during compilation optimization.

Definition at line 1738 of file lz4.c.

1751 {
1752  if (src == NULL) { return -1; }
1753 
1754  { const BYTE* ip = (const BYTE*) src;
1755  const BYTE* const iend = ip + srcSize;
1756 
1757  BYTE* op = (BYTE*) dst;
1758  BYTE* const oend = op + outputSize;
1759  BYTE* cpy;
1760 
1761  const BYTE* const dictEnd = (dictStart == NULL) ? NULL : dictStart + dictSize;
1762 
1763  const int safeDecode = (endOnInput==endOnInputSize);
1764  const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB)));
1765 
1766 
1767  /* Set up the "end" pointers for the shortcut. */
1768  const BYTE* const shortiend = iend - (endOnInput ? 14 : 8) /*maxLL*/ - 2 /*offset*/;
1769  const BYTE* const shortoend = oend - (endOnInput ? 14 : 8) /*maxLL*/ - 18 /*maxML*/;
1770 
1771  const BYTE* match;
1772  size_t offset;
1773  unsigned token;
1774  size_t length;
1775 
1776 
1777  DEBUGLOG(5, "LZ4_decompress_generic (srcSize:%i, dstSize:%i)", srcSize, outputSize);
1778 
1779  /* Special cases */
1780  assert(lowPrefix <= op);
1781  if ((endOnInput) && (unlikely(outputSize==0))) {
1782  /* Empty output buffer */
1783  if (partialDecoding) return 0;
1784  return ((srcSize==1) && (*ip==0)) ? 0 : -1;
1785  }
1786  if ((!endOnInput) && (unlikely(outputSize==0))) { return (*ip==0 ? 1 : -1); }
1787  if ((endOnInput) && unlikely(srcSize==0)) { return -1; }
1788 
1789  /* Currently the fast loop shows a regression on qualcomm arm chips. */
1790 #if LZ4_FAST_DEC_LOOP
1791  if ((oend - op) < FASTLOOP_SAFE_DISTANCE) {
1792  DEBUGLOG(6, "skip fast decode loop");
1793  goto safe_decode;
1794  }
1795 
1796  /* Fast loop : decode sequences as long as output < iend-FASTLOOP_SAFE_DISTANCE */
1797  while (1) {
1798  /* Main fastloop assertion: We can always wildcopy FASTLOOP_SAFE_DISTANCE */
1799  assert(oend - op >= FASTLOOP_SAFE_DISTANCE);
1800  if (endOnInput) { assert(ip < iend); }
1801  token = *ip++;
1802  length = token >> ML_BITS; /* literal length */
1803 
1804  assert(!endOnInput || ip <= iend); /* ip < iend before the increment */
1805 
1806  /* decode literal length */
1807  if (length == RUN_MASK) {
1809  length += read_variable_length(&ip, iend-RUN_MASK, (int)endOnInput, (int)endOnInput, &error);
1810  if (error == initial_error) { goto _output_error; }
1811  if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */
1812  if ((safeDecode) && unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */
1813 
1814  /* copy literals */
1815  cpy = op+length;
1817  if (endOnInput) { /* LZ4_decompress_safe() */
1818  if ((cpy>oend-32) || (ip+length>iend-32)) { goto safe_literal_copy; }
1819  LZ4_wildCopy32(op, ip, cpy);
1820  } else { /* LZ4_decompress_fast() */
1821  if (cpy>oend-8) { goto safe_literal_copy; }
1822  LZ4_wildCopy8(op, ip, cpy); /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time :
1823  * it doesn't know input length, and only relies on end-of-block properties */
1824  }
1825  ip += length; op = cpy;
1826  } else {
1827  cpy = op+length;
1828  if (endOnInput) { /* LZ4_decompress_safe() */
1829  DEBUGLOG(7, "copy %u bytes in a 16-bytes stripe", (unsigned)length);
1830  /* We don't need to check oend, since we check it once for each loop below */
1831  if (ip > iend-(16 + 1/*max lit + offset + nextToken*/)) { goto safe_literal_copy; }
1832  /* Literals can only be 14, but hope compilers optimize if we copy by a register size */
1833  LZ4_memcpy(op, ip, 16);
1834  } else { /* LZ4_decompress_fast() */
1835  /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time :
1836  * it doesn't know input length, and relies on end-of-block properties */
1837  LZ4_memcpy(op, ip, 8);
1838  if (length > 8) { LZ4_memcpy(op+8, ip+8, 8); }
1839  }
1840  ip += length; op = cpy;
1841  }
1842 
1843  /* get offset */
1844  offset = LZ4_readLE16(ip); ip+=2;
1845  match = op - offset;
1846  assert(match <= op);
1847 
1848  /* get matchlength */
1849  length = token & ML_MASK;
1850 
1851  if (length == ML_MASK) {
1853  if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) { goto _output_error; } /* Error : offset outside buffers */
1854  length += read_variable_length(&ip, iend - LASTLITERALS + 1, (int)endOnInput, 0, &error);
1855  if (error != ok) { goto _output_error; }
1856  if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) { goto _output_error; } /* overflow detection */
1857  length += MINMATCH;
1858  if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) {
1859  goto safe_match_copy;
1860  }
1861  } else {
1862  length += MINMATCH;
1863  if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) {
1864  goto safe_match_copy;
1865  }
1866 
1867  /* Fastpath check: Avoids a branch in LZ4_wildCopy32 if true */
1868  if ((dict == withPrefix64k) || (match >= lowPrefix)) {
1869  if (offset >= 8) {
1870  assert(match >= lowPrefix);
1871  assert(match <= op);
1872  assert(op + 18 <= oend);
1873 
1874  LZ4_memcpy(op, match, 8);
1875  LZ4_memcpy(op+8, match+8, 8);
1876  LZ4_memcpy(op+16, match+16, 2);
1877  op += length;
1878  continue;
1879  } } }
1880 
1881  if (checkOffset && (unlikely(match + dictSize < lowPrefix))) { goto _output_error; } /* Error : offset outside buffers */
1882  /* match starting within external dictionary */
1883  if ((dict==usingExtDict) && (match < lowPrefix)) {
1884  if (unlikely(op+length > oend-LASTLITERALS)) {
1885  if (partialDecoding) {
1886  DEBUGLOG(7, "partialDecoding: dictionary match, close to dstEnd");
1887  length = MIN(length, (size_t)(oend-op));
1888  } else {
1889  goto _output_error; /* end-of-block condition violated */
1890  } }
1891 
1892  if (length <= (size_t)(lowPrefix-match)) {
1893  /* match fits entirely within external dictionary : just copy */
1894  memmove(op, dictEnd - (lowPrefix-match), length);
1895  op += length;
1896  } else {
1897  /* match stretches into both external dictionary and current block */
1898  size_t const copySize = (size_t)(lowPrefix - match);
1899  size_t const restSize = length - copySize;
1900  LZ4_memcpy(op, dictEnd - copySize, copySize);
1901  op += copySize;
1902  if (restSize > (size_t)(op - lowPrefix)) { /* overlap copy */
1903  BYTE* const endOfMatch = op + restSize;
1904  const BYTE* copyFrom = lowPrefix;
1905  while (op < endOfMatch) { *op++ = *copyFrom++; }
1906  } else {
1907  LZ4_memcpy(op, lowPrefix, restSize);
1908  op += restSize;
1909  } }
1910  continue;
1911  }
1912 
1913  /* copy match within block */
1914  cpy = op + length;
1915 
1916  assert((op <= oend) && (oend-op >= 32));
1917  if (unlikely(offset<16)) {
1918  LZ4_memcpy_using_offset(op, match, cpy, offset);
1919  } else {
1920  LZ4_wildCopy32(op, match, cpy);
1921  }
1922 
1923  op = cpy; /* wildcopy correction */
1924  }
1925  safe_decode:
1926 #endif
1927 
1928  /* Main Loop : decode remaining sequences where output < FASTLOOP_SAFE_DISTANCE */
1929  while (1) {
1930  token = *ip++;
1931  length = token >> ML_BITS; /* literal length */
1932 
1933  assert(!endOnInput || ip <= iend); /* ip < iend before the increment */
1934 
1935  /* A two-stage shortcut for the most common case:
1936  * 1) If the literal length is 0..14, and there is enough space,
1937  * enter the shortcut and copy 16 bytes on behalf of the literals
1938  * (in the fast mode, only 8 bytes can be safely copied this way).
1939  * 2) Further if the match length is 4..18, copy 18 bytes in a similar
1940  * manner; but we ensure that there's enough space in the output for
1941  * those 18 bytes earlier, upon entering the shortcut (in other words,
1942  * there is a combined check for both stages).
1943  */
1944  if ( (endOnInput ? length != RUN_MASK : length <= 8)
1945  /* strictly "less than" on input, to re-enter the loop with at least one byte */
1946  && likely((endOnInput ? ip < shortiend : 1) & (op <= shortoend)) ) {
1947  /* Copy the literals */
1948  LZ4_memcpy(op, ip, endOnInput ? 16 : 8);
1949  op += length; ip += length;
1950 
1951  /* The second stage: prepare for match copying, decode full info.
1952  * If it doesn't work out, the info won't be wasted. */
1953  length = token & ML_MASK; /* match length */
1954  offset = LZ4_readLE16(ip); ip += 2;
1955  match = op - offset;
1956  assert(match <= op); /* check overflow */
1957 
1958  /* Do not deal with overlapping matches. */
1959  if ( (length != ML_MASK)
1960  && (offset >= 8)
1961  && (dict==withPrefix64k || match >= lowPrefix) ) {
1962  /* Copy the match. */
1963  LZ4_memcpy(op + 0, match + 0, 8);
1964  LZ4_memcpy(op + 8, match + 8, 8);
1965  LZ4_memcpy(op +16, match +16, 2);
1966  op += length + MINMATCH;
1967  /* Both stages worked, load the next token. */
1968  continue;
1969  }
1970 
1971  /* The second stage didn't work out, but the info is ready.
1972  * Propel it right to the point of match copying. */
1973  goto _copy_match;
1974  }
1975 
1976  /* decode literal length */
1977  if (length == RUN_MASK) {
1979  length += read_variable_length(&ip, iend-RUN_MASK, (int)endOnInput, (int)endOnInput, &error);
1980  if (error == initial_error) { goto _output_error; }
1981  if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */
1982  if ((safeDecode) && unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */
1983  }
1984 
1985  /* copy literals */
1986  cpy = op+length;
1987 #if LZ4_FAST_DEC_LOOP
1988  safe_literal_copy:
1989 #endif
1991  if ( ((endOnInput) && ((cpy>oend-MFLIMIT) || (ip+length>iend-(2+1+LASTLITERALS))) )
1992  || ((!endOnInput) && (cpy>oend-WILDCOPYLENGTH)) )
1993  {
1994  /* We've either hit the input parsing restriction or the output parsing restriction.
1995  * In the normal scenario, decoding a full block, it must be the last sequence,
1996  * otherwise it's an error (invalid input or dimensions).
1997  * In partialDecoding scenario, it's necessary to ensure there is no buffer overflow.
1998  */
1999  if (partialDecoding) {
2000  /* Since we are partial decoding we may be in this block because of the output parsing
2001  * restriction, which is not valid since the output buffer is allowed to be undersized.
2002  */
2003  assert(endOnInput);
2004  DEBUGLOG(7, "partialDecoding: copying literals, close to input or output end")
2005  DEBUGLOG(7, "partialDecoding: literal length = %u", (unsigned)length);
2006  DEBUGLOG(7, "partialDecoding: remaining space in dstBuffer : %i", (int)(oend - op));
2007  DEBUGLOG(7, "partialDecoding: remaining space in srcBuffer : %i", (int)(iend - ip));
2008  /* Finishing in the middle of a literals segment,
2009  * due to lack of input.
2010  */
2011  if (ip+length > iend) {
2012  length = (size_t)(iend-ip);
2013  cpy = op + length;
2014  }
2015  /* Finishing in the middle of a literals segment,
2016  * due to lack of output space.
2017  */
2018  if (cpy > oend) {
2019  cpy = oend;
2020  assert(op<=oend);
2021  length = (size_t)(oend-op);
2022  }
2023  } else {
2024  /* We must be on the last sequence because of the parsing limitations so check
2025  * that we exactly regenerate the original size (must be exact when !endOnInput).
2026  */
2027  if ((!endOnInput) && (cpy != oend)) { goto _output_error; }
2028  /* We must be on the last sequence (or invalid) because of the parsing limitations
2029  * so check that we exactly consume the input and don't overrun the output buffer.
2030  */
2031  if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) {
2032  DEBUGLOG(6, "should have been last run of literals")
2033  DEBUGLOG(6, "ip(%p) + length(%i) = %p != iend (%p)", ip, (int)length, ip+length, iend);
2034  DEBUGLOG(6, "or cpy(%p) > oend(%p)", cpy, oend);
2035  goto _output_error;
2036  }
2037  }
2038  memmove(op, ip, length); /* supports overlapping memory regions; only matters for in-place decompression scenarios */
2039  ip += length;
2040  op += length;
2041  /* Necessarily EOF when !partialDecoding.
2042  * When partialDecoding, it is EOF if we've either
2043  * filled the output buffer or
2044  * can't proceed with reading an offset for following match.
2045  */
2046  if (!partialDecoding || (cpy == oend) || (ip >= (iend-2))) {
2047  break;
2048  }
2049  } else {
2050  LZ4_wildCopy8(op, ip, cpy); /* may overwrite up to WILDCOPYLENGTH beyond cpy */
2051  ip += length; op = cpy;
2052  }
2053 
2054  /* get offset */
2055  offset = LZ4_readLE16(ip); ip+=2;
2056  match = op - offset;
2057 
2058  /* get matchlength */
2059  length = token & ML_MASK;
2060 
2061  _copy_match:
2062  if (length == ML_MASK) {
2064  length += read_variable_length(&ip, iend - LASTLITERALS + 1, (int)endOnInput, 0, &error);
2065  if (error != ok) goto _output_error;
2066  if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) goto _output_error; /* overflow detection */
2067  }
2068  length += MINMATCH;
2069 
2070 #if LZ4_FAST_DEC_LOOP
2071  safe_match_copy:
2072 #endif
2073  if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) goto _output_error; /* Error : offset outside buffers */
2074  /* match starting within external dictionary */
2075  if ((dict==usingExtDict) && (match < lowPrefix)) {
2076  if (unlikely(op+length > oend-LASTLITERALS)) {
2077  if (partialDecoding) length = MIN(length, (size_t)(oend-op));
2078  else goto _output_error; /* doesn't respect parsing restriction */
2079  }
2080 
2081  if (length <= (size_t)(lowPrefix-match)) {
2082  /* match fits entirely within external dictionary : just copy */
2083  memmove(op, dictEnd - (lowPrefix-match), length);
2084  op += length;
2085  } else {
2086  /* match stretches into both external dictionary and current block */
2087  size_t const copySize = (size_t)(lowPrefix - match);
2088  size_t const restSize = length - copySize;
2089  LZ4_memcpy(op, dictEnd - copySize, copySize);
2090  op += copySize;
2091  if (restSize > (size_t)(op - lowPrefix)) { /* overlap copy */
2092  BYTE* const endOfMatch = op + restSize;
2093  const BYTE* copyFrom = lowPrefix;
2094  while (op < endOfMatch) *op++ = *copyFrom++;
2095  } else {
2096  LZ4_memcpy(op, lowPrefix, restSize);
2097  op += restSize;
2098  } }
2099  continue;
2100  }
2101  assert(match >= lowPrefix);
2102 
2103  /* copy match within block */
2104  cpy = op + length;
2105 
2106  /* partialDecoding : may end anywhere within the block */
2107  assert(op<=oend);
2108  if (partialDecoding && (cpy > oend-MATCH_SAFEGUARD_DISTANCE)) {
2109  size_t const mlen = MIN(length, (size_t)(oend-op));
2110  const BYTE* const matchEnd = match + mlen;
2111  BYTE* const copyEnd = op + mlen;
2112  if (matchEnd > op) { /* overlap copy */
2113  while (op < copyEnd) { *op++ = *match++; }
2114  } else {
2115  LZ4_memcpy(op, match, mlen);
2116  }
2117  op = copyEnd;
2118  if (op == oend) { break; }
2119  continue;
2120  }
2121 
2122  if (unlikely(offset<8)) {
2123  LZ4_write32(op, 0); /* silence msan warning when offset==0 */
2124  op[0] = match[0];
2125  op[1] = match[1];
2126  op[2] = match[2];
2127  op[3] = match[3];
2128  match += inc32table[offset];
2129  LZ4_memcpy(op+4, match, 4);
2130  match -= dec64table[offset];
2131  } else {
2132  LZ4_memcpy(op, match, 8);
2133  match += 8;
2134  }
2135  op += 8;
2136 
2137  if (unlikely(cpy > oend-MATCH_SAFEGUARD_DISTANCE)) {
2138  BYTE* const oCopyLimit = oend - (WILDCOPYLENGTH-1);
2139  if (cpy > oend-LASTLITERALS) { goto _output_error; } /* Error : last LASTLITERALS bytes must be literals (uncompressed) */
2140  if (op < oCopyLimit) {
2141  LZ4_wildCopy8(op, match, oCopyLimit);
2142  match += oCopyLimit - op;
2143  op = oCopyLimit;
2144  }
2145  while (op < cpy) { *op++ = *match++; }
2146  } else {
2147  LZ4_memcpy(op, match, 8);
2148  if (length > 16) { LZ4_wildCopy8(op+8, match+8, cpy); }
2149  }
2150  op = cpy; /* wildcopy correction */
2151  }
2152 
2153  /* end of decoding */
2154  if (endOnInput) {
2155  DEBUGLOG(5, "decoded %i bytes", (int) (((char*)op)-dst));
2156  return (int) (((char*)op)-dst); /* Nb of output bytes decoded */
2157  } else {
2158  return (int) (((const char*)ip)-src); /* Nb of input bytes read */
2159  }
2160 
2161  /* Overflow error detected */
2162  _output_error:
2163  return (int) (-(((const char*)ip)-src))-1;
2164  }
2165 }
lzma_index ** i
Definition: index.h:629
const lzma_allocator const uint8_t * in
Definition: block.h:527
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
void * p
Definition: libc.cpp:67
#define MIN(a, b)
Definition: lz4.c:1696
static const unsigned inc32table[8]
Definition: lz4.c:417
static const int dec64table[8]
Definition: lz4.c:418
LZ4_FORCE_INLINE unsigned read_variable_length(const BYTE **ip, const BYTE *lencheck, int loop_check, int initial_check, variable_length_error *error)
Definition: lz4.c:1708
static U16 LZ4_readLE16(const void *memPtr)
Definition: lz4.c:385
#define FASTLOOP_SAFE_DISTANCE
Definition: lz4.c:220
#define MATCH_SAFEGUARD_DISTANCE
Definition: lz4.c:219
#define WILDCOPYLENGTH
Definition: lz4.c:216
char int outputSize
Definition: lz4.h:705
static void literal(lzma_lzma1_encoder *coder, lzma_mf *mf, uint32_t position)
Definition: lzma_encoder.c:46
void error(const char *msg)
Definition: untgz.c:593
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4

References assert, DEBUGLOG, dec64table, dst, endOnInputSize, error(), FASTLOOP_SAFE_DISTANCE, inc32table, initial_error, ip, KB, LASTLITERALS, length, likely, LZ4_memcpy, LZ4_readLE16(), LZ4_STATIC_ASSERT, LZ4_wildCopy8(), LZ4_write32(), match, MATCH_SAFEGUARD_DISTANCE, MFLIMIT, MIN, MINMATCH, ML_BITS, ML_MASK, NULL, ok, op, outputSize, read_variable_length(), RUN_MASK, src, srcSize, unlikely, usingExtDict, WILDCOPYLENGTH, and withPrefix64k.

Referenced by LZ4_decompress_fast(), LZ4_decompress_fast_doubleDict(), LZ4_decompress_fast_extDict(), LZ4_decompress_safe(), LZ4_decompress_safe_doubleDict(), LZ4_decompress_safe_forceExtDict(), LZ4_decompress_safe_partial(), LZ4_decompress_safe_withPrefix64k(), and LZ4_decompress_safe_withSmallPrefix().

◆ LZ4_decompress_safe()

LZ4_FORCE_O2 int LZ4_decompress_safe ( const char *  src,
char *  dst,
int  compressedSize,
int  dstCapacity 
)

LZ4_decompress_safe() : compressedSize : is the exact complete size of the compressed block. dstCapacity : is the size of destination buffer (which must be already allocated), presumed an upper bound of decompressed size.

Returns
: the number of bytes decompressed into destination buffer (necessarily <= dstCapacity) If destination buffer is not large enough, decoding will stop and output an error code (negative value). If the source stream is detected malformed, the function will stop decoding and return a negative result. Note 1 : This function is protected against malicious data packets : it will never writes outside 'dst' buffer, nor read outside 'source' buffer, even if the compressed block is maliciously modified to order the decoder to do these actions. In such case, the decoder stops immediately, and considers the compressed block malformed. Note 2 : compressedSize and dstCapacity must be provided to the function, the compressed block does not contain them. The implementation is free to send / store / derive this information in whichever way is most beneficial. If there is a need for a different format which bundles together both compressed data and its metadata, consider looking at lz4frame.h instead.

Definition at line 2171 of file lz4.c.

2172 {
2173  return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize,
2175  (BYTE*)dest, NULL, 0);
2176 }
char int compressedSize
Definition: lz4.h:724

References compressedSize, decode_full_block, dest, endOnInputSize, LZ4_decompress_generic(), noDict, NULL, and source.

Referenced by bench(), decompress(), fullSpeedBench(), FUZ_AddressOverflow(), FUZ_test(), FUZ_unitTests(), LLVMFuzzerTestOneInput(), LZ4_decompress_safe_continue(), LZ4_decompress_safe_usingDict(), LZ4_uncompress_unknownOutputSize(), LZ4IO_decodeLegacyStream(), main(), and roundTripTest().

◆ LZ4_decompress_safe_continue()

LZ4_FORCE_O2 int LZ4_decompress_safe_continue ( LZ4_streamDecode_t LZ4_streamDecode,
const char *  src,
char *  dst,
int  srcSize,
int  dstCapacity 
)

LZ4_decompress_*_continue() : These decoding functions allow decompression of consecutive blocks in "streaming" mode. A block is an unsplittable entity, it must be presented entirely to a decompression function. Decompression functions only accepts one block at a time. The last 64KB of previously decoded data must remain available and unmodified at the memory position where they were decoded. If less than 64KB of data has been decoded, all the data must be present.

Special : if decompression side sets a ring buffer, it must respect one of the following conditions :

  • Decompression buffer size is at least LZ4_decoderRingBufferSize(maxBlockSize). maxBlockSize is the maximum size of any single block. It can have any value > 16 bytes. In which case, encoding and decoding buffers do not need to be synchronized. Actually, data can be produced by any source compliant with LZ4 format specification, and respecting maxBlockSize.
  • Synchronized mode : Decompression buffer size is exactly the same as compression buffer size, and follows exactly same update rule (block boundaries at same positions), and decoding function is provided with exact decompressed size of each block (exception for last block of the stream), then decoding & encoding ring buffer can have any size, including small ones ( < 64 KB).
  • Decompression buffer is larger than encoding buffer, by a minimum of maxBlockSize more bytes. In which case, encoding and decoding buffers do not need to be synchronized, and encoding ring buffer can have any size, including small ones ( < 64 KB).

Whenever these conditions are not possible, save the last 64KB of decoded data into a safe buffer where it can't be modified during decompression, then indicate where this data is saved using LZ4_setStreamDecode(), before decompressing next block.

Definition at line 2322 of file lz4.c.

2323 {
2324  LZ4_streamDecode_t_internal* lz4sd = &LZ4_streamDecode->internal_donotuse;
2325  int result;
2326 
2327  if (lz4sd->prefixSize == 0) {
2328  /* The first call, no dictionary yet. */
2329  assert(lz4sd->extDictSize == 0);
2331  if (result <= 0) return result;
2332  lz4sd->prefixSize = (size_t)result;
2333  lz4sd->prefixEnd = (BYTE*)dest + result;
2334  } else if (lz4sd->prefixEnd == (BYTE*)dest) {
2335  /* They're rolling the current segment. */
2336  if (lz4sd->prefixSize >= 64 KB - 1)
2338  else if (lz4sd->extDictSize == 0)
2340  lz4sd->prefixSize);
2341  else
2343  lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize);
2344  if (result <= 0) return result;
2345  lz4sd->prefixSize += (size_t)result;
2346  lz4sd->prefixEnd += result;
2347  } else {
2348  /* The buffer wraps around, or they're switching to another buffer. */
2349  lz4sd->extDictSize = lz4sd->prefixSize;
2350  lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
2352  lz4sd->externalDict, lz4sd->extDictSize);
2353  if (result <= 0) return result;
2354  lz4sd->prefixSize = (size_t)result;
2355  lz4sd->prefixEnd = (BYTE*)dest + result;
2356  }
2357 
2358  return result;
2359 }
LZ4_FORCE_O2 int LZ4_decompress_safe_withPrefix64k(const char *source, char *dest, int compressedSize, int maxOutputSize)
Definition: lz4.c:2198
static LZ4_FORCE_O2 int LZ4_decompress_safe_withSmallPrefix(const char *source, char *dest, int compressedSize, int maxOutputSize, size_t prefixSize)
Definition: lz4.c:2214
int LZ4_decompress_safe_forceExtDict(const char *source, char *dest, int compressedSize, int maxOutputSize, const void *dictStart, size_t dictSize)
Definition: lz4.c:2223
LZ4_FORCE_INLINE int LZ4_decompress_safe_doubleDict(const char *source, char *dest, int compressedSize, int maxOutputSize, size_t prefixSize, const void *dictStart, size_t dictSize)
Definition: lz4.c:2246
LZ4_FORCE_O2 int LZ4_decompress_safe(const char *source, char *dest, int compressedSize, int maxDecompressedSize)
Definition: lz4.c:2171

References assert, compressedSize, dest, LZ4_streamDecode_t_internal::extDictSize, LZ4_streamDecode_t_internal::externalDict, LZ4_streamDecode_u::internal_donotuse, KB, LZ4_decompress_safe(), LZ4_decompress_safe_doubleDict(), LZ4_decompress_safe_forceExtDict(), LZ4_decompress_safe_withPrefix64k(), LZ4_decompress_safe_withSmallPrefix(), maxOutputSize, LZ4_streamDecode_t_internal::prefixEnd, LZ4_streamDecode_t_internal::prefixSize, and source.

Referenced by FUZ_unitTests(), state_decompress(), and test_decompress().

◆ LZ4_decompress_safe_doubleDict()

LZ4_FORCE_INLINE int LZ4_decompress_safe_doubleDict ( const char *  source,
char *  dest,
int  compressedSize,
int  maxOutputSize,
size_t  prefixSize,
const void *  dictStart,
size_t  dictSize 
)

Definition at line 2246 of file lz4.c.

2248 {
2251  (BYTE*)dest-prefixSize, (const BYTE*)dictStart, dictSize);
2252 }

References compressedSize, decode_full_block, dest, endOnInputSize, LZ4_decompress_generic(), maxOutputSize, source, and usingExtDict.

Referenced by LZ4_decompress_safe_continue().

◆ LZ4_decompress_safe_forceExtDict()

LZ4_FORCE_O2 int LZ4_decompress_safe_forceExtDict ( const char *  source,
char *  dest,
int  compressedSize,
int  maxOutputSize,
const void *  dictStart,
size_t  dictSize 
)

◆ LZ4_decompress_safe_partial()

LZ4_FORCE_O2 int LZ4_decompress_safe_partial ( const char *  src,
char *  dst,
int  srcSize,
int  targetOutputSize,
int  dstCapacity 
)

LZ4_decompress_safe_partial() : 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. This can be useful to boost performance whenever only the beginning of a block is required.

Returns
: the number of bytes decoded in dst (necessarily <= targetOutputSize) If source stream is detected malformed, function returns a negative result.

Note 1 :

Returns
can be < targetOutputSize, if compressed block contains less data.

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 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. 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.

Definition at line 2179 of file lz4.c.

2180 {
2181  dstCapacity = MIN(targetOutputSize, dstCapacity);
2182  return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity,
2184  noDict, (BYTE*)dst, NULL, 0);
2185 }

References compressedSize, dst, endOnInputSize, LZ4_decompress_generic(), MIN, noDict, NULL, partial_decode, and src.

Referenced by FUZ_test(), LLVMFuzzerTestOneInput(), local_LZ4_decompress_safe_partial(), and main().

◆ LZ4_decompress_safe_usingDict()

int LZ4_decompress_safe_usingDict ( const char *  src,
char *  dst,
int  srcSize,
int  dstCapcity,
const char *  dictStart,
int  dictSize 
)

LZ4_decompress_*_usingDict() : These decoding functions work the same as a combination of LZ4_setStreamDecode() followed by LZ4_decompress_*_continue() They are stand-alone, and don't need an LZ4_streamDecode_t structure. Dictionary is presumed stable : it must remain accessible and unmodified during decompression. Performance tip : Decompression speed can be substantially increased when dst == dictStart + dictSize.

Definition at line 2404 of file lz4.c.

2405 {
2406  if (dictSize==0)
2408  if (dictStart+dictSize == dest) {
2409  if (dictSize >= 64 KB - 1) {
2411  }
2412  assert(dictSize >= 0);
2414  }
2415  assert(dictSize >= 0);
2416  return LZ4_decompress_safe_forceExtDict(source, dest, compressedSize, maxOutputSize, dictStart, (size_t)dictSize);
2417 }

References assert, compressedSize, dest, KB, LZ4_decompress_safe(), LZ4_decompress_safe_forceExtDict(), LZ4_decompress_safe_withPrefix64k(), LZ4_decompress_safe_withSmallPrefix(), maxOutputSize, and source.

Referenced by BMK_benchMem(), FUZ_test(), FUZ_unitTests(), LLVMFuzzerTestOneInput(), local_LZ4_decompress_safe_usingDict(), and LZ4F_decompress().

◆ LZ4_decompress_safe_withPrefix64k()

LZ4_FORCE_O2 int LZ4_decompress_safe_withPrefix64k ( const char *  source,
char *  dest,
int  compressedSize,
int  maxOutputSize 
)

◆ LZ4_decompress_safe_withSmallPrefix()

static LZ4_FORCE_O2 int LZ4_decompress_safe_withSmallPrefix ( const char *  source,
char *  dest,
int  compressedSize,
int  maxOutputSize,
size_t  prefixSize 
)
static

◆ LZ4_freeStream()

int LZ4_freeStream ( LZ4_stream_t LZ4_stream)

Definition at line 1465 of file lz4.c.

1466 {
1467  if (!LZ4_stream) return 0; /* support free on NULL */
1468  DEBUGLOG(5, "LZ4_freeStream %p", LZ4_stream);
1470  return (0);
1471 }

References DEBUGLOG, FREEMEM, and LZ4_stream.

Referenced by FUZ_unitTests(), LZ4_compressCleanupStream(), LZ4F_freeCDict(), state_attachDictRoundTrip(), state_free(), and test_compress().

◆ LZ4_freeStreamDecode()

int LZ4_freeStreamDecode ( LZ4_streamDecode_t LZ4_stream)

Definition at line 2272 of file lz4.c.

2273 {
2274  if (LZ4_stream == NULL) { return 0; } /* support free on NULL */
2276  return 0;
2277 }

References FREEMEM, LZ4_stream, and NULL.

Referenced by state_free(), and test_decompress().

◆ LZ4_getIndexOnHash()

LZ4_FORCE_INLINE U32 LZ4_getIndexOnHash ( U32  h,
const void *  tableBase,
tableType_t  tableType 
)

Definition at line 773 of file lz4.c.

774 {
776  if (tableType == byU32) {
777  const U32* const hashTable = (const U32*) tableBase;
778  assert(h < (1U << (LZ4_MEMORY_USAGE-2)));
779  return hashTable[h];
780  }
781  if (tableType == byU16) {
782  const U16* const hashTable = (const U16*) tableBase;
783  assert(h < (1U << (LZ4_MEMORY_USAGE-1)));
784  return hashTable[h];
785  }
786  assert(0); return 0; /* forbidden case */
787 }
#define LZ4_MEMORY_USAGE
Definition: lz4.h:127

References assert, byU16, byU32, h, LZ4_MEMORY_USAGE, and LZ4_STATIC_ASSERT.

Referenced by LZ4_compress_generic_validated().

◆ LZ4_getPosition()

LZ4_FORCE_INLINE const BYTE* LZ4_getPosition ( const BYTE p,
const void *  tableBase,
tableType_t  tableType,
const BYTE srcBase 
)

Definition at line 797 of file lz4.c.

800 {
801  U32 const h = LZ4_hashPosition(p, tableType);
802  return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase);
803 }

References h, LZ4_getPositionOnHash(), LZ4_hashPosition(), and p.

Referenced by LZ4_compress_generic_validated().

◆ LZ4_getPositionOnHash()

static const BYTE* LZ4_getPositionOnHash ( U32  h,
const void *  tableBase,
tableType_t  tableType,
const BYTE srcBase 
)
static

Definition at line 789 of file lz4.c.

790 {
791  if (tableType == byPtr) { const BYTE* const* hashTable = (const BYTE* const*) tableBase; return hashTable[h]; }
792  if (tableType == byU32) { const U32* const hashTable = (const U32*) tableBase; return hashTable[h] + srcBase; }
793  { const U16* const hashTable = (const U16*) tableBase; return hashTable[h] + srcBase; } /* default, to ensure a return */
794 }

References byPtr, byU32, and h.

Referenced by LZ4_compress_generic_validated(), and LZ4_getPosition().

◆ LZ4_hash4()

LZ4_FORCE_INLINE U32 LZ4_hash4 ( U32  sequence,
tableType_t const  tableType 
)

Definition at line 698 of file lz4.c.

699 {
700  if (tableType == byU16)
701  return ((sequence * 2654435761U) >> ((MINMATCH*8)-(LZ4_HASHLOG+1)));
702  else
703  return ((sequence * 2654435761U) >> ((MINMATCH*8)-LZ4_HASHLOG));
704 }
#define LZ4_HASHLOG
Definition: lz4.h:578

References byU16, LZ4_HASHLOG, and MINMATCH.

Referenced by LZ4_hashPosition().

◆ LZ4_hash5()

LZ4_FORCE_INLINE U32 LZ4_hash5 ( U64  sequence,
tableType_t const  tableType 
)

Definition at line 706 of file lz4.c.

707 {
708  const U32 hashLog = (tableType == byU16) ? LZ4_HASHLOG+1 : LZ4_HASHLOG;
709  if (LZ4_isLittleEndian()) {
710  const U64 prime5bytes = 889523592379ULL;
711  return (U32)(((sequence << 24) * prime5bytes) >> (64 - hashLog));
712  } else {
713  const U64 prime8bytes = 11400714785074694791ULL;
714  return (U32)(((sequence >> 24) * prime8bytes) >> (64 - hashLog));
715  }
716 }
unsigned long long U64
Definition: lz4.c:290
static unsigned LZ4_isLittleEndian(void)
Definition: lz4.c:325

References byU16, LZ4_HASHLOG, and LZ4_isLittleEndian().

Referenced by LZ4_hashPosition().

◆ LZ4_hashPosition()

LZ4_FORCE_INLINE U32 LZ4_hashPosition ( const void *const  p,
tableType_t const  tableType 
)

Definition at line 718 of file lz4.c.

719 {
720  if ((sizeof(reg_t)==8) && (tableType != byU16)) return LZ4_hash5(LZ4_read_ARCH(p), tableType);
721  return LZ4_hash4(LZ4_read32(p), tableType);
722 }
LZ4_FORCE_INLINE U32 LZ4_hash4(U32 sequence, tableType_t const tableType)
Definition: lz4.c:698
LZ4_FORCE_INLINE U32 LZ4_hash5(U64 sequence, tableType_t const tableType)
Definition: lz4.c:706

References byU16, LZ4_hash4(), LZ4_hash5(), LZ4_read32(), LZ4_read_ARCH(), and p.

Referenced by LZ4_compress_generic_validated(), LZ4_getPosition(), and LZ4_putPosition().

◆ LZ4_initStream()

LZ4_stream_t* LZ4_initStream ( void *  buffer,
size_t  size 
)

LZ4_initStream() : v1.9.0+ An LZ4_stream_t structure must be initialized at least once. This is automatically done when invoking LZ4_createStream(), but it's not when the structure is simply declared on stack (for example).

Use LZ4_initStream() to properly initialize a newly declared LZ4_stream_t. It can also initialize any arbitrary buffer of sufficient size, and will

Returns
a pointer of proper type upon initialization.

Note : initialization fails if size and alignment conditions are not respected. In which case, the function will

Returns
NULL. Note2: An LZ4_stream_t structure guarantees correct alignment and size. Note3: Before v1.9.0, use LZ4_resetStream() instead

Definition at line 1443 of file lz4.c.

1444 {
1445  DEBUGLOG(5, "LZ4_initStream");
1446  if (buffer == NULL) { return NULL; }
1447  if (size < sizeof(LZ4_stream_t)) { return NULL; }
1449  MEM_INIT(buffer, 0, sizeof(LZ4_stream_t_internal));
1450  return (LZ4_stream_t*)buffer;
1451 }
struct buffer buffer
voidpf void uLong size
Definition: ioapi.h:138
#define MEM_INIT(p, v, s)
Definition: lz4.c:208
static int LZ4_isAligned(const void *ptr, size_t alignment)
Definition: lz4.c:264
static size_t LZ4_stream_t_alignment(void)
Definition: lz4.c:1433
Definition: buffer.h:15

References DEBUGLOG, LZ4_isAligned(), LZ4_stream_t_alignment(), MEM_INIT, and NULL.

Referenced by FUZ_test(), FUZ_unitTests(), local_LZ4_createStream(), local_LZ4_resetDictT(), LZ4_compress_destSize_extState(), LZ4_compress_fast_extState(), LZ4_createStream(), LZ4F_compressBegin_usingCDict(), LZ4F_compressFrame(), and test_compress().

◆ LZ4_isAligned()

static int LZ4_isAligned ( const void *  ptr,
size_t  alignment 
)
static

Definition at line 264 of file lz4.c.

265 {
266  return ((size_t)ptr & (alignment -1)) == 0;
267 }

Referenced by LZ4_compress_HC_extStateHC_fastReset(), LZ4_initStream(), and LZ4_initStreamHC().

◆ LZ4_isLittleEndian()

static unsigned LZ4_isLittleEndian ( void  )
static

Definition at line 325 of file lz4.c.

326 {
327  const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
328  return one.c[0];
329 }
#define c(i)
Definition: sha256.c:43

References c.

Referenced by LZ4_hash5(), LZ4_NbCommonBytes(), LZ4_readLE16(), LZ4_writeLE16(), and LZ4HC_countPattern().

◆ LZ4_loadDict()

int LZ4_loadDict ( LZ4_stream_t streamPtr,
const char *  dictionary,
int  dictSize 
)

LZ4_loadDict() : Use this function to reference a static dictionary into LZ4_stream_t. The dictionary must remain available during compression. LZ4_loadDict() triggers a reset, so any previous data will be forgotten. The same dictionary will have to be loaded on decompression side for successful decoding. Dictionary are useful for better compression of small data (KB range). While LZ4 accept any input as dictionary, results are generally better when using Zstandard's Dictionary Builder. Loading a size of 0 is allowed, and is the same as reset.

Returns
: loaded dictionary size, in bytes (necessarily <= 64 KB)

Definition at line 1475 of file lz4.c.

1476 {
1477  LZ4_stream_t_internal* dict = &LZ4_dict->internal_donotuse;
1478  const tableType_t tableType = byU32;
1479  const BYTE* p = (const BYTE*)dictionary;
1480  const BYTE* const dictEnd = p + dictSize;
1481  const BYTE* base;
1482 
1483  DEBUGLOG(4, "LZ4_loadDict (%i bytes from %p into %p)", dictSize, dictionary, LZ4_dict);
1484 
1485  /* It's necessary to reset the context,
1486  * and not just continue it with prepareTable()
1487  * to avoid any risk of generating overflowing matchIndex
1488  * when compressing using this dictionary */
1489  LZ4_resetStream(LZ4_dict);
1490 
1491  /* We always increment the offset by 64 KB, since, if the dict is longer,
1492  * we truncate it to the last 64k, and if it's shorter, we still want to
1493  * advance by a whole window length so we can provide the guarantee that
1494  * there are only valid offsets in the window, which allows an optimization
1495  * in LZ4_compress_fast_continue() where it uses noDictIssue even when the
1496  * dictionary isn't a full 64k. */
1497  dict->currentOffset += 64 KB;
1498 
1499  if (dictSize < (int)HASH_UNIT) {
1500  return 0;
1501  }
1502 
1503  if ((dictEnd - p) > 64 KB) p = dictEnd - 64 KB;
1504  base = dictEnd - dict->currentOffset;
1505  dict->dictionary = p;
1506  dict->dictSize = (U32)(dictEnd - p);
1507  dict->tableType = (U32)tableType;
1508 
1509  while (p <= dictEnd-HASH_UNIT) {
1510  LZ4_putPosition(p, dict->hashTable, tableType, base);
1511  p+=3;
1512  }
1513 
1514  return (int)dict->dictSize;
1515 }
void LZ4_resetStream(LZ4_stream_t *LZ4_stream)
Definition: lz4.c:1455
#define HASH_UNIT
Definition: lz4.c:1474

References byU32, LZ4_stream_t_internal::currentOffset, DEBUGLOG, LZ4_stream_t_internal::dictionary, LZ4_stream_t_internal::dictSize, HASH_UNIT, LZ4_stream_t_internal::hashTable, LZ4_stream_u::internal_donotuse, KB, LZ4_putPosition(), LZ4_resetStream(), p, and LZ4_stream_t_internal::tableType.

Referenced by fullSpeedBench(), FUZ_test(), LZ4_compressInitStream(), LZ4F_createCDict(), state_attachDictRoundTrip(), state_loadDictRoundTrip(), and test_compress().

◆ LZ4_NbCommonBytes()

static unsigned LZ4_NbCommonBytes ( reg_t  val)
static

Definition at line 513 of file lz4.c.

514 {
515  assert(val != 0);
516  if (LZ4_isLittleEndian()) {
517  if (sizeof(val) == 8) {
518 # if defined(_MSC_VER) && (_MSC_VER >= 1800) && defined(_M_AMD64) && !defined(LZ4_FORCE_SW_BITCOUNT)
519  /* x64 CPUS without BMI support interpret `TZCNT` as `REP BSF` */
520  return (unsigned)_tzcnt_u64(val) >> 3;
521 # elif defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
522  unsigned long r = 0;
523  _BitScanForward64(&r, (U64)val);
524  return (unsigned)r >> 3;
525 # elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
526  ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
527  !defined(LZ4_FORCE_SW_BITCOUNT)
528  return (unsigned)__builtin_ctzll((U64)val) >> 3;
529 # else
530  const U64 m = 0x0101010101010101ULL;
531  val ^= val - 1;
532  return (unsigned)(((U64)((val & (m - 1)) * m)) >> 56);
533 # endif
534  } else /* 32 bits */ {
535 # if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(LZ4_FORCE_SW_BITCOUNT)
536  unsigned long r;
537  _BitScanForward(&r, (U32)val);
538  return (unsigned)r >> 3;
539 # elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
540  ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
541  !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT)
542  return (unsigned)__builtin_ctz((U32)val) >> 3;
543 # else
544  const U32 m = 0x01010101;
545  return (unsigned)((((val - 1) ^ val) & (m - 1)) * m) >> 24;
546 # endif
547  }
548  } else /* Big Endian CPU */ {
549  if (sizeof(val)==8) {
550 # if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
551  ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
552  !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT)
553  return (unsigned)__builtin_clzll((U64)val) >> 3;
554 # else
555 #if 1
556  /* this method is probably faster,
557  * but adds a 128 bytes lookup table */
558  static const unsigned char ctz7_tab[128] = {
559  7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
560  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
561  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
562  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
563  6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
564  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
565  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
566  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
567  };
568  U64 const mask = 0x0101010101010101ULL;
569  U64 const t = (((val >> 8) - mask) | val) & mask;
570  return ctz7_tab[(t * 0x0080402010080402ULL) >> 57];
571 #else
572  /* this method doesn't consume memory space like the previous one,
573  * but it contains several branches,
574  * that may end up slowing execution */
575  static const U32 by32 = sizeof(val)*4; /* 32 on 64 bits (goal), 16 on 32 bits.
576  Just to avoid some static analyzer complaining about shift by 32 on 32-bits target.
577  Note that this code path is never triggered in 32-bits mode. */
578  unsigned r;
579  if (!(val>>by32)) { r=4; } else { r=0; val>>=by32; }
580  if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
581  r += (!val);
582  return r;
583 #endif
584 # endif
585  } else /* 32 bits */ {
586 # if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
587  ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
588  !defined(LZ4_FORCE_SW_BITCOUNT)
589  return (unsigned)__builtin_clz((U32)val) >> 3;
590 # else
591  val >>= 8;
592  val = ((((val + 0x00FFFF00) | 0x00FFFFFF) + val) |
593  (val + 0x00FF0000)) >> 24;
594  return (unsigned)val ^ 3;
595 # endif
596  }
597  }
598 }
#define mask()
ut16 val
Definition: armass64_const.h:6
#define r
Definition: crypto_rc6.c:12

References assert, LZ4_isLittleEndian(), regress::m, mask, r, and val.

Referenced by LZ4_count(), and LZ4HC_countPattern().

◆ LZ4_prepareTable()

LZ4_FORCE_INLINE void LZ4_prepareTable ( LZ4_stream_t_internal *const  cctx,
const int  inputSize,
const tableType_t  tableType 
)

Definition at line 806 of file lz4.c.

808  {
809  /* If the table hasn't been used, it's guaranteed to be zeroed out, and is
810  * therefore safe to use no matter what mode we're in. Otherwise, we figure
811  * out if it's safe to leave as is or whether it needs to be reset.
812  */
813  if ((tableType_t)cctx->tableType != clearedTable) {
814  assert(inputSize >= 0);
815  if ((tableType_t)cctx->tableType != tableType
816  || ((tableType == byU16) && cctx->currentOffset + (unsigned)inputSize >= 0xFFFFU)
817  || ((tableType == byU32) && cctx->currentOffset > 1 GB)
818  || tableType == byPtr
819  || inputSize >= 4 KB)
820  {
821  DEBUGLOG(4, "LZ4_prepareTable: Resetting table in %p", cctx);
823  cctx->currentOffset = 0;
824  cctx->tableType = (U32)clearedTable;
825  } else {
826  DEBUGLOG(4, "LZ4_prepareTable: Re-use hash table (no reset)");
827  }
828  }
829 
830  /* Adding a gap, so all previous entries are > LZ4_DISTANCE_MAX back, is faster
831  * than compressing without a gap. However, compressing with
832  * currentOffset == 0 is faster still, so we preserve that case.
833  */
834  if (cctx->currentOffset != 0 && tableType == byU32) {
835  DEBUGLOG(5, "LZ4_prepareTable: adding 64KB to currentOffset");
836  cctx->currentOffset += 64 KB;
837  }
838 
839  /* Finally, clear history */
840  cctx->dictCtx = NULL;
841  cctx->dictionary = NULL;
842  cctx->dictSize = 0;
843 }
#define LZ4_HASHTABLESIZE
Definition: lz4.h:579

References assert, byPtr, byU16, byU32, clearedTable, LZ4_stream_t_internal::currentOffset, DEBUGLOG, LZ4_stream_t_internal::dictCtx, LZ4_stream_t_internal::dictionary, LZ4_stream_t_internal::dictSize, GB, LZ4_stream_t_internal::hashTable, inputSize, KB, LZ4_HASHTABLESIZE, MEM_INIT, NULL, and LZ4_stream_t_internal::tableType.

Referenced by LZ4_compress_fast_extState_fastReset(), and LZ4_resetStream_fast().

◆ LZ4_putIndexOnHash()

LZ4_FORCE_INLINE void LZ4_putIndexOnHash ( U32  idx,
U32  h,
void *  tableBase,
tableType_t const  tableType 
)

Definition at line 736 of file lz4.c.

737 {
738  switch (tableType)
739  {
740  default: /* fallthrough */
741  case clearedTable: /* fallthrough */
742  case byPtr: { /* illegal! */ assert(0); return; }
743  case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = idx; return; }
744  case byU16: { U16* hashTable = (U16*) tableBase; assert(idx < 65536); hashTable[h] = (U16)idx; return; }
745  }
746 }
int idx
Definition: setup.py:197

References assert, byPtr, byU16, byU32, clearedTable, h, and setup::idx.

Referenced by LZ4_compress_generic_validated().

◆ LZ4_putPosition()

LZ4_FORCE_INLINE void LZ4_putPosition ( const BYTE p,
void *  tableBase,
tableType_t  tableType,
const BYTE srcBase 
)

Definition at line 761 of file lz4.c.

762 {
763  U32 const h = LZ4_hashPosition(p, tableType);
764  LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase);
765 }

References h, LZ4_hashPosition(), LZ4_putPositionOnHash(), and p.

Referenced by LZ4_compress_generic_validated(), and LZ4_loadDict().

◆ LZ4_putPositionOnHash()

LZ4_FORCE_INLINE void LZ4_putPositionOnHash ( const BYTE p,
U32  h,
void *  tableBase,
tableType_t const  tableType,
const BYTE srcBase 
)

Definition at line 748 of file lz4.c.

751 {
752  switch (tableType)
753  {
754  case clearedTable: { /* illegal! */ assert(0); return; }
755  case byPtr: { const BYTE** hashTable = (const BYTE**)tableBase; hashTable[h] = p; return; }
756  case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = (U32)(p-srcBase); return; }
757  case byU16: { U16* hashTable = (U16*) tableBase; hashTable[h] = (U16)(p-srcBase); return; }
758  }
759 }

References assert, byPtr, byU16, byU32, clearedTable, h, and p.

Referenced by LZ4_compress_generic_validated(), and LZ4_putPosition().

◆ LZ4_read16()

static U16 LZ4_read16 ( const void *  memPtr)
static

Definition at line 357 of file lz4.c.

358 {
359  U16 val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val;
360 }

References LZ4_memcpy, and val.

Referenced by LZ4_count(), LZ4_readLE16(), and LZ4HC_InsertAndGetWiderMatch().

◆ LZ4_read32()

static U32 LZ4_read32 ( const void *  memPtr)
static

Definition at line 362 of file lz4.c.

363 {
364  U32 val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val;
365 }

References LZ4_memcpy, and val.

Referenced by LZ4_compress_generic_validated(), LZ4_count(), LZ4_hashPosition(), LZ4HC_hashPtr(), LZ4HC_InsertAndGetWiderMatch(), and LZ4HC_reverseCountPattern().

◆ LZ4_read_ARCH()

static reg_t LZ4_read_ARCH ( const void *  memPtr)
static

Definition at line 367 of file lz4.c.

368 {
369  reg_t val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val;
370 }

References LZ4_memcpy, and val.

Referenced by LZ4_count(), LZ4_hashPosition(), and LZ4HC_countPattern().

◆ LZ4_readLE16()

static U16 LZ4_readLE16 ( const void *  memPtr)
static

Definition at line 385 of file lz4.c.

386 {
387  if (LZ4_isLittleEndian()) {
388  return LZ4_read16(memPtr);
389  } else {
390  const BYTE* p = (const BYTE*)memPtr;
391  return (U16)((U16)p[0] + (p[1]<<8));
392  }
393 }

References LZ4_isLittleEndian(), LZ4_read16(), and p.

Referenced by LZ4_decompress_generic().

◆ LZ4_renormDictT()

static void LZ4_renormDictT ( LZ4_stream_t_internal LZ4_dict,
int  nextSize 
)
static

Definition at line 1545 of file lz4.c.

1546 {
1547  assert(nextSize >= 0);
1548  if (LZ4_dict->currentOffset + (unsigned)nextSize > 0x80000000) { /* potential ptrdiff_t overflow (32-bits mode) */
1549  /* rescale hash table */
1550  U32 const delta = LZ4_dict->currentOffset - 64 KB;
1551  const BYTE* dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize;
1552  int i;
1553  DEBUGLOG(4, "LZ4_renormDictT");
1554  for (i=0; i<LZ4_HASH_SIZE_U32; i++) {
1555  if (LZ4_dict->hashTable[i] < delta) LZ4_dict->hashTable[i]=0;
1556  else LZ4_dict->hashTable[i] -= delta;
1557  }
1558  LZ4_dict->currentOffset = 64 KB;
1559  if (LZ4_dict->dictSize > 64 KB) LZ4_dict->dictSize = 64 KB;
1560  LZ4_dict->dictionary = dictEnd - LZ4_dict->dictSize;
1561  }
1562 }
#define LZ4_HASH_SIZE_U32
Definition: lz4.h:580
static st64 delta
Definition: vmenus.c:2425

References assert, LZ4_stream_t_internal::currentOffset, DEBUGLOG, delta, LZ4_stream_t_internal::dictionary, LZ4_stream_t_internal::dictSize, LZ4_stream_t_internal::hashTable, i, KB, and LZ4_HASH_SIZE_U32.

Referenced by LZ4_compress_fast_continue(), and LZ4_compress_forceExtDict().

◆ LZ4_resetStream()

void LZ4_resetStream ( LZ4_stream_t streamPtr)

LZ4_resetStream() : An LZ4_stream_t structure must be initialized at least once. This is done with LZ4_initStream(), or LZ4_resetStream(). Consider switching to LZ4_initStream(), invoking LZ4_resetStream() will trigger deprecation warnings in the future.

Definition at line 1455 of file lz4.c.

1456 {
1457  DEBUGLOG(5, "LZ4_resetStream (ctx:%p)", LZ4_stream);
1459 }

References DEBUGLOG, LZ4_stream, and MEM_INIT.

Referenced by LZ4_loadDict(), and LZ4_resetStreamState().

◆ LZ4_resetStream_fast()

void LZ4_resetStream_fast ( LZ4_stream_t streamPtr)

LZ4_resetStream_fast() : v1.9.0+ Use this to prepare an LZ4_stream_t for a new chain of dependent blocks (e.g., LZ4_compress_fast_continue()).

An LZ4_stream_t must be initialized once before usage. This is automatically done when created by LZ4_createStream(). However, should the LZ4_stream_t be simply declared on stack (for example), it's necessary to initialize it first, using LZ4_initStream().

After init, start any new stream with LZ4_resetStream_fast(). A same LZ4_stream_t can be re-used multiple times consecutively and compress multiple streams, provided that it starts each new stream with LZ4_resetStream_fast().

LZ4_resetStream_fast() is much faster than LZ4_initStream(), but is not compatible with memory regions containing garbage data.

Note: it's only useful to call LZ4_resetStream_fast() in the context of streaming compression. The extState functions perform their own resets. Invoking LZ4_resetStream_fast() before is redundant, and even counterproductive.

Definition at line 1461 of file lz4.c.

1461  {
1462  LZ4_prepareTable(&(ctx->internal_donotuse), 0, byU32);
1463 }

References byU32, and LZ4_prepareTable().

Referenced by FUZ_test(), FUZ_unitTests(), LZ4_compressResetStream(), LZ4F_initStream(), and state_reset().

◆ LZ4_resetStreamState()

int LZ4_resetStreamState ( void *  state,
char *  inputBuffer 
)

Definition at line 2476 of file lz4.c.

2477 {
2478  (void)inputBuffer;
2480  return 0;
2481 }

References inputBuffer, and LZ4_resetStream().

◆ LZ4_saveDict()

int LZ4_saveDict ( LZ4_stream_t LZ4_dict,
char *  safeBuffer,
int  dictSize 
)

LZ4_saveDict() : If previously compressed data block is not guaranteed to remain available at its memory location, save it into a safer place (char* safeBuffer). Note : you don't need to call LZ4_loadDict() afterwards, dictionary is immediately usable, you can therefore call LZ4_compress_fast_continue(). Return : saved dictionary size in bytes (necessarily <= dictSize), or 0 if error.

Definition at line 1668 of file lz4.c.

1669 {
1670  LZ4_stream_t_internal* const dict = &LZ4_dict->internal_donotuse;
1671  const BYTE* const previousDictEnd = dict->dictionary + dict->dictSize;
1672 
1673  if ((U32)dictSize > 64 KB) { dictSize = 64 KB; } /* useless to define a dictionary > 64 KB */
1674  if ((U32)dictSize > dict->dictSize) { dictSize = (int)dict->dictSize; }
1675 
1676  if (safeBuffer == NULL) assert(dictSize == 0);
1677  if (dictSize > 0)
1678  memmove(safeBuffer, previousDictEnd - dictSize, dictSize);
1679 
1680  dict->dictionary = (const BYTE*)safeBuffer;
1681  dict->dictSize = (U32)dictSize;
1682 
1683  return dictSize;
1684 }

References assert, LZ4_stream_t_internal::dictionary, LZ4_stream_t_internal::dictSize, int, LZ4_stream_u::internal_donotuse, KB, and NULL.

Referenced by FUZ_unitTests(), local_LZ4_saveDict(), and LZ4F_localSaveDict().

◆ LZ4_setStreamDecode()

int LZ4_setStreamDecode ( LZ4_streamDecode_t LZ4_streamDecode,
const char *  dictionary,
int  dictSize 
)

LZ4_setStreamDecode() : Use this function to instruct where to find the dictionary. This function is not necessary if previous data is still available where it was decoded. Loading a size of 0 is allowed (same effect as no dictionary).

Returns
: 1 if OK, 0 if error

Definition at line 2285 of file lz4.c.

2286 {
2287  LZ4_streamDecode_t_internal* lz4sd = &LZ4_streamDecode->internal_donotuse;
2288  lz4sd->prefixSize = (size_t) dictSize;
2289  lz4sd->prefixEnd = (const BYTE*) dictionary + dictSize;
2290  lz4sd->externalDict = NULL;
2291  lz4sd->extDictSize = 0;
2292  return 1;
2293 }

References LZ4_streamDecode_t_internal::extDictSize, LZ4_streamDecode_t_internal::externalDict, LZ4_streamDecode_u::internal_donotuse, NULL, LZ4_streamDecode_t_internal::prefixEnd, and LZ4_streamDecode_t_internal::prefixSize.

Referenced by FUZ_unitTests(), state_attachDictHCRoundTrip(), state_attachDictRoundTrip(), state_loadDictHCRoundTrip(), state_loadDictRoundTrip(), state_reset(), and test_decompress().

◆ LZ4_sizeofState()

int LZ4_sizeofState ( void  )

LZ4_compress_fast_extState() : Same as LZ4_compress_fast(), using an externally allocated memory space for its state. Use LZ4_sizeofState() to know how much memory must be allocated, and allocate it on 8-bytes boundaries (using malloc() typically). Then, provide this buffer as void* state to compression function.

Definition at line 675 of file lz4.c.

675 { return LZ4_STREAMSIZE; }

References LZ4_STREAMSIZE.

Referenced by FUZ_test().

◆ LZ4_sizeofStreamState()

int LZ4_sizeofStreamState ( void  )

Definition at line 2474 of file lz4.c.

2474 { return LZ4_STREAMSIZE; }

References LZ4_STREAMSIZE.

◆ LZ4_slideInputBuffer()

char* LZ4_slideInputBuffer ( void *  state)

Definition at line 2489 of file lz4.c.

2490 {
2491  /* avoid const char * -> char * conversion warning */
2492  return (char *)(uptrval)((LZ4_stream_t*)state)->internal_donotuse.dictionary;
2493 }

◆ LZ4_stream_t_alignment()

static size_t LZ4_stream_t_alignment ( void  )
static

Definition at line 1433 of file lz4.c.

1434 {
1435 #if LZ4_ALIGN_TEST
1436  typedef struct { char c; LZ4_stream_t t; } t_a;
1437  return sizeof(t_a) - sizeof(LZ4_stream_t);
1438 #else
1439  return 1; /* effectively disabled */
1440 #endif
1441 }

References c.

Referenced by LZ4_initStream().

◆ LZ4_uncompress()

int LZ4_uncompress ( const char *  source,
char *  dest,
int  outputSize 
)

Definition at line 2463 of file lz4.c.

2464 {
2466 }

References dest, LZ4_decompress_fast(), outputSize, and source.

◆ LZ4_uncompress_unknownOutputSize()

int LZ4_uncompress_unknownOutputSize ( const char *  source,
char *  dest,
int  isize,
int  maxOutputSize 
)

Definition at line 2467 of file lz4.c.

2468 {
2470 }

References dest, isize, LZ4_decompress_safe(), maxOutputSize, and source.

◆ LZ4_versionNumber()

int LZ4_versionNumber ( void  )

library version number; useful to check dll version

Definition at line 672 of file lz4.c.

672 { return LZ4_VERSION_NUMBER; }
#define LZ4_VERSION_NUMBER
Definition: lz4.h:105

References LZ4_VERSION_NUMBER.

Referenced by main().

◆ LZ4_versionString()

const char* LZ4_versionString ( void  )

library version string; useful to check dll version

Definition at line 673 of file lz4.c.

673 { return LZ4_VERSION_STRING; }
#define LZ4_VERSION_STRING
Definition: lz4.h:110

References LZ4_VERSION_STRING.

Referenced by main().

◆ LZ4_wildCopy8()

LZ4_FORCE_INLINE void LZ4_wildCopy8 ( void *  dstPtr,
const void *  srcPtr,
void *  dstEnd 
)

Definition at line 408 of file lz4.c.

409 {
410  BYTE* d = (BYTE*)dstPtr;
411  const BYTE* s = (const BYTE*)srcPtr;
412  BYTE* const e = (BYTE*)dstEnd;
413 
414  do { LZ4_memcpy(d,s,8); d+=8; s+=8; } while (d<e);
415 }
#define e(frag)
#define d(i)
Definition: sha256.c:44

References d, e, LZ4_memcpy, and s.

Referenced by LZ4_compress_generic_validated(), LZ4_decompress_generic(), and LZ4HC_encodeSequence().

◆ LZ4_write16()

static void LZ4_write16 ( void *  memPtr,
U16  value 
)
static

Definition at line 372 of file lz4.c.

373 {
374  LZ4_memcpy(memPtr, &value, sizeof(value));
375 }
static int value
Definition: cmd_api.c:93

References LZ4_memcpy, and value.

Referenced by LZ4_writeLE16().

◆ LZ4_write32()

static void LZ4_write32 ( void *  memPtr,
U32  value 
)
static

Definition at line 377 of file lz4.c.

378 {
379  LZ4_memcpy(memPtr, &value, sizeof(value));
380 }

References LZ4_memcpy, and value.

Referenced by LZ4_compress_generic_validated(), and LZ4_decompress_generic().

◆ LZ4_writeLE16()

static void LZ4_writeLE16 ( void *  memPtr,
U16  value 
)
static

Definition at line 395 of file lz4.c.

396 {
397  if (LZ4_isLittleEndian()) {
398  LZ4_write16(memPtr, value);
399  } else {
400  BYTE* p = (BYTE*)memPtr;
401  p[0] = (BYTE) value;
402  p[1] = (BYTE)(value>>8);
403  }
404 }
static void LZ4_write16(void *memPtr, U16 value)
Definition: lz4.c:372

References LZ4_isLittleEndian(), LZ4_write16(), p, and value.

Referenced by LZ4_compress_generic_validated(), and LZ4HC_encodeSequence().

◆ read_variable_length()

LZ4_FORCE_INLINE unsigned read_variable_length ( const BYTE **  ip,
const BYTE lencheck,
int  loop_check,
int  initial_check,
variable_length_error error 
)

Definition at line 1708 of file lz4.c.

1711 {
1712  U32 length = 0;
1713  U32 s;
1714  if (initial_check && unlikely((*ip) >= lencheck)) { /* overflow detection */
1715  *error = initial_error;
1716  return length;
1717  }
1718  do {
1719  s = **ip;
1720  (*ip)++;
1721  length += s;
1722  if (loop_check && unlikely((*ip) >= lencheck)) { /* overflow detection */
1723  *error = loop_error;
1724  return length;
1725  }
1726  } while (s==255);
1727 
1728  return length;
1729 }

References error(), initial_error, ip, length, loop_error, s, and unlikely.

Referenced by LZ4_decompress_generic().

Variable Documentation

◆ dec64table

const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3}
static

Definition at line 418 of file lz4.c.

Referenced by LZ4_decompress_generic().

◆ inc32table

const unsigned inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4}
static

Definition at line 417 of file lz4.c.

Referenced by LZ4_decompress_generic().

◆ LZ4_64Klimit

◆ LZ4_minLength

const int LZ4_minLength = (MFLIMIT+1)
static

Definition at line 221 of file lz4.c.

Referenced by LZ4_compress_generic_validated(), and LZ4HC_compress_hashChain().

◆ LZ4_skipTrigger

const U32 LZ4_skipTrigger = 6
static

Definition at line 634 of file lz4.c.

Referenced by LZ4_compress_generic_validated().