extlz4 0.3.3 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/Rakefile +43 -3
  4. data/contrib/lz4/CODING_STYLE +57 -0
  5. data/contrib/lz4/LICENSE +3 -2
  6. data/contrib/lz4/Makefile.inc +56 -30
  7. data/contrib/lz4/NEWS +46 -0
  8. data/contrib/lz4/README.md +17 -6
  9. data/contrib/lz4/SECURITY.md +17 -0
  10. data/contrib/lz4/build/README.md +4 -15
  11. data/contrib/lz4/build/VS2022/_build.bat +39 -0
  12. data/contrib/lz4/build/VS2022/_setup.bat +35 -0
  13. data/contrib/lz4/build/VS2022/_test.bat +38 -0
  14. data/contrib/lz4/build/VS2022/build-and-test-win32-debug.bat +26 -0
  15. data/contrib/lz4/build/VS2022/build-and-test-win32-release.bat +26 -0
  16. data/contrib/lz4/build/VS2022/build-and-test-x64-debug.bat +26 -0
  17. data/contrib/lz4/build/VS2022/build-and-test-x64-release.bat +26 -0
  18. data/contrib/lz4/build/{VS2017 → VS2022}/datagen/datagen.vcxproj +11 -7
  19. data/contrib/lz4/build/{VS2017 → VS2022}/frametest/frametest.vcxproj +4 -4
  20. data/contrib/lz4/build/{VS2017 → VS2022}/fullbench/fullbench.vcxproj +4 -4
  21. data/contrib/lz4/build/{VS2017 → VS2022}/fullbench-dll/fullbench-dll.vcxproj +4 -4
  22. data/contrib/lz4/build/{VS2017 → VS2022}/fuzzer/fuzzer.vcxproj +4 -4
  23. data/contrib/lz4/build/{VS2017 → VS2022}/liblz4/liblz4.vcxproj +4 -4
  24. data/contrib/lz4/build/{VS2010 → VS2022}/liblz4-dll/liblz4-dll.rc +1 -1
  25. data/contrib/lz4/build/{VS2017 → VS2022}/liblz4-dll/liblz4-dll.vcxproj +4 -4
  26. data/contrib/lz4/build/{VS2010 → VS2022}/lz4/lz4.rc +1 -1
  27. data/contrib/lz4/build/{VS2017 → VS2022}/lz4/lz4.vcxproj +33 -8
  28. data/contrib/lz4/build/{VS2017 → VS2022}/lz4.sln +5 -2
  29. data/contrib/lz4/build/cmake/CMakeLists.txt +133 -100
  30. data/contrib/lz4/build/cmake/lz4Config.cmake.in +2 -0
  31. data/contrib/lz4/build/meson/GetLz4LibraryVersion.py +39 -0
  32. data/contrib/lz4/build/meson/README.md +34 -0
  33. data/contrib/lz4/build/meson/meson/contrib/gen_manual/meson.build +42 -0
  34. data/contrib/lz4/build/meson/meson/contrib/meson.build +11 -0
  35. data/contrib/lz4/build/meson/meson/examples/meson.build +32 -0
  36. data/contrib/lz4/build/meson/meson/lib/meson.build +87 -0
  37. data/contrib/lz4/build/meson/meson/meson.build +135 -0
  38. data/contrib/lz4/build/meson/meson/ossfuzz/meson.build +35 -0
  39. data/contrib/lz4/build/meson/meson/programs/meson.build +91 -0
  40. data/contrib/lz4/build/meson/meson/tests/meson.build +162 -0
  41. data/contrib/lz4/build/meson/meson.build +31 -0
  42. data/contrib/lz4/build/meson/meson_options.txt +44 -0
  43. data/contrib/lz4/build/visual/README.md +5 -0
  44. data/contrib/lz4/build/visual/generate_solution.cmd +55 -0
  45. data/contrib/lz4/build/visual/generate_vs2015.cmd +3 -0
  46. data/contrib/lz4/build/visual/generate_vs2017.cmd +3 -0
  47. data/contrib/lz4/build/visual/generate_vs2019.cmd +3 -0
  48. data/contrib/lz4/build/visual/generate_vs2022.cmd +3 -0
  49. data/contrib/lz4/lib/LICENSE +1 -1
  50. data/contrib/lz4/lib/README.md +69 -13
  51. data/contrib/lz4/lib/liblz4-dll.rc.in +1 -1
  52. data/contrib/lz4/lib/liblz4.pc.in +3 -3
  53. data/contrib/lz4/lib/lz4.c +608 -274
  54. data/contrib/lz4/lib/lz4.h +212 -102
  55. data/contrib/lz4/lib/lz4file.c +341 -0
  56. data/contrib/lz4/lib/lz4file.h +93 -0
  57. data/contrib/lz4/lib/lz4frame.c +545 -308
  58. data/contrib/lz4/lib/lz4frame.h +252 -124
  59. data/contrib/lz4/lib/lz4frame_static.h +1 -1
  60. data/contrib/lz4/lib/lz4hc.c +1038 -461
  61. data/contrib/lz4/lib/lz4hc.h +57 -56
  62. data/contrib/lz4/lib/xxhash.c +21 -21
  63. data/contrib/lz4/ossfuzz/Makefile +1 -0
  64. data/contrib/lz4/ossfuzz/decompress_fuzzer.c +18 -2
  65. data/contrib/lz4/ossfuzz/fuzz_helpers.h +4 -3
  66. data/contrib/lz4/ossfuzz/round_trip_frame_uncompressed_fuzzer.c +134 -0
  67. data/contrib/lz4/ossfuzz/round_trip_fuzzer.c +66 -6
  68. data/ext/blockapi.c +19 -19
  69. data/ext/extlz4.h +12 -0
  70. data/ext/frameapi.c +26 -26
  71. data/ext/hashargs.c +7 -1
  72. metadata +47 -30
  73. data/contrib/lz4/build/VS2010/datagen/datagen.vcxproj +0 -169
  74. data/contrib/lz4/build/VS2010/frametest/frametest.vcxproj +0 -176
  75. data/contrib/lz4/build/VS2010/fullbench/fullbench.vcxproj +0 -176
  76. data/contrib/lz4/build/VS2010/fullbench-dll/fullbench-dll.vcxproj +0 -180
  77. data/contrib/lz4/build/VS2010/fuzzer/fuzzer.vcxproj +0 -173
  78. data/contrib/lz4/build/VS2010/liblz4/liblz4.vcxproj +0 -175
  79. data/contrib/lz4/build/VS2010/liblz4-dll/liblz4-dll.vcxproj +0 -179
  80. data/contrib/lz4/build/VS2010/lz4/lz4.vcxproj +0 -189
  81. data/contrib/lz4/build/VS2010/lz4.sln +0 -98
  82. data/contrib/lz4/build/VS2017/liblz4-dll/liblz4-dll.rc +0 -51
  83. data/contrib/lz4/build/VS2017/lz4/lz4.rc +0 -51
  84. data/contrib/lz4/tmp +0 -0
  85. data/contrib/lz4/tmpsparse +0 -0
@@ -1,6 +1,6 @@
1
1
  /*
2
2
  LZ4 - Fast LZ compression algorithm
3
- Copyright (C) 2011-present, Yann Collet.
3
+ Copyright (C) 2011-2023, Yann Collet.
4
4
 
5
5
  BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6
6
 
@@ -37,7 +37,8 @@
37
37
  **************************************/
38
38
  /*
39
39
  * LZ4_HEAPMODE :
40
- * Select how default compression functions will allocate memory for their hash table,
40
+ * Select how stateless compression functions like `LZ4_compress_default()`
41
+ * allocate memory for their hash table,
41
42
  * in memory stack (0:default, fastest), or in memory heap (1:requires malloc()).
42
43
  */
43
44
  #ifndef LZ4_HEAPMODE
@@ -78,7 +79,7 @@
78
79
  ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) \
79
80
  || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
80
81
  # define LZ4_FORCE_MEMORY_ACCESS 2
81
- # elif (defined(__INTEL_COMPILER) && !defined(_WIN32)) || defined(__GNUC__)
82
+ # elif (defined(__INTEL_COMPILER) && !defined(_WIN32)) || defined(__GNUC__) || defined(_MSC_VER)
82
83
  # define LZ4_FORCE_MEMORY_ACCESS 1
83
84
  # endif
84
85
  #endif
@@ -105,15 +106,13 @@
105
106
  # define LZ4_SRC_INCLUDED 1
106
107
  #endif
107
108
 
108
- #ifndef LZ4_STATIC_LINKING_ONLY
109
- #define LZ4_STATIC_LINKING_ONLY
110
- #endif
111
-
112
109
  #ifndef LZ4_DISABLE_DEPRECATE_WARNINGS
113
- #define LZ4_DISABLE_DEPRECATE_WARNINGS /* due to LZ4_decompress_safe_withPrefix64k */
110
+ # define LZ4_DISABLE_DEPRECATE_WARNINGS /* due to LZ4_decompress_safe_withPrefix64k */
114
111
  #endif
115
112
 
116
- #define LZ4_STATIC_LINKING_ONLY /* LZ4_DISTANCE_MAX */
113
+ #ifndef LZ4_STATIC_LINKING_ONLY
114
+ # define LZ4_STATIC_LINKING_ONLY
115
+ #endif
117
116
  #include "lz4.h"
118
117
  /* see also "memory routines" below */
119
118
 
@@ -124,14 +123,18 @@
124
123
  #if defined(_MSC_VER) && (_MSC_VER >= 1400) /* Visual Studio 2005+ */
125
124
  # include <intrin.h> /* only present in VS2005+ */
126
125
  # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
126
+ # pragma warning(disable : 6237) /* disable: C6237: conditional expression is always 0 */
127
+ # pragma warning(disable : 6239) /* disable: C6239: (<non-zero constant> && <expression>) always evaluates to the result of <expression> */
128
+ # pragma warning(disable : 6240) /* disable: C6240: (<expression> && <non-zero constant>) always evaluates to the result of <expression> */
129
+ # pragma warning(disable : 6326) /* disable: C6326: Potential comparison of a constant with another constant */
127
130
  #endif /* _MSC_VER */
128
131
 
129
132
  #ifndef LZ4_FORCE_INLINE
130
- # ifdef _MSC_VER /* Visual Studio */
133
+ # if defined (_MSC_VER) && !defined (__clang__) /* MSVC */
131
134
  # define LZ4_FORCE_INLINE static __forceinline
132
135
  # else
133
136
  # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
134
- # ifdef __GNUC__
137
+ # if defined (__GNUC__) || defined (__clang__)
135
138
  # define LZ4_FORCE_INLINE static inline __attribute__((always_inline))
136
139
  # else
137
140
  # define LZ4_FORCE_INLINE static inline
@@ -187,7 +190,27 @@
187
190
  /*-************************************
188
191
  * Memory routines
189
192
  **************************************/
190
- #ifdef LZ4_USER_MEMORY_FUNCTIONS
193
+
194
+ /*! LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION :
195
+ * Disable relatively high-level LZ4/HC functions that use dynamic memory
196
+ * allocation functions (malloc(), calloc(), free()).
197
+ *
198
+ * Note that this is a compile-time switch. And since it disables
199
+ * public/stable LZ4 v1 API functions, we don't recommend using this
200
+ * symbol to generate a library for distribution.
201
+ *
202
+ * The following public functions are removed when this symbol is defined.
203
+ * - lz4 : LZ4_createStream, LZ4_freeStream,
204
+ * LZ4_createStreamDecode, LZ4_freeStreamDecode, LZ4_create (deprecated)
205
+ * - lz4hc : LZ4_createStreamHC, LZ4_freeStreamHC,
206
+ * LZ4_createHC (deprecated), LZ4_freeHC (deprecated)
207
+ * - lz4frame, lz4file : All LZ4F_* functions
208
+ */
209
+ #if defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION)
210
+ # define ALLOC(s) lz4_error_memory_allocation_is_disabled
211
+ # define ALLOC_AND_ZERO(s) lz4_error_memory_allocation_is_disabled
212
+ # define FREEMEM(p) lz4_error_memory_allocation_is_disabled
213
+ #elif defined(LZ4_USER_MEMORY_FUNCTIONS)
191
214
  /* memory management functions can be customized by user project.
192
215
  * Below functions must exist somewhere in the Project
193
216
  * and be available at link time */
@@ -204,8 +227,13 @@ void LZ4_free(void* p);
204
227
  # define FREEMEM(p) free(p)
205
228
  #endif
206
229
 
207
- #include <string.h> /* memset, memcpy */
208
- #define MEM_INIT(p,v,s) memset((p),(v),(s))
230
+ #if ! LZ4_FREESTANDING
231
+ # include <string.h> /* memset, memcpy */
232
+ #endif
233
+ #if !defined(LZ4_memset)
234
+ # define LZ4_memset(p,v,s) memset((p),(v),(s))
235
+ #endif
236
+ #define MEM_INIT(p,v,s) LZ4_memset((p),(v),(s))
209
237
 
210
238
 
