extzstd 0.3.2 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -3
  3. data/contrib/zstd/CHANGELOG +225 -1
  4. data/contrib/zstd/CONTRIBUTING.md +158 -75
  5. data/contrib/zstd/LICENSE +4 -4
  6. data/contrib/zstd/Makefile +106 -69
  7. data/contrib/zstd/Package.swift +36 -0
  8. data/contrib/zstd/README.md +64 -36
  9. data/contrib/zstd/SECURITY.md +15 -0
  10. data/contrib/zstd/TESTING.md +2 -3
  11. data/contrib/zstd/lib/BUCK +5 -7
  12. data/contrib/zstd/lib/Makefile +117 -199
  13. data/contrib/zstd/lib/README.md +37 -7
  14. data/contrib/zstd/lib/common/allocations.h +55 -0
  15. data/contrib/zstd/lib/common/bits.h +200 -0
  16. data/contrib/zstd/lib/common/bitstream.h +80 -86
  17. data/contrib/zstd/lib/common/compiler.h +225 -63
  18. data/contrib/zstd/lib/common/cpu.h +37 -1
  19. data/contrib/zstd/lib/common/debug.c +7 -1
  20. data/contrib/zstd/lib/common/debug.h +21 -12
  21. data/contrib/zstd/lib/common/entropy_common.c +15 -37
  22. data/contrib/zstd/lib/common/error_private.c +9 -2
  23. data/contrib/zstd/lib/common/error_private.h +93 -5
  24. data/contrib/zstd/lib/common/fse.h +12 -87
  25. data/contrib/zstd/lib/common/fse_decompress.c +37 -117
  26. data/contrib/zstd/lib/common/huf.h +97 -172
  27. data/contrib/zstd/lib/common/mem.h +58 -58
  28. data/contrib/zstd/lib/common/pool.c +38 -17
  29. data/contrib/zstd/lib/common/pool.h +10 -4
  30. data/contrib/zstd/lib/common/portability_macros.h +158 -0
  31. data/contrib/zstd/lib/common/threading.c +74 -14
  32. data/contrib/zstd/lib/common/threading.h +5 -10
  33. data/contrib/zstd/lib/common/xxhash.c +6 -814
  34. data/contrib/zstd/lib/common/xxhash.h +6930 -195
  35. data/contrib/zstd/lib/common/zstd_common.c +1 -36
  36. data/contrib/zstd/lib/common/zstd_deps.h +1 -1
  37. data/contrib/zstd/lib/common/zstd_internal.h +68 -154
  38. data/contrib/zstd/lib/common/zstd_trace.h +163 -0
  39. data/contrib/zstd/lib/compress/clevels.h +134 -0
  40. data/contrib/zstd/lib/compress/fse_compress.c +75 -155
  41. data/contrib/zstd/lib/compress/hist.c +1 -1
  42. data/contrib/zstd/lib/compress/hist.h +1 -1
  43. data/contrib/zstd/lib/compress/huf_compress.c +810 -259
  44. data/contrib/zstd/lib/compress/zstd_compress.c +2864 -919
  45. data/contrib/zstd/lib/compress/zstd_compress_internal.h +523 -192
  46. data/contrib/zstd/lib/compress/zstd_compress_literals.c +117 -40
  47. data/contrib/zstd/lib/compress/zstd_compress_literals.h +16 -6
  48. data/contrib/zstd/lib/compress/zstd_compress_sequences.c +28 -19
  49. data/contrib/zstd/lib/compress/zstd_compress_sequences.h +1 -1
  50. data/contrib/zstd/lib/compress/zstd_compress_superblock.c +251 -412
  51. data/contrib/zstd/lib/compress/zstd_compress_superblock.h +1 -1
  52. data/contrib/zstd/lib/compress/zstd_cwksp.h +284 -97
  53. data/contrib/zstd/lib/compress/zstd_double_fast.c +382 -133
  54. data/contrib/zstd/lib/compress/zstd_double_fast.h +14 -2
  55. data/contrib/zstd/lib/compress/zstd_fast.c +732 -260
  56. data/contrib/zstd/lib/compress/zstd_fast.h +3 -2
  57. data/contrib/zstd/lib/compress/zstd_lazy.c +1177 -390
  58. data/contrib/zstd/lib/compress/zstd_lazy.h +129 -14
  59. data/contrib/zstd/lib/compress/zstd_ldm.c +280 -210
  60. data/contrib/zstd/lib/compress/zstd_ldm.h +3 -2
  61. data/contrib/zstd/lib/compress/zstd_ldm_geartab.h +106 -0
  62. data/contrib/zstd/lib/compress/zstd_opt.c +516 -285
  63. data/contrib/zstd/lib/compress/zstd_opt.h +32 -8
  64. data/contrib/zstd/lib/compress/zstdmt_compress.c +202 -131
  65. data/contrib/zstd/lib/compress/zstdmt_compress.h +9 -6
  66. data/contrib/zstd/lib/decompress/huf_decompress.c +1149 -555
  67. data/contrib/zstd/lib/decompress/huf_decompress_amd64.S +595 -0
  68. data/contrib/zstd/lib/decompress/zstd_ddict.c +4 -4
  69. data/contrib/zstd/lib/decompress/zstd_ddict.h +1 -1
  70. data/contrib/zstd/lib/decompress/zstd_decompress.c +583 -106
  71. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +1054 -379
  72. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +14 -3
  73. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +56 -6
  74. data/contrib/zstd/lib/deprecated/zbuff.h +1 -1
  75. data/contrib/zstd/lib/deprecated/zbuff_common.c +1 -1
  76. data/contrib/zstd/lib/deprecated/zbuff_compress.c +24 -4
  77. data/contrib/zstd/lib/deprecated/zbuff_decompress.c +3 -1
  78. data/contrib/zstd/lib/dictBuilder/cover.c +60 -44
  79. data/contrib/zstd/lib/dictBuilder/cover.h +6 -11
  80. data/contrib/zstd/lib/dictBuilder/divsufsort.c +1 -1
  81. data/contrib/zstd/lib/dictBuilder/fastcover.c +26 -18
  82. data/contrib/zstd/lib/dictBuilder/zdict.c +100 -101
  83. data/contrib/zstd/lib/legacy/zstd_legacy.h +38 -1
  84. data/contrib/zstd/lib/legacy/zstd_v01.c +18 -53
  85. data/contrib/zstd/lib/legacy/zstd_v01.h +1 -1
  86. data/contrib/zstd/lib/legacy/zstd_v02.c +28 -85
  87. data/contrib/zstd/lib/legacy/zstd_v02.h +1 -1
  88. data/contrib/zstd/lib/legacy/zstd_v03.c +29 -88
  89. data/contrib/zstd/lib/legacy/zstd_v03.h +1 -1
  90. data/contrib/zstd/lib/legacy/zstd_v04.c +27 -80
  91. data/contrib/zstd/lib/legacy/zstd_v04.h +1 -1
  92. data/contrib/zstd/lib/legacy/zstd_v05.c +36 -85
  93. data/contrib/zstd/lib/legacy/zstd_v05.h +1 -1
  94. data/contrib/zstd/lib/legacy/zstd_v06.c +44 -96
  95. data/contrib/zstd/lib/legacy/zstd_v06.h +1 -1
  96. data/contrib/zstd/lib/legacy/zstd_v07.c +37 -92
  97. data/contrib/zstd/lib/legacy/zstd_v07.h +1 -1
  98. data/contrib/zstd/lib/libzstd.mk +237 -0
  99. data/contrib/zstd/lib/libzstd.pc.in +4 -3
  100. data/contrib/zstd/lib/module.modulemap +35 -0
  101. data/contrib/zstd/lib/{dictBuilder/zdict.h → zdict.h} +202 -33
  102. data/contrib/zstd/lib/zstd.h +1030 -332
  103. data/contrib/zstd/lib/{common/zstd_errors.h → zstd_errors.h} +27 -8
  104. data/ext/extconf.rb +26 -7
  105. data/ext/extzstd.c +51 -24
  106. data/ext/extzstd.h +33 -6
  107. data/ext/extzstd_stream.c +74 -31
  108. data/ext/libzstd_conf.h +0 -1
  109. data/ext/zstd_decompress_asm.S +1 -0
  110. metadata +17 -7
  111. data/contrib/zstd/appveyor.yml +0 -292
  112. data/ext/depend +0 -2
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Meta Platforms, Inc. and affiliates.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -11,6 +11,7 @@
11
11
 
