Coverage Report

Created: 2023-03-26 06:13

/src/lz4/lib/lz4.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   LZ4 - Fast LZ compression algorithm
3
   Copyright (C) 2011-2020, Yann Collet.
4
5
   BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6
7
   Redistribution and use in source and binary forms, with or without
8
   modification, are permitted provided that the following conditions are
9
   met:
10
11
       * Redistributions of source code must retain the above copyright
12
   notice, this list of conditions and the following disclaimer.
13
       * Redistributions in binary form must reproduce the above
14
   copyright notice, this list of conditions and the following disclaimer
15
   in the documentation and/or other materials provided with the
16
   distribution.
17
18
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
   You can contact the author at :
31
    - LZ4 homepage : http://www.lz4.org
32
    - LZ4 source repository : https://github.com/lz4/lz4
33
*/
34
35
/*-************************************
36
*  Tuning parameters
37
**************************************/
38
/*
39
 * LZ4_HEAPMODE :
40
 * Select how stateless compression functions like `LZ4_compress_default()`
41
 * allocate memory for their hash table,
42
 * in memory stack (0:default, fastest), or in memory heap (1:requires malloc()).
43
 */
44
#ifndef LZ4_HEAPMODE
45
#  define LZ4_HEAPMODE 0
46
#endif
47
48
/*
49
 * LZ4_ACCELERATION_DEFAULT :
50
 * Select "acceleration" for LZ4_compress_fast() when parameter value <= 0
51
 */
52
0
#define LZ4_ACCELERATION_DEFAULT 1
53
/*
54
 * LZ4_ACCELERATION_MAX :
55
 * Any "acceleration" value higher than this threshold
56
 * get treated as LZ4_ACCELERATION_MAX instead (fix #876)
57
 */
58
1.81k
#define LZ4_ACCELERATION_MAX 65537
59
60
61
/*-************************************
62
*  CPU Feature Detection
63
**************************************/
64
/* LZ4_FORCE_MEMORY_ACCESS
65
 * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
66
 * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
67
 * The below switch allow to select different access method for improved performance.
68
 * Method 0 (default) : use `memcpy()`. Safe and portable.
69
 * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).
70
 *            This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
71
 * Method 2 : direct access. This method is portable but violate C standard.
72
 *            It can generate buggy code on targets which assembly generation depends on alignment.
73
 *            But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
74
 * See https://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
75
 * Prefer these methods in priority order (0 > 1 > 2)
76
 */
77
#ifndef LZ4_FORCE_MEMORY_ACCESS   /* can be defined externally */
78
#  if defined(__GNUC__) && \
79
  ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) \
80
  || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
81
#    define LZ4_FORCE_MEMORY_ACCESS 2
82
#  elif (defined(__INTEL_COMPILER) && !defined(_WIN32)) || defined(__GNUC__)
83
#    define LZ4_FORCE_MEMORY_ACCESS 1
84
#  endif
85
#endif
86
87
/*
88
 * LZ4_FORCE_SW_BITCOUNT
89
 * Define this parameter if your target system or compiler does not support hardware bit count
90
 */
91
#if defined(_MSC_VER) && defined(_WIN32_WCE)   /* Visual Studio for WinCE doesn't support Hardware bit count */
92
#  undef  LZ4_FORCE_SW_BITCOUNT  /* avoid double def */
93
#  define LZ4_FORCE_SW_BITCOUNT
94
#endif
95
96
97
98
/*-************************************
99
*  Dependency
100
**************************************/
101
/*
102
 * LZ4_SRC_INCLUDED:
103
 * Amalgamation flag, whether lz4.c is included
104
 */
105
#ifndef LZ4_SRC_INCLUDED
106
#  define LZ4_SRC_INCLUDED 1
107
#endif
108
109
#ifndef LZ4_STATIC_LINKING_ONLY
110
#define LZ4_STATIC_LINKING_ONLY
111
#endif
112
113
#ifndef LZ4_DISABLE_DEPRECATE_WARNINGS
114
#define LZ4_DISABLE_DEPRECATE_WARNINGS /* due to LZ4_decompress_safe_withPrefix64k */
115
#endif
116
117
#define LZ4_STATIC_LINKING_ONLY  /* LZ4_DISTANCE_MAX */
118
#include "lz4.h"
119
/* see also "memory routines" below */
120
121
122
/*-************************************
123
*  Compiler Options
124
**************************************/
125
#if defined(_MSC_VER) && (_MSC_VER >= 1400)  /* Visual Studio 2005+ */
126
#  include <intrin.h>               /* only present in VS2005+ */
127
#  pragma warning(disable : 4127)   /* disable: C4127: conditional expression is constant */
128
#  pragma warning(disable : 6237)   /* disable: C6237: conditional expression is always 0 */
129
#endif  /* _MSC_VER */
130
131
#ifndef LZ4_FORCE_INLINE
132
#  ifdef _MSC_VER    /* Visual Studio */
133
#    define LZ4_FORCE_INLINE static __forceinline
134
#  else
135
#    if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L   /* C99 */
136
#      ifdef __GNUC__
137
#        define LZ4_FORCE_INLINE static inline __attribute__((always_inline))
138
#      else
139
#        define LZ4_FORCE_INLINE static inline
140
#      endif
141
#    else
142
#      define LZ4_FORCE_INLINE static
143
#    endif /* __STDC_VERSION__ */
144
#  endif  /* _MSC_VER */
145
#endif /* LZ4_FORCE_INLINE */
146
147
/* LZ4_FORCE_O2 and LZ4_FORCE_INLINE
148
 * gcc on ppc64le generates an unrolled SIMDized loop for LZ4_wildCopy8,
149
 * together with a simple 8-byte copy loop as a fall-back path.
150
 * However, this optimization hurts the decompression speed by >30%,
151
 * because the execution does not go to the optimized loop
152
 * for typical compressible data, and all of the preamble checks
153
 * before going to the fall-back path become useless overhead.
154
 * This optimization happens only with the -O3 flag, and -O2 generates
155
 * a simple 8-byte copy loop.
156
 * With gcc on ppc64le, all of the LZ4_decompress_* and LZ4_wildCopy8
157
 * functions are annotated with __attribute__((optimize("O2"))),
158
 * and also LZ4_wildCopy8 is forcibly inlined, so that the O2 attribute
159
 * of LZ4_wildCopy8 does not affect the compression speed.
160
 */
161
#if defined(__PPC64__) && defined(__LITTLE_ENDIAN__) && defined(__GNUC__) && !defined(__clang__)
162
#  define LZ4_FORCE_O2  __attribute__((optimize("O2")))
163
#  undef LZ4_FORCE_INLINE
164
#  define LZ4_FORCE_INLINE  static __inline __attribute__((optimize("O2"),always_inline))
165
#else
166
#  define LZ4_FORCE_O2
167
#endif
168
169
#if (defined(__GNUC__) && (__GNUC__ >= 3)) || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) || defined(__clang__)
170
18.5M
#  define expect(expr,value)    (__builtin_expect ((expr),(value)) )
171
#else
172
#  define expect(expr,value)    (expr)
173
#endif
174
175
#ifndef likely
176
688k
#define likely(expr)     expect((expr) != 0, 1)
177
#endif
178
#ifndef unlikely
179
10.0M
#define unlikely(expr)   expect((expr) != 0, 0)
180
#endif
181
182
/* Should the alignment test prove unreliable, for some reason,
183
 * it can be disabled by setting LZ4_ALIGN_TEST to 0 */
184
#ifndef LZ4_ALIGN_TEST  /* can be externally provided */
185
# define LZ4_ALIGN_TEST 1
186
#endif
187
188
189
/*-************************************
190
*  Memory routines
191
**************************************/
192
193
/*! LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION :
194
 *  Disable relatively high-level LZ4/HC functions that use dynamic memory
195
 *  allocation functions (malloc(), calloc(), free()).
196
 *
197
 *  Note that this is a compile-time switch. And since it disables
198
 *  public/stable LZ4 v1 API functions, we don't recommend using this
199
 *  symbol to generate a library for distribution.
200
 *
201
 *  The following public functions are removed when this symbol is defined.
202
 *  - lz4   : LZ4_createStream, LZ4_freeStream,
203
 *            LZ4_createStreamDecode, LZ4_freeStreamDecode, LZ4_create (deprecated)
204
 *  - lz4hc : LZ4_createStreamHC, LZ4_freeStreamHC,
205
 *            LZ4_createHC (deprecated), LZ4_freeHC  (deprecated)
206
 *  - lz4frame, lz4file : All LZ4F_* functions
207
 */
208
#if defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION)
209
#  define ALLOC(s)          lz4_error_memory_allocation_is_disabled
210
#  define ALLOC_AND_ZERO(s) lz4_error_memory_allocation_is_disabled
211
#  define FREEMEM(p)        lz4_error_memory_allocation_is_disabled
212
#elif defined(LZ4_USER_MEMORY_FUNCTIONS)
213
/* memory management functions can be customized by user project.
214
 * Below functions must exist somewhere in the Project
215
 * and be available at link time */
216
void* LZ4_malloc(size_t s);
217
void* LZ4_calloc(size_t n, size_t s);
218
void  LZ4_free(void* p);
219
# define ALLOC(s)          LZ4_malloc(s)
220
# define ALLOC_AND_ZERO(s) LZ4_calloc(1,s)
221
# define FREEMEM(p)        LZ4_free(p)
222
#else
223
# include <stdlib.h>   /* malloc, calloc, free */
224
0
# define ALLOC(s)          malloc(s)
225
0
# define ALLOC_AND_ZERO(s) calloc(1,s)
226
0
# define FREEMEM(p)        free(p)
227
#endif
228
229
#if ! LZ4_FREESTANDING
230
#  include <string.h>   /* memset, memcpy */
231
#endif
232
#if !defined(LZ4_memset)
233
9.78k
#  define LZ4_memset(p,v,s) memset((p),(v),(s))
234
#endif
235
9.78k
#define MEM_INIT(p,v,s)   LZ4_memset((p),(v),(s))
236
237
238
/*-************************************
239
*  Common Constants
240
**************************************/
241
3.39M
#define MINMATCH 4
242
243
16.1k
#define WILDCOPYLENGTH 8
244
405k
#define LASTLITERALS   5   /* see ../doc/lz4_Block_format.md#parsing-restrictions */
245
19.9k
#define MFLIMIT       12   /* see ../doc/lz4_Block_format.md#parsing-restrictions */
246
15.1k
#define MATCH_SAFEGUARD_DISTANCE  ((2*WILDCOPYLENGTH) - MINMATCH)   /* ensure it's possible to write 2 x wildcopyLength without overflowing output buffer */
247
694k
#define FASTLOOP_SAFE_DISTANCE 64
248
static const int LZ4_minLength = (MFLIMIT+1);
249
250
12.7k
#define KB *(1 <<10)
251
#define MB *(1 <<20)
252
0
#define GB *(1U<<30)
253
254
596k
#define LZ4_DISTANCE_ABSOLUTE_MAX 65535
255
#if (LZ4_DISTANCE_MAX > LZ4_DISTANCE_ABSOLUTE_MAX)   /* max supported by LZ4 format */
256
#  error "LZ4_DISTANCE_MAX is too big : must be <= 65535"
257
#endif
258
259
5.42M
#define ML_BITS  4
260
2.85M
#define ML_MASK  ((1U<<ML_BITS)-1)
261
1.39M
#define RUN_BITS (8-ML_BITS)
262
1.39M
#define RUN_MASK ((1U<<RUN_BITS)-1)
263
264
265
/*-************************************
266
*  Error detection
267
**************************************/
268
#if defined(LZ4_DEBUG) && (LZ4_DEBUG>=1)
269
#  include <assert.h>
270
#else
271
#  ifndef assert
272
#    define assert(condition) ((void)0)
273
#  endif
274
#endif
275
276
6.45M
#define LZ4_STATIC_ASSERT(c)   { enum { LZ4_static_assert = 1/(int)(!!(c)) }; }   /* use after variable declarations */
277
278
#if defined(LZ4_DEBUG) && (LZ4_DEBUG>=2)
279
#  include <stdio.h>
280
   static int g_debuglog_enable = 1;
281
#  define DEBUGLOG(l, ...) {                          \
282
        if ((g_debuglog_enable) && (l<=LZ4_DEBUG)) {  \
283
            fprintf(stderr, __FILE__  " %i: ", __LINE__); \
284
            fprintf(stderr, __VA_ARGS__);             \
285
            fprintf(stderr, " \n");                   \
286
    }   }
287
#else
288
9.09M
#  define DEBUGLOG(l, ...) {}    /* disabled */
289
#endif
290
291
static int LZ4_isAligned(const void* ptr, size_t alignment)
292
1.81k
{
293
1.81k
    return ((size_t)ptr & (alignment -1)) == 0;
294
1.81k
}
lz4.c:LZ4_isAligned
Line
Count
Source
292
1.81k
{
293
1.81k
    return ((size_t)ptr & (alignment -1)) == 0;
294
1.81k
}
Unexecuted instantiation: lz4hc.c:LZ4_isAligned
295
296
297
/*-************************************
298
*  Types
299
**************************************/
300
#include <limits.h>
301
#if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
302
# include <stdint.h>
303
  typedef  uint8_t BYTE;
304
  typedef uint16_t U16;
305
  typedef uint32_t U32;
306
  typedef  int32_t S32;
307
  typedef uint64_t U64;
308
  typedef uintptr_t uptrval;
309
#else
310
# if UINT_MAX != 4294967295UL
311
#   error "LZ4 code (when not C++ or C99) assumes that sizeof(int) == 4"
312
# endif
313
  typedef unsigned char       BYTE;
314
  typedef unsigned short      U16;
315
  typedef unsigned int        U32;
316
  typedef   signed int        S32;
317
  typedef unsigned long long  U64;
318
  typedef size_t              uptrval;   /* generally true, except OpenVMS-64 */
319
#endif
320
321
#if defined(__x86_64__)
322
  typedef U64    reg_t;   /* 64-bits in x32 mode */
323
#else
324
  typedef size_t reg_t;   /* 32-bits in x32 mode */
325
#endif
326
327
typedef enum {
328
    notLimited = 0,
329
    limitedOutput = 1,
330
    fillOutput = 2
331
} limitedOutput_directive;
332
333
334
/*-************************************
335
*  Reading and writing into memory
336
**************************************/
337
338
/**
339
 * LZ4 relies on memcpy with a constant size being inlined. In freestanding
340
 * environments, the compiler can't assume the implementation of memcpy() is
341
 * standard compliant, so it can't apply its specialized memcpy() inlining
342
 * logic. When possible, use __builtin_memcpy() to tell the compiler to analyze
343
 * memcpy() as if it were standard compliant, so it can inline it in freestanding
344
 * environments. This is needed when decompressing the Linux Kernel, for example.
345
 */
