zstdlib 0.7.0-x86-mingw32 → 0.8.0-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +5 -0
  3. data/ext/zstdlib/extconf.rb +1 -1
  4. data/ext/zstdlib/ruby/zlib-3.0/zstdlib.c +4994 -0
  5. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/bitstream.h +25 -16
  6. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/compiler.h +118 -4
  7. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/cpu.h +1 -3
  8. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/debug.c +1 -1
  9. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/debug.h +12 -19
  10. data/ext/zstdlib/zstd-1.5.0/lib/common/entropy_common.c +362 -0
  11. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/error_private.c +2 -1
  12. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/error_private.h +3 -3
  13. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/fse.h +40 -12
  14. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/fse_decompress.c +139 -22
  15. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/huf.h +29 -7
  16. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/mem.h +69 -98
  17. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/pool.c +23 -17
  18. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/pool.h +2 -2
  19. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/threading.c +6 -5
  20. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/threading.h +0 -0
  21. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/xxhash.c +20 -60
  22. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/xxhash.h +2 -2
  23. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/zstd_common.c +10 -10
  24. data/ext/zstdlib/zstd-1.5.0/lib/common/zstd_deps.h +111 -0
  25. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/zstd_internal.h +105 -62
  26. data/ext/zstdlib/zstd-1.5.0/lib/common/zstd_trace.h +154 -0
  27. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/fse_compress.c +31 -24
  28. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/hist.c +27 -29
  29. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/hist.h +2 -2
  30. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/huf_compress.c +265 -126
  31. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress.c +2843 -728
  32. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_internal.h +305 -63
  33. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_literals.c +8 -8
  34. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_literals.h +1 -1
  35. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_sequences.c +29 -7
  36. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_sequences.h +1 -1
  37. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_superblock.c +22 -295
  38. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_superblock.h +1 -1
  39. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_cwksp.h +204 -67
  40. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_double_fast.c +25 -25
  41. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_double_fast.h +1 -1
  42. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_fast.c +23 -23
  43. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_fast.h +1 -1
  44. data/ext/zstdlib/zstd-1.5.0/lib/compress/zstd_lazy.c +2184 -0
  45. data/ext/zstdlib/zstd-1.5.0/lib/compress/zstd_lazy.h +125 -0
  46. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_ldm.c +314 -211
  47. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_ldm.h +9 -2
  48. data/ext/zstdlib/zstd-1.5.0/lib/compress/zstd_ldm_geartab.h +103 -0
  49. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_opt.c +191 -46
  50. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_opt.h +1 -1
  51. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstdmt_compress.c +93 -415
  52. data/ext/zstdlib/zstd-1.5.0/lib/compress/zstdmt_compress.h +110 -0
  53. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/huf_decompress.c +342 -239
  54. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_ddict.c +9 -9
  55. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_ddict.h +2 -2
  56. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_decompress.c +369 -87
  57. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_decompress_block.c +191 -75
  58. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_decompress_block.h +6 -3
  59. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_decompress_internal.h +27 -11
  60. data/ext/zstdlib/zstd-1.5.0/lib/zdict.h +452 -0
  61. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/zstd.h +568 -126
  62. data/ext/zstdlib/{zstd-1.4.5/lib/common → zstd-1.5.0/lib}/zstd_errors.h +2 -1
  63. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzclose.c +0 -0
  64. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzcompatibility.h +1 -1
  65. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzguts.h +0 -0
  66. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzlib.c +0 -0
  67. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzread.c +0 -0
  68. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzwrite.c +0 -0
  69. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/zstd_zlibwrapper.c +126 -44
  70. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/zstd_zlibwrapper.h +1 -1
  71. data/lib/2.2/zstdlib.so +0 -0
  72. data/lib/2.3/zstdlib.so +0 -0
  73. data/lib/2.4/zstdlib.so +0 -0
  74. data/lib/2.5/zstdlib.so +0 -0
  75. data/lib/2.6/zstdlib.so +0 -0
  76. data/lib/2.7/zstdlib.so +0 -0
  77. metadata +69 -64
  78. data/ext/zstdlib/zstd-1.4.5/lib/common/entropy_common.c +0 -216
  79. data/ext/zstdlib/zstd-1.4.5/lib/compress/zstd_lazy.c +0 -1138
  80. data/ext/zstdlib/zstd-1.4.5/lib/compress/zstd_lazy.h +0 -67
  81. data/ext/zstdlib/zstd-1.4.5/lib/compress/zstdmt_compress.h +0 -192
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -14,7 +14,7 @@
14
14
  /*-*******************************************************
15
15
  * Dependencies
16
16
  *********************************************************/
17
- #include <string.h> /* memcpy, memmove, memset */
17
+ #include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memmove, ZSTD_memset */
18
18
  #include "../common/cpu.h" /* bmi2 */
19
19
  #include "../common/mem.h" /* low level memory routines */
20
20
  #define FSE_STATIC_LINKING_ONLY
@@ -127,11 +127,11 @@ static size_t ZSTD_initDDict_internal(ZSTD_DDict* ddict,
127
127
  ddict->dictContent = dict;
128
128
  if (!dict) dictSize = 0;
129
129
  } else {
130
- void* const internalBuffer = ZSTD_malloc(dictSize, ddict->cMem);
130
+ void* const internalBuffer = ZSTD_customMalloc(dictSize, ddict->cMem);
131
131
  ddict->dictBuffer = internalBuffer;
132
132
  ddict->dictContent = internalBuffer;
133
133
  if (!internalBuffer) return ERROR(memory_allocation);
134
- memcpy(internalBuffer, dict, dictSize);
134
+ ZSTD_memcpy(internalBuffer, dict, dictSize);
135
135
  }
136
136
  ddict->dictSize = dictSize;
137
137
  ddict->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */
@@ -147,9 +147,9 @@ ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize,
147
147
  ZSTD_dictContentType_e dictContentType,
148
148
  ZSTD_customMem customMem)