12
12
  #include <stddef.h> /* size_t, ptrdiff_t */
13
13
  #include "zstd_v03.h"
14
+ #include "../common/compiler.h"
14
15
  #include "../common/error_private.h"
15
16
 
16
17
 
@@ -29,7 +30,7 @@
29
30
  low-level memory access routines
30
31
  Copyright (C) 2013-2015, Yann Collet.
31
32
 
32
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
33
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
33
34
 
34
35
  Redistribution and use in source and binary forms, with or without
35
36
  modification, are permitted provided that the following conditions are
@@ -72,20 +73,6 @@ extern "C" {
72
73
  #include <string.h> /* memcpy */
73
74
 
74
75
 
75
- /******************************************
76
- * Compiler-specific
77
- ******************************************/
78
- #if defined(__GNUC__)
79
- # define MEM_STATIC static __attribute__((unused))
80
- #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
81
- # define MEM_STATIC static inline
82
- #elif defined(_MSC_VER)
83
- # define MEM_STATIC static __inline
84
- #else
85
- # define MEM_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
86
- #endif
87
-
88
-
89
76
  /****************************************************************
90
77
  * Basic Types
91
78
  *****************************************************************/
@@ -116,27 +103,6 @@ extern "C" {
116
103
  /****************************************************************
117
104
  * Memory I/O
118
105
  *****************************************************************/
119
- /* MEM_FORCE_MEMORY_ACCESS
120
- * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
121
- * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
122
- * The below switch allow to select different access method for improved performance.
123
- * Method 0 (default) : use `memcpy()`. Safe and portable.
124
- * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).
125
- * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
126
- * Method 2 : direct access. This method is portable but violate C standard.
127
- * It can generate buggy code on targets generating assembly depending on alignment.
128
- * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
129
- * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
130
- * Prefer these methods in priority order (0 > 1 > 2)
131
- */
132
- #ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
133
- # if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
134
- # define MEM_FORCE_MEMORY_ACCESS 2
135
- # elif (defined(__INTEL_COMPILER) && !defined(WIN32)) || \
136
- (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) ))
137
- # define MEM_FORCE_MEMORY_ACCESS 1
138
- # endif
139
- #endif
140
106
 
