zstd-ruby 1.4.0.0 → 1.4.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/ext/zstdruby/libzstd/Makefile +5 -0
  4. data/ext/zstdruby/libzstd/common/compiler.h +7 -0
  5. data/ext/zstdruby/libzstd/common/zstd_internal.h +58 -6
  6. data/ext/zstdruby/libzstd/compress/zstd_compress.c +175 -117
  7. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +74 -30
  8. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +56 -36
  9. data/ext/zstdruby/libzstd/compress/zstd_fast.c +35 -14
  10. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +10 -5
  11. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +1 -1
  12. data/ext/zstdruby/libzstd/compress/zstd_opt.c +45 -32
  13. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +18 -7
  14. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +1 -0
  15. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +12 -9
  16. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +20 -9
  17. data/ext/zstdruby/libzstd/dictBuilder/cover.c +154 -43
  18. data/ext/zstdruby/libzstd/dictBuilder/cover.h +38 -3
  19. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +46 -39
  20. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +9 -9
  21. data/ext/zstdruby/libzstd/dictBuilder/zdict.h +5 -0
  22. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +4 -0
  23. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +95 -101
  24. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +11 -6
  25. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +11 -6
  26. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +11 -8
  27. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +88 -84
  28. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +2 -4
  29. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +2 -4
  30. data/ext/zstdruby/libzstd/zstd.h +53 -21
  31. data/lib/zstd-ruby/version.rb +1 -1
  32. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3d3a2e484e068296e89977e4bda01284174aeb6738d7ca0b81ab286abcd3b3be
4
- data.tar.gz: 736d1180bf2aaa4f958ced167b133810dabb5b9dc8eae969a4385aac96776300
3
+ metadata.gz: 615a50803fa84b7e9dfc4c52ec1cf6fe67269deeba96e6ce64703e0b7ca25876
4
+ data.tar.gz: 0c681f172ce1be8634fea10dd7f54aa9fbcca8cfaf27deab8713c16aa4e8b50c
5
5
  SHA512:
6
- metadata.gz: e4bba80e9b2e894bd3d9e734cd4d332073f7ab583f2a45e096a7e7eb015eca7a13be32790a88c6fa1b132977b22278f4e86c2d8a67af0188ce74de2fc84cf98d
7
- data.tar.gz: 7759f59d24166a83c36822934de188b18fb6b7674ab1990da1ec7985c1fdb95bc020c6b40de2de987026553a3947dcb77dc110662d6db3a1504118afbc8f73f7
6
+ metadata.gz: 38ac3457dd0054800082a5592784253fd1ef098d7e69343ee2011ba537be687dde37ba5c40e90bb64cc3d1189a5727a92c45cb2db50c32daca7baa742cbd028f
7
+ data.tar.gz: ced818685583e17de12cfb98d152f24b6a058526192a33be30fb57e577b21b2529690ab9a3e6be1f462861bd3903ee0ea6781a9d1178e5efb6b11ca1c7d4fb96
data/README.md CHANGED
@@ -10,7 +10,7 @@ See https://github.com/facebook/zstd
10
10
  Fork from https://github.com/jarredholman/ruby-zstd.
11
11
 
12
12
  ## Zstd version
13
- v1.4.0 (https://github.com/facebook/zstd/tree/v1.4.0)
13
+ v1.4.1 (https://github.com/facebook/zstd/tree/v1.4.1)
14
14
 
15
15
  ## Installation
16
16
 
@@ -17,6 +17,7 @@ LIBVER_MINOR := $(shell echo $(LIBVER_MINOR_SCRIPT))
17
17
  LIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT))
18
18
  LIBVER := $(shell echo $(LIBVER_SCRIPT))
19
19
  VERSION?= $(LIBVER)
20
+ CCVER := $(shell $(CC) --version)
20
21
 
21
22
  CPPFLAGS+= -I. -I./common -DXXH_NAMESPACE=ZSTD_
22
23
  ifeq ($(OS),Windows_NT) # MinGW assumed
