Coverage Report

Created: 2025-11-16 06:19

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