141
107
  MEM_STATIC unsigned MEM_32bits(void) { return sizeof(void*)==4; }
142
108
  MEM_STATIC unsigned MEM_64bits(void) { return sizeof(void*)==8; }
@@ -147,33 +113,6 @@ MEM_STATIC unsigned MEM_isLittleEndian(void)
147
113
  return one.c[0];
148
114
  }
149
115
 
150
- #if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2)
151
-
152
- /* violates C standard on structure alignment.
153
- Only use if no other choice to achieve best performance on target platform */
154
- MEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*) memPtr; }
155
- MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; }
156
- MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; }
157
-
158
- MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }
159
-
160
- #elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1)
161
-
162
- /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
163
- /* currently only defined for gcc and icc */
164
- typedef union { U16 u16; U32 u32; U64 u64; } __attribute__((packed)) unalign;
165
-
166
- MEM_STATIC U16 MEM_read16(const void* ptr) { return ((const unalign*)ptr)->u16; }
167
- MEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
168
- MEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
169
-
170
- MEM_STATIC void MEM_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; }
171
-
172
- #else
173
-
174
- /* default method, safe and standard.
175
- can sometimes prove slower */
176
-
177
116
  MEM_STATIC U16 MEM_read16(const void* memPtr)
178
117
  {
179
118
  U16 val; memcpy(&val, memPtr, sizeof(val)); return val;
@@ -194,10 +133,6 @@ MEM_STATIC void MEM_write16(void* memPtr, U16 value)
194
133
  memcpy(memPtr, &value, sizeof(value));
195
134
  }
