lz4frame.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. LZ4F - LZ4-Frame library
  3. Header File
  4. Copyright (C) 2011-2020, Yann Collet.
  5. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are
  8. met:
  9. * Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above
  12. copyright notice, this list of conditions and the following disclaimer
  13. in the documentation and/or other materials provided with the
  14. distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. You can contact the author at :
  27. - LZ4 source repository : https://github.com/lz4/lz4
  28. - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
  29. */
  30. /* LZ4F is a stand-alone API able to create and decode LZ4 frames
  31. * conformant with specification v1.6.1 in doc/lz4_Frame_format.md .
  32. * Generated frames are compatible with `lz4` CLI.
  33. *
  34. * LZ4F also offers streaming capabilities.
  35. *
  36. * lz4.h is not required when using lz4frame.h,
  37. * except to extract common constants such as LZ4_VERSION_NUMBER.
  38. * */
  39. #ifndef LZ4F_H_09782039843
  40. #define LZ4F_H_09782039843
  41. #if defined (__cplusplus)
  42. extern "C" {
  43. #endif
  44. /* --- Dependency --- */
  45. #include <stddef.h> /* size_t */
  46. /**
  47. * Introduction
  48. *
  49. * lz4frame.h implements LZ4 frame specification: see doc/lz4_Frame_format.md .
  50. * LZ4 Frames are compatible with `lz4` CLI,
  51. * and designed to be interoperable with any system.
  52. **/
  53. /*-***************************************************************
  54. * Compiler specifics
  55. *****************************************************************/
  56. /* LZ4_DLL_EXPORT :
  57. * Enable exporting of functions when building a Windows DLL
  58. * LZ4FLIB_VISIBILITY :
  59. * Control library symbols visibility.
  60. */
  61. #ifndef LZ4FLIB_VISIBILITY
  62. # if defined(__GNUC__) && (__GNUC__ >= 4)
  63. # define LZ4FLIB_VISIBILITY __attribute__ ((visibility ("default")))
  64. # else
  65. # define LZ4FLIB_VISIBILITY
  66. # endif
  67. #endif
  68. #if defined(LZ4_DLL_EXPORT) && (LZ4_DLL_EXPORT==1)
  69. # define LZ4FLIB_API __declspec(dllexport) LZ4FLIB_VISIBILITY
  70. #elif defined(LZ4_DLL_IMPORT) && (LZ4_DLL_IMPORT==1)
  71. # define LZ4FLIB_API __declspec(dllimport) LZ4FLIB_VISIBILITY
  72. #else
  73. # define LZ4FLIB_API LZ4FLIB_VISIBILITY
  74. #endif
  75. #ifdef LZ4F_DISABLE_DEPRECATE_WARNINGS
  76. # define LZ4F_DEPRECATE(x) x
  77. #else
  78. # if defined(_MSC_VER)
  79. # define LZ4F_DEPRECATE(x) x /* __declspec(deprecated) x - only works with C++ */
  80. # elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 6))
  81. # define LZ4F_DEPRECATE(x) x __attribute__((deprecated))
  82. # else
  83. # define LZ4F_DEPRECATE(x) x /* no deprecation warning for this compiler */
  84. # endif
  85. #endif
  86. /*-************************************
  87. * Error management
  88. **************************************/
  89. typedef size_t LZ4F_errorCode_t;
  90. LZ4FLIB_API unsigned LZ4F_isError(LZ4F_errorCode_t code); /**< tells when a function result is an error code */
  91. LZ4FLIB_API const char* LZ4F_getErrorName(LZ4F_errorCode_t code); /**< return error code string; for debugging */
  92. /*-************************************
  93. * Frame compression types
  94. ************************************* */
  95. /* #define LZ4F_ENABLE_OBSOLETE_ENUMS // uncomment to enable obsolete enums */
  96. #ifdef LZ4F_ENABLE_OBSOLETE_ENUMS
  97. # define LZ4F_OBSOLETE_ENUM(x) , LZ4F_DEPRECATE(x) = LZ4F_##x
  98. #else
  99. # define LZ4F_OBSOLETE_ENUM(x)
  100. #endif
  101. /* The larger the block size, the (slightly) better the compression ratio,
  102. * though there are diminishing returns.
  103. * Larger blocks also increase memory usage on both compression and decompression sides.
  104. */
  105. typedef enum {
  106. LZ4F_default=0,
  107. LZ4F_max64KB=4,
  108. LZ4F_max256KB=5,
  109. LZ4F_max1MB=6,
  110. LZ4F_max4MB=7
  111. LZ4F_OBSOLETE_ENUM(max64KB)
  112. LZ4F_OBSOLETE_ENUM(max256KB)
  113. LZ4F_OBSOLETE_ENUM(max1MB)
  114. LZ4F_OBSOLETE_ENUM(max4MB)
  115. } LZ4F_blockSizeID_t;
  116. /* Linked blocks sharply reduce inefficiencies when using small blocks,
  117. * they compress better.
  118. * However, some LZ4 decoders are only compatible with independent blocks */
  119. typedef enum {
  120. LZ4F_blockLinked=0,
  121. LZ4F_blockIndependent
  122. LZ4F_OBSOLETE_ENUM(blockLinked)
  123. LZ4F_OBSOLETE_ENUM(blockIndependent)
  124. } LZ4F_blockMode_t;
  125. typedef enum {
  126. LZ4F_noContentChecksum=0,
  127. LZ4F_contentChecksumEnabled
  128. LZ4F_OBSOLETE_ENUM(noContentChecksum)
  129. LZ4F_OBSOLETE_ENUM(contentChecksumEnabled)
  130. } LZ4F_contentChecksum_t;
  131. typedef enum {
  132. LZ4F_noBlockChecksum=0,
  133. LZ4F_blockChecksumEnabled
  134. } LZ4F_blockChecksum_t;
  135. typedef enum {
  136. LZ4F_frame=0,
  137. LZ4F_skippableFrame
  138. LZ4F_OBSOLETE_ENUM(skippableFrame)
  139. } LZ4F_frameType_t;
  140. #ifdef LZ4F_ENABLE_OBSOLETE_ENUMS
  141. typedef LZ4F_blockSizeID_t blockSizeID_t;
  142. typedef LZ4F_blockMode_t blockMode_t;
  143. typedef LZ4F_frameType_t frameType_t;
  144. typedef LZ4F_contentChecksum_t contentChecksum_t;
  145. #endif
  146. /*! LZ4F_frameInfo_t :
  147. * makes it possible to set or read frame parameters.
  148. * Structure must be first init to 0, using memset() or LZ4F_INIT_FRAMEINFO,
  149. * setting all parameters to default.
  150. * It's then possible to update selectively some parameters */
  151. typedef struct {
  152. LZ4F_blockSizeID_t blockSizeID; /* max64KB, max256KB, max1MB, max4MB; 0 == default */
  153. LZ4F_blockMode_t blockMode; /* LZ4F_blockLinked, LZ4F_blockIndependent; 0 == default */
  154. LZ4F_contentChecksum_t contentChecksumFlag; /* 1: frame terminated with 32-bit checksum of decompressed data; 0: disabled (default) */
  155. LZ4F_frameType_t frameType; /* read-only field : LZ4F_frame or LZ4F_skippableFrame */
  156. unsigned long long contentSize; /* Size of uncompressed content ; 0 == unknown */
  157. unsigned dictID; /* Dictionary ID, sent by compressor to help decoder select correct dictionary; 0 == no dictID provided */
  158. LZ4F_blockChecksum_t blockChecksumFlag; /* 1: each block followed by a checksum of block's compressed data; 0: disabled (default) */
  159. } LZ4F_frameInfo_t;
  160. #define LZ4F_INIT_FRAMEINFO { LZ4F_default, LZ4F_blockLinked, LZ4F_noContentChecksum, LZ4F_frame, 0ULL, 0U, LZ4F_noBlockChecksum } /* v1.8.3+ */
  161. /*! LZ4F_preferences_t :
  162. * makes it possible to supply advanced compression instructions to streaming interface.
  163. * Structure must be first init to 0, using memset() or LZ4F_INIT_PREFERENCES,
  164. * setting all parameters to default.
  165. * All reserved fields must be set to zero. */
  166. typedef struct {
  167. LZ4F_frameInfo_t frameInfo;
  168. int compressionLevel; /* 0: default (fast mode); values > LZ4HC_CLEVEL_MAX count as LZ4HC_CLEVEL_MAX; values < 0 trigger "fast acceleration" */
  169. unsigned autoFlush; /* 1: always flush; reduces usage of internal buffers */
  170. unsigned favorDecSpeed; /* 1: parser favors decompression speed vs compression ratio. Only works for high compression modes (>= LZ4HC_CLEVEL_OPT_MIN) */ /* v1.8.2+ */
  171. unsigned reserved[3]; /* must be zero for forward compatibility */
  172. } LZ4F_preferences_t;
  173. #define LZ4F_INIT_PREFERENCES { LZ4F_INIT_FRAMEINFO, 0, 0u, 0u, { 0u, 0u, 0u } } /* v1.8.3+ */
  174. /*-*********************************
  175. * Simple compression function
  176. ***********************************/
  177. LZ4FLIB_API int LZ4F_compressionLevel_max(void); /* v1.8.0+ */
  178. /*! LZ4F_compressFrameBound() :
  179. * Returns the maximum possible compressed size with LZ4F_compressFrame() given srcSize and preferences.
  180. * `preferencesPtr` is optional. It can be replaced by NULL, in which case, the function will assume default preferences.
  181. * Note : this result is only usable with LZ4F_compressFrame().
  182. * It may also be relevant to LZ4F_compressUpdate() _only if_ no flush() operation is ever performed.
  183. */
  184. LZ4FLIB_API size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr);
  185. /*! LZ4F_compressFrame() :
  186. * Compress an entire srcBuffer into a valid LZ4 frame.
  187. * dstCapacity MUST be >= LZ4F_compressFrameBound(srcSize, preferencesPtr).
  188. * The LZ4F_preferences_t structure is optional : you can provide NULL as argument. All preferences will be set to default.
  189. * @return : number of bytes written into dstBuffer.
  190. * or an error code if it fails (can be tested using LZ4F_isError())
  191. */
  192. LZ4FLIB_API size_t LZ4F_compressFrame(void* dstBuffer, size_t dstCapacity,
  193. const void* srcBuffer, size_t srcSize,
  194. const LZ4F_preferences_t* preferencesPtr);
  195. /*-***********************************
  196. * Advanced compression functions
  197. *************************************/
  198. typedef struct LZ4F_cctx_s LZ4F_cctx; /* incomplete type */
  199. typedef LZ4F_cctx* LZ4F_compressionContext_t; /* for compatibility with older APIs, prefer using LZ4F_cctx */
  200. typedef struct {
  201. unsigned stableSrc; /* 1 == src content will remain present on future calls to LZ4F_compress(); skip copying src content within tmp buffer */
  202. unsigned reserved[3];
  203. } LZ4F_compressOptions_t;
  204. /*--- Resource Management ---*/
  205. #define LZ4F_VERSION 100 /* This number can be used to check for an incompatible API breaking change */
  206. LZ4FLIB_API unsigned LZ4F_getVersion(void);
  207. /*! LZ4F_createCompressionContext() :
  208. * The first thing to do is to create a compressionContext object,
  209. * which will keep track of operation state during streaming compression.
  210. * This is achieved using LZ4F_createCompressionContext(), which takes as argument a version,
  211. * and a pointer to LZ4F_cctx*, to write the resulting pointer into.
  212. * @version provided MUST be LZ4F_VERSION. It is intended to track potential version mismatch, notably when using DLL.
  213. * The function provides a pointer to a fully allocated LZ4F_cctx object.
  214. * @cctxPtr MUST be != NULL.
  215. * If @return != zero, context creation failed.
  216. * A created compression context can be employed multiple times for consecutive streaming operations.
  217. * Once all streaming compression jobs are completed,
  218. * the state object can be released using LZ4F_freeCompressionContext().
  219. * Note1 : LZ4F_freeCompressionContext() is always successful. Its return value can be ignored.
  220. * Note2 : LZ4F_freeCompressionContext() works fine with NULL input pointers (do nothing).
  221. **/
  222. LZ4FLIB_API LZ4F_errorCode_t LZ4F_createCompressionContext(LZ4F_cctx** cctxPtr, unsigned version);
  223. LZ4FLIB_API LZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctx);
  224. /*---- Compression ----*/
  225. #define LZ4F_HEADER_SIZE_MIN 7 /* LZ4 Frame header size can vary, depending on selected parameters */
  226. #define LZ4F_HEADER_SIZE_MAX 19
  227. /* Size in bytes of a block header in little-endian format. Highest bit indicates if block data is uncompressed */
  228. #define LZ4F_BLOCK_HEADER_SIZE 4
  229. /* Size in bytes of a block checksum footer in little-endian format. */
  230. #define LZ4F_BLOCK_CHECKSUM_SIZE 4
  231. /* Size in bytes of the content checksum. */
  232. #define LZ4F_CONTENT_CHECKSUM_SIZE 4
  233. /*! LZ4F_compressBegin() :
  234. * will write the frame header into dstBuffer.
  235. * dstCapacity must be >= LZ4F_HEADER_SIZE_MAX bytes.
  236. * `prefsPtr` is optional : you can provide NULL as argument, all preferences will then be set to default.
  237. * @return : number of bytes written into dstBuffer for the header
  238. * or an error code (which can be tested using LZ4F_isError())
  239. */
  240. LZ4FLIB_API size_t LZ4F_compressBegin(LZ4F_cctx* cctx,
  241. void* dstBuffer, size_t dstCapacity,
  242. const LZ4F_preferences_t* prefsPtr);
  243. /*! LZ4F_compressBound() :
  244. * Provides minimum dstCapacity required to guarantee success of
  245. * LZ4F_compressUpdate(), given a srcSize and preferences, for a worst case scenario.
  246. * When srcSize==0, LZ4F_compressBound() provides an upper bound for LZ4F_flush() and LZ4F_compressEnd() instead.
  247. * Note that the result is only valid for a single invocation of LZ4F_compressUpdate().
  248. * When invoking LZ4F_compressUpdate() multiple times,
  249. * if the output buffer is gradually filled up instead of emptied and re-used from its start,
  250. * one must check if there is enough remaining capacity before each invocation, using LZ4F_compressBound().
  251. * @return is always the same for a srcSize and prefsPtr.
  252. * prefsPtr is optional : when NULL is provided, preferences will be set to cover worst case scenario.
  253. * tech details :
  254. * @return if automatic flushing is not enabled, includes the possibility that internal buffer might already be filled by up to (blockSize-1) bytes.
  255. * It also includes frame footer (ending + checksum), since it might be generated by LZ4F_compressEnd().
  256. * @return doesn't include frame header, as it was already generated by LZ4F_compressBegin().
  257. */
  258. LZ4FLIB_API size_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* prefsPtr);
  259. /*! LZ4F_compressUpdate() :
  260. * LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary.
  261. * Important rule: dstCapacity MUST be large enough to ensure operation success even in worst case situations.
  262. * This value is provided by LZ4F_compressBound().
  263. * If this condition is not respected, LZ4F_compress() will fail (result is an errorCode).
  264. * After an error, the state is left in a UB state, and must be re-initialized or freed.
  265. * If previously an uncompressed block was written, buffered data is flushed
  266. * before appending compressed data is continued.
  267. * `cOptPtr` is optional : NULL can be provided, in which case all options are set to default.
  268. * @return : number of bytes written into `dstBuffer` (it can be zero, meaning input data was just buffered).
  269. * or an error code if it fails (which can be tested using LZ4F_isError())
  270. */
  271. LZ4FLIB_API size_t LZ4F_compressUpdate(LZ4F_cctx* cctx,
  272. void* dstBuffer, size_t dstCapacity,
  273. const void* srcBuffer, size_t srcSize,
  274. const LZ4F_compressOptions_t* cOptPtr);
  275. /*! LZ4F_flush() :
  276. * When data must be generated and sent immediately, without waiting for a block to be completely filled,
  277. * it's possible to call LZ4_flush(). It will immediately compress any data buffered within cctx.
  278. * `dstCapacity` must be large enough to ensure the operation will be successful.
  279. * `cOptPtr` is optional : it's possible to provide NULL, all options will be set to default.
  280. * @return : nb of bytes written into dstBuffer (can be zero, when there is no data stored within cctx)
  281. * or an error code if it fails (which can be tested using LZ4F_isError())
  282. * Note : LZ4F_flush() is guaranteed to be successful when dstCapacity >= LZ4F_compressBound(0, prefsPtr).
  283. */
  284. LZ4FLIB_API size_t LZ4F_flush(LZ4F_cctx* cctx,
  285. void* dstBuffer, size_t dstCapacity,
  286. const LZ4F_compressOptions_t* cOptPtr);
  287. /*! LZ4F_compressEnd() :
  288. * To properly finish an LZ4 frame, invoke LZ4F_compressEnd().
  289. * It will flush whatever data remained within `cctx` (like LZ4_flush())
  290. * and properly finalize the frame, with an endMark and a checksum.
  291. * `cOptPtr` is optional : NULL can be provided, in which case all options will be set to default.
  292. * @return : nb of bytes written into dstBuffer, necessarily >= 4 (endMark),
  293. * or an error code if it fails (which can be tested using LZ4F_isError())
  294. * Note : LZ4F_compressEnd() is guaranteed to be successful when dstCapacity >= LZ4F_compressBound(0, prefsPtr).
  295. * A successful call to LZ4F_compressEnd() makes `cctx` available again for another compression task.
  296. */
  297. LZ4FLIB_API size_t LZ4F_compressEnd(LZ4F_cctx* cctx,
  298. void* dstBuffer, size_t dstCapacity,
  299. const LZ4F_compressOptions_t* cOptPtr);
  300. /*-*********************************
  301. * Decompression functions
  302. ***********************************/
  303. typedef struct LZ4F_dctx_s LZ4F_dctx; /* incomplete type */
  304. typedef LZ4F_dctx* LZ4F_decompressionContext_t; /* compatibility with previous API versions */
  305. typedef struct {
  306. unsigned stableDst; /* pledges that last 64KB decompressed data will remain available unmodified between invocations.
  307. * This optimization skips storage operations in tmp buffers. */
  308. unsigned skipChecksums; /* disable checksum calculation and verification, even when one is present in frame, to save CPU time.
  309. * Setting this option to 1 once disables all checksums for the rest of the frame. */
  310. unsigned reserved1; /* must be set to zero for forward compatibility */
  311. unsigned reserved0; /* idem */
  312. } LZ4F_decompressOptions_t;
  313. /* Resource management */
  314. /*! LZ4F_createDecompressionContext() :
  315. * Create an LZ4F_dctx object, to track all decompression operations.
  316. * @version provided MUST be LZ4F_VERSION.
  317. * @dctxPtr MUST be valid.
  318. * The function fills @dctxPtr with the value of a pointer to an allocated and initialized LZ4F_dctx object.
  319. * The @return is an errorCode, which can be tested using LZ4F_isError().
  320. * dctx memory can be released using LZ4F_freeDecompressionContext();
  321. * Result of LZ4F_freeDecompressionContext() indicates current state of decompressionContext when being released.
  322. * That is, it should be == 0 if decompression has been completed fully and correctly.
  323. */
  324. LZ4FLIB_API LZ4F_errorCode_t LZ4F_createDecompressionContext(LZ4F_dctx** dctxPtr, unsigned version);
  325. LZ4FLIB_API LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx);
  326. /*-***********************************
  327. * Streaming decompression functions
  328. *************************************/
  329. #define LZ4F_MAGICNUMBER 0x184D2204U
  330. #define LZ4F_MAGIC_SKIPPABLE_START 0x184D2A50U
  331. #define LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH 5
  332. /*! LZ4F_headerSize() : v1.9.0+
  333. * Provide the header size of a frame starting at `src`.
  334. * `srcSize` must be >= LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH,
  335. * which is enough to decode the header length.
  336. * @return : size of frame header
  337. * or an error code, which can be tested using LZ4F_isError()
  338. * note : Frame header size is variable, but is guaranteed to be
  339. * >= LZ4F_HEADER_SIZE_MIN bytes, and <= LZ4F_HEADER_SIZE_MAX bytes.
  340. */
  341. LZ4FLIB_API size_t LZ4F_headerSize(const void* src, size_t srcSize);
  342. /*! LZ4F_getFrameInfo() :
  343. * This function extracts frame parameters (max blockSize, dictID, etc.).
  344. * Its usage is optional: user can also invoke LZ4F_decompress() directly.
  345. *
  346. * Extracted information will fill an existing LZ4F_frameInfo_t structure.
  347. * This can be useful for allocation and dictionary identification purposes.
  348. *
  349. * LZ4F_getFrameInfo() can work in the following situations :
  350. *
  351. * 1) At the beginning of a new frame, before any invocation of LZ4F_decompress().
  352. * It will decode header from `srcBuffer`,
  353. * consuming the header and starting the decoding process.
  354. *
  355. * Input size must be large enough to contain the full frame header.
  356. * Frame header size can be known beforehand by LZ4F_headerSize().
  357. * Frame header size is variable, but is guaranteed to be >= LZ4F_HEADER_SIZE_MIN bytes,
  358. * and not more than <= LZ4F_HEADER_SIZE_MAX bytes.
  359. * Hence, blindly providing LZ4F_HEADER_SIZE_MAX bytes or more will always work.
  360. * It's allowed to provide more input data than the header size,
  361. * LZ4F_getFrameInfo() will only consume the header.
  362. *
  363. * If input size is not large enough,
  364. * aka if it's smaller than header size,
  365. * function will fail and return an error code.
  366. *
  367. * 2) After decoding has been started,
  368. * it's possible to invoke LZ4F_getFrameInfo() anytime
  369. * to extract already decoded frame parameters stored within dctx.
  370. *
  371. * Note that, if decoding has barely started,
  372. * and not yet read enough information to decode the header,
  373. * LZ4F_getFrameInfo() will fail.
  374. *
  375. * The number of bytes consumed from srcBuffer will be updated in *srcSizePtr (necessarily <= original value).
  376. * LZ4F_getFrameInfo() only consumes bytes when decoding has not yet started,
  377. * and when decoding the header has been successful.
  378. * Decompression must then resume from (srcBuffer + *srcSizePtr).
  379. *
  380. * @return : a hint about how many srcSize bytes LZ4F_decompress() expects for next call,
  381. * or an error code which can be tested using LZ4F_isError().
  382. * note 1 : in case of error, dctx is not modified. Decoding operation can resume from beginning safely.
  383. * note 2 : frame parameters are *copied into* an already allocated LZ4F_frameInfo_t structure.
  384. */
  385. LZ4FLIB_API size_t
  386. LZ4F_getFrameInfo(LZ4F_dctx* dctx,
  387. LZ4F_frameInfo_t* frameInfoPtr,
  388. const void* srcBuffer, size_t* srcSizePtr);
  389. /*! LZ4F_decompress() :
  390. * Call this function repetitively to regenerate data compressed in `srcBuffer`.
  391. *
  392. * The function requires a valid dctx state.
  393. * It will read up to *srcSizePtr bytes from srcBuffer,
  394. * and decompress data into dstBuffer, of capacity *dstSizePtr.
  395. *
  396. * The nb of bytes consumed from srcBuffer will be written into *srcSizePtr (necessarily <= original value).
  397. * The nb of bytes decompressed into dstBuffer will be written into *dstSizePtr (necessarily <= original value).
  398. *
  399. * The function does not necessarily read all input bytes, so always check value in *srcSizePtr.
  400. * Unconsumed source data must be presented again in subsequent invocations.
  401. *
  402. * `dstBuffer` can freely change between each consecutive function invocation.
  403. * `dstBuffer` content will be overwritten.
  404. *
  405. * @return : an hint of how many `srcSize` bytes LZ4F_decompress() expects for next call.
  406. * Schematically, it's the size of the current (or remaining) compressed block + header of next block.
  407. * Respecting the hint provides some small speed benefit, because it skips intermediate buffers.
  408. * This is just a hint though, it's always possible to provide any srcSize.
  409. *
  410. * When a frame is fully decoded, @return will be 0 (no more data expected).
  411. * When provided with more bytes than necessary to decode a frame,
  412. * LZ4F_decompress() will stop reading exactly at end of current frame, and @return 0.
  413. *
  414. * If decompression failed, @return is an error code, which can be tested using LZ4F_isError().
  415. * After a decompression error, the `dctx` context is not resumable.
  416. * Use LZ4F_resetDecompressionContext() to return to clean state.
  417. *
  418. * After a frame is fully decoded, dctx can be used again to decompress another frame.
  419. */
  420. LZ4FLIB_API size_t
  421. LZ4F_decompress(LZ4F_dctx* dctx,
  422. void* dstBuffer, size_t* dstSizePtr,
  423. const void* srcBuffer, size_t* srcSizePtr,
  424. const LZ4F_decompressOptions_t* dOptPtr);
  425. /*! LZ4F_resetDecompressionContext() : added in v1.8.0
  426. * In case of an error, the context is left in "undefined" state.
  427. * In which case, it's necessary to reset it, before re-using it.
  428. * This method can also be used to abruptly stop any unfinished decompression,
  429. * and start a new one using same context resources. */
  430. LZ4FLIB_API void LZ4F_resetDecompressionContext(LZ4F_dctx* dctx); /* always successful */
  431. #if defined (__cplusplus)
  432. }
  433. #endif
  434. #endif /* LZ4F_H_09782039843 */
  435. #if defined(LZ4F_STATIC_LINKING_ONLY) && !defined(LZ4F_H_STATIC_09782039843)
  436. #define LZ4F_H_STATIC_09782039843
  437. #if defined (__cplusplus)
  438. extern "C" {
  439. #endif
  440. /* These declarations are not stable and may change in the future.
  441. * They are therefore only safe to depend on
  442. * when the caller is statically linked against the library.
  443. * To access their declarations, define LZ4F_STATIC_LINKING_ONLY.
  444. *
  445. * By default, these symbols aren't published into shared/dynamic libraries.
  446. * You can override this behavior and force them to be published
  447. * by defining LZ4F_PUBLISH_STATIC_FUNCTIONS.
  448. * Use at your own risk.
  449. */
  450. #ifdef LZ4F_PUBLISH_STATIC_FUNCTIONS
  451. # define LZ4FLIB_STATIC_API LZ4FLIB_API
  452. #else
  453. # define LZ4FLIB_STATIC_API
  454. #endif
  455. /* --- Error List --- */
  456. #define LZ4F_LIST_ERRORS(ITEM) \
  457. ITEM(OK_NoError) \
  458. ITEM(ERROR_GENERIC) \
  459. ITEM(ERROR_maxBlockSize_invalid) \
  460. ITEM(ERROR_blockMode_invalid) \
  461. ITEM(ERROR_contentChecksumFlag_invalid) \
  462. ITEM(ERROR_compressionLevel_invalid) \
  463. ITEM(ERROR_headerVersion_wrong) \
  464. ITEM(ERROR_blockChecksum_invalid) \
  465. ITEM(ERROR_reservedFlag_set) \
  466. ITEM(ERROR_allocation_failed) \
  467. ITEM(ERROR_srcSize_tooLarge) \
  468. ITEM(ERROR_dstMaxSize_tooSmall) \
  469. ITEM(ERROR_frameHeader_incomplete) \
  470. ITEM(ERROR_frameType_unknown) \
  471. ITEM(ERROR_frameSize_wrong) \
  472. ITEM(ERROR_srcPtr_wrong) \
  473. ITEM(ERROR_decompressionFailed) \
  474. ITEM(ERROR_headerChecksum_invalid) \
  475. ITEM(ERROR_contentChecksum_invalid) \
  476. ITEM(ERROR_frameDecoding_alreadyStarted) \
  477. ITEM(ERROR_compressionState_uninitialized) \
  478. ITEM(ERROR_parameter_null) \
  479. ITEM(ERROR_maxCode)
  480. #define LZ4F_GENERATE_ENUM(ENUM) LZ4F_##ENUM,
  481. /* enum list is exposed, to handle specific errors */
  482. typedef enum { LZ4F_LIST_ERRORS(LZ4F_GENERATE_ENUM)
  483. _LZ4F_dummy_error_enum_for_c89_never_used } LZ4F_errorCodes;
  484. LZ4FLIB_STATIC_API LZ4F_errorCodes LZ4F_getErrorCode(size_t functionResult);
  485. /*! LZ4F_getBlockSize() :
  486. * Return, in scalar format (size_t),
  487. * the maximum block size associated with blockSizeID.
  488. **/
  489. LZ4FLIB_STATIC_API size_t LZ4F_getBlockSize(LZ4F_blockSizeID_t blockSizeID);
  490. /*! LZ4F_uncompressedUpdate() :
  491. * LZ4F_uncompressedUpdate() can be called repetitively to add as much data uncompressed data as necessary.
  492. * Important rule: dstCapacity MUST be large enough to store the entire source buffer as
  493. * no compression is done for this operation
  494. * If this condition is not respected, LZ4F_uncompressedUpdate() will fail (result is an errorCode).
  495. * After an error, the state is left in a UB state, and must be re-initialized or freed.
  496. * If previously a compressed block was written, buffered data is flushed
  497. * before appending uncompressed data is continued.
  498. * This is only supported when LZ4F_blockIndependent is used
  499. * `cOptPtr` is optional : NULL can be provided, in which case all options are set to default.
  500. * @return : number of bytes written into `dstBuffer` (it can be zero, meaning input data was just buffered).
  501. * or an error code if it fails (which can be tested using LZ4F_isError())
  502. */
  503. LZ4FLIB_STATIC_API size_t
  504. LZ4F_uncompressedUpdate(LZ4F_cctx* cctx,
  505. void* dstBuffer, size_t dstCapacity,
  506. const void* srcBuffer, size_t srcSize,
  507. const LZ4F_compressOptions_t* cOptPtr);
  508. /**********************************
  509. * Bulk processing dictionary API
  510. *********************************/
  511. /* A Dictionary is useful for the compression of small messages (KB range).
  512. * It dramatically improves compression efficiency.
  513. *
  514. * LZ4 can ingest any input as dictionary, though only the last 64 KB are useful.
  515. * Best results are generally achieved by using Zstandard's Dictionary Builder
  516. * to generate a high-quality dictionary from a set of samples.
  517. *
  518. * Loading a dictionary has a cost, since it involves construction of tables.
  519. * The Bulk processing dictionary API makes it possible to share this cost
  520. * over an arbitrary number of compression jobs, even concurrently,
  521. * markedly improving compression latency for these cases.
  522. *
  523. * The same dictionary will have to be used on the decompression side
  524. * for decoding to be successful.
  525. * To help identify the correct dictionary at decoding stage,
  526. * the frame header allows optional embedding of a dictID field.
  527. */
  528. typedef struct LZ4F_CDict_s LZ4F_CDict;
  529. /*! LZ4_createCDict() :
  530. * When compressing multiple messages / blocks using the same dictionary, it's recommended to load it just once.
  531. * LZ4_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay.
  532. * LZ4_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only.
  533. * `dictBuffer` can be released after LZ4_CDict creation, since its content is copied within CDict */
  534. LZ4FLIB_STATIC_API LZ4F_CDict* LZ4F_createCDict(const void* dictBuffer, size_t dictSize);
  535. LZ4FLIB_STATIC_API void LZ4F_freeCDict(LZ4F_CDict* CDict);
  536. /*! LZ4_compressFrame_usingCDict() :
  537. * Compress an entire srcBuffer into a valid LZ4 frame using a digested Dictionary.
  538. * cctx must point to a context created by LZ4F_createCompressionContext().
  539. * If cdict==NULL, compress without a dictionary.
  540. * dstBuffer MUST be >= LZ4F_compressFrameBound(srcSize, preferencesPtr).
  541. * If this condition is not respected, function will fail (@return an errorCode).
  542. * The LZ4F_preferences_t structure is optional : you may provide NULL as argument,
  543. * but it's not recommended, as it's the only way to provide dictID in the frame header.
  544. * @return : number of bytes written into dstBuffer.
  545. * or an error code if it fails (can be tested using LZ4F_isError()) */
  546. LZ4FLIB_STATIC_API size_t
  547. LZ4F_compressFrame_usingCDict(LZ4F_cctx* cctx,
  548. void* dst, size_t dstCapacity,
  549. const void* src, size_t srcSize,
  550. const LZ4F_CDict* cdict,
  551. const LZ4F_preferences_t* preferencesPtr);
  552. /*! LZ4F_compressBegin_usingCDict() :
  553. * Inits streaming dictionary compression, and writes the frame header into dstBuffer.
  554. * dstCapacity must be >= LZ4F_HEADER_SIZE_MAX bytes.
  555. * `prefsPtr` is optional : you may provide NULL as argument,
  556. * however, it's the only way to provide dictID in the frame header.
  557. * @return : number of bytes written into dstBuffer for the header,
  558. * or an error code (which can be tested using LZ4F_isError()) */
  559. LZ4FLIB_STATIC_API size_t
  560. LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctx,
  561. void* dstBuffer, size_t dstCapacity,
  562. const LZ4F_CDict* cdict,
  563. const LZ4F_preferences_t* prefsPtr);
  564. /*! LZ4F_decompress_usingDict() :
  565. * Same as LZ4F_decompress(), using a predefined dictionary.
  566. * Dictionary is used "in place", without any preprocessing.
  567. ** It must remain accessible throughout the entire frame decoding. */
  568. LZ4FLIB_STATIC_API size_t
  569. LZ4F_decompress_usingDict(LZ4F_dctx* dctxPtr,
  570. void* dstBuffer, size_t* dstSizePtr,
  571. const void* srcBuffer, size_t* srcSizePtr,
  572. const void* dict, size_t dictSize,
  573. const LZ4F_decompressOptions_t* decompressOptionsPtr);
  574. /*! Custom memory allocation :
  575. * These prototypes make it possible to pass custom allocation/free functions.
  576. * LZ4F_customMem is provided at state creation time, using LZ4F_create*_advanced() listed below.
  577. * All allocation/free operations will be completed using these custom variants instead of regular <stdlib.h> ones.
  578. */
  579. typedef void* (*LZ4F_AllocFunction) (void* opaqueState, size_t size);
  580. typedef void* (*LZ4F_CallocFunction) (void* opaqueState, size_t size);
  581. typedef void (*LZ4F_FreeFunction) (void* opaqueState, void* address);
  582. typedef struct {
  583. LZ4F_AllocFunction customAlloc;
  584. LZ4F_CallocFunction customCalloc; /* optional; when not defined, uses customAlloc + memset */
  585. LZ4F_FreeFunction customFree;
  586. void* opaqueState;
  587. } LZ4F_CustomMem;
  588. static
  589. #ifdef __GNUC__
  590. __attribute__((__unused__))
  591. #endif
  592. LZ4F_CustomMem const LZ4F_defaultCMem = { NULL, NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */
  593. LZ4FLIB_STATIC_API LZ4F_cctx* LZ4F_createCompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version);
  594. LZ4FLIB_STATIC_API LZ4F_dctx* LZ4F_createDecompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version);
  595. LZ4FLIB_STATIC_API LZ4F_CDict* LZ4F_createCDict_advanced(LZ4F_CustomMem customMem, const void* dictBuffer, size_t dictSize);
  596. #if defined (__cplusplus)
  597. }
  598. #endif
  599. #endif /* defined(LZ4F_STATIC_LINKING_ONLY) && !defined(LZ4F_H_STATIC_09782039843) */