Coverage Report

Created: 2026-12-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zstd/lib/decompress/zstd_ddict.c
Line
Count
Source
1
/*
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
3
 * All rights reserved.
4
 *
5
 * This source code is licensed under both the BSD-style license (found in the
6
 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
 * in the COPYING file in the root directory of this source tree).
8
 * You may select, at your option, one of the above-listed licenses.
9
 */
10
11
/* zstd_ddict.c :
12
 * concentrates all logic that needs to know the internals of ZSTD_DDict object */
13
14
/*-*******************************************************
15
*  Dependencies
16
*********************************************************/
17
#include "../common/allocations.h"  /* ZSTD_customMalloc, ZSTD_customFree */
18
#include "../common/zstd_deps.h"   /* ZSTD_memcpy, ZSTD_memmove, ZSTD_memset */
19
#include "../common/cpu.h"         /* bmi2 */
20
#include "../common/mem.h"         /* low level memory routines */
21
#define FSE_STATIC_LINKING_ONLY
22
#include "../common/fse.h"
23
#include "../common/huf.h"
24
#include "zstd_decompress_internal.h"
25
#include "zstd_ddict.h"
26
27
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
28
#  include "../legacy/zstd_legacy.h"
29
#endif
30
31
32
33
/*-*******************************************************
34
*  Types
35
*********************************************************/
36
struct ZSTD_DDict_s {
37
    void* dictBuffer;
38
    const void* dictContent;  /* complete dictionary, including any header */
39
    size_t dictSize;
40
    size_t dictHeaderSize;    /* excluded from matchable history */
41
    ZSTD_entropyDTables_t entropy;
42
    U32 dictID;
43
    U32 entropyPresent;
44
    ZSTD_customMem cMem;
45
};  /* typedef'd to ZSTD_DDict within "zstd.h" */
46
47
const void* ZSTD_DDict_dictContent(const ZSTD_DDict* ddict)
48
0
{
49
0
    assert(ddict != NULL);
50
0
    return ddict->dictContent;
51
0
}
52
53
size_t ZSTD_DDict_dictSize(const ZSTD_DDict* ddict)
54
0
{
55
0
    assert(ddict != NULL);
56
0
    return ddict->dictSize;
57
0
}
58
59
void ZSTD_copyDDictParameters(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
60
0
{
61
0
    const void* content;
62
0
    DEBUGLOG(4, "ZSTD_copyDDictParameters");
63
0
    assert(dctx != NULL);
64
0
    assert(ddict != NULL);
65
0
    content = ddict->dictHeaderSize != 0
66
0
            ? (const BYTE*)ddict->dictContent + ddict->dictHeaderSize
67
0
            : ddict->dictContent;
68
0
    dctx->dictID = ddict->dictID;
69
0
    dctx->prefixStart = content;
70
0
    dctx->virtualStart = content;
71
    /* An empty DDict may have NULL content. */
72
0
    dctx->dictEnd = ddict->dictSize != 0
73
0
            ? (const BYTE*)ddict->dictContent + ddict->dictSize
74
0
            : ddict->dictContent;
75
0
    dctx->previousDstEnd = dctx->dictEnd;
76
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
77
0
    dctx->dictContentBeginForFuzzing = dctx->prefixStart;
78
0
    dctx->dictContentEndForFuzzing = dctx->previousDstEnd;
79
0
#endif
80
0
    if (ddict->entropyPresent) {
81
0
        dctx->litEntropy = 1;
82
0
        dctx->fseEntropy = 1;
83
0
        dctx->LLTptr = ddict->entropy.LLTable;
84
0
        dctx->MLTptr = ddict->entropy.MLTable;
85
0
        dctx->OFTptr = ddict->entropy.OFTable;
86
0
        dctx->HUFptr = ddict->entropy.hufTable;
87
0
        dctx->entropy.rep[0] = ddict->entropy.rep[0];
88
0
        dctx->entropy.rep[1] = ddict->entropy.rep[1];
89
0
        dctx->entropy.rep[2] = ddict->entropy.rep[2];
90
0
    } else {
91
0
        dctx->litEntropy = 0;
92
0
        dctx->fseEntropy = 0;
93
0
    }
94
0
}
95
96
97
static size_t
98
ZSTD_loadEntropy_intoDDict(ZSTD_DDict* ddict,
99
                           ZSTD_dictContentType_e dictContentType)
100
0
{
101
0
    ddict->dictID = 0;
102
0
    ddict->dictHeaderSize = 0;
103
0
    ddict->entropyPresent = 0;
104
0
    if (dictContentType == ZSTD_dct_rawContent) return 0;
105
106
0
    if (ddict->dictSize < 8) {
107
0
        if (dictContentType == ZSTD_dct_fullDict)
108
0
            return ERROR(dictionary_corrupted);   /* only accept specified dictionaries */
109
0
        return 0;   /* pure content mode */
110
0
    }
111
0
    {   U32 const magic = MEM_readLE32(ddict->dictContent);
112
0
        if (magic != ZSTD_MAGIC_DICTIONARY) {
113
0
            if (dictContentType == ZSTD_dct_fullDict)
114
0
                return ERROR(dictionary_corrupted);   /* only accept specified dictionaries */
115
0
            return 0;   /* pure content mode */
116
0
        }
117
0
    }
118
0
    ddict->dictID = MEM_readLE32((const char*)ddict->dictContent + ZSTD_FRAMEIDSIZE);
119
120
    /* load entropy tables */
121
0
    {   size_t const headerSize = ZSTD_loadDEntropy(
122
0
                &ddict->entropy, ddict->dictContent, ddict->dictSize);
123
0
        RETURN_ERROR_IF(ZSTD_isError(headerSize), dictionary_corrupted, "");
124
0
        assert(headerSize <= ddict->dictSize);
125
0
        ddict->dictHeaderSize = headerSize;
126
0
    }
127
0
    ddict->entropyPresent = 1;
128
0
    return 0;
129
0
}
130
131
132
static size_t ZSTD_initDDict_internal(ZSTD_DDict* ddict,
133
                                      const void* dict, size_t dictSize,
134
                                      ZSTD_dictLoadMethod_e dictLoadMethod,
135
                                      ZSTD_dictContentType_e dictContentType)
136
0
{
137
    /* An empty dictionary has no referenced storage, even in by-copy mode. */
138
0
    if (dictSize == 0) dict = NULL;
139
0
    if ((dictLoadMethod == ZSTD_dlm_byRef) || (!dict) || (!dictSize)) {
140
0
        ddict->dictBuffer = NULL;
141
0
        ddict->dictContent = dict;
142
0
        if (!dict) dictSize = 0;
143
0
    } else {
144
0
        void* const internalBuffer = ZSTD_customMalloc(dictSize, ddict->cMem);
145
0
        ddict->dictBuffer = internalBuffer;
146
0
        ddict->dictContent = internalBuffer;
147
0
        if (!internalBuffer) return ERROR(memory_allocation);
148
0
        ZSTD_memcpy(internalBuffer, dict, dictSize);
149
0
    }
150
0
    ddict->dictSize = dictSize;
151
0
    ddict->entropy.hufTable[0] = (HUF_DTable)((ZSTD_HUFFDTABLE_CAPACITY_LOG)*0x1000001);  /* cover both little and big endian */
152
153
    /* parse dictionary content */
154
0
    FORWARD_IF_ERROR( ZSTD_loadEntropy_intoDDict(ddict, dictContentType) , "");
155
156
0
    return 0;
157
0
}
158
159
size_t ZSTD_createDDict_advanced_internal(ZSTD_DDict** ddictPtr,
160
                                          const void* dict, size_t dictSize,
161
                                          ZSTD_dictLoadMethod_e dictLoadMethod,
162
                                          ZSTD_dictContentType_e dictContentType,
163
                                          ZSTD_customMem customMem)
164
0
{
165
0
    ZSTD_DDict* ddict;
166
0
    *ddictPtr = NULL;
167
0
    RETURN_ERROR_IF((!customMem.customAlloc) ^ (!customMem.customFree),
168
0
                    parameter_combination_unsupported, "Invalid custom allocator");
169
170
0
    ddict = (ZSTD_DDict*) ZSTD_customMalloc(sizeof(ZSTD_DDict), customMem);
171
0
    RETURN_ERROR_IF(ddict == NULL, memory_allocation, "");
172
0
    ddict->cMem = customMem;
173
0
    {   size_t const initResult = ZSTD_initDDict_internal(ddict,
174
0
                                        dict, dictSize,
175
0
                                        dictLoadMethod, dictContentType);
176
0
        if (ZSTD_isError(initResult)) {
177
0
            ZSTD_freeDDict(ddict);
178
0
            return initResult;
179
0
    }   }
180
0
    *ddictPtr = ddict;
181
0
    return 0;
182
0
}
183
184
ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize,
185
                                      ZSTD_dictLoadMethod_e dictLoadMethod,
186
                                      ZSTD_dictContentType_e dictContentType,
187
                                      ZSTD_customMem customMem)