196
135
 
197
-
198
- #endif /* MEM_FORCE_MEMORY_ACCESS */
199
-
200
-
201
136
  MEM_STATIC U16 MEM_readLE16(const void* memPtr)
202
137
  {
203
138
  if (MEM_isLittleEndian())
@@ -274,7 +209,7 @@ MEM_STATIC size_t MEM_readLEST(const void* memPtr)
274
209
  header file (to include)
275
210
  Copyright (C) 2013-2015, Yann Collet.
276
211
 
277
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
212
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
278
213
 
279
214
  Redistribution and use in source and binary forms, with or without
280
215
  modification, are permitted provided that the following conditions are
@@ -356,9 +291,8 @@ MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
356
291
  MEM_STATIC unsigned BIT_highbit32 (U32 val)
357
292
  {
358
293
  # if defined(_MSC_VER) /* Visual */
359
- unsigned long r=0;
360
- _BitScanReverse ( &r, val );
361
- return (unsigned) r;
294
+ unsigned long r;
295
+ return _BitScanReverse(&r, val) ? (unsigned)r : 0;
362
296
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
363
297
  return __builtin_clz (val) ^ 31;
364
298
  # else /* Software version */
@@ -439,7 +373,7 @@ MEM_STATIC size_t BIT_lookBits(BIT_DStream_t* bitD, U32 nbBits)
439
373
  }
440
374
 
441
375
  /*! BIT_lookBitsFast :
442
- * unsafe version; only works only if nbBits >= 1 */
376
+ * unsafe version; only works if nbBits >= 1 */
443
377
  MEM_STATIC size_t BIT_lookBitsFast(BIT_DStream_t* bitD, U32 nbBits)
444
378
  {
445
379
  const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;
@@ -459,7 +393,7 @@ MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits)
459
393
  }
460
394
 
461
395
  /*!BIT_readBitsFast :
462
- * unsafe version; only works only if nbBits >= 1 */
396
+ * unsafe version; only works if nbBits >= 1 */
463
397
  MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits)
464
398
  {
465
399
  size_t value = BIT_lookBitsFast(bitD, nbBits);
@@ -516,7 +450,7 @@ MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)
516
450
  Error codes and messages
517
451
  Copyright (C) 2013-2015, Yann Collet
518
452
 
519
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
453
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
520
454
 
521
455
  Redistribution and use in source and binary forms, with or without
522
456
  modification, are permitted provided that the following conditions are
@@ -615,7 +549,7 @@ typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be mor
615
549
  header file for static linking (only)
616
550
  Copyright (C) 2013-2015, Yann Collet
617
551
 
618
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
552
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
619
553
 
620
554
  Redistribution and use in source and binary forms, with or without
621
555
  modification, are permitted provided that the following conditions are
@@ -759,7 +693,7 @@ MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)
759
693
  header file for static linking (only)
760
694
  Copyright (C) 2013-2015, Yann Collet
761
695
 
762
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
696
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
763
697
 
764
698
  Redistribution and use in source and binary forms, with or without
765
699
  modification, are permitted provided that the following conditions are
@@ -827,7 +761,7 @@ static size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, si
827
761
  Header File
828
762
  Copyright (C) 2014-2015, Yann Collet.
829
763
 
830
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
764
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
831
765
 
832
766
  Redistribution and use in source and binary forms, with or without
833
767
  modification, are permitted provided that the following conditions are
@@ -887,7 +821,7 @@ typedef struct ZSTD_CCtx_s ZSTD_CCtx; /* incomplete type */
887
821
  Header File for static linking only