346
#if !defined(LZ4_memcpy)
347
#  if defined(__GNUC__) && (__GNUC__ >= 4)
348
12.4M
#    define LZ4_memcpy(dst, src, size) __builtin_memcpy(dst, src, size)
349
#  else
350
#    define LZ4_memcpy(dst, src, size) memcpy(dst, src, size)
351
#  endif
352
#endif
353
354
#if !defined(LZ4_memmove)
355
#  if defined(__GNUC__) && (__GNUC__ >= 4)
356
10.8k
#    define LZ4_memmove __builtin_memmove
357
#  else
358
#    define LZ4_memmove memmove
359
#  endif
360
#endif
361
362
static unsigned LZ4_isLittleEndian(void)
363
8.87M
{
364
8.87M
    const union { U32 u; BYTE c[4]; } one = { 1 };   /* don't use static : performance detrimental */
365
8.87M
    return one.c[0];
366
8.87M
}
lz4.c:LZ4_isLittleEndian
Line
Count
Source
363
8.87M
{
364
8.87M
    const union { U32 u; BYTE c[4]; } one = { 1 };   /* don't use static : performance detrimental */
365
8.87M
    return one.c[0];
366
8.87M
}
Unexecuted instantiation: lz4hc.c:LZ4_isLittleEndian
367
368
369
#if defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==2)
370
/* lie to the compiler about data alignment; use with caution */
371
372
static U16 LZ4_read16(const void* memPtr) { return *(const U16*) memPtr; }
373
static U32 LZ4_read32(const void* memPtr) { return *(const U32*) memPtr; }
374
static reg_t LZ4_read_ARCH(const void* memPtr) { return *(const reg_t*) memPtr; }
375
376
static void LZ4_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }
377
static void LZ4_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; }
378
379
#elif defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==1)
380
381
/* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
382
/* currently only defined for gcc and icc */
383
typedef struct { U16 u16; } __attribute__((packed)) LZ4_unalign16;
384
typedef struct { U32 u32; } __attribute__((packed)) LZ4_unalign32;
385
typedef struct { reg_t uArch; } __attribute__((packed)) LZ4_unalignST;
386
387
702k
static U16 LZ4_read16(const void* ptr) { return ((const LZ4_unalign16*)ptr)->u16; }
lz4.c:LZ4_read16
Line
Count
Source
387
702k
static U16 LZ4_read16(const void* ptr) { return ((const LZ4_unalign16*)ptr)->u16; }
Unexecuted instantiation: lz4hc.c:LZ4_read16
388
12.2M
static U32 LZ4_read32(const void* ptr) { return ((const LZ4_unalign32*)ptr)->u32; }
lz4.c:LZ4_read32
Line
Count
Source
388
12.2M
static U32 LZ4_read32(const void* ptr) { return ((const LZ4_unalign32*)ptr)->u32; }
Unexecuted instantiation: lz4hc.c:LZ4_read32
389
23.7M
static reg_t LZ4_read_ARCH(const void* ptr) { return ((const LZ4_unalignST*)ptr)->uArch; }
lz4.c:LZ4_read_ARCH
Line
Count
Source
389
23.7M
static reg_t LZ4_read_ARCH(const void* ptr) { return ((const LZ4_unalignST*)ptr)->uArch; }
Unexecuted instantiation: lz4hc.c:LZ4_read_ARCH
390
391
669k
static void LZ4_write16(void* memPtr, U16 value) { ((LZ4_unalign16*)memPtr)->u16 = value; }
lz4.c:LZ4_write16
Line
Count
Source
391
669k
static void LZ4_write16(void* memPtr, U16 value) { ((LZ4_unalign16*)memPtr)->u16 = value; }
Unexecuted instantiation: lz4hc.c:LZ4_write16
392
447k
static void LZ4_write32(void* memPtr, U32 value) { ((LZ4_unalign32*)memPtr)->u32 = value; }
lz4.c:LZ4_write32
Line
Count
Source
392
447k
static void LZ4_write32(void* memPtr, U32 value) { ((LZ4_unalign32*)memPtr)->u32 = value; }
Unexecuted instantiation: lz4hc.c:LZ4_write32
393
394
#else  /* safe and portable access using memcpy() */
395
396
static U16 LZ4_read16(const void* memPtr)
397
{
398
    U16 val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val;
399
}
400
401
static U32 LZ4_read32(const void* memPtr)
402
{
403
    U32 val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val;
404
}
405
406
static reg_t LZ4_read_ARCH(const void* memPtr)
407
{
408
    reg_t val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val;
409
}
410
411
static void LZ4_write16(void* memPtr, U16 value)
412
{
413
    LZ4_memcpy(memPtr, &value, sizeof(value));
414
}
415
416
static void LZ4_write32(void* memPtr, U32 value)
417
{
418
    LZ4_memcpy(memPtr, &value, sizeof(value));
419
}
420
421
#endif /* LZ4_FORCE_MEMORY_ACCESS */
422
423
424
static U16 LZ4_readLE16(const void* memPtr)
425
700k
{
426
700k
    if (LZ4_isLittleEndian()) {
427
700k
        return LZ4_read16(memPtr);
428
700k
    } else {
429
0
        const BYTE* p = (const BYTE*)memPtr;
430
0
        return (U16)((U16)p[0] + (p[1]<<8));
431
0
    }
432
700k
}
lz4.c:LZ4_readLE16
Line
Count
Source
425
700k
{
426
700k
    if (LZ4_isLittleEndian()) {
427
700k
        return LZ4_read16(memPtr);
428
700k
    } else {
429
0
        const BYTE* p = (const BYTE*)memPtr;
430
0
        return (U16)((U16)p[0] + (p[1]<<8));
431
0
    }
432
700k
}
Unexecuted instantiation: lz4hc.c:LZ4_readLE16
433
434
static void LZ4_writeLE16(void* memPtr, U16 value)
435
669k
{
436
669k
    if (LZ4_isLittleEndian()) {
437
669k
        LZ4_write16(memPtr, value);
438
669k
    } else {
439
0
        BYTE* p = (BYTE*)memPtr;
440
0
        p[0] = (BYTE) value;
441
0
        p[1] = (BYTE)(value>>8);
442
0
    }
443
669k
}
lz4.c:LZ4_writeLE16
Line
Count
Source
435
669k
{
436
669k
    if (LZ4_isLittleEndian()) {
437
669k
        LZ4_write16(memPtr, value);
438
669k
    } else {
439
0
        BYTE* p = (BYTE*)memPtr;
440
0
        p[0] = (BYTE) value;
441
0
        p[1] = (BYTE)(value>>8);
442
0
    }
443
669k
}
Unexecuted instantiation: lz4hc.c:LZ4_writeLE16
444
445
/* customized variant of memcpy, which can overwrite up to 8 bytes beyond dstEnd */
446
LZ4_FORCE_INLINE
447
void LZ4_wildCopy8(void* dstPtr, const void* srcPtr, void* dstEnd)
448
482k
{
449
482k
    BYTE* d = (BYTE*)dstPtr;
450
482k
    const BYTE* s = (const BYTE*)srcPtr;
451
482k
    BYTE* const e = (BYTE*)dstEnd;
452
453
4.85M
    do { LZ4_memcpy(d,s,8); d+=8; s+=8; } while (d<e);
454
482k
}
lz4.c:LZ4_wildCopy8
Line
Count
Source
448
482k
{
449
482k
    BYTE* d = (BYTE*)dstPtr;
450
482k
    const BYTE* s = (const BYTE*)srcPtr;
451
482k
    BYTE* const e = (BYTE*)dstEnd;
452
453
4.85M
    do { LZ4_memcpy(d,s,8); d+=8; s+=8; } while (d<e);
454
482k
}
Unexecuted instantiation: lz4hc.c:LZ4_wildCopy8
455
456
static const unsigned inc32table[8] = {0, 1, 2,  1,  0,  4, 4, 4};
457
static const int      dec64table[8] = {0, 0, 0, -1, -4,  1, 2, 3};
458
459
460
#ifndef LZ4_FAST_DEC_LOOP
461
#  if defined __i386__ || defined _M_IX86 || defined __x86_64__ || defined _M_X64
462
#    define LZ4_FAST_DEC_LOOP 1
463
#  elif defined(__aarch64__) && defined(__APPLE__)
464
#    define LZ4_FAST_DEC_LOOP 1
465
#  elif defined(__aarch64__) && !defined(__clang__)
466
     /* On non-Apple aarch64, we disable this optimization for clang because
467
      * on certain mobile chipsets, performance is reduced with clang. For
468
      * more information refer to https://github.com/lz4/lz4/pull/707 */
469
#    define LZ4_FAST_DEC_LOOP 1
470
#  else
471
#    define LZ4_FAST_DEC_LOOP 0
472
#  endif
473
#endif
474
475
#if LZ4_FAST_DEC_LOOP
476
477
LZ4_FORCE_INLINE void
478
LZ4_memcpy_using_offset_base(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const size_t offset)
479
6.09k
{
480
6.09k
    assert(srcPtr + offset == dstPtr);
481
6.09k
    if (offset < 8) {
482
5.23k
        LZ4_write32(dstPtr, 0);   /* silence an msan warning when offset==0 */
483
5.23k
        dstPtr[0] = srcPtr[0];
484
5.23k
        dstPtr[1] = srcPtr[1];
485
5.23k
        dstPtr[2] = srcPtr[2];
486
5.23k
        dstPtr[3] = srcPtr[3];
487
5.23k
        srcPtr += inc32table[offset];
488
5.23k
        LZ4_memcpy(dstPtr+4, srcPtr, 4);
489
5.23k
        srcPtr -= dec64table[offset];
490
5.23k
        dstPtr += 8;
491
5.23k
    } else {
492
856
        LZ4_memcpy(dstPtr, srcPtr, 8);
493
856
        dstPtr += 8;
494
856
        srcPtr += 8;
495
856
    }
496
497
6.09k
    LZ4_wildCopy8(dstPtr, srcPtr, dstEnd);
498
6.09k
}
lz4.c:LZ4_memcpy_using_offset_base
Line
Count
Source
479
6.09k
{
480
6.09k
    assert(srcPtr + offset == dstPtr);
481
6.09k
    if (offset < 8) {
482
5.23k
        LZ4_write32(dstPtr, 0);   /* silence an msan warning when offset==0 */
483
5.23k
        dstPtr[0] = srcPtr[0];
484
5.23k
        dstPtr[1] = srcPtr[1];
485
5.23k
        dstPtr[2] = srcPtr[2];
486
5.23k
        dstPtr[3] = srcPtr[3];
487
5.23k
        srcPtr += inc32table[offset];
488
5.23k
        LZ4_memcpy(dstPtr+4, srcPtr, 4);
489
5.23k
        srcPtr -= dec64table[offset];
490
5.23k
        dstPtr += 8;
491
5.23k
    } else {
492
856
        LZ4_memcpy(dstPtr, srcPtr, 8);
493
856
        dstPtr += 8;
494
856
        srcPtr += 8;
495
856
    }
496
497
6.09k
    LZ4_wildCopy8(dstPtr, srcPtr, dstEnd);
498
6.09k
}
Unexecuted instantiation: lz4hc.c:LZ4_memcpy_using_offset_base
499
500
/* customized variant of memcpy, which can overwrite up to 32 bytes beyond dstEnd
501
 * this version copies two times 16 bytes (instead of one time 32 bytes)
502
 * because it must be compatible with offsets >= 16. */
503
LZ4_FORCE_INLINE void
504
LZ4_wildCopy32(void* dstPtr, const void* srcPtr, void* dstEnd)
505
443k
{
506
443k
    BYTE* d = (BYTE*)dstPtr;
507
443k
    const BYTE* s = (const BYTE*)srcPtr;
508
443k
    BYTE* const e = (BYTE*)dstEnd;
509
510
1.63M
    do { LZ4_memcpy(d,s,16); LZ4_memcpy(d+16,s+16,16); d+=32; s+=32; } while (d<e);
511
443k
}
lz4.c:LZ4_wildCopy32
Line
Count
Source
505
443k
{
506
443k
    BYTE* d = (BYTE*)dstPtr;
507
443k
    const BYTE* s = (const BYTE*)srcPtr;
508
443k
    BYTE* const e = (BYTE*)dstEnd;
509
510
1.63M
    do { LZ4_memcpy(d,s,16); LZ4_memcpy(d+16,s+16,16); d+=32; s+=32; } while (d<e);
511
443k
}
Unexecuted instantiation: lz4hc.c:LZ4_wildCopy32
512
513
/* LZ4_memcpy_using_offset()  presumes :
514
 * - dstEnd >= dstPtr + MINMATCH
515
 * - there is at least 8 bytes available to write after dstEnd */
516
LZ4_FORCE_INLINE void
517
LZ4_memcpy_using_offset(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const size_t offset)
518
45.0k
{
519
45.0k
    BYTE v[8];
520
521
45.0k
    assert(dstEnd >= dstPtr + MINMATCH);
522
523
45.0k
    switch(offset) {
524
7.96k
    case 1:
525
7.96k
        MEM_INIT(v, *srcPtr, 8);
526
7.96k
        break;
527
22.5k
    case 2:
528
22.5k
        LZ4_memcpy(v, srcPtr, 2);
529
22.5k
        LZ4_memcpy(&v[2], srcPtr, 2);
530
#if defined(_MSC_VER) && (_MSC_VER <= 1933) /* MSVC 2022 ver 17.3 or earlier */
531
#  pragma warning(push)
532
#  pragma warning(disable : 6385) /* warning C6385: Reading invalid data from 'v'. */
533
#endif
534
22.5k
        LZ4_memcpy(&v[4], v, 4);
535
#if defined(_MSC_VER) && (_MSC_VER <= 1933) /* MSVC 2022 ver 17.3 or earlier */
536
#  pragma warning(pop)
537
#endif
538
22.5k
        break;
539
8.46k
    case 4:
540
8.46k
        LZ4_memcpy(v, srcPtr, 4);
541
8.46k
        LZ4_memcpy(&v[4], srcPtr, 4);
542
8.46k
        break;
543
6.09k
    default:
544
6.09k
        LZ4_memcpy_using_offset_base(dstPtr, srcPtr, dstEnd, offset);
545
6.09k
        return;
546
45.0k
    }
547
548
38.9k
    LZ4_memcpy(dstPtr, v, 8);
549
38.9k
    dstPtr += 8;
550
2.75M
    while (dstPtr < dstEnd) {
551
2.71M
        LZ4_memcpy(dstPtr, v, 8);
552
2.71M
        dstPtr += 8;
553
2.71M
    }
554
38.9k
}
lz4.c:LZ4_memcpy_using_offset
Line
Count
Source
518
45.0k
{
519
45.0k
    BYTE v[8];
520
521
45.0k
    assert(dstEnd >= dstPtr + MINMATCH);
522
523
45.0k
    switch(offset) {
524
7.96k
    case 1:
525
7.96k
        MEM_INIT(v, *srcPtr, 8);
526
7.96k
        break;
527
22.5k
    case 2:
528
22.5k
        LZ4_memcpy(v, srcPtr, 2);
529
22.5k
        LZ4_memcpy(&v[2], srcPtr, 2);
530
#if defined(_MSC_VER) && (_MSC_VER <= 1933) /* MSVC 2022 ver 17.3 or earlier */
531
#  pragma warning(push)
532
#  pragma warning(disable : 6385) /* warning C6385: Reading invalid data from 'v'. */
533
#endif
534
22.5k
        LZ4_memcpy(&v[4], v, 4);
535
#if defined(_MSC_VER) && (_MSC_VER <= 1933) /* MSVC 2022 ver 17.3 or earlier */
536
#  pragma warning(pop)
537
#endif
538
22.5k
        break;
539
8.46k
    case 4:
540
8.46k
        LZ4_memcpy(v, srcPtr, 4);
541
8.46k
        LZ4_memcpy(&v[4], srcPtr, 4);
542
8.46k
        break;
543
6.09k
    default:
544
6.09k
        LZ4_memcpy_using_offset_base(dstPtr, srcPtr, dstEnd, offset);
545
6.09k
        return;
546
45.0k
    }
547
548
38.9k
    LZ4_memcpy(dstPtr, v, 8);
549
38.9k
    dstPtr += 8;
550
2.75M
    while (dstPtr < dstEnd) {
551
2.71M
        LZ4_memcpy(dstPtr, v, 8);
552
2.71M
        dstPtr += 8;
553
2.71M
    }
554
38.9k
}
Unexecuted instantiation: lz4hc.c:LZ4_memcpy_using_offset
555
#endif
556
557
558
/*-************************************
559
*  Common functions
560
**************************************/
561
static unsigned LZ4_NbCommonBytes (reg_t val)
562
668k
{
563
668k
    assert(val != 0);
564
668k
    if (LZ4_isLittleEndian()) {
565
668k
        if (sizeof(val) == 8) {
566
#       if defined(_MSC_VER) && (_MSC_VER >= 1800) && (defined(_M_AMD64) && !defined(_M_ARM64EC)) && !defined(LZ4_FORCE_SW_BITCOUNT)
567
/*-*************************************************************************************************
568
* ARM64EC is a Microsoft-designed ARM64 ABI compatible with AMD64 applications on ARM64 Windows 11.
569
* The ARM64EC ABI does not support AVX/AVX2/AVX512 instructions, nor their relevant intrinsics
570
* including _tzcnt_u64. Therefore, we need to neuter the _tzcnt_u64 code path for ARM64EC.
571
****************************************************************************************************/
572
#         if defined(__clang__) && (__clang_major__ < 10)
573
            /* Avoid undefined clang-cl intrinsics issue.
574
             * See https://github.com/lz4/lz4/pull/1017 for details. */
575
            return (unsigned)__builtin_ia32_tzcnt_u64(val) >> 3;
576
#         else
577
            /* x64 CPUS without BMI support interpret `TZCNT` as `REP BSF` */
578
            return (unsigned)_tzcnt_u64(val) >> 3;
579
#         endif
580
#       elif defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
581
            unsigned long r = 0;
582
            _BitScanForward64(&r, (U64)val);
583
            return (unsigned)r >> 3;
584
#       elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
585
                            ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
586
                                        !defined(LZ4_FORCE_SW_BITCOUNT)
587
668k
            return (unsigned)__builtin_ctzll((U64)val) >> 3;
588
#       else
589
            const U64 m = 0x0101010101010101ULL;
590
            val ^= val - 1;
591
            return (unsigned)(((U64)((val & (m - 1)) * m)) >> 56);
592
#       endif
593
668k
        } else /* 32 bits */ {
594
#       if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(LZ4_FORCE_SW_BITCOUNT)
595
            unsigned long r;
596
            _BitScanForward(&r, (U32)val);
597
            return (unsigned)r >> 3;
598
#       elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
599
                            ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
600
                        !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT)
601
0
            return (unsigned)__builtin_ctz((U32)val) >> 3;
602
#       else
603
            const U32 m = 0x01010101;
604
            return (unsigned)((((val - 1) ^ val) & (m - 1)) * m) >> 24;
605
#       endif
606
0
        }
607
668k
    } else   /* Big Endian CPU */ {
608
0
        if (sizeof(val)==8) {
609
0
#       if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
610
0
                            ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
611
0
                        !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT)
612
0
            return (unsigned)__builtin_clzll((U64)val) >> 3;
613
#       else
614
#if 1
615
            /* this method is probably faster,
616
             * but adds a 128 bytes lookup table */
617
            static const unsigned char ctz7_tab[128] = {
618
                7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
619
                4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
620
                5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
621
                4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
622
                6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
623
                4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
624
                5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
625
                4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
626
            };
627
            U64 const mask = 0x0101010101010101ULL;
628
            U64 const t = (((val >> 8) - mask) | val) & mask;
629
            return ctz7_tab[(t * 0x0080402010080402ULL) >> 57];
630
#else
631
            /* this method doesn't consume memory space like the previous one,
632
             * but it contains several branches,
633
             * that may end up slowing execution */
634
            static const U32 by32 = sizeof(val)*4;  /* 32 on 64 bits (goal), 16 on 32 bits.
635
            Just to avoid some static analyzer complaining about shift by 32 on 32-bits target.
636
            Note that this code path is never triggered in 32-bits mode. */
637
            unsigned r;
638
            if (!(val>>by32)) { r=4; } else { r=0; val>>=by32; }
639
            if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
640
            r += (!val);
641
            return r;
642
#endif
643
#       endif
644
0
        } else /* 32 bits */ {
645
0
#       if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
646
0
                            ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
647
0
                                        !defined(LZ4_FORCE_SW_BITCOUNT)
648
0
            return (unsigned)__builtin_clz((U32)val) >> 3;
649
#       else
650
            val >>= 8;
651
            val = ((((val + 0x00FFFF00) | 0x00FFFFFF) + val) |
652
              (val + 0x00FF0000)) >> 24;
653
            return (unsigned)val ^ 3;
654
#       endif
655
0
        }
656
0
    }