149
149
  {
150
- if (!customMem.customAlloc ^ !customMem.customFree) return NULL;
150
+ if ((!customMem.customAlloc) ^ (!customMem.customFree)) return NULL;
151
151
 
152
- { ZSTD_DDict* const ddict = (ZSTD_DDict*) ZSTD_malloc(sizeof(ZSTD_DDict), customMem);
152
+ { ZSTD_DDict* const ddict = (ZSTD_DDict*) ZSTD_customMalloc(sizeof(ZSTD_DDict), customMem);
153
153
  if (ddict == NULL) return NULL;
154
154
  ddict->cMem = customMem;
155
155
  { size_t const initResult = ZSTD_initDDict_internal(ddict,
@@ -198,7 +198,7 @@ const ZSTD_DDict* ZSTD_initStaticDDict(
198
198
  if ((size_t)sBuffer & 7) return NULL; /* 8-aligned */
199
199
  if (sBufferSize < neededSpace) return NULL;
200
200
  if (dictLoadMethod == ZSTD_dlm_byCopy) {
201
- memcpy(ddict+1, dict, dictSize); /* local copy */
201
+ ZSTD_memcpy(ddict+1, dict, dictSize); /* local copy */
202
202
  dict = ddict+1;
203
203
  }
204
204
  if (ZSTD_isError( ZSTD_initDDict_internal(ddict,
@@ -213,8 +213,8 @@ size_t ZSTD_freeDDict(ZSTD_DDict* ddict)
213
213
  {
214
214
  if (ddict==NULL) return 0; /* support free on NULL */
215
215
  { ZSTD_customMem const cMem = ddict->cMem;
216
- ZSTD_free(ddict->dictBuffer, cMem);
217
- ZSTD_free(ddict, cMem);
216
+ ZSTD_customFree(ddict->dictBuffer, cMem);
217
+ ZSTD_customFree(ddict, cMem);
218
218
  return 0;
219
219
  }
220
220
  }
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -15,7 +15,7 @@
15
15
  /*-*******************************************************
16
16
  * Dependencies
17
17
  *********************************************************/
18
- #include <stddef.h> /* size_t */
18
+ #include "../common/zstd_deps.h" /* size_t */
19
19
  #include "../zstd.h" /* ZSTD_DDict, and several public functions */
20
20
 
21
21
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -55,13 +55,14 @@
55
55
  /*-*******************************************************
56
56
  * Dependencies
57
57
  *********************************************************/
58
- #include <string.h> /* memcpy, memmove, memset */
58
+ #include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memmove, ZSTD_memset */
59
59
  #include "../common/cpu.h" /* bmi2 */
60
60
  #include "../common/mem.h" /* low level memory routines */
61
61
  #define FSE_STATIC_LINKING_ONLY
62
62
  #include "../common/fse.h"
63
63
  #define HUF_STATIC_LINKING_ONLY
64
64
  #include "../common/huf.h"
65
+ #include "../common/xxhash.h" /* XXH64_reset, XXH64_update, XXH64_digest, XXH64 */
65
66
  #include "../common/zstd_internal.h" /* blockProperties_t */
66
67
  #include "zstd_decompress_internal.h" /* ZSTD_DCtx */
67
68
  #include "zstd_ddict.h" /* ZSTD_DDictDictContent */
@@ -72,6 +73,144 @@
72
73
  #endif
73
74
 
74
75
 
76
+
77
+ /*************************************
78
+ * Multiple DDicts Hashset internals *
79
+ *************************************/
80
+
81
+ #define DDICT_HASHSET_MAX_LOAD_FACTOR_COUNT_MULT 4
82
+ #define DDICT_HASHSET_MAX_LOAD_FACTOR_SIZE_MULT 3 /* These two constants represent SIZE_MULT/COUNT_MULT load factor without using a float.
83
+ * Currently, that means a 0.75 load factor.
84
+ * So, if count * COUNT_MULT / size * SIZE_MULT != 0, then we've exceeded
85
+ * the load factor of the ddict hash set.
86
+ */
87
+
88
+ #define DDICT_HASHSET_TABLE_BASE_SIZE 64
89
+ #define DDICT_HASHSET_RESIZE_FACTOR 2
90
+
91
+ /* Hash function to determine starting position of dict insertion within the table
92
+ * Returns an index between [0, hashSet->ddictPtrTableSize]
93
+ */
94
+ static size_t ZSTD_DDictHashSet_getIndex(const ZSTD_DDictHashSet* hashSet, U32 dictID) {
95
+ const U64 hash = XXH64(&dictID, sizeof(U32), 0);
96
+ /* DDict ptr table size is a multiple of 2, use size - 1 as mask to get index within [0, hashSet->ddictPtrTableSize) */
97
+ return hash & (hashSet->ddictPtrTableSize - 1);
98
+ }
99
+
100
+ /* Adds DDict to a hashset without resizing it.
101
+ * If inserting a DDict with a dictID that already exists in the set, replaces the one in the set.
102
+ * Returns 0 if successful, or a zstd error code if something went wrong.
103
+ */
104
+ static size_t ZSTD_DDictHashSet_emplaceDDict(ZSTD_DDictHashSet* hashSet, const ZSTD_DDict* ddict) {
105
+ const U32 dictID = ZSTD_getDictID_fromDDict(ddict);
106
+ size_t idx = ZSTD_DDictHashSet_getIndex(hashSet, dictID);
107
+ const size_t idxRangeMask = hashSet->ddictPtrTableSize - 1;
108
+ RETURN_ERROR_IF(hashSet->ddictPtrCount == hashSet->ddictPtrTableSize, GENERIC, "Hash set is full!");
109
+ DEBUGLOG(4, "Hashed index: for dictID: %u is %zu", dictID, idx);
110
+ while (hashSet->ddictPtrTable[idx] != NULL) {
111
+ /* Replace existing ddict if inserting ddict with same dictID */
112
+ if (ZSTD_getDictID_fromDDict(hashSet->ddictPtrTable[idx]) == dictID) {
113
+ DEBUGLOG(4, "DictID already exists, replacing rather than adding");
114
+ hashSet->ddictPtrTable[idx] = ddict;
115
+ return 0;
116
+ }
117
+ idx &= idxRangeMask;
118
+ idx++;
119
+ }
120
+ DEBUGLOG(4, "Final idx after probing for dictID %u is: %zu", dictID, idx);
121
+ hashSet->ddictPtrTable[idx] = ddict;
122
+ hashSet->ddictPtrCount++;
123
+ return 0;
124
+ }
125
+
126
+ /* Expands hash table by factor of DDICT_HASHSET_RESIZE_FACTOR and
127
+ * rehashes all values, allocates new table, frees old table.
128
+ * Returns 0 on success, otherwise a zstd error code.
129
+ */
130
+ static size_t ZSTD_DDictHashSet_expand(ZSTD_DDictHashSet* hashSet, ZSTD_customMem customMem) {
131
+ size_t newTableSize = hashSet->ddictPtrTableSize * DDICT_HASHSET_RESIZE_FACTOR;
132
+ const ZSTD_DDict** newTable = (const ZSTD_DDict**)ZSTD_customCalloc(sizeof(ZSTD_DDict*) * newTableSize, customMem);
133
+ const ZSTD_DDict** oldTable = hashSet->ddictPtrTable;
134
+ size_t oldTableSize = hashSet->ddictPtrTableSize;
135
+ size_t i;
136
+
137
+ DEBUGLOG(4, "Expanding DDict hash table! Old size: %zu new size: %zu", oldTableSize, newTableSize);
138
+ RETURN_ERROR_IF(!newTable, memory_allocation, "Expanded hashset allocation failed!");
139
+ hashSet->ddictPtrTable = newTable;
140
+ hashSet->ddictPtrTableSize = newTableSize;
141
+ hashSet->ddictPtrCount = 0;
142
+ for (i = 0; i < oldTableSize; ++i) {
143
+ if (oldTable[i] != NULL) {
144
+ FORWARD_IF_ERROR(ZSTD_DDictHashSet_emplaceDDict(hashSet, oldTable[i]), "");
145
+ }
146
+ }
147
+ ZSTD_customFree((void*)oldTable, customMem);
148
+ DEBUGLOG(4, "Finished re-hash");
149
+ return 0;
150
+ }
151
+
152
+ /* Fetches a DDict with the given dictID
153
+ * Returns the ZSTD_DDict* with the requested dictID. If it doesn't exist, then returns NULL.
154
+ */
155
+ static const ZSTD_DDict* ZSTD_DDictHashSet_getDDict(ZSTD_DDictHashSet* hashSet, U32 dictID) {
156
+ size_t idx = ZSTD_DDictHashSet_getIndex(hashSet, dictID);
157
+ const size_t idxRangeMask = hashSet->ddictPtrTableSize - 1;
158
+ DEBUGLOG(4, "Hashed index: for dictID: %u is %zu", dictID, idx);
159
+ for (;;) {
160
+ size_t currDictID = ZSTD_getDictID_fromDDict(hashSet->ddictPtrTable[idx]);
161
+ if (currDictID == dictID || currDictID == 0) {
162
+ /* currDictID == 0 implies a NULL ddict entry */
163
+ break;
164
+ } else {
165
+ idx &= idxRangeMask; /* Goes to start of table when we reach the end */
166
+ idx++;
167
+ }
168
+ }
169
+ DEBUGLOG(4, "Final idx after probing for dictID %u is: %zu", dictID, idx);
170
+ return hashSet->ddictPtrTable[idx];
171
+ }
172
+
173
+ /* Allocates space for and returns a ddict hash set
174
+ * The hash set's ZSTD_DDict* table has all values automatically set to NULL to begin with.
175
+ * Returns NULL if allocation failed.
176
+ */
177
+ static ZSTD_DDictHashSet* ZSTD_createDDictHashSet(ZSTD_customMem customMem) {
178
+ ZSTD_DDictHashSet* ret = (ZSTD_DDictHashSet*)ZSTD_customMalloc(sizeof(ZSTD_DDictHashSet), customMem);
179
+ DEBUGLOG(4, "Allocating new hash set");
180
+ ret->ddictPtrTable = (const ZSTD_DDict**)ZSTD_customCalloc(DDICT_HASHSET_TABLE_BASE_SIZE * sizeof(ZSTD_DDict*), customMem);
181
+ ret->ddictPtrTableSize = DDICT_HASHSET_TABLE_BASE_SIZE;
182
+ ret->ddictPtrCount = 0;
183
+ if (!ret || !ret->ddictPtrTable) {
184
+ return NULL;
185
+ }
186
+ return ret;
187
+ }
188
+
189
+ /* Frees the table of ZSTD_DDict* within a hashset, then frees the hashset itself.
190
+ * Note: The ZSTD_DDict* within the table are NOT freed.
191
+ */
192
+ static void ZSTD_freeDDictHashSet(ZSTD_DDictHashSet* hashSet, ZSTD_customMem customMem) {
193
+ DEBUGLOG(4, "Freeing ddict hash set");
194
+ if (hashSet && hashSet->ddictPtrTable) {
195
+ ZSTD_customFree((void*)hashSet->ddictPtrTable, customMem);
196
+ }
197
+ if (hashSet) {
198
+ ZSTD_customFree(hashSet, customMem);
199
+ }
200
+ }
201
+
202
+ /* Public function: Adds a DDict into the ZSTD_DDictHashSet, possibly triggering a resize of the hash set.
203
+ * Returns 0 on success, or a ZSTD error.
204
+ */
205
+ static size_t ZSTD_DDictHashSet_addDDict(ZSTD_DDictHashSet* hashSet, const ZSTD_DDict* ddict, ZSTD_customMem customMem) {
206
+ DEBUGLOG(4, "Adding dict ID: %u to hashset with - Count: %zu Tablesize: %zu", ZSTD_getDictID_fromDDict(ddict), hashSet->ddictPtrCount, hashSet->ddictPtrTableSize);
207
+ if (hashSet->ddictPtrCount * DDICT_HASHSET_MAX_LOAD_FACTOR_COUNT_MULT / hashSet->ddictPtrTableSize * DDICT_HASHSET_MAX_LOAD_FACTOR_SIZE_MULT != 0) {
208
+ FORWARD_IF_ERROR(ZSTD_DDictHashSet_expand(hashSet, customMem), "");
209
+ }
210
+ FORWARD_IF_ERROR(ZSTD_DDictHashSet_emplaceDDict(hashSet, ddict), "");
211
+ return 0;
212
+ }
213
+
75
214
  /*-*************************************************************
76
215
  * Context management
77
216
  ***************************************************************/
@@ -94,11 +233,19 @@ static size_t ZSTD_startingInputLength(ZSTD_format_e format)
94
233
  return startingInputLength;
95
234
  }
96
235
 
236
+ static void ZSTD_DCtx_resetParameters(ZSTD_DCtx* dctx)
237
+ {
238
+ assert(dctx->streamStage == zdss_init);
239
+ dctx->format = ZSTD_f_zstd1;
240
+ dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
241
+ dctx->outBufferMode = ZSTD_bm_buffered;
242
+ dctx->forceIgnoreChecksum = ZSTD_d_validateChecksum;
243
+ dctx->refMultipleDDicts = ZSTD_rmd_refSingleDDict;
244
+ }
245
+
97
246
  static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx)
98
247
  {
99
- dctx->format = ZSTD_f_zstd1; /* ZSTD_decompressBegin() invokes ZSTD_startingInputLength() with argument dctx->format */
100
248
  dctx->staticSize = 0;
101
- dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
102
249
  dctx->ddict = NULL;
103
250
  dctx->ddictLocal = NULL;
104
251
  dctx->dictEnd = NULL;
@@ -113,7 +260,8 @@ static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx)
113
260
  dctx->noForwardProgress = 0;
114
261
  dctx->oversizedDuration = 0;
115
262
  dctx->bmi2 = ZSTD_cpuid_bmi2(ZSTD_cpuid());
116
- dctx->outBufferMode = ZSTD_obm_buffered;
263
+ dctx->ddictSet = NULL;
264
+ ZSTD_DCtx_resetParameters(dctx);
117
265
  #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
118
266
  dctx->dictContentEndForFuzzing = NULL;
119
267
  #endif
@@ -134,9 +282,9 @@ ZSTD_DCtx* ZSTD_initStaticDCtx(void *workspace, size_t workspaceSize)
134
282
 
135
283
  ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem)
136
284
  {
137
- if (!customMem.customAlloc ^ !customMem.customFree) return NULL;
285
+ if ((!customMem.customAlloc) ^ (!customMem.customFree)) return NULL;
138
286
 
139
- { ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_malloc(sizeof(*dctx), customMem);
287
+ { ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_customMalloc(sizeof(*dctx), customMem);
140
288
  if (!dctx) return NULL;
141
289
  dctx->customMem = customMem;
142
290
  ZSTD_initDCtx_internal(dctx);
@@ -164,13 +312,17 @@ size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
164
312
  RETURN_ERROR_IF(dctx->staticSize, memory_allocation, "not compatible with static DCtx");
165
313
  { ZSTD_customMem const cMem = dctx->customMem;
166
314
  ZSTD_clearDict(dctx);
167
- ZSTD_free(dctx->inBuff, cMem);
315
+ ZSTD_customFree(dctx->inBuff, cMem);
168
316
  dctx->inBuff = NULL;
169
317
  #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
170
318
  if (dctx->legacyContext)
171
319
  ZSTD_freeLegacyStreamContext(dctx->legacyContext, dctx->previousLegacyVersion);
172
320
  #endif
173
- ZSTD_free(dctx, cMem);
321
+ if (dctx->ddictSet) {
322
+ ZSTD_freeDDictHashSet(dctx->ddictSet, cMem);
323
+ dctx->ddictSet = NULL;
324
+ }
325
+ ZSTD_customFree(dctx, cMem);
174
326
  return 0;
175
327
  }
176
328
  }
@@ -179,7 +331,30 @@ size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
179
331
  void ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx)
180
332
  {
181
333
  size_t const toCopy = (size_t)((char*)(&dstDCtx->inBuff) - (char*)dstDCtx);
182
- memcpy(dstDCtx, srcDCtx, toCopy); /* no need to copy workspace */
334
+ ZSTD_memcpy(dstDCtx, srcDCtx, toCopy); /* no need to copy workspace */
335
+ }
336
+
337
+ /* Given a dctx with a digested frame params, re-selects the correct ZSTD_DDict based on
338
+ * the requested dict ID from the frame. If there exists a reference to the correct ZSTD_DDict, then
339
+ * accordingly sets the ddict to be used to decompress the frame.
340
+ *
341
+ * If no DDict is found, then no action is taken, and the ZSTD_DCtx::ddict remains as-is.
342
+ *
343
+ * ZSTD_d_refMultipleDDicts must be enabled for this function to be called.
344
+ */
345
+ static void ZSTD_DCtx_selectFrameDDict(ZSTD_DCtx* dctx) {
346
+ assert(dctx->refMultipleDDicts && dctx->ddictSet);
347
+ DEBUGLOG(4, "Adjusting DDict based on requested dict ID from frame");
348
+ if (dctx->ddict) {
349
+ const ZSTD_DDict* frameDDict = ZSTD_DDictHashSet_getDDict(dctx->ddictSet, dctx->fParams.dictID);
350
+ if (frameDDict) {
351
+ DEBUGLOG(4, "DDict found!");
352
+ ZSTD_clearDict(dctx);
353
+ dctx->dictID = dctx->fParams.dictID;
354
+ dctx->ddict = frameDDict;
355
+ dctx->dictUses = ZSTD_use_indefinitely;
356
+ }
357
+ }
183
358
  }
184
359
 
185
360
 
@@ -246,7 +421,7 @@ size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, s
246
421
  const BYTE* ip = (const BYTE*)src;
247
422
  size_t const minInputSize = ZSTD_startingInputLength(format);
248
423
 
249
- memset(zfhPtr, 0, sizeof(*zfhPtr)); /* not strictly necessary, but static analyzer do not understand that zfhPtr is only going to be read only if return value is zero, since they are 2 different signals */
424
+ ZSTD_memset(zfhPtr, 0, sizeof(*zfhPtr)); /* not strictly necessary, but static analyzer do not understand that zfhPtr is only going to be read only if return value is zero, since they are 2 different signals */
250
425
  if (srcSize < minInputSize) return minInputSize;
251
426
  RETURN_ERROR_IF(src==NULL, GENERIC, "invalid parameter");
252
427
 
@@ -256,7 +431,7 @@ size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, s
256
431
  /* skippable frame */
257
432
  if (srcSize < ZSTD_SKIPPABLEHEADERSIZE)
258
433
  return ZSTD_SKIPPABLEHEADERSIZE; /* magic number + frame length */
259
- memset(zfhPtr, 0, sizeof(*zfhPtr));
434
+ ZSTD_memset(zfhPtr, 0, sizeof(*zfhPtr));
260
435
  zfhPtr->frameContentSize = MEM_readLE32((const char *)src + ZSTD_FRAMEIDSIZE);
261
436
  zfhPtr->frameType = ZSTD_skippableFrame;
262
437
  return 0;
@@ -433,12 +608,19 @@ unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize)
433
608
 
434
609
  /** ZSTD_decodeFrameHeader() :
435
610
  * `headerSize` must be the size provided by ZSTD_frameHeaderSize().
611
+ * If multiple DDict references are enabled, also will choose the correct DDict to use.
436
612
  * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */
437
613
  static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize)
438
614
  {
439
615
  size_t const result = ZSTD_getFrameHeader_advanced(&(dctx->fParams), src, headerSize, dctx->format);
440
616
  if (ZSTD_isError(result)) return result; /* invalid header */
441
617
  RETURN_ERROR_IF(result>0, srcSize_wrong, "headerSize too small");
618
+
619
+ /* Reference DDict requested by frame if dctx references multiple ddicts */
620
+ if (dctx->refMultipleDDicts == ZSTD_rmd_refMultipleDDicts && dctx->ddictSet) {
621
+ ZSTD_DCtx_selectFrameDDict(dctx);
622
+ }
623
+
442
624
  #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
443
625
  /* Skip the dictID check in fuzzing mode, because it makes the search
444
626
  * harder.
@@ -446,7 +628,9 @@ static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t he
446
628
  RETURN_ERROR_IF(dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID),
447
629
  dictionary_wrong, "");
448
630
  #endif
449
- if (dctx->fParams.checksumFlag) XXH64_reset(&dctx->xxhState, 0);
631
+ dctx->validateChecksum = (dctx->fParams.checksumFlag && !dctx->forceIgnoreChecksum) ? 1 : 0;
632
+ if (dctx->validateChecksum) XXH64_reset(&dctx->xxhState, 0);
633
+ dctx->processedCSize += headerSize;
450
634
  return 0;
451
635
  }
452
636
 
@@ -461,7 +645,7 @@ static ZSTD_frameSizeInfo ZSTD_errorFrameSizeInfo(size_t ret)
461
645
  static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize)
462
646
  {
463
647
  ZSTD_frameSizeInfo frameSizeInfo;
464
- memset(&frameSizeInfo, 0, sizeof(ZSTD_frameSizeInfo));
648
+ ZSTD_memset(&frameSizeInfo, 0, sizeof(ZSTD_frameSizeInfo));
465
649
 
466
650
  #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
467
651
  if (ZSTD_isLegacy(src, srcSize))
@@ -516,7 +700,7 @@ static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize
516
700
  ip += 4;
517
701
  }
518
702
 
519
- frameSizeInfo.compressedSize = ip - ipstart;
703
+ frameSizeInfo.compressedSize = (size_t)(ip - ipstart);
520
704
  frameSizeInfo.decompressedBound = (zfh.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN)
521
705
  ? zfh.frameContentSize
522
706
  : nbBlocks * zfh.blockSizeMax;
@@ -569,7 +753,7 @@ unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize)
569
753
  size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize)
570
754
  {
571
755
  DEBUGLOG(5, "ZSTD_insertBlock: %u bytes", (unsigned)blockSize);
572
- ZSTD_checkContinuity(dctx, blockStart);
756
+ ZSTD_checkContinuity(dctx, blockStart, blockSize);
573
757
  dctx->previousDstEnd = (const char*)blockStart + blockSize;
574
758
  return blockSize;
575
759
  }
@@ -579,12 +763,12 @@ static size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity,
579
763
  const void* src, size_t srcSize)
580
764
  {
581
765
  DEBUGLOG(5, "ZSTD_copyRawBlock");
766
+ RETURN_ERROR_IF(srcSize > dstCapacity, dstSize_tooSmall, "");
582
767
  if (dst == NULL) {
583
768
  if (srcSize == 0) return 0;
584
769
  RETURN_ERROR(dstBuffer_null, "");
585
770
  }
586
- RETURN_ERROR_IF(srcSize > dstCapacity, dstSize_tooSmall, "");
587
- memcpy(dst, src, srcSize);
771
+ ZSTD_memcpy(dst, src, srcSize);
588
772
  return srcSize;
589
773
  }
590
774
 
@@ -592,15 +776,41 @@ static size_t ZSTD_setRleBlock(void* dst, size_t dstCapacity,
592
776
  BYTE b,
593
777
  size_t regenSize)
594
778
  {
779
+ RETURN_ERROR_IF(regenSize > dstCapacity, dstSize_tooSmall, "");
595
780
  if (dst == NULL) {
596
781
  if (regenSize == 0) return 0;
597
782
  RETURN_ERROR(dstBuffer_null, "");
598
783
  }
599
- RETURN_ERROR_IF(regenSize > dstCapacity, dstSize_tooSmall, "");
600
- memset(dst, b, regenSize);
784
+ ZSTD_memset(dst, b, regenSize);
601
785
  return regenSize;
602
786
  }
603
787
 
788
+ static void ZSTD_DCtx_trace_end(ZSTD_DCtx const* dctx, U64 uncompressedSize, U64 compressedSize, unsigned streaming)
789
+ {
790
+ #if ZSTD_TRACE
791
+ if (dctx->traceCtx && ZSTD_trace_decompress_end != NULL) {
792
+ ZSTD_Trace trace;
793
+ ZSTD_memset(&trace, 0, sizeof(trace));
794
+ trace.version = ZSTD_VERSION_NUMBER;
795
+ trace.streaming = streaming;
796
+ if (dctx->ddict) {
797
+ trace.dictionaryID = ZSTD_getDictID_fromDDict(dctx->ddict);
798
+ trace.dictionarySize = ZSTD_DDict_dictSize(dctx->ddict);
799
+ trace.dictionaryIsCold = dctx->ddictIsCold;
800
+ }
801
+ trace.uncompressedSize = (size_t)uncompressedSize;
802
+ trace.compressedSize = (size_t)compressedSize;
803
+ trace.dctx = dctx;
804
+ ZSTD_trace_decompress_end(dctx->traceCtx, &trace);
805
+ }
806
+ #else
807
+ (void)dctx;
808
+ (void)uncompressedSize;
809
+ (void)compressedSize;
810
+ (void)streaming;
811
+ #endif
812
+ }
813
+
604
814
 
605
815
  /*! ZSTD_decompressFrame() :
606
816
  * @dctx must be properly initialized
@@ -610,8 +820,9 @@ static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx,
610
820
  void* dst, size_t dstCapacity,
611
821
  const void** srcPtr, size_t *srcSizePtr)
612
822
  {
613
- const BYTE* ip = (const BYTE*)(*srcPtr);
614
- BYTE* const ostart = (BYTE* const)dst;
823
+ const BYTE* const istart = (const BYTE*)(*srcPtr);
824
+ const BYTE* ip = istart;
825
+ BYTE* const ostart = (BYTE*)dst;
615
826
  BYTE* const oend = dstCapacity != 0 ? ostart + dstCapacity : ostart;
616
827
  BYTE* op = ostart;
617
828
  size_t remainingSrcSize = *srcSizePtr;
@@ -647,13 +858,13 @@ static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx,
647
858
  switch(blockProperties.blockType)
648
859
  {
649
860
  case bt_compressed:
650
- decodedSize = ZSTD_decompressBlock_internal(dctx, op, oend-op, ip, cBlockSize, /* frame */ 1);
861
+ decodedSize = ZSTD_decompressBlock_internal(dctx, op, (size_t)(oend-op), ip, cBlockSize, /* frame */ 1);
651
862
  break;
652
863
  case bt_raw :
653
- decodedSize = ZSTD_copyRawBlock(op, oend-op, ip, cBlockSize);
864
+ decodedSize = ZSTD_copyRawBlock(op, (size_t)(oend-op), ip, cBlockSize);
654
865
  break;
655
866
  case bt_rle :
656
- decodedSize = ZSTD_setRleBlock(op, oend-op, *ip, blockProperties.origSize);
867
+ decodedSize = ZSTD_setRleBlock(op, (size_t)(oend-op), *ip, blockProperties.origSize);
657
868
  break;
658
869
  case bt_reserved :
659
870
  default:
@@ -661,7 +872,7 @@ static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx,
661
872
  }
662
873
 
663
874
  if (ZSTD_isError(decodedSize)) return decodedSize;
664
- if (dctx->fParams.checksumFlag)
875
+ if (dctx->validateChecksum)
665
876
  XXH64_update(&dctx->xxhState, op, decodedSize);
666
877
  if (decodedSize != 0)
667
878
  op += decodedSize;
@@ -676,19 +887,21 @@ static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx,
676
887
  corruption_detected, "");
677
888
  }
678
889
  if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */
679
- U32 const checkCalc = (U32)XXH64_digest(&dctx->xxhState);
680
- U32 checkRead;
681
890
  RETURN_ERROR_IF(remainingSrcSize<4, checksum_wrong, "");
682
- checkRead = MEM_readLE32(ip);
683
- RETURN_ERROR_IF(checkRead != checkCalc, checksum_wrong, "");
891
+ if (!dctx->forceIgnoreChecksum) {
892
+ U32 const checkCalc = (U32)XXH64_digest(&dctx->xxhState);
893
+ U32 checkRead;
894
+ checkRead = MEM_readLE32(ip);
895
+ RETURN_ERROR_IF(checkRead != checkCalc, checksum_wrong, "");
896
+ }
684
897
  ip += 4;
685
898
  remainingSrcSize -= 4;
686
899
  }
687
-
900
+ ZSTD_DCtx_trace_end(dctx, (U64)(op-ostart), (U64)(ip-istart), /* streaming */ 0);
688
901
  /* Allow caller to get size read */
689
902
  *srcPtr = ip;
690
903
  *srcSizePtr = remainingSrcSize;
691
- return op-ostart;
904
+ return (size_t)(op-ostart);
692
905
  }
