zstd-ruby 1.3.8.0 → 1.4.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +6 -5
  3. data/README.md +1 -1
  4. data/ext/zstdruby/libzstd/Makefile +133 -61
  5. data/ext/zstdruby/libzstd/README.md +51 -18
  6. data/ext/zstdruby/libzstd/common/bitstream.h +38 -39
  7. data/ext/zstdruby/libzstd/common/compiler.h +41 -6
  8. data/ext/zstdruby/libzstd/common/cpu.h +1 -1
  9. data/ext/zstdruby/libzstd/common/debug.c +11 -31
  10. data/ext/zstdruby/libzstd/common/debug.h +11 -31
  11. data/ext/zstdruby/libzstd/common/entropy_common.c +13 -33
  12. data/ext/zstdruby/libzstd/common/error_private.c +2 -1
  13. data/ext/zstdruby/libzstd/common/error_private.h +6 -2
  14. data/ext/zstdruby/libzstd/common/fse.h +13 -33
  15. data/ext/zstdruby/libzstd/common/fse_decompress.c +12 -35
  16. data/ext/zstdruby/libzstd/common/huf.h +15 -33
  17. data/ext/zstdruby/libzstd/common/mem.h +75 -2
  18. data/ext/zstdruby/libzstd/common/pool.c +8 -4
  19. data/ext/zstdruby/libzstd/common/pool.h +2 -2
  20. data/ext/zstdruby/libzstd/common/threading.c +52 -6
  21. data/ext/zstdruby/libzstd/common/threading.h +36 -4
  22. data/ext/zstdruby/libzstd/common/xxhash.c +25 -37
  23. data/ext/zstdruby/libzstd/common/xxhash.h +11 -31
  24. data/ext/zstdruby/libzstd/common/zstd_common.c +1 -1
  25. data/ext/zstdruby/libzstd/common/zstd_errors.h +2 -1
  26. data/ext/zstdruby/libzstd/common/zstd_internal.h +203 -22
  27. data/ext/zstdruby/libzstd/compress/fse_compress.c +19 -42
  28. data/ext/zstdruby/libzstd/compress/hist.c +15 -35
  29. data/ext/zstdruby/libzstd/compress/hist.h +12 -32
  30. data/ext/zstdruby/libzstd/compress/huf_compress.c +92 -92
  31. data/ext/zstdruby/libzstd/compress/zstd_compress.c +1460 -1472
  32. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +330 -65
  33. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +158 -0
  34. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +29 -0
  35. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +419 -0
  36. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.h +54 -0
  37. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +845 -0
  38. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.h +32 -0
  39. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +525 -0
  40. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +65 -43
  41. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +2 -2
  42. data/ext/zstdruby/libzstd/compress/zstd_fast.c +264 -159
  43. data/ext/zstdruby/libzstd/compress/zstd_fast.h +2 -2
  44. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +74 -42
  45. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +2 -2
  46. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +33 -11
  47. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +7 -2
  48. data/ext/zstdruby/libzstd/compress/zstd_opt.c +108 -125
  49. data/ext/zstdruby/libzstd/compress/zstd_opt.h +1 -1
  50. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +129 -93
  51. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +46 -28
  52. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +76 -60
  53. data/ext/zstdruby/libzstd/decompress/zstd_ddict.c +14 -10
  54. data/ext/zstdruby/libzstd/decompress/zstd_ddict.h +2 -2
  55. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +471 -258
  56. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +471 -346
  57. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +3 -3
  58. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +25 -4
  59. data/ext/zstdruby/libzstd/deprecated/zbuff.h +9 -8
  60. data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +2 -2
  61. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +1 -1
  62. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +1 -1
  63. data/ext/zstdruby/libzstd/dictBuilder/cover.c +220 -65
  64. data/ext/zstdruby/libzstd/dictBuilder/cover.h +81 -7
  65. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +85 -56
  66. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +43 -19
  67. data/ext/zstdruby/libzstd/dictBuilder/zdict.h +73 -35
  68. data/ext/zstdruby/libzstd/dll/example/Makefile +2 -1
  69. data/ext/zstdruby/libzstd/dll/example/build_package.bat +3 -2
  70. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +49 -15
  71. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +142 -117
  72. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +13 -8
  73. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +54 -25
  74. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +13 -8
  75. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +55 -25
  76. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +13 -8
  77. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +62 -29
  78. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +13 -8
  79. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +145 -109
  80. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +14 -9
  81. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +56 -26
  82. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +11 -6
  83. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +65 -28
  84. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +11 -6
  85. data/ext/zstdruby/libzstd/libzstd.pc.in +3 -2
  86. data/ext/zstdruby/libzstd/zstd.h +921 -597
  87. data/lib/zstd-ruby/version.rb +1 -1
  88. data/zstd-ruby.gemspec +2 -2
  89. metadata +19 -14
  90. data/ext/zstdruby/libzstd/dll/libzstd.def +0 -87
