zstd.h 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  1. /*
  2. * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
  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. #if defined (__cplusplus)
  11. extern "C" {
  12. #endif
  13. #ifndef ZSTD_H_235446
  14. #define ZSTD_H_235446
  15. /* ====== Dependency ======*/
  16. #include <stddef.h> /* size_t */
  17. /* ===== ZSTDLIB_API : control library symbols visibility ===== */
  18. #ifndef ZSTDLIB_VISIBILITY
  19. # if defined(__GNUC__) && (__GNUC__ >= 4)
  20. # define ZSTDLIB_VISIBILITY __attribute__ ((visibility ("default")))
  21. # else
  22. # define ZSTDLIB_VISIBILITY
  23. # endif
  24. #endif
  25. #if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1)
  26. # define ZSTDLIB_API __declspec(dllexport) ZSTDLIB_VISIBILITY
  27. #elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1)
  28. # define ZSTDLIB_API __declspec(dllimport) ZSTDLIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/
  29. #else
  30. # define ZSTDLIB_API ZSTDLIB_VISIBILITY
  31. #endif
  32. /*******************************************************************************
  33. Introduction
  34. zstd, short for Zstandard, is a fast lossless compression algorithm, targeting
  35. real-time compression scenarios at zlib-level and better compression ratios.
  36. The zstd compression library provides in-memory compression and decompression
  37. functions.
  38. The library supports regular compression levels from 1 up to ZSTD_maxCLevel(),
  39. which is currently 22. Levels >= 20, labeled `--ultra`, should be used with
  40. caution, as they require more memory. The library also offers negative
  41. compression levels, which extend the range of speed vs. ratio preferences.
  42. The lower the level, the faster the speed (at the cost of compression).
  43. Compression can be done in:
  44. - a single step (described as Simple API)
  45. - a single step, reusing a context (described as Explicit context)
  46. - unbounded multiple steps (described as Streaming compression)
  47. The compression ratio achievable on small data can be highly improved using
  48. a dictionary. Dictionary compression can be performed in:
  49. - a single step (described as Simple dictionary API)
  50. - a single step, reusing a dictionary (described as Bulk-processing
  51. dictionary API)
  52. Advanced experimental functions can be accessed using
  53. `#define ZSTD_STATIC_LINKING_ONLY` before including zstd.h.
  54. Advanced experimental APIs should never be used with a dynamically-linked
  55. library. They are not "stable"; their definitions or signatures may change in
  56. the future. Only static linking is allowed.
  57. *******************************************************************************/
  58. /*------ Version ------*/
  59. #define ZSTD_VERSION_MAJOR 1
  60. #define ZSTD_VERSION_MINOR 3
  61. #define ZSTD_VERSION_RELEASE 6
  62. #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
  63. ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< useful to check dll version */
  64. #define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE
  65. #define ZSTD_QUOTE(str) #str
  66. #define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str)
  67. #define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION)
  68. ZSTDLIB_API const char* ZSTD_versionString(void); /* v1.3.0+ */
  69. /***************************************
  70. * Default constant
  71. ***************************************/
  72. #ifndef ZSTD_CLEVEL_DEFAULT
  73. # define ZSTD_CLEVEL_DEFAULT 3
  74. #endif
  75. /***************************************
  76. * Simple API
  77. ***************************************/
  78. /*! ZSTD_compress() :
  79. * Compresses `src` content as a single zstd compressed frame into already allocated `dst`.
  80. * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
  81. * @return : compressed size written into `dst` (<= `dstCapacity),
  82. * or an error code if it fails (which can be tested using ZSTD_isError()). */
  83. ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity,
  84. const void* src, size_t srcSize,
  85. int compressionLevel);
  86. /*! ZSTD_decompress() :
  87. * `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames.
  88. * `dstCapacity` is an upper bound of originalSize to regenerate.
  89. * If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
  90. * @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
  91. * or an errorCode if it fails (which can be tested using ZSTD_isError()). */
  92. ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity,
  93. const void* src, size_t compressedSize);
  94. /*! ZSTD_getFrameContentSize() : added in v1.3.0
  95. * `src` should point to the start of a ZSTD encoded frame.
  96. * `srcSize` must be at least as large as the frame header.
  97. * hint : any size >= `ZSTD_frameHeaderSize_max` is large enough.
  98. * @return : - decompressed size of `src` frame content, if known
  99. * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined
  100. * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small)
  101. * note 1 : a 0 return value means the frame is valid but "empty".
  102. * note 2 : decompressed size is an optional field, it may not be present, typically in streaming mode.
  103. * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size.
  104. * In which case, it's necessary to use streaming mode to decompress data.
  105. * Optionally, application can rely on some implicit limit,
  106. * as ZSTD_decompress() only needs an upper bound of decompressed size.
  107. * (For example, data could be necessarily cut into blocks <= 16 KB).
  108. * note 3 : decompressed size is always present when compression is completed using single-pass functions,
  109. * such as ZSTD_compress(), ZSTD_compressCCtx() ZSTD_compress_usingDict() or ZSTD_compress_usingCDict().
  110. * note 4 : decompressed size can be very large (64-bits value),
  111. * potentially larger than what local system can handle as a single memory segment.
  112. * In which case, it's necessary to use streaming mode to decompress data.
  113. * note 5 : If source is untrusted, decompressed size could be wrong or intentionally modified.
  114. * Always ensure return value fits within application's authorized limits.
  115. * Each application can set its own limits.
  116. * note 6 : This function replaces ZSTD_getDecompressedSize() */
  117. #define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1)
  118. #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
  119. ZSTDLIB_API unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize);
  120. /*! ZSTD_getDecompressedSize() :
  121. * NOTE: This function is now obsolete, in favor of ZSTD_getFrameContentSize().
  122. * Both functions work the same way, but ZSTD_getDecompressedSize() blends
  123. * "empty", "unknown" and "error" results to the same return value (0),
  124. * while ZSTD_getFrameContentSize() gives them separate return values.
  125. * @return : decompressed size of `src` frame content _if known and not empty_, 0 otherwise. */
  126. ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize);
  127. /*====== Helper functions ======*/
  128. #define ZSTD_COMPRESSBOUND(srcSize) ((srcSize) + ((srcSize)>>8) + (((srcSize) < (128<<10)) ? (((128<<10) - (srcSize)) >> 11) /* margin, from 64 to 0 */ : 0)) /* this formula ensures that bound(A) + bound(B) <= bound(A+B) as long as A and B >= 128 KB */
  129. ZSTDLIB_API size_t ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case single-pass scenario */
  130. ZSTDLIB_API unsigned ZSTD_isError(size_t code); /*!< tells if a `size_t` function result is an error code */
  131. ZSTDLIB_API const char* ZSTD_getErrorName(size_t code); /*!< provides readable string from an error code */
  132. ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compression level available */
  133. /***************************************
  134. * Explicit context
  135. ***************************************/
  136. /*= Compression context
  137. * When compressing many times,
  138. * it is recommended to allocate a context just once, and re-use it for each successive compression operation.
  139. * This will make workload friendlier for system's memory.
  140. * Use one context per thread for parallel execution in multi-threaded environments. */
  141. typedef struct ZSTD_CCtx_s ZSTD_CCtx;
  142. ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void);
  143. ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx);
  144. /*! ZSTD_compressCCtx() :
  145. * Same as ZSTD_compress(), requires an allocated ZSTD_CCtx (see ZSTD_createCCtx()). */
  146. ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* ctx,
  147. void* dst, size_t dstCapacity,
  148. const void* src, size_t srcSize,
  149. int compressionLevel);
  150. /*= Decompression context
  151. * When decompressing many times,
  152. * it is recommended to allocate a context only once,
  153. * and re-use it for each successive compression operation.
  154. * This will make workload friendlier for system's memory.
  155. * Use one context per thread for parallel execution. */
  156. typedef struct ZSTD_DCtx_s ZSTD_DCtx;
  157. ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void);
  158. ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx);
  159. /*! ZSTD_decompressDCtx() :
  160. * Same as ZSTD_decompress(), requires an allocated ZSTD_DCtx (see ZSTD_createDCtx()) */
  161. ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx,
  162. void* dst, size_t dstCapacity,
  163. const void* src, size_t srcSize);
  164. /**************************
  165. * Simple dictionary API
  166. ***************************/
  167. /*! ZSTD_compress_usingDict() :
  168. * Compression using a predefined Dictionary (see dictBuilder/zdict.h).
  169. * Note : This function loads the dictionary, resulting in significant startup delay.
  170. * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
  171. ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
  172. void* dst, size_t dstCapacity,
  173. const void* src, size_t srcSize,
  174. const void* dict,size_t dictSize,
  175. int compressionLevel);
  176. /*! ZSTD_decompress_usingDict() :
  177. * Decompression using a predefined Dictionary (see dictBuilder/zdict.h).
  178. * Dictionary must be identical to the one used during compression.
  179. * Note : This function loads the dictionary, resulting in significant startup delay.
  180. * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
  181. ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
  182. void* dst, size_t dstCapacity,
  183. const void* src, size_t srcSize,
  184. const void* dict,size_t dictSize);
  185. /**********************************
  186. * Bulk processing dictionary API
  187. *********************************/
  188. typedef struct ZSTD_CDict_s ZSTD_CDict;
  189. /*! ZSTD_createCDict() :
  190. * When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once.
  191. * ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay.
  192. * ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only.
  193. * `dictBuffer` can be released after ZSTD_CDict creation, since its content is copied within CDict
  194. * Note : A ZSTD_CDict can be created with an empty dictionary, but it is inefficient for small data. */
  195. ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize,
  196. int compressionLevel);
  197. /*! ZSTD_freeCDict() :
  198. * Function frees memory allocated by ZSTD_createCDict(). */
  199. ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict);
  200. /*! ZSTD_compress_usingCDict() :
  201. * Compression using a digested Dictionary.
  202. * Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times.
  203. * Note that compression level is decided during dictionary creation.
  204. * Frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no)
  205. * Note : ZSTD_compress_usingCDict() can be used with a ZSTD_CDict created from an empty dictionary.
  206. * But it is inefficient for small data, and it is recommended to use ZSTD_compressCCtx(). */
  207. ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,
  208. void* dst, size_t dstCapacity,
  209. const void* src, size_t srcSize,
  210. const ZSTD_CDict* cdict);
  211. typedef struct ZSTD_DDict_s ZSTD_DDict;
  212. /*! ZSTD_createDDict() :
  213. * Create a digested dictionary, ready to start decompression operation without startup delay.
  214. * dictBuffer can be released after DDict creation, as its content is copied inside DDict */
  215. ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize);
  216. /*! ZSTD_freeDDict() :
  217. * Function frees memory allocated with ZSTD_createDDict() */
  218. ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict);
  219. /*! ZSTD_decompress_usingDDict() :
  220. * Decompression using a digested Dictionary.
  221. * Faster startup than ZSTD_decompress_usingDict(), recommended when same dictionary is used multiple times. */
  222. ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
  223. void* dst, size_t dstCapacity,
  224. const void* src, size_t srcSize,
  225. const ZSTD_DDict* ddict);
  226. /****************************
  227. * Streaming
  228. ****************************/
  229. typedef struct ZSTD_inBuffer_s {
  230. const void* src; /**< start of input buffer */
  231. size_t size; /**< size of input buffer */
  232. size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */
  233. } ZSTD_inBuffer;
  234. typedef struct ZSTD_outBuffer_s {
  235. void* dst; /**< start of output buffer */
  236. size_t size; /**< size of output buffer */
  237. size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */
  238. } ZSTD_outBuffer;
  239. /*-***********************************************************************
  240. * Streaming compression - HowTo
  241. *
  242. * A ZSTD_CStream object is required to track streaming operation.
  243. * Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources.
  244. * ZSTD_CStream objects can be reused multiple times on consecutive compression operations.
  245. * It is recommended to re-use ZSTD_CStream in situations where many streaming operations will be achieved consecutively,
  246. * since it will play nicer with system's memory, by re-using already allocated memory.
  247. * Use one separate ZSTD_CStream per thread for parallel execution.
  248. *
  249. * Start a new compression by initializing ZSTD_CStream context.
  250. * Use ZSTD_initCStream() to start a new compression operation.
  251. * Use variants ZSTD_initCStream_usingDict() or ZSTD_initCStream_usingCDict() for streaming with dictionary (experimental section)
  252. *
  253. * Use ZSTD_compressStream() as many times as necessary to consume input stream.
  254. * The function will automatically update both `pos` fields within `input` and `output`.
  255. * Note that the function may not consume the entire input,
  256. * for example, because the output buffer is already full,
  257. * in which case `input.pos < input.size`.
  258. * The caller must check if input has been entirely consumed.
  259. * If not, the caller must make some room to receive more compressed data,
  260. * typically by emptying output buffer, or allocating a new output buffer,
  261. * and then present again remaining input data.
  262. * @return : a size hint, preferred nb of bytes to use as input for next function call
  263. * or an error code, which can be tested using ZSTD_isError().
  264. * Note 1 : it's just a hint, to help latency a little, any other value will work fine.
  265. * Note 2 : size hint is guaranteed to be <= ZSTD_CStreamInSize()
  266. *
  267. * At any moment, it's possible to flush whatever data might remain stuck within internal buffer,
  268. * using ZSTD_flushStream(). `output->pos` will be updated.
  269. * Note that, if `output->size` is too small, a single invocation of ZSTD_flushStream() might not be enough (return code > 0).
  270. * In which case, make some room to receive more compressed data, and call again ZSTD_flushStream().
  271. * @return : 0 if internal buffers are entirely flushed,
  272. * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size),
  273. * or an error code, which can be tested using ZSTD_isError().
  274. *
  275. * ZSTD_endStream() instructs to finish a frame.
  276. * It will perform a flush and write frame epilogue.
  277. * The epilogue is required for decoders to consider a frame completed.
  278. * flush() operation is the same, and follows same rules as ZSTD_flushStream().
  279. * @return : 0 if frame fully completed and fully flushed,
  280. * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size),
  281. * or an error code, which can be tested using ZSTD_isError().
  282. *
  283. * *******************************************************************/
  284. typedef ZSTD_CCtx ZSTD_CStream; /**< CCtx and CStream are now effectively same object (>= v1.3.0) */
  285. /* Continue to distinguish them for compatibility with older versions <= v1.2.0 */
  286. /*===== ZSTD_CStream management functions =====*/
  287. ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void);
  288. ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs);
  289. /*===== Streaming compression functions =====*/
  290. ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel);
  291. ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
  292. ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
  293. ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
  294. ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */
  295. ZSTDLIB_API size_t ZSTD_CStreamOutSize(void); /**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block in all circumstances. */
  296. /*-***************************************************************************
  297. * Streaming decompression - HowTo
  298. *
  299. * A ZSTD_DStream object is required to track streaming operations.
  300. * Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources.
  301. * ZSTD_DStream objects can be re-used multiple times.
  302. *
  303. * Use ZSTD_initDStream() to start a new decompression operation,
  304. * or ZSTD_initDStream_usingDict() if decompression requires a dictionary.
  305. * @return : recommended first input size
  306. *
  307. * Use ZSTD_decompressStream() repetitively to consume your input.
  308. * The function will update both `pos` fields.
  309. * If `input.pos < input.size`, some input has not been consumed.
  310. * It's up to the caller to present again remaining data.
  311. * If `output.pos < output.size`, decoder has flushed everything it could.
  312. * @return : 0 when a frame is completely decoded and fully flushed,
  313. * an error code, which can be tested using ZSTD_isError(),
  314. * any other value > 0, which means there is still some decoding to do to complete current frame.
  315. * The return value is a suggested next input size (a hint to improve latency) that will never load more than the current frame.
  316. * *******************************************************************************/
  317. typedef ZSTD_DCtx ZSTD_DStream; /**< DCtx and DStream are now effectively same object (>= v1.3.0) */
  318. /* For compatibility with versions <= v1.2.0, continue to consider them separated. */
  319. /*===== ZSTD_DStream management functions =====*/
  320. ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void);
  321. ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds);
  322. /*===== Streaming decompression functions =====*/
  323. ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds);
  324. ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
  325. ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */
  326. ZSTDLIB_API size_t ZSTD_DStreamOutSize(void); /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */
  327. #endif /* ZSTD_H_235446 */
  328. #if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY)
  329. #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY
  330. /****************************************************************************************
  331. * ADVANCED AND EXPERIMENTAL FUNCTIONS
  332. ****************************************************************************************
  333. * The definitions in this section are considered experimental.
  334. * They should never be used with a dynamic library, as prototypes may change in the future.
  335. * They are provided for advanced scenarios.
  336. * Use them only in association with static linking.
  337. * ***************************************************************************************/
  338. ZSTDLIB_API int ZSTD_minCLevel(void); /*!< minimum negative compression level allowed */
  339. /* --- Constants ---*/
  340. #define ZSTD_MAGICNUMBER 0xFD2FB528 /* v0.8+ */
  341. #define ZSTD_MAGIC_DICTIONARY 0xEC30A437 /* v0.7+ */
  342. #define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50U
  343. #define ZSTD_BLOCKSIZELOG_MAX 17
  344. #define ZSTD_BLOCKSIZE_MAX (1<<ZSTD_BLOCKSIZELOG_MAX) /* define, for static allocation */
  345. #define ZSTD_WINDOWLOG_MAX_32 30
  346. #define ZSTD_WINDOWLOG_MAX_64 31
  347. #define ZSTD_WINDOWLOG_MAX ((unsigned)(sizeof(size_t) == 4 ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64))
  348. #define ZSTD_WINDOWLOG_MIN 10
  349. #define ZSTD_HASHLOG_MAX ((ZSTD_WINDOWLOG_MAX < 30) ? ZSTD_WINDOWLOG_MAX : 30)
  350. #define ZSTD_HASHLOG_MIN 6
  351. #define ZSTD_CHAINLOG_MAX_32 29
  352. #define ZSTD_CHAINLOG_MAX_64 30
  353. #define ZSTD_CHAINLOG_MAX ((unsigned)(sizeof(size_t) == 4 ? ZSTD_CHAINLOG_MAX_32 : ZSTD_CHAINLOG_MAX_64))
  354. #define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN
  355. #define ZSTD_HASHLOG3_MAX 17
  356. #define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1)
  357. #define ZSTD_SEARCHLOG_MIN 1
  358. #define ZSTD_SEARCHLENGTH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */
  359. #define ZSTD_SEARCHLENGTH_MIN 3 /* only for ZSTD_btopt, other strategies are limited to 4 */
  360. #define ZSTD_TARGETLENGTH_MAX ZSTD_BLOCKSIZE_MAX
  361. #define ZSTD_TARGETLENGTH_MIN 0 /* note : comparing this constant to an unsigned results in a tautological test */
  362. #define ZSTD_LDM_MINMATCH_MAX 4096
  363. #define ZSTD_LDM_MINMATCH_MIN 4
  364. #define ZSTD_LDM_BUCKETSIZELOG_MAX 8
  365. #define ZSTD_FRAMEHEADERSIZE_PREFIX 5 /* minimum input size to know frame header size */
  366. #define ZSTD_FRAMEHEADERSIZE_MIN 6
  367. #define ZSTD_FRAMEHEADERSIZE_MAX 18 /* for static allocation */
  368. static const size_t ZSTD_frameHeaderSize_prefix = ZSTD_FRAMEHEADERSIZE_PREFIX;
  369. static const size_t ZSTD_frameHeaderSize_min = ZSTD_FRAMEHEADERSIZE_MIN;
  370. static const size_t ZSTD_frameHeaderSize_max = ZSTD_FRAMEHEADERSIZE_MAX;
  371. static const size_t ZSTD_skippableHeaderSize = 8; /* magic number + skippable frame length */
  372. /* --- Advanced types --- */
  373. typedef enum { ZSTD_fast=1, ZSTD_dfast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2,
  374. ZSTD_btlazy2, ZSTD_btopt, ZSTD_btultra } ZSTD_strategy; /* from faster to stronger */
  375. typedef struct {
  376. unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */
  377. unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */
  378. unsigned hashLog; /**< dispatch table : larger == faster, more memory */
  379. unsigned searchLog; /**< nb of searches : larger == more compression, slower */
  380. unsigned searchLength; /**< match length searched : larger == faster decompression, sometimes less compression */
  381. unsigned targetLength; /**< acceptable match size for optimal parser (only) : larger == more compression, slower */
  382. ZSTD_strategy strategy;
  383. } ZSTD_compressionParameters;
  384. typedef struct {
  385. unsigned contentSizeFlag; /**< 1: content size will be in frame header (when known) */
  386. unsigned checksumFlag; /**< 1: generate a 32-bits checksum at end of frame, for error detection */
  387. unsigned noDictIDFlag; /**< 1: no dictID will be saved into frame header (if dictionary compression) */
  388. } ZSTD_frameParameters;
  389. typedef struct {
  390. ZSTD_compressionParameters cParams;
  391. ZSTD_frameParameters fParams;
  392. } ZSTD_parameters;
  393. typedef struct ZSTD_CCtx_params_s ZSTD_CCtx_params;
  394. typedef enum {
  395. ZSTD_dct_auto=0, /* dictionary is "full" when starting with ZSTD_MAGIC_DICTIONARY, otherwise it is "rawContent" */
  396. ZSTD_dct_rawContent, /* ensures dictionary is always loaded as rawContent, even if it starts with ZSTD_MAGIC_DICTIONARY */
  397. ZSTD_dct_fullDict /* refuses to load a dictionary if it does not respect Zstandard's specification */
  398. } ZSTD_dictContentType_e;
  399. typedef enum {
  400. ZSTD_dlm_byCopy = 0, /**< Copy dictionary content internally */
  401. ZSTD_dlm_byRef, /**< Reference dictionary content -- the dictionary buffer must outlive its users. */
  402. } ZSTD_dictLoadMethod_e;
  403. /***************************************
  404. * Frame size functions
  405. ***************************************/
  406. /*! ZSTD_findFrameCompressedSize() :
  407. * `src` should point to the start of a ZSTD encoded frame or skippable frame
  408. * `srcSize` must be >= first frame size
  409. * @return : the compressed size of the first frame starting at `src`,
  410. * suitable to pass to `ZSTD_decompress` or similar,
  411. * or an error code if input is invalid */
  412. ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize);
  413. /*! ZSTD_findDecompressedSize() :
  414. * `src` should point the start of a series of ZSTD encoded and/or skippable frames
  415. * `srcSize` must be the _exact_ size of this series
  416. * (i.e. there should be a frame boundary exactly at `srcSize` bytes after `src`)
  417. * @return : - decompressed size of all data in all successive frames
  418. * - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN
  419. * - if an error occurred: ZSTD_CONTENTSIZE_ERROR
  420. *
  421. * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode.
  422. * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size.
  423. * In which case, it's necessary to use streaming mode to decompress data.
  424. * note 2 : decompressed size is always present when compression is done with ZSTD_compress()
  425. * note 3 : decompressed size can be very large (64-bits value),
  426. * potentially larger than what local system can handle as a single memory segment.
  427. * In which case, it's necessary to use streaming mode to decompress data.
  428. * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified.
  429. * Always ensure result fits within application's authorized limits.
  430. * Each application can set its own limits.
  431. * note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to
  432. * read each contained frame header. This is fast as most of the data is skipped,
  433. * however it does mean that all frame data must be present and valid. */
  434. ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize);
  435. /*! ZSTD_frameHeaderSize() :
  436. * srcSize must be >= ZSTD_frameHeaderSize_prefix.
  437. * @return : size of the Frame Header,
  438. * or an error code (if srcSize is too small) */
  439. ZSTDLIB_API size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize);
  440. /***************************************
  441. * Memory management
  442. ***************************************/
  443. /*! ZSTD_sizeof_*() :
  444. * These functions give the current memory usage of selected object.
  445. * Object memory usage can evolve when re-used. */
  446. ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx);
  447. ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx);
  448. ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);
  449. ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);
  450. ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict);
  451. ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
  452. /*! ZSTD_estimate*() :
  453. * These functions make it possible to estimate memory usage
  454. * of a future {D,C}Ctx, before its creation.
  455. * ZSTD_estimateCCtxSize() will provide a budget large enough for any compression level up to selected one.
  456. * It will also consider src size to be arbitrarily "large", which is worst case.
  457. * If srcSize is known to always be small, ZSTD_estimateCCtxSize_usingCParams() can provide a tighter estimation.
  458. * ZSTD_estimateCCtxSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.
  459. * ZSTD_estimateCCtxSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParam_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_p_nbWorkers is >= 1.
  460. * Note : CCtx size estimation is only correct for single-threaded compression. */
  461. ZSTDLIB_API size_t ZSTD_estimateCCtxSize(int compressionLevel);
  462. ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams);
  463. ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params);
  464. ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void);
  465. /*! ZSTD_estimateCStreamSize() :
  466. * ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one.
  467. * It will also consider src size to be arbitrarily "large", which is worst case.
  468. * If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation.
  469. * ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.
  470. * ZSTD_estimateCStreamSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParam_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_p_nbWorkers is >= 1.
  471. * Note : CStream size estimation is only correct for single-threaded compression.
  472. * ZSTD_DStream memory budget depends on window Size.
  473. * This information can be passed manually, using ZSTD_estimateDStreamSize,
  474. * or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame();
  475. * Note : if streaming is init with function ZSTD_init?Stream_usingDict(),
  476. * an internal ?Dict will be created, which additional size is not estimated here.
  477. * In this case, get total size by adding ZSTD_estimate?DictSize */
  478. ZSTDLIB_API size_t ZSTD_estimateCStreamSize(int compressionLevel);
  479. ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams);
  480. ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params);
  481. ZSTDLIB_API size_t ZSTD_estimateDStreamSize(size_t windowSize);
  482. ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize);
  483. /*! ZSTD_estimate?DictSize() :
  484. * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict().
  485. * ZSTD_estimateCDictSize_advanced() makes it possible to control compression parameters precisely, like ZSTD_createCDict_advanced().
  486. * Note : dictionaries created by reference (`ZSTD_dlm_byRef`) are logically smaller.
  487. */
  488. ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel);
  489. ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, ZSTD_dictLoadMethod_e dictLoadMethod);
  490. ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod);
  491. /*! ZSTD_initStatic*() :
  492. * Initialize an object using a pre-allocated fixed-size buffer.
  493. * workspace: The memory area to emplace the object into.
  494. * Provided pointer *must be 8-bytes aligned*.
  495. * Buffer must outlive object.
  496. * workspaceSize: Use ZSTD_estimate*Size() to determine
  497. * how large workspace must be to support target scenario.
  498. * @return : pointer to object (same address as workspace, just different type),
  499. * or NULL if error (size too small, incorrect alignment, etc.)
  500. * Note : zstd will never resize nor malloc() when using a static buffer.
  501. * If the object requires more memory than available,
  502. * zstd will just error out (typically ZSTD_error_memory_allocation).
  503. * Note 2 : there is no corresponding "free" function.
  504. * Since workspace is allocated externally, it must be freed externally too.
  505. * Note 3 : cParams : use ZSTD_getCParams() to convert a compression level
  506. * into its associated cParams.
  507. * Limitation 1 : currently not compatible with internal dictionary creation, triggered by
  508. * ZSTD_CCtx_loadDictionary(), ZSTD_initCStream_usingDict() or ZSTD_initDStream_usingDict().
  509. * Limitation 2 : static cctx currently not compatible with multi-threading.
  510. * Limitation 3 : static dctx is incompatible with legacy support.
  511. */
  512. ZSTDLIB_API ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize);
  513. ZSTDLIB_API ZSTD_CStream* ZSTD_initStaticCStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticCCtx() */
  514. ZSTDLIB_API ZSTD_DCtx* ZSTD_initStaticDCtx(void* workspace, size_t workspaceSize);
  515. ZSTDLIB_API ZSTD_DStream* ZSTD_initStaticDStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticDCtx() */
  516. ZSTDLIB_API const ZSTD_CDict* ZSTD_initStaticCDict(
  517. void* workspace, size_t workspaceSize,
  518. const void* dict, size_t dictSize,
  519. ZSTD_dictLoadMethod_e dictLoadMethod,
  520. ZSTD_dictContentType_e dictContentType,
  521. ZSTD_compressionParameters cParams);
  522. ZSTDLIB_API const ZSTD_DDict* ZSTD_initStaticDDict(
  523. void* workspace, size_t workspaceSize,
  524. const void* dict, size_t dictSize,
  525. ZSTD_dictLoadMethod_e dictLoadMethod,
  526. ZSTD_dictContentType_e dictContentType);
  527. /*! Custom memory allocation :
  528. * These prototypes make it possible to pass your own allocation/free functions.
  529. * ZSTD_customMem is provided at creation time, using ZSTD_create*_advanced() variants listed below.
  530. * All allocation/free operations will be completed using these custom variants instead of regular <stdlib.h> ones.
  531. */
  532. typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size);
  533. typedef void (*ZSTD_freeFunction) (void* opaque, void* address);
  534. typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem;
  535. static ZSTD_customMem const ZSTD_defaultCMem = { NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */
  536. ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem);
  537. ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem);
  538. ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem);
  539. ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem);
  540. ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize,
  541. ZSTD_dictLoadMethod_e dictLoadMethod,
  542. ZSTD_dictContentType_e dictContentType,
  543. ZSTD_compressionParameters cParams,
  544. ZSTD_customMem customMem);
  545. ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize,
  546. ZSTD_dictLoadMethod_e dictLoadMethod,
  547. ZSTD_dictContentType_e dictContentType,
  548. ZSTD_customMem customMem);
  549. /***************************************
  550. * Advanced compression functions
  551. ***************************************/
  552. /*! ZSTD_createCDict_byReference() :
  553. * Create a digested dictionary for compression
  554. * Dictionary content is simply referenced, and therefore stays in dictBuffer.
  555. * It is important that dictBuffer outlives CDict, it must remain read accessible throughout the lifetime of CDict */
  556. ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel);
  557. /*! ZSTD_getCParams() :
  558. * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize.
  559. * `estimatedSrcSize` value is optional, select 0 if not known */
  560. ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
  561. /*! ZSTD_getParams() :
  562. * same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`.
  563. * All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 */
  564. ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
  565. /*! ZSTD_checkCParams() :
  566. * Ensure param values remain within authorized range */
  567. ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
  568. /*! ZSTD_adjustCParams() :
  569. * optimize params for a given `srcSize` and `dictSize`.
  570. * both values are optional, select `0` if unknown. */
  571. ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);
  572. /*! ZSTD_compress_advanced() :
  573. * Same as ZSTD_compress_usingDict(), with fine-tune control over each compression parameter */
  574. ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* cctx,
  575. void* dst, size_t dstCapacity,
  576. const void* src, size_t srcSize,
  577. const void* dict,size_t dictSize,
  578. ZSTD_parameters params);
  579. /*! ZSTD_compress_usingCDict_advanced() :
  580. * Same as ZSTD_compress_usingCDict(), with fine-tune control over frame parameters */
  581. ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx,
  582. void* dst, size_t dstCapacity,
  583. const void* src, size_t srcSize,
  584. const ZSTD_CDict* cdict, ZSTD_frameParameters fParams);
  585. /*--- Advanced decompression functions ---*/
  586. /*! ZSTD_isFrame() :
  587. * Tells if the content of `buffer` starts with a valid Frame Identifier.
  588. * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
  589. * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
  590. * Note 3 : Skippable Frame Identifiers are considered valid. */
  591. ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size);
  592. /*! ZSTD_createDDict_byReference() :
  593. * Create a digested dictionary, ready to start decompression operation without startup delay.
  594. * Dictionary content is referenced, and therefore stays in dictBuffer.
  595. * It is important that dictBuffer outlives DDict,
  596. * it must remain read accessible throughout the lifetime of DDict */
  597. ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize);
  598. /*! ZSTD_getDictID_fromDict() :
  599. * Provides the dictID stored within dictionary.
  600. * if @return == 0, the dictionary is not conformant with Zstandard specification.
  601. * It can still be loaded, but as a content-only dictionary. */
  602. ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize);
  603. /*! ZSTD_getDictID_fromDDict() :
  604. * Provides the dictID of the dictionary loaded into `ddict`.
  605. * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
  606. * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
  607. ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict);
  608. /*! ZSTD_getDictID_fromFrame() :
  609. * Provides the dictID required to decompressed the frame stored within `src`.
  610. * If @return == 0, the dictID could not be decoded.
  611. * This could for one of the following reasons :
  612. * - The frame does not require a dictionary to be decoded (most common case).
  613. * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information.
  614. * Note : this use case also happens when using a non-conformant dictionary.
  615. * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).
  616. * - This is not a Zstandard frame.
  617. * When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */
  618. ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize);
  619. /********************************************************************
  620. * Advanced streaming functions
  621. ********************************************************************/
  622. /*===== Advanced Streaming compression functions =====*/
  623. ZSTDLIB_API size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize); /**< pledgedSrcSize must be correct. If it is not known at init time, use ZSTD_CONTENTSIZE_UNKNOWN. Note that, for compatibility with older programs, "0" also disables frame content size field. It may be enabled in the future. */
  624. ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel); /**< creates of an internal CDict (incompatible with static CCtx), except if dict == NULL or dictSize < 8, in which case no dict is used. Note: dict is loaded with ZSTD_dm_auto (treated as a full zstd dictionary if it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy.*/
  625. ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize,
  626. ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize must be correct. If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN. dict is loaded with ZSTD_dm_auto and ZSTD_dlm_byCopy. */
  627. ZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict); /**< note : cdict will just be referenced, and must outlive compression session */
  628. ZSTDLIB_API size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, const ZSTD_CDict* cdict, ZSTD_frameParameters fParams, unsigned long long pledgedSrcSize); /**< same as ZSTD_initCStream_usingCDict(), with control over frame parameters. pledgedSrcSize must be correct. If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN. */
  629. /*! ZSTD_resetCStream() :
  630. * start a new compression job, using same parameters from previous job.
  631. * This is typically useful to skip dictionary loading stage, since it will re-use it in-place.
  632. * Note that zcs must be init at least once before using ZSTD_resetCStream().
  633. * If pledgedSrcSize is not known at reset time, use macro ZSTD_CONTENTSIZE_UNKNOWN.
  634. * If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end.
  635. * For the time being, pledgedSrcSize==0 is interpreted as "srcSize unknown" for compatibility with older programs,
  636. * but it will change to mean "empty" in future version, so use macro ZSTD_CONTENTSIZE_UNKNOWN instead.
  637. * @return : 0, or an error code (which can be tested using ZSTD_isError())
  638. */
  639. ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize);
  640. typedef struct {
  641. unsigned long long ingested; /* nb input bytes read and buffered */
  642. unsigned long long consumed; /* nb input bytes actually compressed */
  643. unsigned long long produced; /* nb of compressed bytes generated and buffered */
  644. unsigned long long flushed; /* nb of compressed bytes flushed : not provided; can be tracked from caller side */
  645. unsigned currentJobID; /* MT only : latest started job nb */
  646. unsigned nbActiveWorkers; /* MT only : nb of workers actively compressing at probe time */
  647. } ZSTD_frameProgression;
  648. /* ZSTD_getFrameProgression() :
  649. * tells how much data has been ingested (read from input)
  650. * consumed (input actually compressed) and produced (output) for current frame.
  651. * Note : (ingested - consumed) is amount of input data buffered internally, not yet compressed.
  652. * Aggregates progression inside active worker threads.
  653. */
  654. ZSTDLIB_API ZSTD_frameProgression ZSTD_getFrameProgression(const ZSTD_CCtx* cctx);
  655. /*! ZSTD_toFlushNow() :
  656. * Tell how many bytes are ready to be flushed immediately.
  657. * Useful for multithreading scenarios (nbWorkers >= 1).
  658. * Probe the oldest active job, defined as oldest job not yet entirely flushed,
  659. * and check its output buffer.
  660. * @return : amount of data stored in oldest job and ready to be flushed immediately.
  661. * if @return == 0, it means either :
  662. * + there is no active job (could be checked with ZSTD_frameProgression()), or
  663. * + oldest job is still actively compressing data,
  664. * but everything it has produced has also been flushed so far,
  665. * therefore flushing speed is currently limited by production speed of oldest job
  666. * irrespective of the speed of concurrent newer jobs.
  667. */
  668. ZSTDLIB_API size_t ZSTD_toFlushNow(ZSTD_CCtx* cctx);
  669. /*===== Advanced Streaming decompression functions =====*/
  670. typedef enum { DStream_p_maxWindowSize } ZSTD_DStreamParameter_e;
  671. ZSTDLIB_API size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, ZSTD_DStreamParameter_e paramType, unsigned paramValue); /* obsolete : this API will be removed in a future version */
  672. ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize); /**< note: no dictionary will be used if dict == NULL or dictSize < 8 */
  673. ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict); /**< note : ddict is referenced, it must outlive decompression session */
  674. ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds); /**< re-use decompression parameters from previous init; saves dictionary loading */
  675. /*********************************************************************
  676. * Buffer-less and synchronous inner streaming functions
  677. *
  678. * This is an advanced API, giving full control over buffer management, for users which need direct control over memory.
  679. * But it's also a complex one, with several restrictions, documented below.
  680. * Prefer normal streaming API for an easier experience.
  681. ********************************************************************* */
  682. /**
  683. Buffer-less streaming compression (synchronous mode)
  684. A ZSTD_CCtx object is required to track streaming operations.
  685. Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource.
  686. ZSTD_CCtx object can be re-used multiple times within successive compression operations.
  687. Start by initializing a context.
  688. Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression,
  689. or ZSTD_compressBegin_advanced(), for finer parameter control.
  690. It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx()
  691. Then, consume your input using ZSTD_compressContinue().
  692. There are some important considerations to keep in mind when using this advanced function :
  693. - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffers only.
  694. - Interface is synchronous : input is consumed entirely and produces 1+ compressed blocks.
  695. - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario.
  696. Worst case evaluation is provided by ZSTD_compressBound().
  697. ZSTD_compressContinue() doesn't guarantee recover after a failed compression.
  698. - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog).
  699. It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks)
  700. - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps.
  701. In which case, it will "discard" the relevant memory section from its history.
  702. Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum.
  703. It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame.
  704. Without last block mark, frames are considered unfinished (hence corrupted) by compliant decoders.
  705. `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress again.
  706. */
  707. /*===== Buffer-less streaming compression functions =====*/
  708. ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel);
  709. ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel);
  710. ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize : If srcSize is not known at init time, use ZSTD_CONTENTSIZE_UNKNOWN */
  711. ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); /**< note: fails if cdict==NULL */
  712. ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict_advanced(ZSTD_CCtx* const cctx, const ZSTD_CDict* const cdict, ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize); /* compression parameters are already set within cdict. pledgedSrcSize must be correct. If srcSize is not known, use macro ZSTD_CONTENTSIZE_UNKNOWN */
  713. ZSTDLIB_API size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize); /**< note: if pledgedSrcSize is not known, use ZSTD_CONTENTSIZE_UNKNOWN */
  714. ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  715. ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  716. /*-
  717. Buffer-less streaming decompression (synchronous mode)
  718. A ZSTD_DCtx object is required to track streaming operations.
  719. Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.
  720. A ZSTD_DCtx object can be re-used multiple times.
  721. First typical operation is to retrieve frame parameters, using ZSTD_getFrameHeader().
  722. Frame header is extracted from the beginning of compressed frame, so providing only the frame's beginning is enough.
  723. Data fragment must be large enough to ensure successful decoding.
  724. `ZSTD_frameHeaderSize_max` bytes is guaranteed to always be large enough.
  725. @result : 0 : successful decoding, the `ZSTD_frameHeader` structure is correctly filled.
  726. >0 : `srcSize` is too small, please provide at least @result bytes on next attempt.
  727. errorCode, which can be tested using ZSTD_isError().
  728. It fills a ZSTD_frameHeader structure with important information to correctly decode the frame,
  729. such as the dictionary ID, content size, or maximum back-reference distance (`windowSize`).
  730. Note that these values could be wrong, either because of data corruption, or because a 3rd party deliberately spoofs false information.
  731. As a consequence, check that values remain within valid application range.
  732. For example, do not allocate memory blindly, check that `windowSize` is within expectation.
  733. Each application can set its own limits, depending on local restrictions.
  734. For extended interoperability, it is recommended to support `windowSize` of at least 8 MB.
  735. ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize` bytes.
  736. ZSTD_decompressContinue() is very sensitive to contiguity,
  737. if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place,
  738. or that previous contiguous segment is large enough to properly handle maximum back-reference distance.
  739. There are multiple ways to guarantee this condition.
  740. The most memory efficient way is to use a round buffer of sufficient size.
  741. Sufficient size is determined by invoking ZSTD_decodingBufferSize_min(),
  742. which can @return an error code if required value is too large for current system (in 32-bits mode).
  743. In a round buffer methodology, ZSTD_decompressContinue() decompresses each block next to previous one,
  744. up to the moment there is not enough room left in the buffer to guarantee decoding another full block,
  745. which maximum size is provided in `ZSTD_frameHeader` structure, field `blockSizeMax`.
  746. At which point, decoding can resume from the beginning of the buffer.
  747. Note that already decoded data stored in the buffer should be flushed before being overwritten.
  748. There are alternatives possible, for example using two or more buffers of size `windowSize` each, though they consume more memory.
  749. Finally, if you control the compression process, you can also ignore all buffer size rules,
  750. as long as the encoder and decoder progress in "lock-step",
  751. aka use exactly the same buffer sizes, break contiguity at the same place, etc.
  752. Once buffers are setup, start decompression, with ZSTD_decompressBegin().
  753. If decompression requires a dictionary, use ZSTD_decompressBegin_usingDict() or ZSTD_decompressBegin_usingDDict().
  754. Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively.
  755. ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue().
  756. ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail.
  757. @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity).
  758. It can be zero : it just means ZSTD_decompressContinue() has decoded some metadata item.
  759. It can also be an error code, which can be tested with ZSTD_isError().
  760. A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
  761. Context can then be reset to start a new decompression.
  762. Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType().
  763. This information is not required to properly decode a frame.
  764. == Special case : skippable frames ==
  765. Skippable frames allow integration of user-defined data into a flow of concatenated frames.
  766. Skippable frames will be ignored (skipped) by decompressor.
  767. The format of skippable frames is as follows :
  768. a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F
  769. b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits
  770. c) Frame Content - any content (User Data) of length equal to Frame Size
  771. For skippable frames ZSTD_getFrameHeader() returns zfhPtr->frameType==ZSTD_skippableFrame.
  772. For skippable frames ZSTD_decompressContinue() always returns 0 : it only skips the content.
  773. */
  774. /*===== Buffer-less streaming decompression functions =====*/
  775. typedef enum { ZSTD_frame, ZSTD_skippableFrame } ZSTD_frameType_e;
  776. typedef struct {
  777. unsigned long long frameContentSize; /* if == ZSTD_CONTENTSIZE_UNKNOWN, it means this field is not available. 0 means "empty" */
  778. unsigned long long windowSize; /* can be very large, up to <= frameContentSize */
  779. unsigned blockSizeMax;
  780. ZSTD_frameType_e frameType; /* if == ZSTD_skippableFrame, frameContentSize is the size of skippable content */
  781. unsigned headerSize;
  782. unsigned dictID;
  783. unsigned checksumFlag;
  784. } ZSTD_frameHeader;
  785. /** ZSTD_getFrameHeader() :
  786. * decode Frame Header, or requires larger `srcSize`.
  787. * @return : 0, `zfhPtr` is correctly filled,
  788. * >0, `srcSize` is too small, value is wanted `srcSize` amount,
  789. * or an error code, which can be tested using ZSTD_isError() */
  790. ZSTDLIB_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize); /**< doesn't consume input */
  791. ZSTDLIB_API size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize); /**< when frame content size is not known, pass in frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN */
  792. ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx);
  793. ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
  794. ZSTDLIB_API size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);
  795. ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);
  796. ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  797. /* misc */
  798. ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx);
  799. typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e;
  800. ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx);
  801. /* ============================================ */
  802. /** New advanced API (experimental) */
  803. /* ============================================ */
  804. /* API design :
  805. * In this advanced API, parameters are pushed one by one into an existing context,
  806. * using ZSTD_CCtx_set*() functions.
  807. * Pushed parameters are sticky : they are applied to next job, and any subsequent job.
  808. * It's possible to reset parameters to "default" using ZSTD_CCtx_reset().
  809. * Important : "sticky" parameters only work with `ZSTD_compress_generic()` !
  810. * For any other entry point, "sticky" parameters are ignored !
  811. *
  812. * This API is intended to replace all others advanced / experimental API entry points.
  813. */
  814. /* note on enum design :
  815. * All enum will be pinned to explicit values before reaching "stable API" status */
  816. typedef enum {
  817. /* Opened question : should we have a format ZSTD_f_auto ?
  818. * Today, it would mean exactly the same as ZSTD_f_zstd1.
  819. * But, in the future, should several formats become supported,
  820. * on the compression side, it would mean "default format".
  821. * On the decompression side, it would mean "automatic format detection",
  822. * so that ZSTD_f_zstd1 would mean "accept *only* zstd frames".
  823. * Since meaning is a little different, another option could be to define different enums for compression and decompression.
  824. * This question could be kept for later, when there are actually multiple formats to support,
  825. * but there is also the question of pinning enum values, and pinning value `0` is especially important */
  826. ZSTD_f_zstd1 = 0, /* zstd frame format, specified in zstd_compression_format.md (default) */
  827. ZSTD_f_zstd1_magicless, /* Variant of zstd frame format, without initial 4-bytes magic number.
  828. * Useful to save 4 bytes per generated frame.
  829. * Decoder cannot recognise automatically this format, requiring instructions. */
  830. } ZSTD_format_e;
  831. typedef enum {
  832. /* compression format */
  833. ZSTD_p_format = 10, /* See ZSTD_format_e enum definition.
  834. * Cast selected format as unsigned for ZSTD_CCtx_setParameter() compatibility. */
  835. /* compression parameters */
  836. ZSTD_p_compressionLevel=100, /* Update all compression parameters according to pre-defined cLevel table
  837. * Default level is ZSTD_CLEVEL_DEFAULT==3.
  838. * Special: value 0 means default, which is controlled by ZSTD_CLEVEL_DEFAULT.
  839. * Note 1 : it's possible to pass a negative compression level by casting it to unsigned type.
  840. * Note 2 : setting a level sets all default values of other compression parameters.
  841. * Note 3 : setting compressionLevel automatically updates ZSTD_p_compressLiterals. */
  842. ZSTD_p_windowLog, /* Maximum allowed back-reference distance, expressed as power of 2.
  843. * Must be clamped between ZSTD_WINDOWLOG_MIN and ZSTD_WINDOWLOG_MAX.
  844. * Special: value 0 means "use default windowLog".
  845. * Note: Using a window size greater than ZSTD_MAXWINDOWSIZE_DEFAULT (default: 2^27)
  846. * requires explicitly allowing such window size during decompression stage. */
  847. ZSTD_p_hashLog, /* Size of the initial probe table, as a power of 2.
  848. * Resulting table size is (1 << (hashLog+2)).
  849. * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX.
  850. * Larger tables improve compression ratio of strategies <= dFast,
  851. * and improve speed of strategies > dFast.
  852. * Special: value 0 means "use default hashLog". */
  853. ZSTD_p_chainLog, /* Size of the multi-probe search table, as a power of 2.
  854. * Resulting table size is (1 << (chainLog+2)).
  855. * Must be clamped between ZSTD_CHAINLOG_MIN and ZSTD_CHAINLOG_MAX.
  856. * Larger tables result in better and slower compression.
  857. * This parameter is useless when using "fast" strategy.
  858. * Note it's still useful when using "dfast" strategy,
  859. * in which case it defines a secondary probe table.
  860. * Special: value 0 means "use default chainLog". */
  861. ZSTD_p_searchLog, /* Number of search attempts, as a power of 2.
  862. * More attempts result in better and slower compression.
  863. * This parameter is useless when using "fast" and "dFast" strategies.
  864. * Special: value 0 means "use default searchLog". */
  865. ZSTD_p_minMatch, /* Minimum size of searched matches (note : repCode matches can be smaller).
  866. * Larger values make faster compression and decompression, but decrease ratio.
  867. * Must be clamped between ZSTD_SEARCHLENGTH_MIN and ZSTD_SEARCHLENGTH_MAX.
  868. * Note that currently, for all strategies < btopt, effective minimum is 4.
  869. * , for all strategies > fast, effective maximum is 6.
  870. * Special: value 0 means "use default minMatchLength". */
  871. ZSTD_p_targetLength, /* Impact of this field depends on strategy.
  872. * For strategies btopt & btultra:
  873. * Length of Match considered "good enough" to stop search.
  874. * Larger values make compression stronger, and slower.
  875. * For strategy fast:
  876. * Distance between match sampling.
  877. * Larger values make compression faster, and weaker.
  878. * Special: value 0 means "use default targetLength". */
  879. ZSTD_p_compressionStrategy, /* See ZSTD_strategy enum definition.
  880. * Cast selected strategy as unsigned for ZSTD_CCtx_setParameter() compatibility.
  881. * The higher the value of selected strategy, the more complex it is,
  882. * resulting in stronger and slower compression.
  883. * Special: value 0 means "use default strategy". */
  884. ZSTD_p_enableLongDistanceMatching=160, /* Enable long distance matching.
  885. * This parameter is designed to improve compression ratio
  886. * for large inputs, by finding large matches at long distance.
  887. * It increases memory usage and window size.
  888. * Note: enabling this parameter increases ZSTD_p_windowLog to 128 MB
  889. * except when expressly set to a different value. */
  890. ZSTD_p_ldmHashLog, /* Size of the table for long distance matching, as a power of 2.
  891. * Larger values increase memory usage and compression ratio,
  892. * but decrease compression speed.
  893. * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX
  894. * default: windowlog - 7.
  895. * Special: value 0 means "automatically determine hashlog". */
  896. ZSTD_p_ldmMinMatch, /* Minimum match size for long distance matcher.
  897. * Larger/too small values usually decrease compression ratio.
  898. * Must be clamped between ZSTD_LDM_MINMATCH_MIN and ZSTD_LDM_MINMATCH_MAX.
  899. * Special: value 0 means "use default value" (default: 64). */
  900. ZSTD_p_ldmBucketSizeLog, /* Log size of each bucket in the LDM hash table for collision resolution.
  901. * Larger values improve collision resolution but decrease compression speed.
  902. * The maximum value is ZSTD_LDM_BUCKETSIZELOG_MAX .
  903. * Special: value 0 means "use default value" (default: 3). */
  904. ZSTD_p_ldmHashEveryLog, /* Frequency of inserting/looking up entries in the LDM hash table.
  905. * Must be clamped between 0 and (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN).
  906. * Default is MAX(0, (windowLog - ldmHashLog)), optimizing hash table usage.
  907. * Larger values improve compression speed.
  908. * Deviating far from default value will likely result in a compression ratio decrease.
  909. * Special: value 0 means "automatically determine hashEveryLog". */
  910. /* frame parameters */
  911. ZSTD_p_contentSizeFlag=200, /* Content size will be written into frame header _whenever known_ (default:1)
  912. * Content size must be known at the beginning of compression,
  913. * it is provided using ZSTD_CCtx_setPledgedSrcSize() */
  914. ZSTD_p_checksumFlag, /* A 32-bits checksum of content is written at end of frame (default:0) */
  915. ZSTD_p_dictIDFlag, /* When applicable, dictionary's ID is written into frame header (default:1) */
  916. /* multi-threading parameters */
  917. /* These parameters are only useful if multi-threading is enabled (ZSTD_MULTITHREAD).
  918. * They return an error otherwise. */
  919. ZSTD_p_nbWorkers=400, /* Select how many threads will be spawned to compress in parallel.
  920. * When nbWorkers >= 1, triggers asynchronous mode :
  921. * ZSTD_compress_generic() consumes some input, flush some output if possible, and immediately gives back control to caller,
  922. * while compression work is performed in parallel, within worker threads.
  923. * (note : a strong exception to this rule is when first invocation sets ZSTD_e_end : it becomes a blocking call).
  924. * More workers improve speed, but also increase memory usage.
  925. * Default value is `0`, aka "single-threaded mode" : no worker is spawned, compression is performed inside Caller's thread, all invocations are blocking */
  926. ZSTD_p_jobSize, /* Size of a compression job. This value is enforced only in non-blocking mode.
  927. * Each compression job is completed in parallel, so this value indirectly controls the nb of active threads.
  928. * 0 means default, which is dynamically determined based on compression parameters.
  929. * Job size must be a minimum of overlapSize, or 1 MB, whichever is largest.
  930. * The minimum size is automatically and transparently enforced */
  931. ZSTD_p_overlapSizeLog, /* Size of previous input reloaded at the beginning of each job.
  932. * 0 => no overlap, 6(default) => use 1/8th of windowSize, >=9 => use full windowSize */
  933. /* =================================================================== */
  934. /* experimental parameters - no stability guaranteed */
  935. /* =================================================================== */
  936. ZSTD_p_forceMaxWindow=1100, /* Force back-reference distances to remain < windowSize,
  937. * even when referencing into Dictionary content (default:0) */
  938. ZSTD_p_forceAttachDict, /* ZSTD supports usage of a CDict in-place
  939. * (avoiding having to copy the compression tables
  940. * from the CDict into the working context). Using
  941. * a CDict in this way saves an initial setup step,
  942. * but comes at the cost of more work per byte of
  943. * input. ZSTD has a simple internal heuristic that
  944. * guesses which strategy will be faster. You can
  945. * use this flag to override that guess.
  946. *
  947. * Note that the by-reference, in-place strategy is
  948. * only used when reusing a compression context
  949. * with compatible compression parameters. (If
  950. * incompatible / uninitialized, the working
  951. * context needs to be cleared anyways, which is
  952. * about as expensive as overwriting it with the
  953. * dictionary context, so there's no savings in
  954. * using the CDict by-ref.)
  955. *
  956. * Values greater than 0 force attaching the dict.
  957. * Values less than 0 force copying the dict.
  958. * 0 selects the default heuristic-guided behavior.
  959. */
  960. } ZSTD_cParameter;
  961. /*! ZSTD_CCtx_setParameter() :
  962. * Set one compression parameter, selected by enum ZSTD_cParameter.
  963. * Setting a parameter is generally only possible during frame initialization (before starting compression).
  964. * Exception : when using multi-threading mode (nbThreads >= 1),
  965. * following parameters can be updated _during_ compression (within same frame):
  966. * => compressionLevel, hashLog, chainLog, searchLog, minMatch, targetLength and strategy.
  967. * new parameters will be active on next job, or after a flush().
  968. * Note : when `value` type is not unsigned (int, or enum), cast it to unsigned for proper type checking.
  969. * @result : informational value (typically, value being set, correctly clamped),
  970. * or an error code (which can be tested with ZSTD_isError()). */
  971. ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned value);
  972. /*! ZSTD_CCtx_getParameter() :
  973. * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter.
  974. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  975. */
  976. ZSTDLIB_API size_t ZSTD_CCtx_getParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned* value);
  977. /*! ZSTD_CCtx_setPledgedSrcSize() :
  978. * Total input data size to be compressed as a single frame.
  979. * This value will be controlled at the end, and result in error if not respected.
  980. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  981. * Note 1 : 0 means zero, empty.
  982. * In order to mean "unknown content size", pass constant ZSTD_CONTENTSIZE_UNKNOWN.
  983. * ZSTD_CONTENTSIZE_UNKNOWN is default value for any new compression job.
  984. * Note 2 : If all data is provided and consumed in a single round,
  985. * this value is overriden by srcSize instead. */
  986. ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize);
  987. /*! ZSTD_CCtx_loadDictionary() :
  988. * Create an internal CDict from `dict` buffer.
  989. * Decompression will have to use same dictionary.
  990. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  991. * Special: Adding a NULL (or 0-size) dictionary invalidates previous dictionary,
  992. * meaning "return to no-dictionary mode".
  993. * Note 1 : Dictionary will be used for all future compression jobs.
  994. * To return to "no-dictionary" situation, load a NULL dictionary
  995. * Note 2 : Loading a dictionary involves building tables, which are dependent on compression parameters.
  996. * For this reason, compression parameters cannot be changed anymore after loading a dictionary.
  997. * It's also a CPU consuming operation, with non-negligible impact on latency.
  998. * Note 3 :`dict` content will be copied internally.
  999. * Use ZSTD_CCtx_loadDictionary_byReference() to reference dictionary content instead.
  1000. * In such a case, dictionary buffer must outlive its users.
  1001. * Note 4 : Use ZSTD_CCtx_loadDictionary_advanced()
  1002. * to precisely select how dictionary content must be interpreted. */
  1003. ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize);
  1004. ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize);
  1005. ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType);
  1006. /*! ZSTD_CCtx_refCDict() :
  1007. * Reference a prepared dictionary, to be used for all next compression jobs.
  1008. * Note that compression parameters are enforced from within CDict,
  1009. * and supercede any compression parameter previously set within CCtx.
  1010. * The dictionary will remain valid for future compression jobs using same CCtx.
  1011. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  1012. * Special : adding a NULL CDict means "return to no-dictionary mode".
  1013. * Note 1 : Currently, only one dictionary can be managed.
  1014. * Adding a new dictionary effectively "discards" any previous one.
  1015. * Note 2 : CDict is just referenced, its lifetime must outlive CCtx. */
  1016. ZSTDLIB_API size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict);
  1017. /*! ZSTD_CCtx_refPrefix() :
  1018. * Reference a prefix (single-usage dictionary) for next compression job.
  1019. * Decompression will need same prefix to properly regenerate data.
  1020. * Compressing with a prefix is similar in outcome as performing a diff and compressing it,
  1021. * but performs much faster, especially during decompression (compression speed is tunable with compression level).
  1022. * Note that prefix is **only used once**. Tables are discarded at end of compression job (ZSTD_e_end).
  1023. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  1024. * Special: Adding any prefix (including NULL) invalidates any previous prefix or dictionary
  1025. * Note 1 : Prefix buffer is referenced. It **must** outlive compression job.
  1026. * Its contain must remain unmodified up to end of compression (ZSTD_e_end).
  1027. * Note 2 : If the intention is to diff some large src data blob with some prior version of itself,
  1028. * ensure that the window size is large enough to contain the entire source.
  1029. * See ZSTD_p_windowLog.
  1030. * Note 3 : Referencing a prefix involves building tables, which are dependent on compression parameters.
  1031. * It's a CPU consuming operation, with non-negligible impact on latency.
  1032. * If there is a need to use same prefix multiple times, consider loadDictionary instead.
  1033. * Note 4 : By default, the prefix is treated as raw content (ZSTD_dm_rawContent).
  1034. * Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode. */
  1035. ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx,
  1036. const void* prefix, size_t prefixSize);
  1037. ZSTDLIB_API size_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx,
  1038. const void* prefix, size_t prefixSize,
  1039. ZSTD_dictContentType_e dictContentType);
  1040. /*! ZSTD_CCtx_reset() :
  1041. * Return a CCtx to clean state.
  1042. * Useful after an error, or to interrupt an ongoing compression job and start a new one.
  1043. * Any internal data not yet flushed is cancelled.
  1044. * The parameters and dictionary are kept unchanged, to reset them use ZSTD_CCtx_resetParameters().
  1045. */
  1046. ZSTDLIB_API void ZSTD_CCtx_reset(ZSTD_CCtx* cctx);
  1047. /*! ZSTD_CCtx_resetParameters() :
  1048. * All parameters are back to default values (compression level is ZSTD_CLEVEL_DEFAULT).
  1049. * Dictionary (if any) is dropped.
  1050. * Resetting parameters is only possible during frame initialization (before starting compression).
  1051. * To reset the context use ZSTD_CCtx_reset().
  1052. * @return 0 or an error code (which can be checked with ZSTD_isError()).
  1053. */
  1054. ZSTDLIB_API size_t ZSTD_CCtx_resetParameters(ZSTD_CCtx* cctx);
  1055. typedef enum {
  1056. ZSTD_e_continue=0, /* collect more data, encoder decides when to output compressed result, for optimal conditions */
  1057. ZSTD_e_flush, /* flush any data provided so far - frame will continue, future data can still reference previous data for better compression */
  1058. ZSTD_e_end /* flush any remaining data and close current frame. Any additional data starts a new frame. */
  1059. } ZSTD_EndDirective;
  1060. /*! ZSTD_compress_generic() :
  1061. * Behave about the same as ZSTD_compressStream. To note :
  1062. * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_setParameter()
  1063. * - Compression parameters cannot be changed once compression is started.
  1064. * - outpot->pos must be <= dstCapacity, input->pos must be <= srcSize
  1065. * - outpot->pos and input->pos will be updated. They are guaranteed to remain below their respective limit.
  1066. * - In single-thread mode (default), function is blocking : it completed its job before returning to caller.
  1067. * - In multi-thread mode, function is non-blocking : it just acquires a copy of input, and distribute job to internal worker threads,
  1068. * and then immediately returns, just indicating that there is some data remaining to be flushed.
  1069. * The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte.
  1070. * - Exception : in multi-threading mode, if the first call requests a ZSTD_e_end directive, it is blocking : it will complete compression before giving back control to caller.
  1071. * - @return provides a minimum amount of data remaining to be flushed from internal buffers
  1072. * or an error code, which can be tested using ZSTD_isError().
  1073. * if @return != 0, flush is not fully completed, there is still some data left within internal buffers.
  1074. * This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers.
  1075. * For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed.
  1076. * - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0),
  1077. * only ZSTD_e_end or ZSTD_e_flush operations are allowed.
  1078. * Before starting a new compression job, or changing compression parameters,
  1079. * it is required to fully flush internal buffers.
  1080. */
  1081. ZSTDLIB_API size_t ZSTD_compress_generic (ZSTD_CCtx* cctx,
  1082. ZSTD_outBuffer* output,
  1083. ZSTD_inBuffer* input,
  1084. ZSTD_EndDirective endOp);
  1085. /*! ZSTD_compress_generic_simpleArgs() :
  1086. * Same as ZSTD_compress_generic(),
  1087. * but using only integral types as arguments.
  1088. * Argument list is larger than ZSTD_{in,out}Buffer,
  1089. * but can be helpful for binders from dynamic languages
  1090. * which have troubles handling structures containing memory pointers.
  1091. */
  1092. ZSTDLIB_API size_t ZSTD_compress_generic_simpleArgs (
  1093. ZSTD_CCtx* cctx,
  1094. void* dst, size_t dstCapacity, size_t* dstPos,
  1095. const void* src, size_t srcSize, size_t* srcPos,
  1096. ZSTD_EndDirective endOp);
  1097. /*! ZSTD_CCtx_params :
  1098. * Quick howto :
  1099. * - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure
  1100. * - ZSTD_CCtxParam_setParameter() : Push parameters one by one into
  1101. * an existing ZSTD_CCtx_params structure.
  1102. * This is similar to
  1103. * ZSTD_CCtx_setParameter().
  1104. * - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to
  1105. * an existing CCtx.
  1106. * These parameters will be applied to
  1107. * all subsequent compression jobs.
  1108. * - ZSTD_compress_generic() : Do compression using the CCtx.
  1109. * - ZSTD_freeCCtxParams() : Free the memory.
  1110. *
  1111. * This can be used with ZSTD_estimateCCtxSize_advanced_usingCCtxParams()
  1112. * for static allocation for single-threaded compression.
  1113. */
  1114. ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void);
  1115. ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params);
  1116. /*! ZSTD_CCtxParams_reset() :
  1117. * Reset params to default values.
  1118. */
  1119. ZSTDLIB_API size_t ZSTD_CCtxParams_reset(ZSTD_CCtx_params* params);
  1120. /*! ZSTD_CCtxParams_init() :
  1121. * Initializes the compression parameters of cctxParams according to
  1122. * compression level. All other parameters are reset to their default values.
  1123. */
  1124. ZSTDLIB_API size_t ZSTD_CCtxParams_init(ZSTD_CCtx_params* cctxParams, int compressionLevel);
  1125. /*! ZSTD_CCtxParams_init_advanced() :
  1126. * Initializes the compression and frame parameters of cctxParams according to
  1127. * params. All other parameters are reset to their default values.
  1128. */
  1129. ZSTDLIB_API size_t ZSTD_CCtxParams_init_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params);
  1130. /*! ZSTD_CCtxParam_setParameter() :
  1131. * Similar to ZSTD_CCtx_setParameter.
  1132. * Set one compression parameter, selected by enum ZSTD_cParameter.
  1133. * Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_setParametersUsingCCtxParams().
  1134. * Note : when `value` is an enum, cast it to unsigned for proper type checking.
  1135. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  1136. */
  1137. ZSTDLIB_API size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned value);
  1138. /*! ZSTD_CCtxParam_getParameter() :
  1139. * Similar to ZSTD_CCtx_getParameter.
  1140. * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter.
  1141. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  1142. */
  1143. ZSTDLIB_API size_t ZSTD_CCtxParam_getParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned* value);
  1144. /*! ZSTD_CCtx_setParametersUsingCCtxParams() :
  1145. * Apply a set of ZSTD_CCtx_params to the compression context.
  1146. * This can be done even after compression is started,
  1147. * if nbWorkers==0, this will have no impact until a new compression is started.
  1148. * if nbWorkers>=1, new parameters will be picked up at next job,
  1149. * with a few restrictions (windowLog, pledgedSrcSize, nbWorkers, jobSize, and overlapLog are not updated).
  1150. */
  1151. ZSTDLIB_API size_t ZSTD_CCtx_setParametersUsingCCtxParams(
  1152. ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params);
  1153. /* ==================================== */
  1154. /*=== Advanced decompression API ===*/
  1155. /* ==================================== */
  1156. /* The following API works the same way as the advanced compression API :
  1157. * a context is created, parameters are pushed into it one by one,
  1158. * then the context can be used to decompress data using an interface similar to the straming API.
  1159. */
  1160. /*! ZSTD_DCtx_loadDictionary() :
  1161. * Create an internal DDict from dict buffer,
  1162. * to be used to decompress next frames.
  1163. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  1164. * Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary,
  1165. * meaning "return to no-dictionary mode".
  1166. * Note 1 : `dict` content will be copied internally.
  1167. * Use ZSTD_DCtx_loadDictionary_byReference()
  1168. * to reference dictionary content instead.
  1169. * In which case, the dictionary buffer must outlive its users.
  1170. * Note 2 : Loading a dictionary involves building tables,
  1171. * which has a non-negligible impact on CPU usage and latency.
  1172. * Note 3 : Use ZSTD_DCtx_loadDictionary_advanced() to select
  1173. * how dictionary content will be interpreted and loaded.
  1174. */
  1175. ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
  1176. ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
  1177. ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType);
  1178. /*! ZSTD_DCtx_refDDict() :
  1179. * Reference a prepared dictionary, to be used to decompress next frames.
  1180. * The dictionary remains active for decompression of future frames using same DCtx.
  1181. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  1182. * Note 1 : Currently, only one dictionary can be managed.
  1183. * Referencing a new dictionary effectively "discards" any previous one.
  1184. * Special : adding a NULL DDict means "return to no-dictionary mode".
  1185. * Note 2 : DDict is just referenced, its lifetime must outlive its usage from DCtx.
  1186. */
  1187. ZSTDLIB_API size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);
  1188. /*! ZSTD_DCtx_refPrefix() :
  1189. * Reference a prefix (single-usage dictionary) for next compression job.
  1190. * This is the reverse operation of ZSTD_CCtx_refPrefix(),
  1191. * and must use the same prefix as the one used during compression.
  1192. * Prefix is **only used once**. Reference is discarded at end of frame.
  1193. * End of frame is reached when ZSTD_DCtx_decompress_generic() returns 0.
  1194. * @result : 0, or an error code (which can be tested with ZSTD_isError()).
  1195. * Note 1 : Adding any prefix (including NULL) invalidates any previously set prefix or dictionary
  1196. * Note 2 : Prefix buffer is referenced. It **must** outlive decompression job.
  1197. * Prefix buffer must remain unmodified up to the end of frame,
  1198. * reached when ZSTD_DCtx_decompress_generic() returns 0.
  1199. * Note 3 : By default, the prefix is treated as raw content (ZSTD_dm_rawContent).
  1200. * Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode.
  1201. * Note 4 : Referencing a raw content prefix has almost no cpu nor memory cost.
  1202. * A fulldict prefix is more costly though.
  1203. */
  1204. ZSTDLIB_API size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx,
  1205. const void* prefix, size_t prefixSize);
  1206. ZSTDLIB_API size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx,
  1207. const void* prefix, size_t prefixSize,
  1208. ZSTD_dictContentType_e dictContentType);
  1209. /*! ZSTD_DCtx_setMaxWindowSize() :
  1210. * Refuses allocating internal buffers for frames requiring a window size larger than provided limit.
  1211. * This is useful to prevent a decoder context from reserving too much memory for itself (potential attack scenario).
  1212. * This parameter is only useful in streaming mode, since no internal buffer is allocated in direct mode.
  1213. * By default, a decompression context accepts all window sizes <= (1 << ZSTD_WINDOWLOG_MAX)
  1214. * @return : 0, or an error code (which can be tested using ZSTD_isError()).
  1215. */
  1216. ZSTDLIB_API size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize);
  1217. /*! ZSTD_DCtx_setFormat() :
  1218. * Instruct the decoder context about what kind of data to decode next.
  1219. * This instruction is mandatory to decode data without a fully-formed header,
  1220. * such ZSTD_f_zstd1_magicless for example.
  1221. * @return : 0, or an error code (which can be tested using ZSTD_isError()).
  1222. */
  1223. ZSTDLIB_API size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format);
  1224. /*! ZSTD_getFrameHeader_advanced() :
  1225. * same as ZSTD_getFrameHeader(),
  1226. * with added capability to select a format (like ZSTD_f_zstd1_magicless) */
  1227. ZSTDLIB_API size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr,
  1228. const void* src, size_t srcSize, ZSTD_format_e format);
  1229. /*! ZSTD_decompress_generic() :
  1230. * Behave the same as ZSTD_decompressStream.
  1231. * Decompression parameters cannot be changed once decompression is started.
  1232. * @return : an error code, which can be tested using ZSTD_isError()
  1233. * if >0, a hint, nb of expected input bytes for next invocation.
  1234. * `0` means : a frame has just been fully decoded and flushed.
  1235. */
  1236. ZSTDLIB_API size_t ZSTD_decompress_generic(ZSTD_DCtx* dctx,
  1237. ZSTD_outBuffer* output,
  1238. ZSTD_inBuffer* input);
  1239. /*! ZSTD_decompress_generic_simpleArgs() :
  1240. * Same as ZSTD_decompress_generic(),
  1241. * but using only integral types as arguments.
  1242. * Argument list is larger than ZSTD_{in,out}Buffer,
  1243. * but can be helpful for binders from dynamic languages
  1244. * which have troubles handling structures containing memory pointers.
  1245. */
  1246. ZSTDLIB_API size_t ZSTD_decompress_generic_simpleArgs (
  1247. ZSTD_DCtx* dctx,
  1248. void* dst, size_t dstCapacity, size_t* dstPos,
  1249. const void* src, size_t srcSize, size_t* srcPos);
  1250. /*! ZSTD_DCtx_reset() :
  1251. * Return a DCtx to clean state.
  1252. * If a decompression was ongoing, any internal data not yet flushed is cancelled.
  1253. * All parameters are back to default values, including sticky ones.
  1254. * Dictionary (if any) is dropped.
  1255. * Parameters can be modified again after a reset.
  1256. */
  1257. ZSTDLIB_API void ZSTD_DCtx_reset(ZSTD_DCtx* dctx);
  1258. /* ============================ */
  1259. /** Block level API */
  1260. /* ============================ */
  1261. /*!
  1262. Block functions produce and decode raw zstd blocks, without frame metadata.
  1263. Frame metadata cost is typically ~18 bytes, which can be non-negligible for very small blocks (< 100 bytes).
  1264. User will have to take in charge required information to regenerate data, such as compressed and content sizes.
  1265. A few rules to respect :
  1266. - Compressing and decompressing require a context structure
  1267. + Use ZSTD_createCCtx() and ZSTD_createDCtx()
  1268. - It is necessary to init context before starting
  1269. + compression : any ZSTD_compressBegin*() variant, including with dictionary
  1270. + decompression : any ZSTD_decompressBegin*() variant, including with dictionary
  1271. + copyCCtx() and copyDCtx() can be used too
  1272. - Block size is limited, it must be <= ZSTD_getBlockSize() <= ZSTD_BLOCKSIZE_MAX == 128 KB
  1273. + If input is larger than a block size, it's necessary to split input data into multiple blocks
  1274. + For inputs larger than a single block size, consider using the regular ZSTD_compress() instead.
  1275. Frame metadata is not that costly, and quickly becomes negligible as source size grows larger.
  1276. - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero.
  1277. In which case, nothing is produced into `dst`.
  1278. + User must test for such outcome and deal directly with uncompressed data
  1279. + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!!
  1280. + In case of multiple successive blocks, should some of them be uncompressed,
  1281. decoder must be informed of their existence in order to follow proper history.
  1282. Use ZSTD_insertBlock() for such a case.
  1283. */
  1284. /*===== Raw zstd block functions =====*/
  1285. ZSTDLIB_API size_t ZSTD_getBlockSize (const ZSTD_CCtx* cctx);
  1286. ZSTDLIB_API size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  1287. ZSTDLIB_API size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
  1288. ZSTDLIB_API size_t ZSTD_insertBlock (ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize); /**< insert uncompressed block into `dctx` history. Useful for multi-blocks decompression. */
  1289. #endif /* ZSTD_H_ZSTD_STATIC_LINKING_ONLY */
  1290. #if defined (__cplusplus)
  1291. }
  1292. #endif