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