657
668k
}
lz4.c:LZ4_NbCommonBytes
Line
Count
Source
562
668k
{
563
668k
    assert(val != 0);
564
668k
    if (LZ4_isLittleEndian()) {
565
668k
        if (sizeof(val) == 8) {
566
#       if defined(_MSC_VER) && (_MSC_VER >= 1800) && (defined(_M_AMD64) && !defined(_M_ARM64EC)) && !defined(LZ4_FORCE_SW_BITCOUNT)
567
/*-*************************************************************************************************
568
* ARM64EC is a Microsoft-designed ARM64 ABI compatible with AMD64 applications on ARM64 Windows 11.
569
* The ARM64EC ABI does not support AVX/AVX2/AVX512 instructions, nor their relevant intrinsics
570
* including _tzcnt_u64. Therefore, we need to neuter the _tzcnt_u64 code path for ARM64EC.
571
****************************************************************************************************/
572
#         if defined(__clang__) && (__clang_major__ < 10)
573
            /* Avoid undefined clang-cl intrinsics issue.
574
             * See https://github.com/lz4/lz4/pull/1017 for details. */
575
            return (unsigned)__builtin_ia32_tzcnt_u64(val) >> 3;
576
#         else
577
            /* x64 CPUS without BMI support interpret `TZCNT` as `REP BSF` */
578
            return (unsigned)_tzcnt_u64(val) >> 3;
579
#         endif
580
#       elif defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
581
            unsigned long r = 0;
582
            _BitScanForward64(&r, (U64)val);
583
            return (unsigned)r >> 3;
584
#       elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
585
                            ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
586
                                        !defined(LZ4_FORCE_SW_BITCOUNT)
587
668k
            return (unsigned)__builtin_ctzll((U64)val) >> 3;
588
#       else
589
            const U64 m = 0x0101010101010101ULL;
590
            val ^= val - 1;
591
            return (unsigned)(((U64)((val & (m - 1)) * m)) >> 56);
592
#       endif
593
668k
        } else /* 32 bits */ {
594
#       if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(LZ4_FORCE_SW_BITCOUNT)
595
            unsigned long r;
596
            _BitScanForward(&r, (U32)val);
597
            return (unsigned)r >> 3;
598
#       elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
599
                            ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
600
                        !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT)
601
0
            return (unsigned)__builtin_ctz((U32)val) >> 3;
602
#       else
603
            const U32 m = 0x01010101;
604
            return (unsigned)((((val - 1) ^ val) & (m - 1)) * m) >> 24;
605
#       endif
606
0
        }
607
668k
    } else   /* Big Endian CPU */ {
608
0
        if (sizeof(val)==8) {
609
0
#       if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
610
0
                            ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
611
0
                        !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT)
612
0
            return (unsigned)__builtin_clzll((U64)val) >> 3;
613
#       else
614
#if 1
615
            /* this method is probably faster,
616
             * but adds a 128 bytes lookup table */
617
            static const unsigned char ctz7_tab[128] = {
618
                7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
619
                4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
620
                5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
621
                4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
622
                6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
623
                4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
624
                5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
625
                4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
626
            };
627
            U64 const mask = 0x0101010101010101ULL;
628
            U64 const t = (((val >> 8) - mask) | val) & mask;
629
            return ctz7_tab[(t * 0x0080402010080402ULL) >> 57];
630
#else
631
            /* this method doesn't consume memory space like the previous one,
632
             * but it contains several branches,
633
             * that may end up slowing execution */
634
            static const U32 by32 = sizeof(val)*4;  /* 32 on 64 bits (goal), 16 on 32 bits.
635
            Just to avoid some static analyzer complaining about shift by 32 on 32-bits target.
636
            Note that this code path is never triggered in 32-bits mode. */
637
            unsigned r;
638
            if (!(val>>by32)) { r=4; } else { r=0; val>>=by32; }
639
            if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
640
            r += (!val);
641
            return r;
642
#endif
643
#       endif
644
0
        } else /* 32 bits */ {
645
0
#       if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \
646
0
                            ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \
647
0
                                        !defined(LZ4_FORCE_SW_BITCOUNT)
648
0
            return (unsigned)__builtin_clz((U32)val) >> 3;
649
#       else
650
            val >>= 8;
651
            val = ((((val + 0x00FFFF00) | 0x00FFFFFF) + val) |
652
              (val + 0x00FF0000)) >> 24;
653
            return (unsigned)val ^ 3;
654
#       endif
655
0
        }
656
0
    }
657
668k
}
Unexecuted instantiation: lz4hc.c:LZ4_NbCommonBytes
658
659
660
15.5M
#define STEPSIZE sizeof(reg_t)
661
LZ4_FORCE_INLINE
662
unsigned LZ4_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* pInLimit)
663
669k
{
664
669k
    const BYTE* const pStart = pIn;
665
666
669k
    if (likely(pIn < pInLimit-(STEPSIZE-1))) {
667
668k
        reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
668
668k
        if (!diff) {
669
418k
            pIn+=STEPSIZE; pMatch+=STEPSIZE;
670
418k
        } else {
671
249k
            return LZ4_NbCommonBytes(diff);
672
249k
    }   }
673
674
7.79M
    while (likely(pIn < pInLimit-(STEPSIZE-1))) {
675
7.79M
        reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
676
7.79M
        if (!diff) { pIn+=STEPSIZE; pMatch+=STEPSIZE; continue; }
677
418k
        pIn += LZ4_NbCommonBytes(diff);
678
418k
        return (unsigned)(pIn - pStart);
679
7.79M
    }
680
681
966
    if ((STEPSIZE==8) && (pIn<(pInLimit-3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) { pIn+=4; pMatch+=4; }
682
966
    if ((pIn<(pInLimit-1)) && (LZ4_read16(pMatch) == LZ4_read16(pIn))) { pIn+=2; pMatch+=2; }
683
966
    if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++;
684
966
    return (unsigned)(pIn - pStart);
685
419k
}
lz4.c:LZ4_count
Line
Count
Source
663
669k
{
664
669k
    const BYTE* const pStart = pIn;
665
666
669k
    if (likely(pIn < pInLimit-(STEPSIZE-1))) {
667
668k
        reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
668
668k
        if (!diff) {
669
418k
            pIn+=STEPSIZE; pMatch+=STEPSIZE;
670
418k
        } else {
671
249k
            return LZ4_NbCommonBytes(diff);
672
249k
    }   }
673
674
7.79M
    while (likely(pIn < pInLimit-(STEPSIZE-1))) {
675
7.79M
        reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
676
7.79M
        if (!diff) { pIn+=STEPSIZE; pMatch+=STEPSIZE; continue; }
677
418k
        pIn += LZ4_NbCommonBytes(diff);
678
418k
        return (unsigned)(pIn - pStart);
679
7.79M
    }
680
681
966
    if ((STEPSIZE==8) && (pIn<(pInLimit-3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) { pIn+=4; pMatch+=4; }
682
966
    if ((pIn<(pInLimit-1)) && (LZ4_read16(pMatch) == LZ4_read16(pIn))) { pIn+=2; pMatch+=2; }
683
966
    if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++;
684
966
    return (unsigned)(pIn - pStart);
685
419k
}
Unexecuted instantiation: lz4hc.c:LZ4_count
686
687
688
#ifndef LZ4_COMMONDEFS_ONLY
689
/*-************************************
690
*  Local Constants
691
**************************************/
692
static const int LZ4_64Klimit = ((64 KB) + (MFLIMIT-1));
693
static const U32 LZ4_skipTrigger = 6;  /* Increase this value ==> compression run slower on incompressible data */
694
695
696
/*-************************************
697
*  Local Structures and types
698
**************************************/
699
typedef enum { clearedTable = 0, byPtr, byU32, byU16 } tableType_t;
700
701
/**
702
 * This enum distinguishes several different modes of accessing previous
703
 * content in the stream.
704
 *
705
 * - noDict        : There is no preceding content.
706
 * - withPrefix64k : Table entries up to ctx->dictSize before the current blob
707
 *                   blob being compressed are valid and refer to the preceding
708
 *                   content (of length ctx->dictSize), which is available
709
 *                   contiguously preceding in memory the content currently
710
 *                   being compressed.
711
 * - usingExtDict  : Like withPrefix64k, but the preceding content is somewhere
712
 *                   else in memory, starting at ctx->dictionary with length
713
 *                   ctx->dictSize.
714
 * - usingDictCtx  : Everything concerning the preceding content is
715
 *                   in a separate context, pointed to by ctx->dictCtx.
716
 *                   ctx->dictionary, ctx->dictSize, and table entries
717
 *                   in the current context that refer to positions
718
 *                   preceding the beginning of the current compression are
719
 *                   ignored. Instead, ctx->dictCtx->dictionary and ctx->dictCtx
720
 *                   ->dictSize describe the location and size of the preceding
721
 *                   content, and matches are found by looking in the ctx
722
 *                   ->dictCtx->hashTable.
723
 */
724
typedef enum { noDict = 0, withPrefix64k, usingExtDict, usingDictCtx } dict_directive;
725
typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive;
726
727
728
/*-************************************
729
*  Local Utils
730
**************************************/
731
0
int LZ4_versionNumber (void) { return LZ4_VERSION_NUMBER; }
732
0
const char* LZ4_versionString(void) { return LZ4_VERSION_STRING; }
733
3.63k
int LZ4_compressBound(int isize)  { return LZ4_COMPRESSBOUND(isize); }
734
0
int LZ4_sizeofState(void) { return sizeof(LZ4_stream_t); }
735
736
737
/*-****************************************
738
*  Internal Definitions, used only in Tests
739
*******************************************/
740
#if defined (__cplusplus)
741
extern "C" {
742
#endif
743
744
int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* dest, int srcSize);
745
746
int LZ4_decompress_safe_forceExtDict(const char* source, char* dest,
747
                                     int compressedSize, int maxOutputSize,
748
                                     const void* dictStart, size_t dictSize);
749
int LZ4_decompress_safe_partial_forceExtDict(const char* source, char* dest,
750
                                     int compressedSize, int targetOutputSize, int dstCapacity,
751
                                     const void* dictStart, size_t dictSize);
752
#if defined (__cplusplus)
753
}
754
#endif
755
756
/*-******************************
757
*  Compression functions
758
********************************/
759
LZ4_FORCE_INLINE U32 LZ4_hash4(U32 sequence, tableType_t const tableType)
760
669k
{
761
669k
    if (tableType == byU16)
762
669k
        return ((sequence * 2654435761U) >> ((MINMATCH*8)-(LZ4_HASHLOG+1)));
763
0
    else
764
0
        return ((sequence * 2654435761U) >> ((MINMATCH*8)-LZ4_HASHLOG));
765
669k
}
766
767
LZ4_FORCE_INLINE U32 LZ4_hash5(U64 sequence, tableType_t const tableType)
768
6.83M
{
769
6.83M
    const U32 hashLog = (tableType == byU16) ? LZ4_HASHLOG+1 : LZ4_HASHLOG;
770
6.83M
    if (LZ4_isLittleEndian()) {
771
6.83M
        const U64 prime5bytes = 889523592379ULL;
772
6.83M
        return (U32)(((sequence << 24) * prime5bytes) >> (64 - hashLog));
773
6.83M
    } else {
774
0
        const U64 prime8bytes = 11400714785074694791ULL;
775
0
        return (U32)(((sequence >> 24) * prime8bytes) >> (64 - hashLog));
776
0
    }
777
6.83M
}
778
779
LZ4_FORCE_INLINE U32 LZ4_hashPosition(const void* const p, tableType_t const tableType)
780
7.50M
{
781
7.50M
    if ((sizeof(reg_t)==8) && (tableType != byU16)) return LZ4_hash5(LZ4_read_ARCH(p), tableType);
782
669k
    return LZ4_hash4(LZ4_read32(p), tableType);
783
7.50M
}
784
785
LZ4_FORCE_INLINE void LZ4_clearHash(U32 h, void* tableBase, tableType_t const tableType)
786
0
{
787
0
    switch (tableType)
788
0
    {
789
0
    default: /* fallthrough */
790
0
    case clearedTable: { /* illegal! */ assert(0); return; }
791
0
    case byPtr: { const BYTE** hashTable = (const BYTE**)tableBase; hashTable[h] = NULL; return; }
792
0
    case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = 0; return; }
793
0
    case byU16: { U16* hashTable = (U16*) tableBase; hashTable[h] = 0; return; }
794
0
    }
795
0
}
796
797
LZ4_FORCE_INLINE void LZ4_putIndexOnHash(U32 idx, U32 h, void* tableBase, tableType_t const tableType)
798
7.04M
{
799
7.04M
    switch (tableType)
800
7.04M
    {
801
0
    default: /* fallthrough */
802
0
    case clearedTable: /* fallthrough */
803
0
    case byPtr: { /* illegal! */ assert(0); return; }
804
6.39M
    case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = idx; return; }
805
644k
    case byU16: { U16* hashTable = (U16*) tableBase; assert(idx < 65536); hashTable[h] = (U16)idx; return; }
806
7.04M
    }
807
7.04M
}
808
809
/* LZ4_putPosition*() : only used in byPtr mode */
810
LZ4_FORCE_INLINE void LZ4_putPositionOnHash(const BYTE* p, U32 h,
811
                                  void* tableBase, tableType_t const tableType)
812
0
{
813
0
    const BYTE** const hashTable = (const BYTE**)tableBase;
814
0
    assert(tableType == byPtr); (void)tableType;
815
0
    hashTable[h] = p;
816
0
}
817
818
LZ4_FORCE_INLINE void LZ4_putPosition(const BYTE* p, void* tableBase, tableType_t tableType)
819
0
{
820
0
    U32 const h = LZ4_hashPosition(p, tableType);
821
0
    LZ4_putPositionOnHash(p, h, tableBase, tableType);
822
0
}
823
824
/* LZ4_getIndexOnHash() :
825
 * Index of match position registered in hash table.
826
 * hash position must be calculated by using base+index, or dictBase+index.
827
 * Assumption 1 : only valid if tableType == byU32 or byU16.
828
 * Assumption 2 : h is presumed valid (within limits of hash table)
829
 */
830
LZ4_FORCE_INLINE U32 LZ4_getIndexOnHash(U32 h, const void* tableBase, tableType_t tableType)
831
6.37M
{
832
6.37M
    LZ4_STATIC_ASSERT(LZ4_MEMORY_USAGE > 2);
833
6.37M
    if (tableType == byU32) {
834
5.77M
        const U32* const hashTable = (const U32*) tableBase;
835
5.77M
        assert(h < (1U << (LZ4_MEMORY_USAGE-2)));
836
5.77M
        return hashTable[h];
837
5.77M
    }
838
596k
    if (tableType == byU16) {
839
596k
        const U16* const hashTable = (const U16*) tableBase;
840
596k
        assert(h < (1U << (LZ4_MEMORY_USAGE-1)));
841
596k
        return hashTable[h];
842
596k
    }
843
0
    assert(0); return 0;  /* forbidden case */
844
0
}
845
846
static const BYTE* LZ4_getPositionOnHash(U32 h, const void* tableBase, tableType_t tableType)
847
0
{
848
0
    assert(tableType == byPtr); (void)tableType;
849
0
    { const BYTE* const* hashTable = (const BYTE* const*) tableBase; return hashTable[h]; }
850
0
}
851
852
LZ4_FORCE_INLINE const BYTE*
853
LZ4_getPosition(const BYTE* p,
854
                const void* tableBase, tableType_t tableType)
855
0
{
856
0
    U32 const h = LZ4_hashPosition(p, tableType);
857
0
    return LZ4_getPositionOnHash(h, tableBase, tableType);
858
0
}
859
860
LZ4_FORCE_INLINE void
861
LZ4_prepareTable(LZ4_stream_t_internal* const cctx,
862
           const int inputSize,
863
0
           const tableType_t tableType) {
864
    /* If the table hasn't been used, it's guaranteed to be zeroed out, and is
865
     * therefore safe to use no matter what mode we're in. Otherwise, we figure
866
     * out if it's safe to leave as is or whether it needs to be reset.
867
     */
868
0
    if ((tableType_t)cctx->tableType != clearedTable) {
869
0
        assert(inputSize >= 0);
870
0
        if ((tableType_t)cctx->tableType != tableType
871
0
          || ((tableType == byU16) && cctx->currentOffset + (unsigned)inputSize >= 0xFFFFU)
872
0
          || ((tableType == byU32) && cctx->currentOffset > 1 GB)
873
0
          || tableType == byPtr
874
0
          || inputSize >= 4 KB)
875
0
        {
876
0
            DEBUGLOG(4, "LZ4_prepareTable: Resetting table in %p", cctx);
877
0
            MEM_INIT(cctx->hashTable, 0, LZ4_HASHTABLESIZE);
878
0
            cctx->currentOffset = 0;
879
0
            cctx->tableType = (U32)clearedTable;
880
0
        } else {
881
0
            DEBUGLOG(4, "LZ4_prepareTable: Re-use hash table (no reset)");
882
0
        }
883
0
    }
884
885
    /* Adding a gap, so all previous entries are > LZ4_DISTANCE_MAX back,
886
     * is faster than compressing without a gap.
887
     * However, compressing with currentOffset == 0 is faster still,
888
     * so we preserve that case.
889
     */
890
0
    if (cctx->currentOffset != 0 && tableType == byU32) {
891
0
        DEBUGLOG(5, "LZ4_prepareTable: adding 64KB to currentOffset");
892
0
        cctx->currentOffset += 64 KB;
893
0
    }
894
895
    /* Finally, clear history */
896
0
    cctx->dictCtx = NULL;
897
0
    cctx->dictionary = NULL;
898
0
    cctx->dictSize = 0;
899
0
}
900
901
/** LZ4_compress_generic() :
902
 *  inlined, to ensure branches are decided at compilation time.
903
 *  The following conditions are presumed already validated:
904
 *  - source != NULL
905
 *  - inputSize > 0
906
 */
907
LZ4_FORCE_INLINE int LZ4_compress_generic_validated(
908
                 LZ4_stream_t_internal* const cctx,
909
                 const char* const source,
910
                 char* const dest,
911
                 const int inputSize,
912
                 int*  inputConsumed, /* only written when outputDirective == fillOutput */
913
                 const int maxOutputSize,
914
                 const limitedOutput_directive outputDirective,
915
                 const tableType_t tableType,
916
                 const dict_directive dictDirective,
917
                 const dictIssue_directive dictIssue,
918
                 const int acceleration)
919
1.81k
{
920
1.81k
    int result;
921
1.81k
    const BYTE* ip = (const BYTE*)source;
922
923
1.81k
    U32 const startIndex = cctx->currentOffset;
924
1.81k
    const BYTE* base = (const BYTE*)source - startIndex;
925
1.81k
    const BYTE* lowLimit;
926
927
1.81k
    const LZ4_stream_t_internal* dictCtx = (const LZ4_stream_t_internal*) cctx->dictCtx;
928
1.81k
    const BYTE* const dictionary =
929
1.81k
        dictDirective == usingDictCtx ? dictCtx->dictionary : cctx->dictionary;
930
1.81k
    const U32 dictSize =
931
1.81k
        dictDirective == usingDictCtx ? dictCtx->dictSize : cctx->dictSize;
932
1.81k
    const U32 dictDelta =
933
1.81k
        (dictDirective == usingDictCtx) ? startIndex - dictCtx->currentOffset : 0;   /* make indexes in dictCtx comparable with indexes in current context */
934
935
1.81k
    int const maybe_extMem = (dictDirective == usingExtDict) || (dictDirective == usingDictCtx);
936
1.81k
    U32 const prefixIdxLimit = startIndex - dictSize;   /* used when dictDirective == dictSmall */
937
1.81k
    const BYTE* const dictEnd = dictionary ? dictionary + dictSize : dictionary;
938
1.81k
    const BYTE* anchor = (const BYTE*) source;
939
1.81k
    const BYTE* const iend = ip + inputSize;
940
1.81k
    const BYTE* const mflimitPlusOne = iend - MFLIMIT + 1;
941
1.81k
    const BYTE* const matchlimit = iend - LASTLITERALS;
942
943
    /* the dictCtx currentOffset is indexed on the start of the dictionary,
944
     * while a dictionary in the current context precedes the currentOffset */
945
1.81k
    const BYTE* dictBase = (dictionary == NULL) ? NULL :
946
1.81k
                           (dictDirective == usingDictCtx) ?
947
0
                            dictionary + dictSize - dictCtx->currentOffset :
948
0
                            dictionary + dictSize - startIndex;
949
950
1.81k
    BYTE* op = (BYTE*) dest;
951
1.81k
    BYTE* const olimit = op + maxOutputSize;
952
953
1.81k
    U32 offset = 0;
954
1.81k
    U32 forwardH;
955
956
1.81k
    DEBUGLOG(5, "LZ4_compress_generic_validated: srcSize=%i, tableType=%u", inputSize, tableType);
957
1.81k
    assert(ip != NULL);
958
1.81k
    if (tableType == byU16) assert(inputSize<LZ4_64Klimit);  /* Size too large (not within 64K limit) */
959
1.81k
    if (tableType == byPtr) assert(dictDirective==noDict);   /* only supported use case with byPtr */
960
    /* If init conditions are not met, we don't have to mark stream
961
     * as having dirty context, since no action was taken yet */
962
1.81k
    if (outputDirective == fillOutput && maxOutputSize < 1) { return 0; } /* Impossible to store anything */
963
1.81k
    assert(acceleration >= 1);
964
965
1.81k
    lowLimit = (const BYTE*)source - (dictDirective == withPrefix64k ? dictSize : 0);
966
967
    /* Update context state */
968
1.81k
    if (dictDirective == usingDictCtx) {
969
        /* Subsequent linked blocks can't use the dictionary. */
970
        /* Instead, they use the block we just compressed. */
971
0
        cctx->dictCtx = NULL;
972
0
        cctx->dictSize = (U32)inputSize;
973
1.81k
    } else {
974
1.81k
        cctx->dictSize += (U32)inputSize;
975
1.81k
    }
976
1.81k
    cctx->currentOffset += (U32)inputSize;
977
1.81k
    cctx->tableType = (U32)tableType;
978
979
1.81k
    if (inputSize<LZ4_minLength) goto _last_literals;        /* Input too small, no compression (all literals) */
980
981
    /* First Byte */
982
1.79k
    {   U32 const h = LZ4_hashPosition(ip, tableType);
983
1.79k
        if (tableType == byPtr) {
984
0
            LZ4_putPositionOnHash(ip, h, cctx->hashTable, byPtr);
985
1.79k
        } else {
986
1.79k
            LZ4_putIndexOnHash(startIndex, h, cctx->hashTable, tableType);
987
1.79k
    }   }
988
1.79k
    ip++; forwardH = LZ4_hashPosition(ip, tableType);
989
990
    /* Main Loop */
991
467k
    for ( ; ; ) {
992
467k
        const BYTE* match;
993
467k
        BYTE* token;
994
467k
        const BYTE* filledIp;
995
996
        /* Find a match */
997
467k
        if (tableType == byPtr) {
998
0
            const BYTE* forwardIp = ip;
999
0
            int step = 1;
1000
0
            int searchMatchNb = acceleration << LZ4_skipTrigger;
1001
0
            do {
1002
0
                U32 const h = forwardH;
1003
0
                ip = forwardIp;
1004
0
                forwardIp += step;
1005
0
                step = (searchMatchNb++ >> LZ4_skipTrigger);
1006
1007
0
                if (unlikely(forwardIp > mflimitPlusOne)) goto _last_literals;
1008
0
                assert(ip < mflimitPlusOne);
1009
1010
0
                match = LZ4_getPositionOnHash(h, cctx->hashTable, tableType);
1011
0
                forwardH = LZ4_hashPosition(forwardIp, tableType);
1012
0
                LZ4_putPositionOnHash(ip, h, cctx->hashTable, tableType);
1013
1014
0
            } while ( (match+LZ4_DISTANCE_MAX < ip)
1015
0
                   || (LZ4_read32(match) != LZ4_read32(ip)) );
1016
1017
467k
        } else {   /* byU32, byU16 */
1018
1019
467k
            const BYTE* forwardIp = ip;
1020
467k
            int step = 1;
1021
467k
            int searchMatchNb = acceleration << LZ4_skipTrigger;
1022
5.70M
            do {
1023
5.70M
                U32 const h = forwardH;
1024
5.70M
                U32 const current = (U32)(forwardIp - base);
1025
5.70M
                U32 matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType);
1026
5.70M
                assert(matchIndex <= current);
1027
5.70M
                assert(forwardIp - base < (ptrdiff_t)(2 GB - 1));
1028
5.70M
                ip = forwardIp;
1029
5.70M
                forwardIp += step;
1030
5.70M
                step = (searchMatchNb++ >> LZ4_skipTrigger);
1031
1032
5.70M
                if (unlikely(forwardIp > mflimitPlusOne)) goto _last_literals;
1033
5.70M
                assert(ip < mflimitPlusOne);
1034
1035
5.70M
                if (dictDirective == usingDictCtx) {
1036
0
                    if (matchIndex < startIndex) {
1037
                        /* there was no match, try the dictionary */
1038
0
                        assert(tableType == byU32);
1039
0
                        matchIndex = LZ4_getIndexOnHash(h, dictCtx->hashTable, byU32);
1040
0
                        match = dictBase + matchIndex;
1041
0
                        matchIndex += dictDelta;   /* make dictCtx index comparable with current context */
1042
0
                        lowLimit = dictionary;
1043
0
                    } else {
1044
0
                        match = base + matchIndex;
1045
0
                        lowLimit = (const BYTE*)source;
1046
0
                    }
1047
5.70M
                } else if (dictDirective == usingExtDict) {
1048
0
                    if (matchIndex < startIndex) {
1049
0
                        DEBUGLOG(7, "extDict candidate: matchIndex=%5u  <  startIndex=%5u", matchIndex, startIndex);
1050
0
                        assert(startIndex - matchIndex >= MINMATCH);
1051
0
                        assert(dictBase);
1052
0
                        match = dictBase + matchIndex;
1053
0
                        lowLimit = dictionary;
1054
0
                    } else {
1055
0
                        match = base + matchIndex;
1056
0
                        lowLimit = (const BYTE*)source;
1057
0
                    }
1058
5.70M
                } else {   /* single continuous memory segment */
1059
5.70M
                    match = base + matchIndex;
1060
5.70M
                }
1061
5.70M
                forwardH = LZ4_hashPosition(forwardIp, tableType);
1062
5.70M
                LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType);
1063
1064
5.70M
                DEBUGLOG(7, "candidate at pos=%u  (offset=%u \n", matchIndex, current - matchIndex);
1065
5.70M
                if ((dictIssue == dictSmall) && (matchIndex < prefixIdxLimit)) { continue; }    /* match outside of valid area */
1066
5.70M
                assert(matchIndex < current);
1067
5.70M
                if ( ((tableType != byU16) || (LZ4_DISTANCE_MAX < LZ4_DISTANCE_ABSOLUTE_MAX))
1068
5.70M
                  && (matchIndex+LZ4_DISTANCE_MAX < current)) {
1069
476k
                    continue;
1070
476k
                } /* too far */
1071
5.22M
                assert((current - matchIndex) <= LZ4_DISTANCE_MAX);  /* match now expected within distance */
1072
1073
5.22M
                if (LZ4_read32(match) == LZ4_read32(ip)) {
1074
466k
                    if (maybe_extMem) offset = current - matchIndex;
1075
466k
                    break;   /* match found */
1076
466k
                }
1077
1078
5.23M
            } while(1);
1079
467k
        }
1080
1081
        /* Catch up */
1082
466k
        filledIp = ip;
1083
574k
        while (((ip>anchor) & (match > lowLimit)) && (unlikely(ip[-1]==match[-1]))) { ip--; match--; }
1084
1085
        /* Encode Literals */
1086
466k
        {   unsigned const litLength = (unsigned)(ip - anchor);
1087
466k
            token = op++;
1088
466k
            if ((outputDirective == limitedOutput) &&  /* Check output buffer overflow */
1089
466k
                (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit)) ) {
1090
0
                return 0;   /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */
1091
0
            }
1092
466k
            if ((outputDirective == fillOutput) &&
1093
466k
                (unlikely(op + (litLength+240)/255 /* litlen */ + litLength /* literals */ + 2 /* offset */ + 1 /* token */ + MFLIMIT - MINMATCH /* min last literals so last match is <= end - MFLIMIT */ > olimit))) {
1094
0
                op--;
1095
0
                goto _last_literals;
1096
0
            }
1097
466k
            if (litLength >= RUN_MASK) {
1098
63.6k
                int len = (int)(litLength - RUN_MASK);
1099
63.6k
                *token = (RUN_MASK<<ML_BITS);
1100
113k
                for(; len >= 255 ; len-=255) *op++ = 255;
1101
63.6k
                *op++ = (BYTE)len;
1102
63.6k
            }
1103
402k
            else *token = (BYTE)(litLength<<ML_BITS);
1104
1105
            /* Copy Literals */
1106
466k
            LZ4_wildCopy8(op, anchor, op+litLength);
1107
466k
            op+=litLength;
1108
466k
            DEBUGLOG(6, "seq.start:%i, literals=%u, match.start:%i",
1109
466k
                        (int)(anchor-(const BYTE*)source), litLength, (int)(ip-(const BYTE*)source));
1110
466k
        }