211
239
  /*-************************************
@@ -253,7 +281,7 @@ static const int LZ4_minLength = (MFLIMIT+1);
253
281
  static int g_debuglog_enable = 1;
254
282
  # define DEBUGLOG(l, ...) { \
255
283
  if ((g_debuglog_enable) && (l<=LZ4_DEBUG)) { \
256
- fprintf(stderr, __FILE__ ": "); \
284
+ fprintf(stderr, __FILE__ " %i: ", __LINE__); \
257
285
  fprintf(stderr, __VA_ARGS__); \
258
286
  fprintf(stderr, " \n"); \
259
287
  } }
@@ -316,10 +344,20 @@ typedef enum {
316
344
  * memcpy() as if it were standard compliant, so it can inline it in freestanding
317
345
  * environments. This is needed when decompressing the Linux Kernel, for example.
318
346
  */
319
- #if defined(__GNUC__) && (__GNUC__ >= 4)
320
- #define LZ4_memcpy(dst, src, size) __builtin_memcpy(dst, src, size)
321
- #else
322
- #define LZ4_memcpy(dst, src, size) memcpy(dst, src, size)
347
+ #if !defined(LZ4_memcpy)
348
+ # if defined(__GNUC__) && (__GNUC__ >= 4)
349
+ # define LZ4_memcpy(dst, src, size) __builtin_memcpy(dst, src, size)
350
+ # else
351
+ # define LZ4_memcpy(dst, src, size) memcpy(dst, src, size)
352
+ # endif
353
+ #endif
354
+
355
+ #if !defined(LZ4_memmove)
356
+ # if defined(__GNUC__) && (__GNUC__ >= 4)
357
+ # define LZ4_memmove __builtin_memmove
358
+ # else
359
+ # define LZ4_memmove memmove
360
+ # endif
323
361
  #endif
324
362
 
325
363
  static unsigned LZ4_isLittleEndian(void)
@@ -328,6 +366,11 @@ static unsigned LZ4_isLittleEndian(void)
328
366
  return one.c[0];
329
367
  }
330
368
 
369
+ #if defined(__GNUC__) || defined(__INTEL_COMPILER)
370
+ #define LZ4_PACK( __Declaration__ ) __Declaration__ __attribute__((__packed__))
371
+ #elif defined(_MSC_VER)
372
+ #define LZ4_PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop))
373
+ #endif
331
374
 
332
375
  #if defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==2)
333
376
  /* lie to the compiler about data alignment; use with caution */
@@ -343,14 +386,16 @@ static void LZ4_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; }
343
386
 
344
387
  /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
345
388
  /* currently only defined for gcc and icc */
346
- typedef union { U16 u16; U32 u32; reg_t uArch; } __attribute__((packed)) unalign;
389
+ LZ4_PACK(typedef struct { U16 u16; }) LZ4_unalign16;
390
+ LZ4_PACK(typedef struct { U32 u32; }) LZ4_unalign32;
391
+ LZ4_PACK(typedef struct { reg_t uArch; }) LZ4_unalignST;
347
392
 
348
- static U16 LZ4_read16(const void* ptr) { return ((const unalign*)ptr)->u16; }
349
- static U32 LZ4_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
350
- static reg_t LZ4_read_ARCH(const void* ptr) { return ((const unalign*)ptr)->uArch; }
393
+ static U16 LZ4_read16(const void* ptr) { return ((const LZ4_unalign16*)ptr)->u16; }
394
+ static U32 LZ4_read32(const void* ptr) { return ((const LZ4_unalign32*)ptr)->u32; }
395
+ static reg_t LZ4_read_ARCH(const void* ptr) { return ((const LZ4_unalignST*)ptr)->uArch; }
351
396
 
352
- static void LZ4_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; }
353
- static void LZ4_write32(void* memPtr, U32 value) { ((unalign*)memPtr)->u32 = value; }
397
+ static void LZ4_write16(void* memPtr, U16 value) { ((LZ4_unalign16*)memPtr)->u16 = value; }
398
+ static void LZ4_write32(void* memPtr, U32 value) { ((LZ4_unalign32*)memPtr)->u32 = value; }
354
399
 
355
400
  #else /* safe and portable access using memcpy() */
356
401
 
@@ -388,9 +433,21 @@ static U16 LZ4_readLE16(const void* memPtr)
388
433
  return LZ4_read16(memPtr);
389
434
  } else {
390
435
  const BYTE* p = (const BYTE*)memPtr;
391
- return (U16)((U16)p[0] + (p[1]<<8));
436
+ return (U16)((U16)p[0] | (p[1]<<8));
437
+ }
438
+ }
439
+
440
+ #ifdef LZ4_STATIC_LINKING_ONLY_ENDIANNESS_INDEPENDENT_OUTPUT
441
+ static U32 LZ4_readLE32(const void* memPtr)
442
+ {
443
+ if (LZ4_isLittleEndian()) {
444
+ return LZ4_read32(memPtr);
445
+ } else {
446
+ const BYTE* p = (const BYTE*)memPtr;
447
+ return (U32)p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
392
448
  }
393
449
  }
450
+ #endif
394
451
 
395
452
  static void LZ4_writeLE16(void* memPtr, U16 value)
396
453
  {
@@ -421,10 +478,12 @@ static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3};
421
478
  #ifndef LZ4_FAST_DEC_LOOP
422
479
  # if defined __i386__ || defined _M_IX86 || defined __x86_64__ || defined _M_X64
423
480
  # define LZ4_FAST_DEC_LOOP 1
481
+ # elif defined(__aarch64__) && defined(__APPLE__)
482
+ # define LZ4_FAST_DEC_LOOP 1
424
483
  # elif defined(__aarch64__) && !defined(__clang__)
425
- /* On aarch64, we disable this optimization for clang because on certain
426
- * mobile chipsets, performance is reduced with clang. For information
427
- * refer to https://github.com/lz4/lz4/pull/707 */
484
+ /* On non-Apple aarch64, we disable this optimization for clang because
485
+ * on certain mobile chipsets, performance is reduced with clang. For
486
+ * more information refer to https://github.com/lz4/lz4/pull/707 */
428
487
  # define LZ4_FAST_DEC_LOOP 1
429
488
  # else
430
489
  # define LZ4_FAST_DEC_LOOP 0
@@ -471,7 +530,7 @@ LZ4_wildCopy32(void* dstPtr, const void* srcPtr, void* dstEnd)
471
530
 
472
531
  /* LZ4_memcpy_using_offset() presumes :
473
532
  * - dstEnd >= dstPtr + MINMATCH
474
- * - there is at least 8 bytes available to write after dstEnd */
533
+ * - there is at least 12 bytes available to write after dstEnd */
475
534
  LZ4_FORCE_INLINE void
476
535
  LZ4_memcpy_using_offset(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const size_t offset)
477
536
  {
@@ -486,7 +545,14 @@ LZ4_memcpy_using_offset(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const si
486
545
  case 2:
487
546
  LZ4_memcpy(v, srcPtr, 2);
488
547
  LZ4_memcpy(&v[2], srcPtr, 2);
548
+ #if defined(_MSC_VER) && (_MSC_VER <= 1937) /* MSVC 2022 ver 17.7 or earlier */
549
+ # pragma warning(push)
550
+ # pragma warning(disable : 6385) /* warning C6385: Reading invalid data from 'v'. */
551
+ #endif
489
552
  LZ4_memcpy(&v[4], v, 4);
553
+ #if defined(_MSC_VER) && (_MSC_VER <= 1937) /* MSVC 2022 ver 17.7 or earlier */
554
+ # pragma warning(pop)
555
+ #endif
490
556
  break;
491
557
  case 4:
492
558
  LZ4_memcpy(v, srcPtr, 4);
@@ -515,9 +581,20 @@ static unsigned LZ4_NbCommonBytes (reg_t val)
515
581
  assert(val != 0);
516
582
  if (LZ4_isLittleEndian()) {
517
583
  if (sizeof(val) == 8) {
518
- # if defined(_MSC_VER) && (_MSC_VER >= 1800) && defined(_M_AMD64) && !defined(LZ4_FORCE_SW_BITCOUNT)
584
+ # if defined(_MSC_VER) && (_MSC_VER >= 1800) && (defined(_M_AMD64) && !defined(_M_ARM64EC)) && !defined(LZ4_FORCE_SW_BITCOUNT)
585
+ /*-*************************************************************************************************
586
+ * ARM64EC is a Microsoft-designed ARM64 ABI compatible with AMD64 applications on ARM64 Windows 11.
587
+ * The ARM64EC ABI does not support AVX/AVX2/AVX512 instructions, nor their relevant intrinsics
588
+ * including _tzcnt_u64. Therefore, we need to neuter the _tzcnt_u64 code path for ARM64EC.
589
+ ****************************************************************************************************/
590
+ # if defined(__clang__) && (__clang_major__ < 10)
591
+ /* Avoid undefined clang-cl intrinsics issue.
592
+ * See https://github.com/lz4/lz4/pull/1017 for details. */
593
+ return (unsigned)__builtin_ia32_tzcnt_u64(val) >> 3;
594
+ # else
519
595
  /* x64 CPUS without BMI support interpret `TZCNT` as `REP BSF` */
520
596
  return (unsigned)_tzcnt_u64(val) >> 3;
597
+ # endif
521
598
  # elif defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
522
599
  unsigned long r = 0;
523
600
  _BitScanForward64(&r, (U64)val);
@@ -652,10 +729,10 @@ typedef enum { clearedTable = 0, byPtr, byU32, byU16 } tableType_t;
652
729
  * - usingExtDict : Like withPrefix64k, but the preceding content is somewhere
653
730
  * else in memory, starting at ctx->dictionary with length
654
731
  * ctx->dictSize.
655
- * - usingDictCtx : Like usingExtDict, but everything concerning the preceding
656
- * content is in a separate context, pointed to by
657
- * ctx->dictCtx. ctx->dictionary, ctx->dictSize, and table
658
- * entries in the current context that refer to positions
732
+ * - usingDictCtx : Everything concerning the preceding content is
733
+ * in a separate context, pointed to by ctx->dictCtx.
734
+ * ctx->dictionary, ctx->dictSize, and table entries
735
+ * in the current context that refer to positions
659
736
  * preceding the beginning of the current compression are
660
737
  * ignored. Instead, ctx->dictCtx->dictionary and ctx->dictCtx
661
738
  * ->dictSize describe the location and size of the preceding
@@ -672,12 +749,12 @@ typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive;
672
749
  int LZ4_versionNumber (void) { return LZ4_VERSION_NUMBER; }
673
750
  const char* LZ4_versionString(void) { return LZ4_VERSION_STRING; }
674
751
  int LZ4_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); }
675
- int LZ4_sizeofState(void) { return LZ4_STREAMSIZE; }
752
+ int LZ4_sizeofState(void) { return sizeof(LZ4_stream_t); }
676
753
 
677
754
 
678
- /*-************************************
679
- * Internal Definitions used in Tests
680
- **************************************/
755
+ /*-****************************************
756
+ * Internal Definitions, used only in Tests
757
+ *******************************************/
681
758
  #if defined (__cplusplus)
682
759
  extern "C" {
683
760
  #endif
@@ -687,7 +764,9 @@ int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char*
687
764
  int LZ4_decompress_safe_forceExtDict(const char* source, char* dest,
688
765
  int compressedSize, int maxOutputSize,
689
766
  const void* dictStart, size_t dictSize);
690
-
767
+ int LZ4_decompress_safe_partial_forceExtDict(const char* source, char* dest,
768
+ int compressedSize, int targetOutputSize, int dstCapacity,
769
+ const void* dictStart, size_t dictSize);
691
770
  #if defined (__cplusplus)
692
771
  }
693
772
  #endif
@@ -718,7 +797,12 @@ LZ4_FORCE_INLINE U32 LZ4_hash5(U64 sequence, tableType_t const tableType)
718
797
  LZ4_FORCE_INLINE U32 LZ4_hashPosition(const void* const p, tableType_t const tableType)
719
798
  {
720
799
  if ((sizeof(reg_t)==8) && (tableType != byU16)) return LZ4_hash5(LZ4_read_ARCH(p), tableType);
800
+
801
+ #ifdef LZ4_STATIC_LINKING_ONLY_ENDIANNESS_INDEPENDENT_OUTPUT
802
+ return LZ4_hash4(LZ4_readLE32(p), tableType);
803
+ #else
721
804
  return LZ4_hash4(LZ4_read32(p), tableType);
805
+ #endif
722
806
  }
723
807
 
724
808
  LZ4_FORCE_INLINE void LZ4_clearHash(U32 h, void* tableBase, tableType_t const tableType)
@@ -745,23 +829,19 @@ LZ4_FORCE_INLINE void LZ4_putIndexOnHash(U32 idx, U32 h, void* tableBase, tableT
745
829
  }
746
830
  }
747
831
 
832
+ /* LZ4_putPosition*() : only used in byPtr mode */
748
833
  LZ4_FORCE_INLINE void LZ4_putPositionOnHash(const BYTE* p, U32 h,
749
- void* tableBase, tableType_t const tableType,
750
- const BYTE* srcBase)
834
+ void* tableBase, tableType_t const tableType)
751
835
  {
752
- switch (tableType)
753
- {
754
- case clearedTable: { /* illegal! */ assert(0); return; }
755
- case byPtr: { const BYTE** hashTable = (const BYTE**)tableBase; hashTable[h] = p; return; }
756
- case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = (U32)(p-srcBase); return; }
757
- case byU16: { U16* hashTable = (U16*) tableBase; hashTable[h] = (U16)(p-srcBase); return; }
758
- }
836
+ const BYTE** const hashTable = (const BYTE**)tableBase;
837
+ assert(tableType == byPtr); (void)tableType;
838
+ hashTable[h] = p;
759
839
  }