@@ -45,6 +46,10 @@ ZDICT_FILES := $(sort $(wildcard dictBuilder/*.c))
45
46
  ZDEPR_FILES := $(sort $(wildcard deprecated/*.c))
46
47
  ZSTD_FILES := $(ZSTDCOMMON_FILES)
47
48
 
49
+ ifeq ($(findstring GCC,$(CCVER)),GCC)
50
+ decompress/zstd_decompress_block.o : CFLAGS+=-fno-tree-vectorize
51
+ endif
52
+
48
53
  ZSTD_LEGACY_SUPPORT ?= 5
49
54
  ZSTD_LIB_COMPRESSION ?= 1
50
55
  ZSTD_LIB_DECOMPRESSION ?= 1
@@ -127,6 +127,13 @@
127
127
  } \
128
128
  }
129
129
 
130
+ /* vectorization */
131
+ #if !defined(__clang__) && defined(__GNUC__)
132
+ # define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
133
+ #else
134
+ # define DONT_VECTORIZE
135
+ #endif
136
+
130
137
  /* disable warnings */
131
138
  #ifdef _MSC_VER /* Visual Studio */
132
139
  # include <intrin.h> /* For Visual 2005 */
@@ -34,7 +34,6 @@
34
34
  #endif
35
35
  #include "xxhash.h" /* XXH_reset, update, digest */
36
36
 
37
-
38
37
  #if defined (__cplusplus)
39
38
  extern "C" {
40
39
  #endif
@@ -193,19 +192,72 @@ static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
193
192
  * Shared functions to include for inlining
194
193
  *********************************************/
195
194
  static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
195
+
196
196
  #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
197
+ static void ZSTD_copy16(void* dst, const void* src) { memcpy(dst, src, 16); }
198
+ #define COPY16(d,s) { ZSTD_copy16(d,s); d+=16; s+=16; }
199
+
200
+ #define WILDCOPY_OVERLENGTH 8
201
+ #define VECLEN 16
202
+
203
+ typedef enum {
204
+ ZSTD_no_overlap,
205
+ ZSTD_overlap_src_before_dst,
206
+ /* ZSTD_overlap_dst_before_src, */
207
+ } ZSTD_overlap_e;
197
208
 
198
209
  /*! ZSTD_wildcopy() :
199
210
  * custom version of memcpy(), can overwrite up to WILDCOPY_OVERLENGTH bytes (if length==0) */
200
- #define WILDCOPY_OVERLENGTH 8
201
- MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length)
211
+ MEM_STATIC FORCE_INLINE_ATTR DONT_VECTORIZE
212
+ void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e ovtype)
202
213
  {
214
+ ptrdiff_t diff = (BYTE*)dst - (const BYTE*)src;
203
215
  const BYTE* ip = (const BYTE*)src;
204
216
  BYTE* op = (BYTE*)dst;
205
217
  BYTE* const oend = op + length;
206
- do
207
- COPY8(op, ip)
208
- while (op < oend);
218
+
219
+ assert(diff >= 8 || (ovtype == ZSTD_no_overlap && diff < -8));
220
+ if (length < VECLEN || (ovtype == ZSTD_overlap_src_before_dst && diff < VECLEN)) {
221
+ do
222
+ COPY8(op, ip)
223
+ while (op < oend);
224
+ }
225
+ else {
226
+ if ((length & 8) == 0)
227
+ COPY8(op, ip);
228
+ do {
229
+ COPY16(op, ip);
230
+ }
231
+ while (op < oend);
232
+ }
233
+ }
234
+
235
+ /*! ZSTD_wildcopy_16min() :
236
+ * same semantics as ZSTD_wilcopy() except guaranteed to be able to copy 16 bytes at the start */
237
+ MEM_STATIC FORCE_INLINE_ATTR DONT_VECTORIZE
238
+ void ZSTD_wildcopy_16min(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e ovtype)
239
+ {
240
+ ptrdiff_t diff = (BYTE*)dst - (const BYTE*)src;
241
+ const BYTE* ip = (const BYTE*)src;
242
+ BYTE* op = (BYTE*)dst;
243
+ BYTE* const oend = op + length;
244
+
245
+ assert(length >= 8);
246
+ assert(diff >= 8 || (ovtype == ZSTD_no_overlap && diff < -8));
247
+
248
+ if (ovtype == ZSTD_overlap_src_before_dst && diff < VECLEN) {
249
+ do
250
+ COPY8(op, ip)
251
+ while (op < oend);
252
+ }
253
+ else {
254
+ if ((length & 8) == 0)
255
+ COPY8(op, ip);
256
+ do {
257
+ COPY16(op, ip);
258
+ }
259
+ while (op < oend);
260
+ }
209
261
  }
210
262
 
211
263
  MEM_STATIC void ZSTD_wildcopy_e(void* dst, const void* src, void* dstEnd) /* should be faster for decoding, but strangely, not verified on all platform */
@@ -385,6 +385,11 @@ ZSTD_bounds ZSTD_cParam_getBounds(ZSTD_cParameter param)
385
385
  bounds.upperBound = ZSTD_lcm_uncompressed;
386
386
  return bounds;
387
387
 
388
+ case ZSTD_c_targetCBlockSize:
389
+ bounds.lowerBound = ZSTD_TARGETCBLOCKSIZE_MIN;
390
+ bounds.upperBound = ZSTD_TARGETCBLOCKSIZE_MAX;
391
+ return bounds;
392
+
388
393
  default:
389
394
  { ZSTD_bounds const boundError = { ERROR(parameter_unsupported), 0, 0 };
390
395
  return boundError;
@@ -452,6 +457,7 @@ static int ZSTD_isUpdateAuthorized(ZSTD_cParameter param)
452
457
  case ZSTD_c_ldmHashRateLog:
453
458
  case ZSTD_c_forceAttachDict:
454
459
  case ZSTD_c_literalCompressionMode:
460
+ case ZSTD_c_targetCBlockSize:
455
461
  default:
456
462
  return 0;
457
463
  }
@@ -497,6 +503,7 @@ size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int value)
497
503
  case ZSTD_c_ldmHashLog:
498
504
  case ZSTD_c_ldmMinMatch:
499
505
  case ZSTD_c_ldmBucketSizeLog:
506
+ case ZSTD_c_targetCBlockSize:
500
507
  break;
501
508
 
502
509
  default: RETURN_ERROR(parameter_unsupported);
@@ -671,6 +678,12 @@ size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* CCtxParams,
671
678
  CCtxParams->ldmParams.hashRateLog = value;
672
679
  return CCtxParams->ldmParams.hashRateLog;
673
680
 
681
+ case ZSTD_c_targetCBlockSize :
682
+ if (value!=0) /* 0 ==> default */
683
+ BOUNDCHECK(ZSTD_c_targetCBlockSize, value);
684
+ CCtxParams->targetCBlockSize = value;
685
+ return CCtxParams->targetCBlockSize;
686
+
674
687
  default: RETURN_ERROR(parameter_unsupported, "unknown parameter");
675
688
  }
676
689
  }
@@ -692,13 +705,13 @@ size_t ZSTD_CCtxParams_getParameter(
692
705
  *value = CCtxParams->compressionLevel;
693
706
  break;
694
707
  case ZSTD_c_windowLog :
695
- *value = CCtxParams->cParams.windowLog;
708
+ *value = (int)CCtxParams->cParams.windowLog;
696
709
  break;
697
710
  case ZSTD_c_hashLog :
698
- *value = CCtxParams->cParams.hashLog;
711
+ *value = (int)CCtxParams->cParams.hashLog;
699
712
  break;
700
713
  case ZSTD_c_chainLog :
701
- *value = CCtxParams->cParams.chainLog;
714
+ *value = (int)CCtxParams->cParams.chainLog;
702
715
  break;
703
716
  case ZSTD_c_searchLog :
704
717
  *value = CCtxParams->cParams.searchLog;
@@ -773,6 +786,9 @@ size_t ZSTD_CCtxParams_getParameter(
773
786
  case ZSTD_c_ldmHashRateLog :
774
787
  *value = CCtxParams->ldmParams.hashRateLog;
775
788
  break;
789
+ case ZSTD_c_targetCBlockSize :
790
+ *value = (int)CCtxParams->targetCBlockSize;
791
+ break;
776
792
  default: RETURN_ERROR(parameter_unsupported, "unknown parameter");
777
793
  }
778
794
  return 0;
@@ -930,12 +946,12 @@ size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset)
930
946
  @return : 0, or an error code if one value is beyond authorized range */
931
947
  size_t ZSTD_checkCParams(ZSTD_compressionParameters cParams)