1111
1112
669k
_next_match:
1113
        /* at this stage, the following variables must be correctly set :
1114
         * - ip : at start of LZ operation
1115
         * - match : at start of previous pattern occurrence; can be within current prefix, or within extDict
1116
         * - offset : if maybe_ext_memSegment==1 (constant)
1117
         * - lowLimit : must be == dictionary to mean "match is within extDict"; must be == source otherwise
1118
         * - token and *token : position to write 4-bits for match length; higher 4-bits for literal length supposed already written
1119
         */
1120
1121
669k
        if ((outputDirective == fillOutput) &&
1122
669k
            (op + 2 /* offset */ + 1 /* token */ + MFLIMIT - MINMATCH /* min last literals so last match is <= end - MFLIMIT */ > olimit)) {
1123
            /* the match was too close to the end, rewind and go to last literals */
1124
0
            op = token;
1125
0
            goto _last_literals;
1126
0
        }
1127
1128
        /* Encode Offset */
1129
669k
        if (maybe_extMem) {   /* static test */
1130
0
            DEBUGLOG(6, "             with offset=%u  (ext if > %i)", offset, (int)(ip - (const BYTE*)source));
1131
0
            assert(offset <= LZ4_DISTANCE_MAX && offset > 0);
1132
0
            LZ4_writeLE16(op, (U16)offset); op+=2;
1133
669k
        } else  {
1134
669k
            DEBUGLOG(6, "             with offset=%u  (same segment)", (U32)(ip - match));
1135
669k
            assert(ip-match <= LZ4_DISTANCE_MAX);
1136
669k
            LZ4_writeLE16(op, (U16)(ip - match)); op+=2;
1137
669k
        }
1138
1139
        /* Encode MatchLength */
1140
669k
        {   unsigned matchCode;
1141
1142
669k
            if ( (dictDirective==usingExtDict || dictDirective==usingDictCtx)
1143
669k
              && (lowLimit==dictionary) /* match within extDict */ ) {
1144
0
                const BYTE* limit = ip + (dictEnd-match);
1145
0
                assert(dictEnd > match);
1146
0
                if (limit > matchlimit) limit = matchlimit;
1147
0
                matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, limit);
1148
0
                ip += (size_t)matchCode + MINMATCH;
1149
0
                if (ip==limit) {
1150
0
                    unsigned const more = LZ4_count(limit, (const BYTE*)source, matchlimit);
1151
0
                    matchCode += more;
1152
0
                    ip += more;
1153
0
                }
1154
0
                DEBUGLOG(6, "             with matchLength=%u starting in extDict", matchCode+MINMATCH);
1155
669k
            } else {
1156
669k
                matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit);
1157
669k
                ip += (size_t)matchCode + MINMATCH;
1158
669k
                DEBUGLOG(6, "             with matchLength=%u", matchCode+MINMATCH);
1159
669k
            }
1160
1161
669k
            if ((outputDirective) &&    /* Check output buffer overflow */
1162
669k
                (unlikely(op + (1 + LASTLITERALS) + (matchCode+240)/255 > olimit)) ) {
1163
0
                if (outputDirective == fillOutput) {
1164
                    /* Match description too long : reduce it */
1165
0
                    U32 newMatchCode = 15 /* in token */ - 1 /* to avoid needing a zero byte */ + ((U32)(olimit - op) - 1 - LASTLITERALS) * 255;
1166
0
                    ip -= matchCode - newMatchCode;
1167
0
                    assert(newMatchCode < matchCode);
1168
0
                    matchCode = newMatchCode;
1169
0
                    if (unlikely(ip <= filledIp)) {
1170
                        /* We have already filled up to filledIp so if ip ends up less than filledIp
1171
                         * we have positions in the hash table beyond the current position. This is
1172
                         * a problem if we reuse the hash table. So we have to remove these positions
1173
                         * from the hash table.
1174
                         */
1175
0
                        const BYTE* ptr;
1176
0
                        DEBUGLOG(5, "Clearing %u positions", (U32)(filledIp - ip));
1177
0
                        for (ptr = ip; ptr <= filledIp; ++ptr) {
1178
0
                            U32 const h = LZ4_hashPosition(ptr, tableType);
1179
0
                            LZ4_clearHash(h, cctx->hashTable, tableType);
1180
0
                        }
1181
0
                    }
1182
0
                } else {
1183
0
                    assert(outputDirective == limitedOutput);
1184
0
                    return 0;   /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */
1185
0
                }
1186
0
            }
1187
669k
            if (matchCode >= ML_MASK) {
1188
389k
                *token += ML_MASK;
1189
389k
                matchCode -= ML_MASK;
1190
389k
                LZ4_write32(op, 0xFFFFFFFF);
1191
434k
                while (matchCode >= 4*255) {
1192
44.6k
                    op+=4;
1193
44.6k
                    LZ4_write32(op, 0xFFFFFFFF);
1194
44.6k
                    matchCode -= 4*255;
1195
44.6k
                }
1196
389k
                op += matchCode / 255;
1197
389k
                *op++ = (BYTE)(matchCode % 255);
1198
389k
            } else
1199
279k
                *token += (BYTE)(matchCode);
1200
669k
        }
1201
        /* Ensure we have enough space for the last literals. */
1202
0
        assert(!(outputDirective == fillOutput && op + 1 + LASTLITERALS > olimit));
1203
1204
669k
        anchor = ip;
1205
1206
        /* Test end of chunk */
1207
669k
        if (ip >= mflimitPlusOne) break;
1208
1209
        /* Fill table */
1210
668k
        {   U32 const h = LZ4_hashPosition(ip-2, tableType);
1211
668k
            if (tableType == byPtr) {
1212
0
                LZ4_putPositionOnHash(ip-2, h, cctx->hashTable, byPtr);
1213
668k
            } else {
1214
668k
                U32 const idx = (U32)((ip-2) - base);
1215
668k
                LZ4_putIndexOnHash(idx, h, cctx->hashTable, tableType);
1216
668k
        }   }
1217
1218
        /* Test next position */
1219
668k
        if (tableType == byPtr) {
1220
1221
0
            match = LZ4_getPosition(ip, cctx->hashTable, tableType);
1222
0
            LZ4_putPosition(ip, cctx->hashTable, tableType);
1223
0
            if ( (match+LZ4_DISTANCE_MAX >= ip)
1224
0
              && (LZ4_read32(match) == LZ4_read32(ip)) )
1225
0
            { token=op++; *token=0; goto _next_match; }
1226
1227
668k
        } else {   /* byU32, byU16 */
1228
1229
668k
            U32 const h = LZ4_hashPosition(ip, tableType);
1230
668k
            U32 const current = (U32)(ip-base);
1231
668k
            U32 matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType);
1232
668k
            assert(matchIndex < current);
1233
668k
            if (dictDirective == usingDictCtx) {
1234
0
                if (matchIndex < startIndex) {
1235
                    /* there was no match, try the dictionary */
1236
0
                    assert(tableType == byU32);
1237
0
                    matchIndex = LZ4_getIndexOnHash(h, dictCtx->hashTable, byU32);
1238
0
                    match = dictBase + matchIndex;
1239
0
                    lowLimit = dictionary;   /* required for match length counter */
1240
0
                    matchIndex += dictDelta;
1241
0
                } else {
1242
0
                    match = base + matchIndex;
1243
0
                    lowLimit = (const BYTE*)source;  /* required for match length counter */
1244
0
                }
1245
668k
            } else if (dictDirective==usingExtDict) {
1246
0
                if (matchIndex < startIndex) {
1247
0
                    assert(dictBase);
1248
0
                    match = dictBase + matchIndex;
1249
0
                    lowLimit = dictionary;   /* required for match length counter */
1250
0
                } else {
1251
0
                    match = base + matchIndex;
1252
0
                    lowLimit = (const BYTE*)source;   /* required for match length counter */
1253
0
                }
1254
668k
            } else {   /* single memory segment */
1255
668k
                match = base + matchIndex;
1256
668k
            }
1257
668k
            LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType);
1258
668k
            assert(matchIndex < current);
1259
668k
            if ( ((dictIssue==dictSmall) ? (matchIndex >= prefixIdxLimit) : 1)
1260
668k
              && (((tableType==byU16) && (LZ4_DISTANCE_MAX == LZ4_DISTANCE_ABSOLUTE_MAX)) ? 1 : (matchIndex+LZ4_DISTANCE_MAX >= current))
1261
668k
              && (LZ4_read32(match) == LZ4_read32(ip)) ) {
1262
202k
                token=op++;
1263
202k
                *token=0;
1264
202k
                if (maybe_extMem) offset = current - matchIndex;
1265
202k
                DEBUGLOG(6, "seq.start:%i, literals=%u, match.start:%i",
1266
202k
                            (int)(anchor-(const BYTE*)source), 0, (int)(ip-(const BYTE*)source));
1267
202k
                goto _next_match;
1268
202k
            }
1269
668k
        }
1270
1271
        /* Prepare next loop */