@@ -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
@@ -35,13 +35,18 @@ ZSTDv01_decompress() : decompress ZSTD frames compliant with v0.1.x format
35
35
  size_t ZSTDv01_decompress( void* dst, size_t maxOriginalSize,
36
36
  const void* src, size_t compressedSize);
37
37
 
38
- /**
39
- ZSTDv01_getFrameSrcSize() : get the source length of a ZSTD frame compliant with v0.1.x format
40
- compressedSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
41
- return : the number of bytes that would be read to decompress this frame
42
- or an errorCode if it fails (which can be tested using ZSTDv01_isError())
43
- */
44
- size_t ZSTDv01_findFrameCompressedSize(const void* src, size_t compressedSize);
38
+ /**
39
+ ZSTDv01_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.1.x format
40
+ srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
41
+ cSize (output parameter) : the number of bytes that would be read to decompress this frame
42
+ or an error code if it fails (which can be tested using ZSTDv01_isError())
43
+ dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame
44
+ or ZSTD_CONTENTSIZE_ERROR if an error occurs
45
+
46
+ note : assumes `cSize` and `dBound` are _not_ NULL.
47
+ */
48
+ void ZSTDv01_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
49
+ size_t* cSize, unsigned long long* dBound);
45
50
 
46
51
  /**
47
52
  ZSTDv01_isError() : tells if the result of ZSTDv01_decompress() is an error
@@ -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;
@@ -2728,6 +2733,8 @@ static size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_
2728
2733
  #define LITERAL_NOENTROPY 63
2729
2734
  #define COMMAND_NOENTROPY 7 /* to remove */
2730
2735
 
2736
+ #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
2737
+
2731
2738
  static const size_t ZSTD_blockHeaderSize = 3;
2732
2739
  static const size_t ZSTD_frameHeaderSize = 4;
2733
2740
 
@@ -2829,7 +2836,9 @@ static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockPropertie
2829
2836
  static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2830
2837
  {
2831
2838
  if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
2832
- memcpy(dst, src, srcSize);
2839
+ if (srcSize > 0) {
2840
+ memcpy(dst, src, srcSize);
2841
+ }
2833
2842
  return srcSize;
2834
2843
  }
2835
2844
 