888
822
  Copyright (C) 2014-2015, Yann Collet.
889
823
 
890
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
824
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
891
825
 
892
826
  Redistribution and use in source and binary forms, with or without
893
827
  modification, are permitted provided that the following conditions are
@@ -928,7 +862,7 @@ extern "C" {
928
862
  * Streaming functions
929
863
  ***************************************/
930
864
 
931
- typedef struct ZSTD_DCtx_s ZSTD_DCtx;
865
+ typedef struct ZSTDv03_Dctx_s ZSTD_DCtx;
932
866
 
933
867
  /*
934
868
  Use above functions alternatively.
@@ -951,7 +885,7 @@ typedef struct ZSTD_DCtx_s ZSTD_DCtx;
951
885
  FSE : Finite State Entropy coder
952
886
  Copyright (C) 2013-2015, Yann Collet.
953
887
 
954
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
888
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
955
889
 
956
890
  Redistribution and use in source and binary forms, with or without
957
891
  modification, are permitted provided that the following conditions are
@@ -1455,7 +1389,7 @@ static size_t FSE_decompress(void* dst, size_t maxDstSize, const void* cSrc, siz
1455
1389
  Huff0 : Huffman coder, part of New Generation Entropy library
1456
1390
  Copyright (C) 2013-2015, Yann Collet.
1457
1391
 
1458
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
1392
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
1459
1393
 
1460
1394
  Redistribution and use in source and binary forms, with or without
1461
1395
  modification, are permitted provided that the following conditions are
@@ -2252,7 +2186,7 @@ static size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_
2252
2186
  zstd - standard compression library
2253
2187
  Copyright (C) 2014-2015, Yann Collet.
2254
2188
 
2255
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
2189
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
2256
2190
 
2257
2191
  Redistribution and use in source and binary forms, with or without
2258
2192
  modification, are permitted provided that the following conditions are
@@ -2443,7 +2377,7 @@ static unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
2443
2377
  /* *************************************************************
2444
2378
  * Decompression section
2445
2379
  ***************************************************************/
2446
- struct ZSTD_DCtx_s
2380
+ struct ZSTDv03_Dctx_s
2447
2381
  {
2448
2382
  U32 LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)];
2449
2383
  U32 OffTable[FSE_DTABLE_SIZE_U32(OffFSELog)];
@@ -2759,18 +2693,24 @@ static size_t ZSTD_execSequence(BYTE* op,
2759
2693
  const BYTE* const litEnd = *litPtr + sequence.litLength;
2760
2694
 
2761
2695
  /* checks */
2762
- if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of 8 from oend */
2696
+ size_t const seqLength = sequence.litLength + sequence.matchLength;
2697
+
2698
+ if (seqLength > (size_t)(oend - op)) return ERROR(dstSize_tooSmall);
2699
+ if (sequence.litLength > (size_t)(litLimit - *litPtr)) return ERROR(corruption_detected);
2700
+ /* Now we know there are no overflow in literal nor match lengths, can use pointer checks */
2701
+ if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall);
2702
+ if (sequence.offset > (U32)(oLitEnd - base)) return ERROR(corruption_detected);
2703
+
2763
2704
  if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */
2764
2705
  if (litEnd > litLimit) return ERROR(corruption_detected); /* overRead beyond lit buffer */
2765
2706
 
2766
2707
  /* copy Literals */
