zstd-ruby 1.4.0.0 → 1.4.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +35 -0
  3. data/README.md +2 -2
  4. data/ext/zstdruby/libzstd/Makefile +274 -107
  5. data/ext/zstdruby/libzstd/README.md +75 -16
  6. data/ext/zstdruby/libzstd/common/bitstream.h +59 -51
  7. data/ext/zstdruby/libzstd/common/compiler.h +154 -5
  8. data/ext/zstdruby/libzstd/common/cpu.h +1 -3
  9. data/ext/zstdruby/libzstd/common/debug.c +11 -31
  10. data/ext/zstdruby/libzstd/common/debug.h +22 -49
  11. data/ext/zstdruby/libzstd/common/entropy_common.c +201 -75
  12. data/ext/zstdruby/libzstd/common/error_private.c +3 -1
  13. data/ext/zstdruby/libzstd/common/error_private.h +7 -3
  14. data/ext/zstdruby/libzstd/common/fse.h +50 -42
  15. data/ext/zstdruby/libzstd/common/fse_decompress.c +134 -50
  16. data/ext/zstdruby/libzstd/common/huf.h +41 -38
  17. data/ext/zstdruby/libzstd/common/mem.h +68 -22
  18. data/ext/zstdruby/libzstd/common/pool.c +30 -20
  19. data/ext/zstdruby/libzstd/common/pool.h +3 -3
  20. data/ext/zstdruby/libzstd/common/threading.c +51 -4
  21. data/ext/zstdruby/libzstd/common/threading.h +36 -4
  22. data/ext/zstdruby/libzstd/common/xxhash.c +39 -89
  23. data/ext/zstdruby/libzstd/common/xxhash.h +12 -32
  24. data/ext/zstdruby/libzstd/common/zstd_common.c +10 -10
  25. data/ext/zstdruby/libzstd/common/zstd_deps.h +111 -0
  26. data/ext/zstdruby/libzstd/common/zstd_errors.h +3 -1
  27. data/ext/zstdruby/libzstd/common/zstd_internal.h +231 -72
  28. data/ext/zstdruby/libzstd/common/zstd_trace.c +42 -0
  29. data/ext/zstdruby/libzstd/common/zstd_trace.h +152 -0
  30. data/ext/zstdruby/libzstd/compress/fse_compress.c +47 -63
  31. data/ext/zstdruby/libzstd/compress/hist.c +41 -63
  32. data/ext/zstdruby/libzstd/compress/hist.h +13 -33
  33. data/ext/zstdruby/libzstd/compress/huf_compress.c +288 -172
  34. data/ext/zstdruby/libzstd/compress/zstd_compress.c +2504 -1626
  35. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +446 -85
  36. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +158 -0
  37. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +29 -0
  38. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +433 -0
  39. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.h +54 -0
  40. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +849 -0
  41. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.h +32 -0
  42. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +561 -0
  43. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +82 -60
  44. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +2 -2
  45. data/ext/zstdruby/libzstd/compress/zstd_fast.c +106 -80
  46. data/ext/zstdruby/libzstd/compress/zstd_fast.h +2 -2
  47. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +411 -105
  48. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +21 -1
  49. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +296 -207
  50. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +14 -3
  51. data/ext/zstdruby/libzstd/compress/zstd_ldm_geartab.h +103 -0
  52. data/ext/zstdruby/libzstd/compress/zstd_opt.c +260 -148
  53. data/ext/zstdruby/libzstd/compress/zstd_opt.h +1 -1
  54. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +153 -440
  55. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +29 -110
  56. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +356 -238
  57. data/ext/zstdruby/libzstd/decompress/zstd_ddict.c +20 -16
  58. data/ext/zstdruby/libzstd/decompress/zstd_ddict.h +3 -3
  59. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +641 -238
  60. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +600 -371
  61. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +8 -5
  62. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +40 -9
  63. data/ext/zstdruby/libzstd/deprecated/zbuff.h +9 -8
  64. data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +2 -2
  65. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +1 -1
  66. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +1 -1
  67. data/ext/zstdruby/libzstd/dictBuilder/cover.c +197 -78
  68. data/ext/zstdruby/libzstd/dictBuilder/cover.h +52 -7
  69. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.c +1 -1
  70. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +84 -66
  71. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +58 -36
  72. data/ext/zstdruby/libzstd/dictBuilder/zdict.h +60 -31
  73. data/ext/zstdruby/libzstd/dll/example/Makefile +2 -1
  74. data/ext/zstdruby/libzstd/dll/example/README.md +16 -22
  75. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +8 -4
  76. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +115 -111
  77. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +1 -1
  78. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +28 -14
  79. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +1 -1
  80. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +28 -14
  81. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +1 -1
  82. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +36 -19
  83. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +1 -1
  84. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +122 -107
  85. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +2 -2
  86. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +29 -23
  87. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +1 -1
  88. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +34 -24
  89. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +1 -1
  90. data/ext/zstdruby/libzstd/libzstd.pc.in +2 -1
  91. data/ext/zstdruby/libzstd/zstd.h +655 -118
  92. data/lib/zstd-ruby/version.rb +1 -1
  93. data/zstd-ruby.gemspec +1 -1
  94. metadata +20 -10
  95. data/.travis.yml +0 -14
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2021, 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-2021, 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
  /******************************************
@@ -89,7 +89,11 @@ extern "C" {
89
89
  * Basic Types
90
90
  *****************************************************************/