@@ -2882,6 +2891,7 @@ static size_t ZSTD_decodeLiteralsBlock(void* ctx,
2882
2891
  const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2883
2892
  if (litSize > srcSize-11) /* risk of reading too far with wildcopy */
2884
2893
  {
2894
+ if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2885
2895
  if (litSize > srcSize-3) return ERROR(corruption_detected);
2886
2896
  memcpy(dctx->litBuffer, istart, litSize);
2887
2897
  dctx->litPtr = dctx->litBuffer;
@@ -3041,11 +3051,11 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
3041
3051
  seqState->prevOffset = seq->offset;
3042
3052
  if (litLength == MaxLL)
3043
3053
  {
3044
- U32 add = *dumps++;
3054
+ const U32 add = dumps<de ? *dumps++ : 0;
3045
3055
  if (add < 255) litLength += add;
3046
- else
3056
+ else if (dumps + 3 <= de)
3047
3057
  {
3048
- litLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
3058
+ litLength = MEM_readLE24(dumps);
3049
3059
  dumps += 3;
3050
3060
  }
3051
3061
  if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
@@ -3071,11 +3081,11 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
3071
3081
  matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
3072
3082
  if (matchLength == MaxML)
3073
3083
  {
3074
- U32 add = *dumps++;
3084
+ const U32 add = dumps<de ? *dumps++ : 0;
3075
3085
  if (add < 255) matchLength += add;
3076
- else
3086
+ else if (dumps + 3 <= de)
3077
3087
  {
3078
- matchLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
3088
+ matchLength = MEM_readLE24(dumps);
3079
3089
  dumps += 3;
3080
3090
  }
3081
3091
  if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
@@ -3096,7 +3106,7 @@ static size_t ZSTD_execSequence(BYTE* op,
3096
3106
  BYTE* const base, BYTE* const oend)
3097
3107
  {
3098
3108
  static const int dec32table[] = {0, 1, 2, 1, 4, 4, 4, 4}; /* added */
3099
- static const int dec64table[] = {8, 8, 8, 7, 8, 9,10,11}; /* substracted */
3109
+ static const int dec64table[] = {8, 8, 8, 7, 8, 9,10,11}; /* subtracted */
3100
3110
  const BYTE* const ostart = op;
3101
3111
  BYTE* const oLitEnd = op + sequence.litLength;
3102
3112
  BYTE* const oMatchEnd = op + sequence.litLength + sequence.matchLength; /* risk : address space overflow (32-bits) */
@@ -3221,8 +3231,10 @@ static size_t ZSTD_decompressSequences(
3221
3231
  size_t lastLLSize = litEnd - litPtr;
3222
3232
  if (litPtr > litEnd) return ERROR(corruption_detected);
3223
3233
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3224
- if (op != litPtr) memmove(op, litPtr, lastLLSize);
3225
- op += lastLLSize;
3234
+ if (lastLLSize > 0) {
3235
+ if (op != litPtr) memmove(op, litPtr, lastLLSize);
3236
+ op += lastLLSize;
3237
+ }
3226
3238
  }
3227
3239
  }
3228
3240
 
@@ -3312,37 +3324,59 @@ static size_t ZSTD_decompress(void* dst, size_t maxDstSize, const void* src, siz
3312
3324
  return ZSTD_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize);
3313
3325
  }
3314
3326
 
3315
- static size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize)
3327
+ /* ZSTD_errorFrameSizeInfoLegacy() :
3328
+ assumes `cSize` and `dBound` are _not_ NULL */
3329
+ static void ZSTD_errorFrameSizeInfoLegacy(size_t* cSize, unsigned long long* dBound, size_t ret)
3316
3330
  {
3331
+ *cSize = ret;
3332
+ *dBound = ZSTD_CONTENTSIZE_ERROR;
3333
+ }
3317
3334
 
3335
+ void ZSTDv02_findFrameSizeInfoLegacy(const void *src, size_t srcSize, size_t* cSize, unsigned long long* dBound)
3336
+ {
3318
3337
  const BYTE* ip = (const BYTE*)src;
3319
3338
  size_t remainingSize = srcSize;
3339
+ size_t nbBlocks = 0;
3320
3340
  U32 magicNumber;
3321
3341
  blockProperties_t blockProperties;
3322
3342
 
3323
3343
  /* Frame Header */
3324
- if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);
3344
+ if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) {
3345
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3346
+ return;
3347
+ }
3325
3348
  magicNumber = MEM_readLE32(src);
3326
- if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown);
3349
+ if (magicNumber != ZSTD_magicNumber) {
3350
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(prefix_unknown));
3351
+ return;
3352
+ }
3327
3353
  ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize;
3328
3354
 
3329
3355
  /* Loop on each block */
3330
3356
  while (1)
3331
3357
  {
3332
3358
  size_t cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
3333
- if (ZSTD_isError(cBlockSize)) return cBlockSize;
3359
+ if (ZSTD_isError(cBlockSize)) {
3360
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, cBlockSize);
3361
+ return;
3362
+ }
3334
3363
 
3335
3364
  ip += ZSTD_blockHeaderSize;
3336
3365
  remainingSize -= ZSTD_blockHeaderSize;
3337
- if (cBlockSize > remainingSize) return ERROR(srcSize_wrong);
3366
+ if (cBlockSize > remainingSize) {
3367
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3368
+ return;
3369
+ }
3338
3370
 
3339
3371
  if (cBlockSize == 0) break; /* bt_end */
3340
3372
 
3341
3373
  ip += cBlockSize;
3342
3374
  remainingSize -= cBlockSize;
3375
+ nbBlocks++;
3343
3376
  }
3344
3377
 
3345
- return ip - (const BYTE*)src;
3378
+ *cSize = ip - (const BYTE*)src;
3379
+ *dBound = nbBlocks * BLOCKSIZE;
3346
3380
  }
3347
3381
 
