zstd-ruby 1.4.1.0 → 1.5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +8 -0
  3. data/.github/workflows/ruby.yml +35 -0
  4. data/README.md +2 -2
  5. data/ext/zstdruby/libzstd/BUCK +5 -7
  6. data/ext/zstdruby/libzstd/Makefile +304 -113
  7. data/ext/zstdruby/libzstd/README.md +83 -20
  8. data/ext/zstdruby/libzstd/common/bitstream.h +59 -51
  9. data/ext/zstdruby/libzstd/common/compiler.h +150 -8
  10. data/ext/zstdruby/libzstd/common/cpu.h +1 -3
  11. data/ext/zstdruby/libzstd/common/debug.c +11 -31
  12. data/ext/zstdruby/libzstd/common/debug.h +22 -49
  13. data/ext/zstdruby/libzstd/common/entropy_common.c +201 -75
  14. data/ext/zstdruby/libzstd/common/error_private.c +3 -1
  15. data/ext/zstdruby/libzstd/common/error_private.h +8 -4
  16. data/ext/zstdruby/libzstd/common/fse.h +50 -42
  17. data/ext/zstdruby/libzstd/common/fse_decompress.c +149 -55
  18. data/ext/zstdruby/libzstd/common/huf.h +43 -39
  19. data/ext/zstdruby/libzstd/common/mem.h +69 -25
  20. data/ext/zstdruby/libzstd/common/pool.c +30 -20
  21. data/ext/zstdruby/libzstd/common/pool.h +3 -3
  22. data/ext/zstdruby/libzstd/common/threading.c +51 -4
  23. data/ext/zstdruby/libzstd/common/threading.h +36 -4
  24. data/ext/zstdruby/libzstd/common/xxhash.c +40 -92
  25. data/ext/zstdruby/libzstd/common/xxhash.h +12 -32
  26. data/ext/zstdruby/libzstd/common/zstd_common.c +10 -10
  27. data/ext/zstdruby/libzstd/common/zstd_deps.h +111 -0
  28. data/ext/zstdruby/libzstd/common/zstd_internal.h +230 -111
  29. data/ext/zstdruby/libzstd/common/zstd_trace.h +154 -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 +332 -193
  34. data/ext/zstdruby/libzstd/compress/zstd_compress.c +3614 -1696
  35. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +546 -86
  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 +441 -0
  39. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.h +54 -0
  40. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +572 -0
  41. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.h +32 -0
  42. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +662 -0
  43. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +43 -41
  44. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +2 -2
  45. data/ext/zstdruby/libzstd/compress/zstd_fast.c +85 -80
  46. data/ext/zstdruby/libzstd/compress/zstd_fast.h +2 -2
  47. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +1184 -111
  48. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +59 -1
  49. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +333 -208
  50. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +15 -3
  51. data/ext/zstdruby/libzstd/compress/zstd_ldm_geartab.h +103 -0
  52. data/ext/zstdruby/libzstd/compress/zstd_opt.c +228 -129
  53. data/ext/zstdruby/libzstd/compress/zstd_opt.h +1 -1
  54. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +151 -440
  55. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +32 -114
  56. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +395 -276
  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 +630 -231
  60. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +606 -380
  61. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +8 -5
  62. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +39 -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 +55 -46
  68. data/ext/zstdruby/libzstd/dictBuilder/cover.h +20 -9
  69. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.c +1 -1
  70. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +43 -31
  71. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +53 -30
  72. data/ext/zstdruby/libzstd/dll/example/Makefile +2 -1
  73. data/ext/zstdruby/libzstd/dll/example/README.md +16 -22
  74. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +4 -4
  75. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +24 -14
  76. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +1 -1
  77. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +17 -8
  78. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +1 -1
  79. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +17 -8
  80. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +1 -1
  81. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +25 -11
  82. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +1 -1
  83. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +43 -32
  84. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +2 -2
  85. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +27 -19
  86. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +1 -1
  87. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +32 -20
  88. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +1 -1
  89. data/ext/zstdruby/libzstd/libzstd.pc.in +2 -1
  90. data/ext/zstdruby/libzstd/{dictBuilder/zdict.h → zdict.h} +201 -31
  91. data/ext/zstdruby/libzstd/zstd.h +740 -153
  92. data/ext/zstdruby/libzstd/{common/zstd_errors.h → zstd_errors.h} +3 -1
  93. data/lib/zstd-ruby/version.rb +1 -1
  94. data/zstd-ruby.gemspec +1 -1
  95. metadata +21 -10
  96. data/.travis.yml +0 -14
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 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
@@ -18,9 +18,9 @@ extern "C" {
18
18
  /* *************************************
19
19
  * Includes
20
20
  ***************************************/
21
- #include "mem.h" /* MEM_STATIC */
22
- #include "error_private.h" /* ERROR */
23
- #include "zstd_internal.h" /* ZSTD_inBuffer, ZSTD_outBuffer, ZSTD_frameSizeInfo */
21
+ #include "../common/mem.h" /* MEM_STATIC */
22
+ #include "../common/error_private.h" /* ERROR */
23
+ #include "../common/zstd_internal.h" /* ZSTD_inBuffer, ZSTD_outBuffer, ZSTD_frameSizeInfo */
24
24
 
25
25
  #if !defined (ZSTD_LEGACY_SUPPORT) || (ZSTD_LEGACY_SUPPORT == 0)
26
26
  # undef ZSTD_LEGACY_SUPPORT
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 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
@@ -14,7 +14,7 @@
14
14
  ******************************************/
15
15
  #include <stddef.h> /* size_t, ptrdiff_t */
16
16
  #include "zstd_v01.h"
17
- #include "error_private.h"
17
+ #include "../common/error_private.h"
18
18
 
19
19
 
20
20
  /******************************************
@@ -257,7 +257,7 @@ static U64 FSE_read64(const void* memPtr)
257
257
  U64 val; memcpy(&val, memPtr, sizeof(val)); return val;
258
258
  }
259
259
 
260
- #endif // FSE_FORCE_MEMORY_ACCESS
260
+ #endif /* FSE_FORCE_MEMORY_ACCESS */
261
261
 
262
262
  static U16 FSE_readLE16(const void* memPtr)
263
263
  {
@@ -346,7 +346,7 @@ FORCE_INLINE unsigned FSE_highbit32 (U32 val)
346
346
  _BitScanReverse ( &r, val );
347
347
  return (unsigned) r;
348
348
  # elif defined(__GNUC__) && (GCC_VERSION >= 304) /* GCC Intrinsic */
349
- return 31 - __builtin_clz (val);
349
+ return __builtin_clz (val) ^ 31;
350
350
  # else /* Software version */
351
351
  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 };
352
352
  U32 v = val;
@@ -1078,7 +1078,7 @@ static size_t HUF_decompress_usingDTable( /* -3% slower when non static */
1078
1078
  BYTE* const ostart = (BYTE*) dst;
1079
1079
  BYTE* op = ostart;
1080
1080
  BYTE* const omax = op + maxDstSize;
1081
- BYTE* const olimit = omax-15;
1081
+ BYTE* const olimit = maxDstSize < 15 ? op : omax-15;
1082
1082
 
1083
1083
  const void* ptr = DTable;
1084
1084
  const HUF_DElt* const dt = (const HUF_DElt*)(ptr)+1;
@@ -1092,7 +1092,7 @@ static size_t HUF_decompress_usingDTable( /* -3% slower when non static */
1092
1092
  const size_t length1 = FSE_readLE16(jumpTable);
1093
1093
  const size_t length2 = FSE_readLE16(jumpTable+1);
1094
1094
  const size_t length3 = FSE_readLE16(jumpTable+2);
1095
- const size_t length4 = cSrcSize - 6 - length1 - length2 - length3; // check coherency !!
1095
+ const size_t length4 = cSrcSize - 6 - length1 - length2 - length3; /* check coherency !! */
1096
1096
  const char* const start1 = (const char*)(cSrc) + 6;
1097
1097
  const char* const start2 = start1 + length1;
1098
1098
  const char* const start3 = start2 + length2;
@@ -1150,11 +1150,11 @@ static size_t HUF_decompress_usingDTable( /* -3% slower when non static */
1150
1150
 
1151
1151
  /* tail */
1152
1152
  {
1153
- // bitTail = bitD1; // *much* slower : -20% !??!
1153
+ /* bitTail = bitD1; */ /* *much* slower : -20% !??! */
1154
1154
  FSE_DStream_t bitTail;
1155
1155
  bitTail.ptr = bitD1.ptr;
1156
1156
  bitTail.bitsConsumed = bitD1.bitsConsumed;
1157
- bitTail.bitContainer = bitD1.bitContainer; // required in case of FSE_DStream_endOfBuffer
1157
+ bitTail.bitContainer = bitD1.bitContainer; /* required in case of FSE_DStream_endOfBuffer */
1158
1158
  bitTail.start = start1;
1159
1159
  for ( ; (FSE_reloadDStream(&bitTail) < FSE_DStream_completed) && (op<omax) ; op++)
1160
1160
  {
@@ -1280,7 +1280,11 @@ static size_t HUF_decompress (void* dst, size_t maxDstSize, const void* cSrc, si
1280
1280
  * Basic Types
1281
1281
  *********************************************************/
1282
1282
  #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
1283
- # include <stdint.h>
1283
+ # if defined(_AIX)
1284
+ # include <inttypes.h>
1285
+ # else
1286
+ # include <stdint.h> /* intptr_t */
1287
+ # endif
1284
1288
  typedef uint8_t BYTE;
1285
1289
  typedef uint16_t U16;
1286
1290
  typedef int16_t S16;
@@ -1483,7 +1487,9 @@ static size_t ZSTDv01_getcBlockSize(const void* src, size_t srcSize, blockProper
1483
1487
  static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
1484
1488
  {
1485
1489
  if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
1486
- memcpy(dst, src, srcSize);
1490
+ if (srcSize > 0) {
1491
+ memcpy(dst, src, srcSize);
1492
+ }
1487
1493
  return srcSize;
1488
1494
  }
1489
1495
 
@@ -1502,7 +1508,7 @@ static size_t ZSTD_decompressLiterals(void* ctx,
1502
1508
  if (srcSize <= 3) return ERROR(corruption_detected);
1503
1509
 
1504
1510
  litSize = ip[1] + (ip[0]<<8);
1505
- litSize += ((ip[-3] >> 3) & 7) << 16; // mmmmh....
1511
+ litSize += ((ip[-3] >> 3) & 7) << 16; /* mmmmh.... */
1506
1512
  op = oend - litSize;
1507
1513
 
1508
1514
  (void)ctx;
@@ -1541,7 +1547,9 @@ static size_t ZSTDv01_decodeLiteralsBlock(void* ctx,
1541
1547
  size_t rleSize = litbp.origSize;
1542
1548
  if (rleSize>maxDstSize) return ERROR(dstSize_tooSmall);
1543
1549
  if (!srcSize) return ERROR(srcSize_wrong);
1544
- memset(oend - rleSize, *ip, rleSize);
1550
+ if (rleSize > 0) {
1551
+ memset(oend - rleSize, *ip, rleSize);
1552
+ }
1545
1553
  *litStart = oend - rleSize;
1546
1554
  *litSize = rleSize;
1547
1555
  ip++;
@@ -1901,8 +1909,10 @@ static size_t ZSTD_decompressSequences(
1901
1909
  {
1902
1910
  size_t lastLLSize = litEnd - litPtr;
1903
1911
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
1904
- if (op != litPtr) memmove(op, litPtr, lastLLSize);
1905
- op += lastLLSize;
1912
+ if (lastLLSize > 0) {
1913
+ if (op != litPtr) memmove(op, litPtr, lastLLSize);
1914
+ op += lastLLSize;
1915
+ }
1906
1916
  }
1907
1917
  }
1908
1918
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 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) 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)
@@ -353,7 +357,7 @@ MEM_STATIC unsigned BIT_highbit32 (U32 val)
353
357
  _BitScanReverse ( &r, val );
354
358
  return (unsigned) r;
355
359
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
356
- return 31 - __builtin_clz (val);
360
+ return __builtin_clz (val) ^ 31;
357
361
  # else /* Software version */
358
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 };
359
363
  U32 v = val;
@@ -2836,7 +2840,9 @@ static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockPropertie
2836
2840
  static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2837
2841
  {
2838
2842
  if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
2839
- memcpy(dst, src, srcSize);
2843
+ if (srcSize > 0) {
2844
+ memcpy(dst, src, srcSize);
2845
+ }
2840
2846
  return srcSize;
2841
2847
  }
2842
2848
 
@@ -2889,6 +2895,7 @@ static size_t ZSTD_decodeLiteralsBlock(void* ctx,
2889
2895
  const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2890
2896
  if (litSize > srcSize-11) /* risk of reading too far with wildcopy */
2891
2897
  {
2898
+ if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2892
2899
  if (litSize > srcSize-3) return ERROR(corruption_detected);
2893
2900
  memcpy(dctx->litBuffer, istart, litSize);
2894
2901
  dctx->litPtr = dctx->litBuffer;
@@ -3228,8 +3235,10 @@ static size_t ZSTD_decompressSequences(
3228
3235
  size_t lastLLSize = litEnd - litPtr;
3229
3236
  if (litPtr > litEnd) return ERROR(corruption_detected);
3230
3237
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3231
- if (op != litPtr) memmove(op, litPtr, lastLLSize);
3232
- op += lastLLSize;
3238
+ if (lastLLSize > 0) {
3239
+ if (op != litPtr) memmove(op, litPtr, lastLLSize);
3240
+ op += lastLLSize;
3241
+ }
3233
3242
  }
3234
3243
  }
3235
3244
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 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) 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)
@@ -356,7 +360,7 @@ MEM_STATIC unsigned BIT_highbit32 (U32 val)
356
360
  _BitScanReverse ( &r, val );
357
361
  return (unsigned) r;
358
362
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
359
- return 31 - __builtin_clz (val);
363
+ return __builtin_clz (val) ^ 31;
360
364
  # else /* Software version */
361
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 };
362
366
  U32 v = val;
@@ -2477,7 +2481,9 @@ static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockPropertie
2477
2481
  static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2478
2482
  {
2479
2483
  if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
2480
- memcpy(dst, src, srcSize);
2484
+ if (srcSize > 0) {
2485
+ memcpy(dst, src, srcSize);
2486
+ }
2481
2487
  return srcSize;
2482
2488
  }
2483
2489
 
@@ -2530,6 +2536,7 @@ static size_t ZSTD_decodeLiteralsBlock(void* ctx,
2530
2536
  const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2531
2537
  if (litSize > srcSize-11) /* risk of reading too far with wildcopy */
2532
2538
  {
2539
+ if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2533
2540
  if (litSize > srcSize-3) return ERROR(corruption_detected);
2534
2541
  memcpy(dctx->litBuffer, istart, litSize);
2535
2542
  dctx->litPtr = dctx->litBuffer;
@@ -2869,8 +2876,10 @@ static size_t ZSTD_decompressSequences(
2869
2876
  size_t lastLLSize = litEnd - litPtr;
2870
2877
  if (litPtr > litEnd) return ERROR(corruption_detected);
2871
2878
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
2872
- if (op != litPtr) memmove(op, litPtr, lastLLSize);
2873
- op += lastLLSize;
2879
+ if (lastLLSize > 0) {
2880
+ if (op != litPtr) memmove(op, litPtr, lastLLSize);
2881
+ op += lastLLSize;
2882
+ }
2874
2883
  }
2875
2884
  }
2876
2885
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 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) 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)
@@ -627,7 +631,7 @@ MEM_STATIC unsigned BIT_highbit32 (U32 val)
627
631
  _BitScanReverse ( &r, val );
628
632
  return (unsigned) r;
629
633
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
630
- return 31 - __builtin_clz (val);
634
+ return __builtin_clz (val) ^ 31;
631
635
  # else /* Software version */
632
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 };
633
637
  U32 v = val;
@@ -2603,7 +2607,9 @@ static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockPropertie
2603
2607
  static size_t ZSTD_copyRawBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2604
2608
  {
2605
2609
  if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
2606
- memcpy(dst, src, srcSize);
2610
+ if (srcSize > 0) {
2611
+ memcpy(dst, src, srcSize);
2612
+ }
2607
2613
  return srcSize;
2608
2614
  }
2609
2615
 
@@ -2655,6 +2661,7 @@ static size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
2655
2661
  const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2656
2662
  if (litSize > srcSize-11) /* risk of reading too far with wildcopy */
2657
2663
  {
2664
+ if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2658
2665
  if (litSize > srcSize-3) return ERROR(corruption_detected);
2659
2666
  memcpy(dctx->litBuffer, istart, litSize);
2660
2667
  dctx->litPtr = dctx->litBuffer;
@@ -3007,8 +3014,10 @@ static size_t ZSTD_decompressSequences(
3007
3014
  size_t lastLLSize = litEnd - litPtr;
3008
3015
  if (litPtr > litEnd) return ERROR(corruption_detected);
3009
3016
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3010
- if (op != litPtr) memcpy(op, litPtr, lastLLSize);
3011
- op += lastLLSize;
3017
+ if (lastLLSize > 0) {
3018
+ if (op != litPtr) memcpy(op, litPtr, lastLLSize);
3019
+ op += lastLLSize;
3020
+ }
3012
3021
  }
3013
3022
  }
3014
3023
 
@@ -3034,9 +3043,12 @@ static size_t ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx,
3034
3043
  {
3035
3044
  /* blockType == blockCompressed */
3036
3045
  const BYTE* ip = (const BYTE*)src;
3046
+ size_t litCSize;
3047
+
3048
+ if (srcSize > BLOCKSIZE) return ERROR(corruption_detected);
3037
3049
 
3038
3050
  /* Decode literals sub-block */
3039
- size_t litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);
3051
+ litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);
3040
3052
  if (ZSTD_isError(litCSize)) return litCSize;
3041
3053
  ip += litCSize;
3042
3054
  srcSize -= litCSize;
@@ -3403,7 +3415,9 @@ static size_t ZBUFF_decompressWithDictionary(ZBUFF_DCtx* zbc, const void* src, s
3403
3415
  static size_t ZBUFF_limitCopy(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3404
3416
  {
3405
3417
  size_t length = MIN(maxDstSize, srcSize);
3406
- memcpy(dst, src, length);
3418
+ if (length > 0) {
3419
+ memcpy(dst, src, length);
3420
+ }
3407
3421
  return length;
3408
3422
  }
3409
3423
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 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) 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;
@@ -218,11 +222,6 @@ MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
218
222
  }
219
223
  }
220
224
 
221
- MEM_STATIC U32 MEM_readLE24(const void* memPtr)
222
- {
223
- return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
224
- }
225
-
226
225
  MEM_STATIC U32 MEM_readLE32(const void* memPtr)
227
226
  {
228
227
  if (MEM_isLittleEndian())
@@ -761,7 +760,7 @@ MEM_STATIC unsigned BITv05_highbit32 (U32 val)
761
760
  _BitScanReverse ( &r, val );
762
761
  return (unsigned) r;
763
762
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
764
- return 31 - __builtin_clz (val);
763
+ return __builtin_clz (val) ^ 31;
765
764
  # else /* Software version */
766
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 };
767
766
  U32 v = val;
@@ -1809,7 +1808,7 @@ static size_t HUFv05_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
1809
1808
 
1810
1809
  if (!srcSize) return ERROR(srcSize_wrong);
1811
1810
  iSize = ip[0];
1812
- //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 ... */
1813
1812
 
1814
1813
  if (iSize >= 128) { /* special header */
1815
1814
  if (iSize >= (242)) { /* RLE */
@@ -1884,7 +1883,7 @@ size_t HUFv05_readDTableX2 (U16* DTable, const void* src, size_t srcSize)
1884
1883
  HUFv05_DEltX2* const dt = (HUFv05_DEltX2*)dtPtr;
1885
1884
 
1886
1885
  HUFv05_STATIC_ASSERT(sizeof(HUFv05_DEltX2) == sizeof(U16)); /* if compilation fails here, assertion is false */
1887
- //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 ... */
1888
1887
 
1889
1888
  iSize = HUFv05_readStats(huffWeight, HUFv05_MAX_SYMBOL_VALUE + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
1890
1889
  if (HUFv05_isError(iSize)) return iSize;
@@ -2215,7 +2214,7 @@ size_t HUFv05_readDTableX4 (unsigned* DTable, const void* src, size_t srcSize)
2215
2214
 
2216
2215
  HUFv05_STATIC_ASSERT(sizeof(HUFv05_DEltX4) == sizeof(unsigned)); /* if compilation fails here, assertion is false */
2217
2216
  if (memLog > HUFv05_ABSOLUTEMAX_TABLELOG) return ERROR(tableLog_tooLarge);
2218
- //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 ... */
2219
2218
 
2220
2219
  iSize = HUFv05_readStats(weightList, HUFv05_MAX_SYMBOL_VALUE + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
2221
2220
  if (HUFv05_isError(iSize)) return iSize;
@@ -2544,9 +2543,9 @@ size_t HUFv05_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cS
2544
2543
 
2545
2544
  return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
2546
2545
 
2547
- //return HUFv05_decompress4X2(dst, dstSize, cSrc, cSrcSize); /* multi-streams single-symbol decoding */
2548
- //return HUFv05_decompress4X4(dst, dstSize, cSrc, cSrcSize); /* multi-streams double-symbols decoding */
2549
- //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 */
2550
2549
  }
2551
2550
  /*
2552
2551
  zstd - standard compression library
@@ -2834,7 +2833,7 @@ static size_t ZSTDv05_decodeFrameHeader_Part2(ZSTDv05_DCtx* zc, const void* src,
2834
2833
 
2835
2834
  static size_t ZSTDv05_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
2836
2835
  {
2837
- const BYTE* const in = (const BYTE* const)src;
2836
+ const BYTE* const in = (const BYTE*)src;
2838
2837
  BYTE headerFlags;
2839
2838
  U32 cSize;
2840
2839
 
@@ -3003,7 +3002,7 @@ static size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t
3003
3002
  FSEv05_DTable* DTableLL, FSEv05_DTable* DTableML, FSEv05_DTable* DTableOffb,
3004
3003
  const void* src, size_t srcSize, U32 flagStaticTable)
3005
3004
  {
3006
- const BYTE* const istart = (const BYTE* const)src;
3005
+ const BYTE* const istart = (const BYTE*)src;
3007
3006
  const BYTE* ip = istart;
3008
3007
  const BYTE* const iend = istart + srcSize;
3009
3008
  U32 LLtype, Offtype, MLtype;
@@ -3158,10 +3157,14 @@ static void ZSTDv05_decodeSequence(seq_t* seq, seqState_t* seqState)
3158
3157
  if (litLength == MaxLL) {
3159
3158
  const U32 add = *dumps++;
3160
3159
  if (add < 255) litLength += add;
3161
- else if (dumps + 3 <= de) {
3162
- litLength = MEM_readLE24(dumps);
3163
- if (litLength&1) litLength>>=1, dumps += 3;
3164
- 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;
3165
3168
  }
3166
3169
  if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
3167
3170
  }
@@ -3191,10 +3194,14 @@ static void ZSTDv05_decodeSequence(seq_t* seq, seqState_t* seqState)
3191
3194
  if (matchLength == MaxML) {
3192
3195
  const U32 add = dumps<de ? *dumps++ : 0;
3193
3196
  if (add < 255) matchLength += add;
3194
- else if (dumps + 3 <= de) {
3195
- matchLength = MEM_readLE24(dumps);
3196
- if (matchLength&1) matchLength>>=1, dumps += 3;
3197
- 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;
3198
3205
  }
3199
3206
  if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
3200
3207
  }
@@ -3303,7 +3310,7 @@ static size_t ZSTDv05_decompressSequences(
3303
3310
  {
3304
3311
  const BYTE* ip = (const BYTE*)seqStart;
3305
3312
  const BYTE* const iend = ip + seqSize;
3306
- BYTE* const ostart = (BYTE* const)dst;
3313
+ BYTE* const ostart = (BYTE*)dst;
3307
3314
  BYTE* op = ostart;
3308
3315
  BYTE* const oend = ostart + maxDstSize;
3309
3316
  size_t errorCode, dumpsLength=0;
@@ -3359,8 +3366,10 @@ static size_t ZSTDv05_decompressSequences(
3359
3366
  size_t lastLLSize = litEnd - litPtr;
3360
3367
  if (litPtr > litEnd) return ERROR(corruption_detected); /* too many literals already used */
3361
3368
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3362
- memcpy(op, litPtr, lastLLSize);
3363
- op += lastLLSize;
3369
+ if (lastLLSize > 0) {
3370
+ memcpy(op, litPtr, lastLLSize);
3371
+ op += lastLLSize;
3372
+ }
3364
3373
  }
3365
3374
 
3366
3375
  return op-ostart;
@@ -3414,7 +3423,7 @@ static size_t ZSTDv05_decompress_continueDCtx(ZSTDv05_DCtx* dctx,
3414
3423
  {
3415
3424
  const BYTE* ip = (const BYTE*)src;
3416
3425
  const BYTE* iend = ip + srcSize;
3417
- BYTE* const ostart = (BYTE* const)dst;
3426
+ BYTE* const ostart = (BYTE*)dst;
3418
3427
  BYTE* op = ostart;
3419
3428
  BYTE* const oend = ostart + maxDstSize;
3420
3429
  size_t remainingSize = srcSize;
@@ -3788,7 +3797,9 @@ static size_t ZBUFFv05_blockHeaderSize = 3;
3788
3797
  static size_t ZBUFFv05_limitCopy(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3789
3798
  {
3790
3799
  size_t length = MIN(maxDstSize, srcSize);
3791
- memcpy(dst, src, length);
3800
+ if (length > 0) {
3801
+ memcpy(dst, src, length);
3802
+ }
3792
3803
  return length;
3793
3804
  }
3794
3805
 
@@ -3925,7 +3936,7 @@ size_t ZBUFFv05_decompressContinue(ZBUFFv05_DCtx* zbc, void* dst, size_t* maxDst
3925
3936
  *maxDstSizePtr = 0;
3926
3937
  return headerSize - zbc->hPos;
3927
3938
  }
3928
- // zbc->stage = ZBUFFv05ds_decodeHeader; break; /* useless : stage follows */
3939
+ /* zbc->stage = ZBUFFv05ds_decodeHeader; break; */ /* useless : stage follows */
3929
3940
  }
3930
3941
  /* fall-through */
3931
3942
  case ZBUFFv05ds_decodeHeader:
@@ -3998,7 +4009,7 @@ size_t ZBUFFv05_decompressContinue(ZBUFFv05_DCtx* zbc, void* dst, size_t* maxDst
3998
4009
  if (!decodedSize) { zbc->stage = ZBUFFv05ds_read; break; } /* this was just a header */
3999
4010
  zbc->outEnd = zbc->outStart + decodedSize;
4000
4011
  zbc->stage = ZBUFFv05ds_flush;
4001
- // break; /* ZBUFFv05ds_flush follows */
4012
+ /* break; */ /* ZBUFFv05ds_flush follows */
4002
4013
  }
4003
4014
  }
4004
4015
  /* fall-through */