Coverage Report

Created: 2026-03-19 07:00

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