3348
3382
  /*******************************
@@ -3458,11 +3492,6 @@ size_t ZSTDv02_decompress( void* dst, size_t maxOriginalSize,
3458
3492
  return ZSTD_decompress(dst, maxOriginalSize, src, compressedSize);
3459
3493
  }
3460
3494
 
3461
- size_t ZSTDv02_findFrameCompressedSize(const void *src, size_t compressedSize)
3462
- {
3463
- return ZSTD_findFrameCompressedSize(src, compressedSize);
3464
- }
3465
-
3466
3495
  ZSTDv02_Dctx* ZSTDv02_createDCtx(void)
3467
3496
  {
3468
3497
  return (ZSTDv02_Dctx*)ZSTD_createDCtx();
@@ -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
@@ -35,13 +35,18 @@ ZSTDv02_decompress() : decompress ZSTD frames compliant with v0.2.x format
35
35
  size_t ZSTDv02_decompress( void* dst, size_t maxOriginalSize,
36
36
  const void* src, size_t compressedSize);
37
37
 
38
- /**
39
- ZSTDv02_getFrameSrcSize() : get the source length of a ZSTD frame compliant with v0.2.x format
40
- compressedSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
41
- return : the number of bytes that would be read to decompress this frame
42
- or an errorCode if it fails (which can be tested using ZSTDv02_isError())
43
- */
44
- size_t ZSTDv02_findFrameCompressedSize(const void* src, size_t compressedSize);
38
+ /**
39
+ ZSTDv02_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.2.x format
40
+ srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
41
+ cSize (output parameter) : the number of bytes that would be read to decompress this frame
42
+ or an error code if it fails (which can be tested using ZSTDv01_isError())
43
+ dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame
44
+ or ZSTD_CONTENTSIZE_ERROR if an error occurs
45
+
46
+ note : assumes `cSize` and `dBound` are _not_ NULL.
47
+ */
48
+ void ZSTDv02_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
49
+ size_t* cSize, unsigned long long* dBound);
45
50
 
46
51
  /**
47
52
  ZSTDv02_isError() : tells if the result of ZSTDv02_decompress() is an error
@@ -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;
@@ -2369,6 +2374,8 @@ static size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_
2369
2374
  #define LITERAL_NOENTROPY 63
2370
2375
  #define COMMAND_NOENTROPY 7 /* to remove */
2371
2376
 
2377
+ #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
2378
+
2372
2379
  static const size_t ZSTD_blockHeaderSize = 3;
2373
2380
  static const size_t ZSTD_frameHeaderSize = 4;
2374
2381
 
@@ -2470,7 +2477,9 @@ static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockPropertie
2470
2477
  static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2471
2478
  {
2472
2479
  if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
2473
- memcpy(dst, src, srcSize);
2480
+ if (srcSize > 0) {
2481
+ memcpy(dst, src, srcSize);
2482
+ }
2474
2483
  return srcSize;
2475
2484
  }
2476
2485
 
