zstd_ddict.c 8.9 KB

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