693
906
 
694
907
  static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
@@ -721,7 +934,7 @@ static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
721
934
  decodedSize = ZSTD_decompressLegacy(dst, dstCapacity, src, frameSize, dict, dictSize);
722
935
  if (ZSTD_isError(decodedSize)) return decodedSize;
723
936
 
724
- assert(decodedSize <=- dstCapacity);
937
+ assert(decodedSize <= dstCapacity);
725
938
  dst = (BYTE*)dst + decodedSize;
726
939
  dstCapacity -= decodedSize;
727
940
 
@@ -753,7 +966,7 @@ static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
753
966
  * use this in all cases but ddict */
754
967
  FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDict(dctx, dict, dictSize), "");
755
968
  }
756
- ZSTD_checkContinuity(dctx, dst);
969
+ ZSTD_checkContinuity(dctx, dst, dstCapacity);
757
970
 
758
971
  { const size_t res = ZSTD_decompressFrame(dctx, dst, dstCapacity,
759
972
  &src, &srcSize);
@@ -761,15 +974,13 @@ static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
761
974
  (ZSTD_getErrorCode(res) == ZSTD_error_prefix_unknown)
762
975
  && (moreThan1Frame==1),
763
976
  srcSize_wrong,
764
- "at least one frame successfully completed, but following "
765
- "bytes are garbage: it's more likely to be a srcSize error, "
766
- "specifying more bytes than compressed size of frame(s). This "
767
- "error message replaces ERROR(prefix_unknown), which would be "
768
- "confusing, as the first header is actually correct. Note that "
769
- "one could be unlucky, it might be a corruption error instead, "
770
- "happening right at the place where we expect zstd magic "
771
- "bytes. But this is _much_ less likely than a srcSize field "
772
- "error.");
977
+ "At least one frame successfully completed, "
978
+ "but following bytes are garbage: "
979
+ "it's more likely to be a srcSize error, "
980
+ "specifying more input bytes than size of frame(s). "
981
+ "Note: one could be unlucky, it might be a corruption error instead, "
982
+ "happening right at the place where we expect zstd magic bytes. "
983
+ "But this is _much_ less likely than a srcSize field error.");
773
984
  if (ZSTD_isError(res)) return res;
