ReactOS  0.4.15-dev-4873-gb671b1c
zstd_ddict.c File Reference
#include <string.h>
#include "cpu.h"
#include "mem.h"
#include "fse.h"
#include "huf.h"
#include "zstd_decompress_internal.h"
#include "zstd_ddict.h"
Include dependency graph for zstd_ddict.c:

Go to the source code of this file.

Classes

struct  ZSTD_DDict_s
 

Macros

#define FSE_STATIC_LINKING_ONLY
 
#define HUF_STATIC_LINKING_ONLY
 

Functions

const voidZSTD_DDict_dictContent (const ZSTD_DDict *ddict)
 
size_t ZSTD_DDict_dictSize (const ZSTD_DDict *ddict)
 
void ZSTD_copyDDictParameters (ZSTD_DCtx *dctx, const ZSTD_DDict *ddict)
 
static size_t ZSTD_loadEntropy_intoDDict (ZSTD_DDict *ddict, ZSTD_dictContentType_e dictContentType)
 
static size_t ZSTD_initDDict_internal (ZSTD_DDict *ddict, const void *dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType)
 
ZSTD_DDictZSTD_createDDict_advanced (const void *dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType, ZSTD_customMem customMem)
 
ZSTD_DDictZSTD_createDDict (const void *dict, size_t dictSize)
 
ZSTD_DDictZSTD_createDDict_byReference (const void *dictBuffer, size_t dictSize)
 
const ZSTD_DDictZSTD_initStaticDDict (void *sBuffer, size_t sBufferSize, const void *dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType)
 
size_t ZSTD_freeDDict (ZSTD_DDict *ddict)
 
size_t ZSTD_estimateDDictSize (size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod)
 
size_t ZSTD_sizeof_DDict (const ZSTD_DDict *ddict)
 
unsigned ZSTD_getDictID_fromDDict (const ZSTD_DDict *ddict)
 

Macro Definition Documentation

◆ FSE_STATIC_LINKING_ONLY

#define FSE_STATIC_LINKING_ONLY

Definition at line 20 of file zstd_ddict.c.

◆ HUF_STATIC_LINKING_ONLY

#define HUF_STATIC_LINKING_ONLY

Definition at line 22 of file zstd_ddict.c.

Function Documentation

◆ ZSTD_copyDDictParameters()

void ZSTD_copyDDictParameters ( ZSTD_DCtx dctx,
const ZSTD_DDict ddict 
)

Definition at line 58 of file zstd_ddict.c.

59 {
60  DEBUGLOG(4, "ZSTD_copyDDictParameters");
61  assert(dctx != NULL);
62  assert(ddict != NULL);
63  dctx->dictID = ddict->dictID;
64  dctx->prefixStart = ddict->dictContent;
65  dctx->virtualStart = ddict->dictContent;
66  dctx->dictEnd = (const BYTE*)ddict->dictContent + ddict->dictSize;
67  dctx->previousDstEnd = dctx->dictEnd;
68 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
69  dctx->dictContentBeginForFuzzing = dctx->prefixStart;
70  dctx->dictContentEndForFuzzing = dctx->previousDstEnd;
71 #endif
72  if (ddict->entropyPresent) {
73  dctx->litEntropy = 1;
74  dctx->fseEntropy = 1;
75  dctx->LLTptr = ddict->entropy.LLTable;
76  dctx->MLTptr = ddict->entropy.MLTable;
77  dctx->OFTptr = ddict->entropy.OFTable;
78  dctx->HUFptr = ddict->entropy.hufTable;
79  dctx->entropy.rep[0] = ddict->entropy.rep[0];
80  dctx->entropy.rep[1] = ddict->entropy.rep[1];
81  dctx->entropy.rep[2] = ddict->entropy.rep[2];
82  } else {
83  dctx->litEntropy = 0;
84  dctx->fseEntropy = 0;
85  }
86 }
const void * previousDstEnd
const void * dictContent
Definition: zstd_ddict.c:38
ZSTD_entropyDTables_t entropy
Definition: zstd_ddict.c:40
const ZSTD_seqSymbol * OFTptr
#define assert(x)
Definition: debug.h:53
ZSTD_seqSymbol OFTable[SEQSYMBOL_TABLE_SIZE(OffFSELog)]
if(dx==0 &&dy==0)
Definition: linetemp.h:174
U32 entropyPresent
Definition: zstd_ddict.c:42
#define DEBUGLOG(l,...)
Definition: debug.h:106
const void * virtualStart
const void * prefixStart
const ZSTD_seqSymbol * LLTptr
const ZSTD_seqSymbol * MLTptr
ZSTD_entropyDTables_t entropy
ZSTD_seqSymbol MLTable[SEQSYMBOL_TABLE_SIZE(MLFSELog)]
size_t dictSize
Definition: zstd_ddict.c:39
HUF_DTable hufTable[HUF_DTABLE_SIZE(HufLog)]
unsigned char BYTE
Definition: xxhash.c:193
ZSTD_seqSymbol LLTable[SEQSYMBOL_TABLE_SIZE(LLFSELog)]
#define NULL
Definition: types.h:112
const HUF_DTable * HUFptr