188
0
{
189
0
    ZSTD_DDict* ddict;
190
0
    size_t const err = ZSTD_createDDict_advanced_internal(&ddict,
191
0
                            dict, dictSize, dictLoadMethod, dictContentType, customMem);
192
0
    if (ZSTD_isError(err)) return NULL;
193
0
    return ddict;
194
0
}
195
196
/*! ZSTD_createDDict() :
197
*   Create a digested dictionary, to start decompression without startup delay.
198
*   `dict` content is copied inside DDict.
199
*   Consequently, `dict` can be released after `ZSTD_DDict` creation */
200
ZSTD_DDict* ZSTD_createDDict(const void* dict, size_t dictSize)
201
0
{
202
0
    ZSTD_customMem const allocator = { NULL, NULL, NULL };
203
0
    return ZSTD_createDDict_advanced(dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto, allocator);
204
0
}
205
206
/*! ZSTD_createDDict_byReference() :
207
 *  Create a digested dictionary, to start decompression without startup delay.
208
 *  Dictionary content is simply referenced, it will be accessed during decompression.
209
 *  Warning : dictBuffer must outlive DDict (DDict must be freed before dictBuffer) */
210
ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize)
211
0
{
212
0
    ZSTD_customMem const allocator = { NULL, NULL, NULL };
213
0
    return ZSTD_createDDict_advanced(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto, allocator);
214
0
}
215
216
217
const ZSTD_DDict* ZSTD_initStaticDDict(
218
                                void* sBuffer, size_t sBufferSize,
219
                                const void* dict, size_t dictSize,
220
                                ZSTD_dictLoadMethod_e dictLoadMethod,
221
                                ZSTD_dictContentType_e dictContentType)