774
985
  assert(res <= dstCapacity);
775
986
  if (res != 0)
@@ -781,7 +992,7 @@ static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
781
992
 
782
993
  RETURN_ERROR_IF(srcSize, srcSize_wrong, "input not entirely consumed");
783
994
 
784
- return (BYTE*)dst - (BYTE*)dststart;
995
+ return (size_t)((BYTE*)dst - (BYTE*)dststart);
785
996
  }
786
997
 
787
998
  size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
@@ -890,7 +1101,9 @@ size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, c
890
1101
  DEBUGLOG(5, "ZSTD_decompressContinue (srcSize:%u)", (unsigned)srcSize);
891
1102
  /* Sanity check */
892
1103
  RETURN_ERROR_IF(srcSize != ZSTD_nextSrcSizeToDecompressWithInputSize(dctx, srcSize), srcSize_wrong, "not allowed");
893
- if (dstCapacity) ZSTD_checkContinuity(dctx, dst);
1104
+ ZSTD_checkContinuity(dctx, dst, dstCapacity);
1105
+
1106
+ dctx->processedCSize += srcSize;
894
1107
 
895
1108
  switch (dctx->stage)
896
1109
  {
@@ -899,21 +1112,21 @@ size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, c
899
1112
  if (dctx->format == ZSTD_f_zstd1) { /* allows header */
900
1113
  assert(srcSize >= ZSTD_FRAMEIDSIZE); /* to read skippable magic number */
901
1114
  if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */
902
- memcpy(dctx->headerBuffer, src, srcSize);
1115
+ ZSTD_memcpy(dctx->headerBuffer, src, srcSize);
903
1116
  dctx->expected = ZSTD_SKIPPABLEHEADERSIZE - srcSize; /* remaining to load to get full skippable frame header */
904
1117
  dctx->stage = ZSTDds_decodeSkippableHeader;
905
1118
  return 0;
906
1119
  } }