1272
465k
        forwardH = LZ4_hashPosition(++ip, tableType);
1273
1274
465k
    }
1275
1276
1.81k
_last_literals:
1277
    /* Encode Last Literals */
1278
1.81k
    {   size_t lastRun = (size_t)(iend - anchor);
1279
1.81k
        if ( (outputDirective) &&  /* Check output buffer overflow */
1280
1.81k
            (op + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > olimit)) {
1281
0
            if (outputDirective == fillOutput) {
1282
                /* adapt lastRun to fill 'dst' */
1283
0
                assert(olimit >= op);
1284
0
                lastRun  = (size_t)(olimit-op) - 1/*token*/;
1285
0
                lastRun -= (lastRun + 256 - RUN_MASK) / 256;  /*additional length tokens*/
1286
0
            } else {
1287
0
                assert(outputDirective == limitedOutput);
1288
0
                return 0;   /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */
1289
0
            }
1290
0
        }
1291
1.81k
        DEBUGLOG(6, "Final literal run : %i literals", (int)lastRun);
1292
1.81k
        if (lastRun >= RUN_MASK) {
1293
488
            size_t accumulator = lastRun - RUN_MASK;
1294
488
            *op++ = RUN_MASK << ML_BITS;
1295
99.8k
            for(; accumulator >= 255 ; accumulator-=255) *op++ = 255;
1296
488
            *op++ = (BYTE) accumulator;
1297
1.32k
        } else {
1298
1.32k
            *op++ = (BYTE)(lastRun<<ML_BITS);
1299
1.32k
        }
1300
1.81k
        LZ4_memcpy(op, anchor, lastRun);
1301
1.81k
        ip = anchor + lastRun;
1302
1.81k
        op += lastRun;
1303
1.81k
    }
1304
1305
1.81k
    if (outputDirective == fillOutput) {
1306
0
        *inputConsumed = (int) (((const char*)ip)-source);
1307
0
    }
1308
1.81k
    result = (int)(((char*)op) - dest);
1309
1.81k
    assert(result > 0);
1310
1.81k
    DEBUGLOG(5, "LZ4_compress_generic: compressed %i bytes into %i bytes", inputSize, result);
1311
1.81k
    return result;
1312
1.81k
}
1313
1314
/** LZ4_compress_generic() :
1315
 *  inlined, to ensure branches are decided at compilation time;
1316
 *  takes care of src == (NULL, 0)
1317
 *  and forward the rest to LZ4_compress_generic_validated */
1318
LZ4_FORCE_INLINE int LZ4_compress_generic(
1319
                 LZ4_stream_t_internal* const cctx,
1320
                 const char* const src,
1321
                 char* const dst,
1322
                 const int srcSize,
1323
                 int *inputConsumed, /* only written when outputDirective == fillOutput */
1324
                 const int dstCapacity,
1325
                 const limitedOutput_directive outputDirective,
1326
                 const tableType_t tableType,
1327
                 const dict_directive dictDirective,
1328
                 const dictIssue_directive dictIssue,
1329
                 const int acceleration)
1330
1.81k
{
1331
1.81k
    DEBUGLOG(5, "LZ4_compress_generic: srcSize=%i, dstCapacity=%i",
1332
1.81k
                srcSize, dstCapacity);
1333
1334
1.81k
    if ((U32)srcSize > (U32)LZ4_MAX_INPUT_SIZE) { return 0; }  /* Unsupported srcSize, too large (or negative) */
1335
1.81k
    if (srcSize == 0) {   /* src == NULL supported if srcSize == 0 */
1336
3
        if (outputDirective != notLimited && dstCapacity <= 0) return 0;  /* no output, can't write anything */
1337
3
        DEBUGLOG(5, "Generating an empty block");
1338
3
        assert(outputDirective == notLimited || dstCapacity >= 1);
1339
3
        assert(dst != NULL);
1340
3
        dst[0] = 0;
1341
3
        if (outputDirective == fillOutput) {
1342
0
            assert (inputConsumed != NULL);
1343
0
            *inputConsumed = 0;
1344
0
        }
1345
3
        return 1;
1346
3
    }
1347
1.81k
    assert(src != NULL);
1348
1349
1.81k
    return LZ4_compress_generic_validated(cctx, src, dst, srcSize,
1350
1.81k
                inputConsumed, /* only written into if outputDirective == fillOutput */
1351
1.81k
                dstCapacity, outputDirective,
1352
1.81k
                tableType, dictDirective, dictIssue, acceleration);
1353
1.81k
}
1354
1355
1356
int LZ4_compress_fast_extState(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration)
1357
1.81k
{
1358
1.81k
    LZ4_stream_t_internal* const ctx = & LZ4_initStream(state, sizeof(LZ4_stream_t)) -> internal_donotuse;
1359
1.81k
    assert(ctx != NULL);
1360
1.81k
    if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT;
1361
1.81k
    if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX;
1362
1.81k
    if (maxOutputSize >= LZ4_compressBound(inputSize)) {
1363
1.81k
        if (inputSize < LZ4_64Klimit) {
1364
1.46k
            return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, 0, notLimited, byU16, noDict, noDictIssue, acceleration);
1365
1.46k
        } else {
1366
350
            const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)source > LZ4_DISTANCE_MAX)) ? byPtr : byU32;
1367
350
            return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration);
1368
350
        }
1369
1.81k
    } else {
1370
0
        if (inputSize < LZ4_64Klimit) {
1371
0
            return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration);
1372
0
        } else {
1373
0
            const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)source > LZ4_DISTANCE_MAX)) ? byPtr : byU32;
1374
0
            return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, noDict, noDictIssue, acceleration);
1375
0
        }
1376
0
    }
1377
1.81k
}
1378
1379
/**
1380
 * LZ4_compress_fast_extState_fastReset() :
1381
 * A variant of LZ4_compress_fast_extState().
1382
 *
1383
 * Using this variant avoids an expensive initialization step. It is only safe
1384
 * to call if the state buffer is known to be correctly initialized already
1385
 * (see comment in lz4.h on LZ4_resetStream_fast() for a definition of
1386
 * "correctly initialized").
1387
 */
1388
int LZ4_compress_fast_extState_fastReset(void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration)
1389
0
{
1390
0
    LZ4_stream_t_internal* const ctx = &((LZ4_stream_t*)state)->internal_donotuse;
1391
0
    if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT;
1392
0
    if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX;
1393
0
    assert(ctx != NULL);
1394
1395
0
    if (dstCapacity >= LZ4_compressBound(srcSize)) {
1396
0
        if (srcSize < LZ4_64Klimit) {
1397
0
            const tableType_t tableType = byU16;
1398
0
            LZ4_prepareTable(ctx, srcSize, tableType);
1399
0
            if (ctx->currentOffset) {
1400
0
                return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, dictSmall, acceleration);
1401
0
            } else {
1402
0
                return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration);
1403
0
            }
1404
0
        } else {
1405
0
            const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32;
1406
0
            LZ4_prepareTable(ctx, srcSize, tableType);
1407
0
            return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration);
1408
0
        }
1409
0
    } else {
1410
0
        if (srcSize < LZ4_64Klimit) {
1411
0
            const tableType_t tableType = byU16;
1412
0
            LZ4_prepareTable(ctx, srcSize, tableType);
1413
0
            if (ctx->currentOffset) {
1414
0
                return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, dictSmall, acceleration);
1415
0
            } else {
1416
0
                return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, noDictIssue, acceleration);
1417
0
            }
1418
0
        } else {
1419
0
            const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32;
1420
0
            LZ4_prepareTable(ctx, srcSize, tableType);
1421
0
            return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, noDictIssue, acceleration);
1422
0
        }
1423
0
    }
1424
0
}
1425
1426
1427
int LZ4_compress_fast(const char* src, char* dest, int srcSize, int dstCapacity, int acceleration)
1428
1.81k
{
1429
1.81k
    int result;
1430
#if (LZ4_HEAPMODE)
1431
    LZ4_stream_t* const ctxPtr = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t));   /* malloc-calloc always properly aligned */
1432
    if (ctxPtr == NULL) return 0;
1433
#else
1434
1.81k
    LZ4_stream_t ctx;
1435
1.81k
    LZ4_stream_t* const ctxPtr = &ctx;
1436
1.81k
#endif
1437
1.81k
    result = LZ4_compress_fast_extState(ctxPtr, src, dest, srcSize, dstCapacity, acceleration);
1438
1439
#if (LZ4_HEAPMODE)
1440
    FREEMEM(ctxPtr);
1441
#endif
1442
1.81k
    return result;
1443
1.81k
}
1444
1445
1446
int LZ4_compress_default(const char* src, char* dst, int srcSize, int dstCapacity)
1447
1.81k
{
1448
1.81k
    return LZ4_compress_fast(src, dst, srcSize, dstCapacity, 1);
1449
1.81k
}
1450
1451
1452
/* Note!: This function leaves the stream in an unclean/broken state!
1453
 * It is not safe to subsequently use the same state with a _fastReset() or
1454
 * _continue() call without resetting it. */
1455
static int LZ4_compress_destSize_extState (LZ4_stream_t* state, const char* src, char* dst, int* srcSizePtr, int targetDstSize)
1456
0
{
1457
0
    void* const s = LZ4_initStream(state, sizeof (*state));
1458
0
    assert(s != NULL); (void)s;
1459
1460
0
    if (targetDstSize >= LZ4_compressBound(*srcSizePtr)) {  /* compression success is guaranteed */
1461
0
        return LZ4_compress_fast_extState(state, src, dst, *srcSizePtr, targetDstSize, 1);
1462
0
    } else {
1463
0
        if (*srcSizePtr < LZ4_64Klimit) {
1464
0
            return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, byU16, noDict, noDictIssue, 1);
1465
0
        } else {
1466
0
            tableType_t const addrMode = ((sizeof(void*)==4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32;
1467
0
            return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, addrMode, noDict, noDictIssue, 1);
1468
0
    }   }
1469
0
}
1470
1471
1472
int LZ4_compress_destSize(const char* src, char* dst, int* srcSizePtr, int targetDstSize)
1473
0
{
1474
#if (LZ4_HEAPMODE)
1475
    LZ4_stream_t* const ctx = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t));   /* malloc-calloc always properly aligned */
1476
    if (ctx == NULL) return 0;
1477
#else
1478
0
    LZ4_stream_t ctxBody;
1479
0
    LZ4_stream_t* const ctx = &ctxBody;
1480
0
#endif
1481
1482
0
    int result = LZ4_compress_destSize_extState(ctx, src, dst, srcSizePtr, targetDstSize);
1483
1484
#if (LZ4_HEAPMODE)
1485
    FREEMEM(ctx);
1486
#endif
1487
0
    return result;
1488
0
}
1489
1490
1491
1492
/*-******************************
1493
*  Streaming functions
1494
********************************/
1495
1496
#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION)
1497
LZ4_stream_t* LZ4_createStream(void)
1498
0
{
1499
0
    LZ4_stream_t* const lz4s = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t));
1500
0
    LZ4_STATIC_ASSERT(sizeof(LZ4_stream_t) >= sizeof(LZ4_stream_t_internal));
1501
0
    DEBUGLOG(4, "LZ4_createStream %p", lz4s);
1502
0
    if (lz4s == NULL) return NULL;
1503
0
    LZ4_initStream(lz4s, sizeof(*lz4s));
1504
0
    return lz4s;
1505
0
}
1506
#endif
1507
1508
static size_t LZ4_stream_t_alignment(void)
1509
1.81k
{
1510
1.81k
#if LZ4_ALIGN_TEST
1511
1.81k
    typedef struct { char c; LZ4_stream_t t; } t_a;
1512
1.81k
    return sizeof(t_a) - sizeof(LZ4_stream_t);
1513
#else
1514
    return 1;  /* effectively disabled */
1515
#endif
1516
1.81k
}
1517
1518
LZ4_stream_t* LZ4_initStream (void* buffer, size_t size)
1519
1.81k
{
1520
1.81k
    DEBUGLOG(5, "LZ4_initStream");
1521
1.81k
    if (buffer == NULL) { return NULL; }
1522
1.81k
    if (size < sizeof(LZ4_stream_t)) { return NULL; }
1523
1.81k
    if (!LZ4_isAligned(buffer, LZ4_stream_t_alignment())) return NULL;
1524
1.81k
    MEM_INIT(buffer, 0, sizeof(LZ4_stream_t_internal));
1525
1.81k
    return (LZ4_stream_t*)buffer;
1526
1.81k
}
1527
1528
/* resetStream is now deprecated,
1529
 * prefer initStream() which is more general */
1530
void LZ4_resetStream (LZ4_stream_t* LZ4_stream)
1531
0
{
1532
0
    DEBUGLOG(5, "LZ4_resetStream (ctx:%p)", LZ4_stream);
1533
0
    MEM_INIT(LZ4_stream, 0, sizeof(LZ4_stream_t_internal));
1534
0
}
1535
1536
0
void LZ4_resetStream_fast(LZ4_stream_t* ctx) {
1537
0
    LZ4_prepareTable(&(ctx->internal_donotuse), 0, byU32);
1538
0
}
1539
1540
#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION)
1541
int LZ4_freeStream (LZ4_stream_t* LZ4_stream)
1542
0
{
1543
0
    if (!LZ4_stream) return 0;   /* support free on NULL */
1544
0
    DEBUGLOG(5, "LZ4_freeStream %p", LZ4_stream);
1545
0
    FREEMEM(LZ4_stream);
1546
0
    return (0);
1547
0
}
1548
#endif
1549
1550
1551
0
#define HASH_UNIT sizeof(reg_t)
1552
int LZ4_loadDict (LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize)
1553
0
{
1554
0
    LZ4_stream_t_internal* const dict = &LZ4_dict->internal_donotuse;
1555
0
    const tableType_t tableType = byU32;
1556
0
    const BYTE* p = (const BYTE*)dictionary;
1557
0
    const BYTE* const dictEnd = p + dictSize;
1558
0
    U32 idx32;
1559
1560
0
    DEBUGLOG(4, "LZ4_loadDict (%i bytes from %p into %p)", dictSize, dictionary, LZ4_dict);
1561
1562
    /* It's necessary to reset the context,
1563
     * and not just continue it with prepareTable()
1564
     * to avoid any risk of generating overflowing matchIndex
1565
     * when compressing using this dictionary */
1566
0
    LZ4_resetStream(LZ4_dict);
1567
1568
    /* We always increment the offset by 64 KB, since, if the dict is longer,
1569
     * we truncate it to the last 64k, and if it's shorter, we still want to
1570
     * advance by a whole window length so we can provide the guarantee that
1571
     * there are only valid offsets in the window, which allows an optimization
1572
     * in LZ4_compress_fast_continue() where it uses noDictIssue even when the
1573
     * dictionary isn't a full 64k. */
1574
0
    dict->currentOffset += 64 KB;
1575
1576
0
    if (dictSize < (int)HASH_UNIT) {
1577
0
        return 0;
1578
0
    }
1579
1580
0
    if ((dictEnd - p) > 64 KB) p = dictEnd - 64 KB;
1581
0
    dict->dictionary = p;
1582
0
    dict->dictSize = (U32)(dictEnd - p);
1583
0
    dict->tableType = (U32)tableType;
1584
0
    idx32 = dict->currentOffset - dict->dictSize;
1585
1586
0
    while (p <= dictEnd-HASH_UNIT) {
1587
0
        U32 const h = LZ4_hashPosition(p, tableType);
1588
0
        LZ4_putIndexOnHash(idx32, h, dict->hashTable, tableType);
1589
0
        p+=3; idx32+=3;
1590
0
    }
1591
1592
0
    return (int)dict->dictSize;
1593
0
}
1594
1595
void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dictionaryStream)
1596
0
{
1597
0
    const LZ4_stream_t_internal* dictCtx = (dictionaryStream == NULL) ? NULL :
1598
0
        &(dictionaryStream->internal_donotuse);
1599
1600
0
    DEBUGLOG(4, "LZ4_attach_dictionary (%p, %p, size %u)",
1601
0
             workingStream, dictionaryStream,
1602
0
             dictCtx != NULL ? dictCtx->dictSize : 0);
1603
1604
0
    if (dictCtx != NULL) {
1605
        /* If the current offset is zero, we will never look in the
1606
         * external dictionary context, since there is no value a table
1607
         * entry can take that indicate a miss. In that case, we need
1608
         * to bump the offset to something non-zero.
1609
         */
1610
0
        if (workingStream->internal_donotuse.currentOffset == 0) {
1611
0
            workingStream->internal_donotuse.currentOffset = 64 KB;
1612
0
        }
1613
1614
        /* Don't actually attach an empty dictionary.
1615
         */
1616
0
        if (dictCtx->dictSize == 0) {
1617
0
            dictCtx = NULL;
1618
0
        }
1619
0
    }
1620
0
    workingStream->internal_donotuse.dictCtx = dictCtx;
1621
0
}
1622
1623
1624
static void LZ4_renormDictT(LZ4_stream_t_internal* LZ4_dict, int nextSize)
1625
0
{
1626
0
    assert(nextSize >= 0);
1627
0
    if (LZ4_dict->currentOffset + (unsigned)nextSize > 0x80000000) {   /* potential ptrdiff_t overflow (32-bits mode) */
1628
        /* rescale hash table */
1629
0
        U32 const delta = LZ4_dict->currentOffset - 64 KB;
1630
0
        const BYTE* dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize;
1631
0
        int i;
1632
0
        DEBUGLOG(4, "LZ4_renormDictT");
1633
0
        for (i=0; i<LZ4_HASH_SIZE_U32; i++) {
1634
0
            if (LZ4_dict->hashTable[i] < delta) LZ4_dict->hashTable[i]=0;
1635
0
            else LZ4_dict->hashTable[i] -= delta;
1636
0
        }
1637
0
        LZ4_dict->currentOffset = 64 KB;
1638
0
        if (LZ4_dict->dictSize > 64 KB) LZ4_dict->dictSize = 64 KB;
1639
0
        LZ4_dict->dictionary = dictEnd - LZ4_dict->dictSize;
1640
0
    }
1641
0
}
1642
1643
1644
int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream,
1645
                                const char* source, char* dest,
1646
                                int inputSize, int maxOutputSize,
1647
                                int acceleration)
