extzstd 0.3 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/HISTORY.ja.md +8 -0
  3. data/README.md +1 -1
  4. data/contrib/zstd/CHANGELOG +94 -0
  5. data/contrib/zstd/CONTRIBUTING.md +351 -1
  6. data/contrib/zstd/Makefile +32 -10
  7. data/contrib/zstd/README.md +33 -10
  8. data/contrib/zstd/TESTING.md +2 -2
  9. data/contrib/zstd/appveyor.yml +42 -4
  10. data/contrib/zstd/lib/Makefile +128 -60
  11. data/contrib/zstd/lib/README.md +47 -16
  12. data/contrib/zstd/lib/common/bitstream.h +38 -39
  13. data/contrib/zstd/lib/common/compiler.h +40 -5
  14. data/contrib/zstd/lib/common/cpu.h +1 -1
  15. data/contrib/zstd/lib/common/debug.c +11 -31
  16. data/contrib/zstd/lib/common/debug.h +11 -31
  17. data/contrib/zstd/lib/common/entropy_common.c +13 -33
  18. data/contrib/zstd/lib/common/error_private.c +2 -1
  19. data/contrib/zstd/lib/common/error_private.h +6 -2
  20. data/contrib/zstd/lib/common/fse.h +12 -32
  21. data/contrib/zstd/lib/common/fse_decompress.c +12 -35
  22. data/contrib/zstd/lib/common/huf.h +15 -33
  23. data/contrib/zstd/lib/common/mem.h +75 -2
  24. data/contrib/zstd/lib/common/pool.c +8 -4
  25. data/contrib/zstd/lib/common/pool.h +2 -2
  26. data/contrib/zstd/lib/common/threading.c +50 -4
  27. data/contrib/zstd/lib/common/threading.h +36 -4
  28. data/contrib/zstd/lib/common/xxhash.c +23 -35
  29. data/contrib/zstd/lib/common/xxhash.h +11 -31
  30. data/contrib/zstd/lib/common/zstd_common.c +1 -1
  31. data/contrib/zstd/lib/common/zstd_errors.h +2 -1
  32. data/contrib/zstd/lib/common/zstd_internal.h +154 -26
  33. data/contrib/zstd/lib/compress/fse_compress.c +17 -40
  34. data/contrib/zstd/lib/compress/hist.c +15 -35
  35. data/contrib/zstd/lib/compress/hist.h +12 -32
  36. data/contrib/zstd/lib/compress/huf_compress.c +92 -92
  37. data/contrib/zstd/lib/compress/zstd_compress.c +1191 -1330
  38. data/contrib/zstd/lib/compress/zstd_compress_internal.h +317 -55
  39. data/contrib/zstd/lib/compress/zstd_compress_literals.c +158 -0
  40. data/contrib/zstd/lib/compress/zstd_compress_literals.h +29 -0
  41. data/contrib/zstd/lib/compress/zstd_compress_sequences.c +419 -0
  42. data/contrib/zstd/lib/compress/zstd_compress_sequences.h +54 -0
  43. data/contrib/zstd/lib/compress/zstd_compress_superblock.c +845 -0
  44. data/contrib/zstd/lib/compress/zstd_compress_superblock.h +32 -0
  45. data/contrib/zstd/lib/compress/zstd_cwksp.h +525 -0
  46. data/contrib/zstd/lib/compress/zstd_double_fast.c +65 -43
  47. data/contrib/zstd/lib/compress/zstd_double_fast.h +2 -2
  48. data/contrib/zstd/lib/compress/zstd_fast.c +92 -66
  49. data/contrib/zstd/lib/compress/zstd_fast.h +2 -2
  50. data/contrib/zstd/lib/compress/zstd_lazy.c +74 -42
  51. data/contrib/zstd/lib/compress/zstd_lazy.h +1 -1
  52. data/contrib/zstd/lib/compress/zstd_ldm.c +32 -10
  53. data/contrib/zstd/lib/compress/zstd_ldm.h +7 -2
  54. data/contrib/zstd/lib/compress/zstd_opt.c +81 -114
  55. data/contrib/zstd/lib/compress/zstd_opt.h +1 -1
  56. data/contrib/zstd/lib/compress/zstdmt_compress.c +95 -51
  57. data/contrib/zstd/lib/compress/zstdmt_compress.h +3 -2
  58. data/contrib/zstd/lib/decompress/huf_decompress.c +76 -60
  59. data/contrib/zstd/lib/decompress/zstd_ddict.c +12 -8
  60. data/contrib/zstd/lib/decompress/zstd_ddict.h +2 -2
  61. data/contrib/zstd/lib/decompress/zstd_decompress.c +292 -172
  62. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +459 -338
  63. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +3 -3
  64. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +18 -4
  65. data/contrib/zstd/lib/deprecated/zbuff.h +9 -8
  66. data/contrib/zstd/lib/deprecated/zbuff_common.c +2 -2
  67. data/contrib/zstd/lib/deprecated/zbuff_compress.c +1 -1
  68. data/contrib/zstd/lib/deprecated/zbuff_decompress.c +1 -1
  69. data/contrib/zstd/lib/dictBuilder/cover.c +164 -54
  70. data/contrib/zstd/lib/dictBuilder/cover.h +52 -7
  71. data/contrib/zstd/lib/dictBuilder/fastcover.c +60 -43
  72. data/contrib/zstd/lib/dictBuilder/zdict.c +43 -19
  73. data/contrib/zstd/lib/dictBuilder/zdict.h +56 -28
  74. data/contrib/zstd/lib/legacy/zstd_legacy.h +8 -4
  75. data/contrib/zstd/lib/legacy/zstd_v01.c +110 -110
  76. data/contrib/zstd/lib/legacy/zstd_v01.h +1 -1
  77. data/contrib/zstd/lib/legacy/zstd_v02.c +23 -13
  78. data/contrib/zstd/lib/legacy/zstd_v02.h +1 -1
  79. data/contrib/zstd/lib/legacy/zstd_v03.c +23 -13
  80. data/contrib/zstd/lib/legacy/zstd_v03.h +1 -1
  81. data/contrib/zstd/lib/legacy/zstd_v04.c +30 -17
  82. data/contrib/zstd/lib/legacy/zstd_v04.h +1 -1
  83. data/contrib/zstd/lib/legacy/zstd_v05.c +113 -102
  84. data/contrib/zstd/lib/legacy/zstd_v05.h +2 -2
  85. data/contrib/zstd/lib/legacy/zstd_v06.c +20 -18
  86. data/contrib/zstd/lib/legacy/zstd_v06.h +1 -1
  87. data/contrib/zstd/lib/legacy/zstd_v07.c +25 -19
  88. data/contrib/zstd/lib/legacy/zstd_v07.h +1 -1
  89. data/contrib/zstd/lib/libzstd.pc.in +3 -2
  90. data/contrib/zstd/lib/zstd.h +265 -88
  91. data/ext/extzstd.h +1 -1
  92. data/ext/libzstd_conf.h +8 -0
  93. data/ext/zstd_common.c +1 -3
  94. data/ext/zstd_compress.c +3 -3
  95. data/ext/zstd_decompress.c +1 -5
  96. data/ext/zstd_dictbuilder.c +2 -3
  97. data/ext/zstd_dictbuilder_fastcover.c +1 -3
  98. data/ext/zstd_legacy_v01.c +2 -0
  99. data/ext/zstd_legacy_v02.c +2 -0
  100. data/ext/zstd_legacy_v03.c +2 -0
  101. data/ext/zstd_legacy_v04.c +2 -0
  102. data/ext/zstd_legacy_v05.c +2 -0
  103. data/ext/zstd_legacy_v06.c +2 -0
  104. data/ext/zstd_legacy_v07.c +2 -0
  105. data/lib/extzstd.rb +18 -10
  106. data/lib/extzstd/version.rb +1 -1
  107. metadata +15 -6
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -11,7 +11,7 @@
11
11
 