907
1120
  dctx->headerSize = ZSTD_frameHeaderSize_internal(src, srcSize, dctx->format);
908
1121
  if (ZSTD_isError(dctx->headerSize)) return dctx->headerSize;
909
- memcpy(dctx->headerBuffer, src, srcSize);
1122
+ ZSTD_memcpy(dctx->headerBuffer, src, srcSize);
910
1123
  dctx->expected = dctx->headerSize - srcSize;
911
1124
  dctx->stage = ZSTDds_decodeFrameHeader;
912
1125
  return 0;
913
1126
 
914
1127
  case ZSTDds_decodeFrameHeader:
915
1128
  assert(src != NULL);
916
- memcpy(dctx->headerBuffer + (dctx->headerSize - srcSize), src, srcSize);
1129
+ ZSTD_memcpy(dctx->headerBuffer + (dctx->headerSize - srcSize), src, srcSize);
917
1130
  FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize), "");
918
1131
  dctx->expected = ZSTD_blockHeaderSize;
919
1132
  dctx->stage = ZSTDds_decodeBlockHeader;
@@ -977,7 +1190,7 @@ size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, c
977
1190
  RETURN_ERROR_IF(rSize > dctx->fParams.blockSizeMax, corruption_detected, "Decompressed Block Size Exceeds Maximum");
978
1191
  DEBUGLOG(5, "ZSTD_decompressContinue: decoded size from block : %u", (unsigned)rSize);
979
1192
  dctx->decodedSize += rSize;
980
- if (dctx->fParams.checksumFlag) XXH64_update(&dctx->xxhState, dst, rSize);
1193
+ if (dctx->validateChecksum) XXH64_update(&dctx->xxhState, dst, rSize);
981
1194
  dctx->previousDstEnd = (char*)dst + rSize;
982
1195
 
983
1196
  /* Stay on the same stage until we are finished streaming the block. */
@@ -995,6 +1208,7 @@ size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, c
995
1208
  dctx->expected = 4;
996
1209
  dctx->stage = ZSTDds_checkChecksum;
997
1210
  } else {
1211
+ ZSTD_DCtx_trace_end(dctx, dctx->decodedSize, dctx->processedCSize, /* streaming */ 1);
998
1212
  dctx->expected = 0; /* ends here */
999
1213
  dctx->stage = ZSTDds_getFrameHeaderSize;
1000
1214
  }