Referenced by ZSTD_decompressBegin_usingDDict().

◆ ZSTD_createDDict()

ZSTD_DDict* ZSTD_createDDict ( const void dict,
size_t  dictSize 
)

ZSTD_createDDict() : Create a digested dictionary, to start decompression without startup delay. dict content is copied inside DDict. Consequently, dict can be released after ZSTD_DDict creation

Definition at line 170 of file zstd_ddict.c.

171 {
172  ZSTD_customMem const allocator = { NULL, NULL, NULL };
173  return ZSTD_createDDict_advanced(dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto, allocator);
174 }
ZSTD_DDict * ZSTD_createDDict_advanced(const void *dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType, ZSTD_customMem customMem)
Definition: zstd_ddict.c:145
#define NULL
Definition: types.h:112

◆ ZSTD_createDDict_advanced()

ZSTD_DDict* ZSTD_createDDict_advanced ( const void dict,
size_t  dictSize,
ZSTD_dictLoadMethod_e  dictLoadMethod,
ZSTD_dictContentType_e  dictContentType,
ZSTD_customMem  customMem 
)

Definition at line 145 of file zstd_ddict.c.

149 {
150  if (!customMem.customAlloc ^ !customMem.customFree) return NULL;
151 
152  { ZSTD_DDict* const ddict = (ZSTD_DDict*) ZSTD_malloc(sizeof(ZSTD_DDict), customMem);
153  if (ddict == NULL) return NULL;
154  ddict->cMem = customMem;
155  { size_t const initResult = ZSTD_initDDict_internal(ddict,
156  dict, dictSize,
157  dictLoadMethod, dictContentType);
158  if (ZSTD_isError(initResult)) {
159  ZSTD_freeDDict(ddict);
160  return NULL;
161  } }
162  return ddict;
163  }
164 }
#define ZSTD_isError
Definition: zstd_internal.h:46
ZSTD_customMem cMem
Definition: zstd_ddict.c:43
void * ZSTD_malloc(size_t size, ZSTD_customMem customMem)
Definition: zstd_common.c:56
size_t ZSTD_freeDDict(ZSTD_DDict *ddict)
Definition: zstd_ddict.c:212
static size_t ZSTD_initDDict_internal(ZSTD_DDict *ddict, const void *dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType)
Definition: zstd_ddict.c:120
#define NULL
Definition: types.h:112

Referenced by ZSTD_createDDict(), ZSTD_createDDict_byReference(), and ZSTD_DCtx_loadDictionary_advanced().

◆ ZSTD_createDDict_byReference()

ZSTD_DDict* ZSTD_createDDict_byReference ( const void dictBuffer,
size_t  dictSize 
)

ZSTD_createDDict_byReference() : Create a digested dictionary, to start decompression without startup delay. Dictionary content is simply referenced, it will be accessed during decompression. Warning : dictBuffer must outlive DDict (DDict must be freed before dictBuffer)

Definition at line 180 of file zstd_ddict.c.

181 {
182  ZSTD_customMem const allocator = { NULL, NULL, NULL };
183  return ZSTD_createDDict_advanced(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto, allocator);
184 }
ZSTD_DDict * ZSTD_createDDict_advanced(const void *dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType, ZSTD_customMem customMem)
Definition: zstd_ddict.c:145
#define NULL
Definition: types.h:112

◆ ZSTD_DDict_dictContent()

const void* ZSTD_DDict_dictContent ( const ZSTD_DDict ddict)

Definition at line 46 of file zstd_ddict.c.

47 {
48  assert(ddict != NULL);
49  return ddict->dictContent;
50 }
const void * dictContent
Definition: zstd_ddict.c:38
#define assert(x)
Definition: debug.h:53
#define NULL
Definition: types.h:112

Referenced by ZSTD_decompressBegin_usingDDict(), ZSTD_decompressMultiFrame(), and ZSTD_decompressStream().

◆ ZSTD_DDict_dictSize()

size_t ZSTD_DDict_dictSize ( const ZSTD_DDict ddict)

Definition at line 52 of file zstd_ddict.c.

