![]() |
|
|||
0001 /* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */ 0002 /* 0003 * Copyright (c) Yann Collet, Facebook, Inc. 0004 * All rights reserved. 0005 * 0006 * This source code is licensed under both the BSD-style license (found in the 0007 * LICENSE file in the root directory of https://github.com/facebook/zstd) and 0008 * the GPLv2 (found in the COPYING file in the root directory of 0009 * https://github.com/facebook/zstd). You may select, at your option, one of the 0010 * above-listed licenses. 0011 */ 0012 0013 #ifndef LINUX_ZSTD_H 0014 #define LINUX_ZSTD_H 0015 0016 /** 0017 * This is a kernel-style API that wraps the upstream zstd API, which cannot be 0018 * used directly because the symbols aren't exported. It exposes the minimal 0019 * functionality which is currently required by users of zstd in the kernel. 0020 * Expose extra functions from lib/zstd/zstd.h as needed. 0021 */ 0022 0023 /* ====== Dependency ====== */ 0024 #include <linux/types.h> 0025 #include <linux/zstd_errors.h> 0026 #include <linux/zstd_lib.h> 0027 0028 /* ====== Helper Functions ====== */ 0029 /** 0030 * zstd_compress_bound() - maximum compressed size in worst case scenario 0031 * @src_size: The size of the data to compress. 0032 * 0033 * Return: The maximum compressed size in the worst case scenario. 0034 */ 0035 size_t zstd_compress_bound(size_t src_size); 0036 0037 /** 0038 * zstd_is_error() - tells if a size_t function result is an error code 0039 * @code: The function result to check for error. 0040 * 0041 * Return: Non-zero iff the code is an error. 0042 */ 0043 unsigned int zstd_is_error(size_t code); 0044 0045 /** 0046 * enum zstd_error_code - zstd error codes 0047 */ 0048 typedef ZSTD_ErrorCode zstd_error_code; 0049 0050 /** 0051 * zstd_get_error_code() - translates an error function result to an error code 0052 * @code: The function result for which zstd_is_error(code) is true. 0053 * 0054 * Return: A unique error code for this error. 0055 */ 0056 zstd_error_code zstd_get_error_code(size_t code); 0057 0058 /** 0059 * zstd_get_error_name() - translates an error function result to a string 0060 * @code: The function result for which zstd_is_error(code) is true. 0061 * 0062 * Return: An error string corresponding to the error code. 0063 */ 0064 const char *zstd_get_error_name(size_t code); 0065 0066 /** 0067 * zstd_min_clevel() - minimum allowed compression level 0068 * 0069 * Return: The minimum allowed compression level. 0070 */ 0071 int zstd_min_clevel(void); 0072 0073 /** 0074 * zstd_max_clevel() - maximum allowed compression level 0075 * 0076 * Return: The maximum allowed compression level. 0077 */ 0078 int zstd_max_clevel(void); 0079 0080 /* ====== Parameter Selection ====== */ 0081 0082 /** 0083 * enum zstd_strategy - zstd compression search strategy 0084 * 0085 * From faster to stronger. See zstd_lib.h. 0086 */ 0087 typedef ZSTD_strategy zstd_strategy; 0088 0089 /** 0090 * struct zstd_compression_parameters - zstd compression parameters 0091 * @windowLog: Log of the largest match distance. Larger means more 0092 * compression, and more memory needed during decompression. 0093 * @chainLog: Fully searched segment. Larger means more compression, 0094 * slower, and more memory (useless for fast). 0095 * @hashLog: Dispatch table. Larger means more compression, 0096 * slower, and more memory. 0097 * @searchLog: Number of searches. Larger means more compression and slower. 0098 * @searchLength: Match length searched. Larger means faster decompression, 0099 * sometimes less compression. 0100 * @targetLength: Acceptable match size for optimal parser (only). Larger means 0101 * more compression, and slower. 0102 * @strategy: The zstd compression strategy. 0103 * 0104 * See zstd_lib.h. 0105 */ 0106 typedef ZSTD_compressionParameters zstd_compression_parameters; 0107 0108 /** 0109 * struct zstd_frame_parameters - zstd frame parameters 0110 * @contentSizeFlag: Controls whether content size will be present in the 0111 * frame header (when known). 0112 * @checksumFlag: Controls whether a 32-bit checksum is generated at the 0113 * end of the frame for error detection. 0114 * @noDictIDFlag: Controls whether dictID will be saved into the frame 0115 * header when using dictionary compression. 0116 * 0117 * The default value is all fields set to 0. See zstd_lib.h. 0118 */ 0119 typedef ZSTD_frameParameters zstd_frame_parameters; 0120 0121 /** 0122 * struct zstd_parameters - zstd parameters 0123 * @cParams: The compression parameters. 0124 * @fParams: The frame parameters. 0125 */ 0126 typedef ZSTD_parameters zstd_parameters; 0127 0128 /** 0129 * zstd_get_params() - returns zstd_parameters for selected level 0130 * @level: The compression level 0131 * @estimated_src_size: The estimated source size to compress or 0 0132 * if unknown. 0133 * 0134 * Return: The selected zstd_parameters. 0135 */ 0136 zstd_parameters zstd_get_params(int level, 0137 unsigned long long estimated_src_size); 0138 0139 /* ====== Single-pass Compression ====== */ 0140 0141 typedef ZSTD_CCtx zstd_cctx; 0142 0143 /** 0144 * zstd_cctx_workspace_bound() - max memory needed to initialize a zstd_cctx 0145 * @parameters: The compression parameters to be used. 0146 * 0147 * If multiple compression parameters might be used, the caller must call 0148 * zstd_cctx_workspace_bound() for each set of parameters and use the maximum 0149 * size. 0150 * 0151 * Return: A lower bound on the size of the workspace that is passed to 0152 * zstd_init_cctx(). 0153 */ 0154 size_t zstd_cctx_workspace_bound(const zstd_compression_parameters *parameters); 0155 0156 /** 0157 * zstd_init_cctx() - initialize a zstd compression context 0158 * @workspace: The workspace to emplace the context into. It must outlive 0159 * the returned context. 0160 * @workspace_size: The size of workspace. Use zstd_cctx_workspace_bound() to 0161 * determine how large the workspace must be. 0162 * 0163 * Return: A zstd compression context or NULL on error. 0164 */ 0165 zstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size); 0166 0167 /** 0168 * zstd_compress_cctx() - compress src into dst with the initialized parameters 0169 * @cctx: The context. Must have been initialized with zstd_init_cctx(). 0170 * @dst: The buffer to compress src into. 0171 * @dst_capacity: The size of the destination buffer. May be any size, but 0172 * ZSTD_compressBound(srcSize) is guaranteed to be large enough. 0173 * @src: The data to compress. 0174 * @src_size: The size of the data to compress. 0175 * @parameters: The compression parameters to be used. 0176 * 0177 * Return: The compressed size or an error, which can be checked using 0178 * zstd_is_error(). 0179 */ 0180 size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity, 0181 const void *src, size_t src_size, const zstd_parameters *parameters); 0182 0183 /* ====== Single-pass Decompression ====== */ 0184 0185 typedef ZSTD_DCtx zstd_dctx; 0186 0187 /** 0188 * zstd_dctx_workspace_bound() - max memory needed to initialize a zstd_dctx 0189 * 0190 * Return: A lower bound on the size of the workspace that is passed to 0191 * zstd_init_dctx(). 0192 */ 0193 size_t zstd_dctx_workspace_bound(void); 0194 0195 /** 0196 * zstd_init_dctx() - initialize a zstd decompression context 0197 * @workspace: The workspace to emplace the context into. It must outlive 0198 * the returned context. 0199 * @workspace_size: The size of workspace. Use zstd_dctx_workspace_bound() to 0200 * determine how large the workspace must be. 0201 * 0202 * Return: A zstd decompression context or NULL on error. 0203 */ 0204 zstd_dctx *zstd_init_dctx(void *workspace, size_t workspace_size); 0205 0206 /** 0207 * zstd_decompress_dctx() - decompress zstd compressed src into dst 0208 * @dctx: The decompression context. 0209 * @dst: The buffer to decompress src into. 0210 * @dst_capacity: The size of the destination buffer. Must be at least as large 0211 * as the decompressed size. If the caller cannot upper bound the 0212 * decompressed size, then it's better to use the streaming API. 0213 * @src: The zstd compressed data to decompress. Multiple concatenated 0214 * frames and skippable frames are allowed. 0215 * @src_size: The exact size of the data to decompress. 0216 * 0217 * Return: The decompressed size or an error, which can be checked using 0218 * zstd_is_error(). 0219 */ 0220 size_t zstd_decompress_dctx(zstd_dctx *dctx, void *dst, size_t dst_capacity, 0221 const void *src, size_t src_size); 0222 0223 /* ====== Streaming Buffers ====== */ 0224 0225 /** 0226 * struct zstd_in_buffer - input buffer for streaming 0227 * @src: Start of the input buffer. 0228 * @size: Size of the input buffer. 0229 * @pos: Position where reading stopped. Will be updated. 0230 * Necessarily 0 <= pos <= size. 0231 * 0232 * See zstd_lib.h. 0233 */ 0234 typedef ZSTD_inBuffer zstd_in_buffer; 0235 0236 /** 0237 * struct zstd_out_buffer - output buffer for streaming 0238 * @dst: Start of the output buffer. 0239 * @size: Size of the output buffer. 0240 * @pos: Position where writing stopped. Will be updated. 0241 * Necessarily 0 <= pos <= size. 0242 * 0243 * See zstd_lib.h. 0244 */ 0245 typedef ZSTD_outBuffer zstd_out_buffer; 0246 0247 /* ====== Streaming Compression ====== */ 0248 0249 typedef ZSTD_CStream zstd_cstream; 0250 0251 /** 0252 * zstd_cstream_workspace_bound() - memory needed to initialize a zstd_cstream 0253 * @cparams: The compression parameters to be used for compression. 0254 * 0255 * Return: A lower bound on the size of the workspace that is passed to 0256 * zstd_init_cstream(). 0257 */ 0258 size_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams); 0259 0260 /** 0261 * zstd_init_cstream() - initialize a zstd streaming compression context 0262 * @parameters The zstd parameters to use for compression. 0263 * @pledged_src_size: If params.fParams.contentSizeFlag == 1 then the caller 0264 * must pass the source size (zero means empty source). 0265 * Otherwise, the caller may optionally pass the source 0266 * size, or zero if unknown. 0267 * @workspace: The workspace to emplace the context into. It must outlive 0268 * the returned context. 0269 * @workspace_size: The size of workspace. 0270 * Use zstd_cstream_workspace_bound(params->cparams) to 0271 * determine how large the workspace must be. 0272 * 0273 * Return: The zstd streaming compression context or NULL on error. 0274 */ 0275 zstd_cstream *zstd_init_cstream(const zstd_parameters *parameters, 0276 unsigned long long pledged_src_size, void *workspace, size_t workspace_size); 0277 0278 /** 0279 * zstd_reset_cstream() - reset the context using parameters from creation 0280 * @cstream: The zstd streaming compression context to reset. 0281 * @pledged_src_size: Optionally the source size, or zero if unknown. 0282 * 0283 * Resets the context using the parameters from creation. Skips dictionary 0284 * loading, since it can be reused. If `pledged_src_size` is non-zero the frame 0285 * content size is always written into the frame header. 0286 * 0287 * Return: Zero or an error, which can be checked using 0288 * zstd_is_error(). 0289 */ 0290 size_t zstd_reset_cstream(zstd_cstream *cstream, 0291 unsigned long long pledged_src_size); 0292 0293 /** 0294 * zstd_compress_stream() - streaming compress some of input into output 0295 * @cstream: The zstd streaming compression context. 0296 * @output: Destination buffer. `output->pos` is updated to indicate how much 0297 * compressed data was written. 0298 * @input: Source buffer. `input->pos` is updated to indicate how much data 0299 * was read. Note that it may not consume the entire input, in which 0300 * case `input->pos < input->size`, and it's up to the caller to 0301 * present remaining data again. 0302 * 0303 * The `input` and `output` buffers may be any size. Guaranteed to make some 0304 * forward progress if `input` and `output` are not empty. 0305 * 0306 * Return: A hint for the number of bytes to use as the input for the next 0307 * function call or an error, which can be checked using 0308 * zstd_is_error(). 0309 */ 0310 size_t zstd_compress_stream(zstd_cstream *cstream, zstd_out_buffer *output, 0311 zstd_in_buffer *input); 0312 0313 /** 0314 * zstd_flush_stream() - flush internal buffers into output 0315 * @cstream: The zstd streaming compression context. 0316 * @output: Destination buffer. `output->pos` is updated to indicate how much 0317 * compressed data was written. 0318 * 0319 * zstd_flush_stream() must be called until it returns 0, meaning all the data 0320 * has been flushed. Since zstd_flush_stream() causes a block to be ended, 0321 * calling it too often will degrade the compression ratio. 0322 * 0323 * Return: The number of bytes still present within internal buffers or an 0324 * error, which can be checked using zstd_is_error(). 0325 */ 0326 size_t zstd_flush_stream(zstd_cstream *cstream, zstd_out_buffer *output); 0327 0328 /** 0329 * zstd_end_stream() - flush internal buffers into output and end the frame 0330 * @cstream: The zstd streaming compression context. 0331 * @output: Destination buffer. `output->pos` is updated to indicate how much 0332 * compressed data was written. 0333 * 0334 * zstd_end_stream() must be called until it returns 0, meaning all the data has 0335 * been flushed and the frame epilogue has been written. 0336 * 0337 * Return: The number of bytes still present within internal buffers or an 0338 * error, which can be checked using zstd_is_error(). 0339 */ 0340 size_t zstd_end_stream(zstd_cstream *cstream, zstd_out_buffer *output); 0341 0342 /* ====== Streaming Decompression ====== */ 0343 0344 typedef ZSTD_DStream zstd_dstream; 0345 0346 /** 0347 * zstd_dstream_workspace_bound() - memory needed to initialize a zstd_dstream 0348 * @max_window_size: The maximum window size allowed for compressed frames. 0349 * 0350 * Return: A lower bound on the size of the workspace that is passed 0351 * to zstd_init_dstream(). 0352 */ 0353 size_t zstd_dstream_workspace_bound(size_t max_window_size); 0354 0355 /** 0356 * zstd_init_dstream() - initialize a zstd streaming decompression context 0357 * @max_window_size: The maximum window size allowed for compressed frames. 0358 * @workspace: The workspace to emplace the context into. It must outlive 0359 * the returned context. 0360 * @workspaceSize: The size of workspace. 0361 * Use zstd_dstream_workspace_bound(max_window_size) to 0362 * determine how large the workspace must be. 0363 * 0364 * Return: The zstd streaming decompression context. 0365 */ 0366 zstd_dstream *zstd_init_dstream(size_t max_window_size, void *workspace, 0367 size_t workspace_size); 0368 0369 /** 0370 * zstd_reset_dstream() - reset the context using parameters from creation 0371 * @dstream: The zstd streaming decompression context to reset. 0372 * 0373 * Resets the context using the parameters from creation. Skips dictionary 0374 * loading, since it can be reused. 0375 * 0376 * Return: Zero or an error, which can be checked using zstd_is_error(). 0377 */ 0378 size_t zstd_reset_dstream(zstd_dstream *dstream); 0379 0380 /** 0381 * zstd_decompress_stream() - streaming decompress some of input into output 0382 * @dstream: The zstd streaming decompression context. 0383 * @output: Destination buffer. `output.pos` is updated to indicate how much 0384 * decompressed data was written. 0385 * @input: Source buffer. `input.pos` is updated to indicate how much data was 0386 * read. Note that it may not consume the entire input, in which case 0387 * `input.pos < input.size`, and it's up to the caller to present 0388 * remaining data again. 0389 * 0390 * The `input` and `output` buffers may be any size. Guaranteed to make some 0391 * forward progress if `input` and `output` are not empty. 0392 * zstd_decompress_stream() will not consume the last byte of the frame until 0393 * the entire frame is flushed. 0394 * 0395 * Return: Returns 0 iff a frame is completely decoded and fully flushed. 0396 * Otherwise returns a hint for the number of bytes to use as the 0397 * input for the next function call or an error, which can be checked 0398 * using zstd_is_error(). The size hint will never load more than the 0399 * frame. 0400 */ 0401 size_t zstd_decompress_stream(zstd_dstream *dstream, zstd_out_buffer *output, 0402 zstd_in_buffer *input); 0403 0404 /* ====== Frame Inspection Functions ====== */ 0405 0406 /** 0407 * zstd_find_frame_compressed_size() - returns the size of a compressed frame 0408 * @src: Source buffer. It should point to the start of a zstd encoded 0409 * frame or a skippable frame. 0410 * @src_size: The size of the source buffer. It must be at least as large as the 0411 * size of the frame. 0412 * 0413 * Return: The compressed size of the frame pointed to by `src` or an error, 0414 * which can be check with zstd_is_error(). 0415 * Suitable to pass to ZSTD_decompress() or similar functions. 0416 */ 0417 size_t zstd_find_frame_compressed_size(const void *src, size_t src_size); 0418 0419 /** 0420 * struct zstd_frame_params - zstd frame parameters stored in the frame header 0421 * @frameContentSize: The frame content size, or ZSTD_CONTENTSIZE_UNKNOWN if not 0422 * present. 0423 * @windowSize: The window size, or 0 if the frame is a skippable frame. 0424 * @blockSizeMax: The maximum block size. 0425 * @frameType: The frame type (zstd or skippable) 0426 * @headerSize: The size of the frame header. 0427 * @dictID: The dictionary id, or 0 if not present. 0428 * @checksumFlag: Whether a checksum was used. 0429 * 0430 * See zstd_lib.h. 0431 */ 0432 typedef ZSTD_frameHeader zstd_frame_header; 0433 0434 /** 0435 * zstd_get_frame_header() - extracts parameters from a zstd or skippable frame 0436 * @params: On success the frame parameters are written here. 0437 * @src: The source buffer. It must point to a zstd or skippable frame. 0438 * @src_size: The size of the source buffer. 0439 * 0440 * Return: 0 on success. If more data is required it returns how many bytes 0441 * must be provided to make forward progress. Otherwise it returns 0442 * an error, which can be checked using zstd_is_error(). 0443 */ 0444 size_t zstd_get_frame_header(zstd_frame_header *params, const void *src, 0445 size_t src_size); 0446 0447 #endif /* LINUX_ZSTD_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |