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