2767
- ZSTD_wildcopy(op, *litPtr, sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
2708
+ ZSTD_wildcopy(op, *litPtr, (ptrdiff_t)sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
2768
2709
  op = oLitEnd;
2769
2710
  *litPtr = litEnd; /* update for next sequence */
2770
2711
 
2771
2712
  /* copy Match */
2772
- {
2773
- const BYTE* match = op - sequence.offset;
2713
+ { const BYTE* match = op - sequence.offset;
2774
2714
 
2775
2715
  /* check */
2776
2716
  if (sequence.offset > (size_t)op) return ERROR(corruption_detected); /* address space overflow test (this test seems kept by clang optimizer) */
@@ -3118,6 +3058,7 @@ static size_t ZSTD_decompressContinue(ZSTD_DCtx* ctx, void* dst, size_t maxDstSi
3118
3058
  }
3119
3059
  ctx->phase = 1;
3120
3060
  ctx->expected = ZSTD_blockHeaderSize;
3061
+ if (ZSTD_isError(rSize)) return rSize;
3121
3062
  ctx->previousDstEnd = (void*)( ((char*)dst) + rSize);
3122
3063
  return rSize;
3123
3064
  }
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Meta Platforms, Inc. and affiliates.
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-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Meta Platforms, Inc. and affiliates.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -16,6 +16,7 @@
16
16
  #include <string.h> /* memcpy */
17
17
 
18
18
  #include "zstd_v04.h"
19
+ #include "../common/compiler.h"
19
20
  #include "../common/error_private.h"
20
21
 
21
22
 
@@ -37,15 +38,6 @@ extern "C" {
37
38
  # include <stdlib.h> /* _byteswap_ulong */
38
39
  # include <intrin.h> /* _byteswap_* */
39
40
  #endif
40
- #if defined(__GNUC__)
41
- # define MEM_STATIC static __attribute__((unused))
42
- #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
43
- # define MEM_STATIC static inline
44
- #elif defined(_MSC_VER)
45
- # define MEM_STATIC static __inline
46
- #else
47
- # define MEM_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
48
- #endif
49
41
 
50
42
 
51
43
  /****************************************************************
@@ -87,27 +79,6 @@ extern "C" {
87
79
  /****************************************************************
88
80
  * Memory I/O
89
81
  *****************************************************************/
90
- /* MEM_FORCE_MEMORY_ACCESS
91
- * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
92
- * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
93
- * The below switch allow to select different access method for improved performance.
94
- * Method 0 (default) : use `memcpy()`. Safe and portable.
95
- * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).
96
- * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
97
- * Method 2 : direct access. This method is portable but violate C standard.
98
- * It can generate buggy code on targets generating assembly depending on alignment.
99
- * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
100
- * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
101
- * Prefer these methods in priority order (0 > 1 > 2)
102
- */
103
- #ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
104
- # if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
105
- # define MEM_FORCE_MEMORY_ACCESS 2
106
- # elif (defined(__INTEL_COMPILER) && !defined(WIN32)) || \
107
- (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) ))
108
- # define MEM_FORCE_MEMORY_ACCESS 1
109
- # endif
110
- #endif
111
82
 
112
83
  MEM_STATIC unsigned MEM_32bits(void) { return sizeof(void*)==4; }
113
84
  MEM_STATIC unsigned MEM_64bits(void) { return sizeof(void*)==8; }
@@ -118,33 +89,6 @@ MEM_STATIC unsigned MEM_isLittleEndian(void)
118
89
  return one.c[0];
119
90
  }
120
91
 
121
- #if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2)
122
-
123
- /* violates C standard on structure alignment.
124
- Only use if no other choice to achieve best performance on target platform */
125
- MEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*) memPtr; }
126
- MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; }
127
- MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; }
128
-
129
- MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }
130
-
131
- #elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1)
132
-
133
- /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
134
- /* currently only defined for gcc and icc */
135
- typedef union { U16 u16; U32 u32; U64 u64; } __attribute__((packed)) unalign;
136
-
137
- MEM_STATIC U16 MEM_read16(const void* ptr) { return ((const unalign*)ptr)->u16; }
138
- MEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
139
- MEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
140
-
141
- MEM_STATIC void MEM_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; }
142
-
143
- #else
144
-
145
- /* default method, safe and standard.
146
- can sometimes prove slower */
147
-
148
92
  MEM_STATIC U16 MEM_read16(const void* memPtr)
