Coverage Report

Created: 2026-10-17 06:51

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