12
12
  #include <stddef.h> /* size_t, ptrdiff_t */
13
13
  #include "zstd_v02.h"
14
- #include "error_private.h"
14
+ #include "../common/error_private.h"
15
15
 
16
16
 
17
17
  /******************************************
@@ -189,7 +189,7 @@ MEM_STATIC void MEM_write16(void* memPtr, U16 value)
189
189
  memcpy(memPtr, &value, sizeof(value));
190
190
  }
191
191
 
192
- #endif // MEM_FORCE_MEMORY_ACCESS
192
+ #endif /* MEM_FORCE_MEMORY_ACCESS */
193
193
 
194
194
 
195
195
  MEM_STATIC U16 MEM_readLE16(const void* memPtr)
@@ -217,6 +217,11 @@ MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
217
217
  }
218
218
  }
219
219
 
220
+ MEM_STATIC U32 MEM_readLE24(const void* memPtr)
221
+ {
222
+ return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
223
+ }
224
+
220
225
  MEM_STATIC U32 MEM_readLE32(const void* memPtr)
221
226
  {
222
227
  if (MEM_isLittleEndian())
@@ -348,7 +353,7 @@ MEM_STATIC unsigned BIT_highbit32 (U32 val)
348
353
  _BitScanReverse ( &r, val );
349
354
  return (unsigned) r;
350
355
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
351
- return 31 - __builtin_clz (val);
356
+ return __builtin_clz (val) ^ 31;
352
357
  # else /* Software version */
353
358
  static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
354
359
  U32 v = val;
@@ -2831,7 +2836,9 @@ static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockPropertie
2831
2836
  static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2832
2837
  {
2833
2838
  if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
2834
- memcpy(dst, src, srcSize);
2839
+ if (srcSize > 0) {
2840
+ memcpy(dst, src, srcSize);
2841
+ }
2835
2842
  return srcSize;
2836
2843
  }
2837
2844
 
@@ -2884,6 +2891,7 @@ static size_t ZSTD_decodeLiteralsBlock(void* ctx,
2884
2891
  const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2885
2892
  if (litSize > srcSize-11) /* risk of reading too far with wildcopy */
2886
2893
  {
2894
+ if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2887
2895
  if (litSize > srcSize-3) return ERROR(corruption_detected);
2888
2896
  memcpy(dctx->litBuffer, istart, litSize);
2889
2897
  dctx->litPtr = dctx->litBuffer;
@@ -3043,11 +3051,11 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
3043
3051
  seqState->prevOffset = seq->offset;
3044
3052
  if (litLength == MaxLL)
3045
3053
  {
3046
- U32 add = *dumps++;
3054
+ const U32 add = dumps<de ? *dumps++ : 0;
3047
3055
  if (add < 255) litLength += add;
3048
- else
3056
+ else if (dumps + 3 <= de)
3049
3057
  {
3050
- litLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
3058
+ litLength = MEM_readLE24(dumps);
3051
3059
  dumps += 3;
3052
3060
  }
3053
3061
  if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
@@ -3073,11 +3081,11 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
3073
3081
  matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
3074
3082
  if (matchLength == MaxML)
3075
3083
  {
3076
- U32 add = *dumps++;
3084
+ const U32 add = dumps<de ? *dumps++ : 0;
3077
3085
  if (add < 255) matchLength += add;
3078
- else
3086
+ else if (dumps + 3 <= de)
3079
3087
  {
3080
- matchLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
3088
+ matchLength = MEM_readLE24(dumps);
3081
3089
  dumps += 3;
3082
3090
  }
3083
3091
  if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
@@ -3223,8 +3231,10 @@ static size_t ZSTD_decompressSequences(
3223
3231
  size_t lastLLSize = litEnd - litPtr;
3224
3232
  if (litPtr > litEnd) return ERROR(corruption_detected);
3225
3233
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3226
- if (op != litPtr) memmove(op, litPtr, lastLLSize);
3227
- op += lastLLSize;
3234
+ if (lastLLSize > 0) {
3235
+ if (op != litPtr) memmove(op, litPtr, lastLLSize);
3236
+ op += lastLLSize;
3237
+ }
3228
3238
  }
3229
3239
  }
3230
3240
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -11,7 +11,7 @@
11
11
 
12
12
  #include <stddef.h> /* size_t, ptrdiff_t */
13
13
  #include "zstd_v03.h"
14
- #include "error_private.h"
14
+ #include "../common/error_private.h"
15
15
 
16
16
 
17
17
  /******************************************
@@ -191,7 +191,7 @@ MEM_STATIC void MEM_write16(void* memPtr, U16 value)
191
191
  }
192
192
 
193
193
 
194
- #endif // MEM_FORCE_MEMORY_ACCESS
194
+ #endif /* MEM_FORCE_MEMORY_ACCESS */
195
195
 
196
196
 
197
197
  MEM_STATIC U16 MEM_readLE16(const void* memPtr)
@@ -219,6 +219,11 @@ MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
219
219
  }
220
220
  }
221
221
 
222
+ MEM_STATIC U32 MEM_readLE24(const void* memPtr)
223
+ {
224
+ return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
225
+ }
226
+
222
227
  MEM_STATIC U32 MEM_readLE32(const void* memPtr)
223
228
  {
224
229
  if (MEM_isLittleEndian())
@@ -351,7 +356,7 @@ MEM_STATIC unsigned BIT_highbit32 (U32 val)
351
356
  _BitScanReverse ( &r, val );
352
357
  return (unsigned) r;
353
358
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
354
- return 31 - __builtin_clz (val);
359
+ return __builtin_clz (val) ^ 31;
355
360
  # else /* Software version */
356
361
  static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
357
362
  U32 v = val;
@@ -2472,7 +2477,9 @@ static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockPropertie
2472
2477
  static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2473
2478
  {
2474
2479
  if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
2475
- memcpy(dst, src, srcSize);
2480
+ if (srcSize > 0) {
2481
+ memcpy(dst, src, srcSize);
2482
+ }
2476
2483
  return srcSize;
2477
2484
  }
2478
2485
 
@@ -2525,6 +2532,7 @@ static size_t ZSTD_decodeLiteralsBlock(void* ctx,
2525
2532
  const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2526
2533
  if (litSize > srcSize-11) /* risk of reading too far with wildcopy */
2527
2534
  {
2535
+ if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2528
2536
  if (litSize > srcSize-3) return ERROR(corruption_detected);
2529
2537
  memcpy(dctx->litBuffer, istart, litSize);
2530
2538
  dctx->litPtr = dctx->litBuffer;
@@ -2684,11 +2692,11 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
2684
2692
  seqState->prevOffset = seq->offset;
2685
2693
  if (litLength == MaxLL)
2686
2694
  {
2687
- U32 add = *dumps++;
2695
+ const U32 add = dumps<de ? *dumps++ : 0;
2688
2696
  if (add < 255) litLength += add;
2689
- else
2697
+ else if (dumps + 3 <= de)
2690
2698
  {
2691
- litLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
2699
+ litLength = MEM_readLE24(dumps);
2692
2700
  dumps += 3;
2693
2701
  }
2694
2702
  if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
@@ -2714,11 +2722,11 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
2714
2722
  matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
2715
2723
  if (matchLength == MaxML)
2716
2724
  {
2717
- U32 add = *dumps++;
2725
+ const U32 add = dumps<de ? *dumps++ : 0;
2718
2726
  if (add < 255) matchLength += add;
2719
- else
2727
+ else if (dumps + 3 <= de)
2720
2728
  {
2721
- matchLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
2729
+ matchLength = MEM_readLE24(dumps);
2722
2730
  dumps += 3;
2723
2731
  }
2724
2732
  if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
@@ -2864,8 +2872,10 @@ static size_t ZSTD_decompressSequences(
2864
2872
  size_t lastLLSize = litEnd - litPtr;
2865
2873
  if (litPtr > litEnd) return ERROR(corruption_detected);
2866
2874
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
2867
- if (op != litPtr) memmove(op, litPtr, lastLLSize);
2868
- op += lastLLSize;
2875
+ if (lastLLSize > 0) {
2876
+ if (op != litPtr) memmove(op, litPtr, lastLLSize);
2877
+ op += lastLLSize;
2878
+ }
2869
2879
  }
2870
2880
  }
2871
2881
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -16,7 +16,7 @@
16
16
  #include <string.h> /* memcpy */
17
17
 
18
18
  #include "zstd_v04.h"
19
- #include "error_private.h"
19
+ #include "../common/error_private.h"
20
20
 
21
21
 
22
22
  /* ******************************************************************
@@ -161,7 +161,7 @@ MEM_STATIC void MEM_write16(void* memPtr, U16 value)
161
161
  memcpy(memPtr, &value, sizeof(value));
162
162
  }
163
163
 
164
- #endif // MEM_FORCE_MEMORY_ACCESS
164
+ #endif /* MEM_FORCE_MEMORY_ACCESS */
165
165
 
166
166
 
167
167
  MEM_STATIC U16 MEM_readLE16(const void* memPtr)
@@ -189,6 +189,11 @@ MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
189
189
  }
190
190
  }
191
191
 
192
+ MEM_STATIC U32 MEM_readLE24(const void* memPtr)
193
+ {
194
+ return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
195
+ }
196
+
192
197
  MEM_STATIC U32 MEM_readLE32(const void* memPtr)
193
198
  {
194
199
  if (MEM_isLittleEndian())
@@ -622,7 +627,7 @@ MEM_STATIC unsigned BIT_highbit32 (U32 val)
622
627
  _BitScanReverse ( &r, val );
623
628
  return (unsigned) r;
624
629
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
625
- return 31 - __builtin_clz (val);
630
+ return __builtin_clz (val) ^ 31;
626
631
  # else /* Software version */
627
632
  static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
628
633
  U32 v = val;
@@ -2598,7 +2603,9 @@ static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockPropertie
2598
2603
  static size_t ZSTD_copyRawBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2599
2604
  {
2600
2605
  if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
2601
- memcpy(dst, src, srcSize);
2606
+ if (srcSize > 0) {
2607
+ memcpy(dst, src, srcSize);
2608
+ }
2602
2609
  return srcSize;
2603
2610
  }
2604
2611
 
@@ -2650,6 +2657,7 @@ static size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
2650
2657
  const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2651
2658
  if (litSize > srcSize-11) /* risk of reading too far with wildcopy */
2652
2659
  {
2660
+ if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2653
2661
  if (litSize > srcSize-3) return ERROR(corruption_detected);
2654
2662
  memcpy(dctx->litBuffer, istart, litSize);
2655
2663
  dctx->litPtr = dctx->litBuffer;
@@ -2808,13 +2816,12 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
2808
2816
  litLength = FSE_decodeSymbol(&(seqState->stateLL), &(seqState->DStream));
2809
2817
  prevOffset = litLength ? seq->offset : seqState->prevOffset;
2810
2818
  if (litLength == MaxLL) {
2811
- U32 add = *dumps++;
2819
+ const U32 add = dumps<de ? *dumps++ : 0;
2812
2820
  if (add < 255) litLength += add;
2813
- else {
2814
- litLength = dumps[0] + (dumps[1]<<8) + (dumps[2]<<16);
2821
+ else if (dumps + 3 <= de) {
2822
+ litLength = MEM_readLE24(dumps);
2815
2823
  dumps += 3;
2816
2824
  }
2817
- if (dumps > de) { litLength = MaxLL+255; } /* late correction, to avoid using uninitialized memory */
2818
2825
  if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
2819
2826
  }
2820
2827
 
@@ -2837,13 +2844,12 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
2837
2844
  /* MatchLength */
2838
2845
  matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
2839
2846
  if (matchLength == MaxML) {
2840
- U32 add = *dumps++;
2847
+ const U32 add = dumps<de ? *dumps++ : 0;
2841
2848
  if (add < 255) matchLength += add;
2842
- else {
2843
- matchLength = dumps[0] + (dumps[1]<<8) + (dumps[2]<<16);
2849
+ else if (dumps + 3 <= de){
2850
+ matchLength = MEM_readLE24(dumps);
2844
2851
  dumps += 3;
2845
2852
  }
2846
- if (dumps > de) { matchLength = MaxML+255; } /* late correction, to avoid using uninitialized memory */
2847
2853
  if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
2848
2854
  }
2849
2855
  matchLength += MINMATCH;
@@ -3004,8 +3010,10 @@ static size_t ZSTD_decompressSequences(
3004
3010
  size_t lastLLSize = litEnd - litPtr;
3005
3011
  if (litPtr > litEnd) return ERROR(corruption_detected);
3006
3012
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3007
- if (op != litPtr) memcpy(op, litPtr, lastLLSize);
3008
- op += lastLLSize;
3013
+ if (lastLLSize > 0) {
3014
+ if (op != litPtr) memcpy(op, litPtr, lastLLSize);
3015
+ op += lastLLSize;
3016
+ }
3009
3017
  }
3010
3018
  }
3011
3019
 
@@ -3031,9 +3039,12 @@ static size_t ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx,
3031
3039
  {
3032
3040
  /* blockType == blockCompressed */
3033
3041
  const BYTE* ip = (const BYTE*)src;
3042
+ size_t litCSize;
3043
+
3044
+ if (srcSize > BLOCKSIZE) return ERROR(corruption_detected);
3034
3045
 
3035
3046
  /* Decode literals sub-block */
3036
- size_t litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);
3047
+ litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);
3037
3048
  if (ZSTD_isError(litCSize)) return litCSize;
3038
3049
  ip += litCSize;
3039
3050
  srcSize -= litCSize;
@@ -3400,7 +3411,9 @@ static size_t ZBUFF_decompressWithDictionary(ZBUFF_DCtx* zbc, const void* src, s
3400
3411
  static size_t ZBUFF_limitCopy(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3401
3412
  {
3402
3413
  size_t length = MIN(maxDstSize, srcSize);
3403
- memcpy(dst, src, length);
3414
+ if (length > 0) {
3415
+ memcpy(dst, src, length);
3416
+ }
3404
3417
  return length;
3405
3418
  }
3406
3419
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -11,7 +11,7 @@
11
11
 
12
12
  /*- Dependencies -*/
13
13
  #include "zstd_v05.h"
14
- #include "error_private.h"
14
+ #include "../common/error_private.h"
15
15
 
16
16
 
17
17
  /* ******************************************************************
@@ -756,7 +756,7 @@ MEM_STATIC unsigned BITv05_highbit32 (U32 val)
756
756
  _BitScanReverse ( &r, val );
757
757
  return (unsigned) r;
758
758
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
759
- return 31 - __builtin_clz (val);
759
+ return __builtin_clz (val) ^ 31;
760
760
  # else /* Software version */
761
761
  static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
762
762
  U32 v = val;
@@ -1804,7 +1804,7 @@ static size_t HUFv05_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
1804
1804
 
1805
1805
  if (!srcSize) return ERROR(srcSize_wrong);
1806
1806
  iSize = ip[0];
1807
- //memset(huffWeight, 0, hwSize); /* is not necessary, even though some analyzer complain ... */
1807
+ /* memset(huffWeight, 0, hwSize); */ /* is not necessary, even though some analyzer complain ... */
1808
1808
 
1809
1809
  if (iSize >= 128) { /* special header */
1810
1810
  if (iSize >= (242)) { /* RLE */
@@ -1879,7 +1879,7 @@ size_t HUFv05_readDTableX2 (U16* DTable, const void* src, size_t srcSize)
1879
1879
  HUFv05_DEltX2* const dt = (HUFv05_DEltX2*)dtPtr;
1880
1880
 
1881
1881
  HUFv05_STATIC_ASSERT(sizeof(HUFv05_DEltX2) == sizeof(U16)); /* if compilation fails here, assertion is false */
1882
- //memset(huffWeight, 0, sizeof(huffWeight)); /* is not necessary, even though some analyzer complain ... */
1882
+ /* memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */
1883
1883
 
1884
1884
  iSize = HUFv05_readStats(huffWeight, HUFv05_MAX_SYMBOL_VALUE + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
1885
1885
  if (HUFv05_isError(iSize)) return iSize;
@@ -1998,91 +1998,92 @@ size_t HUFv05_decompress4X2_usingDTable(
1998
1998
  const void* cSrc, size_t cSrcSize,
1999
1999
  const U16* DTable)
2000
2000
  {
2001
- const BYTE* const istart = (const BYTE*) cSrc;
2002
- BYTE* const ostart = (BYTE*) dst;
2003
- BYTE* const oend = ostart + dstSize;
2004
- const void* const dtPtr = DTable;
2005
- const HUFv05_DEltX2* const dt = ((const HUFv05_DEltX2*)dtPtr) +1;
2006
- const U32 dtLog = DTable[0];
2007
- size_t errorCode;
2008
-
2009
- /* Init */
2010
- BITv05_DStream_t bitD1;
2011
- BITv05_DStream_t bitD2;
2012
- BITv05_DStream_t bitD3;
2013
- BITv05_DStream_t bitD4;
2014
- const size_t length1 = MEM_readLE16(istart);
2015
- const size_t length2 = MEM_readLE16(istart+2);
2016
- const size_t length3 = MEM_readLE16(istart+4);
2017
- size_t length4;
2018
- const BYTE* const istart1 = istart + 6; /* jumpTable */
2019
- const BYTE* const istart2 = istart1 + length1;
2020
- const BYTE* const istart3 = istart2 + length2;
2021
- const BYTE* const istart4 = istart3 + length3;
2022
- const size_t segmentSize = (dstSize+3) / 4;
2023
- BYTE* const opStart2 = ostart + segmentSize;
2024
- BYTE* const opStart3 = opStart2 + segmentSize;
2025
- BYTE* const opStart4 = opStart3 + segmentSize;
2026
- BYTE* op1 = ostart;
2027
- BYTE* op2 = opStart2;
2028
- BYTE* op3 = opStart3;
2029
- BYTE* op4 = opStart4;
2030
- U32 endSignal;
2031
-
2032
2001
  /* Check */
2033
2002
  if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
2003
+ {
2004
+ const BYTE* const istart = (const BYTE*) cSrc;
2005
+ BYTE* const ostart = (BYTE*) dst;
2006
+ BYTE* const oend = ostart + dstSize;
2007
+ const void* const dtPtr = DTable;
2008
+ const HUFv05_DEltX2* const dt = ((const HUFv05_DEltX2*)dtPtr) +1;
2009
+ const U32 dtLog = DTable[0];
2010
+ size_t errorCode;
2034
2011
 
2035
- length4 = cSrcSize - (length1 + length2 + length3 + 6);
2036
- if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
2037
- errorCode = BITv05_initDStream(&bitD1, istart1, length1);
2038
- if (HUFv05_isError(errorCode)) return errorCode;
2039
- errorCode = BITv05_initDStream(&bitD2, istart2, length2);
2040
- if (HUFv05_isError(errorCode)) return errorCode;
2041
- errorCode = BITv05_initDStream(&bitD3, istart3, length3);
2042
- if (HUFv05_isError(errorCode)) return errorCode;
2043
- errorCode = BITv05_initDStream(&bitD4, istart4, length4);
2044
- if (HUFv05_isError(errorCode)) return errorCode;
2012
+ /* Init */
2013
+ BITv05_DStream_t bitD1;
2014
+ BITv05_DStream_t bitD2;
2015
+ BITv05_DStream_t bitD3;
2016
+ BITv05_DStream_t bitD4;
2017
+ const size_t length1 = MEM_readLE16(istart);
2018
+ const size_t length2 = MEM_readLE16(istart+2);
2019
+ const size_t length3 = MEM_readLE16(istart+4);
2020
+ size_t length4;
2021
+ const BYTE* const istart1 = istart + 6; /* jumpTable */
2022
+ const BYTE* const istart2 = istart1 + length1;
2023
+ const BYTE* const istart3 = istart2 + length2;
2024
+ const BYTE* const istart4 = istart3 + length3;
2025
+ const size_t segmentSize = (dstSize+3) / 4;
2026
+ BYTE* const opStart2 = ostart + segmentSize;
2027
+ BYTE* const opStart3 = opStart2 + segmentSize;
2028
+ BYTE* const opStart4 = opStart3 + segmentSize;
2029
+ BYTE* op1 = ostart;
2030
+ BYTE* op2 = opStart2;
2031
+ BYTE* op3 = opStart3;
2032
+ BYTE* op4 = opStart4;
2033
+ U32 endSignal;
2034
+
2035
+ length4 = cSrcSize - (length1 + length2 + length3 + 6);
2036
+ if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
2037
+ errorCode = BITv05_initDStream(&bitD1, istart1, length1);
2038
+ if (HUFv05_isError(errorCode)) return errorCode;
2039
+ errorCode = BITv05_initDStream(&bitD2, istart2, length2);
2040
+ if (HUFv05_isError(errorCode)) return errorCode;
2041
+ errorCode = BITv05_initDStream(&bitD3, istart3, length3);
2042
+ if (HUFv05_isError(errorCode)) return errorCode;
2043
+ errorCode = BITv05_initDStream(&bitD4, istart4, length4);
2044
+ if (HUFv05_isError(errorCode)) return errorCode;
2045
2045
 
2046
- /* 16-32 symbols per loop (4-8 symbols per stream) */
2047
- endSignal = BITv05_reloadDStream(&bitD1) | BITv05_reloadDStream(&bitD2) | BITv05_reloadDStream(&bitD3) | BITv05_reloadDStream(&bitD4);
2048
- for ( ; (endSignal==BITv05_DStream_unfinished) && (op4<(oend-7)) ; ) {
2049
- HUFv05_DECODE_SYMBOLX2_2(op1, &bitD1);
2050
- HUFv05_DECODE_SYMBOLX2_2(op2, &bitD2);
2051
- HUFv05_DECODE_SYMBOLX2_2(op3, &bitD3);
2052
- HUFv05_DECODE_SYMBOLX2_2(op4, &bitD4);
2053
- HUFv05_DECODE_SYMBOLX2_1(op1, &bitD1);
2054
- HUFv05_DECODE_SYMBOLX2_1(op2, &bitD2);
2055
- HUFv05_DECODE_SYMBOLX2_1(op3, &bitD3);
2056
- HUFv05_DECODE_SYMBOLX2_1(op4, &bitD4);
2057
- HUFv05_DECODE_SYMBOLX2_2(op1, &bitD1);
2058
- HUFv05_DECODE_SYMBOLX2_2(op2, &bitD2);
2059
- HUFv05_DECODE_SYMBOLX2_2(op3, &bitD3);
2060
- HUFv05_DECODE_SYMBOLX2_2(op4, &bitD4);
2061
- HUFv05_DECODE_SYMBOLX2_0(op1, &bitD1);
2062
- HUFv05_DECODE_SYMBOLX2_0(op2, &bitD2);
2063
- HUFv05_DECODE_SYMBOLX2_0(op3, &bitD3);
2064
- HUFv05_DECODE_SYMBOLX2_0(op4, &bitD4);
2046
+ /* 16-32 symbols per loop (4-8 symbols per stream) */
2065
2047
  endSignal = BITv05_reloadDStream(&bitD1) | BITv05_reloadDStream(&bitD2) | BITv05_reloadDStream(&bitD3) | BITv05_reloadDStream(&bitD4);
2066
- }
2048
+ for ( ; (endSignal==BITv05_DStream_unfinished) && (op4<(oend-7)) ; ) {
2049
+ HUFv05_DECODE_SYMBOLX2_2(op1, &bitD1);
2050
+ HUFv05_DECODE_SYMBOLX2_2(op2, &bitD2);
2051
+ HUFv05_DECODE_SYMBOLX2_2(op3, &bitD3);
2052
+ HUFv05_DECODE_SYMBOLX2_2(op4, &bitD4);
2053
+ HUFv05_DECODE_SYMBOLX2_1(op1, &bitD1);
2054
+ HUFv05_DECODE_SYMBOLX2_1(op2, &bitD2);
2055
+ HUFv05_DECODE_SYMBOLX2_1(op3, &bitD3);
2056
+ HUFv05_DECODE_SYMBOLX2_1(op4, &bitD4);
2057
+ HUFv05_DECODE_SYMBOLX2_2(op1, &bitD1);
2058
+ HUFv05_DECODE_SYMBOLX2_2(op2, &bitD2);
2059
+ HUFv05_DECODE_SYMBOLX2_2(op3, &bitD3);
2060
+ HUFv05_DECODE_SYMBOLX2_2(op4, &bitD4);
2061
+ HUFv05_DECODE_SYMBOLX2_0(op1, &bitD1);
2062
+ HUFv05_DECODE_SYMBOLX2_0(op2, &bitD2);
2063
+ HUFv05_DECODE_SYMBOLX2_0(op3, &bitD3);
2064
+ HUFv05_DECODE_SYMBOLX2_0(op4, &bitD4);
2065
+ endSignal = BITv05_reloadDStream(&bitD1) | BITv05_reloadDStream(&bitD2) | BITv05_reloadDStream(&bitD3) | BITv05_reloadDStream(&bitD4);
2066
+ }
2067
2067
 
2068
- /* check corruption */
2069
- if (op1 > opStart2) return ERROR(corruption_detected);
2070
- if (op2 > opStart3) return ERROR(corruption_detected);
2071
- if (op3 > opStart4) return ERROR(corruption_detected);
2072
- /* note : op4 supposed already verified within main loop */
2068
+ /* check corruption */
2069
+ if (op1 > opStart2) return ERROR(corruption_detected);
2070
+ if (op2 > opStart3) return ERROR(corruption_detected);
2071
+ if (op3 > opStart4) return ERROR(corruption_detected);
2072
+ /* note : op4 supposed already verified within main loop */
2073
2073
 
2074
- /* finish bitStreams one by one */
2075
- HUFv05_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
2076
- HUFv05_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
2077
- HUFv05_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
2078
- HUFv05_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
2074
+ /* finish bitStreams one by one */
2075
+ HUFv05_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
2076
+ HUFv05_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
2077
+ HUFv05_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
2078
+ HUFv05_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
2079
2079
 
2080
- /* check */
2081
- endSignal = BITv05_endOfDStream(&bitD1) & BITv05_endOfDStream(&bitD2) & BITv05_endOfDStream(&bitD3) & BITv05_endOfDStream(&bitD4);
2082
- if (!endSignal) return ERROR(corruption_detected);
2080
+ /* check */
2081
+ endSignal = BITv05_endOfDStream(&bitD1) & BITv05_endOfDStream(&bitD2) & BITv05_endOfDStream(&bitD3) & BITv05_endOfDStream(&bitD4);
2082
+ if (!endSignal) return ERROR(corruption_detected);
2083
2083
 
2084
- /* decoded size */
2085
- return dstSize;
2084
+ /* decoded size */
2085
+ return dstSize;
2086
+ }
2086
2087
  }
2087
2088
 
2088
2089
 
@@ -2209,7 +2210,7 @@ size_t HUFv05_readDTableX4 (unsigned* DTable, const void* src, size_t srcSize)
2209
2210
 
2210
2211
  HUFv05_STATIC_ASSERT(sizeof(HUFv05_DEltX4) == sizeof(unsigned)); /* if compilation fails here, assertion is false */
2211
2212
  if (memLog > HUFv05_ABSOLUTEMAX_TABLELOG) return ERROR(tableLog_tooLarge);
2212
- //memset(weightList, 0, sizeof(weightList)); /* is not necessary, even though some analyzer complain ... */
2213
+ /* memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */
2213
2214
 
2214
2215
  iSize = HUFv05_readStats(weightList, HUFv05_MAX_SYMBOL_VALUE + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
2215
2216
  if (HUFv05_isError(iSize)) return iSize;
@@ -2538,9 +2539,9 @@ size_t HUFv05_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cS
2538
2539
 
2539
2540
  return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
2540
2541
 
2541
- //return HUFv05_decompress4X2(dst, dstSize, cSrc, cSrcSize); /* multi-streams single-symbol decoding */
2542
- //return HUFv05_decompress4X4(dst, dstSize, cSrc, cSrcSize); /* multi-streams double-symbols decoding */
2543
- //return HUFv05_decompress4X6(dst, dstSize, cSrc, cSrcSize); /* multi-streams quad-symbols decoding */
2542
+ /* return HUFv05_decompress4X2(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams single-symbol decoding */
2543
+ /* return HUFv05_decompress4X4(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams double-symbols decoding */
2544
+ /* return HUFv05_decompress4X6(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams quad-symbols decoding */
2544
2545
  }
2545
2546
  /*
2546
2547
  zstd - standard compression library
@@ -3150,14 +3151,17 @@ static void ZSTDv05_decodeSequence(seq_t* seq, seqState_t* seqState)
3150
3151
  litLength = FSEv05_peakSymbol(&(seqState->stateLL));
3151
3152
  prevOffset = litLength ? seq->offset : seqState->prevOffset;
3152
3153
  if (litLength == MaxLL) {
3153
- U32 add = *dumps++;
3154
+ const U32 add = *dumps++;
3154
3155
  if (add < 255) litLength += add;
3155
- else {
3156
- litLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no risk : dumps is always followed by seq tables > 1 byte */
3157
- if (litLength&1) litLength>>=1, dumps += 3;
3158
- else litLength = (U16)(litLength)>>1, dumps += 2;
3156
+ else if (dumps + 2 <= de) {
3157
+ litLength = MEM_readLE16(dumps);
3158
+ dumps += 2;
3159
+ if ((litLength & 1) && dumps < de) {
3160
+ litLength += *dumps << 16;
3161
+ dumps += 1;
3162
+ }
3163
+ litLength>>=1;
3159
3164
  }
3160
- if (dumps > de) { litLength = MaxLL+255; } /* late correction, to avoid using uninitialized memory */
3161
3165
  if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
3162
3166
  }
3163
3167
 
@@ -3184,14 +3188,17 @@ static void ZSTDv05_decodeSequence(seq_t* seq, seqState_t* seqState)
3184
3188
  /* MatchLength */
3185
3189
  matchLength = FSEv05_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
3186
3190
  if (matchLength == MaxML) {
3187
- U32 add = *dumps++;
3191
+ const U32 add = dumps<de ? *dumps++ : 0;
3188
3192
  if (add < 255) matchLength += add;
3189
- else {
3190
- matchLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
3191
- if (matchLength&1) matchLength>>=1, dumps += 3;
3192
- else matchLength = (U16)(matchLength)>>1, dumps += 2;
3193
+ else if (dumps + 2 <= de) {
3194
+ matchLength = MEM_readLE16(dumps);
3195
+ dumps += 2;
3196
+ if ((matchLength & 1) && dumps < de) {
3197
+ matchLength += *dumps << 16;
3198
+ dumps += 1;
3199
+ }
3200
+ matchLength >>= 1;
3193
3201
  }
3194
- if (dumps > de) { matchLength = MaxML+255; } /* late correction, to avoid using uninitialized memory */
3195
3202
  if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
3196
3203
  }
3197
3204
  matchLength += MINMATCH;
@@ -3355,8 +3362,10 @@ static size_t ZSTDv05_decompressSequences(
3355
3362
  size_t lastLLSize = litEnd - litPtr;
3356
3363
  if (litPtr > litEnd) return ERROR(corruption_detected); /* too many literals already used */
3357
3364
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3358
- memcpy(op, litPtr, lastLLSize);
3359
- op += lastLLSize;
3365
+ if (lastLLSize > 0) {
3366
+ memcpy(op, litPtr, lastLLSize);
3367
+ op += lastLLSize;
3368
+ }
3360
3369
  }
3361
3370
 
3362
3371
  return op-ostart;
@@ -3784,7 +3793,9 @@ static size_t ZBUFFv05_blockHeaderSize = 3;
3784
3793
  static size_t ZBUFFv05_limitCopy(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3785
3794
  {
3786
3795
  size_t length = MIN(maxDstSize, srcSize);
3787
- memcpy(dst, src, length);
3796
+ if (length > 0) {
3797
+ memcpy(dst, src, length);
3798
+ }
3788
3799
  return length;
3789
3800
  }
3790
3801
 
@@ -3921,7 +3932,7 @@ size_t ZBUFFv05_decompressContinue(ZBUFFv05_DCtx* zbc, void* dst, size_t* maxDst
3921
3932
  *maxDstSizePtr = 0;
3922
3933
  return headerSize - zbc->hPos;
3923
3934
  }
3924
- // zbc->stage = ZBUFFv05ds_decodeHeader; break; /* useless : stage follows */
3935
+ /* zbc->stage = ZBUFFv05ds_decodeHeader; break; */ /* useless : stage follows */
3925
3936
  }
3926
3937
  /* fall-through */
3927
3938
  case ZBUFFv05ds_decodeHeader:
@@ -3994,7 +4005,7 @@ size_t ZBUFFv05_decompressContinue(ZBUFFv05_DCtx* zbc, void* dst, size_t* maxDst
3994
4005
  if (!decodedSize) { zbc->stage = ZBUFFv05ds_read; break; } /* this was just a header */
3995
4006
  zbc->outEnd = zbc->outStart + decodedSize;
3996
4007
  zbc->stage = ZBUFFv05ds_flush;
3997
- // break; /* ZBUFFv05ds_flush follows */
4008
+ /* break; */ /* ZBUFFv05ds_flush follows */
3998
4009
  }
3999
4010
  }
4000
4011
  /* fall-through */