Coverage Report

Created: 2025-07-11 06:35

/src/zstd/lib/decompress/zstd_ddict.c
Line
Count
Source (jump to first uncovered line)
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;
39
    size_t dictSize;
40
    ZSTD_entropyDTables_t entropy;
41
    U32 dictID;
42
    U32 entropyPresent;
43
    ZSTD_customMem cMem;
44
};  /* typedef'd to ZSTD_DDict within "zstd.h" */
45
46
const void* ZSTD_DDict_dictContent(const ZSTD_DDict* ddict)
47
673k
{
48
673k
    assert(ddict != NULL);
49
673k
    return ddict->dictContent;
50
673k
}
51
52
size_t ZSTD_DDict_dictSize(const ZSTD_DDict* ddict)
53
673k
{
54
673k
    assert(ddict != NULL);
55
673k
    return ddict->dictSize;
56
673k
}
57
58
void ZSTD_copyDDictParameters(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
59
533k
{
60
533k
    DEBUGLOG(4, "ZSTD_copyDDictParameters");
61
533k
    assert(dctx != NULL);
62
533k
    assert(ddict != NULL);
63
533k
    dctx->dictID = ddict->dictID;
64
533k
    dctx->prefixStart = ddict->dictContent;
65
533k
    dctx->virtualStart = ddict->dictContent;
66
533k
    dctx->dictEnd = (const BYTE*)ddict->dictContent + ddict->dictSize;
67
533k
    dctx->previousDstEnd = dctx->dictEnd;
68
533k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
69
533k
    dctx->dictContentBeginForFuzzing = dctx->prefixStart;
70
533k
    dctx->dictContentEndForFuzzing = dctx->previousDstEnd;
71
533k
#endif
72
533k
    if (ddict->entropyPresent) {
73
435k
        dctx->litEntropy = 1;
74
435k
        dctx->fseEntropy = 1;
75
435k
        dctx->LLTptr = ddict->entropy.LLTable;
76
435k
        dctx->MLTptr = ddict->entropy.MLTable;
77
435k
        dctx->OFTptr = ddict->entropy.OFTable;
78
435k
        dctx->HUFptr = ddict->entropy.hufTable;
79
435k
        dctx->entropy.rep[0] = ddict->entropy.rep[0];
80
435k
        dctx->entropy.rep[1] = ddict->entropy.rep[1];
81
435k
        dctx->entropy.rep[2] = ddict->entropy.rep[2];
82
435k
    } else {
83
98.6k
        dctx->litEntropy = 0;
84
98.6k
        dctx->fseEntropy = 0;
85
98.6k
    }
86
533k
}
87
88
89
static size_t
90
ZSTD_loadEntropy_intoDDict(ZSTD_DDict* ddict,
91
                           ZSTD_dictContentType_e dictContentType)
92
137k
{
93
137k
    ddict->dictID = 0;
94
137k
    ddict->entropyPresent = 0;
95
137k
    if (dictContentType == ZSTD_dct_rawContent) return 0;
96
97
69.0k
    if (ddict->dictSize < 8) {
98
15
        if (dictContentType == ZSTD_dct_fullDict)
99
0
            return ERROR(dictionary_corrupted);   /* only accept specified dictionaries */
100
15
        return 0;   /* pure content mode */
101
15
    }
102
69.0k
    {   U32 const magic = MEM_readLE32(ddict->dictContent);
103
69.0k
        if (magic != ZSTD_MAGIC_DICTIONARY) {
104
4.84k
            if (dictContentType == ZSTD_dct_fullDict)
105
0
                return ERROR(dictionary_corrupted);   /* only accept specified dictionaries */
106
4.84k
            return 0;   /* pure content mode */
107
4.84k
        }
108
69.0k
    }
109
64.1k
    ddict->dictID = MEM_readLE32((const char*)ddict->dictContent + ZSTD_FRAMEIDSIZE);
110
111
    /* load entropy tables */
112
64.1k
    RETURN_ERROR_IF(ZSTD_isError(ZSTD_loadDEntropy(
113
64.1k
            &ddict->entropy, ddict->dictContent, ddict->dictSize)),
114
64.1k
        dictionary_corrupted, "");
115
64.1k
    ddict->entropyPresent = 1;
116
64.1k
    return 0;
117
64.1k
}
118
119
120
static size_t ZSTD_initDDict_internal(ZSTD_DDict* ddict,
121
                                      const void* dict, size_t dictSize,
122
                                      ZSTD_dictLoadMethod_e dictLoadMethod,
123
                                      ZSTD_dictContentType_e dictContentType)
124
137k
{
125
137k
    if ((dictLoadMethod == ZSTD_dlm_byRef) || (!dict) || (!dictSize)) {
126
56.3k
        ddict->dictBuffer = NULL;
127
56.3k
        ddict->dictContent = dict;
128
56.3k
        if (!dict) dictSize = 0;
129
80.9k
    } else {
130
80.9k
        void* const internalBuffer = ZSTD_customMalloc(dictSize, ddict->cMem);
131
80.9k
        ddict->dictBuffer = internalBuffer;
132
80.9k
        ddict->dictContent = internalBuffer;
133
80.9k
        if (!internalBuffer) return ERROR(memory_allocation);
134
80.9k
        ZSTD_memcpy(internalBuffer, dict, dictSize);
135
80.9k
    }
136
137k
    ddict->dictSize = dictSize;
137
137k
    ddict->entropy.hufTable[0] = (HUF_DTable)((ZSTD_HUFFDTABLE_CAPACITY_LOG)*0x1000001);  /* cover both little and big endian */
138
139
    /* parse dictionary content */
140
137k
    FORWARD_IF_ERROR( ZSTD_loadEntropy_intoDDict(ddict, dictContentType) , "");
141
142
137k
    return 0;
143
137k
}
144
145
ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize,
146
                                      ZSTD_dictLoadMethod_e dictLoadMethod,
147
                                      ZSTD_dictContentType_e dictContentType,
148
                                      ZSTD_customMem customMem)