53 {
54  assert(ddict != NULL);
55  return ddict->dictSize;
56 }
#define assert(x)
Definition: debug.h:53
size_t dictSize
Definition: zstd_ddict.c:39
#define NULL
Definition: types.h:112

Referenced by ZSTD_decompressBegin_usingDDict(), ZSTD_decompressMultiFrame(), and ZSTD_decompressStream().

◆ ZSTD_estimateDDictSize()

size_t ZSTD_estimateDDictSize ( size_t  dictSize,
ZSTD_dictLoadMethod_e  dictLoadMethod 
)

ZSTD_estimateDDictSize() : Estimate amount of memory that will be needed to create a dictionary for decompression. Note : dictionary created by reference using ZSTD_dlm_byRef are smaller

Definition at line 225 of file zstd_ddict.c.

226 {
227  return sizeof(ZSTD_DDict) + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize);
228 }
struct ZSTD_DDict_s ZSTD_DDict
Definition: zstd.h:845

◆ ZSTD_freeDDict()

size_t ZSTD_freeDDict ( ZSTD_DDict ddict)

ZSTD_freeDDict() : Function frees memory allocated with ZSTD_createDDict()

Definition at line 212 of file zstd_ddict.c.

213 {
214  if (ddict==NULL) return 0; /* support free on NULL */
215  { ZSTD_customMem const cMem = ddict->cMem;
216  ZSTD_free(ddict->dictBuffer, cMem);
217  ZSTD_free(ddict, cMem);
218  return 0;
219  }
220 }
void ZSTD_free(void *ptr, ZSTD_customMem customMem)
Definition: zstd_common.c:70
void * dictBuffer
Definition: zstd_ddict.c:37
ZSTD_customMem cMem
Definition: zstd_ddict.c:43
#define NULL
Definition: types.h:112

Referenced by ZSTD_clearDict(), and ZSTD_createDDict_advanced().

◆ ZSTD_getDictID_fromDDict()

unsigned ZSTD_getDictID_fromDDict ( const ZSTD_DDict ddict)

ZSTD_getDictID_fromDDict() : Provides the dictID of the dictionary loaded into ddict. If

Returns
== 0, the dictionary is not conformant to Zstandard specification, or empty. Non-conformant dictionaries can still be loaded, but as content-only dictionaries.

Definition at line 240 of file zstd_ddict.c.

241 {
242  if (ddict==NULL) return 0;
243  return ZSTD_getDictID_fromDict(ddict->dictContent, ddict->dictSize);
244 }
const void * dictContent
Definition: zstd_ddict.c:38
size_t dictSize
Definition: zstd_ddict.c:39
ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void *dict, size_t dictSize)
#define NULL
Definition: types.h:112

◆ ZSTD_initDDict_internal()

static size_t ZSTD_initDDict_internal ( ZSTD_DDict ddict,
const void dict,
size_t  dictSize,
ZSTD_dictLoadMethod_e  dictLoadMethod,
ZSTD_dictContentType_e  dictContentType 
)
static

Definition at line 120 of file zstd_ddict.c.

124 {
125  if ((dictLoadMethod == ZSTD_dlm_byRef) || (!dict) || (!dictSize)) {
126  ddict->dictBuffer = NULL;
127  ddict->dictContent = dict;
128  if (!dict) dictSize = 0;
129  } else {
130  void* const internalBuffer = ZSTD_malloc(dictSize, ddict->cMem);
131  ddict->dictBuffer = internalBuffer;
132  ddict->dictContent = internalBuffer;
133  if (!internalBuffer) return ERROR(memory_allocation);
134  memcpy(internalBuffer, dict, dictSize);
135  }
136  ddict->dictSize = dictSize;
137  ddict->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */
138 
139  /* parse dictionary content */
140  FORWARD_IF_ERROR( ZSTD_loadEntropy_intoDDict(ddict, dictContentType) , "");
141 
142  return 0;
143 }
const void * dictContent
Definition: zstd_ddict.c:38
#define ERROR(name)
Definition: error_private.h:53
ZSTD_entropyDTables_t entropy
Definition: zstd_ddict.c:40
void * dictBuffer
Definition: zstd_ddict.c:37
static size_t ZSTD_loadEntropy_intoDDict(ZSTD_DDict *ddict, ZSTD_dictContentType_e dictContentType)
Definition: zstd_ddict.c:90
#define HufLog
ZSTD_customMem cMem
Definition: zstd_ddict.c:43
size_t dictSize
Definition: zstd_ddict.c:39
HUF_DTable hufTable[HUF_DTABLE_SIZE(HufLog)]
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
void * ZSTD_malloc(size_t size, ZSTD_customMem customMem)
Definition: zstd_common.c:56
#define NULL
Definition: types.h:112
#define FORWARD_IF_ERROR(err,...)