@@ -1007,10 +1221,14 @@ size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, c
1007
1221
 
1008
1222
  case ZSTDds_checkChecksum:
1009
1223
  assert(srcSize == 4); /* guaranteed by dctx->expected */
1010
- { U32 const h32 = (U32)XXH64_digest(&dctx->xxhState);
1011
- U32 const check32 = MEM_readLE32(src);
1012
- DEBUGLOG(4, "ZSTD_decompressContinue: checksum : calculated %08X :: %08X read", (unsigned)h32, (unsigned)check32);
1013
- RETURN_ERROR_IF(check32 != h32, checksum_wrong, "");
1224
+ {
1225
+ if (dctx->validateChecksum) {
1226
+ U32 const h32 = (U32)XXH64_digest(&dctx->xxhState);
1227
+ U32 const check32 = MEM_readLE32(src);
1228
+ DEBUGLOG(4, "ZSTD_decompressContinue: checksum : calculated %08X :: %08X read", (unsigned)h32, (unsigned)check32);
1229
+ RETURN_ERROR_IF(check32 != h32, checksum_wrong, "");
1230
+ }
1231
+ ZSTD_DCtx_trace_end(dctx, dctx->decodedSize, dctx->processedCSize, /* streaming */ 1);
1014
1232
  dctx->expected = 0;
1015
1233
  dctx->stage = ZSTDds_getFrameHeaderSize;
1016
1234
  return 0;
@@ -1019,7 +1237,7 @@ size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, c
1019
1237
  case ZSTDds_decodeSkippableHeader:
1020
1238
  assert(src != NULL);
1021
1239
  assert(srcSize <= ZSTD_SKIPPABLEHEADERSIZE);
1022
- memcpy(dctx->headerBuffer + (ZSTD_SKIPPABLEHEADERSIZE - srcSize), src, srcSize); /* complete skippable header */
1240
+ ZSTD_memcpy(dctx->headerBuffer + (ZSTD_SKIPPABLEHEADERSIZE - srcSize), src, srcSize); /* complete skippable header */
1023
1241
  dctx->expected = MEM_readLE32(dctx->headerBuffer + ZSTD_FRAMEIDSIZE); /* note : dctx->expected can grow seriously large, beyond local buffer size */
1024
1242
  dctx->stage = ZSTDds_skipFrame;
1025
1243
  return 0;
@@ -1075,7 +1293,7 @@ ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,
1075
1293
  workspace, workspaceSize);
1076
1294
  #else
1077
1295
  size_t const hSize = HUF_readDTableX2_wksp(entropy->hufTable,
1078
- dictPtr, dictEnd - dictPtr,
1296
+ dictPtr, (size_t)(dictEnd - dictPtr),
1079
1297
  workspace, workspaceSize);
1080
1298
  #endif
1081
1299
  RETURN_ERROR_IF(HUF_isError(hSize), dictionary_corrupted, "");
@@ -1084,40 +1302,46 @@ ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,
1084
1302
 
1085
1303
  { short offcodeNCount[MaxOff+1];
1086
1304
  unsigned offcodeMaxValue = MaxOff, offcodeLog;
1087
- size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr);
1305
+ size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, (size_t)(dictEnd-dictPtr));
1088
1306
  RETURN_ERROR_IF(FSE_isError(offcodeHeaderSize), dictionary_corrupted, "");
1089
1307
  RETURN_ERROR_IF(offcodeMaxValue > MaxOff, dictionary_corrupted, "");
1090
1308
  RETURN_ERROR_IF(offcodeLog > OffFSELog, dictionary_corrupted, "");
1091
1309
  ZSTD_buildFSETable( entropy->OFTable,
1092
1310
  offcodeNCount, offcodeMaxValue,
1093
1311
  OF_base, OF_bits,
1094
- offcodeLog);
1312
+ offcodeLog,
1313
+ entropy->workspace, sizeof(entropy->workspace),
1314
+ /* bmi2 */0);
1095
1315
  dictPtr += offcodeHeaderSize;
1096
1316
  }
1097
1317
 
1098
1318
  { short matchlengthNCount[MaxML+1];
1099
1319
  unsigned matchlengthMaxValue = MaxML, matchlengthLog;
1100
- size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr);
1320
+ size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, (size_t)(dictEnd-dictPtr));
1101
1321
  RETURN_ERROR_IF(FSE_isError(matchlengthHeaderSize), dictionary_corrupted, "");
1102
1322
  RETURN_ERROR_IF(matchlengthMaxValue > MaxML, dictionary_corrupted, "");
1103
1323
  RETURN_ERROR_IF(matchlengthLog > MLFSELog, dictionary_corrupted, "");
1104
1324
  ZSTD_buildFSETable( entropy->MLTable,
1105
1325
  matchlengthNCount, matchlengthMaxValue,
1106
1326
  ML_base, ML_bits,
1107
- matchlengthLog);
1327
+ matchlengthLog,
1328
+ entropy->workspace, sizeof(entropy->workspace),
1329
+ /* bmi2 */ 0);
1108
1330
  dictPtr += matchlengthHeaderSize;
1109
1331
  }
1110
1332
 
1111
1333
  { short litlengthNCount[MaxLL+1];
1112
1334
  unsigned litlengthMaxValue = MaxLL, litlengthLog;
1113
- size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr);
1335
+ size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, (size_t)(dictEnd-dictPtr));
1114
1336
  RETURN_ERROR_IF(FSE_isError(litlengthHeaderSize), dictionary_corrupted, "");
1115
1337
  RETURN_ERROR_IF(litlengthMaxValue > MaxLL, dictionary_corrupted, "");
1116
1338
  RETURN_ERROR_IF(litlengthLog > LLFSELog, dictionary_corrupted, "");
1117
1339
  ZSTD_buildFSETable( entropy->LLTable,
1118
1340
  litlengthNCount, litlengthMaxValue,
1119
1341
  LL_base, LL_bits,
1120
- litlengthLog);
1342
+ litlengthLog,
1343
+ entropy->workspace, sizeof(entropy->workspace),
1344
+ /* bmi2 */ 0);
1121
1345
  dictPtr += litlengthHeaderSize;
1122
1346
  }
1123
1347
 
@@ -1131,7 +1355,7 @@ ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,
1131
1355
  entropy->rep[i] = rep;
1132
1356
  } }
1133
1357
 
1134
- return dictPtr - (const BYTE*)dict;
1358
+ return (size_t)(dictPtr - (const BYTE*)dict);
1135
1359
  }
1136
1360
 
1137
1361
  static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
@@ -1158,8 +1382,12 @@ static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict
1158
1382
  size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx)
1159
1383
  {
1160
1384
  assert(dctx != NULL);
1385
+ #if ZSTD_TRACE
1386
+ dctx->traceCtx = (ZSTD_trace_decompress_begin != NULL) ? ZSTD_trace_decompress_begin(dctx) : 0;
1387
+ #endif
1161
1388
  dctx->expected = ZSTD_startingInputLength(dctx->format); /* dctx->format must be properly set */
1162
1389
  dctx->stage = ZSTDds_getFrameHeaderSize;
1390
+ dctx->processedCSize = 0;
1163
1391
  dctx->decodedSize = 0;
1164
1392
  dctx->previousDstEnd = NULL;
1165
1393
  dctx->prefixStart = NULL;
@@ -1170,7 +1398,7 @@ size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx)
1170
1398
  dctx->dictID = 0;
1171
1399
  dctx->bType = bt_reserved;
1172
1400
  ZSTD_STATIC_ASSERT(sizeof(dctx->entropy.rep) == sizeof(repStartValue));
1173
- memcpy(dctx->entropy.rep, repStartValue, sizeof(repStartValue)); /* initial repcodes */
1401
+ ZSTD_memcpy(dctx->entropy.rep, repStartValue, sizeof(repStartValue)); /* initial repcodes */
1174
1402
  dctx->LLTptr = dctx->entropy.LLTable;
1175
1403
  dctx->MLTptr = dctx->entropy.MLTable;
1176
1404
  dctx->OFTptr = dctx->entropy.OFTable;