91
91
  #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
92
- # include <stdint.h>
92
+ # if defined(_AIX)
93
+ # include <inttypes.h>
94
+ # else
95
+ # include <stdint.h> /* intptr_t */
96
+ # endif
93
97
  typedef uint8_t BYTE;
94
98
  typedef uint16_t U16;
95
99
  typedef int16_t S16;
@@ -189,7 +193,7 @@ MEM_STATIC void MEM_write16(void* memPtr, U16 value)
189
193
  memcpy(memPtr, &value, sizeof(value));
190
194
  }
191
195
 
192
- #endif // MEM_FORCE_MEMORY_ACCESS
196
+ #endif /* MEM_FORCE_MEMORY_ACCESS */
193
197
 
194
198
 
195
199
  MEM_STATIC U16 MEM_readLE16(const void* memPtr)
@@ -217,6 +221,11 @@ MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
217
221
  }
218
222
  }
219
223
 
224
+ MEM_STATIC U32 MEM_readLE24(const void* memPtr)
225
+ {
226
+ return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
227
+ }
228
+
220
229
  MEM_STATIC U32 MEM_readLE32(const void* memPtr)
221
230
  {
222
231
  if (MEM_isLittleEndian())
@@ -348,7 +357,7 @@ MEM_STATIC unsigned BIT_highbit32 (U32 val)
348
357
  _BitScanReverse ( &r, val );
349
358
  return (unsigned) r;
350
359
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
351
- return 31 - __builtin_clz (val);
360
+ return __builtin_clz (val) ^ 31;
352
361
  # else /* Software version */
353
362
  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
363
  U32 v = val;
@@ -2831,7 +2840,9 @@ static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockPropertie
2831
2840
  static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2832
2841
  {
2833
2842
  if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
2834
- memcpy(dst, src, srcSize);
2843
+ if (srcSize > 0) {
2844
+ memcpy(dst, src, srcSize);
2845
+ }
2835
2846
  return srcSize;
2836
2847
  }
2837
2848
 
@@ -2884,6 +2895,7 @@ static size_t ZSTD_decodeLiteralsBlock(void* ctx,
2884
2895
  const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2885
2896
  if (litSize > srcSize-11) /* risk of reading too far with wildcopy */
2886
2897
  {
2898
+ if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2887
2899
  if (litSize > srcSize-3) return ERROR(corruption_detected);
2888
2900
  memcpy(dctx->litBuffer, istart, litSize);
2889
2901
  dctx->litPtr = dctx->litBuffer;
@@ -3043,11 +3055,11 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
3043
3055
  seqState->prevOffset = seq->offset;
3044
3056
  if (litLength == MaxLL)
3045
3057
  {
3046
- U32 add = *dumps++;
3058
+ const U32 add = dumps<de ? *dumps++ : 0;
3047
3059
  if (add < 255) litLength += add;
3048
- else
3060
+ else if (dumps + 3 <= de)
3049
3061
  {
3050
- litLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
3062
+ litLength = MEM_readLE24(dumps);
3051
3063
  dumps += 3;
3052
3064
  }
3053
3065
  if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
@@ -3073,11 +3085,11 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
3073
3085
  matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
3074
3086
  if (matchLength == MaxML)
3075
3087
  {
3076
- U32 add = *dumps++;
3088
+ const U32 add = dumps<de ? *dumps++ : 0;
3077
3089
  if (add < 255) matchLength += add;
3078
- else
3090
+ else if (dumps + 3 <= de)
3079
3091
  {
3080
- matchLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
3092
+ matchLength = MEM_readLE24(dumps);
3081
3093
  dumps += 3;
3082
3094
  }
3083
3095
  if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
@@ -3223,8 +3235,10 @@ static size_t ZSTD_decompressSequences(
3223
3235
  size_t lastLLSize = litEnd - litPtr;
3224
3236
  if (litPtr > litEnd) return ERROR(corruption_detected);
3225
3237
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3226
- if (op != litPtr) memmove(op, litPtr, lastLLSize);
3227
- op += lastLLSize;
3238
+ if (lastLLSize > 0) {
3239
+ if (op != litPtr) memmove(op, litPtr, lastLLSize);
3240
+ op += lastLLSize;
3241
+ }
3228
3242
  }
3229
3243
  }
3230
3244
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2021, 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-2021, 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
  /******************************************
@@ -90,7 +90,11 @@ extern "C" {
90
90
  * Basic Types
91
91
  *****************************************************************/
92
92
  #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
93
- # include <stdint.h>
93
+ # if defined(_AIX)
94
+ # include <inttypes.h>
95
+ # else
96
+ # include <stdint.h> /* intptr_t */
97
+ # endif
94
98
  typedef uint8_t BYTE;
95
99
  typedef uint16_t U16;
96
100
  typedef int16_t S16;
@@ -191,7 +195,7 @@ MEM_STATIC void MEM_write16(void* memPtr, U16 value)
191
195
  }
192
196
 
193
197
 
194
- #endif // MEM_FORCE_MEMORY_ACCESS
198
+ #endif /* MEM_FORCE_MEMORY_ACCESS */
195
199
 
196
200
 
197
201
  MEM_STATIC U16 MEM_readLE16(const void* memPtr)
@@ -219,6 +223,11 @@ MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
219
223
  }
220
224
  }
221
225
 
226
+ MEM_STATIC U32 MEM_readLE24(const void* memPtr)
227
+ {
228
+ return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
229
+ }
230
+
222
231
  MEM_STATIC U32 MEM_readLE32(const void* memPtr)
223
232
  {
224
233
  if (MEM_isLittleEndian())
@@ -351,7 +360,7 @@ MEM_STATIC unsigned BIT_highbit32 (U32 val)
351
360
  _BitScanReverse ( &r, val );
352
361
  return (unsigned) r;
353
362
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
354
- return 31 - __builtin_clz (val);
363
+ return __builtin_clz (val) ^ 31;
355
364
  # else /* Software version */
356
365
  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
366
  U32 v = val;
@@ -2472,7 +2481,9 @@ static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockPropertie
2472
2481
  static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2473
2482
  {
2474
2483
  if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
2475
- memcpy(dst, src, srcSize);
2484
+ if (srcSize > 0) {
2485
+ memcpy(dst, src, srcSize);
2486
+ }
2476
2487
  return srcSize;
2477
2488
  }
2478
2489
 
@@ -2525,6 +2536,7 @@ static size_t ZSTD_decodeLiteralsBlock(void* ctx,
2525
2536
  const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2526
2537
  if (litSize > srcSize-11) /* risk of reading too far with wildcopy */
2527
2538
  {
2539
+ if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2528
2540
  if (litSize > srcSize-3) return ERROR(corruption_detected);
2529
2541
  memcpy(dctx->litBuffer, istart, litSize);
2530
2542
  dctx->litPtr = dctx->litBuffer;
@@ -2684,11 +2696,11 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
2684
2696
  seqState->prevOffset = seq->offset;
2685
2697
  if (litLength == MaxLL)
2686
2698
  {
2687
- U32 add = *dumps++;
2699
+ const U32 add = dumps<de ? *dumps++ : 0;
2688
2700
  if (add < 255) litLength += add;
2689
- else
2701
+ else if (dumps + 3 <= de)
2690
2702
  {
2691
- litLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
2703
+ litLength = MEM_readLE24(dumps);
2692
2704
  dumps += 3;
2693
2705
  }
2694
2706
  if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
@@ -2714,11 +2726,11 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
2714
2726
  matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
2715
2727
  if (matchLength == MaxML)
2716
2728
  {
2717
- U32 add = *dumps++;
2729
+ const U32 add = dumps<de ? *dumps++ : 0;
2718
2730
  if (add < 255) matchLength += add;
2719
- else
2731
+ else if (dumps + 3 <= de)
2720
2732
  {
2721
- matchLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
2733
+ matchLength = MEM_readLE24(dumps);
2722
2734
  dumps += 3;
2723
2735
  }
2724
2736
  if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
@@ -2864,8 +2876,10 @@ static size_t ZSTD_decompressSequences(
2864
2876
  size_t lastLLSize = litEnd - litPtr;
2865
2877
  if (litPtr > litEnd) return ERROR(corruption_detected);
2866
2878
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
2867
- if (op != litPtr) memmove(op, litPtr, lastLLSize);
2868
- op += lastLLSize;
2879
+ if (lastLLSize > 0) {
2880
+ if (op != litPtr) memmove(op, litPtr, lastLLSize);
2881
+ op += lastLLSize;
2882
+ }
2869
2883
  }
2870
2884
  }
2871
2885
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2021, 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-2021, 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
  /* ******************************************************************
@@ -52,7 +52,11 @@ extern "C" {
52
52
  * Basic Types
53
53
  *****************************************************************/
54
54
  #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
55
- # include <stdint.h>
55
+ # if defined(_AIX)
56
+ # include <inttypes.h>
57
+ # else
58
+ # include <stdint.h> /* intptr_t */
59
+ # endif
56
60
  typedef uint8_t BYTE;
57
61
  typedef uint16_t U16;
58
62
  typedef int16_t S16;
@@ -74,7 +78,7 @@ extern "C" {
74
78
  /*-*************************************
75
79
  * Debug
76
80
  ***************************************/
77
- #include "debug.h"
81
+ #include "../common/debug.h"
78
82
  #ifndef assert
79
83
  # define assert(condition) ((void)0)
80
84
  #endif
@@ -161,7 +165,7 @@ MEM_STATIC void MEM_write16(void* memPtr, U16 value)
161
165
  memcpy(memPtr, &value, sizeof(value));
162
166
  }
163
167
 
164
- #endif // MEM_FORCE_MEMORY_ACCESS
168
+ #endif /* MEM_FORCE_MEMORY_ACCESS */
165
169
 
166
170
 
167
171
  MEM_STATIC U16 MEM_readLE16(const void* memPtr)
@@ -189,6 +193,11 @@ MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
189
193
  }
190
194
  }
191
195
 
196
+ MEM_STATIC U32 MEM_readLE24(const void* memPtr)
197
+ {
198
+ return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
199
+ }
200
+
192
201
  MEM_STATIC U32 MEM_readLE32(const void* memPtr)
193
202
  {
194
203
  if (MEM_isLittleEndian())
@@ -622,7 +631,7 @@ MEM_STATIC unsigned BIT_highbit32 (U32 val)
622
631
  _BitScanReverse ( &r, val );
623
632
  return (unsigned) r;
624
633
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
625
- return 31 - __builtin_clz (val);
634
+ return __builtin_clz (val) ^ 31;
626
635
  # else /* Software version */
627
636
  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
637
  U32 v = val;
@@ -2598,7 +2607,9 @@ static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockPropertie
2598
2607
  static size_t ZSTD_copyRawBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2599
2608
  {
2600
2609
  if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
2601
- memcpy(dst, src, srcSize);
2610
+ if (srcSize > 0) {
2611
+ memcpy(dst, src, srcSize);
2612
+ }
2602
2613
  return srcSize;
2603
2614
  }
2604
2615
 
@@ -2650,6 +2661,7 @@ static size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
2650
2661
  const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2651
2662
  if (litSize > srcSize-11) /* risk of reading too far with wildcopy */
2652
2663
  {
2664
+ if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2653
2665
  if (litSize > srcSize-3) return ERROR(corruption_detected);
2654
2666
  memcpy(dctx->litBuffer, istart, litSize);
2655
2667
  dctx->litPtr = dctx->litBuffer;
@@ -2808,13 +2820,12 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
2808
2820
  litLength = FSE_decodeSymbol(&(seqState->stateLL), &(seqState->DStream));
2809
2821
  prevOffset = litLength ? seq->offset : seqState->prevOffset;
2810
2822
  if (litLength == MaxLL) {
2811
- U32 add = *dumps++;
2823
+ const U32 add = dumps<de ? *dumps++ : 0;
2812
2824
  if (add < 255) litLength += add;
2813
- else {
2814
- litLength = dumps[0] + (dumps[1]<<8) + (dumps[2]<<16);
2825
+ else if (dumps + 3 <= de) {
2826
+ litLength = MEM_readLE24(dumps);
2815
2827
  dumps += 3;
2816
2828
  }
2817
- if (dumps > de) { litLength = MaxLL+255; } /* late correction, to avoid using uninitialized memory */
2818
2829
  if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
2819
2830
  }
2820
2831
 
@@ -2837,13 +2848,12 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
2837
2848
  /* MatchLength */
2838
2849
  matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
2839
2850
  if (matchLength == MaxML) {
2840
- U32 add = *dumps++;
2851
+ const U32 add = dumps<de ? *dumps++ : 0;
2841
2852
  if (add < 255) matchLength += add;
2842
- else {
2843
- matchLength = dumps[0] + (dumps[1]<<8) + (dumps[2]<<16);
2853
+ else if (dumps + 3 <= de){
2854
+ matchLength = MEM_readLE24(dumps);
2844
2855
  dumps += 3;
2845
2856
  }
2846
- if (dumps > de) { matchLength = MaxML+255; } /* late correction, to avoid using uninitialized memory */
2847
2857
  if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
2848
2858
  }
2849
2859
  matchLength += MINMATCH;
@@ -3004,8 +3014,10 @@ static size_t ZSTD_decompressSequences(
3004
3014
  size_t lastLLSize = litEnd - litPtr;
3005
3015
  if (litPtr > litEnd) return ERROR(corruption_detected);
3006
3016
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3007
- if (op != litPtr) memcpy(op, litPtr, lastLLSize);
3008
- op += lastLLSize;
3017
+ if (lastLLSize > 0) {
3018
+ if (op != litPtr) memcpy(op, litPtr, lastLLSize);
3019
+ op += lastLLSize;
3020
+ }
3009
3021
  }
3010
3022
  }
3011
3023
 
@@ -3031,9 +3043,12 @@ static size_t ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx,
3031
3043
  {
3032
3044
  /* blockType == blockCompressed */
3033
3045
  const BYTE* ip = (const BYTE*)src;
3046
+ size_t litCSize;
3047
+
3048
+ if (srcSize > BLOCKSIZE) return ERROR(corruption_detected);
3034
3049
 
3035
3050
  /* Decode literals sub-block */
3036
- size_t litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);
3051
+ litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);
3037
3052
  if (ZSTD_isError(litCSize)) return litCSize;
3038
3053
  ip += litCSize;
3039
3054
  srcSize -= litCSize;
@@ -3400,7 +3415,9 @@ static size_t ZBUFF_decompressWithDictionary(ZBUFF_DCtx* zbc, const void* src, s
3400
3415
  static size_t ZBUFF_limitCopy(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3401
3416
  {
3402
3417
  size_t length = MIN(maxDstSize, srcSize);
3403
- memcpy(dst, src, length);
3418
+ if (length > 0) {
3419
+ memcpy(dst, src, length);
3420
+ }
3404
3421
  return length;
3405
3422
  }
3406
3423
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2021, 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-2021, 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
  /* ******************************************************************
@@ -80,7 +80,11 @@ extern "C" {
80
80
  * Basic Types
81
81
  *****************************************************************/
82
82
  #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
83
- # include <stdint.h>
83
+ # if defined(_AIX)
84
+ # include <inttypes.h>
85
+ # else
86
+ # include <stdint.h> /* intptr_t */
87
+ # endif
84
88
  typedef uint8_t BYTE;
85
89
  typedef uint16_t U16;
86
90
  typedef int16_t S16;
@@ -756,7 +760,7 @@ MEM_STATIC unsigned BITv05_highbit32 (U32 val)
756
760
  _BitScanReverse ( &r, val );
757
761
  return (unsigned) r;
758
762
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
759
- return 31 - __builtin_clz (val);
763
+ return __builtin_clz (val) ^ 31;
760
764
  # else /* Software version */
761
765
  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
766
  U32 v = val;
@@ -1804,7 +1808,7 @@ static size_t HUFv05_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
1804
1808
 
1805
1809
  if (!srcSize) return ERROR(srcSize_wrong);
1806
1810
  iSize = ip[0];
1807
- //memset(huffWeight, 0, hwSize); /* is not necessary, even though some analyzer complain ... */
1811
+ /* memset(huffWeight, 0, hwSize); */ /* is not necessary, even though some analyzer complain ... */
1808
1812
 
1809
1813
  if (iSize >= 128) { /* special header */
1810
1814
  if (iSize >= (242)) { /* RLE */
@@ -1879,7 +1883,7 @@ size_t HUFv05_readDTableX2 (U16* DTable, const void* src, size_t srcSize)
1879
1883
  HUFv05_DEltX2* const dt = (HUFv05_DEltX2*)dtPtr;
1880
1884
 
1881
1885
  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 ... */
1886
+ /* memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */
1883
1887
 
1884
1888
  iSize = HUFv05_readStats(huffWeight, HUFv05_MAX_SYMBOL_VALUE + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
1885
1889
  if (HUFv05_isError(iSize)) return iSize;
@@ -1998,91 +2002,92 @@ size_t HUFv05_decompress4X2_usingDTable(
1998
2002
  const void* cSrc, size_t cSrcSize,
1999
2003
  const U16* DTable)
2000
2004
  {
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
2005
  /* Check */
2033
2006
  if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
2007
+ {
2008
+ const BYTE* const istart = (const BYTE*) cSrc;
2009
+ BYTE* const ostart = (BYTE*) dst;
2010
+ BYTE* const oend = ostart + dstSize;
2011
+ const void* const dtPtr = DTable;
2012
+ const HUFv05_DEltX2* const dt = ((const HUFv05_DEltX2*)dtPtr) +1;
2013
+ const U32 dtLog = DTable[0];
2014
+ size_t errorCode;
2034
2015
 
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;
2016
+ /* Init */
2017
+ BITv05_DStream_t bitD1;
2018
+ BITv05_DStream_t bitD2;
2019
+ BITv05_DStream_t bitD3;
2020
+ BITv05_DStream_t bitD4;
2021
+ const size_t length1 = MEM_readLE16(istart);
2022
+ const size_t length2 = MEM_readLE16(istart+2);
2023
+ const size_t length3 = MEM_readLE16(istart+4);
2024
+ size_t length4;
2025
+ const BYTE* const istart1 = istart + 6; /* jumpTable */
2026
+ const BYTE* const istart2 = istart1 + length1;
2027
+ const BYTE* const istart3 = istart2 + length2;
2028
+ const BYTE* const istart4 = istart3 + length3;
2029
+ const size_t segmentSize = (dstSize+3) / 4;
2030
+ BYTE* const opStart2 = ostart + segmentSize;
2031
+ BYTE* const opStart3 = opStart2 + segmentSize;
2032
+ BYTE* const opStart4 = opStart3 + segmentSize;
2033
+ BYTE* op1 = ostart;
2034
+ BYTE* op2 = opStart2;
2035
+ BYTE* op3 = opStart3;
2036
+ BYTE* op4 = opStart4;
2037
+ U32 endSignal;
2038
+
2039
+ length4 = cSrcSize - (length1 + length2 + length3 + 6);
2040
+ if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
2041
+ errorCode = BITv05_initDStream(&bitD1, istart1, length1);
2042
+ if (HUFv05_isError(errorCode)) return errorCode;
2043
+ errorCode = BITv05_initDStream(&bitD2, istart2, length2);
2044
+ if (HUFv05_isError(errorCode)) return errorCode;
2045
+ errorCode = BITv05_initDStream(&bitD3, istart3, length3);
2046
+ if (HUFv05_isError(errorCode)) return errorCode;
2047
+ errorCode = BITv05_initDStream(&bitD4, istart4, length4);
2048
+ if (HUFv05_isError(errorCode)) return errorCode;
2045
2049
 
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);
2050
+ /* 16-32 symbols per loop (4-8 symbols per stream) */
2065
2051
  endSignal = BITv05_reloadDStream(&bitD1) | BITv05_reloadDStream(&bitD2) | BITv05_reloadDStream(&bitD3) | BITv05_reloadDStream(&bitD4);
2066
- }
2052
+ for ( ; (endSignal==BITv05_DStream_unfinished) && (op4<(oend-7)) ; ) {
2053
+ HUFv05_DECODE_SYMBOLX2_2(op1, &bitD1);
2054
+ HUFv05_DECODE_SYMBOLX2_2(op2, &bitD2);
2055
+ HUFv05_DECODE_SYMBOLX2_2(op3, &bitD3);
2056
+ HUFv05_DECODE_SYMBOLX2_2(op4, &bitD4);
2057
+ HUFv05_DECODE_SYMBOLX2_1(op1, &bitD1);
2058
+ HUFv05_DECODE_SYMBOLX2_1(op2, &bitD2);
2059
+ HUFv05_DECODE_SYMBOLX2_1(op3, &bitD3);
2060
+ HUFv05_DECODE_SYMBOLX2_1(op4, &bitD4);
2061
+ HUFv05_DECODE_SYMBOLX2_2(op1, &bitD1);
2062
+ HUFv05_DECODE_SYMBOLX2_2(op2, &bitD2);
2063
+ HUFv05_DECODE_SYMBOLX2_2(op3, &bitD3);
2064
+ HUFv05_DECODE_SYMBOLX2_2(op4, &bitD4);
2065
+ HUFv05_DECODE_SYMBOLX2_0(op1, &bitD1);
2066
+ HUFv05_DECODE_SYMBOLX2_0(op2, &bitD2);
2067
+ HUFv05_DECODE_SYMBOLX2_0(op3, &bitD3);
2068
+ HUFv05_DECODE_SYMBOLX2_0(op4, &bitD4);
2069
+ endSignal = BITv05_reloadDStream(&bitD1) | BITv05_reloadDStream(&bitD2) | BITv05_reloadDStream(&bitD3) | BITv05_reloadDStream(&bitD4);
2070
+ }
2067
2071
 
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 */
2072
+ /* check corruption */
2073
+ if (op1 > opStart2) return ERROR(corruption_detected);
2074
+ if (op2 > opStart3) return ERROR(corruption_detected);
2075
+ if (op3 > opStart4) return ERROR(corruption_detected);
2076
+ /* note : op4 supposed already verified within main loop */
2073
2077
 
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);
2078
+ /* finish bitStreams one by one */
2079
+ HUFv05_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
2080
+ HUFv05_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
2081
+ HUFv05_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
2082
+ HUFv05_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
2079
2083
 
2080
- /* check */
2081
- endSignal = BITv05_endOfDStream(&bitD1) & BITv05_endOfDStream(&bitD2) & BITv05_endOfDStream(&bitD3) & BITv05_endOfDStream(&bitD4);
2082
- if (!endSignal) return ERROR(corruption_detected);
2084
+ /* check */
2085
+ endSignal = BITv05_endOfDStream(&bitD1) & BITv05_endOfDStream(&bitD2) & BITv05_endOfDStream(&bitD3) & BITv05_endOfDStream(&bitD4);
2086
+ if (!endSignal) return ERROR(corruption_detected);
2083
2087
 
2084
- /* decoded size */
2085
- return dstSize;
2088
+ /* decoded size */
2089
+ return dstSize;
2090
+ }
2086
2091
  }
2087
2092
 
2088
2093
 
@@ -2209,7 +2214,7 @@ size_t HUFv05_readDTableX4 (unsigned* DTable, const void* src, size_t srcSize)
2209
2214
 
2210
2215
  HUFv05_STATIC_ASSERT(sizeof(HUFv05_DEltX4) == sizeof(unsigned)); /* if compilation fails here, assertion is false */
2211
2216
  if (memLog > HUFv05_ABSOLUTEMAX_TABLELOG) return ERROR(tableLog_tooLarge);
2212
- //memset(weightList, 0, sizeof(weightList)); /* is not necessary, even though some analyzer complain ... */
2217
+ /* memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */
2213
2218
 
2214
2219
  iSize = HUFv05_readStats(weightList, HUFv05_MAX_SYMBOL_VALUE + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
2215
2220
  if (HUFv05_isError(iSize)) return iSize;
@@ -2538,9 +2543,9 @@ size_t HUFv05_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cS
2538
2543
 
2539
2544
  return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
2540
2545
 
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 */
2546
+ /* return HUFv05_decompress4X2(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams single-symbol decoding */
2547
+ /* return HUFv05_decompress4X4(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams double-symbols decoding */
2548
+ /* return HUFv05_decompress4X6(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams quad-symbols decoding */
2544
2549
  }
2545
2550
  /*
2546
2551
  zstd - standard compression library
@@ -2828,7 +2833,7 @@ static size_t ZSTDv05_decodeFrameHeader_Part2(ZSTDv05_DCtx* zc, const void* src,
2828
2833
 
2829
2834
  static size_t ZSTDv05_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
2830
2835
  {
2831
- const BYTE* const in = (const BYTE* const)src;
2836
+ const BYTE* const in = (const BYTE*)src;
2832
2837
  BYTE headerFlags;
2833
2838
  U32 cSize;
2834
2839
 
@@ -2997,7 +3002,7 @@ static size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t
2997
3002
  FSEv05_DTable* DTableLL, FSEv05_DTable* DTableML, FSEv05_DTable* DTableOffb,
2998
3003
  const void* src, size_t srcSize, U32 flagStaticTable)
2999
3004
  {
3000
- const BYTE* const istart = (const BYTE* const)src;
3005
+ const BYTE* const istart = (const BYTE*)src;
3001
3006
  const BYTE* ip = istart;
3002
3007
  const BYTE* const iend = istart + srcSize;
3003
3008
  U32 LLtype, Offtype, MLtype;
@@ -3150,14 +3155,17 @@ static void ZSTDv05_decodeSequence(seq_t* seq, seqState_t* seqState)
3150
3155
  litLength = FSEv05_peakSymbol(&(seqState->stateLL));
3151
3156
  prevOffset = litLength ? seq->offset : seqState->prevOffset;
3152
3157
  if (litLength == MaxLL) {
3153
- U32 add = *dumps++;
3158
+ const U32 add = *dumps++;
3154
3159
  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;
3160
+ else if (dumps + 2 <= de) {
3161
+ litLength = MEM_readLE16(dumps);
3162
+ dumps += 2;
3163
+ if ((litLength & 1) && dumps < de) {
3164
+ litLength += *dumps << 16;
3165
+ dumps += 1;
3166
+ }
3167
+ litLength>>=1;
3159
3168
  }
3160
- if (dumps > de) { litLength = MaxLL+255; } /* late correction, to avoid using uninitialized memory */
3161
3169
  if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
3162
3170
  }
3163
3171
 
@@ -3184,14 +3192,17 @@ static void ZSTDv05_decodeSequence(seq_t* seq, seqState_t* seqState)
3184
3192
  /* MatchLength */
3185
3193
  matchLength = FSEv05_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
3186
3194
  if (matchLength == MaxML) {
3187
- U32 add = *dumps++;
3195
+ const U32 add = dumps<de ? *dumps++ : 0;
3188
3196
  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;
3197
+ else if (dumps + 2 <= de) {
3198
+ matchLength = MEM_readLE16(dumps);
3199
+ dumps += 2;
3200
+ if ((matchLength & 1) && dumps < de) {
3201
+ matchLength += *dumps << 16;
3202
+ dumps += 1;
3203
+ }
3204
+ matchLength >>= 1;
3193
3205
  }
3194
- if (dumps > de) { matchLength = MaxML+255; } /* late correction, to avoid using uninitialized memory */
3195
3206
  if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
3196
3207
  }
3197
3208
  matchLength += MINMATCH;
@@ -3299,7 +3310,7 @@ static size_t ZSTDv05_decompressSequences(
3299
3310
  {
3300
3311
  const BYTE* ip = (const BYTE*)seqStart;
3301
3312
  const BYTE* const iend = ip + seqSize;
3302
- BYTE* const ostart = (BYTE* const)dst;
3313
+ BYTE* const ostart = (BYTE*)dst;
3303
3314
  BYTE* op = ostart;
3304
3315
  BYTE* const oend = ostart + maxDstSize;
3305
3316
  size_t errorCode, dumpsLength=0;
@@ -3355,8 +3366,10 @@ static size_t ZSTDv05_decompressSequences(
3355
3366
  size_t lastLLSize = litEnd - litPtr;
3356
3367
  if (litPtr > litEnd) return ERROR(corruption_detected); /* too many literals already used */
3357
3368
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3358
- memcpy(op, litPtr, lastLLSize);
3359
- op += lastLLSize;
3369
+ if (lastLLSize > 0) {
3370
+ memcpy(op, litPtr, lastLLSize);
3371
+ op += lastLLSize;
3372
+ }
3360
3373
  }
3361
3374
 
3362
3375
  return op-ostart;
@@ -3410,7 +3423,7 @@ static size_t ZSTDv05_decompress_continueDCtx(ZSTDv05_DCtx* dctx,
3410
3423
  {
3411
3424
  const BYTE* ip = (const BYTE*)src;
3412
3425
  const BYTE* iend = ip + srcSize;
3413
- BYTE* const ostart = (BYTE* const)dst;
3426
+ BYTE* const ostart = (BYTE*)dst;
3414
3427
  BYTE* op = ostart;
3415
3428
  BYTE* const oend = ostart + maxDstSize;
3416
3429
  size_t remainingSize = srcSize;
@@ -3784,7 +3797,9 @@ static size_t ZBUFFv05_blockHeaderSize = 3;
3784
3797
  static size_t ZBUFFv05_limitCopy(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3785
3798
  {
3786
3799
  size_t length = MIN(maxDstSize, srcSize);
3787
- memcpy(dst, src, length);
3800
+ if (length > 0) {
3801
+ memcpy(dst, src, length);
3802
+ }
3788
3803
  return length;
3789
3804
  }
3790
3805
 
@@ -3921,7 +3936,7 @@ size_t ZBUFFv05_decompressContinue(ZBUFFv05_DCtx* zbc, void* dst, size_t* maxDst
3921
3936
  *maxDstSizePtr = 0;
3922
3937
  return headerSize - zbc->hPos;
3923
3938
  }
3924
- // zbc->stage = ZBUFFv05ds_decodeHeader; break; /* useless : stage follows */
3939
+ /* zbc->stage = ZBUFFv05ds_decodeHeader; break; */ /* useless : stage follows */
3925
3940
  }
3926
3941
  /* fall-through */
3927
3942
  case ZBUFFv05ds_decodeHeader:
@@ -3994,7 +4009,7 @@ size_t ZBUFFv05_decompressContinue(ZBUFFv05_DCtx* zbc, void* dst, size_t* maxDst
3994
4009
  if (!decodedSize) { zbc->stage = ZBUFFv05ds_read; break; } /* this was just a header */
3995
4010
  zbc->outEnd = zbc->outStart + decodedSize;
3996
4011
  zbc->stage = ZBUFFv05ds_flush;
3997
- // break; /* ZBUFFv05ds_flush follows */
4012
+ /* break; */ /* ZBUFFv05ds_flush follows */
3998
4013
  }
3999
4014
  }
4000
4015
  /* fall-through */