zstd.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
  2. /*
  3. * Copyright (c) Yann Collet, Facebook, Inc.
  4. * All rights reserved.
  5. *
  6. * This source code is licensed under both the BSD-style license (found in the
  7. * LICENSE file in the root directory of https://github.com/facebook/zstd) and
  8. * the GPLv2 (found in the COPYING file in the root directory of
  9. * https://github.com/facebook/zstd). You may select, at your option, one of the
  10. * above-listed licenses.
  11. */
  12. #ifndef LINUX_ZSTD_H
  13. #define LINUX_ZSTD_H
  14. /**
  15. * This is a kernel-style API that wraps the upstream zstd API, which cannot be
  16. * used directly because the symbols aren't exported. It exposes the minimal
  17. * functionality which is currently required by users of zstd in the kernel.
  18. * Expose extra functions from lib/zstd/zstd.h as needed.
  19. */
  20. /* ====== Dependency ====== */
  21. #include <linux/types.h>
  22. #include <linux/zstd_errors.h>
  23. #include <linux/zstd_lib.h>
  24. /* ====== Helper Functions ====== */
  25. /**
  26. * zstd_compress_bound() - maximum compressed size in worst case scenario
  27. * @src_size: The size of the data to compress.
  28. *
  29. * Return: The maximum compressed size in the worst case scenario.
  30. */
  31. size_t zstd_compress_bound(size_t src_size);
  32. /**
  33. * zstd_is_error() - tells if a size_t function result is an error code
  34. * @code: The function result to check for error.
  35. *
  36. * Return: Non-zero iff the code is an error.
  37. */
  38. unsigned int zstd_is_error(size_t code);
  39. /**
  40. * enum zstd_error_code - zstd error codes
  41. */
  42. typedef ZSTD_ErrorCode zstd_error_code;
  43. /**
  44. * zstd_get_error_code() - translates an error function result to an error code
  45. * @code: The function result for which zstd_is_error(code) is true.
  46. *
  47. * Return: A unique error code for this error.
  48. */
  49. zstd_error_code zstd_get_error_code(size_t code);
  50. /**
  51. * zstd_get_error_name() - translates an error function result to a string
  52. * @code: The function result for which zstd_is_error(code) is true.
  53. *
  54. * Return: An error string corresponding to the error code.
  55. */
  56. const char *zstd_get_error_name(size_t code);
  57. /**
  58. * zstd_min_clevel() - minimum allowed compression level
  59. *
  60. * Return: The minimum allowed compression level.
  61. */
  62. int zstd_min_clevel(void);
  63. /**
  64. * zstd_max_clevel() - maximum allowed compression level
  65. *
  66. * Return: The maximum allowed compression level.
  67. */
  68. int zstd_max_clevel(void);
  69. /* ====== Parameter Selection ====== */
  70. /**
  71. * enum zstd_strategy - zstd compression search strategy
  72. *
  73. * From faster to stronger. See zstd_lib.h.
  74. */
  75. typedef ZSTD_strategy zstd_strategy;
  76. /**
  77. * struct zstd_compression_parameters - zstd compression parameters
  78. * @windowLog: Log of the largest match distance. Larger means more
  79. * compression, and more memory needed during decompression.
  80. * @chainLog: Fully searched segment. Larger means more compression,
  81. * slower, and more memory (useless for fast).
  82. * @hashLog: Dispatch table. Larger means more compression,
  83. * slower, and more memory.
  84. * @searchLog: Number of searches. Larger means more compression and slower.
  85. * @searchLength: Match length searched. Larger means faster decompression,
  86. * sometimes less compression.
  87. * @targetLength: Acceptable match size for optimal parser (only). Larger means
  88. * more compression, and slower.
  89. * @strategy: The zstd compression strategy.
  90. *
  91. * See zstd_lib.h.
  92. */
  93. typedef ZSTD_compressionParameters zstd_compression_parameters;
  94. /**
  95. * struct zstd_frame_parameters - zstd frame parameters
  96. * @contentSizeFlag: Controls whether content size will be present in the
  97. * frame header (when known).
  98. * @checksumFlag: Controls whether a 32-bit checksum is generated at the
  99. * end of the frame for error detection.
  100. * @noDictIDFlag: Controls whether dictID will be saved into the frame
  101. * header when using dictionary compression.
  102. *
  103. * The default value is all fields set to 0. See zstd_lib.h.
  104. */
  105. typedef ZSTD_frameParameters zstd_frame_parameters;
  106. /**
  107. * struct zstd_parameters - zstd parameters
  108. * @cParams: The compression parameters.
  109. * @fParams: The frame parameters.
  110. */
  111. typedef ZSTD_parameters zstd_parameters;
  112. /**
  113. * zstd_get_params() - returns zstd_parameters for selected level
  114. * @level: The compression level
  115. * @estimated_src_size: The estimated source size to compress or 0
  116. * if unknown.
  117. *
  118. * Return: The selected zstd_parameters.
  119. */
  120. zstd_parameters zstd_get_params(int level,
  121. unsigned long long estimated_src_size);
  122. /* ====== Single-pass Compression ====== */
  123. typedef ZSTD_CCtx zstd_cctx;
  124. /**
  125. * zstd_cctx_workspace_bound() - max memory needed to initialize a zstd_cctx
  126. * @parameters: The compression parameters to be used.
  127. *
  128. * If multiple compression parameters might be used, the caller must call
  129. * zstd_cctx_workspace_bound() for each set of parameters and use the maximum
  130. * size.
  131. *
  132. * Return: A lower bound on the size of the workspace that is passed to
  133. * zstd_init_cctx().
  134. */
  135. size_t zstd_cctx_workspace_bound(const zstd_compression_parameters *parameters);
  136. /**
  137. * zstd_init_cctx() - initialize a zstd compression context
  138. * @workspace: The workspace to emplace the context into. It must outlive
  139. * the returned context.
  140. * @workspace_size: The size of workspace. Use zstd_cctx_workspace_bound() to
  141. * determine how large the workspace must be.
  142. *
  143. * Return: A zstd compression context or NULL on error.
  144. */
  145. zstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size);
  146. /**
  147. * zstd_compress_cctx() - compress src into dst with the initialized parameters
  148. * @cctx: The context. Must have been initialized with zstd_init_cctx().
  149. * @dst: The buffer to compress src into.
  150. * @dst_capacity: The size of the destination buffer. May be any size, but
  151. * ZSTD_compressBound(srcSize) is guaranteed to be large enough.
  152. * @src: The data to compress.
  153. * @src_size: The size of the data to compress.
  154. * @parameters: The compression parameters to be used.
  155. *
  156. * Return: The compressed size or an error, which can be checked using
  157. * zstd_is_error().
  158. */
  159. size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity,
  160. const void *src, size_t src_size, const zstd_parameters *parameters);
  161. /* ====== Single-pass Decompression ====== */
  162. typedef ZSTD_DCtx zstd_dctx;
  163. /**
  164. * zstd_dctx_workspace_bound() - max memory needed to initialize a zstd_dctx
  165. *
  166. * Return: A lower bound on the size of the workspace that is passed to
  167. * zstd_init_dctx().
  168. */
  169. size_t zstd_dctx_workspace_bound(void);
  170. /**
  171. * zstd_init_dctx() - initialize a zstd decompression context
  172. * @workspace: The workspace to emplace the context into. It must outlive
  173. * the returned context.
  174. * @workspace_size: The size of workspace. Use zstd_dctx_workspace_bound() to
  175. * determine how large the workspace must be.
  176. *
  177. * Return: A zstd decompression context or NULL on error.
  178. */
  179. zstd_dctx *zstd_init_dctx(void *workspace, size_t workspace_size);
  180. /**
  181. * zstd_decompress_dctx() - decompress zstd compressed src into dst
  182. * @dctx: The decompression context.
  183. * @dst: The buffer to decompress src into.
  184. * @dst_capacity: The size of the destination buffer. Must be at least as large
  185. * as the decompressed size. If the caller cannot upper bound the
  186. * decompressed size, then it's better to use the streaming API.
  187. * @src: The zstd compressed data to decompress. Multiple concatenated
  188. * frames and skippable frames are allowed.
  189. * @src_size: The exact size of the data to decompress.
  190. *
  191. * Return: The decompressed size or an error, which can be checked using
  192. * zstd_is_error().
  193. */
  194. size_t zstd_decompress_dctx(zstd_dctx *dctx, void *dst, size_t dst_capacity,
  195. const void *src, size_t src_size);
  196. /* ====== Streaming Buffers ====== */
  197. /**
  198. * struct zstd_in_buffer - input buffer for streaming
  199. * @src: Start of the input buffer.
  200. * @size: Size of the input buffer.
  201. * @pos: Position where reading stopped. Will be updated.
  202. * Necessarily 0 <= pos <= size.
  203. *
  204. * See zstd_lib.h.
  205. */
  206. typedef ZSTD_inBuffer zstd_in_buffer;
  207. /**
  208. * struct zstd_out_buffer - output buffer for streaming
  209. * @dst: Start of the output buffer.
  210. * @size: Size of the output buffer.
  211. * @pos: Position where writing stopped. Will be updated.
  212. * Necessarily 0 <= pos <= size.
  213. *
  214. * See zstd_lib.h.
  215. */
  216. typedef ZSTD_outBuffer zstd_out_buffer;
  217. /* ====== Streaming Compression ====== */
  218. typedef ZSTD_CStream zstd_cstream;
  219. /**
  220. * zstd_cstream_workspace_bound() - memory needed to initialize a zstd_cstream
  221. * @cparams: The compression parameters to be used for compression.
  222. *
  223. * Return: A lower bound on the size of the workspace that is passed to
  224. * zstd_init_cstream().
  225. */
  226. size_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams);
  227. /**
  228. * zstd_init_cstream() - initialize a zstd streaming compression context
  229. * @parameters The zstd parameters to use for compression.
  230. * @pledged_src_size: If params.fParams.contentSizeFlag == 1 then the caller
  231. * must pass the source size (zero means empty source).
  232. * Otherwise, the caller may optionally pass the source
  233. * size, or zero if unknown.
  234. * @workspace: The workspace to emplace the context into. It must outlive
  235. * the returned context.
  236. * @workspace_size: The size of workspace.
  237. * Use zstd_cstream_workspace_bound(params->cparams) to
  238. * determine how large the workspace must be.
  239. *
  240. * Return: The zstd streaming compression context or NULL on error.
  241. */
  242. zstd_cstream *zstd_init_cstream(const zstd_parameters *parameters,
  243. unsigned long long pledged_src_size, void *workspace, size_t workspace_size);
  244. /**
  245. * zstd_reset_cstream() - reset the context using parameters from creation
  246. * @cstream: The zstd streaming compression context to reset.
  247. * @pledged_src_size: Optionally the source size, or zero if unknown.
  248. *
  249. * Resets the context using the parameters from creation. Skips dictionary
  250. * loading, since it can be reused. If `pledged_src_size` is non-zero the frame
  251. * content size is always written into the frame header.
  252. *
  253. * Return: Zero or an error, which can be checked using
  254. * zstd_is_error().
  255. */
  256. size_t zstd_reset_cstream(zstd_cstream *cstream,
  257. unsigned long long pledged_src_size);
  258. /**
  259. * zstd_compress_stream() - streaming compress some of input into output
  260. * @cstream: The zstd streaming compression context.
  261. * @output: Destination buffer. `output->pos` is updated to indicate how much
  262. * compressed data was written.
  263. * @input: Source buffer. `input->pos` is updated to indicate how much data
  264. * was read. Note that it may not consume the entire input, in which
  265. * case `input->pos < input->size`, and it's up to the caller to
  266. * present remaining data again.
  267. *
  268. * The `input` and `output` buffers may be any size. Guaranteed to make some
  269. * forward progress if `input` and `output` are not empty.
  270. *
  271. * Return: A hint for the number of bytes to use as the input for the next
  272. * function call or an error, which can be checked using
  273. * zstd_is_error().
  274. */
  275. size_t zstd_compress_stream(zstd_cstream *cstream, zstd_out_buffer *output,
  276. zstd_in_buffer *input);
  277. /**
  278. * zstd_flush_stream() - flush internal buffers into output
  279. * @cstream: The zstd streaming compression context.
  280. * @output: Destination buffer. `output->pos` is updated to indicate how much
  281. * compressed data was written.
  282. *
  283. * zstd_flush_stream() must be called until it returns 0, meaning all the data
  284. * has been flushed. Since zstd_flush_stream() causes a block to be ended,
  285. * calling it too often will degrade the compression ratio.
  286. *
  287. * Return: The number of bytes still present within internal buffers or an
  288. * error, which can be checked using zstd_is_error().
  289. */
  290. size_t zstd_flush_stream(zstd_cstream *cstream, zstd_out_buffer *output);
  291. /**
  292. * zstd_end_stream() - flush internal buffers into output and end the frame
  293. * @cstream: The zstd streaming compression context.
  294. * @output: Destination buffer. `output->pos` is updated to indicate how much
  295. * compressed data was written.
  296. *
  297. * zstd_end_stream() must be called until it returns 0, meaning all the data has
  298. * been flushed and the frame epilogue has been written.
  299. *
  300. * Return: The number of bytes still present within internal buffers or an
  301. * error, which can be checked using zstd_is_error().
  302. */
  303. size_t zstd_end_stream(zstd_cstream *cstream, zstd_out_buffer *output);
  304. /* ====== Streaming Decompression ====== */
  305. typedef ZSTD_DStream zstd_dstream;
  306. /**
  307. * zstd_dstream_workspace_bound() - memory needed to initialize a zstd_dstream
  308. * @max_window_size: The maximum window size allowed for compressed frames.
  309. *
  310. * Return: A lower bound on the size of the workspace that is passed
  311. * to zstd_init_dstream().
  312. */
  313. size_t zstd_dstream_workspace_bound(size_t max_window_size);
  314. /**
  315. * zstd_init_dstream() - initialize a zstd streaming decompression context
  316. * @max_window_size: The maximum window size allowed for compressed frames.
  317. * @workspace: The workspace to emplace the context into. It must outlive
  318. * the returned context.
  319. * @workspaceSize: The size of workspace.
  320. * Use zstd_dstream_workspace_bound(max_window_size) to
  321. * determine how large the workspace must be.
  322. *
  323. * Return: The zstd streaming decompression context.
  324. */
  325. zstd_dstream *zstd_init_dstream(size_t max_window_size, void *workspace,
  326. size_t workspace_size);
  327. /**
  328. * zstd_reset_dstream() - reset the context using parameters from creation
  329. * @dstream: The zstd streaming decompression context to reset.
  330. *
  331. * Resets the context using the parameters from creation. Skips dictionary
  332. * loading, since it can be reused.
  333. *
  334. * Return: Zero or an error, which can be checked using zstd_is_error().
  335. */
  336. size_t zstd_reset_dstream(zstd_dstream *dstream);
  337. /**
  338. * zstd_decompress_stream() - streaming decompress some of input into output
  339. * @dstream: The zstd streaming decompression context.
  340. * @output: Destination buffer. `output.pos` is updated to indicate how much
  341. * decompressed data was written.
  342. * @input: Source buffer. `input.pos` is updated to indicate how much data was
  343. * read. Note that it may not consume the entire input, in which case
  344. * `input.pos < input.size`, and it's up to the caller to present
  345. * remaining data again.
  346. *
  347. * The `input` and `output` buffers may be any size. Guaranteed to make some
  348. * forward progress if `input` and `output` are not empty.
  349. * zstd_decompress_stream() will not consume the last byte of the frame until
  350. * the entire frame is flushed.
  351. *
  352. * Return: Returns 0 iff a frame is completely decoded and fully flushed.
  353. * Otherwise returns a hint for the number of bytes to use as the
  354. * input for the next function call or an error, which can be checked
  355. * using zstd_is_error(). The size hint will never load more than the
  356. * frame.
  357. */
  358. size_t zstd_decompress_stream(zstd_dstream *dstream, zstd_out_buffer *output,
  359. zstd_in_buffer *input);
  360. /* ====== Frame Inspection Functions ====== */
  361. /**
  362. * zstd_find_frame_compressed_size() - returns the size of a compressed frame
  363. * @src: Source buffer. It should point to the start of a zstd encoded
  364. * frame or a skippable frame.
  365. * @src_size: The size of the source buffer. It must be at least as large as the
  366. * size of the frame.
  367. *
  368. * Return: The compressed size of the frame pointed to by `src` or an error,
  369. * which can be check with zstd_is_error().
  370. * Suitable to pass to ZSTD_decompress() or similar functions.
  371. */
  372. size_t zstd_find_frame_compressed_size(const void *src, size_t src_size);
  373. /**
  374. * struct zstd_frame_params - zstd frame parameters stored in the frame header
  375. * @frameContentSize: The frame content size, or ZSTD_CONTENTSIZE_UNKNOWN if not
  376. * present.
  377. * @windowSize: The window size, or 0 if the frame is a skippable frame.
  378. * @blockSizeMax: The maximum block size.
  379. * @frameType: The frame type (zstd or skippable)
  380. * @headerSize: The size of the frame header.
  381. * @dictID: The dictionary id, or 0 if not present.
  382. * @checksumFlag: Whether a checksum was used.
  383. *
  384. * See zstd_lib.h.
  385. */
  386. typedef ZSTD_frameHeader zstd_frame_header;
  387. /**
  388. * zstd_get_frame_header() - extracts parameters from a zstd or skippable frame
  389. * @params: On success the frame parameters are written here.
  390. * @src: The source buffer. It must point to a zstd or skippable frame.
  391. * @src_size: The size of the source buffer.
  392. *
  393. * Return: 0 on success. If more data is required it returns how many bytes
  394. * must be provided to make forward progress. Otherwise it returns
  395. * an error, which can be checked using zstd_is_error().
  396. */
  397. size_t zstd_get_frame_header(zstd_frame_header *params, const void *src,
  398. size_t src_size);
  399. #endif /* LINUX_ZSTD_H */