1648
0
{
1649
0
    const tableType_t tableType = byU32;
1650
0
    LZ4_stream_t_internal* const streamPtr = &LZ4_stream->internal_donotuse;
1651
0
    const char* dictEnd = streamPtr->dictSize ? (const char*)streamPtr->dictionary + streamPtr->dictSize : NULL;
1652
1653
0
    DEBUGLOG(5, "LZ4_compress_fast_continue (inputSize=%i, dictSize=%u)", inputSize, streamPtr->dictSize);
1654
1655
0
    LZ4_renormDictT(streamPtr, inputSize);   /* fix index overflow */
1656
0
    if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT;
1657
0
    if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX;
1658
1659
    /* invalidate tiny dictionaries */
1660
0
    if ( (streamPtr->dictSize < 4)     /* tiny dictionary : not enough for a hash */
1661
0
      && (dictEnd != source)           /* prefix mode */
1662
0
      && (inputSize > 0)               /* tolerance : don't lose history, in case next invocation would use prefix mode */
1663
0
      && (streamPtr->dictCtx == NULL)  /* usingDictCtx */
1664
0
      ) {
1665
0
        DEBUGLOG(5, "LZ4_compress_fast_continue: dictSize(%u) at addr:%p is too small", streamPtr->dictSize, streamPtr->dictionary);
1666
        /* remove dictionary existence from history, to employ faster prefix mode */
1667
0
        streamPtr->dictSize = 0;
1668
0
        streamPtr->dictionary = (const BYTE*)source;
1669
0
        dictEnd = source;
1670
0
    }
1671
1672
    /* Check overlapping input/dictionary space */
1673
0
    {   const char* const sourceEnd = source + inputSize;
1674
0
        if ((sourceEnd > (const char*)streamPtr->dictionary) && (sourceEnd < dictEnd)) {
1675
0
            streamPtr->dictSize = (U32)(dictEnd - sourceEnd);
1676
0
            if (streamPtr->dictSize > 64 KB) streamPtr->dictSize = 64 KB;
1677
0
            if (streamPtr->dictSize < 4) streamPtr->dictSize = 0;
1678
0
            streamPtr->dictionary = (const BYTE*)dictEnd - streamPtr->dictSize;
1679
0
        }
1680
0
    }
1681
1682
    /* prefix mode : source data follows dictionary */
1683
0
    if (dictEnd == source) {
1684
0
        if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset))
1685
0
            return LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, withPrefix64k, dictSmall, acceleration);
1686
0
        else
1687
0
            return LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, withPrefix64k, noDictIssue, acceleration);
1688
0
    }
1689
1690
    /* external dictionary mode */
1691
0
    {   int result;
1692
0
        if (streamPtr->dictCtx) {
1693
            /* We depend here on the fact that dictCtx'es (produced by
1694
             * LZ4_loadDict) guarantee that their tables contain no references
1695
             * to offsets between dictCtx->currentOffset - 64 KB and
1696
             * dictCtx->currentOffset - dictCtx->dictSize. This makes it safe
1697
             * to use noDictIssue even when the dict isn't a full 64 KB.
1698
             */
1699
0
            if (inputSize > 4 KB) {
1700
                /* For compressing large blobs, it is faster to pay the setup
1701
                 * cost to copy the dictionary's tables into the active context,
1702
                 * so that the compression loop is only looking into one table.
1703
                 */
1704
0
                LZ4_memcpy(streamPtr, streamPtr->dictCtx, sizeof(*streamPtr));
1705
0
                result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, noDictIssue, acceleration);
1706
0
            } else {
1707
0
                result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingDictCtx, noDictIssue, acceleration);
1708
0
            }
1709
0
        } else {  /* small data <= 4 KB */
1710
0
            if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) {
1711
0
                result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, dictSmall, acceleration);
1712
0
            } else {
1713
0
                result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, noDictIssue, acceleration);
1714
0
            }
1715
0
        }
1716
0
        streamPtr->dictionary = (const BYTE*)source;
1717
0
        streamPtr->dictSize = (U32)inputSize;
1718
0
        return result;
1719
0
    }
1720
0
}
1721
1722
1723
/* Hidden debug function, to force-test external dictionary mode */
1724
int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* dest, int srcSize)
1725
0
{
1726
0
    LZ4_stream_t_internal* const streamPtr = &LZ4_dict->internal_donotuse;
1727
0
    int result;
1728
1729
0
    LZ4_renormDictT(streamPtr, srcSize);
1730
1731
0
    if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) {
1732
0
        result = LZ4_compress_generic(streamPtr, source, dest, srcSize, NULL, 0, notLimited, byU32, usingExtDict, dictSmall, 1);
1733
0
    } else {
1734
0
        result = LZ4_compress_generic(streamPtr, source, dest, srcSize, NULL, 0, notLimited, byU32, usingExtDict, noDictIssue, 1);
1735
0
    }
1736
1737
0
    streamPtr->dictionary = (const BYTE*)source;
1738
0
    streamPtr->dictSize = (U32)srcSize;
1739
1740
0
    return result;
1741
0
}
1742
1743
1744
/*! LZ4_saveDict() :
1745
 *  If previously compressed data block is not guaranteed to remain available at its memory location,
1746
 *  save it into a safer place (char* safeBuffer).
1747
 *  Note : no need to call LZ4_loadDict() afterwards, dictionary is immediately usable,
1748
 *         one can therefore call LZ4_compress_fast_continue() right after.
1749
 * @return : saved dictionary size in bytes (necessarily <= dictSize), or 0 if error.
1750
 */
1751
int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize)
1752
0
{
1753
0
    LZ4_stream_t_internal* const dict = &LZ4_dict->internal_donotuse;
1754
1755
0
    DEBUGLOG(5, "LZ4_saveDict : dictSize=%i, safeBuffer=%p", dictSize, safeBuffer);
1756
1757
0
    if ((U32)dictSize > 64 KB) { dictSize = 64 KB; } /* useless to define a dictionary > 64 KB */
1758
0
    if ((U32)dictSize > dict->dictSize) { dictSize = (int)dict->dictSize; }
1759
1760
0
    if (safeBuffer == NULL) assert(dictSize == 0);
1761
0
    if (dictSize > 0) {
1762
0
        const BYTE* const previousDictEnd = dict->dictionary + dict->dictSize;
1763
0
        assert(dict->dictionary);
1764
0
        LZ4_memmove(safeBuffer, previousDictEnd - dictSize, (size_t)dictSize);
1765
0
    }
1766
1767
0
    dict->dictionary = (const BYTE*)safeBuffer;
1768
0
    dict->dictSize = (U32)dictSize;
1769
1770
0
    return dictSize;
1771
0
}
1772
1773
1774
1775
/*-*******************************
1776
 *  Decompression functions
1777
 ********************************/
1778
1779
typedef enum { decode_full_block = 0, partial_decode = 1 } earlyEnd_directive;
1780
1781
#undef MIN
1782
17.8k
#define MIN(a,b)    ( (a) < (b) ? (a) : (b) )
1783
1784
1785
/* variant for decompress_unsafe()
1786
 * does not know end of input
1787
 * presumes input is well formed
1788
 * note : will consume at least one byte */
1789
static size_t read_long_length_no_check(const BYTE** pp)
1790
0
{
1791
0
    size_t b, l = 0;
1792
0
    do { b = **pp; (*pp)++; l += b; } while (b==255);
1793
0
    DEBUGLOG(6, "read_long_length_no_check: +length=%zu using %zu input bytes", l, l/255 + 1)
1794
0
    return l;
1795
0
}
1796
1797
/* core decoder variant for LZ4_decompress_fast*()
1798
 * for legacy support only : these entry points are deprecated.
1799
 * - Presumes input is correctly formed (no defense vs malformed inputs)
1800
 * - Does not know input size (presume input buffer is "large enough")
1801
 * - Decompress a full block (only)
1802
 * @return : nb of bytes read from input.
1803
 * Note : this variant is not optimized for speed, just for maintenance.
1804
 *        the goal is to remove support of decompress_fast*() variants by v2.0
1805
**/
1806
LZ4_FORCE_INLINE int
1807
LZ4_decompress_unsafe_generic(
1808
                 const BYTE* const istart,
1809
                 BYTE* const ostart,
1810
                 int decompressedSize,
1811
1812
                 size_t prefixSize,
1813
                 const BYTE* const dictStart,  /* only if dict==usingExtDict */
1814
                 const size_t dictSize         /* note: =0 if dictStart==NULL */
1815
                 )
1816
0
{
1817
0
    const BYTE* ip = istart;
1818
0
    BYTE* op = (BYTE*)ostart;
1819
0
    BYTE* const oend = ostart + decompressedSize;
1820
0
    const BYTE* const prefixStart = ostart - prefixSize;
1821
1822
0
    DEBUGLOG(5, "LZ4_decompress_unsafe_generic");
1823
0
    if (dictStart == NULL) assert(dictSize == 0);
1824
1825
0
    while (1) {
1826
        /* start new sequence */
1827
0
        unsigned token = *ip++;
1828
1829
        /* literals */
1830
0
        {   size_t ll = token >> ML_BITS;
1831
0
            if (ll==15) {
1832
                /* long literal length */
1833
0
                ll += read_long_length_no_check(&ip);
1834
0
            }
1835
0
            if ((size_t)(oend-op) < ll) return -1; /* output buffer overflow */
1836
0
            LZ4_memmove(op, ip, ll); /* support in-place decompression */
1837
0
            op += ll;
1838
0
            ip += ll;
1839
0
            if ((size_t)(oend-op) < MFLIMIT) {
1840
0
                if (op==oend) break;  /* end of block */
1841
0
                DEBUGLOG(5, "invalid: literals end at distance %zi from end of block", oend-op);
1842
                /* incorrect end of block :
1843
                 * last match must start at least MFLIMIT==12 bytes before end of output block */
1844
0
                return -1;
1845
0
        }   }
1846
1847
        /* match */
1848
0
        {   size_t ml = token & 15;
1849
0
            size_t const offset = LZ4_readLE16(ip);
1850
0
            ip+=2;
1851
1852
0
            if (ml==15) {
1853
                /* long literal length */
1854
0
                ml += read_long_length_no_check(&ip);
1855
0
            }
1856
0
            ml += MINMATCH;
1857
1858
0
            if ((size_t)(oend-op) < ml) return -1; /* output buffer overflow */
1859
1860
0
            {   const BYTE* match = op - offset;
1861
1862
                /* out of range */
1863
0
                if (offset > (size_t)(op - prefixStart) + dictSize) {
1864
0
                    DEBUGLOG(6, "offset out of range");
1865
0
                    return -1;
1866
0
                }
1867
1868
                /* check special case : extDict */
1869
0
                if (offset > (size_t)(op - prefixStart)) {
1870
                    /* extDict scenario */
1871
0
                    const BYTE* const dictEnd = dictStart + dictSize;
1872
0
                    const BYTE* extMatch = dictEnd - (offset - (size_t)(op-prefixStart));
1873
0
                    size_t const extml = (size_t)(dictEnd - extMatch);
1874
0
                    if (extml > ml) {
1875
                        /* match entirely within extDict */
1876
0
                        LZ4_memmove(op, extMatch, ml);
1877
0
                        op += ml;
1878
0
                        ml = 0;
1879
0
                    } else {
1880
                        /* match split between extDict & prefix */
1881
0
                        LZ4_memmove(op, extMatch, extml);
1882
0
                        op += extml;
1883
0
                        ml -= extml;
1884
0
                    }
1885
0
                    match = prefixStart;
1886
0
                }
1887
1888
                /* match copy - slow variant, supporting overlap copy */
1889
0
                {   size_t u;
1890
0
                    for (u=0; u<ml; u++) {
1891
0
                        op[u] = match[u];
1892
0
            }   }   }
1893
0
            op += ml;
1894
0
            if ((size_t)(oend-op) < LASTLITERALS) {
1895
0
                DEBUGLOG(5, "invalid: match ends at distance %zi from end of block", oend-op);
1896
                /* incorrect end of block :
1897
                 * last match must stop at least LASTLITERALS==5 bytes before end of output block */
1898
0
                return -1;
1899
0
            }
1900
0
        } /* match */
1901
0
    } /* main loop */
1902
0
    return (int)(ip - istart);
1903
0
}
1904
1905
1906
/* Read the variable-length literal or match length.
1907
 *
1908
 * @ip : input pointer
1909
 * @ilimit : position after which if length is not decoded, the input is necessarily corrupted.
1910
 * @initial_check - check ip >= ipmax before start of loop.  Returns initial_error if so.
1911
 * @error (output) - error code.  Must be set to 0 before call.
1912
**/
1913
typedef size_t Rvl_t;
1914
static const Rvl_t rvl_error = (Rvl_t)(-1);
1915
LZ4_FORCE_INLINE Rvl_t
1916
read_variable_length(const BYTE** ip, const BYTE* ilimit,
1917
                     int initial_check)
1918
466k
{
1919
466k
    Rvl_t s, length = 0;
1920
466k
    assert(ip != NULL);
1921
466k
    assert(*ip !=  NULL);
1922
466k
    assert(ilimit != NULL);
1923
466k
    if (initial_check && unlikely((*ip) >= ilimit)) {    /* read limit reached */
1924
0
        return rvl_error;
1925
0
    }
1926
1.85M
    do {
1927
1.85M
        s = **ip;
1928
1.85M
        (*ip)++;
1929
1.85M
        length += s;
1930
1.85M
        if (unlikely((*ip) > ilimit)) {    /* read limit reached */
1931
0
            return rvl_error;
1932
0
        }
1933
        /* accumulator overflow detection (32-bit mode only) */
1934
1.85M
        if ((sizeof(length)<8) && unlikely(length > ((Rvl_t)(-1)/2)) ) {
1935
0
            return rvl_error;
1936
0
        }
1937
1.85M
    } while (s==255);
1938
1939
466k
    return length;
1940
466k
}
1941
1942
/*! LZ4_decompress_generic() :
1943
 *  This generic decompression function covers all use cases.
1944
 *  It shall be instantiated several times, using different sets of directives.
1945
 *  Note that it is important for performance that this function really get inlined,
1946
 *  in order to remove useless branches during compilation optimization.
1947
 */
1948
LZ4_FORCE_INLINE int
1949
LZ4_decompress_generic(
1950
                 const char* const src,
1951
                 char* const dst,
1952
                 int srcSize,
1953
                 int outputSize,         /* If endOnInput==endOnInputSize, this value is `dstCapacity` */
1954
1955
                 earlyEnd_directive partialDecoding,  /* full, partial */
1956
                 dict_directive dict,                 /* noDict, withPrefix64k, usingExtDict */
1957
                 const BYTE* const lowPrefix,  /* always <= dst, == dst when no prefix */
1958
                 const BYTE* const dictStart,  /* only if dict==usingExtDict */
1959
                 const size_t dictSize         /* note : = 0 if noDict */
1960
                 )