760
840
 
761
- LZ4_FORCE_INLINE void LZ4_putPosition(const BYTE* p, void* tableBase, tableType_t tableType, const BYTE* srcBase)
841
+ LZ4_FORCE_INLINE void LZ4_putPosition(const BYTE* p, void* tableBase, tableType_t tableType)
762
842
  {
763
843
  U32 const h = LZ4_hashPosition(p, tableType);
764
- LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase);
844
+ LZ4_putPositionOnHash(p, h, tableBase, tableType);
765
845
  }
766
846
 
767
847
  /* LZ4_getIndexOnHash() :
@@ -786,20 +866,18 @@ LZ4_FORCE_INLINE U32 LZ4_getIndexOnHash(U32 h, const void* tableBase, tableType_
786
866
  assert(0); return 0; /* forbidden case */
787
867
  }
788
868
 
789
- static const BYTE* LZ4_getPositionOnHash(U32 h, const void* tableBase, tableType_t tableType, const BYTE* srcBase)
869
+ static const BYTE* LZ4_getPositionOnHash(U32 h, const void* tableBase, tableType_t tableType)
790
870
  {
791
- if (tableType == byPtr) { const BYTE* const* hashTable = (const BYTE* const*) tableBase; return hashTable[h]; }
792
- if (tableType == byU32) { const U32* const hashTable = (const U32*) tableBase; return hashTable[h] + srcBase; }
793
- { const U16* const hashTable = (const U16*) tableBase; return hashTable[h] + srcBase; } /* default, to ensure a return */
871
+ assert(tableType == byPtr); (void)tableType;
872
+ { const BYTE* const* hashTable = (const BYTE* const*) tableBase; return hashTable[h]; }
794
873
  }
795
874
 
796
875
  LZ4_FORCE_INLINE const BYTE*
797
876
  LZ4_getPosition(const BYTE* p,
798
- const void* tableBase, tableType_t tableType,
799
- const BYTE* srcBase)
877
+ const void* tableBase, tableType_t tableType)
800
878
  {
801
879
  U32 const h = LZ4_hashPosition(p, tableType);
802
- return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase);
880
+ return LZ4_getPositionOnHash(h, tableBase, tableType);
803
881
  }
804
882
 
805
883
  LZ4_FORCE_INLINE void
@@ -827,9 +905,10 @@ LZ4_prepareTable(LZ4_stream_t_internal* const cctx,
827
905
  }
828
906
  }
829
907
 
830
- /* Adding a gap, so all previous entries are > LZ4_DISTANCE_MAX back, is faster
831
- * than compressing without a gap. However, compressing with
832
- * currentOffset == 0 is faster still, so we preserve that case.
908
+ /* Adding a gap, so all previous entries are > LZ4_DISTANCE_MAX back,
909
+ * is faster than compressing without a gap.
910
+ * However, compressing with currentOffset == 0 is faster still,
911
+ * so we preserve that case.
833
912
  */