222
0
{
223
0
    size_t const neededSpace = ZSTD_estimateDDictSize(dictSize, dictLoadMethod);
224
0
    ZSTD_DDict* const ddict = (ZSTD_DDict*)sBuffer;
225
0
    assert(sBuffer != NULL);
226
0
    assert(dict != NULL || dictSize == 0);
227
0
    if ((size_t)sBuffer & 7) return NULL;   /* 8-aligned */
228
0
    if (ZSTD_isError(neededSpace) || sBufferSize < neededSpace) return NULL;
229
0
    if (dictLoadMethod == ZSTD_dlm_byCopy && dictSize != 0) {
230
0
        ZSTD_memcpy(ddict+1, dict, dictSize);  /* local copy */
231
0
        dict = ddict+1;
232
0
    }
233
0
    if (ZSTD_isError( ZSTD_initDDict_internal(ddict,
234
0
                                              dict, dictSize,
235
0
                                              ZSTD_dlm_byRef, dictContentType) ))
236
0
        return NULL;
237
0
    return ddict;
238
0
}
239
240
241
size_t ZSTD_freeDDict(ZSTD_DDict* ddict)
242
0
{
243
0
    if (ddict==NULL) return 0;   /* support free on NULL */
244
0
    {   ZSTD_customMem const cMem = ddict->cMem;
245
0
        ZSTD_customFree(ddict->dictBuffer, cMem);
246
0
        ZSTD_customFree(ddict, cMem);
247
0
        return 0;
248
0
    }
249
0
}
250
251
/*! ZSTD_estimateDDictSize() :
252
 *  Estimate amount of memory that will be needed to create a dictionary for decompression.
253
 *  Note : dictionary created by reference using ZSTD_dlm_byRef are smaller */
254
size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod)
255
0
{
256
0
    if (dictLoadMethod == ZSTD_dlm_byRef) return sizeof(ZSTD_DDict);
257
0
    RETURN_ERROR_IF(dictSize > (size_t)-1 - sizeof(ZSTD_DDict),
258
0
                    memory_allocation, "DDict size overflow");
259
0
    return sizeof(ZSTD_DDict) + dictSize;
260
0
}
261
262
size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict)
263
0
{
264
0
    if (ddict==NULL) return 0;   /* support sizeof on NULL */
265
0
    return sizeof(*ddict) + (ddict->dictBuffer ? ddict->dictSize : 0) ;
266
0
}
267
268
/*! ZSTD_getDictID_fromDDict() :
269
 *  Provides the dictID of the dictionary loaded into `ddict`.
270
 *  If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
271
 *  Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
272
unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict)
273
0
{
274
0
    if (ddict==NULL) return 0;
275
0
    return ddict->dictID;
276
0
}