932
948
  {
933
- BOUNDCHECK(ZSTD_c_windowLog, cParams.windowLog);
934
- BOUNDCHECK(ZSTD_c_chainLog, cParams.chainLog);
935
- BOUNDCHECK(ZSTD_c_hashLog, cParams.hashLog);
936
- BOUNDCHECK(ZSTD_c_searchLog, cParams.searchLog);
937
- BOUNDCHECK(ZSTD_c_minMatch, cParams.minMatch);
938
- BOUNDCHECK(ZSTD_c_targetLength,cParams.targetLength);
949
+ BOUNDCHECK(ZSTD_c_windowLog, (int)cParams.windowLog);
950
+ BOUNDCHECK(ZSTD_c_chainLog, (int)cParams.chainLog);
951
+ BOUNDCHECK(ZSTD_c_hashLog, (int)cParams.hashLog);
952
+ BOUNDCHECK(ZSTD_c_searchLog, (int)cParams.searchLog);
953
+ BOUNDCHECK(ZSTD_c_minMatch, (int)cParams.minMatch);
954
+ BOUNDCHECK(ZSTD_c_targetLength,(int)cParams.targetLength);
939
955
  BOUNDCHECK(ZSTD_c_strategy, cParams.strategy);
940
956
  return 0;
941
957
  }
@@ -951,7 +967,7 @@ ZSTD_clampCParams(ZSTD_compressionParameters cParams)
951
967
  if ((int)val<bounds.lowerBound) val=(type)bounds.lowerBound; \
952
968
  else if ((int)val>bounds.upperBound) val=(type)bounds.upperBound; \
953
969
  }
954
- # define CLAMP(cParam, val) CLAMP_TYPE(cParam, val, int)
970
+ # define CLAMP(cParam, val) CLAMP_TYPE(cParam, val, unsigned)
955
971
  CLAMP(ZSTD_c_windowLog, cParams.windowLog);
956
972
  CLAMP(ZSTD_c_chainLog, cParams.chainLog);
957
973
  CLAMP(ZSTD_c_hashLog, cParams.hashLog);
@@ -1282,15 +1298,14 @@ static void ZSTD_reset_compressedBlockState(ZSTD_compressedBlockState_t* bs)
1282
1298
  }
1283
1299
 
1284
1300
  /*! ZSTD_invalidateMatchState()
1285
- * Invalidate all the matches in the match finder tables.
1286
- * Requires nextSrc and base to be set (can be NULL).
1301
+ * Invalidate all the matches in the match finder tables.
1302
+ * Requires nextSrc and base to be set (can be NULL).
1287
1303
  */
1288
1304
  static void ZSTD_invalidateMatchState(ZSTD_matchState_t* ms)