149
93
  {
150
94
  U16 val; memcpy(&val, memPtr, sizeof(val)); return val;
@@ -165,9 +109,6 @@ MEM_STATIC void MEM_write16(void* memPtr, U16 value)
165
109
  memcpy(memPtr, &value, sizeof(value));
166
110
  }
167
111
 
168
- #endif /* MEM_FORCE_MEMORY_ACCESS */
169
-
170
-
171
112
  MEM_STATIC U16 MEM_readLE16(const void* memPtr)
172
113
  {
173
114
  if (MEM_isLittleEndian())
@@ -545,7 +486,7 @@ If there is an error, the function will return an error code, which can be teste
545
486
  header file (to include)
546
487
  Copyright (C) 2013-2015, Yann Collet.
547
488
 
548
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
489
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
549
490
 
550
491
  Redistribution and use in source and binary forms, with or without
551
492
  modification, are permitted provided that the following conditions are
@@ -627,9 +568,8 @@ MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
627
568
  MEM_STATIC unsigned BIT_highbit32 (U32 val)
628
569
  {
629
570
  # if defined(_MSC_VER) /* Visual */
630
- unsigned long r=0;
631
- _BitScanReverse ( &r, val );
632
- return (unsigned) r;
571
+ unsigned long r;
572
+ return _BitScanReverse(&r, val) ? (unsigned)r : 0;
633
573
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
634
574
  return __builtin_clz (val) ^ 31;
635
575
  # else /* Software version */
@@ -704,7 +644,7 @@ MEM_STATIC size_t BIT_lookBits(BIT_DStream_t* bitD, U32 nbBits)
704
644
  }
705
645
 
706
646
  /*! BIT_lookBitsFast :
707
- * unsafe version; only works only if nbBits >= 1 */
647
+ * unsafe version; only works if nbBits >= 1 */
708
648
  MEM_STATIC size_t BIT_lookBitsFast(BIT_DStream_t* bitD, U32 nbBits)
709
649
  {
710
650
  const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;
@@ -724,7 +664,7 @@ MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits)
724
664
  }
725
665
 
726
666
  /*!BIT_readBitsFast :
727
- * unsafe version; only works only if nbBits >= 1 */
667
+ * unsafe version; only works if nbBits >= 1 */
728
668
  MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits)
729
669
  {
730
670
  size_t value = BIT_lookBitsFast(bitD, nbBits);
@@ -785,7 +725,7 @@ MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)
785
725
  header file for static linking (only)
786
726
  Copyright (C) 2013-2015, Yann Collet
787
727
 
788
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
728
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
789
729
 
790
730
  Redistribution and use in source and binary forms, with or without
791
731
  modification, are permitted provided that the following conditions are
@@ -934,7 +874,7 @@ MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)
934
874
  FSE : Finite State Entropy coder
935
875
  Copyright (C) 2013-2015, Yann Collet.
936
876
 
937
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
877
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
938
878
 
939
879
  Redistribution and use in source and binary forms, with or without
940
880
  modification, are permitted provided that the following conditions are