1961
12.7k
{
1962
12.7k
    if ((src == NULL) || (outputSize < 0)) { return -1; }
1963
1964
12.7k
    {   const BYTE* ip = (const BYTE*) src;
1965
12.7k
        const BYTE* const iend = ip + srcSize;
1966
1967
12.7k
        BYTE* op = (BYTE*) dst;
1968
12.7k
        BYTE* const oend = op + outputSize;
1969
12.7k
        BYTE* cpy;
1970
1971
12.7k
        const BYTE* const dictEnd = (dictStart == NULL) ? NULL : dictStart + dictSize;
1972
1973
12.7k
        const int checkOffset = (dictSize < (int)(64 KB));
1974
1975
1976
        /* Set up the "end" pointers for the shortcut. */
1977
12.7k
        const BYTE* const shortiend = iend - 14 /*maxLL*/ - 2 /*offset*/;
1978
12.7k
        const BYTE* const shortoend = oend - 14 /*maxLL*/ - 18 /*maxML*/;
1979
1980
12.7k
        const BYTE* match;
1981
12.7k
        size_t offset;
1982
12.7k
        unsigned token;
1983
12.7k
        size_t length;
1984
1985
1986
12.7k
        DEBUGLOG(5, "LZ4_decompress_generic (srcSize:%i, dstSize:%i)", srcSize, outputSize);
1987
1988
        /* Special cases */
1989
12.7k
        assert(lowPrefix <= op);
1990
12.7k
        if (unlikely(outputSize==0)) {
1991
            /* Empty output buffer */
1992
1.56k
            if (partialDecoding) return 0;
1993
3
            return ((srcSize==1) && (*ip==0)) ? 0 : -1;
1994
1.56k
        }
1995
11.1k
        if (unlikely(srcSize==0)) { return -1; }
1996
1997
    /* LZ4_FAST_DEC_LOOP:
1998
     * designed for modern OoO performance cpus,
1999
     * where copying reliably 32-bytes is preferable to an unpredictable branch.
2000
     * note : fast loop may show a regression for some client arm chips. */
2001
11.1k
#if LZ4_FAST_DEC_LOOP
2002
11.1k
        if ((oend - op) < FASTLOOP_SAFE_DISTANCE) {
2003
5.52k
            DEBUGLOG(6, "skip fast decode loop");
2004
5.52k
            goto safe_decode;
2005
5.52k
        }
2006
2007
        /* Fast loop : decode sequences as long as output < oend-FASTLOOP_SAFE_DISTANCE */
2008
5.62k
        DEBUGLOG(6, "using fast decode loop");
2009
684k
        while (1) {
2010
            /* Main fastloop assertion: We can always wildcopy FASTLOOP_SAFE_DISTANCE */
2011
684k
            assert(oend - op >= FASTLOOP_SAFE_DISTANCE);
2012
684k
            assert(ip < iend);
2013
684k
            token = *ip++;
2014
684k
            length = token >> ML_BITS;  /* literal length */
2015
2016
            /* decode literal length */
2017
684k
            if (length == RUN_MASK) {
2018
67.1k
                size_t const addl = read_variable_length(&ip, iend-RUN_MASK, 1);
2019
67.1k
                if (addl == rvl_error) {
2020
0
                    DEBUGLOG(6, "error reading long literal length");
2021
0
                    goto _output_error;
2022
0
                }
2023
67.1k
                length += addl;
2024
67.1k
                if (unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */
2025
67.1k
                if (unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */
2026
2027
                /* copy literals */
2028
67.1k
                cpy = op+length;
2029
67.1k
                LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH);
2030
67.1k
                if ((cpy>oend-32) || (ip+length>iend-32)) { goto safe_literal_copy; }
2031
65.2k
                LZ4_wildCopy32(op, ip, cpy);
2032
65.2k
                ip += length; op = cpy;
2033
617k
            } else {
2034
617k
                cpy = op+length;
2035
617k
                DEBUGLOG(7, "copy %u bytes in a 16-bytes stripe", (unsigned)length);
2036
                /* We don't need to check oend, since we check it once for each loop below */
2037
617k
                if (ip > iend-(16 + 1/*max lit + offset + nextToken*/)) { goto safe_literal_copy; }
2038
                /* Literals can only be <= 14, but hope compilers optimize better when copy by a register size */
2039
617k
                LZ4_memcpy(op, ip, 16);
2040
617k
                ip += length; op = cpy;
2041
617k
            }
2042
2043
            /* get offset */
2044
682k
            offset = LZ4_readLE16(ip); ip+=2;
2045
682k
            DEBUGLOG(6, " offset = %zu", offset);
2046
682k
            match = op - offset;
2047
682k
            assert(match <= op);  /* overflow check */
2048
2049
            /* get matchlength */
2050
682k
            length = token & ML_MASK;
2051
2052
682k
            if (length == ML_MASK) {
2053
392k
                size_t const addl = read_variable_length(&ip, iend - LASTLITERALS + 1, 0);
2054
392k
                if (addl == rvl_error) {
2055
0
                    DEBUGLOG(6, "error reading long match length");
2056
0
                    goto _output_error;
2057
0
                }
2058
392k
                length += addl;
2059
392k
                length += MINMATCH;
2060
392k
                if (unlikely((uptrval)(op)+length<(uptrval)op)) { goto _output_error; } /* overflow detection */
2061
392k
                if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) {
2062
0
                    DEBUGLOG(6, "Error : offset outside buffers");
2063
0
                    goto _output_error;
2064
0
                }
2065
392k
                if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) {
2066
1.74k
                    goto safe_match_copy;
2067
1.74k
                }
2068
392k
            } else {
2069
290k
                length += MINMATCH;
2070
290k
                if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) {
2071
1.86k
                    goto safe_match_copy;
2072
1.86k
                }
2073
2074
                /* Fastpath check: skip LZ4_wildCopy32 when true */
2075
288k
                if ((dict == withPrefix64k) || (match >= lowPrefix)) {
2076
288k
                    if (offset >= 8) {
2077
256k
                        assert(match >= lowPrefix);
2078
256k
                        assert(match <= op);
2079
256k
                        assert(op + 18 <= oend);
2080
2081
256k
                        LZ4_memcpy(op, match, 8);
2082
256k
                        LZ4_memcpy(op+8, match+8, 8);
2083
256k
                        LZ4_memcpy(op+16, match+16, 2);
2084
256k
                        op += length;
2085
256k
                        continue;
2086
256k
            }   }   }
2087
2088
422k
            if ( checkOffset && (unlikely(match + dictSize < lowPrefix)) ) {
2089
0
                DEBUGLOG(6, "Error : pos=%zi, offset=%zi => outside buffers", op-lowPrefix, op-match);
2090
0
                goto _output_error;
2091
0
            }
2092
            /* match starting within external dictionary */
2093
422k
            if ((dict==usingExtDict) && (match < lowPrefix)) {
2094
0
                assert(dictEnd != NULL);
2095
0
                if (unlikely(op+length > oend-LASTLITERALS)) {
2096
0
                    if (partialDecoding) {
2097
0
                        DEBUGLOG(7, "partialDecoding: dictionary match, close to dstEnd");
2098
0
                        length = MIN(length, (size_t)(oend-op));
2099
0
                    } else {
2100
0
                        DEBUGLOG(6, "end-of-block condition violated")
2101
0
                        goto _output_error;
2102
0
                }   }
2103
2104
0
                if (length <= (size_t)(lowPrefix-match)) {
2105
                    /* match fits entirely within external dictionary : just copy */
2106
0
                    LZ4_memmove(op, dictEnd - (lowPrefix-match), length);
2107
0
                    op += length;
2108
0
                } else {
2109
                    /* match stretches into both external dictionary and current block */
2110
0
                    size_t const copySize = (size_t)(lowPrefix - match);
2111
0
                    size_t const restSize = length - copySize;
2112
0
                    LZ4_memcpy(op, dictEnd - copySize, copySize);
2113
0
                    op += copySize;
2114
0
                    if (restSize > (size_t)(op - lowPrefix)) {  /* overlap copy */
2115
0
                        BYTE* const endOfMatch = op + restSize;
2116
0
                        const BYTE* copyFrom = lowPrefix;
2117
0
                        while (op < endOfMatch) { *op++ = *copyFrom++; }
2118
0
                    } else {
2119
0
                        LZ4_memcpy(op, lowPrefix, restSize);
2120
0
                        op += restSize;
2121
0
                }   }
2122
0
                continue;
2123
0
            }
2124
2125
            /* copy match within block */
2126
422k
            cpy = op + length;
2127
2128
422k
            assert((op <= oend) && (oend-op >= 32));
2129
422k
            if (unlikely(offset<16)) {
2130
45.0k
                LZ4_memcpy_using_offset(op, match, cpy, offset);
2131
377k
            } else {
2132
377k
                LZ4_wildCopy32(op, match, cpy);
2133
377k
            }
2134
2135
422k
            op = cpy;   /* wildcopy correction */
2136
422k
        }
2137
5.52k
    safe_decode:
2138
5.52k
#endif
2139
2140
        /* Main Loop : decode remaining sequences where output < FASTLOOP_SAFE_DISTANCE */
2141
5.52k
        DEBUGLOG(6, "using safe decode loop");
2142
22.7k
        while (1) {
2143
22.7k
            assert(ip < iend);
2144
22.7k
            token = *ip++;
2145
22.7k
            length = token >> ML_BITS;  /* literal length */
2146
2147
            /* A two-stage shortcut for the most common case:
2148
             * 1) If the literal length is 0..14, and there is enough space,
2149
             * enter the shortcut and copy 16 bytes on behalf of the literals
2150
             * (in the fast mode, only 8 bytes can be safely copied this way).
2151
             * 2) Further if the match length is 4..18, copy 18 bytes in a similar
2152
             * manner; but we ensure that there's enough space in the output for
2153
             * those 18 bytes earlier, upon entering the shortcut (in other words,
2154
             * there is a combined check for both stages).
2155
             */
2156
22.7k
            if ( (length != RUN_MASK)
2157
                /* strictly "less than" on input, to re-enter the loop with at least one byte */
2158
22.7k
              && likely((ip < shortiend) & (op <= shortoend)) ) {
2159
                /* Copy the literals */
2160
6.64k
                LZ4_memcpy(op, ip, 16);
2161
6.64k
                op += length; ip += length;
2162
2163
                /* The second stage: prepare for match copying, decode full info.
2164
                 * If it doesn't work out, the info won't be wasted. */
2165
6.64k
                length = token & ML_MASK; /* match length */
2166
6.64k
                offset = LZ4_readLE16(ip); ip += 2;
2167
6.64k
                match = op - offset;
2168
6.64k
                assert(match <= op); /* check overflow */
2169
2170
                /* Do not deal with overlapping matches. */
2171
6.64k
                if ( (length != ML_MASK)
2172
6.64k
                  && (offset >= 8)
2173
6.64k
                  && (dict==withPrefix64k || match >= lowPrefix) ) {
2174
                    /* Copy the match. */
2175
2.83k
                    LZ4_memcpy(op + 0, match + 0, 8);
2176
2.83k
                    LZ4_memcpy(op + 8, match + 8, 8);
2177
2.83k
                    LZ4_memcpy(op +16, match +16, 2);
2178
2.83k
                    op += length + MINMATCH;
2179
                    /* Both stages worked, load the next token. */
2180
2.83k
                    continue;
2181
2.83k
                }
2182
2183
                /* The second stage didn't work out, but the info is ready.
2184
                 * Propel it right to the point of match copying. */
2185
3.80k
                goto _copy_match;
2186
6.64k
            }
2187
2188
            /* decode literal length */
2189
16.0k
            if (length == RUN_MASK) {
2190
3.34k
                size_t const addl = read_variable_length(&ip, iend-RUN_MASK, 1);
2191
3.34k
                if (addl == rvl_error) { goto _output_error; }
2192
3.34k
                length += addl;
2193
3.34k
                if (unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */
2194
3.34k
                if (unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */
2195
3.34k
            }
2196
2197
            /* copy literals */
2198
16.0k
            cpy = op+length;
2199
16.0k
#if LZ4_FAST_DEC_LOOP
2200
18.1k
        safe_literal_copy:
2201
18.1k
#endif
2202
18.1k
            LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH);
2203
18.1k
            if ((cpy>oend-MFLIMIT) || (ip+length>iend-(2+1+LASTLITERALS))) {
2204
                /* We've either hit the input parsing restriction or the output parsing restriction.
2205
                 * In the normal scenario, decoding a full block, it must be the last sequence,
2206
                 * otherwise it's an error (invalid input or dimensions).
2207
                 * In partialDecoding scenario, it's necessary to ensure there is no buffer overflow.
2208
                 */
2209
10.8k
                if (partialDecoding) {
2210
                    /* Since we are partial decoding we may be in this block because of the output parsing
2211
                     * restriction, which is not valid since the output buffer is allowed to be undersized.
2212
                     */
2213
9.07k
                    DEBUGLOG(7, "partialDecoding: copying literals, close to input or output end")
2214
9.07k
                    DEBUGLOG(7, "partialDecoding: literal length = %u", (unsigned)length);
2215
9.07k
                    DEBUGLOG(7, "partialDecoding: remaining space in dstBuffer : %i", (int)(oend - op));
2216
9.07k
                    DEBUGLOG(7, "partialDecoding: remaining space in srcBuffer : %i", (int)(iend - ip));
2217
                    /* Finishing in the middle of a literals segment,
2218
                     * due to lack of input.
2219
                     */
2220
9.07k
                    if (ip+length > iend) {
2221
0
                        length = (size_t)(iend-ip);
2222
0
                        cpy = op + length;
2223
0
                    }
2224
                    /* Finishing in the middle of a literals segment,
2225
                     * due to lack of output space.
2226
                     */
2227
9.07k
                    if (cpy > oend) {
2228
3.79k
                        cpy = oend;
2229
3.79k
                        assert(op<=oend);
2230
3.79k
                        length = (size_t)(oend-op);
2231
3.79k
                    }
2232
9.07k
                } else {
2233
                     /* We must be on the last sequence (or invalid) because of the parsing limitations
2234
                      * so check that we exactly consume the input and don't overrun the output buffer.
2235
                      */
2236
1.81k
                    if ((ip+length != iend) || (cpy > oend)) {
2237
0
                        DEBUGLOG(6, "should have been last run of literals")
2238
0
                        DEBUGLOG(6, "ip(%p) + length(%i) = %p != iend (%p)", ip, (int)length, ip+length, iend);
2239
0
                        DEBUGLOG(6, "or cpy(%p) > oend(%p)", cpy, oend);
2240
0
                        goto _output_error;
2241
0
                    }
2242
1.81k
                }
2243
10.8k
                LZ4_memmove(op, ip, length);  /* supports overlapping memory regions, for in-place decompression scenarios */
2244
10.8k
                ip += length;
2245
10.8k
                op += length;
2246
                /* Necessarily EOF when !partialDecoding.
2247
                 * When partialDecoding, it is EOF if we've either
2248
                 * filled the output buffer or
2249
                 * can't proceed with reading an offset for following match.
2250
                 */
2251
10.8k
                if (!partialDecoding || (cpy == oend) || (ip >= (iend-2))) {
2252
7.25k
                    break;
2253
7.25k
                }
2254
10.8k
            } else {
2255
7.22k
                LZ4_wildCopy8(op, ip, cpy);   /* can overwrite up to 8 bytes beyond cpy */
2256
7.22k
                ip += length; op = cpy;
2257
7.22k
            }
2258
2259
            /* get offset */
2260
10.8k
            offset = LZ4_readLE16(ip); ip+=2;
2261
10.8k
            match = op - offset;
2262
2263
            /* get matchlength */
2264
10.8k
            length = token & ML_MASK;
2265
2266
14.6k
    _copy_match:
2267
14.6k
            if (length == ML_MASK) {
2268
2.82k
                size_t const addl = read_variable_length(&ip, iend - LASTLITERALS + 1, 0);
2269
2.82k
                if (addl == rvl_error) { goto _output_error; }
2270
2.82k
                length += addl;
2271
2.82k
                if (unlikely((uptrval)(op)+length<(uptrval)op)) goto _output_error;   /* overflow detection */
2272
2.82k
            }
2273
14.6k
            length += MINMATCH;
2274
2275
14.6k
#if LZ4_FAST_DEC_LOOP
2276
18.2k
        safe_match_copy:
2277
18.2k
#endif
2278
18.2k
            if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) goto _output_error;   /* Error : offset outside buffers */
2279
            /* match starting within external dictionary */
2280
18.2k
            if ((dict==usingExtDict) && (match < lowPrefix)) {
2281
0
                assert(dictEnd != NULL);
2282
0
                if (unlikely(op+length > oend-LASTLITERALS)) {
2283
0
                    if (partialDecoding) length = MIN(length, (size_t)(oend-op));
2284
0
                    else goto _output_error;   /* doesn't respect parsing restriction */
2285
0
                }
2286
2287
0
                if (length <= (size_t)(lowPrefix-match)) {
2288
                    /* match fits entirely within external dictionary : just copy */
2289
0
                    LZ4_memmove(op, dictEnd - (lowPrefix-match), length);
2290
0
                    op += length;
2291
0
                } else {
2292
                    /* match stretches into both external dictionary and current block */
2293
0
                    size_t const copySize = (size_t)(lowPrefix - match);
2294
0
                    size_t const restSize = length - copySize;
2295
0
                    LZ4_memcpy(op, dictEnd - copySize, copySize);
2296
0
                    op += copySize;
2297
0
                    if (restSize > (size_t)(op - lowPrefix)) {  /* overlap copy */
2298
0
                        BYTE* const endOfMatch = op + restSize;
2299
0
                        const BYTE* copyFrom = lowPrefix;
2300
0
                        while (op < endOfMatch) *op++ = *copyFrom++;
2301
0
                    } else {
2302
0
                        LZ4_memcpy(op, lowPrefix, restSize);
2303
0
                        op += restSize;
2304
0
                }   }
2305
0
                continue;
2306
0
            }
2307
18.2k
            assert(match >= lowPrefix);
2308
2309
            /* copy match within block */
2310
18.2k
            cpy = op + length;
2311
2312
            /* partialDecoding : may end anywhere within the block */
2313
18.2k
            assert(op<=oend);
2314
18.2k
            if (partialDecoding && (cpy > oend-MATCH_SAFEGUARD_DISTANCE)) {
2315
6.92k
                size_t const mlen = MIN(length, (size_t)(oend-op));
2316
6.92k
                const BYTE* const matchEnd = match + mlen;
2317
6.92k
                BYTE* const copyEnd = op + mlen;
2318
6.92k
                if (matchEnd > op) {   /* overlap copy */
2319
171k
                    while (op < copyEnd) { *op++ = *match++; }
2320
4.00k
                } else {
2321
2.92k
                    LZ4_memcpy(op, match, mlen);
2322
2.92k
                }
2323
6.92k
                op = copyEnd;
2324
6.92k
                if (op == oend) { break; }
2325
3.03k
                continue;
2326
6.92k
            }
2327
2328
11.3k
            if (unlikely(offset<8)) {
2329
7.26k
                LZ4_write32(op, 0);   /* silence msan warning when offset==0 */
2330
7.26k
                op[0] = match[0];
2331
7.26k
                op[1] = match[1];
2332
7.26k
                op[2] = match[2];
2333
7.26k
                op[3] = match[3];
2334
7.26k
                match += inc32table[offset];
2335
7.26k
                LZ4_memcpy(op+4, match, 4);
2336
7.26k
                match -= dec64table[offset];
2337
7.26k
            } else {
2338
4.08k
                LZ4_memcpy(op, match, 8);
2339
4.08k
                match += 8;
2340
4.08k
            }
2341
11.3k
            op += 8;
2342
2343
11.3k
            if (unlikely(cpy > oend-MATCH_SAFEGUARD_DISTANCE)) {
2344
927
                BYTE* const oCopyLimit = oend - (WILDCOPYLENGTH-1);
2345
927
                if (cpy > oend-LASTLITERALS) { goto _output_error; } /* Error : last LASTLITERALS bytes must be literals (uncompressed) */
2346
927
                if (op < oCopyLimit) {
2347
453
                    LZ4_wildCopy8(op, match, oCopyLimit);
2348
453
                    match += oCopyLimit - op;
2349
453
                    op = oCopyLimit;
2350
453
                }
2351
1.54k
                while (op < cpy) { *op++ = *match++; }
2352
10.4k
            } else {
2353
10.4k
                LZ4_memcpy(op, match, 8);
2354
10.4k
                if (length > 16)  { LZ4_wildCopy8(op+8, match+8, cpy); }
2355
10.4k
            }
2356
11.3k
            op = cpy;   /* wildcopy correction */
2357
11.3k
        }
2358
2359
        /* end of decoding */
2360
11.1k
        DEBUGLOG(5, "decoded %i bytes", (int) (((char*)op)-dst));
2361
11.1k
        return (int) (((char*)op)-dst);     /* Nb of output bytes decoded */
2362
2363
        /* Overflow error detected */
2364
0
    _output_error:
2365
0
        return (int) (-(((const char*)ip)-src))-1;
2366
5.52k
    }
2367
5.52k
}
2368
2369
2370
/*===== Instantiate the API decoding functions. =====*/
2371
2372
LZ4_FORCE_O2
2373
int LZ4_decompress_safe(const char* source, char* dest, int compressedSize, int maxDecompressedSize)
2374
1.81k
{
2375
1.81k
    return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize,
2376
1.81k
                                  decode_full_block, noDict,
2377
1.81k
                                  (BYTE*)dest, NULL, 0);
2378
1.81k
}
2379
2380
LZ4_FORCE_O2
2381
int LZ4_decompress_safe_partial(const char* src, char* dst, int compressedSize, int targetOutputSize, int dstCapacity)
2382
3.63k
{
2383
3.63k
    dstCapacity = MIN(targetOutputSize, dstCapacity);
2384
3.63k
    return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity,
2385
3.63k
                                  partial_decode,
2386
3.63k
                                  noDict, (BYTE*)dst, NULL, 0);