1289
1305
  {
1290
1306
  ZSTD_window_clear(&ms->window);
1291
1307
 
1292
1308
  ms->nextToUpdate = ms->window.dictLimit;
1293
- ms->nextToUpdate3 = ms->window.dictLimit;
1294
1309
  ms->loadedDictEnd = 0;
1295
1310
  ms->opt.litLengthSum = 0; /* force reset of btopt stats */
1296
1311
  ms->dictMatchState = NULL;
@@ -1327,15 +1342,17 @@ static size_t ZSTD_continueCCtx(ZSTD_CCtx* cctx, ZSTD_CCtx_params params, U64 pl
1327
1342
 
1328
1343
  typedef enum { ZSTDcrp_continue, ZSTDcrp_noMemset } ZSTD_compResetPolicy_e;
1329
1344
 
1345
+ typedef enum { ZSTD_resetTarget_CDict, ZSTD_resetTarget_CCtx } ZSTD_resetTarget_e;
1346
+
1330
1347
  static void*
1331
1348
  ZSTD_reset_matchState(ZSTD_matchState_t* ms,
1332
1349
  void* ptr,
1333
1350
  const ZSTD_compressionParameters* cParams,
1334
- ZSTD_compResetPolicy_e const crp, U32 const forCCtx)
1351
+ ZSTD_compResetPolicy_e const crp, ZSTD_resetTarget_e const forWho)
1335
1352
  {
1336
1353
  size_t const chainSize = (cParams->strategy == ZSTD_fast) ? 0 : ((size_t)1 << cParams->chainLog);
1337
1354
  size_t const hSize = ((size_t)1) << cParams->hashLog;
1338
- U32 const hashLog3 = (forCCtx && cParams->minMatch==3) ? MIN(ZSTD_HASHLOG3_MAX, cParams->windowLog) : 0;
1355
+ U32 const hashLog3 = ((forWho == ZSTD_resetTarget_CCtx) && cParams->minMatch==3) ? MIN(ZSTD_HASHLOG3_MAX, cParams->windowLog) : 0;
1339
1356
  size_t const h3Size = ((size_t)1) << hashLog3;
1340
1357
  size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32);
1341
1358
 
@@ -1349,7 +1366,7 @@ ZSTD_reset_matchState(ZSTD_matchState_t* ms,
1349
1366
  ZSTD_invalidateMatchState(ms);
1350
1367
 
1351
1368
  /* opt parser space */
1352
- if (forCCtx && (cParams->strategy >= ZSTD_btopt)) {
1369
+ if ((forWho == ZSTD_resetTarget_CCtx) && (cParams->strategy >= ZSTD_btopt)) {
1353
1370
  DEBUGLOG(4, "reserving optimal parser space");
1354
1371
  ms->opt.litFreq = (unsigned*)ptr;
1355
1372
  ms->opt.litLengthFreq = ms->opt.litFreq + (1<<Litbits);
@@ -1377,6 +1394,19 @@ ZSTD_reset_matchState(ZSTD_matchState_t* ms,
1377
1394
  return ptr;
1378
1395
  }
1379
1396
 
1397
+ /* ZSTD_indexTooCloseToMax() :
1398
+ * minor optimization : prefer memset() rather than reduceIndex()
1399
+ * which is measurably slow in some circumstances (reported for Visual Studio).
1400
+ * Works when re-using a context for a lot of smallish inputs :
1401
+ * if all inputs are smaller than ZSTD_INDEXOVERFLOW_MARGIN,
1402
+ * memset() will be triggered before reduceIndex().
1403
+ */
1404
+ #define ZSTD_INDEXOVERFLOW_MARGIN (16 MB)
1405
+ static int ZSTD_indexTooCloseToMax(ZSTD_window_t w)
1406
+ {
1407
+ return (size_t)(w.nextSrc - w.base) > (ZSTD_CURRENT_MAX - ZSTD_INDEXOVERFLOW_MARGIN);
1408
+ }
1409
+
1380
1410
  #define ZSTD_WORKSPACETOOLARGE_FACTOR 3 /* define "workspace is too large" as this number of times larger than needed */
1381
1411
  #define ZSTD_WORKSPACETOOLARGE_MAXDURATION 128 /* when workspace is continuously too large
1382
1412
  * during at least this number of times,
@@ -1388,7 +1418,7 @@ ZSTD_reset_matchState(ZSTD_matchState_t* ms,
1388
1418
  note : `params` are assumed fully validated at this stage */
1389
1419
  static size_t ZSTD_resetCCtx_internal(ZSTD_CCtx* zc,
1390
1420
  ZSTD_CCtx_params params,
1391
- U64 pledgedSrcSize,
1421
+ U64 const pledgedSrcSize,
1392
1422
  ZSTD_compResetPolicy_e const crp,
1393
1423
  ZSTD_buffered_policy_e const zbuff)
1394
1424
  {
@@ -1400,13 +1430,21 @@ static size_t ZSTD_resetCCtx_internal(ZSTD_CCtx* zc,
1400
1430
  if (ZSTD_equivalentParams(zc->appliedParams, params,
1401
1431
  zc->inBuffSize,
1402
1432
  zc->seqStore.maxNbSeq, zc->seqStore.maxNbLit,
1403
- zbuff, pledgedSrcSize)) {
1404
- DEBUGLOG(4, "ZSTD_equivalentParams()==1 -> continue mode (wLog1=%u, blockSize1=%zu)",
1405
- zc->appliedParams.cParams.windowLog, zc->blockSize);
1433
+ zbuff, pledgedSrcSize) ) {
1434
+ DEBUGLOG(4, "ZSTD_equivalentParams()==1 -> consider continue mode");
1406
1435
  zc->workSpaceOversizedDuration += (zc->workSpaceOversizedDuration > 0); /* if it was too large, it still is */
1407
- if (zc->workSpaceOversizedDuration <= ZSTD_WORKSPACETOOLARGE_MAXDURATION)
1436
+ if (zc->workSpaceOversizedDuration <= ZSTD_WORKSPACETOOLARGE_MAXDURATION) {
1437
+ DEBUGLOG(4, "continue mode confirmed (wLog1=%u, blockSize1=%zu)",
1438
+ zc->appliedParams.cParams.windowLog, zc->blockSize);
1439
+ if (ZSTD_indexTooCloseToMax(zc->blockState.matchState.window)) {
1440
+ /* prefer a reset, faster than a rescale */
1441
+ ZSTD_reset_matchState(&zc->blockState.matchState,
1442
+ zc->entropyWorkspace + HUF_WORKSPACE_SIZE_U32,
1443
+ &params.cParams,
1444
+ crp, ZSTD_resetTarget_CCtx);
1445
+ }
1408
1446
  return ZSTD_continueCCtx(zc, params, pledgedSrcSize);
1409
- } }
1447
+ } } }
1410
1448
  DEBUGLOG(4, "ZSTD_equivalentParams()==0 -> reset CCtx");
1411
1449
 
1412
1450
  if (params.ldmParams.enableLdm) {
@@ -1449,7 +1487,7 @@ static size_t ZSTD_resetCCtx_internal(ZSTD_CCtx* zc,
1449
1487
  DEBUGLOG(4, "windowSize: %zu - blockSize: %zu", windowSize, blockSize);
1450
1488
 
1451
1489
  if (workSpaceTooSmall || workSpaceWasteful) {
1452
- DEBUGLOG(4, "Need to resize workSpaceSize from %zuKB to %zuKB",
1490
+ DEBUGLOG(4, "Resize workSpaceSize from %zuKB to %zuKB",
1453
1491
  zc->workSpaceSize >> 10,
1454
1492
  neededSpace >> 10);
1455
1493
 
@@ -1491,7 +1529,10 @@ static size_t ZSTD_resetCCtx_internal(ZSTD_CCtx* zc,
1491
1529
 
1492
1530
  ZSTD_reset_compressedBlockState(zc->blockState.prevCBlock);
1493
1531
 
1494
- ptr = zc->entropyWorkspace + HUF_WORKSPACE_SIZE_U32;
1532
+ ptr = ZSTD_reset_matchState(&zc->blockState.matchState,
1533
+ zc->entropyWorkspace + HUF_WORKSPACE_SIZE_U32,
1534
+ &params.cParams,
1535
+ crp, ZSTD_resetTarget_CCtx);
1495
1536
 
1496
1537
  /* ldm hash table */
1497
1538
  /* initialize bucketOffsets table later for pointer alignment */
@@ -1509,8 +1550,6 @@ static size_t ZSTD_resetCCtx_internal(ZSTD_CCtx* zc,
1509
1550
  }
1510
1551
  assert(((size_t)ptr & 3) == 0); /* ensure ptr is properly aligned */
1511
1552
 
1512
- ptr = ZSTD_reset_matchState(&zc->blockState.matchState, ptr, &params.cParams, crp, /* forCCtx */ 1);
1513
-
1514
1553
  /* sequences storage */
1515
1554
  zc->seqStore.maxNbSeq = maxNbSeq;
1516
1555
  zc->seqStore.sequencesStart = (seqDef*)ptr;
@@ -1587,15 +1626,14 @@ static int ZSTD_shouldAttachDict(const ZSTD_CDict* cdict,
1587
1626
  * handled in _enforceMaxDist */
1588
1627
  }
1589
1628
 
1590
- static size_t ZSTD_resetCCtx_byAttachingCDict(
1591
- ZSTD_CCtx* cctx,
1592
- const ZSTD_CDict* cdict,
1593
- ZSTD_CCtx_params params,
1594
- U64 pledgedSrcSize,
1595
- ZSTD_buffered_policy_e zbuff)
1629
+ static size_t
1630
+ ZSTD_resetCCtx_byAttachingCDict(ZSTD_CCtx* cctx,
1631
+ const ZSTD_CDict* cdict,
1632
+ ZSTD_CCtx_params params,
1633
+ U64 pledgedSrcSize,
1634
+ ZSTD_buffered_policy_e zbuff)
1596
1635
  {
1597
- {
1598
- const ZSTD_compressionParameters *cdict_cParams = &cdict->matchState.cParams;
1636
+ { const ZSTD_compressionParameters* const cdict_cParams = &cdict->matchState.cParams;
1599
1637
  unsigned const windowLog = params.cParams.windowLog;
1600
1638
  assert(windowLog != 0);
1601
1639
  /* Resize working context table params for input only, since the dict
@@ -1607,8 +1645,7 @@ static size_t ZSTD_resetCCtx_byAttachingCDict(
1607
1645
  assert(cctx->appliedParams.cParams.strategy == cdict_cParams->strategy);
1608
1646
  }
1609
1647
 
1610
- {
1611
- const U32 cdictEnd = (U32)( cdict->matchState.window.nextSrc
1648
+ { const U32 cdictEnd = (U32)( cdict->matchState.window.nextSrc
1612
1649
  - cdict->matchState.window.base);
1613
1650
  const U32 cdictLen = cdictEnd - cdict->matchState.window.dictLimit;
1614
1651
  if (cdictLen == 0) {
@@ -1625,9 +1662,9 @@ static size_t ZSTD_resetCCtx_byAttachingCDict(
1625
1662
  cctx->blockState.matchState.window.base + cdictEnd;
1626
1663
  ZSTD_window_clear(&cctx->blockState.matchState.window);
1627
1664
  }
1665
+ /* loadedDictEnd is expressed within the referential of the active context */
1628
1666
  cctx->blockState.matchState.loadedDictEnd = cctx->blockState.matchState.window.dictLimit;
1629
- }
1630
- }
1667
+ } }
1631
1668
 
1632
1669
  cctx->dictID = cdict->dictID;
1633
1670
 
@@ -1681,7 +1718,6 @@ static size_t ZSTD_resetCCtx_byCopyingCDict(ZSTD_CCtx* cctx,
1681
1718
  ZSTD_matchState_t* dstMatchState = &cctx->blockState.matchState;
1682
1719
  dstMatchState->window = srcMatchState->window;
1683
1720
  dstMatchState->nextToUpdate = srcMatchState->nextToUpdate;
1684
- dstMatchState->nextToUpdate3= srcMatchState->nextToUpdate3;
1685
1721
  dstMatchState->loadedDictEnd= srcMatchState->loadedDictEnd;
1686
1722
  }
1687
1723
 
@@ -1761,7 +1797,6 @@ static size_t ZSTD_copyCCtx_internal(ZSTD_CCtx* dstCCtx,
1761
1797
  ZSTD_matchState_t* dstMatchState = &dstCCtx->blockState.matchState;
1762
1798
  dstMatchState->window = srcMatchState->window;
1763
1799
  dstMatchState->nextToUpdate = srcMatchState->nextToUpdate;
1764
- dstMatchState->nextToUpdate3= srcMatchState->nextToUpdate3;
1765
1800
  dstMatchState->loadedDictEnd= srcMatchState->loadedDictEnd;
1766
1801
  }
1767
1802
  dstCCtx->dictID = srcCCtx->dictID;
@@ -1831,16 +1866,15 @@ static void ZSTD_reduceTable_btlazy2(U32* const table, U32 const size, U32 const
1831
1866
 
1832
1867
  /*! ZSTD_reduceIndex() :
1833
1868
  * rescale all indexes to avoid future overflow (indexes are U32) */
1834
- static void ZSTD_reduceIndex (ZSTD_CCtx* zc, const U32 reducerValue)
1869
+ static void ZSTD_reduceIndex (ZSTD_matchState_t* ms, ZSTD_CCtx_params const* params, const U32 reducerValue)
1835
1870
  {
1836
- ZSTD_matchState_t* const ms = &zc->blockState.matchState;
1837
- { U32 const hSize = (U32)1 << zc->appliedParams.cParams.hashLog;
1871
+ { U32 const hSize = (U32)1 << params->cParams.hashLog;
1838
1872
  ZSTD_reduceTable(ms->hashTable, hSize, reducerValue);
1839
1873
  }
1840
1874
 
1841
- if (zc->appliedParams.cParams.strategy != ZSTD_fast) {
1842
- U32 const chainSize = (U32)1 << zc->appliedParams.cParams.chainLog;
1843
- if (zc->appliedParams.cParams.strategy == ZSTD_btlazy2)
1875
+ if (params->cParams.strategy != ZSTD_fast) {
1876
+ U32 const chainSize = (U32)1 << params->cParams.chainLog;
1877
+ if (params->cParams.strategy == ZSTD_btlazy2)
1844
1878
  ZSTD_reduceTable_btlazy2(ms->chainTable, chainSize, reducerValue);
1845
1879
  else
1846
1880
  ZSTD_reduceTable(ms->chainTable, chainSize, reducerValue);
@@ -2524,6 +2558,7 @@ ZSTD_compressSequences_internal(seqStore_t* seqStorePtr,
2524
2558
  op[0] = (BYTE)((nbSeq>>8) + 0x80), op[1] = (BYTE)nbSeq, op+=2;
2525
2559
  else
2526
2560
  op[0]=0xFF, MEM_writeLE16(op+1, (U16)(nbSeq - LONGNBSEQ)), op+=3;
2561
+ assert(op <= oend);
2527
2562
  if (nbSeq==0) {
2528
2563
  /* Copy the old tables over as if we repeated them */
2529
2564
  memcpy(&nextEntropy->fse, &prevEntropy->fse, sizeof(prevEntropy->fse));
@@ -2532,6 +2567,7 @@ ZSTD_compressSequences_internal(seqStore_t* seqStorePtr,
2532
2567
 
2533
2568
  /* seqHead : flags for FSE encoding type */
2534
2569
  seqHead = op++;
2570
+ assert(op <= oend);
2535
2571
 
2536
2572
  /* convert length/distances into codes */
2537
2573
  ZSTD_seqToCodes(seqStorePtr);
@@ -2555,6 +2591,7 @@ ZSTD_compressSequences_internal(seqStore_t* seqStorePtr,
2555
2591
  if (LLtype == set_compressed)
2556
2592
  lastNCount = op;
2557
2593
  op += countSize;
2594
+ assert(op <= oend);
2558
2595
  } }
2559
2596
  /* build CTable for Offsets */
2560
2597
  { unsigned max = MaxOff;
@@ -2577,6 +2614,7 @@ ZSTD_compressSequences_internal(seqStore_t* seqStorePtr,
2577
2614
  if (Offtype == set_compressed)
2578
2615
  lastNCount = op;
2579
2616
  op += countSize;
2617
+ assert(op <= oend);
2580
2618
  } }
2581
2619
  /* build CTable for MatchLengths */
2582
2620
  { unsigned max = MaxML;
@@ -2597,6 +2635,7 @@ ZSTD_compressSequences_internal(seqStore_t* seqStorePtr,
2597
2635
  if (MLtype == set_compressed)
2598
2636
  lastNCount = op;
2599
2637
  op += countSize;
2638
+ assert(op <= oend);
2600
2639
  } }
2601
2640
 
2602
2641
  *seqHead = (BYTE)((LLtype<<6) + (Offtype<<4) + (MLtype<<2));
@@ -2610,6 +2649,7 @@ ZSTD_compressSequences_internal(seqStore_t* seqStorePtr,
2610
2649
  longOffsets, bmi2);
2611
2650
  FORWARD_IF_ERROR(bitstreamSize);
2612
2651
  op += bitstreamSize;
2652
+ assert(op <= oend);
2613
2653
  /* zstd versions <= 1.3.4 mistakenly report corruption when
2614
2654
  * FSE_readNCount() receives a buffer < 4 bytes.
2615
2655
  * Fixed by https://github.com/facebook/zstd/pull/1146.
@@ -2721,30 +2761,24 @@ void ZSTD_resetSeqStore(seqStore_t* ssPtr)
2721
2761
  ssPtr->longLengthID = 0;
2722
2762
  }
2723
2763
 
2724
- static size_t ZSTD_compressBlock_internal(ZSTD_CCtx* zc,
2725
- void* dst, size_t dstCapacity,
2726
- const void* src, size_t srcSize)
2764
+ typedef enum { ZSTDbss_compress, ZSTDbss_noCompress } ZSTD_buildSeqStore_e;
2765
+
2766
+ static size_t ZSTD_buildSeqStore(ZSTD_CCtx* zc, const void* src, size_t srcSize)
2727
2767
  {
2728
2768
  ZSTD_matchState_t* const ms = &zc->blockState.matchState;
2729
- size_t cSize;
2730
- DEBUGLOG(5, "ZSTD_compressBlock_internal (dstCapacity=%u, dictLimit=%u, nextToUpdate=%u)",
2731
- (unsigned)dstCapacity, (unsigned)ms->window.dictLimit, (unsigned)ms->nextToUpdate);
2769
+ DEBUGLOG(5, "ZSTD_buildSeqStore (srcSize=%zu)", srcSize);
2732
2770
  assert(srcSize <= ZSTD_BLOCKSIZE_MAX);
2733
-
2734
2771
  /* Assert that we have correctly flushed the ctx params into the ms's copy */
2735
2772
  ZSTD_assertEqualCParams(zc->appliedParams.cParams, ms->cParams);
2736
-
2737
2773
  if (srcSize < MIN_CBLOCK_SIZE+ZSTD_blockHeaderSize+1) {
2738
2774
  ZSTD_ldm_skipSequences(&zc->externSeqStore, srcSize, zc->appliedParams.cParams.minMatch);
2739
- cSize = 0;
2740
- goto out; /* don't even attempt compression below a certain srcSize */
2775
+ return ZSTDbss_noCompress; /* don't even attempt compression below a certain srcSize */
2741
2776
  }
2742
2777
  ZSTD_resetSeqStore(&(zc->seqStore));
2743
2778
  /* required for optimal parser to read stats from dictionary */
2744
2779
  ms->opt.symbolCosts = &zc->blockState.prevCBlock->entropy;
2745
2780
  /* tell the optimal parser how we expect to compress literals */
2746
2781
  ms->opt.literalCompressionMode = zc->appliedParams.literalCompressionMode;
2747
-
2748
2782
  /* a gap between an attached dict and the current window is not safe,
2749
2783
  * they must remain adjacent,
2750
2784
  * and when that stops being the case, the dict must be unset */
@@ -2798,6 +2832,21 @@ static size_t ZSTD_compressBlock_internal(ZSTD_CCtx* zc,
2798
2832
  { const BYTE* const lastLiterals = (const BYTE*)src + srcSize - lastLLSize;
2799
2833
  ZSTD_storeLastLiterals(&zc->seqStore, lastLiterals, lastLLSize);
2800
2834
  } }
2835
+ return ZSTDbss_compress;
2836
+ }
2837
+
2838
+ static size_t ZSTD_compressBlock_internal(ZSTD_CCtx* zc,
2839
+ void* dst, size_t dstCapacity,
2840
+ const void* src, size_t srcSize)
2841
+ {
2842
+ size_t cSize;
2843
+ DEBUGLOG(5, "ZSTD_compressBlock_internal (dstCapacity=%u, dictLimit=%u, nextToUpdate=%u)",
2844
+ (unsigned)dstCapacity, (unsigned)zc->blockState.matchState.window.dictLimit, (unsigned)zc->blockState.matchState.nextToUpdate);
2845
+
2846
+ { const size_t bss = ZSTD_buildSeqStore(zc, src, srcSize);
2847
+ FORWARD_IF_ERROR(bss);
2848
+ if (bss == ZSTDbss_noCompress) { cSize = 0; goto out; }
2849
+ }
2801
2850
 
2802
2851
  /* encode sequences and literals */
2803
2852
  cSize = ZSTD_compressSequences(&zc->seqStore,
@@ -2826,6 +2875,25 @@ out:
2826
2875
  }
2827
2876
 
2828
2877
 
2878
+ static void ZSTD_overflowCorrectIfNeeded(ZSTD_matchState_t* ms, ZSTD_CCtx_params const* params, void const* ip, void const* iend)
2879
+ {
2880
+ if (ZSTD_window_needOverflowCorrection(ms->window, iend)) {
2881
+ U32 const maxDist = (U32)1 << params->cParams.windowLog;
2882
+ U32 const cycleLog = ZSTD_cycleLog(params->cParams.chainLog, params->cParams.strategy);
2883
+ U32 const correction = ZSTD_window_correctOverflow(&ms->window, cycleLog, maxDist, ip);
2884
+ ZSTD_STATIC_ASSERT(ZSTD_CHAINLOG_MAX <= 30);
2885
+ ZSTD_STATIC_ASSERT(ZSTD_WINDOWLOG_MAX_32 <= 30);
2886
+ ZSTD_STATIC_ASSERT(ZSTD_WINDOWLOG_MAX <= 31);
2887
+ ZSTD_reduceIndex(ms, params, correction);
2888
+ if (ms->nextToUpdate < correction) ms->nextToUpdate = 0;
2889
+ else ms->nextToUpdate -= correction;
2890
+ /* invalidate dictionaries on overflow correction */
2891
+ ms->loadedDictEnd = 0;
2892
+ ms->dictMatchState = NULL;
2893
+ }
2894
+ }
2895
+
2896
+
2829
2897
  /*! ZSTD_compress_frameChunk() :
2830
2898
  * Compress a chunk of data into one or multiple blocks.
2831
2899
  * All blocks will be terminated, all input will be consumed.
@@ -2844,7 +2912,7 @@ static size_t ZSTD_compress_frameChunk (ZSTD_CCtx* cctx,
2844
2912
  BYTE* const ostart = (BYTE*)dst;
2845
2913
  BYTE* op = ostart;
2846
2914
  U32 const maxDist = (U32)1 << cctx->appliedParams.cParams.windowLog;
2847
- assert(cctx->appliedParams.cParams.windowLog <= 31);
2915
+ assert(cctx->appliedParams.cParams.windowLog <= ZSTD_WINDOWLOG_MAX);
2848
2916
 
2849
2917
  DEBUGLOG(5, "ZSTD_compress_frameChunk (blockSize=%u)", (unsigned)blockSize);
2850
2918
  if (cctx->appliedParams.fParams.checksumFlag && srcSize)
@@ -2859,19 +2927,10 @@ static size_t ZSTD_compress_frameChunk (ZSTD_CCtx* cctx,
2859
2927
  "not enough space to store compressed block");
2860
2928
  if (remaining < blockSize) blockSize = remaining;
2861
2929
 
2862
- if (ZSTD_window_needOverflowCorrection(ms->window, ip + blockSize)) {
2863
- U32 const cycleLog = ZSTD_cycleLog(cctx->appliedParams.cParams.chainLog, cctx->appliedParams.cParams.strategy);
2864
- U32 const correction = ZSTD_window_correctOverflow(&ms->window, cycleLog, maxDist, ip);
2865
- ZSTD_STATIC_ASSERT(ZSTD_CHAINLOG_MAX <= 30);
2866
- ZSTD_STATIC_ASSERT(ZSTD_WINDOWLOG_MAX_32 <= 30);
2867
- ZSTD_STATIC_ASSERT(ZSTD_WINDOWLOG_MAX <= 31);
2868
- ZSTD_reduceIndex(cctx, correction);
2869
- if (ms->nextToUpdate < correction) ms->nextToUpdate = 0;
2870
- else ms->nextToUpdate -= correction;
2871
- ms->loadedDictEnd = 0;
2872
- ms->dictMatchState = NULL;
2873
- }
2874
- ZSTD_window_enforceMaxDist(&ms->window, ip + blockSize, maxDist, &ms->loadedDictEnd, &ms->dictMatchState);
2930
+ ZSTD_overflowCorrectIfNeeded(ms, &cctx->appliedParams, ip, ip + blockSize);
2931
+ ZSTD_checkDictValidity(&ms->window, ip + blockSize, maxDist, &ms->loadedDictEnd, &ms->dictMatchState);
2932
+
2933
+ /* Ensure hash/chain table insertion resumes no sooner than lowlimit */
2875
2934
  if (ms->nextToUpdate < ms->window.lowLimit) ms->nextToUpdate = ms->window.lowLimit;
2876
2935
 
2877
2936
  { size_t cSize = ZSTD_compressBlock_internal(cctx,
@@ -2899,7 +2958,7 @@ static size_t ZSTD_compress_frameChunk (ZSTD_CCtx* cctx,
2899
2958
  } }
2900
2959
 
2901
2960
  if (lastFrameChunk && (op>ostart)) cctx->stage = ZSTDcs_ending;
2902
- return op-ostart;
2961
+ return (size_t)(op-ostart);
2903
2962
  }
2904
2963
 
2905
2964
 
@@ -2991,6 +3050,7 @@ static size_t ZSTD_compressContinue_internal (ZSTD_CCtx* cctx,
2991
3050
  fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, cctx->appliedParams,
2992
3051
  cctx->pledgedSrcSizePlusOne-1, cctx->dictID);
2993
3052
  FORWARD_IF_ERROR(fhSize);
3053
+ assert(fhSize <= dstCapacity);
2994
3054
  dstCapacity -= fhSize;
2995
3055
  dst = (char*)dst + fhSize;
2996
3056
  cctx->stage = ZSTDcs_ongoing;
@@ -3007,18 +3067,7 @@ static size_t ZSTD_compressContinue_internal (ZSTD_CCtx* cctx,
3007
3067
 
3008
3068
  if (!frame) {
3009
3069
  /* overflow check and correction for block mode */
3010
- if (ZSTD_window_needOverflowCorrection(ms->window, (const char*)src + srcSize)) {
3011
- U32 const cycleLog = ZSTD_cycleLog(cctx->appliedParams.cParams.chainLog, cctx->appliedParams.cParams.strategy);
3012
- U32 const correction = ZSTD_window_correctOverflow(&ms->window, cycleLog, 1 << cctx->appliedParams.cParams.windowLog, src);
3013
- ZSTD_STATIC_ASSERT(ZSTD_CHAINLOG_MAX <= 30);
3014
- ZSTD_STATIC_ASSERT(ZSTD_WINDOWLOG_MAX_32 <= 30);
3015
- ZSTD_STATIC_ASSERT(ZSTD_WINDOWLOG_MAX <= 31);
3016
- ZSTD_reduceIndex(cctx, correction);
3017
- if (ms->nextToUpdate < correction) ms->nextToUpdate = 0;
3018
- else ms->nextToUpdate -= correction;
3019
- ms->loadedDictEnd = 0;
3020
- ms->dictMatchState = NULL;
3021
- }
3070
+ ZSTD_overflowCorrectIfNeeded(ms, &cctx->appliedParams, src, (BYTE const*)src + srcSize);
3022
3071
  }
3023
3072
 
3024
3073
  DEBUGLOG(5, "ZSTD_compressContinue_internal (blockSize=%u)", (unsigned)cctx->blockSize);
@@ -3074,7 +3123,7 @@ static size_t ZSTD_loadDictionaryContent(ZSTD_matchState_t* ms,
3074
3123
  const void* src, size_t srcSize,
3075
3124
  ZSTD_dictTableLoadMethod_e dtlm)
3076
3125
  {
3077
- const BYTE* const ip = (const BYTE*) src;
3126
+ const BYTE* ip = (const BYTE*) src;
3078
3127
  const BYTE* const iend = ip + srcSize;
3079
3128
 
3080
3129
  ZSTD_window_update(&ms->window, src, srcSize);
@@ -3085,32 +3134,42 @@ static size_t ZSTD_loadDictionaryContent(ZSTD_matchState_t* ms,
3085
3134
 
3086
3135
  if (srcSize <= HASH_READ_SIZE) return 0;
3087
3136
 
3088
- switch(params->cParams.strategy)
3089
- {
3090
- case ZSTD_fast:
3091
- ZSTD_fillHashTable(ms, iend, dtlm);
3092
- break;
3093
- case ZSTD_dfast:
3094
- ZSTD_fillDoubleHashTable(ms, iend, dtlm);
3095
- break;
3137
+ while (iend - ip > HASH_READ_SIZE) {
3138
+ size_t const remaining = iend - ip;
3139
+ size_t const chunk = MIN(remaining, ZSTD_CHUNKSIZE_MAX);
3140
+ const BYTE* const ichunk = ip + chunk;
3096
3141
 
3097
- case ZSTD_greedy:
3098
- case ZSTD_lazy:
3099
- case ZSTD_lazy2:
3100
- if (srcSize >= HASH_READ_SIZE)
3101
- ZSTD_insertAndFindFirstIndex(ms, iend-HASH_READ_SIZE);
3102
- break;
3142
+ ZSTD_overflowCorrectIfNeeded(ms, params, ip, ichunk);
3103
3143
 
3104
- case ZSTD_btlazy2: /* we want the dictionary table fully sorted */
3105
- case ZSTD_btopt:
3106
- case ZSTD_btultra:
3107
- case ZSTD_btultra2:
3108
- if (srcSize >= HASH_READ_SIZE)
3109
- ZSTD_updateTree(ms, iend-HASH_READ_SIZE, iend);
3110
- break;
3144
+ switch(params->cParams.strategy)
3145
+ {
3146
+ case ZSTD_fast:
3147
+ ZSTD_fillHashTable(ms, ichunk, dtlm);
3148
+ break;
3149
+ case ZSTD_dfast:
3150
+ ZSTD_fillDoubleHashTable(ms, ichunk, dtlm);
3151
+ break;
3111
3152
 
3112
- default:
3113
- assert(0); /* not possible : not a valid strategy id */
3153
+ case ZSTD_greedy:
3154
+ case ZSTD_lazy:
3155
+ case ZSTD_lazy2:
3156
+ if (chunk >= HASH_READ_SIZE)
3157
+ ZSTD_insertAndFindFirstIndex(ms, ichunk-HASH_READ_SIZE);
3158
+ break;
3159
+
3160
+ case ZSTD_btlazy2: /* we want the dictionary table fully sorted */
3161
+ case ZSTD_btopt:
3162
+ case ZSTD_btultra:
3163
+ case ZSTD_btultra2:
3164
+ if (chunk >= HASH_READ_SIZE)
3165
+ ZSTD_updateTree(ms, ichunk-HASH_READ_SIZE, ichunk);
3166
+ break;
3167
+
3168
+ default:
3169
+ assert(0); /* not possible : not a valid strategy id */
3170
+ }
3171
+
3172
+ ip = ichunk;
3114
3173
  }
3115
3174
 
3116
3175
  ms->nextToUpdate = (U32)(iend - ms->window.base);
@@ -3297,12 +3356,11 @@ static size_t ZSTD_compressBegin_internal(ZSTD_CCtx* cctx,
3297
3356
 
3298
3357
  FORWARD_IF_ERROR( ZSTD_resetCCtx_internal(cctx, params, pledgedSrcSize,
3299
3358
  ZSTDcrp_continue, zbuff) );
3300
- {
3301
- size_t const dictID = ZSTD_compress_insertDictionary(
3359
+ { size_t const dictID = ZSTD_compress_insertDictionary(
3302
3360
  cctx->blockState.prevCBlock, &cctx->blockState.matchState,
3303
3361
  &params, dict, dictSize, dictContentType, dtlm, cctx->entropyWorkspace);
3304
3362
  FORWARD_IF_ERROR(dictID);
3305
- assert(dictID <= (size_t)(U32)-1);
3363
+ assert(dictID <= UINT_MAX);
3306
3364
  cctx->dictID = (U32)dictID;
3307
3365
  }
3308
3366
  return 0;
@@ -3555,10 +3613,10 @@ static size_t ZSTD_initCDict_internal(
3555
3613
 
3556
3614
  /* Reset the state to no dictionary */
3557
3615
  ZSTD_reset_compressedBlockState(&cdict->cBlockState);
3558
- { void* const end = ZSTD_reset_matchState(
3559
- &cdict->matchState,
3560
- (U32*)cdict->workspace + HUF_WORKSPACE_SIZE_U32,
3561
- &cParams, ZSTDcrp_continue, /* forCCtx */ 0);
3616
+ { void* const end = ZSTD_reset_matchState(&cdict->matchState,
3617
+ (U32*)cdict->workspace + HUF_WORKSPACE_SIZE_U32,
3618
+ &cParams,
3619
+ ZSTDcrp_continue, ZSTD_resetTarget_CDict);
3562
3620
  assert(end == (char*)cdict->workspace + cdict->workspaceSize);
3563
3621
  (void)end;
3564
3622
  }
@@ -4068,7 +4126,7 @@ static size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs,
4068
4126
  case zcss_flush:
4069
4127
  DEBUGLOG(5, "flush stage");
4070
4128
  { size_t const toFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize;
4071
- size_t const flushed = ZSTD_limitCopy(op, oend-op,
4129
+ size_t const flushed = ZSTD_limitCopy(op, (size_t)(oend-op),
4072
4130
  zcs->outBuff + zcs->outBuffFlushedSize, toFlush);
4073
4131
  DEBUGLOG(5, "toFlush: %u into %u ==> flushed: %u",
4074
4132
  (unsigned)toFlush, (unsigned)(oend-op), (unsigned)flushed);
@@ -4262,7 +4320,7 @@ size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output)
4262
4320
  if (zcs->appliedParams.nbWorkers > 0) return remainingToFlush; /* minimal estimation */
4263
4321
  /* single thread mode : attempt to calculate remaining to flush more precisely */
4264
4322
  { size_t const lastBlockSize = zcs->frameEnded ? 0 : ZSTD_BLOCKHEADERSIZE;
4265
- size_t const checksumSize = zcs->frameEnded ? 0 : zcs->appliedParams.fParams.checksumFlag * 4;
4323
+ size_t const checksumSize = (size_t)(zcs->frameEnded ? 0 : zcs->appliedParams.fParams.checksumFlag * 4);
4266
4324
  size_t const toFlush = remainingToFlush + lastBlockSize + checksumSize;
4267
4325
  DEBUGLOG(4, "ZSTD_endStream : remaining to flush : %u", (unsigned)toFlush);
4268
4326
  return toFlush;