834
913
  if (cctx->currentOffset != 0 && tableType == byU32) {
835
914
  DEBUGLOG(5, "LZ4_prepareTable: adding 64KB to currentOffset");
@@ -842,9 +921,9 @@ LZ4_prepareTable(LZ4_stream_t_internal* const cctx,
842
921
  cctx->dictSize = 0;
843
922
  }
844
923
 
845
- /** LZ4_compress_generic() :
924
+ /** LZ4_compress_generic_validated() :
846
925
  * inlined, to ensure branches are decided at compilation time.
847
- * Presumed already validated at this stage:
926
+ * The following conditions are presumed already validated:
848
927
  * - source != NULL
849
928
  * - inputSize > 0
850
929
  */
@@ -853,7 +932,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic_validated(
853
932
  const char* const source,
854
933
  char* const dest,
855
934
  const int inputSize,
856
- int *inputConsumed, /* only written when outputDirective == fillOutput */
935
+ int* inputConsumed, /* only written when outputDirective == fillOutput */
857
936
  const int maxOutputSize,
858
937
  const limitedOutput_directive outputDirective,
859
938
  const tableType_t tableType,
@@ -862,10 +941,10 @@ LZ4_FORCE_INLINE int LZ4_compress_generic_validated(
862
941
  const int acceleration)
863
942
  {
864
943
  int result;
865
- const BYTE* ip = (const BYTE*) source;
944
+ const BYTE* ip = (const BYTE*)source;
866
945
 
867
946
  U32 const startIndex = cctx->currentOffset;
868
- const BYTE* base = (const BYTE*) source - startIndex;
947
+ const BYTE* base = (const BYTE*)source - startIndex;
869
948
  const BYTE* lowLimit;
870
949
 
871
950
  const LZ4_stream_t_internal* dictCtx = (const LZ4_stream_t_internal*) cctx->dictCtx;
@@ -873,7 +952,8 @@ LZ4_FORCE_INLINE int LZ4_compress_generic_validated(
873
952
  dictDirective == usingDictCtx ? dictCtx->dictionary : cctx->dictionary;
874
953
  const U32 dictSize =
875
954
  dictDirective == usingDictCtx ? dictCtx->dictSize : cctx->dictSize;
876
- const U32 dictDelta = (dictDirective == usingDictCtx) ? startIndex - dictCtx->currentOffset : 0; /* make indexes in dictCtx comparable with index in current context */
955
+ const U32 dictDelta =
956
+ (dictDirective == usingDictCtx) ? startIndex - dictCtx->currentOffset : 0; /* make indexes in dictCtx comparable with indexes in current context */
877
957
 
878
958
  int const maybe_extMem = (dictDirective == usingExtDict) || (dictDirective == usingDictCtx);
879
959
  U32 const prefixIdxLimit = startIndex - dictSize; /* used when dictDirective == dictSmall */
@@ -885,7 +965,8 @@ LZ4_FORCE_INLINE int LZ4_compress_generic_validated(
885
965
 
886
966
  /* the dictCtx currentOffset is indexed on the start of the dictionary,
887
967
  * while a dictionary in the current context precedes the currentOffset */
888
- const BYTE* dictBase = !dictionary ? NULL : (dictDirective == usingDictCtx) ?
968
+ const BYTE* dictBase = (dictionary == NULL) ? NULL :
969
+ (dictDirective == usingDictCtx) ?
889
970
  dictionary + dictSize - dictCtx->currentOffset :
890
971
  dictionary + dictSize - startIndex;
891
972
 
@@ -897,11 +978,11 @@ LZ4_FORCE_INLINE int LZ4_compress_generic_validated(
897
978
 
898
979
  DEBUGLOG(5, "LZ4_compress_generic_validated: srcSize=%i, tableType=%u", inputSize, tableType);
899
980
  assert(ip != NULL);
981
+ if (tableType == byU16) assert(inputSize<LZ4_64Klimit); /* Size too large (not within 64K limit) */
982
+ if (tableType == byPtr) assert(dictDirective==noDict); /* only supported use case with byPtr */
900
983
  /* If init conditions are not met, we don't have to mark stream
901
984
  * as having dirty context, since no action was taken yet */
902
985
  if (outputDirective == fillOutput && maxOutputSize < 1) { return 0; } /* Impossible to store anything */
903
- if ((tableType == byU16) && (inputSize>=LZ4_64Klimit)) { return 0; } /* Size too large (not within 64K limit) */
904
- if (tableType==byPtr) assert(dictDirective==noDict); /* only supported use case with byPtr */
905
986
  assert(acceleration >= 1);
906
987
 
907
988
  lowLimit = (const BYTE*)source - (dictDirective == withPrefix64k ? dictSize : 0);
@@ -921,7 +1002,12 @@ LZ4_FORCE_INLINE int LZ4_compress_generic_validated(
921
1002
  if (inputSize<LZ4_minLength) goto _last_literals; /* Input too small, no compression (all literals) */
922
1003
 
923
1004
  /* First Byte */
924
- LZ4_putPosition(ip, cctx->hashTable, tableType, base);
1005
+ { U32 const h = LZ4_hashPosition(ip, tableType);
1006
+ if (tableType == byPtr) {
1007
+ LZ4_putPositionOnHash(ip, h, cctx->hashTable, byPtr);
1008
+ } else {
1009
+ LZ4_putIndexOnHash(startIndex, h, cctx->hashTable, tableType);
1010
+ } }
925
1011
  ip++; forwardH = LZ4_hashPosition(ip, tableType);
926
1012
 
927
1013
  /* Main Loop */
@@ -944,9 +1030,9 @@ LZ4_FORCE_INLINE int LZ4_compress_generic_validated(
944
1030
  if (unlikely(forwardIp > mflimitPlusOne)) goto _last_literals;
945
1031
  assert(ip < mflimitPlusOne);
946
1032
 
947
- match = LZ4_getPositionOnHash(h, cctx->hashTable, tableType, base);
1033
+ match = LZ4_getPositionOnHash(h, cctx->hashTable, tableType);
948
1034
  forwardH = LZ4_hashPosition(forwardIp, tableType);
949
- LZ4_putPositionOnHash(ip, h, cctx->hashTable, tableType, base);
1035
+ LZ4_putPositionOnHash(ip, h, cctx->hashTable, tableType);
950
1036
 
951
1037
  } while ( (match+LZ4_DISTANCE_MAX < ip)
952
1038
  || (LZ4_read32(match) != LZ4_read32(ip)) );
@@ -981,10 +1067,11 @@ LZ4_FORCE_INLINE int LZ4_compress_generic_validated(
981
1067
  match = base + matchIndex;
982
1068
  lowLimit = (const BYTE*)source;
983
1069
  }
984
- } else if (dictDirective==usingExtDict) {
1070
+ } else if (dictDirective == usingExtDict) {
985
1071
  if (matchIndex < startIndex) {
986
1072
  DEBUGLOG(7, "extDict candidate: matchIndex=%5u < startIndex=%5u", matchIndex, startIndex);
987
1073
  assert(startIndex - matchIndex >= MINMATCH);
1074
+ assert(dictBase);
988
1075
  match = dictBase + matchIndex;
989
1076
  lowLimit = dictionary;
990
1077
  } else {
@@ -1016,7 +1103,10 @@ LZ4_FORCE_INLINE int LZ4_compress_generic_validated(
1016
1103
 
1017
1104
  /* Catch up */
1018
1105
  filledIp = ip;
1019
- while (((ip>anchor) & (match > lowLimit)) && (unlikely(ip[-1]==match[-1]))) { ip--; match--; }
1106
+ assert(ip > anchor); /* this is always true as ip has been advanced before entering the main loop */
1107
+ if ((match > lowLimit) && unlikely(ip[-1] == match[-1])) {
1108
+ do { ip--; match--; } while (((ip > anchor) & (match > lowLimit)) && (unlikely(ip[-1] == match[-1])));
1109
+ }
1020
1110
 
1021
1111
  /* Encode Literals */
1022
1112
  { unsigned const litLength = (unsigned)(ip - anchor);
@@ -1031,7 +1121,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic_validated(
1031
1121
  goto _last_literals;
1032
1122
  }
1033
1123
  if (litLength >= RUN_MASK) {
1034
- int len = (int)(litLength - RUN_MASK);
1124
+ unsigned len = litLength - RUN_MASK;
1035
1125
  *token = (RUN_MASK<<ML_BITS);
1036
1126
  for(; len >= 255 ; len-=255) *op++ = 255;
1037
1127
  *op++ = (BYTE)len;
@@ -1048,7 +1138,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic_validated(
1048
1138
  _next_match:
1049
1139
  /* at this stage, the following variables must be correctly set :
1050
1140
  * - ip : at start of LZ operation
1051
- * - match : at start of previous pattern occurence; can be within current prefix, or within extDict
1141
+ * - match : at start of previous pattern occurrence; can be within current prefix, or within extDict
1052
1142
  * - offset : if maybe_ext_memSegment==1 (constant)
1053
1143
  * - lowLimit : must be == dictionary to mean "match is within extDict"; must be == source otherwise
1054
1144
  * - token and *token : position to write 4-bits for match length; higher 4-bits for literal length supposed already written
@@ -1143,13 +1233,19 @@ _next_match:
1143
1233
  if (ip >= mflimitPlusOne) break;
1144
1234
 
1145
1235
  /* Fill table */
1146
- LZ4_putPosition(ip-2, cctx->hashTable, tableType, base);
1236
+ { U32 const h = LZ4_hashPosition(ip-2, tableType);
1237
+ if (tableType == byPtr) {
1238
+ LZ4_putPositionOnHash(ip-2, h, cctx->hashTable, byPtr);
1239
+ } else {
1240
+ U32 const idx = (U32)((ip-2) - base);
1241
+ LZ4_putIndexOnHash(idx, h, cctx->hashTable, tableType);
1242
+ } }
1147
1243
 
1148
1244
  /* Test next position */
1149
1245
  if (tableType == byPtr) {
1150
1246
 
1151
- match = LZ4_getPosition(ip, cctx->hashTable, tableType, base);
1152
- LZ4_putPosition(ip, cctx->hashTable, tableType, base);
1247
+ match = LZ4_getPosition(ip, cctx->hashTable, tableType);
1248
+ LZ4_putPosition(ip, cctx->hashTable, tableType);
1153
1249
  if ( (match+LZ4_DISTANCE_MAX >= ip)
1154
1250
  && (LZ4_read32(match) == LZ4_read32(ip)) )
1155
1251
  { token=op++; *token=0; goto _next_match; }
@@ -1163,6 +1259,7 @@ _next_match:
1163
1259
  if (dictDirective == usingDictCtx) {
1164
1260
  if (matchIndex < startIndex) {
1165
1261
  /* there was no match, try the dictionary */
1262
+ assert(tableType == byU32);
1166
1263
  matchIndex = LZ4_getIndexOnHash(h, dictCtx->hashTable, byU32);
1167
1264
  match = dictBase + matchIndex;
1168
1265
  lowLimit = dictionary; /* required for match length counter */
@@ -1173,6 +1270,7 @@ _next_match:
1173
1270
  }
1174
1271
  } else if (dictDirective==usingExtDict) {
1175
1272
  if (matchIndex < startIndex) {
1273
+ assert(dictBase);
1176
1274
  match = dictBase + matchIndex;
1177
1275
  lowLimit = dictionary; /* required for match length counter */
1178
1276
  } else {
@@ -1315,9 +1413,10 @@ int LZ4_compress_fast_extState(void* state, const char* source, char* dest, int
1315
1413
  */
1316
1414
  int LZ4_compress_fast_extState_fastReset(void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration)
1317
1415
  {
1318
- LZ4_stream_t_internal* ctx = &((LZ4_stream_t*)state)->internal_donotuse;
1416
+ LZ4_stream_t_internal* const ctx = &((LZ4_stream_t*)state)->internal_donotuse;
1319
1417
  if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT;
1320
1418
  if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX;
1419
+ assert(ctx != NULL);
1321
1420
 
1322
1421
  if (dstCapacity >= LZ4_compressBound(srcSize)) {
1323
1422
  if (srcSize < LZ4_64Klimit) {
@@ -1351,17 +1450,17 @@ int LZ4_compress_fast_extState_fastReset(void* state, const char* src, char* dst
1351
1450
  }
1352
1451
 
1353
1452
 
1354
- int LZ4_compress_fast(const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration)
1453
+ int LZ4_compress_fast(const char* src, char* dest, int srcSize, int dstCapacity, int acceleration)
1355
1454
  {
1356
1455
  int result;
1357
1456
  #if (LZ4_HEAPMODE)
1358
- LZ4_stream_t* ctxPtr = ALLOC(sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */
1457
+ LZ4_stream_t* const ctxPtr = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */
1359
1458
  if (ctxPtr == NULL) return 0;
1360
1459
  #else
1361
1460
  LZ4_stream_t ctx;
1362
1461
  LZ4_stream_t* const ctxPtr = &ctx;
1363
1462
  #endif
1364
- result = LZ4_compress_fast_extState(ctxPtr, source, dest, inputSize, maxOutputSize, acceleration);
1463
+ result = LZ4_compress_fast_extState(ctxPtr, src, dest, srcSize, dstCapacity, acceleration);
1365
1464
 
1366
1465
  #if (LZ4_HEAPMODE)
1367
1466
  FREEMEM(ctxPtr);
@@ -1370,43 +1469,51 @@ int LZ4_compress_fast(const char* source, char* dest, int inputSize, int maxOutp
1370
1469
  }
1371
1470
 
1372
1471
 
1373
- int LZ4_compress_default(const char* src, char* dst, int srcSize, int maxOutputSize)
1472
+ int LZ4_compress_default(const char* src, char* dst, int srcSize, int dstCapacity)
1374
1473
  {
1375
- return LZ4_compress_fast(src, dst, srcSize, maxOutputSize, 1);
1474
+ return LZ4_compress_fast(src, dst, srcSize, dstCapacity, 1);
1376
1475
  }
1377
1476
 
1378
1477
 
1379
1478
  /* Note!: This function leaves the stream in an unclean/broken state!
1380
1479
  * It is not safe to subsequently use the same state with a _fastReset() or
1381
1480
  * _continue() call without resetting it. */
1382
- static int LZ4_compress_destSize_extState (LZ4_stream_t* state, const char* src, char* dst, int* srcSizePtr, int targetDstSize)
1481
+ static int LZ4_compress_destSize_extState_internal(LZ4_stream_t* state, const char* src, char* dst, int* srcSizePtr, int targetDstSize, int acceleration)
1383
1482
  {
1384
1483
  void* const s = LZ4_initStream(state, sizeof (*state));
1385
1484
  assert(s != NULL); (void)s;
1386
1485
 
1387
1486
  if (targetDstSize >= LZ4_compressBound(*srcSizePtr)) { /* compression success is guaranteed */
1388
- return LZ4_compress_fast_extState(state, src, dst, *srcSizePtr, targetDstSize, 1);
1487
+ return LZ4_compress_fast_extState(state, src, dst, *srcSizePtr, targetDstSize, acceleration);
1389
1488
  } else {
1390
1489
  if (*srcSizePtr < LZ4_64Klimit) {
1391
- return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, byU16, noDict, noDictIssue, 1);
1490
+ return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, byU16, noDict, noDictIssue, acceleration);
1392
1491
  } else {
1393
1492
  tableType_t const addrMode = ((sizeof(void*)==4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32;
1394
- return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, addrMode, noDict, noDictIssue, 1);
1493
+ return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, addrMode, noDict, noDictIssue, acceleration);
1395
1494
  } }
1396
1495
  }
1397
1496
 
1497
+ int LZ4_compress_destSize_extState(void* state, const char* src, char* dst, int* srcSizePtr, int targetDstSize, int acceleration)
1498
+ {
1499
+ int const r = LZ4_compress_destSize_extState_internal((LZ4_stream_t*)state, src, dst, srcSizePtr, targetDstSize, acceleration);
1500
+ /* clean the state on exit */
1501
+ LZ4_initStream(state, sizeof (LZ4_stream_t));
1502
+ return r;
1503
+ }
1504
+
1398
1505
 
1399
1506
  int LZ4_compress_destSize(const char* src, char* dst, int* srcSizePtr, int targetDstSize)
1400
1507
  {
1401
1508
  #if (LZ4_HEAPMODE)
1402
- LZ4_stream_t* ctx = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */
1509
+ LZ4_stream_t* const ctx = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */
1403
1510
  if (ctx == NULL) return 0;
1404
1511
  #else
1405
1512
  LZ4_stream_t ctxBody;
1406
- LZ4_stream_t* ctx = &ctxBody;
1513
+ LZ4_stream_t* const ctx = &ctxBody;
1407
1514
  #endif
1408
1515
 
1409
- int result = LZ4_compress_destSize_extState(ctx, src, dst, srcSizePtr, targetDstSize);
1516
+ int result = LZ4_compress_destSize_extState_internal(ctx, src, dst, srcSizePtr, targetDstSize, 1);
1410
1517
 
1411
1518
  #if (LZ4_HEAPMODE)
1412
1519
  FREEMEM(ctx);
@@ -1420,15 +1527,17 @@ int LZ4_compress_destSize(const char* src, char* dst, int* srcSizePtr, int targe
1420
1527
  * Streaming functions
1421
1528
  ********************************/
1422
1529
 
1530
+ #if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION)
1423
1531
  LZ4_stream_t* LZ4_createStream(void)
1424
1532
  {
1425
1533
  LZ4_stream_t* const lz4s = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t));
1426
- LZ4_STATIC_ASSERT(LZ4_STREAMSIZE >= sizeof(LZ4_stream_t_internal)); /* A compilation error here means LZ4_STREAMSIZE is not large enough */
1534
+ LZ4_STATIC_ASSERT(sizeof(LZ4_stream_t) >= sizeof(LZ4_stream_t_internal));
1427
1535
  DEBUGLOG(4, "LZ4_createStream %p", lz4s);
1428
1536
  if (lz4s == NULL) return NULL;
1429
1537
  LZ4_initStream(lz4s, sizeof(*lz4s));
1430
1538
  return lz4s;
1431
1539
  }
1540
+ #endif
1432
1541
 
1433
1542
  static size_t LZ4_stream_t_alignment(void)
1434
1543
  {
@@ -1462,6 +1571,7 @@ void LZ4_resetStream_fast(LZ4_stream_t* ctx) {
1462
1571
  LZ4_prepareTable(&(ctx->internal_donotuse), 0, byU32);
1463
1572
  }
1464
1573
 
1574
+ #if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION)
1465
1575
  int LZ4_freeStream (LZ4_stream_t* LZ4_stream)
1466
1576
  {
1467
1577
  if (!LZ4_stream) return 0; /* support free on NULL */
@@ -1469,16 +1579,20 @@ int LZ4_freeStream (LZ4_stream_t* LZ4_stream)
1469
1579
  FREEMEM(LZ4_stream);
1470
1580
  return (0);
1471
1581
  }
1582
+ #endif
1472
1583
 
1473
1584
 
1585
+ typedef enum { _ld_fast, _ld_slow } LoadDict_mode_e;
1474
1586
  #define HASH_UNIT sizeof(reg_t)
1475
- int LZ4_loadDict (LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize)
1587
+ int LZ4_loadDict_internal(LZ4_stream_t* LZ4_dict,
1588
+ const char* dictionary, int dictSize,
1589
+ LoadDict_mode_e _ld)
1476
1590
  {
1477
- LZ4_stream_t_internal* dict = &LZ4_dict->internal_donotuse;
1591
+ LZ4_stream_t_internal* const dict = &LZ4_dict->internal_donotuse;
1478
1592
  const tableType_t tableType = byU32;
1479
1593
  const BYTE* p = (const BYTE*)dictionary;
1480
1594
  const BYTE* const dictEnd = p + dictSize;
1481
- const BYTE* base;
1595
+ U32 idx32;
1482
1596
 
1483
1597
  DEBUGLOG(4, "LZ4_loadDict (%i bytes from %p into %p)", dictSize, dictionary, LZ4_dict);
1484
1598
 
@@ -1501,21 +1615,49 @@ int LZ4_loadDict (LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize)
1501
1615
  }
1502
1616
 
1503
1617
  if ((dictEnd - p) > 64 KB) p = dictEnd - 64 KB;
1504
- base = dictEnd - dict->currentOffset;
1505
1618
  dict->dictionary = p;
1506
1619
  dict->dictSize = (U32)(dictEnd - p);
1507
1620
  dict->tableType = (U32)tableType;
1621
+ idx32 = dict->currentOffset - dict->dictSize;
1508
1622
 
1509
1623
  while (p <= dictEnd-HASH_UNIT) {
1510
- LZ4_putPosition(p, dict->hashTable, tableType, base);
1511
- p+=3;
1624
+ U32 const h = LZ4_hashPosition(p, tableType);
1625
+ /* Note: overwriting => favors positions end of dictionary */
1626
+ LZ4_putIndexOnHash(idx32, h, dict->hashTable, tableType);
1627
+ p+=3; idx32+=3;
1628
+ }
1629
+
1630
+ if (_ld == _ld_slow) {
1631
+ /* Fill hash table with additional references, to improve compression capability */
1632
+ p = dict->dictionary;
1633
+ idx32 = dict->currentOffset - dict->dictSize;
1634
+ while (p <= dictEnd-HASH_UNIT) {
1635
+ U32 const h = LZ4_hashPosition(p, tableType);
1636
+ U32 const limit = dict->currentOffset - 64 KB;
1637
+ if (LZ4_getIndexOnHash(h, dict->hashTable, tableType) <= limit) {
1638
+ /* Note: not overwriting => favors positions beginning of dictionary */
1639
+ LZ4_putIndexOnHash(idx32, h, dict->hashTable, tableType);
1640
+ }
1641
+ p++; idx32++;
1642
+ }
1512
1643
  }
1513
1644
 
1514
1645
  return (int)dict->dictSize;
1515
1646
  }
1516
1647
 
1517
- void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dictionaryStream) {
1518
- const LZ4_stream_t_internal* dictCtx = dictionaryStream == NULL ? NULL :
1648
+ int LZ4_loadDict(LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize)
1649
+ {
1650
+ return LZ4_loadDict_internal(LZ4_dict, dictionary, dictSize, _ld_fast);
1651
+ }
1652
+
1653
+ int LZ4_loadDictSlow(LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize)
1654
+ {
1655
+ return LZ4_loadDict_internal(LZ4_dict, dictionary, dictSize, _ld_slow);
1656
+ }
1657
+
1658
+ void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dictionaryStream)
1659
+ {
1660
+ const LZ4_stream_t_internal* dictCtx = (dictionaryStream == NULL) ? NULL :
1519
1661
  &(dictionaryStream->internal_donotuse);
1520
1662
 
1521
1663
  DEBUGLOG(4, "LZ4_attach_dictionary (%p, %p, size %u)",
@@ -1568,36 +1710,40 @@ int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream,
1568
1710
  int acceleration)
1569
1711
  {
1570
1712
  const tableType_t tableType = byU32;
1571
- LZ4_stream_t_internal* streamPtr = &LZ4_stream->internal_donotuse;
1572
- const BYTE* dictEnd = streamPtr->dictionary + streamPtr->dictSize;
1713
+ LZ4_stream_t_internal* const streamPtr = &LZ4_stream->internal_donotuse;
1714
+ const char* dictEnd = streamPtr->dictSize ? (const char*)streamPtr->dictionary + streamPtr->dictSize : NULL;
1573
1715
 
1574
- DEBUGLOG(5, "LZ4_compress_fast_continue (inputSize=%i)", inputSize);
1716
+ DEBUGLOG(5, "LZ4_compress_fast_continue (inputSize=%i, dictSize=%u)", inputSize, streamPtr->dictSize);
1575
1717
 
1576
- LZ4_renormDictT(streamPtr, inputSize); /* avoid index overflow */
1718
+ LZ4_renormDictT(streamPtr, inputSize); /* fix index overflow */
1577
1719
  if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT;
1578
1720
  if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX;
1579
1721
 
1580
1722
  /* invalidate tiny dictionaries */
1581
- if ( (streamPtr->dictSize-1 < 4-1) /* intentional underflow */
1582
- && (dictEnd != (const BYTE*)source) ) {
1723
+ if ( (streamPtr->dictSize < 4) /* tiny dictionary : not enough for a hash */
1724
+ && (dictEnd != source) /* prefix mode */
1725
+ && (inputSize > 0) /* tolerance : don't lose history, in case next invocation would use prefix mode */
1726
+ && (streamPtr->dictCtx == NULL) /* usingDictCtx */
1727
+ ) {
1583
1728
  DEBUGLOG(5, "LZ4_compress_fast_continue: dictSize(%u) at addr:%p is too small", streamPtr->dictSize, streamPtr->dictionary);
1729
+ /* remove dictionary existence from history, to employ faster prefix mode */
1584
1730
  streamPtr->dictSize = 0;
1585
1731
  streamPtr->dictionary = (const BYTE*)source;
1586
- dictEnd = (const BYTE*)source;
1732
+ dictEnd = source;
1587
1733
  }
1588
1734
 
1589
1735
  /* Check overlapping input/dictionary space */
1590
- { const BYTE* sourceEnd = (const BYTE*) source + inputSize;
1591
- if ((sourceEnd > streamPtr->dictionary) && (sourceEnd < dictEnd)) {
1736
+ { const char* const sourceEnd = source + inputSize;
1737
+ if ((sourceEnd > (const char*)streamPtr->dictionary) && (sourceEnd < dictEnd)) {
1592
1738
  streamPtr->dictSize = (U32)(dictEnd - sourceEnd);
1593
1739
  if (streamPtr->dictSize > 64 KB) streamPtr->dictSize = 64 KB;
1594
1740
  if (streamPtr->dictSize < 4) streamPtr->dictSize = 0;
1595
- streamPtr->dictionary = dictEnd - streamPtr->dictSize;
1741
+ streamPtr->dictionary = (const BYTE*)dictEnd - streamPtr->dictSize;
1596
1742
  }
1597
1743
  }
1598
1744
 
1599
1745
  /* prefix mode : source data follows dictionary */
1600
- if (dictEnd == (const BYTE*)source) {
1746
+ if (dictEnd == source) {
1601
1747
  if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset))
1602
1748
  return LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, withPrefix64k, dictSmall, acceleration);
1603
1749
  else
@@ -1623,7 +1769,7 @@ int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream,
1623
1769
  } else {
1624
1770
  result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingDictCtx, noDictIssue, acceleration);
1625
1771
  }
1626
- } else {
1772
+ } else { /* small data <= 4 KB */
1627
1773
  if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) {
1628
1774
  result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, dictSmall, acceleration);
1629
1775
  } else {
@@ -1640,7 +1786,7 @@ int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream,
1640
1786
  /* Hidden debug function, to force-test external dictionary mode */
1641
1787
  int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* dest, int srcSize)
1642
1788
  {
1643
- LZ4_stream_t_internal* streamPtr = &LZ4_dict->internal_donotuse;
1789
+ LZ4_stream_t_internal* const streamPtr = &LZ4_dict->internal_donotuse;
1644
1790
  int result;
1645
1791
 
1646
1792
  LZ4_renormDictT(streamPtr, srcSize);
@@ -1661,21 +1807,25 @@ int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char*
1661
1807
  /*! LZ4_saveDict() :
1662
1808
  * If previously compressed data block is not guaranteed to remain available at its memory location,
1663
1809
  * save it into a safer place (char* safeBuffer).
1664
- * Note : you don't need to call LZ4_loadDict() afterwards,
1665
- * dictionary is immediately usable, you can therefore call LZ4_compress_fast_continue().
1666
- * Return : saved dictionary size in bytes (necessarily <= dictSize), or 0 if error.
1810
+ * Note : no need to call LZ4_loadDict() afterwards, dictionary is immediately usable,
1811
+ * one can therefore call LZ4_compress_fast_continue() right after.
1812
+ * @return : saved dictionary size in bytes (necessarily <= dictSize), or 0 if error.
1667
1813
  */
1668
1814
  int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize)
1669
1815
  {
1670
1816
  LZ4_stream_t_internal* const dict = &LZ4_dict->internal_donotuse;
1671
- const BYTE* const previousDictEnd = dict->dictionary + dict->dictSize;
1817
+
1818
+ DEBUGLOG(5, "LZ4_saveDict : dictSize=%i, safeBuffer=%p", dictSize, safeBuffer);
1672
1819
 
1673
1820
  if ((U32)dictSize > 64 KB) { dictSize = 64 KB; } /* useless to define a dictionary > 64 KB */
1674
1821
  if ((U32)dictSize > dict->dictSize) { dictSize = (int)dict->dictSize; }
1675
1822
 
1676
1823
  if (safeBuffer == NULL) assert(dictSize == 0);
1677
- if (dictSize > 0)
1678
- memmove(safeBuffer, previousDictEnd - dictSize, dictSize);
1824
+ if (dictSize > 0) {
1825
+ const BYTE* const previousDictEnd = dict->dictionary + dict->dictSize;
1826
+ assert(dict->dictionary);
1827
+ LZ4_memmove(safeBuffer, previousDictEnd - dictSize, (size_t)dictSize);
1828
+ }
1679
1829
 
1680
1830
  dict->dictionary = (const BYTE*)safeBuffer;
1681
1831
  dict->dictSize = (U32)dictSize;
@@ -1689,41 +1839,176 @@ int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize)
1689
1839
  * Decompression functions
1690
1840
  ********************************/
1691
1841
 
1692
- typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive;
1693
1842
  typedef enum { decode_full_block = 0, partial_decode = 1 } earlyEnd_directive;
1694
1843
 
1695
1844
  #undef MIN
1696
1845
  #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
1697
1846
 
1847
+
1848
+ /* variant for decompress_unsafe()
1849
+ * does not know end of input
1850
+ * presumes input is well formed
1851
+ * note : will consume at least one byte */
1852
+ static size_t read_long_length_no_check(const BYTE** pp)
1853
+ {
1854
+ size_t b, l = 0;
1855
+ do { b = **pp; (*pp)++; l += b; } while (b==255);
1856
+ DEBUGLOG(6, "read_long_length_no_check: +length=%zu using %zu input bytes", l, l/255 + 1)
1857
+ return l;
1858
+ }
1859
+
1860
+ /* core decoder variant for LZ4_decompress_fast*()
1861
+ * for legacy support only : these entry points are deprecated.
1862
+ * - Presumes input is correctly formed (no defense vs malformed inputs)
1863
+ * - Does not know input size (presume input buffer is "large enough")
1864
+ * - Decompress a full block (only)
1865
+ * @return : nb of bytes read from input.
1866
+ * Note : this variant is not optimized for speed, just for maintenance.
1867
+ * the goal is to remove support of decompress_fast*() variants by v2.0
1868
+ **/
1869
+ LZ4_FORCE_INLINE int
1870
+ LZ4_decompress_unsafe_generic(
1871
+ const BYTE* const istart,
1872
+ BYTE* const ostart,
1873
+ int decompressedSize,
1874
+
1875
+ size_t prefixSize,
1876
+ const BYTE* const dictStart, /* only if dict==usingExtDict */
1877
+ const size_t dictSize /* note: =0 if dictStart==NULL */
1878
+ )
1879
+ {
1880
+ const BYTE* ip = istart;
1881
+ BYTE* op = (BYTE*)ostart;
1882
+ BYTE* const oend = ostart + decompressedSize;
1883
+ const BYTE* const prefixStart = ostart - prefixSize;
1884
+
1885
+ DEBUGLOG(5, "LZ4_decompress_unsafe_generic");
1886
+ if (dictStart == NULL) assert(dictSize == 0);
1887
+
1888
+ while (1) {
1889
+ /* start new sequence */
1890
+ unsigned token = *ip++;
1891
+
1892
+ /* literals */
1893
+ { size_t ll = token >> ML_BITS;
1894
+ if (ll==15) {
1895
+ /* long literal length */
1896
+ ll += read_long_length_no_check(&ip);
1897
+ }
1898
+ if ((size_t)(oend-op) < ll) return -1; /* output buffer overflow */
1899
+ LZ4_memmove(op, ip, ll); /* support in-place decompression */
1900
+ op += ll;
1901
+ ip += ll;
1902
+ if ((size_t)(oend-op) < MFLIMIT) {
1903
+ if (op==oend) break; /* end of block */
1904
+ DEBUGLOG(5, "invalid: literals end at distance %zi from end of block", oend-op);
1905
+ /* incorrect end of block :
1906
+ * last match must start at least MFLIMIT==12 bytes before end of output block */
1907
+ return -1;
1908
+ } }
1909
+
1910
+ /* match */
1911
+ { size_t ml = token & 15;
1912
+ size_t const offset = LZ4_readLE16(ip);
1913
+ ip+=2;
1914
+
1915
+ if (ml==15) {
1916
+ /* long literal length */
1917
+ ml += read_long_length_no_check(&ip);
1918
+ }
1919
+ ml += MINMATCH;
1920
+
1921
+ if ((size_t)(oend-op) < ml) return -1; /* output buffer overflow */
1922
+
1923
+ { const BYTE* match = op - offset;
1924
+
1925
+ /* out of range */
1926
+ if (offset > (size_t)(op - prefixStart) + dictSize) {
1927
+ DEBUGLOG(6, "offset out of range");
1928
+ return -1;
1929
+ }
1930
+
1931
+ /* check special case : extDict */
1932
+ if (offset > (size_t)(op - prefixStart)) {
1933
+ /* extDict scenario */
1934
+ const BYTE* const dictEnd = dictStart + dictSize;
1935
+ const BYTE* extMatch = dictEnd - (offset - (size_t)(op-prefixStart));
1936
+ size_t const extml = (size_t)(dictEnd - extMatch);
1937
+ if (extml > ml) {
1938
+ /* match entirely within extDict */
1939
+ LZ4_memmove(op, extMatch, ml);
1940
+ op += ml;
1941
+ ml = 0;
1942
+ } else {
1943
+ /* match split between extDict & prefix */
1944
+ LZ4_memmove(op, extMatch, extml);
1945
+ op += extml;
1946
+ ml -= extml;
1947
+ }
1948
+ match = prefixStart;
1949
+ }
1950
+
1951
+ /* match copy - slow variant, supporting overlap copy */
1952
+ { size_t u;
1953
+ for (u=0; u<ml; u++) {
1954
+ op[u] = match[u];
1955
+ } } }
1956
+ op += ml;
1957
+ if ((size_t)(oend-op) < LASTLITERALS) {
1958
+ DEBUGLOG(5, "invalid: match ends at distance %zi from end of block", oend-op);
1959
+ /* incorrect end of block :
1960
+ * last match must stop at least LASTLITERALS==5 bytes before end of output block */
1961
+ return -1;
1962
+ }
1963
+ } /* match */
1964
+ } /* main loop */
1965
+ return (int)(ip - istart);
1966
+ }
1967
+
1968
+
1698
1969
  /* Read the variable-length literal or match length.
1699
1970
  *
1700
- * ip - pointer to use as input.
1701
- * lencheck - end ip. Return an error if ip advances >= lencheck.
1702
- * loop_check - check ip >= lencheck in body of loop. Returns loop_error if so.
1703
- * initial_check - check ip >= lencheck before start of loop. Returns initial_error if so.
1704
- * error (output) - error code. Should be set to 0 before call.
1705
- */
1706
- typedef enum { loop_error = -2, initial_error = -1, ok = 0 } variable_length_error;
1707
- LZ4_FORCE_INLINE unsigned
1708
- read_variable_length(const BYTE**ip, const BYTE* lencheck,
1709
- int loop_check, int initial_check,
1710
- variable_length_error* error)
1711
- {
1712
- U32 length = 0;
1713
- U32 s;
1714
- if (initial_check && unlikely((*ip) >= lencheck)) { /* overflow detection */
1715
- *error = initial_error;
1716
- return length;
1971
+ * @ip : input pointer
1972
+ * @ilimit : position after which if length is not decoded, the input is necessarily corrupted.
1973
+ * @initial_check - check ip >= ipmax before start of loop. Returns initial_error if so.
1974
+ * @error (output) - error code. Must be set to 0 before call.
1975
+ **/
1976
+ typedef size_t Rvl_t;
1977
+ static const Rvl_t rvl_error = (Rvl_t)(-1);
1978
+ LZ4_FORCE_INLINE Rvl_t
1979
+ read_variable_length(const BYTE** ip, const BYTE* ilimit,
1980
+ int initial_check)
1981
+ {
1982
+ Rvl_t s, length = 0;
1983
+ assert(ip != NULL);
1984
+ assert(*ip != NULL);
1985
+ assert(ilimit != NULL);
1986
+ if (initial_check && unlikely((*ip) >= ilimit)) { /* read limit reached */
1987
+ return rvl_error;
1717
1988
  }
1989
+ s = **ip;
1990
+ (*ip)++;
1991
+ length += s;
1992
+ if (unlikely((*ip) > ilimit)) { /* read limit reached */
1993
+ return rvl_error;
1994
+ }
1995
+ /* accumulator overflow detection (32-bit mode only) */
1996
+ if ((sizeof(length) < 8) && unlikely(length > ((Rvl_t)(-1)/2)) ) {
1997
+ return rvl_error;
1998
+ }
1999
+ if (likely(s != 255)) return length;
1718
2000
  do {
1719
2001
  s = **ip;
1720
2002
  (*ip)++;
1721
2003
  length += s;
1722
- if (loop_check && unlikely((*ip) >= lencheck)) { /* overflow detection */
1723
- *error = loop_error;
1724
- return length;
2004
+ if (unlikely((*ip) > ilimit)) { /* read limit reached */
2005
+ return rvl_error;
2006
+ }
2007
+ /* accumulator overflow detection (32-bit mode only) */
2008
+ if ((sizeof(length) < 8) && unlikely(length > ((Rvl_t)(-1)/2)) ) {
2009
+ return rvl_error;
1725
2010
  }
1726
- } while (s==255);
2011
+ } while (s == 255);
1727
2012
 
1728
2013
  return length;
1729
2014
  }
@@ -1741,7 +2026,6 @@ LZ4_decompress_generic(
1741
2026
  int srcSize,
1742
2027
  int outputSize, /* If endOnInput==endOnInputSize, this value is `dstCapacity` */
1743
2028
 
1744
- endCondition_directive endOnInput, /* endOnOutputSize, endOnInputSize */
1745
2029
  earlyEnd_directive partialDecoding, /* full, partial */
1746
2030
  dict_directive dict, /* noDict, withPrefix64k, usingExtDict */
1747
2031
  const BYTE* const lowPrefix, /* always <= dst, == dst when no prefix */
@@ -1749,7 +2033,7 @@ LZ4_decompress_generic(
1749
2033
  const size_t dictSize /* note : = 0 if noDict */
1750
2034
  )
1751
2035
  {
1752
- if (src == NULL) { return -1; }
2036
+ if ((src == NULL) || (outputSize < 0)) { return -1; }
1753
2037
 
1754
2038
  { const BYTE* ip = (const BYTE*) src;
1755
2039
  const BYTE* const iend = ip + srcSize;
@@ -1760,13 +2044,12 @@ LZ4_decompress_generic(
1760
2044
 
1761
2045
  const BYTE* const dictEnd = (dictStart == NULL) ? NULL : dictStart + dictSize;
1762
2046
 
1763
- const int safeDecode = (endOnInput==endOnInputSize);
1764
- const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB)));
2047
+ const int checkOffset = (dictSize < (int)(64 KB));
1765
2048
 
1766
2049
 
1767
2050
  /* Set up the "end" pointers for the shortcut. */
1768
- const BYTE* const shortiend = iend - (endOnInput ? 14 : 8) /*maxLL*/ - 2 /*offset*/;
1769
- const BYTE* const shortoend = oend - (endOnInput ? 14 : 8) /*maxLL*/ - 18 /*maxML*/;
2051
+ const BYTE* const shortiend = iend - 14 /*maxLL*/ - 2 /*offset*/;
2052
+ const BYTE* const shortoend = oend - 14 /*maxLL*/ - 18 /*maxML*/;
1770
2053
 
1771
2054
  const BYTE* match;
1772
2055
  size_t offset;
@@ -1778,93 +2061,90 @@ LZ4_decompress_generic(
1778
2061
 
1779
2062
  /* Special cases */
1780
2063
  assert(lowPrefix <= op);
1781
- if ((endOnInput) && (unlikely(outputSize==0))) {
2064
+ if (unlikely(outputSize==0)) {
1782
2065
  /* Empty output buffer */
1783
2066
  if (partialDecoding) return 0;
1784
2067
  return ((srcSize==1) && (*ip==0)) ? 0 : -1;
1785
2068
  }
1786
- if ((!endOnInput) && (unlikely(outputSize==0))) { return (*ip==0 ? 1 : -1); }
1787
- if ((endOnInput) && unlikely(srcSize==0)) { return -1; }
2069
+ if (unlikely(srcSize==0)) { return -1; }
1788
2070
 
1789
- /* Currently the fast loop shows a regression on qualcomm arm chips. */
2071
+ /* LZ4_FAST_DEC_LOOP:
2072
+ * designed for modern OoO performance cpus,
2073
+ * where copying reliably 32-bytes is preferable to an unpredictable branch.
2074
+ * note : fast loop may show a regression for some client arm chips. */
1790
2075
  #if LZ4_FAST_DEC_LOOP
1791
2076
  if ((oend - op) < FASTLOOP_SAFE_DISTANCE) {
1792
- DEBUGLOG(6, "skip fast decode loop");
2077
+ DEBUGLOG(6, "move to safe decode loop");
1793
2078
  goto safe_decode;
1794
2079
  }
1795
2080
 
1796
- /* Fast loop : decode sequences as long as output < iend-FASTLOOP_SAFE_DISTANCE */
2081
+ /* Fast loop : decode sequences as long as output < oend-FASTLOOP_SAFE_DISTANCE */
2082
+ DEBUGLOG(6, "using fast decode loop");
1797
2083
  while (1) {
1798
2084
  /* Main fastloop assertion: We can always wildcopy FASTLOOP_SAFE_DISTANCE */
1799
2085
  assert(oend - op >= FASTLOOP_SAFE_DISTANCE);
1800
- if (endOnInput) { assert(ip < iend); }
2086
+ assert(ip < iend);
1801
2087
  token = *ip++;
1802
2088
  length = token >> ML_BITS; /* literal length */
1803
-
1804
- assert(!endOnInput || ip <= iend); /* ip < iend before the increment */
2089
+ DEBUGLOG(7, "blockPos%6u: litLength token = %u", (unsigned)(op-(BYTE*)dst), (unsigned)length);
1805
2090
 
1806
2091
  /* decode literal length */
1807
2092
  if (length == RUN_MASK) {
1808
- variable_length_error error = ok;
1809
- length += read_variable_length(&ip, iend-RUN_MASK, (int)endOnInput, (int)endOnInput, &error);
1810
- if (error == initial_error) { goto _output_error; }
1811
- if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */
1812
- if ((safeDecode) && unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */
2093
+ size_t const addl = read_variable_length(&ip, iend-RUN_MASK, 1);
2094
+ if (addl == rvl_error) {
2095
+ DEBUGLOG(6, "error reading long literal length");
2096
+ goto _output_error;
2097
+ }
2098
+ length += addl;
2099
+ if (unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */
2100
+ if (unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */
1813
2101
 
1814
2102
  /* copy literals */
1815
- cpy = op+length;
1816
2103
  LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH);
1817
- if (endOnInput) { /* LZ4_decompress_safe() */
1818
- if ((cpy>oend-32) || (ip+length>iend-32)) { goto safe_literal_copy; }
1819
- LZ4_wildCopy32(op, ip, cpy);
1820
- } else { /* LZ4_decompress_fast() */
1821
- if (cpy>oend-8) { goto safe_literal_copy; }
1822
- LZ4_wildCopy8(op, ip, cpy); /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time :
1823
- * it doesn't know input length, and only relies on end-of-block properties */
1824
- }
1825
- ip += length; op = cpy;
2104
+ if ((op+length>oend-32) || (ip+length>iend-32)) { goto safe_literal_copy; }
2105
+ LZ4_wildCopy32(op, ip, op+length);
2106
+ ip += length; op += length;
2107
+ } else if (ip <= iend-(16 + 1/*max lit + offset + nextToken*/)) {
2108
+ /* We don't need to check oend, since we check it once for each loop below */
2109
+ DEBUGLOG(7, "copy %u bytes in a 16-bytes stripe", (unsigned)length);
2110
+ /* Literals can only be <= 14, but hope compilers optimize better when copy by a register size */
2111
+ LZ4_memcpy(op, ip, 16);
2112
+ ip += length; op += length;
1826
2113
  } else {
1827
- cpy = op+length;
1828
- if (endOnInput) { /* LZ4_decompress_safe() */
1829
- DEBUGLOG(7, "copy %u bytes in a 16-bytes stripe", (unsigned)length);
1830
- /* We don't need to check oend, since we check it once for each loop below */
1831
- if (ip > iend-(16 + 1/*max lit + offset + nextToken*/)) { goto safe_literal_copy; }
1832
- /* Literals can only be 14, but hope compilers optimize if we copy by a register size */
1833
- LZ4_memcpy(op, ip, 16);
1834
- } else { /* LZ4_decompress_fast() */
1835
- /* LZ4_decompress_fast() cannot copy more than 8 bytes at a time :
1836
- * it doesn't know input length, and relies on end-of-block properties */
1837
- LZ4_memcpy(op, ip, 8);
1838
- if (length > 8) { LZ4_memcpy(op+8, ip+8, 8); }
1839
- }
1840
- ip += length; op = cpy;
2114
+ goto safe_literal_copy;
1841
2115
  }
1842
2116
 
1843
2117
  /* get offset */
1844
2118
  offset = LZ4_readLE16(ip); ip+=2;
2119
+ DEBUGLOG(6, "blockPos%6u: offset = %u", (unsigned)(op-(BYTE*)dst), (unsigned)offset);
1845
2120
  match = op - offset;
1846
- assert(match <= op);
2121
+ assert(match <= op); /* overflow check */
1847
2122
 
1848
2123
  /* get matchlength */
1849
2124
  length = token & ML_MASK;
2125
+ DEBUGLOG(7, " match length token = %u (len==%u)", (unsigned)length, (unsigned)length+MINMATCH);
1850
2126
 
1851
2127
  if (length == ML_MASK) {
1852
- variable_length_error error = ok;
1853
- if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) { goto _output_error; } /* Error : offset outside buffers */
1854
- length += read_variable_length(&ip, iend - LASTLITERALS + 1, (int)endOnInput, 0, &error);
1855
- if (error != ok) { goto _output_error; }
1856
- if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) { goto _output_error; } /* overflow detection */
2128
+ size_t const addl = read_variable_length(&ip, iend - LASTLITERALS + 1, 0);
2129
+ if (addl == rvl_error) {
2130
+ DEBUGLOG(5, "error reading long match length");
2131
+ goto _output_error;
2132
+ }
2133
+ length += addl;
1857
2134
  length += MINMATCH;
2135
+ DEBUGLOG(7, " long match length == %u", (unsigned)length);
2136
+ if (unlikely((uptrval)(op)+length<(uptrval)op)) { goto _output_error; } /* overflow detection */
1858
2137
  if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) {
1859
2138
  goto safe_match_copy;
1860
2139
  }
1861
2140
  } else {
1862
2141
  length += MINMATCH;
1863
2142
  if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) {
2143
+ DEBUGLOG(7, "moving to safe_match_copy (ml==%u)", (unsigned)length);
1864
2144
  goto safe_match_copy;
1865
2145
  }
1866
2146
 
1867
- /* Fastpath check: Avoids a branch in LZ4_wildCopy32 if true */
2147
+ /* Fastpath check: skip LZ4_wildCopy32 when true */
1868
2148
  if ((dict == withPrefix64k) || (match >= lowPrefix)) {
1869
2149
  if (offset >= 8) {
1870
2150
  assert(match >= lowPrefix);
@@ -1878,20 +2158,25 @@ LZ4_decompress_generic(
1878
2158
  continue;
1879
2159
  } } }
1880
2160
 
1881
- if (checkOffset && (unlikely(match + dictSize < lowPrefix))) { goto _output_error; } /* Error : offset outside buffers */
2161
+ if ( checkOffset && (unlikely(match + dictSize < lowPrefix)) ) {
2162
+ DEBUGLOG(5, "Error : pos=%zi, offset=%zi => outside buffers", op-lowPrefix, op-match);
2163
+ goto _output_error;
2164
+ }
1882
2165
  /* match starting within external dictionary */
1883
2166
  if ((dict==usingExtDict) && (match < lowPrefix)) {
2167
+ assert(dictEnd != NULL);
1884
2168
  if (unlikely(op+length > oend-LASTLITERALS)) {
1885
2169
  if (partialDecoding) {
1886
2170
  DEBUGLOG(7, "partialDecoding: dictionary match, close to dstEnd");
1887
2171
  length = MIN(length, (size_t)(oend-op));
1888
2172
  } else {
1889
- goto _output_error; /* end-of-block condition violated */
2173
+ DEBUGLOG(6, "end-of-block condition violated")
2174
+ goto _output_error;
1890
2175
  } }
1891
2176
 
1892
2177
  if (length <= (size_t)(lowPrefix-match)) {
1893
2178
  /* match fits entirely within external dictionary : just copy */
1894
- memmove(op, dictEnd - (lowPrefix-match), length);
2179
+ LZ4_memmove(op, dictEnd - (lowPrefix-match), length);
1895
2180
  op += length;
1896
2181
  } else {
1897
2182
  /* match stretches into both external dictionary and current block */
@@ -1926,11 +2211,12 @@ LZ4_decompress_generic(
1926
2211
  #endif
1927
2212
 
1928
2213
  /* Main Loop : decode remaining sequences where output < FASTLOOP_SAFE_DISTANCE */
2214
+ DEBUGLOG(6, "using safe decode loop");
1929
2215
  while (1) {
2216
+ assert(ip < iend);
1930
2217
  token = *ip++;
1931
2218
  length = token >> ML_BITS; /* literal length */
1932
-
1933
- assert(!endOnInput || ip <= iend); /* ip < iend before the increment */
2219
+ DEBUGLOG(7, "blockPos%6u: litLength token = %u", (unsigned)(op-(BYTE*)dst), (unsigned)length);
1934
2220
 
1935
2221
  /* A two-stage shortcut for the most common case:
1936
2222
  * 1) If the literal length is 0..14, and there is enough space,
@@ -1941,16 +2227,17 @@ LZ4_decompress_generic(
1941
2227
  * those 18 bytes earlier, upon entering the shortcut (in other words,
1942
2228
  * there is a combined check for both stages).
1943
2229
  */
1944
- if ( (endOnInput ? length != RUN_MASK : length <= 8)
2230
+ if ( (length != RUN_MASK)
1945
2231
  /* strictly "less than" on input, to re-enter the loop with at least one byte */
1946
- && likely((endOnInput ? ip < shortiend : 1) & (op <= shortoend)) ) {
2232
+ && likely((ip < shortiend) & (op <= shortoend)) ) {
1947
2233
  /* Copy the literals */
1948
- LZ4_memcpy(op, ip, endOnInput ? 16 : 8);
2234
+ LZ4_memcpy(op, ip, 16);
1949
2235
  op += length; ip += length;
1950
2236
 
1951
2237
  /* The second stage: prepare for match copying, decode full info.
1952
2238
  * If it doesn't work out, the info won't be wasted. */
1953
2239
  length = token & ML_MASK; /* match length */
2240
+ DEBUGLOG(7, "blockPos%6u: matchLength token = %u (len=%u)", (unsigned)(op-(BYTE*)dst), (unsigned)length, (unsigned)length + 4);
1954
2241
  offset = LZ4_readLE16(ip); ip += 2;
1955
2242
  match = op - offset;
1956
2243
  assert(match <= op); /* check overflow */
@@ -1975,22 +2262,21 @@ LZ4_decompress_generic(
1975
2262
 
1976
2263
  /* decode literal length */
1977
2264
  if (length == RUN_MASK) {
1978
- variable_length_error error = ok;
1979
- length += read_variable_length(&ip, iend-RUN_MASK, (int)endOnInput, (int)endOnInput, &error);
1980
- if (error == initial_error) { goto _output_error; }
1981
- if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */
1982
- if ((safeDecode) && unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */
2265
+ size_t const addl = read_variable_length(&ip, iend-RUN_MASK, 1);
2266
+ if (addl == rvl_error) { goto _output_error; }
2267
+ length += addl;
2268
+ if (unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */
2269
+ if (unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */
1983
2270
  }
1984
2271
 
1985
- /* copy literals */
1986
- cpy = op+length;
1987
2272
  #if LZ4_FAST_DEC_LOOP
1988
2273
  safe_literal_copy:
1989
2274
  #endif
2275
+ /* copy literals */
2276
+ cpy = op+length;
2277
+
1990
2278
  LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH);
1991
- if ( ((endOnInput) && ((cpy>oend-MFLIMIT) || (ip+length>iend-(2+1+LASTLITERALS))) )
1992
- || ((!endOnInput) && (cpy>oend-WILDCOPYLENGTH)) )
1993
- {
2279
+ if ((cpy>oend-MFLIMIT) || (ip+length>iend-(2+1+LASTLITERALS))) {
1994
2280
  /* We've either hit the input parsing restriction or the output parsing restriction.
1995
2281
  * In the normal scenario, decoding a full block, it must be the last sequence,
1996
2282
  * otherwise it's an error (invalid input or dimensions).
@@ -2000,7 +2286,6 @@ LZ4_decompress_generic(
2000
2286
  /* Since we are partial decoding we may be in this block because of the output parsing
2001
2287
  * restriction, which is not valid since the output buffer is allowed to be undersized.
2002
2288
  */
2003
- assert(endOnInput);
2004
2289
  DEBUGLOG(7, "partialDecoding: copying literals, close to input or output end")
2005
2290
  DEBUGLOG(7, "partialDecoding: literal length = %u", (unsigned)length);
2006
2291
  DEBUGLOG(7, "partialDecoding: remaining space in dstBuffer : %i", (int)(oend - op));
@@ -2021,21 +2306,18 @@ LZ4_decompress_generic(
2021
2306
  length = (size_t)(oend-op);
2022
2307
  }
2023
2308
  } else {
2024
- /* We must be on the last sequence because of the parsing limitations so check
2025
- * that we exactly regenerate the original size (must be exact when !endOnInput).
2026
- */
2027
- if ((!endOnInput) && (cpy != oend)) { goto _output_error; }
2028
2309
  /* We must be on the last sequence (or invalid) because of the parsing limitations
2029
2310
  * so check that we exactly consume the input and don't overrun the output buffer.
2030
2311
  */
2031
- if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) {
2032
- DEBUGLOG(6, "should have been last run of literals")
2033
- DEBUGLOG(6, "ip(%p) + length(%i) = %p != iend (%p)", ip, (int)length, ip+length, iend);
2034
- DEBUGLOG(6, "or cpy(%p) > oend(%p)", cpy, oend);
2312
+ if ((ip+length != iend) || (cpy > oend)) {
2313
+ DEBUGLOG(5, "should have been last run of literals")
2314
+ DEBUGLOG(5, "ip(%p) + length(%i) = %p != iend (%p)", ip, (int)length, ip+length, iend);
2315
+ DEBUGLOG(5, "or cpy(%p) > (oend-MFLIMIT)(%p)", cpy, oend-MFLIMIT);
2316
+ DEBUGLOG(5, "after writing %u bytes / %i bytes available", (unsigned)(op-(BYTE*)dst), outputSize);
2035
2317
  goto _output_error;
2036
2318
  }
2037
2319
  }
2038
- memmove(op, ip, length); /* supports overlapping memory regions; only matters for in-place decompression scenarios */
2320
+ LZ4_memmove(op, ip, length); /* supports overlapping memory regions, for in-place decompression scenarios */
2039
2321
  ip += length;
2040
2322
  op += length;
2041
2323
  /* Necessarily EOF when !partialDecoding.
@@ -2047,7 +2329,7 @@ LZ4_decompress_generic(
2047
2329
  break;
2048
2330
  }
2049
2331
  } else {
2050
- LZ4_wildCopy8(op, ip, cpy); /* may overwrite up to WILDCOPYLENGTH beyond cpy */
2332
+ LZ4_wildCopy8(op, ip, cpy); /* can overwrite up to 8 bytes beyond cpy */
2051
2333
  ip += length; op = cpy;
2052
2334
  }
2053
2335
 
@@ -2057,13 +2339,14 @@ LZ4_decompress_generic(
2057
2339
 
2058
2340
  /* get matchlength */
2059
2341
  length = token & ML_MASK;
2342
+ DEBUGLOG(7, "blockPos%6u: matchLength token = %u", (unsigned)(op-(BYTE*)dst), (unsigned)length);
2060
2343
 
2061
2344
  _copy_match:
2062
2345
  if (length == ML_MASK) {
2063
- variable_length_error error = ok;
2064
- length += read_variable_length(&ip, iend - LASTLITERALS + 1, (int)endOnInput, 0, &error);
2065
- if (error != ok) goto _output_error;
2066
- if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) goto _output_error; /* overflow detection */
2346
+ size_t const addl = read_variable_length(&ip, iend - LASTLITERALS + 1, 0);
2347
+ if (addl == rvl_error) { goto _output_error; }
2348
+ length += addl;
2349
+ if (unlikely((uptrval)(op)+length<(uptrval)op)) goto _output_error; /* overflow detection */
2067
2350
  }
2068
2351
  length += MINMATCH;
2069
2352
 
@@ -2073,6 +2356,7 @@ LZ4_decompress_generic(
2073
2356
  if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) goto _output_error; /* Error : offset outside buffers */
2074
2357
  /* match starting within external dictionary */
2075
2358
  if ((dict==usingExtDict) && (match < lowPrefix)) {
2359
+ assert(dictEnd != NULL);
2076
2360
  if (unlikely(op+length > oend-LASTLITERALS)) {
2077
2361
  if (partialDecoding) length = MIN(length, (size_t)(oend-op));
2078
2362
  else goto _output_error; /* doesn't respect parsing restriction */
@@ -2080,7 +2364,7 @@ LZ4_decompress_generic(
2080
2364
 
2081
2365
  if (length <= (size_t)(lowPrefix-match)) {
2082
2366
  /* match fits entirely within external dictionary : just copy */
2083
- memmove(op, dictEnd - (lowPrefix-match), length);
2367
+ LZ4_memmove(op, dictEnd - (lowPrefix-match), length);
2084
2368
  op += length;
2085
2369
  } else {
2086
2370
  /* match stretches into both external dictionary and current block */
@@ -2145,18 +2429,14 @@ LZ4_decompress_generic(
2145
2429
  while (op < cpy) { *op++ = *match++; }
2146
2430
  } else {
2147
2431
  LZ4_memcpy(op, match, 8);
2148
- if (length > 16) { LZ4_wildCopy8(op+8, match+8, cpy); }
2432
+ if (length > 16) { LZ4_wildCopy8(op+8, match+8, cpy); }
2149
2433
  }
2150
2434
  op = cpy; /* wildcopy correction */
2151
2435
  }
2152
2436
 
2153
2437
  /* end of decoding */
2154
- if (endOnInput) {
2155
- DEBUGLOG(5, "decoded %i bytes", (int) (((char*)op)-dst));
2156
- return (int) (((char*)op)-dst); /* Nb of output bytes decoded */
2157
- } else {
2158
- return (int) (((const char*)ip)-src); /* Nb of input bytes read */
2159
- }
2438
+ DEBUGLOG(5, "decoded %i bytes", (int) (((char*)op)-dst));
2439
+ return (int) (((char*)op)-dst); /* Nb of output bytes decoded */
2160
2440
 
2161
2441
  /* Overflow error detected */
2162
2442
  _output_error:
@@ -2171,7 +2451,7 @@ LZ4_FORCE_O2
2171
2451
  int LZ4_decompress_safe(const char* source, char* dest, int compressedSize, int maxDecompressedSize)
2172
2452
  {
2173
2453
  return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize,
2174
- endOnInputSize, decode_full_block, noDict,
2454
+ decode_full_block, noDict,
2175
2455
  (BYTE*)dest, NULL, 0);
2176
2456
  }
2177
2457
 
@@ -2180,16 +2460,17 @@ int LZ4_decompress_safe_partial(const char* src, char* dst, int compressedSize,
2180
2460
  {
2181
2461
  dstCapacity = MIN(targetOutputSize, dstCapacity);
2182
2462
  return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity,
2183
- endOnInputSize, partial_decode,
2463
+ partial_decode,
2184
2464
  noDict, (BYTE*)dst, NULL, 0);
2185
2465
  }
2186
2466
 
2187
2467
  LZ4_FORCE_O2
2188
2468
  int LZ4_decompress_fast(const char* source, char* dest, int originalSize)
2189
2469
  {
2190
- return LZ4_decompress_generic(source, dest, 0, originalSize,
2191
- endOnOutputSize, decode_full_block, withPrefix64k,
2192
- (BYTE*)dest - 64 KB, NULL, 0);
2470
+ DEBUGLOG(5, "LZ4_decompress_fast");
2471
+ return LZ4_decompress_unsafe_generic(
2472
+ (const BYTE*)source, (BYTE*)dest, originalSize,
2473
+ 0, NULL, 0);
2193
2474
  }
2194
2475
 
2195
2476
  /*===== Instantiate a few more decoding cases, used more than once. =====*/
@@ -2198,16 +2479,25 @@ LZ4_FORCE_O2 /* Exported, an obsolete API function. */
2198
2479
  int LZ4_decompress_safe_withPrefix64k(const char* source, char* dest, int compressedSize, int maxOutputSize)
2199
2480
  {
2200
2481
  return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize,
2201
- endOnInputSize, decode_full_block, withPrefix64k,
2482
+ decode_full_block, withPrefix64k,
2483
+ (BYTE*)dest - 64 KB, NULL, 0);
2484
+ }
2485
+
2486
+ LZ4_FORCE_O2
2487
+ static int LZ4_decompress_safe_partial_withPrefix64k(const char* source, char* dest, int compressedSize, int targetOutputSize, int dstCapacity)
2488
+ {
2489
+ dstCapacity = MIN(targetOutputSize, dstCapacity);
2490
+ return LZ4_decompress_generic(source, dest, compressedSize, dstCapacity,
2491
+ partial_decode, withPrefix64k,
2202
2492
  (BYTE*)dest - 64 KB, NULL, 0);
2203
2493
  }
2204
2494
 
2205
2495
  /* Another obsolete API function, paired with the previous one. */
2206
2496
  int LZ4_decompress_fast_withPrefix64k(const char* source, char* dest, int originalSize)
2207
2497
  {
2208
- /* LZ4_decompress_fast doesn't validate match offsets,
2209
- * and thus serves well with any prefixed dictionary. */
2210
- return LZ4_decompress_fast(source, dest, originalSize);
2498
+ return LZ4_decompress_unsafe_generic(
2499
+ (const BYTE*)source, (BYTE*)dest, originalSize,
2500
+ 64 KB, NULL, 0);
2211
2501
  }
2212
2502
 
2213
2503
  LZ4_FORCE_O2
@@ -2215,7 +2505,17 @@ static int LZ4_decompress_safe_withSmallPrefix(const char* source, char* dest, i
2215
2505
  size_t prefixSize)
2216
2506
  {
2217
2507
  return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize,
2218
- endOnInputSize, decode_full_block, noDict,
2508
+ decode_full_block, noDict,
2509
+ (BYTE*)dest-prefixSize, NULL, 0);
2510
+ }
2511
+
2512
+ LZ4_FORCE_O2
2513
+ static int LZ4_decompress_safe_partial_withSmallPrefix(const char* source, char* dest, int compressedSize, int targetOutputSize, int dstCapacity,
2514
+ size_t prefixSize)
2515
+ {
2516
+ dstCapacity = MIN(targetOutputSize, dstCapacity);
2517
+ return LZ4_decompress_generic(source, dest, compressedSize, dstCapacity,
2518
+ partial_decode, noDict,
2219
2519
  (BYTE*)dest-prefixSize, NULL, 0);
2220
2520
  }
2221
2521
 
@@ -2224,8 +2524,20 @@ int LZ4_decompress_safe_forceExtDict(const char* source, char* dest,
2224
2524
  int compressedSize, int maxOutputSize,
2225
2525
  const void* dictStart, size_t dictSize)
2226
2526
  {
2527
+ DEBUGLOG(5, "LZ4_decompress_safe_forceExtDict");
2227
2528
  return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize,
2228
- endOnInputSize, decode_full_block, usingExtDict,
2529
+ decode_full_block, usingExtDict,
2530
+ (BYTE*)dest, (const BYTE*)dictStart, dictSize);
2531
+ }
2532
+
2533
+ LZ4_FORCE_O2
2534
+ int LZ4_decompress_safe_partial_forceExtDict(const char* source, char* dest,
2535
+ int compressedSize, int targetOutputSize, int dstCapacity,
2536
+ const void* dictStart, size_t dictSize)
2537
+ {
2538
+ dstCapacity = MIN(targetOutputSize, dstCapacity);
2539
+ return LZ4_decompress_generic(source, dest, compressedSize, dstCapacity,
2540
+ partial_decode, usingExtDict,
2229
2541
  (BYTE*)dest, (const BYTE*)dictStart, dictSize);
2230
2542
  }
2231
2543
 
@@ -2233,9 +2545,9 @@ LZ4_FORCE_O2
2233
2545
  static int LZ4_decompress_fast_extDict(const char* source, char* dest, int originalSize,
2234
2546
  const void* dictStart, size_t dictSize)
2235
2547
  {
2236
- return LZ4_decompress_generic(source, dest, 0, originalSize,
2237
- endOnOutputSize, decode_full_block, usingExtDict,
2238
- (BYTE*)dest, (const BYTE*)dictStart, dictSize);
2548
+ return LZ4_decompress_unsafe_generic(
2549
+ (const BYTE*)source, (BYTE*)dest, originalSize,
2550
+ 0, (const BYTE*)dictStart, dictSize);
2239
2551
  }
2240
2552
 
2241
2553
  /* The "double dictionary" mode, for use with e.g. ring buffers: the first part
@@ -2247,26 +2559,17 @@ int LZ4_decompress_safe_doubleDict(const char* source, char* dest, int compresse
2247
2559
  size_t prefixSize, const void* dictStart, size_t dictSize)
2248
2560
  {
2249
2561
  return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize,
2250
- endOnInputSize, decode_full_block, usingExtDict,
2251
- (BYTE*)dest-prefixSize, (const BYTE*)dictStart, dictSize);
2252
- }
2253
-
2254
- LZ4_FORCE_INLINE
2255
- int LZ4_decompress_fast_doubleDict(const char* source, char* dest, int originalSize,
2256
- size_t prefixSize, const void* dictStart, size_t dictSize)
2257
- {
2258
- return LZ4_decompress_generic(source, dest, 0, originalSize,
2259
- endOnOutputSize, decode_full_block, usingExtDict,
2562
+ decode_full_block, usingExtDict,
2260
2563
  (BYTE*)dest-prefixSize, (const BYTE*)dictStart, dictSize);
2261
2564
  }
2262
2565
 
2263
2566
  /*===== streaming decompression functions =====*/
2264
2567
 
2568
+ #if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION)
2265
2569
  LZ4_streamDecode_t* LZ4_createStreamDecode(void)
2266
2570
  {
2267
- LZ4_streamDecode_t* lz4s = (LZ4_streamDecode_t*) ALLOC_AND_ZERO(sizeof(LZ4_streamDecode_t));
2268
- LZ4_STATIC_ASSERT(LZ4_STREAMDECODESIZE >= sizeof(LZ4_streamDecode_t_internal)); /* A compilation error here means LZ4_STREAMDECODESIZE is not large enough */
2269
- return lz4s;
2571
+ LZ4_STATIC_ASSERT(sizeof(LZ4_streamDecode_t) >= sizeof(LZ4_streamDecode_t_internal));
2572
+ return (LZ4_streamDecode_t*) ALLOC_AND_ZERO(sizeof(LZ4_streamDecode_t));
2270
2573
  }
2271
2574
 
2272
2575
  int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream)
@@ -2275,6 +2578,7 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream)
2275
2578
  FREEMEM(LZ4_stream);
2276
2579
  return 0;
2277
2580
  }
2581
+ #endif
2278
2582
 
2279
2583
  /*! LZ4_setStreamDecode() :
2280
2584
  * Use this function to instruct where to find the dictionary.
@@ -2285,8 +2589,13 @@ int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream)
2285
2589
  int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize)
2286
2590
  {
2287
2591
  LZ4_streamDecode_t_internal* lz4sd = &LZ4_streamDecode->internal_donotuse;
2288
- lz4sd->prefixSize = (size_t) dictSize;
2289
- lz4sd->prefixEnd = (const BYTE*) dictionary + dictSize;
2592
+ lz4sd->prefixSize = (size_t)dictSize;
2593
+ if (dictSize) {
2594
+ assert(dictionary != NULL);
2595
+ lz4sd->prefixEnd = (const BYTE*) dictionary + dictSize;
2596
+ } else {
2597
+ lz4sd->prefixEnd = (const BYTE*) dictionary;
2598
+ }
2290
2599
  lz4sd->externalDict = NULL;
2291
2600
  lz4sd->extDictSize = 0;
2292
2601
  return 1;
@@ -2358,29 +2667,35 @@ int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const ch
2358
2667
  return result;
2359
2668
  }
2360
2669
 
2361
- LZ4_FORCE_O2
2362
- int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int originalSize)
2670
+ LZ4_FORCE_O2 int
2671
+ LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode,
2672
+ const char* source, char* dest, int originalSize)
2363
2673
  {
2364
- LZ4_streamDecode_t_internal* lz4sd = &LZ4_streamDecode->internal_donotuse;
2674
+ LZ4_streamDecode_t_internal* const lz4sd =
2675
+ (assert(LZ4_streamDecode!=NULL), &LZ4_streamDecode->internal_donotuse);
2365
2676
  int result;
2677
+
2678
+ DEBUGLOG(5, "LZ4_decompress_fast_continue (toDecodeSize=%i)", originalSize);
2366
2679
  assert(originalSize >= 0);
2367
2680
 
2368
2681
  if (lz4sd->prefixSize == 0) {
2682
+ DEBUGLOG(5, "first invocation : no prefix nor extDict");
2369
2683
  assert(lz4sd->extDictSize == 0);
2370
2684
  result = LZ4_decompress_fast(source, dest, originalSize);
2371
2685
  if (result <= 0) return result;
2372
2686
  lz4sd->prefixSize = (size_t)originalSize;
2373
2687
  lz4sd->prefixEnd = (BYTE*)dest + originalSize;
2374
2688
  } else if (lz4sd->prefixEnd == (BYTE*)dest) {
2375
- if (lz4sd->prefixSize >= 64 KB - 1 || lz4sd->extDictSize == 0)
2376
- result = LZ4_decompress_fast(source, dest, originalSize);
2377
- else
2378
- result = LZ4_decompress_fast_doubleDict(source, dest, originalSize,
2379
- lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize);
2689
+ DEBUGLOG(5, "continue using existing prefix");
2690
+ result = LZ4_decompress_unsafe_generic(
2691
+ (const BYTE*)source, (BYTE*)dest, originalSize,
2692
+ lz4sd->prefixSize,
2693
+ lz4sd->externalDict, lz4sd->extDictSize);
2380
2694
  if (result <= 0) return result;
2381
2695
  lz4sd->prefixSize += (size_t)originalSize;
2382
2696
  lz4sd->prefixEnd += originalSize;
2383
2697
  } else {
2698
+ DEBUGLOG(5, "prefix becomes extDict");
2384
2699
  lz4sd->extDictSize = lz4sd->prefixSize;
2385
2700
  lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
2386
2701
  result = LZ4_decompress_fast_extDict(source, dest, originalSize,
@@ -2416,10 +2731,27 @@ int LZ4_decompress_safe_usingDict(const char* source, char* dest, int compressed
2416
2731
  return LZ4_decompress_safe_forceExtDict(source, dest, compressedSize, maxOutputSize, dictStart, (size_t)dictSize);
2417
2732
  }
2418
2733
 
2734
+ int LZ4_decompress_safe_partial_usingDict(const char* source, char* dest, int compressedSize, int targetOutputSize, int dstCapacity, const char* dictStart, int dictSize)
2735
+ {
2736
+ if (dictSize==0)
2737
+ return LZ4_decompress_safe_partial(source, dest, compressedSize, targetOutputSize, dstCapacity);
2738
+ if (dictStart+dictSize == dest) {
2739
+ if (dictSize >= 64 KB - 1) {
2740
+ return LZ4_decompress_safe_partial_withPrefix64k(source, dest, compressedSize, targetOutputSize, dstCapacity);
2741
+ }
2742
+ assert(dictSize >= 0);
2743
+ return LZ4_decompress_safe_partial_withSmallPrefix(source, dest, compressedSize, targetOutputSize, dstCapacity, (size_t)dictSize);
2744
+ }
2745
+ assert(dictSize >= 0);
2746
+ return LZ4_decompress_safe_partial_forceExtDict(source, dest, compressedSize, targetOutputSize, dstCapacity, dictStart, (size_t)dictSize);
2747
+ }
2748
+
2419
2749
  int LZ4_decompress_fast_usingDict(const char* source, char* dest, int originalSize, const char* dictStart, int dictSize)
2420
2750
  {
2421
2751
  if (dictSize==0 || dictStart+dictSize == dest)
2422
- return LZ4_decompress_fast(source, dest, originalSize);
2752
+ return LZ4_decompress_unsafe_generic(
2753
+ (const BYTE*)source, (BYTE*)dest, originalSize,
2754
+ (size_t)dictSize, NULL, 0);
2423
2755
  assert(dictSize >= 0);
2424
2756
  return LZ4_decompress_fast_extDict(source, dest, originalSize, dictStart, (size_t)dictSize);
2425
2757
  }
@@ -2471,7 +2803,7 @@ int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize,
2471
2803
 
2472
2804
  /* Obsolete Streaming functions */
2473
2805
 
2474
- int LZ4_sizeofStreamState(void) { return LZ4_STREAMSIZE; }
2806
+ int LZ4_sizeofStreamState(void) { return sizeof(LZ4_stream_t); }
2475
2807
 
2476
2808
  int LZ4_resetStreamState(void* state, char* inputBuffer)
2477
2809
  {
@@ -2480,11 +2812,13 @@ int LZ4_resetStreamState(void* state, char* inputBuffer)
2480
2812
  return 0;
2481
2813
  }
2482
2814
 
2815
+ #if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION)
2483
2816
  void* LZ4_create (char* inputBuffer)
2484
2817
  {
2485
2818
  (void)inputBuffer;
2486
2819
  return LZ4_createStream();
2487
2820
  }
2821
+ #endif
2488
2822
 
2489
2823
  char* LZ4_slideInputBuffer (void* state)
2490
2824
  {