149
137k
{
150
137k
    if ((!customMem.customAlloc) ^ (!customMem.customFree)) return NULL;
151
152
137k
    {   ZSTD_DDict* const ddict = (ZSTD_DDict*) ZSTD_customMalloc(sizeof(ZSTD_DDict), customMem);
153
137k
        if (ddict == NULL) return NULL;
154
137k
        ddict->cMem = customMem;
155
137k
        {   size_t const initResult = ZSTD_initDDict_internal(ddict,
156
137k
                                            dict, dictSize,
157
137k
                                            dictLoadMethod, dictContentType);
158
137k
            if (ZSTD_isError(initResult)) {
159
0
                ZSTD_freeDDict(ddict);
160
0
                return NULL;
161
0
        }   }
162
137k
        return ddict;
163
137k
    }
164
137k
}
165
166
/*! ZSTD_createDDict() :
167
*   Create a digested dictionary, to start decompression without startup delay.
168
*   `dict` content is copied inside DDict.
169
*   Consequently, `dict` can be released after `ZSTD_DDict` creation */
170
ZSTD_DDict* ZSTD_createDDict(const void* dict, size_t dictSize)
171
14.8k
{
172
14.8k
    ZSTD_customMem const allocator = { NULL, NULL, NULL };
173
14.8k
    return ZSTD_createDDict_advanced(dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto, allocator);
174
14.8k
}
175
176
/*! ZSTD_createDDict_byReference() :
177
 *  Create a digested dictionary, to start decompression without startup delay.
178
 *  Dictionary content is simply referenced, it will be accessed during decompression.
179
 *  Warning : dictBuffer must outlive DDict (DDict must be freed before dictBuffer) */
180
ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize)
181
0
{
182
0
    ZSTD_customMem const allocator = { NULL, NULL, NULL };
183
0
    return ZSTD_createDDict_advanced(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto, allocator);
184
0
}
185
186
187
const ZSTD_DDict* ZSTD_initStaticDDict(
188
                                void* sBuffer, size_t sBufferSize,
189
                                const void* dict, size_t dictSize,
190
                                ZSTD_dictLoadMethod_e dictLoadMethod,
191
                                ZSTD_dictContentType_e dictContentType)
192
0
{
193
0
    size_t const neededSpace = sizeof(ZSTD_DDict)
194
0
                             + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize);
195
0
    ZSTD_DDict* const ddict = (ZSTD_DDict*)sBuffer;
196
0
    assert(sBuffer != NULL);
197
0
    assert(dict != NULL);
198
0
    if ((size_t)sBuffer & 7) return NULL;   /* 8-aligned */
199
0
    if (sBufferSize < neededSpace) return NULL;
200
0
    if (dictLoadMethod == ZSTD_dlm_byCopy) {
201
0
        ZSTD_memcpy(ddict+1, dict, dictSize);  /* local copy */
202
0
        dict = ddict+1;
203
0
    }
204
0
    if (ZSTD_isError( ZSTD_initDDict_internal(ddict,
205
0
                                              dict, dictSize,
206
0
                                              ZSTD_dlm_byRef, dictContentType) ))
207
0
        return NULL;
208
0
    return ddict;
209
0
}
210
211
212
size_t ZSTD_freeDDict(ZSTD_DDict* ddict)
213
756k
{
214
756k
    if (ddict==NULL) return 0;   /* support free on NULL */
215
137k
    {   ZSTD_customMem const cMem = ddict->cMem;
216
137k
        ZSTD_customFree(ddict->dictBuffer, cMem);
217
137k
        ZSTD_customFree(ddict, cMem);
218
137k
        return 0;
219
756k
    }
220
756k
}
221
222
/*! ZSTD_estimateDDictSize() :
223
 *  Estimate amount of memory that will be needed to create a dictionary for decompression.
224
 *  Note : dictionary created by reference using ZSTD_dlm_byRef are smaller */
225
size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod)
226
0
{
227
0
    return sizeof(ZSTD_DDict) + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize);
228
0
}
229
230
size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict)
231
0
{
232
0
    if (ddict==NULL) return 0;   /* support sizeof on NULL */
233
0
    return sizeof(*ddict) + (ddict->dictBuffer ? ddict->dictSize : 0) ;
234
0
}
235
236
/*! ZSTD_getDictID_fromDDict() :
237
 *  Provides the dictID of the dictionary loaded into `ddict`.
238
 *  If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
239
 *  Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
240
unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict)
241
0
{
242
0
    if (ddict==NULL) return 0;
243
0
    return ddict->dictID;
244
0
}