@@ -1373,6 +1601,16 @@ size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
1373
1601
  if (ddict) {
1374
1602
  dctx->ddict = ddict;
1375
1603
  dctx->dictUses = ZSTD_use_indefinitely;
1604
+ if (dctx->refMultipleDDicts == ZSTD_rmd_refMultipleDDicts) {
1605
+ if (dctx->ddictSet == NULL) {
1606
+ dctx->ddictSet = ZSTD_createDDictHashSet(dctx->customMem);
1607
+ if (!dctx->ddictSet) {
1608
+ RETURN_ERROR(memory_allocation, "Failed to allocate memory for hash set!");
1609
+ }
1610
+ }
1611
+ assert(!dctx->staticSize); /* Impossible: ddictSet cannot have been allocated if static dctx */
1612
+ FORWARD_IF_ERROR(ZSTD_DDictHashSet_addDDict(dctx->ddictSet, ddict, dctx->customMem), "");
1613
+ }
1376
1614
  }
1377
1615
  return 0;
1378
1616
  }
@@ -1394,7 +1632,7 @@ size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize)
1394
1632
 
1395
1633
  size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format)
1396
1634
  {
1397
- return ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, format);
1635
+ return ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, (int)format);
1398
1636
  }
1399
1637
 
1400
1638
  ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam)
@@ -1411,8 +1649,16 @@ ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam)
1411
1649
  ZSTD_STATIC_ASSERT(ZSTD_f_zstd1 < ZSTD_f_zstd1_magicless);
1412
1650
  return bounds;
1413
1651
  case ZSTD_d_stableOutBuffer:
1414
- bounds.lowerBound = (int)ZSTD_obm_buffered;
1415
- bounds.upperBound = (int)ZSTD_obm_stable;
1652
+ bounds.lowerBound = (int)ZSTD_bm_buffered;
1653
+ bounds.upperBound = (int)ZSTD_bm_stable;
1654
+ return bounds;
1655
+ case ZSTD_d_forceIgnoreChecksum:
1656
+ bounds.lowerBound = (int)ZSTD_d_validateChecksum;
1657
+ bounds.upperBound = (int)ZSTD_d_ignoreChecksum;
1658
+ return bounds;
1659
+ case ZSTD_d_refMultipleDDicts:
1660
+ bounds.lowerBound = (int)ZSTD_rmd_refSingleDDict;
1661
+ bounds.upperBound = (int)ZSTD_rmd_refMultipleDDicts;
1416
1662
  return bounds;
1417
1663
  default:;
1418
1664
  }
@@ -1436,6 +1682,29 @@ static int ZSTD_dParam_withinBounds(ZSTD_dParameter dParam, int value)
1436
1682
  RETURN_ERROR_IF(!ZSTD_dParam_withinBounds(p, v), parameter_outOfBound, ""); \
1437
1683
  }
1438
1684
 
1685
+ size_t ZSTD_DCtx_getParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int* value)
1686
+ {
1687
+ switch (param) {
1688
+ case ZSTD_d_windowLogMax:
1689
+ *value = (int)ZSTD_highbit32((U32)dctx->maxWindowSize);
1690
+ return 0;
1691
+ case ZSTD_d_format:
1692
+ *value = (int)dctx->format;
1693
+ return 0;
1694
+ case ZSTD_d_stableOutBuffer:
1695
+ *value = (int)dctx->outBufferMode;
1696
+ return 0;
1697
+ case ZSTD_d_forceIgnoreChecksum:
1698
+ *value = (int)dctx->forceIgnoreChecksum;
1699
+ return 0;
1700
+ case ZSTD_d_refMultipleDDicts:
1701
+ *value = (int)dctx->refMultipleDDicts;
1702
+ return 0;
1703
+ default:;
1704
+ }
1705
+ RETURN_ERROR(parameter_unsupported, "");
1706
+ }
1707
+
1439
1708
  size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter dParam, int value)
1440
1709
  {
1441
1710
  RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
@@ -1451,7 +1720,18 @@ size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter dParam, int value
1451
1720
  return 0;
1452
1721
  case ZSTD_d_stableOutBuffer:
1453
1722
  CHECK_DBOUNDS(ZSTD_d_stableOutBuffer, value);
1454
- dctx->outBufferMode = (ZSTD_outBufferMode_e)value;
1723
+ dctx->outBufferMode = (ZSTD_bufferMode_e)value;
1724
+ return 0;
1725
+ case ZSTD_d_forceIgnoreChecksum:
1726
+ CHECK_DBOUNDS(ZSTD_d_forceIgnoreChecksum, value);
1727
+ dctx->forceIgnoreChecksum = (ZSTD_forceIgnoreChecksum_e)value;
1728
+ return 0;
1729
+ case ZSTD_d_refMultipleDDicts:
1730
+ CHECK_DBOUNDS(ZSTD_d_refMultipleDDicts, value);
1731
+ if (dctx->staticSize != 0) {
1732
+ RETURN_ERROR(parameter_unsupported, "Static dctx does not support multiple DDicts!");
1733
+ }
1734
+ dctx->refMultipleDDicts = (ZSTD_refMultipleDDicts_e)value;
1455
1735
  return 0;
1456
1736
  default:;
1457
1737
  }
@@ -1469,8 +1749,7 @@ size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset)
1469
1749
  || (reset == ZSTD_reset_session_and_parameters) ) {
1470
1750
  RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
1471
1751
  ZSTD_clearDict(dctx);
1472
- dctx->format = ZSTD_f_zstd1;
1473
- dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
1752
+ ZSTD_DCtx_resetParameters(dctx);
1474
1753
  }
1475
1754
  return 0;
1476
1755
  }
@@ -1524,7 +1803,7 @@ static void ZSTD_DCtx_updateOversizedDuration(ZSTD_DStream* zds, size_t const ne
1524
1803
  {
1525
1804
  if (ZSTD_DCtx_isOverflow(zds, neededInBuffSize, neededOutBuffSize))
1526
1805
  zds->oversizedDuration++;
1527
- else
1806
+ else
1528
1807
  zds->oversizedDuration = 0;
1529
1808
  }
1530
1809
 
@@ -1538,7 +1817,7 @@ static size_t ZSTD_checkOutBuffer(ZSTD_DStream const* zds, ZSTD_outBuffer const*
1538
1817
  {
1539
1818
  ZSTD_outBuffer const expect = zds->expectedOutBuffer;
1540
1819
  /* No requirement when ZSTD_obm_stable is not enabled. */
1541
- if (zds->outBufferMode != ZSTD_obm_stable)
1820
+ if (zds->outBufferMode != ZSTD_bm_stable)
1542
1821
  return 0;
1543
1822
  /* Any buffer is allowed in zdss_init, this must be the same for every other call until
1544
1823
  * the context is reset.
@@ -1548,7 +1827,7 @@ static size_t ZSTD_checkOutBuffer(ZSTD_DStream const* zds, ZSTD_outBuffer const*
1548
1827
  /* The buffer must match our expectation exactly. */
1549
1828
  if (expect.dst == output->dst && expect.pos == output->pos && expect.size == output->size)
1550
1829
  return 0;
1551
- RETURN_ERROR(dstBuffer_wrong, "ZSTD_obm_stable enabled but output differs!");
1830
+ RETURN_ERROR(dstBuffer_wrong, "ZSTD_d_stableOutBuffer enabled but output differs!");
1552
1831
  }
1553
1832
 
1554
1833
  /* Calls ZSTD_decompressContinue() with the right parameters for ZSTD_decompressStream()
@@ -1560,7 +1839,7 @@ static size_t ZSTD_decompressContinueStream(
1560
1839
  ZSTD_DStream* zds, char** op, char* oend,
1561
1840
  void const* src, size_t srcSize) {
1562
1841
  int const isSkipFrame = ZSTD_isSkipFrame(zds);
1563
- if (zds->outBufferMode == ZSTD_obm_buffered) {
1842
+ if (zds->outBufferMode == ZSTD_bm_buffered) {
1564
1843
  size_t const dstSize = isSkipFrame ? 0 : zds->outBuffSize - zds->outStart;
1565
1844
  size_t const decodedSize = ZSTD_decompressContinue(zds,
1566
1845
  zds->outBuff + zds->outStart, dstSize, src, srcSize);
@@ -1573,14 +1852,14 @@ static size_t ZSTD_decompressContinueStream(
1573
1852
  }
1574
1853
  } else {
1575
1854
  /* Write directly into the output buffer */
1576
- size_t const dstSize = isSkipFrame ? 0 : oend - *op;
1855
+ size_t const dstSize = isSkipFrame ? 0 : (size_t)(oend - *op);
1577
1856
  size_t const decodedSize = ZSTD_decompressContinue(zds, *op, dstSize, src, srcSize);
1578
1857
  FORWARD_IF_ERROR(decodedSize, "");
1579
1858
  *op += decodedSize;
1580
1859
  /* Flushing is not needed. */
1581
1860
  zds->streamStage = zdss_read;
1582
1861
  assert(*op <= oend);
1583
- assert(zds->outBufferMode == ZSTD_obm_stable);
1862
+ assert(zds->outBufferMode == ZSTD_bm_stable);
1584
1863
  }
1585
1864
  return 0;
1586
1865
  }