2387
3.63k
}
2388
2389
LZ4_FORCE_O2
2390
int LZ4_decompress_fast(const char* source, char* dest, int originalSize)
2391
0
{
2392
0
    DEBUGLOG(5, "LZ4_decompress_fast");
2393
0
    return LZ4_decompress_unsafe_generic(
2394
0
                (const BYTE*)source, (BYTE*)dest, originalSize,
2395
0
                0, NULL, 0);
2396
0
}
2397
2398
/*===== Instantiate a few more decoding cases, used more than once. =====*/
2399
2400
LZ4_FORCE_O2 /* Exported, an obsolete API function. */
2401
int LZ4_decompress_safe_withPrefix64k(const char* source, char* dest, int compressedSize, int maxOutputSize)
2402
0
{
2403
0
    return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize,
2404
0
                                  decode_full_block, withPrefix64k,
2405
0
                                  (BYTE*)dest - 64 KB, NULL, 0);
2406
0
}
2407
2408
LZ4_FORCE_O2
2409
static int LZ4_decompress_safe_partial_withPrefix64k(const char* source, char* dest, int compressedSize, int targetOutputSize, int dstCapacity)
2410
0
{
2411
0
    dstCapacity = MIN(targetOutputSize, dstCapacity);
2412
0
    return LZ4_decompress_generic(source, dest, compressedSize, dstCapacity,
2413
0
                                  partial_decode, withPrefix64k,
2414
0
                                  (BYTE*)dest - 64 KB, NULL, 0);
2415
0
}
2416
2417
/* Another obsolete API function, paired with the previous one. */
2418
int LZ4_decompress_fast_withPrefix64k(const char* source, char* dest, int originalSize)
2419
0
{
2420
0
    return LZ4_decompress_unsafe_generic(
2421
0
                (const BYTE*)source, (BYTE*)dest, originalSize,
2422
0
                64 KB, NULL, 0);
2423
0
}
2424
2425
LZ4_FORCE_O2
2426
static int LZ4_decompress_safe_withSmallPrefix(const char* source, char* dest, int compressedSize, int maxOutputSize,
2427
                                               size_t prefixSize)
2428
0
{
2429
0
    return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize,
2430
0
                                  decode_full_block, noDict,
2431
0
                                  (BYTE*)dest-prefixSize, NULL, 0);
2432
0
}
2433
2434
LZ4_FORCE_O2
2435
static int LZ4_decompress_safe_partial_withSmallPrefix(const char* source, char* dest, int compressedSize, int targetOutputSize, int dstCapacity,
2436
                                               size_t prefixSize)
2437
0
{
2438
0
    dstCapacity = MIN(targetOutputSize, dstCapacity);
2439
0
    return LZ4_decompress_generic(source, dest, compressedSize, dstCapacity,
2440
0
                                  partial_decode, noDict,
2441
0
                                  (BYTE*)dest-prefixSize, NULL, 0);
2442
0
}
2443
2444
LZ4_FORCE_O2
2445
int LZ4_decompress_safe_forceExtDict(const char* source, char* dest,
2446
                                     int compressedSize, int maxOutputSize,
2447
                                     const void* dictStart, size_t dictSize)
2448
0
{
2449
0
    DEBUGLOG(5, "LZ4_decompress_safe_forceExtDict");
2450
0
    return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize,
2451
0
                                  decode_full_block, usingExtDict,
2452
0
                                  (BYTE*)dest, (const BYTE*)dictStart, dictSize);
2453
0
}
2454
2455
LZ4_FORCE_O2
2456
int LZ4_decompress_safe_partial_forceExtDict(const char* source, char* dest,
2457
                                     int compressedSize, int targetOutputSize, int dstCapacity,
2458
                                     const void* dictStart, size_t dictSize)
2459
7.26k
{
2460
7.26k
    dstCapacity = MIN(targetOutputSize, dstCapacity);
2461
7.26k
    return LZ4_decompress_generic(source, dest, compressedSize, dstCapacity,
2462
7.26k
                                  partial_decode, usingExtDict,
2463
7.26k
                                  (BYTE*)dest, (const BYTE*)dictStart, dictSize);
2464
7.26k
}
2465
2466
LZ4_FORCE_O2
2467
static int LZ4_decompress_fast_extDict(const char* source, char* dest, int originalSize,
2468
                                       const void* dictStart, size_t dictSize)
2469
0
{
2470
0
    return LZ4_decompress_unsafe_generic(
2471
0
                (const BYTE*)source, (BYTE*)dest, originalSize,
2472
0
                0, (const BYTE*)dictStart, dictSize);
2473
0
}
2474
2475
/* The "double dictionary" mode, for use with e.g. ring buffers: the first part
2476
 * of the dictionary is passed as prefix, and the second via dictStart + dictSize.
2477
 * These routines are used only once, in LZ4_decompress_*_continue().
2478
 */
2479
LZ4_FORCE_INLINE
2480
int LZ4_decompress_safe_doubleDict(const char* source, char* dest, int compressedSize, int maxOutputSize,
2481
                                   size_t prefixSize, const void* dictStart, size_t dictSize)
2482
0
{
2483
0
    return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize,
2484
0
                                  decode_full_block, usingExtDict,
2485
0
                                  (BYTE*)dest-prefixSize, (const BYTE*)dictStart, dictSize);
2486
0
}
2487
2488
/*===== streaming decompression functions =====*/
2489
2490
#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION)
2491
LZ4_streamDecode_t* LZ4_createStreamDecode(void)
2492
0
{
2493
0
    LZ4_STATIC_ASSERT(sizeof(LZ4_streamDecode_t) >= sizeof(LZ4_streamDecode_t_internal));
2494
0
    return (LZ4_streamDecode_t*) ALLOC_AND_ZERO(sizeof(LZ4_streamDecode_t));
2495
0
}
2496
2497
int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream)
2498
0
{
2499
0
    if (LZ4_stream == NULL) { return 0; }  /* support free on NULL */
2500
0
    FREEMEM(LZ4_stream);
2501
0
    return 0;
2502
0
}
2503
#endif
2504
2505
/*! LZ4_setStreamDecode() :
2506
 *  Use this function to instruct where to find the dictionary.
2507
 *  This function is not necessary if previous data is still available where it was decoded.
2508
 *  Loading a size of 0 is allowed (same effect as no dictionary).
2509
 * @return : 1 if OK, 0 if error
2510
 */
2511
int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize)
2512
0
{
2513
0
    LZ4_streamDecode_t_internal* lz4sd = &LZ4_streamDecode->internal_donotuse;
2514
0
    lz4sd->prefixSize = (size_t)dictSize;
2515
0
    if (dictSize) {
2516
0
        assert(dictionary != NULL);
2517
0
        lz4sd->prefixEnd = (const BYTE*) dictionary + dictSize;
2518
0
    } else {
2519
0
        lz4sd->prefixEnd = (const BYTE*) dictionary;
2520
0
    }
2521
0
    lz4sd->externalDict = NULL;
2522
0
    lz4sd->extDictSize  = 0;
2523
0
    return 1;
2524
0
}
2525
2526
/*! LZ4_decoderRingBufferSize() :
2527
 *  when setting a ring buffer for streaming decompression (optional scenario),
2528
 *  provides the minimum size of this ring buffer
2529
 *  to be compatible with any source respecting maxBlockSize condition.
2530
 *  Note : in a ring buffer scenario,
2531
 *  blocks are presumed decompressed next to each other.
2532
 *  When not enough space remains for next block (remainingSize < maxBlockSize),
2533
 *  decoding resumes from beginning of ring buffer.
2534
 * @return : minimum ring buffer size,
2535
 *           or 0 if there is an error (invalid maxBlockSize).
2536
 */
2537
int LZ4_decoderRingBufferSize(int maxBlockSize)
2538
0
{
2539
0
    if (maxBlockSize < 0) return 0;
2540
0
    if (maxBlockSize > LZ4_MAX_INPUT_SIZE) return 0;
2541
0
    if (maxBlockSize < 16) maxBlockSize = 16;
2542
0
    return LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize);
2543
0
}
2544
2545
/*
2546
*_continue() :
2547
    These decoding functions allow decompression of multiple blocks in "streaming" mode.
2548
    Previously decoded blocks must still be available at the memory position where they were decoded.
2549
    If it's not possible, save the relevant part of decoded data into a safe buffer,
2550
    and indicate where it stands using LZ4_setStreamDecode()
2551
*/
2552
LZ4_FORCE_O2
2553
int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxOutputSize)
2554
0
{
2555
0
    LZ4_streamDecode_t_internal* lz4sd = &LZ4_streamDecode->internal_donotuse;
2556
0
    int result;
2557
2558
0
    if (lz4sd->prefixSize == 0) {
2559
        /* The first call, no dictionary yet. */
2560
0
        assert(lz4sd->extDictSize == 0);
2561
0
        result = LZ4_decompress_safe(source, dest, compressedSize, maxOutputSize);
2562
0
        if (result <= 0) return result;
2563
0
        lz4sd->prefixSize = (size_t)result;
2564
0
        lz4sd->prefixEnd = (BYTE*)dest + result;
2565
0
    } else if (lz4sd->prefixEnd == (BYTE*)dest) {
2566
        /* They're rolling the current segment. */
2567
0
        if (lz4sd->prefixSize >= 64 KB - 1)
2568
0
            result = LZ4_decompress_safe_withPrefix64k(source, dest, compressedSize, maxOutputSize);
2569
0
        else if (lz4sd->extDictSize == 0)
2570
0
            result = LZ4_decompress_safe_withSmallPrefix(source, dest, compressedSize, maxOutputSize,
2571
0
                                                         lz4sd->prefixSize);
2572
0
        else
2573
0
            result = LZ4_decompress_safe_doubleDict(source, dest, compressedSize, maxOutputSize,
2574
0
                                                    lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize);
2575
0
        if (result <= 0) return result;
2576
0
        lz4sd->prefixSize += (size_t)result;
2577
0
        lz4sd->prefixEnd  += result;
2578
0
    } else {
2579
        /* The buffer wraps around, or they're switching to another buffer. */
2580
0
        lz4sd->extDictSize = lz4sd->prefixSize;
2581
0
        lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
2582
0
        result = LZ4_decompress_safe_forceExtDict(source, dest, compressedSize, maxOutputSize,
2583
0
                                                  lz4sd->externalDict, lz4sd->extDictSize);
2584
0
        if (result <= 0) return result;
2585
0
        lz4sd->prefixSize = (size_t)result;
2586
0
        lz4sd->prefixEnd  = (BYTE*)dest + result;
2587
0
    }
2588
2589
0
    return result;
2590
0
}
2591
2592
LZ4_FORCE_O2 int
2593
LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode,
2594
                        const char* source, char* dest, int originalSize)
2595
0
{
2596
0
    LZ4_streamDecode_t_internal* const lz4sd =
2597
0
        (assert(LZ4_streamDecode!=NULL), &LZ4_streamDecode->internal_donotuse);
2598
0
    int result;
2599
2600
0
    DEBUGLOG(5, "LZ4_decompress_fast_continue (toDecodeSize=%i)", originalSize);
2601
0
    assert(originalSize >= 0);
2602
2603
0
    if (lz4sd->prefixSize == 0) {
2604
0
        DEBUGLOG(5, "first invocation : no prefix nor extDict");
2605
0
        assert(lz4sd->extDictSize == 0);
2606
0
        result = LZ4_decompress_fast(source, dest, originalSize);
2607
0
        if (result <= 0) return result;
2608
0
        lz4sd->prefixSize = (size_t)originalSize;
2609
0
        lz4sd->prefixEnd = (BYTE*)dest + originalSize;
2610
0
    } else if (lz4sd->prefixEnd == (BYTE*)dest) {
2611
0
        DEBUGLOG(5, "continue using existing prefix");
2612
0
        result = LZ4_decompress_unsafe_generic(
2613
0
                        (const BYTE*)source, (BYTE*)dest, originalSize,
2614
0
                        lz4sd->prefixSize,
2615
0
                        lz4sd->externalDict, lz4sd->extDictSize);
2616
0
        if (result <= 0) return result;
2617
0
        lz4sd->prefixSize += (size_t)originalSize;
2618
0
        lz4sd->prefixEnd  += originalSize;
2619
0
    } else {
2620
0
        DEBUGLOG(5, "prefix becomes extDict");
2621
0
        lz4sd->extDictSize = lz4sd->prefixSize;
2622
0
        lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
2623
0
        result = LZ4_decompress_fast_extDict(source, dest, originalSize,
2624
0
                                             lz4sd->externalDict, lz4sd->extDictSize);
2625
0
        if (result <= 0) return result;
2626
0
        lz4sd->prefixSize = (size_t)originalSize;
2627
0
        lz4sd->prefixEnd  = (BYTE*)dest + originalSize;
2628
0
    }
2629
2630
0
    return result;
2631
0
}
2632
2633
2634
/*
2635
Advanced decoding functions :
2636
*_usingDict() :
2637
    These decoding functions work the same as "_continue" ones,
2638
    the dictionary must be explicitly provided within parameters
2639
*/
2640
2641
int LZ4_decompress_safe_usingDict(const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize)
2642
0
{
2643
0
    if (dictSize==0)
2644
0
        return LZ4_decompress_safe(source, dest, compressedSize, maxOutputSize);
2645
0
    if (dictStart+dictSize == dest) {
2646
0
        if (dictSize >= 64 KB - 1) {
2647
0
            return LZ4_decompress_safe_withPrefix64k(source, dest, compressedSize, maxOutputSize);
2648
0
        }
2649
0
        assert(dictSize >= 0);
2650
0
        return LZ4_decompress_safe_withSmallPrefix(source, dest, compressedSize, maxOutputSize, (size_t)dictSize);
2651
0
    }
2652
0
    assert(dictSize >= 0);
2653
0
    return LZ4_decompress_safe_forceExtDict(source, dest, compressedSize, maxOutputSize, dictStart, (size_t)dictSize);
2654
0
}
2655
2656
int LZ4_decompress_safe_partial_usingDict(const char* source, char* dest, int compressedSize, int targetOutputSize, int dstCapacity, const char* dictStart, int dictSize)
2657
9.07k
{
2658
9.07k
    if (dictSize==0)
2659
1.81k
        return LZ4_decompress_safe_partial(source, dest, compressedSize, targetOutputSize, dstCapacity);
2660
7.26k
    if (dictStart+dictSize == dest) {
2661
0
        if (dictSize >= 64 KB - 1) {
2662
0
            return LZ4_decompress_safe_partial_withPrefix64k(source, dest, compressedSize, targetOutputSize, dstCapacity);
2663
0
        }
2664
0
        assert(dictSize >= 0);
2665
0
        return LZ4_decompress_safe_partial_withSmallPrefix(source, dest, compressedSize, targetOutputSize, dstCapacity, (size_t)dictSize);
2666
0
    }
2667
7.26k
    assert(dictSize >= 0);
2668
7.26k
    return LZ4_decompress_safe_partial_forceExtDict(source, dest, compressedSize, targetOutputSize, dstCapacity, dictStart, (size_t)dictSize);
2669
7.26k
}
2670
2671
int LZ4_decompress_fast_usingDict(const char* source, char* dest, int originalSize, const char* dictStart, int dictSize)
2672
0
{
2673
0
    if (dictSize==0 || dictStart+dictSize == dest)
2674
0
        return LZ4_decompress_unsafe_generic(
2675
0
                        (const BYTE*)source, (BYTE*)dest, originalSize,
2676
0
                        (size_t)dictSize, NULL, 0);
2677
0
    assert(dictSize >= 0);
2678
0
    return LZ4_decompress_fast_extDict(source, dest, originalSize, dictStart, (size_t)dictSize);
2679
0
}
2680
2681
2682
/*=*************************************************
2683
*  Obsolete Functions
2684
***************************************************/
2685
/* obsolete compression functions */
2686
int LZ4_compress_limitedOutput(const char* source, char* dest, int inputSize, int maxOutputSize)
2687
0
{
2688
0
    return LZ4_compress_default(source, dest, inputSize, maxOutputSize);
2689
0
}
2690
int LZ4_compress(const char* src, char* dest, int srcSize)
2691
0
{
2692
0
    return LZ4_compress_default(src, dest, srcSize, LZ4_compressBound(srcSize));
2693
0
}
2694
int LZ4_compress_limitedOutput_withState (void* state, const char* src, char* dst, int srcSize, int dstSize)
2695
0
{
2696
0
    return LZ4_compress_fast_extState(state, src, dst, srcSize, dstSize, 1);
2697
0
}
2698
int LZ4_compress_withState (void* state, const char* src, char* dst, int srcSize)
2699
0
{
2700
0
    return LZ4_compress_fast_extState(state, src, dst, srcSize, LZ4_compressBound(srcSize), 1);
2701
0
}
2702
int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_stream, const char* src, char* dst, int srcSize, int dstCapacity)
2703
0
{
2704
0
    return LZ4_compress_fast_continue(LZ4_stream, src, dst, srcSize, dstCapacity, 1);
2705
0
}
2706
int LZ4_compress_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize)
2707
0
{
2708
0
    return LZ4_compress_fast_continue(LZ4_stream, source, dest, inputSize, LZ4_compressBound(inputSize), 1);
2709
0
}
2710
2711
/*
2712
These decompression functions are deprecated and should no longer be used.
2713
They are only provided here for compatibility with older user programs.
2714
- LZ4_uncompress is totally equivalent to LZ4_decompress_fast
2715
- LZ4_uncompress_unknownOutputSize is totally equivalent to LZ4_decompress_safe
2716
*/
2717
int LZ4_uncompress (const char* source, char* dest, int outputSize)
2718
0
{
2719
0
    return LZ4_decompress_fast(source, dest, outputSize);
2720
0
}
2721
int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize)
2722
0
{
2723
0
    return LZ4_decompress_safe(source, dest, isize, maxOutputSize);
2724
0
}
2725
2726
/* Obsolete Streaming functions */
2727
2728
0
int LZ4_sizeofStreamState(void) { return sizeof(LZ4_stream_t); }
2729
2730
int LZ4_resetStreamState(void* state, char* inputBuffer)
2731
0
{
2732
0
    (void)inputBuffer;
2733
0
    LZ4_resetStream((LZ4_stream_t*)state);
2734
0
    return 0;
2735
0
}
2736
2737
#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION)
2738
void* LZ4_create (char* inputBuffer)
2739
0
{
2740
0
    (void)inputBuffer;
2741
0
    return LZ4_createStream();
2742
0
}
2743
#endif
2744
2745
char* LZ4_slideInputBuffer (void* state)
2746
0
{
2747
    /* avoid const char * -> char * conversion warning */
2748
0
    return (char *)(uptrval)((LZ4_stream_t*)state)->internal_donotuse.dictionary;
2749
0
}
2750
2751
#endif   /* LZ4_COMMONDEFS_ONLY */