@@ -2523,6 +2532,7 @@ static size_t ZSTD_decodeLiteralsBlock(void* ctx,
2523
2532
  const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2524
2533
  if (litSize > srcSize-11) /* risk of reading too far with wildcopy */
2525
2534
  {
2535
+ if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2526
2536
  if (litSize > srcSize-3) return ERROR(corruption_detected);
2527
2537
  memcpy(dctx->litBuffer, istart, litSize);
2528
2538
  dctx->litPtr = dctx->litBuffer;
@@ -2682,11 +2692,11 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
2682
2692
  seqState->prevOffset = seq->offset;
2683
2693
  if (litLength == MaxLL)
2684
2694
  {
2685
- U32 add = *dumps++;
2695
+ const U32 add = dumps<de ? *dumps++ : 0;
2686
2696
  if (add < 255) litLength += add;
2687
- else
2697
+ else if (dumps + 3 <= de)
2688
2698
  {
2689
- litLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
2699
+ litLength = MEM_readLE24(dumps);
2690
2700
  dumps += 3;
2691
2701
  }
2692
2702
  if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
@@ -2712,11 +2722,11 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
2712
2722
  matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
2713
2723
  if (matchLength == MaxML)
2714
2724
  {
2715
- U32 add = *dumps++;
2725
+ const U32 add = dumps<de ? *dumps++ : 0;
2716
2726
  if (add < 255) matchLength += add;
2717
- else
2727
+ else if (dumps + 3 <= de)
2718
2728
  {
2719
- matchLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
2729
+ matchLength = MEM_readLE24(dumps);
2720
2730
  dumps += 3;
2721
2731
  }
2722
2732
  if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
@@ -2737,7 +2747,7 @@ static size_t ZSTD_execSequence(BYTE* op,
2737
2747
  BYTE* const base, BYTE* const oend)
2738
2748
  {
2739
2749
  static const int dec32table[] = {0, 1, 2, 1, 4, 4, 4, 4}; /* added */
2740
- static const int dec64table[] = {8, 8, 8, 7, 8, 9,10,11}; /* substracted */
2750
+ static const int dec64table[] = {8, 8, 8, 7, 8, 9,10,11}; /* subtracted */
2741
2751
  const BYTE* const ostart = op;
2742
2752
  BYTE* const oLitEnd = op + sequence.litLength;
2743
2753
  BYTE* const oMatchEnd = op + sequence.litLength + sequence.matchLength; /* risk : address space overflow (32-bits) */
@@ -2862,8 +2872,10 @@ static size_t ZSTD_decompressSequences(
2862
2872
  size_t lastLLSize = litEnd - litPtr;
2863
2873
  if (litPtr > litEnd) return ERROR(corruption_detected);
2864
2874
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
2865
- if (op != litPtr) memmove(op, litPtr, lastLLSize);
2866
- op += lastLLSize;
2875
+ if (lastLLSize > 0) {
2876
+ if (op != litPtr) memmove(op, litPtr, lastLLSize);
2877
+ op += lastLLSize;
2878
+ }
2867
2879
  }
2868
2880
  }
2869
2881
 
@@ -2953,36 +2965,59 @@ static size_t ZSTD_decompress(void* dst, size_t maxDstSize, const void* src, siz
2953
2965
  return ZSTD_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize);
2954
2966
  }
2955
2967
 
2956
- static size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize)
2968
+ /* ZSTD_errorFrameSizeInfoLegacy() :
2969
+ assumes `cSize` and `dBound` are _not_ NULL */
2970
+ MEM_STATIC void ZSTD_errorFrameSizeInfoLegacy(size_t* cSize, unsigned long long* dBound, size_t ret)
2971
+ {
2972
+ *cSize = ret;
2973
+ *dBound = ZSTD_CONTENTSIZE_ERROR;
2974
+ }
2975
+
2976
+ void ZSTDv03_findFrameSizeInfoLegacy(const void *src, size_t srcSize, size_t* cSize, unsigned long long* dBound)
2957
2977
  {
2958
2978
  const BYTE* ip = (const BYTE*)src;
2959
2979
  size_t remainingSize = srcSize;
2980
+ size_t nbBlocks = 0;
2960
2981
  U32 magicNumber;
2961
2982
  blockProperties_t blockProperties;
2962
2983
 
2963
2984
  /* Frame Header */
2964
- if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);
2985
+ if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) {
2986
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
2987
+ return;
2988
+ }
2965
2989
  magicNumber = MEM_readLE32(src);
2966
- if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown);
2990
+ if (magicNumber != ZSTD_magicNumber) {
2991
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(prefix_unknown));
2992
+ return;
2993
+ }
2967
2994
  ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize;
2968
2995
 
2969
2996
  /* Loop on each block */
2970
2997
  while (1)
2971
2998
  {
2972
2999
  size_t cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
2973
- if (ZSTD_isError(cBlockSize)) return cBlockSize;
3000
+ if (ZSTD_isError(cBlockSize)) {
3001
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, cBlockSize);
3002
+ return;
3003
+ }
2974
3004
 
2975
3005
  ip += ZSTD_blockHeaderSize;
2976
3006
  remainingSize -= ZSTD_blockHeaderSize;
2977
- if (cBlockSize > remainingSize) return ERROR(srcSize_wrong);
3007
+ if (cBlockSize > remainingSize) {
3008
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3009
+ return;
3010
+ }
2978
3011
 
2979
3012
  if (cBlockSize == 0) break; /* bt_end */
2980
3013
 
2981
3014
  ip += cBlockSize;
2982
3015
  remainingSize -= cBlockSize;
3016
+ nbBlocks++;
2983
3017
  }
2984
3018
 
2985
- return ip - (const BYTE*)src;
3019
+ *cSize = ip - (const BYTE*)src;
3020
+ *dBound = nbBlocks * BLOCKSIZE;
2986
3021
  }
2987
3022
 
2988
3023
 
@@ -3099,11 +3134,6 @@ size_t ZSTDv03_decompress( void* dst, size_t maxOriginalSize,
3099
3134
  return ZSTD_decompress(dst, maxOriginalSize, src, compressedSize);
3100
3135
  }
3101
3136
 
3102
- size_t ZSTDv03_findFrameCompressedSize(const void* src, size_t srcSize)
3103
- {
3104
- return ZSTD_findFrameCompressedSize(src, srcSize);
3105
- }
3106
-
3107
3137
  ZSTDv03_Dctx* ZSTDv03_createDCtx(void)
3108
3138
  {
3109
3139
  return (ZSTDv03_Dctx*)ZSTD_createDCtx();