@@ -1635,6 +1914,9 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB
1635
1914
  } }
1636
1915
  #endif
1637
1916
  { size_t const hSize = ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format);
1917
+ if (zds->refMultipleDDicts && zds->ddictSet) {
1918
+ ZSTD_DCtx_selectFrameDDict(zds);
1919
+ }
1638
1920
  DEBUGLOG(5, "header size : %u", (U32)hSize);
1639
1921
  if (ZSTD_isError(hSize)) {
1640
1922
  #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
@@ -1663,14 +1945,14 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB
1663
1945
  assert(iend >= ip);
1664
1946
  if (toLoad > remainingInput) { /* not enough input to load full header */
1665
1947
  if (remainingInput > 0) {
1666
- memcpy(zds->headerBuffer + zds->lhSize, ip, remainingInput);
1948
+ ZSTD_memcpy(zds->headerBuffer + zds->lhSize, ip, remainingInput);
1667
1949
  zds->lhSize += remainingInput;
1668
1950
  }
1669
1951
  input->pos = input->size;
1670
1952
  return (MAX((size_t)ZSTD_FRAMEHEADERSIZE_MIN(zds->format), hSize) - zds->lhSize) + ZSTD_blockHeaderSize; /* remaining header bytes + next block header */
1671
1953
  }
1672
1954
  assert(ip != NULL);
1673
- memcpy(zds->headerBuffer + zds->lhSize, ip, toLoad); zds->lhSize = hSize; ip += toLoad;
1955
+ ZSTD_memcpy(zds->headerBuffer + zds->lhSize, ip, toLoad); zds->lhSize = hSize; ip += toLoad;
1674
1956
  break;
1675
1957
  } }
1676
1958
 
@@ -1678,10 +1960,10 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB
1678
1960
  if (zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
1679
1961
  && zds->fParams.frameType != ZSTD_skippableFrame
1680
1962
  && (U64)(size_t)(oend-op) >= zds->fParams.frameContentSize) {
1681
- size_t const cSize = ZSTD_findFrameCompressedSize(istart, iend-istart);
1963
+ size_t const cSize = ZSTD_findFrameCompressedSize(istart, (size_t)(iend-istart));
1682
1964
  if (cSize <= (size_t)(iend-istart)) {
1683
1965
  /* shortcut : using single-pass mode */
1684
- size_t const decompressedSize = ZSTD_decompress_usingDDict(zds, op, oend-op, istart, cSize, ZSTD_getDDict(zds));
1966
+ size_t const decompressedSize = ZSTD_decompress_usingDDict(zds, op, (size_t)(oend-op), istart, cSize, ZSTD_getDDict(zds));
1685
1967
  if (ZSTD_isError(decompressedSize)) return decompressedSize;
1686
1968
  DEBUGLOG(4, "shortcut to single-pass ZSTD_decompress_usingDDict()")
1687
1969
  ip = istart + cSize;
@@ -1693,7 +1975,7 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB
1693
1975
  } }
1694
1976
 
1695
1977
  /* Check output buffer is large enough for ZSTD_odm_stable. */
1696
- if (zds->outBufferMode == ZSTD_obm_stable
1978
+ if (zds->outBufferMode == ZSTD_bm_stable
1697
1979
  && zds->fParams.frameType != ZSTD_skippableFrame
1698
1980
  && zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
1699
1981
  && (U64)(size_t)(oend-op) < zds->fParams.frameContentSize) {
@@ -1723,7 +2005,7 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB
1723
2005
 
1724
2006
  /* Adapt buffer sizes to frame header instructions */
1725
2007
  { size_t const neededInBuffSize = MAX(zds->fParams.blockSizeMax, 4 /* frame checksum */);
1726
- size_t const neededOutBuffSize = zds->outBufferMode == ZSTD_obm_buffered
2008
+ size_t const neededOutBuffSize = zds->outBufferMode == ZSTD_bm_buffered
1727
2009
  ? ZSTD_decodingBufferSize_min(zds->fParams.windowSize, zds->fParams.frameContentSize)
1728
2010
  : 0;
1729
2011
 
@@ -1731,7 +2013,7 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB
1731
2013
 
1732
2014
  { int const tooSmall = (zds->inBuffSize < neededInBuffSize) || (zds->outBuffSize < neededOutBuffSize);
1733
2015
  int const tooLarge = ZSTD_DCtx_isOversizedTooLong(zds);
1734
-
2016
+
1735
2017
  if (tooSmall || tooLarge) {
1736
2018
  size_t const bufferSize = neededInBuffSize + neededOutBuffSize;
1737
2019
  DEBUGLOG(4, "inBuff : from %u to %u",
@@ -1745,10 +2027,10 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB
1745
2027
  bufferSize > zds->staticSize - sizeof(ZSTD_DCtx),
1746
2028
  memory_allocation, "");
1747
2029
  } else {
1748
- ZSTD_free(zds->inBuff, zds->customMem);
2030
+ ZSTD_customFree(zds->inBuff, zds->customMem);
1749
2031
  zds->inBuffSize = 0;
1750
2032
  zds->outBuffSize = 0;
1751
- zds->inBuff = (char*)ZSTD_malloc(bufferSize, zds->customMem);
2033
+ zds->inBuff = (char*)ZSTD_customMalloc(bufferSize, zds->customMem);
1752
2034
  RETURN_ERROR_IF(zds->inBuff == NULL, memory_allocation, "");
1753
2035
  }
1754
2036
  zds->inBuffSize = neededInBuffSize;
@@ -1760,7 +2042,7 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB
1760
2042
 
1761
2043
  case zdss_read:
1762
2044
  DEBUGLOG(5, "stage zdss_read");
1763
- { size_t const neededInSize = ZSTD_nextSrcSizeToDecompressWithInputSize(zds, iend - ip);
2045
+ { size_t const neededInSize = ZSTD_nextSrcSizeToDecompressWithInputSize(zds, (size_t)(iend - ip));
1764
2046
  DEBUGLOG(5, "neededInSize = %u", (U32)neededInSize);
1765
2047
  if (neededInSize==0) { /* end of frame */
1766
2048
  zds->streamStage = zdss_init;
@@ -1790,7 +2072,7 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB
1790
2072
  RETURN_ERROR_IF(toLoad > zds->inBuffSize - zds->inPos,
1791
2073
  corruption_detected,
1792
2074
  "should never happen");
1793
- loadedSize = ZSTD_limitCopy(zds->inBuff + zds->inPos, toLoad, ip, iend-ip);
2075
+ loadedSize = ZSTD_limitCopy(zds->inBuff + zds->inPos, toLoad, ip, (size_t)(iend-ip));
1794
2076
  }
1795
2077
  ip += loadedSize;
1796
2078
  zds->inPos += loadedSize;
@@ -1804,7 +2086,7 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB
1804
2086
  }
1805
2087
  case zdss_flush:
1806
2088
  { size_t const toFlushSize = zds->outEnd - zds->outStart;
1807
- size_t const flushedSize = ZSTD_limitCopy(op, oend-op, zds->outBuff + zds->outStart, toFlushSize);
2089
+ size_t const flushedSize = ZSTD_limitCopy(op, (size_t)(oend-op), zds->outBuff + zds->outStart, toFlushSize);
1808
2090
  op += flushedSize;
1809
2091
  zds->outStart += flushedSize;
1810
2092
  if (flushedSize == toFlushSize) { /* flush completed */