@@ -1440,7 +1380,7 @@ static size_t FSE_decompress(void* dst, size_t maxDstSize, const void* cSrc, siz
1440
1380
  header file
1441
1381
  Copyright (C) 2013-2015, Yann Collet.
1442
1382
 
1443
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
1383
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
1444
1384
 
1445
1385
  Redistribution and use in source and binary forms, with or without
1446
1386
  modification, are permitted provided that the following conditions are
@@ -1518,7 +1458,7 @@ static unsigned HUF_isError(size_t code); /* tells if a return value i
1518
1458
  header file for static linking (only)
1519
1459
  Copyright (C) 2013-2015, Yann Collet
1520
1460
 
1521
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
1461
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
1522
1462
 
1523
1463
  Redistribution and use in source and binary forms, with or without
1524
1464
  modification, are permitted provided that the following conditions are
@@ -1605,7 +1545,7 @@ static size_t HUF_decompress4X4_usingDTable(void* dst, size_t maxDstSize, const
1605
1545
  Huff0 : Huffman coder, part of New Generation Entropy library
1606
1546
  Copyright (C) 2013-2015, Yann Collet.
1607
1547
 
1608
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
1548
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
1609
1549
 
1610
1550
  Redistribution and use in source and binary forms, with or without
1611
1551
  modification, are permitted provided that the following conditions are
@@ -2405,7 +2345,7 @@ static size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_
2405
2345
  zstd - decompression module fo v0.4 legacy format
2406
2346
  Copyright (C) 2015-2016, Yann Collet.
2407
2347
 
2408
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
2348
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
2409
2349
 
2410
2350
  Redistribution and use in source and binary forms, with or without
2411
2351
  modification, are permitted provided that the following conditions are
@@ -2880,13 +2820,19 @@ static size_t ZSTD_execSequence(BYTE* op,
2880
2820
  const BYTE* const litEnd = *litPtr + sequence.litLength;
2881
2821
  const BYTE* match = oLitEnd - sequence.offset;
2882
2822
 
2883
- /* check */
2884
- if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of 8 from oend */
2823
+ /* checks */
2824
+ size_t const seqLength = sequence.litLength + sequence.matchLength;
2825
+
2826
+ if (seqLength > (size_t)(oend - op)) return ERROR(dstSize_tooSmall);
2827
+ if (sequence.litLength > (size_t)(litLimit - *litPtr)) return ERROR(corruption_detected);
2828
+ /* Now we know there are no overflow in literal nor match lengths, can use pointer checks */
2829
+ if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall);
2830
+
2885
2831
  if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */
2886
- if (litEnd > litLimit) return ERROR(corruption_detected); /* risk read beyond lit buffer */
2832
+ if (litEnd > litLimit) return ERROR(corruption_detected); /* overRead beyond lit buffer */
2887
2833
 
2888
2834
  /* copy Literals */
2889
- ZSTD_wildcopy(op, *litPtr, sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
2835
+ ZSTD_wildcopy(op, *litPtr, (ptrdiff_t)sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
2890
2836
  op = oLitEnd;
2891
2837
  *litPtr = litEnd; /* update for next sequence */
2892
2838
 
@@ -3264,6 +3210,7 @@ static size_t ZSTD_decompressContinue(ZSTD_DCtx* ctx, void* dst, size_t maxDstSi
3264
3210
  }
3265
3211
  ctx->stage = ZSTDds_decodeBlockHeader;
3266
3212
  ctx->expected = ZSTD_blockHeaderSize;
3213
+ if (ZSTD_isError(rSize)) return rSize;
3267
3214
  ctx->previousDstEnd = (char*)dst + rSize;
3268
3215
  return rSize;
3269
3216
  }
@@ -3287,7 +3234,7 @@ static void ZSTD_decompress_insertDictionary(ZSTD_DCtx* ctx, const void* dict, s
3287
3234
  Buffered version of Zstd compression library
3288
3235
  Copyright (C) 2015, Yann Collet.
3289
3236
 
3290
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
3237
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
3291
3238
 
3292
3239
  Redistribution and use in source and binary forms, with or without
3293
3240
  modification, are permitted provided that the following conditions are
@@ -3591,8 +3538,8 @@ static size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc, void* dst, size_t* maxDs
3591
3538
  unsigned ZBUFFv04_isError(size_t errorCode) { return ERR_isError(errorCode); }
3592
3539
  const char* ZBUFFv04_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); }
3593
3540
 
3594
- size_t ZBUFFv04_recommendedDInSize() { return BLOCKSIZE + 3; }
3595
- size_t ZBUFFv04_recommendedDOutSize() { return BLOCKSIZE; }
3541
+ size_t ZBUFFv04_recommendedDInSize(void) { return BLOCKSIZE + 3; }
3542
+ size_t ZBUFFv04_recommendedDOutSize(void) { return BLOCKSIZE; }
3596
3543
 
3597
3544
 
3598
3545
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Meta Platforms, Inc. and affiliates.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the