Referenced by ZSTD_createDDict_advanced(), and ZSTD_initStaticDDict().

◆ ZSTD_initStaticDDict()

const ZSTD_DDict* ZSTD_initStaticDDict ( void sBuffer,
size_t  sBufferSize,
const void dict,
size_t  dictSize,
ZSTD_dictLoadMethod_e  dictLoadMethod,
ZSTD_dictContentType_e  dictContentType 
)

Definition at line 187 of file zstd_ddict.c.

192 {
193  size_t const neededSpace = sizeof(ZSTD_DDict)
194  + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize);
195  ZSTD_DDict* const ddict = (ZSTD_DDict*)sBuffer;
196  assert(sBuffer != NULL);
197  assert(dict != NULL);
198  if ((size_t)sBuffer & 7) return NULL; /* 8-aligned */
199  if (sBufferSize < neededSpace) return NULL;
200  if (dictLoadMethod == ZSTD_dlm_byCopy) {
201  memcpy(ddict+1, dict, dictSize); /* local copy */
202  dict = ddict+1;
203  }
205  dict, dictSize,
206  ZSTD_dlm_byRef, dictContentType) ))
207  return NULL;
208  return ddict;
209 }
struct ZSTD_DDict_s ZSTD_DDict
Definition: zstd.h:845
#define assert(x)
Definition: debug.h:53
#define ZSTD_isError
Definition: zstd_internal.h:46
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
static size_t ZSTD_initDDict_internal(ZSTD_DDict *ddict, const void *dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType)
Definition: zstd_ddict.c:120
#define NULL
Definition: types.h:112

◆ ZSTD_loadEntropy_intoDDict()

static size_t ZSTD_loadEntropy_intoDDict ( ZSTD_DDict ddict,
ZSTD_dictContentType_e  dictContentType 
)
static

Definition at line 90 of file zstd_ddict.c.

92 {
93  ddict->dictID = 0;
94  ddict->entropyPresent = 0;
95  if (dictContentType == ZSTD_dct_rawContent) return 0;
96 
97  if (ddict->dictSize < 8) {
98  if (dictContentType == ZSTD_dct_fullDict)
99  return ERROR(dictionary_corrupted); /* only accept specified dictionaries */
100  return 0; /* pure content mode */
101  }
102  { U32 const magic = MEM_readLE32(ddict->dictContent);
103  if (magic != ZSTD_MAGIC_DICTIONARY) {
104  if (dictContentType == ZSTD_dct_fullDict)
105  return ERROR(dictionary_corrupted); /* only accept specified dictionaries */
106  return 0; /* pure content mode */
107  }
108  }
109  ddict->dictID = MEM_readLE32((const char*)ddict->dictContent + ZSTD_FRAMEIDSIZE);
110 
111  /* load entropy tables */
113  &ddict->entropy, ddict->dictContent, ddict->dictSize)),
114  dictionary_corrupted, "");
115  ddict->entropyPresent = 1;
116  return 0;
117 }
const void * dictContent
Definition: zstd_ddict.c:38
#define ERROR(name)
Definition: error_private.h:53
MEM_STATIC U32 MEM_readLE32(const void *memPtr)
Definition: mem.h:346
ZSTD_entropyDTables_t entropy
Definition: zstd_ddict.c:40
u32_t magic(void)
U32 entropyPresent
Definition: zstd_ddict.c:42
#define ZSTD_isError
Definition: zstd_internal.h:46
#define RETURN_ERROR_IF(cond, err,...)
Definition: zstd_internal.h:91
size_t dictSize
Definition: zstd_ddict.c:39
size_t ZSTD_loadDEntropy(ZSTD_entropyDTables_t *entropy, const void *const dict, size_t const dictSize)
#define ZSTD_MAGIC_DICTIONARY
Definition: zstd.h:99
#define ZSTD_FRAMEIDSIZE
unsigned int U32
Definition: xxhash.c:195

Referenced by ZSTD_initDDict_internal().

◆ ZSTD_sizeof_DDict()

size_t ZSTD_sizeof_DDict ( const ZSTD_DDict ddict)

Definition at line 230 of file zstd_ddict.c.

231 {
232  if (ddict==NULL) return 0; /* support sizeof on NULL */
233  return sizeof(*ddict) + (ddict->dictBuffer ? ddict->dictSize : 0) ;
234 }
void * dictBuffer
Definition: zstd_ddict.c:37
size_t dictSize
Definition: zstd_ddict.c:39
#define NULL
Definition: types.h:112

Referenced by ZSTD_sizeof_DCtx().