You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

zstd_ddict.c 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. /* zstd_ddict.c :
  11. * concentrates all logic that needs to know the internals of ZSTD_DDict object */
  12. /*-*******************************************************
  13. * Dependencies
  14. *********************************************************/
  15. #include "zstd_deps.h" /* ZSTD_memcpy, ZSTD_memmove, ZSTD_memset */
  16. #include "cpu.h" /* bmi2 */
  17. #include "mem.h" /* low level memory routines */
  18. #define FSE_STATIC_LINKING_ONLY
  19. #include "fse.h"
  20. #include "huf.h"
  21. #include "zstd_decompress_internal.h"
  22. #include "zstd_ddict.h"
  23. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
  24. # include "../legacy/zstd_legacy.h"
  25. #endif
  26. /*-*******************************************************
  27. * Types
  28. *********************************************************/
  29. struct ZSTD_DDict_s {
  30. void* dictBuffer;
  31. const void* dictContent;
  32. size_t dictSize;
  33. ZSTD_entropyDTables_t entropy;
  34. U32 dictID;
  35. U32 entropyPresent;
  36. ZSTD_customMem cMem;
  37. }; /* typedef'd to ZSTD_DDict within "zstd.h" */
  38. const void* ZSTD_DDict_dictContent(const ZSTD_DDict* ddict)
  39. {
  40. assert(ddict != NULL);
  41. return ddict->dictContent;
  42. }
  43. size_t ZSTD_DDict_dictSize(const ZSTD_DDict* ddict)
  44. {
  45. assert(ddict != NULL);
  46. return ddict->dictSize;
  47. }
  48. void ZSTD_copyDDictParameters(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
  49. {
  50. DEBUGLOG(4, "ZSTD_copyDDictParameters");
  51. assert(dctx != NULL);
  52. assert(ddict != NULL);
  53. dctx->dictID = ddict->dictID;
  54. dctx->prefixStart = ddict->dictContent;
  55. dctx->virtualStart = ddict->dictContent;
  56. dctx->dictEnd = (const BYTE*)ddict->dictContent + ddict->dictSize;
  57. dctx->previousDstEnd = dctx->dictEnd;
  58. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  59. dctx->dictContentBeginForFuzzing = dctx->prefixStart;
  60. dctx->dictContentEndForFuzzing = dctx->previousDstEnd;
  61. #endif
  62. if (ddict->entropyPresent) {
  63. dctx->litEntropy = 1;
  64. dctx->fseEntropy = 1;
  65. dctx->LLTptr = ddict->entropy.LLTable;
  66. dctx->MLTptr = ddict->entropy.MLTable;
  67. dctx->OFTptr = ddict->entropy.OFTable;
  68. dctx->HUFptr = ddict->entropy.hufTable;
  69. dctx->entropy.rep[0] = ddict->entropy.rep[0];
  70. dctx->entropy.rep[1] = ddict->entropy.rep[1];
  71. dctx->entropy.rep[2] = ddict->entropy.rep[2];
  72. } else {
  73. dctx->litEntropy = 0;
  74. dctx->fseEntropy = 0;
  75. }
  76. }
  77. static size_t
  78. ZSTD_loadEntropy_intoDDict(ZSTD_DDict* ddict,
  79. ZSTD_dictContentType_e dictContentType)
  80. {
  81. ddict->dictID = 0;
  82. ddict->entropyPresent = 0;
  83. if (dictContentType == ZSTD_dct_rawContent) return 0;
  84. if (ddict->dictSize < 8) {
  85. if (dictContentType == ZSTD_dct_fullDict)
  86. return ERROR(dictionary_corrupted); /* only accept specified dictionaries */
  87. return 0; /* pure content mode */
  88. }
  89. { U32 const magic = MEM_readLE32(ddict->dictContent);
  90. if (magic != ZSTD_MAGIC_DICTIONARY) {
  91. if (dictContentType == ZSTD_dct_fullDict)
  92. return ERROR(dictionary_corrupted); /* only accept specified dictionaries */
  93. return 0; /* pure content mode */
  94. }
  95. }
  96. ddict->dictID = MEM_readLE32((const char*)ddict->dictContent + ZSTD_FRAMEIDSIZE);
  97. /* load entropy tables */
  98. RETURN_ERROR_IF(ZSTD_isError(ZSTD_loadDEntropy(
  99. &ddict->entropy, ddict->dictContent, ddict->dictSize)),
  100. dictionary_corrupted, "");
  101. ddict->entropyPresent = 1;
  102. return 0;
  103. }
  104. static size_t ZSTD_initDDict_internal(ZSTD_DDict* ddict,
  105. const void* dict, size_t dictSize,
  106. ZSTD_dictLoadMethod_e dictLoadMethod,
  107. ZSTD_dictContentType_e dictContentType)
  108. {
  109. if ((dictLoadMethod == ZSTD_dlm_byRef) || (!dict) || (!dictSize)) {
  110. ddict->dictBuffer = NULL;
  111. ddict->dictContent = dict;
  112. if (!dict) dictSize = 0;
  113. } else {
  114. void* const internalBuffer = ZSTD_customMalloc(dictSize, ddict->cMem);
  115. ddict->dictBuffer = internalBuffer;
  116. ddict->dictContent = internalBuffer;
  117. if (!internalBuffer) return ERROR(memory_allocation);
  118. ZSTD_memcpy(internalBuffer, dict, dictSize);
  119. }
  120. ddict->dictSize = dictSize;
  121. ddict->entropy.hufTable[0] = (HUF_DTable)((ZSTD_HUFFDTABLE_CAPACITY_LOG)*0x1000001); /* cover both little and big endian */
  122. /* parse dictionary content */
  123. FORWARD_IF_ERROR( ZSTD_loadEntropy_intoDDict(ddict, dictContentType) , "");
  124. return 0;
  125. }
  126. ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize,
  127. ZSTD_dictLoadMethod_e dictLoadMethod,
  128. ZSTD_dictContentType_e dictContentType,
  129. ZSTD_customMem customMem)
  130. {
  131. if ((!customMem.customAlloc) ^ (!customMem.customFree)) return NULL;
  132. { ZSTD_DDict* const ddict = (ZSTD_DDict*) ZSTD_customMalloc(sizeof(ZSTD_DDict), customMem);
  133. if (ddict == NULL) return NULL;
  134. ddict->cMem = customMem;
  135. { size_t const initResult = ZSTD_initDDict_internal(ddict,
  136. dict, dictSize,
  137. dictLoadMethod, dictContentType);
  138. if (ZSTD_isError(initResult)) {
  139. ZSTD_freeDDict(ddict);
  140. return NULL;
  141. } }
  142. return ddict;
  143. }
  144. }
  145. /*! ZSTD_createDDict() :
  146. * Create a digested dictionary, to start decompression without startup delay.
  147. * `dict` content is copied inside DDict.
  148. * Consequently, `dict` can be released after `ZSTD_DDict` creation */
  149. ZSTD_DDict* ZSTD_createDDict(const void* dict, size_t dictSize)
  150. {
  151. ZSTD_customMem const allocator = { NULL, NULL, NULL };
  152. return ZSTD_createDDict_advanced(dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto, allocator);
  153. }
  154. /*! ZSTD_createDDict_byReference() :
  155. * Create a digested dictionary, to start decompression without startup delay.
  156. * Dictionary content is simply referenced, it will be accessed during decompression.
  157. * Warning : dictBuffer must outlive DDict (DDict must be freed before dictBuffer) */
  158. ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize)
  159. {
  160. ZSTD_customMem const allocator = { NULL, NULL, NULL };
  161. return ZSTD_createDDict_advanced(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto, allocator);
  162. }
  163. const ZSTD_DDict* ZSTD_initStaticDDict(
  164. void* sBuffer, size_t sBufferSize,
  165. const void* dict, size_t dictSize,
  166. ZSTD_dictLoadMethod_e dictLoadMethod,
  167. ZSTD_dictContentType_e dictContentType)
  168. {
  169. size_t const neededSpace = sizeof(ZSTD_DDict)
  170. + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize);
  171. ZSTD_DDict* const ddict = (ZSTD_DDict*)sBuffer;
  172. assert(sBuffer != NULL);
  173. assert(dict != NULL);
  174. if ((size_t)sBuffer & 7) return NULL; /* 8-aligned */
  175. if (sBufferSize < neededSpace) return NULL;
  176. if (dictLoadMethod == ZSTD_dlm_byCopy) {
  177. ZSTD_memcpy(ddict+1, dict, dictSize); /* local copy */
  178. dict = ddict+1;
  179. }
  180. if (ZSTD_isError( ZSTD_initDDict_internal(ddict,
  181. dict, dictSize,
  182. ZSTD_dlm_byRef, dictContentType) ))
  183. return NULL;
  184. return ddict;
  185. }
  186. size_t ZSTD_freeDDict(ZSTD_DDict* ddict)
  187. {
  188. if (ddict==NULL) return 0; /* support free on NULL */
  189. { ZSTD_customMem const cMem = ddict->cMem;
  190. ZSTD_customFree(ddict->dictBuffer, cMem);
  191. ZSTD_customFree(ddict, cMem);
  192. return 0;
  193. }
  194. }
  195. /*! ZSTD_estimateDDictSize() :
  196. * Estimate amount of memory that will be needed to create a dictionary for decompression.
  197. * Note : dictionary created by reference using ZSTD_dlm_byRef are smaller */
  198. size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod)
  199. {
  200. return sizeof(ZSTD_DDict) + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize);
  201. }
  202. size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict)
  203. {
  204. if (ddict==NULL) return 0; /* support sizeof on NULL */
  205. return sizeof(*ddict) + (ddict->dictBuffer ? ddict->dictSize : 0) ;
  206. }
  207. /*! ZSTD_getDictID_fromDDict() :
  208. * Provides the dictID of the dictionary loaded into `ddict`.
  209. * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
  210. * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
  211. unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict)
  212. {
  213. if (ddict==NULL) return 0;
  214. return ddict->dictID;
  215. }