162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (c) Yann Collet, Facebook, Inc. 462306a36Sopenharmony_ci * All rights reserved. 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * This source code is licensed under both the BSD-style license (found in the 762306a36Sopenharmony_ci * LICENSE file in the root directory of https://github.com/facebook/zstd) and 862306a36Sopenharmony_ci * the GPLv2 (found in the COPYING file in the root directory of 962306a36Sopenharmony_ci * https://github.com/facebook/zstd). You may select, at your option, one of the 1062306a36Sopenharmony_ci * above-listed licenses. 1162306a36Sopenharmony_ci */ 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci#ifndef LINUX_ZSTD_H 1462306a36Sopenharmony_ci#define LINUX_ZSTD_H 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci/** 1762306a36Sopenharmony_ci * This is a kernel-style API that wraps the upstream zstd API, which cannot be 1862306a36Sopenharmony_ci * used directly because the symbols aren't exported. It exposes the minimal 1962306a36Sopenharmony_ci * functionality which is currently required by users of zstd in the kernel. 2062306a36Sopenharmony_ci * Expose extra functions from lib/zstd/zstd.h as needed. 2162306a36Sopenharmony_ci */ 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci/* ====== Dependency ====== */ 2462306a36Sopenharmony_ci#include <linux/types.h> 2562306a36Sopenharmony_ci#include <linux/zstd_errors.h> 2662306a36Sopenharmony_ci#include <linux/zstd_lib.h> 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci/* ====== Helper Functions ====== */ 2962306a36Sopenharmony_ci/** 3062306a36Sopenharmony_ci * zstd_compress_bound() - maximum compressed size in worst case scenario 3162306a36Sopenharmony_ci * @src_size: The size of the data to compress. 3262306a36Sopenharmony_ci * 3362306a36Sopenharmony_ci * Return: The maximum compressed size in the worst case scenario. 3462306a36Sopenharmony_ci */ 3562306a36Sopenharmony_cisize_t zstd_compress_bound(size_t src_size); 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci/** 3862306a36Sopenharmony_ci * zstd_is_error() - tells if a size_t function result is an error code 3962306a36Sopenharmony_ci * @code: The function result to check for error. 4062306a36Sopenharmony_ci * 4162306a36Sopenharmony_ci * Return: Non-zero iff the code is an error. 4262306a36Sopenharmony_ci */ 4362306a36Sopenharmony_ciunsigned int zstd_is_error(size_t code); 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci/** 4662306a36Sopenharmony_ci * enum zstd_error_code - zstd error codes 4762306a36Sopenharmony_ci */ 4862306a36Sopenharmony_citypedef ZSTD_ErrorCode zstd_error_code; 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ci/** 5162306a36Sopenharmony_ci * zstd_get_error_code() - translates an error function result to an error code 5262306a36Sopenharmony_ci * @code: The function result for which zstd_is_error(code) is true. 5362306a36Sopenharmony_ci * 5462306a36Sopenharmony_ci * Return: A unique error code for this error. 5562306a36Sopenharmony_ci */ 5662306a36Sopenharmony_cizstd_error_code zstd_get_error_code(size_t code); 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci/** 5962306a36Sopenharmony_ci * zstd_get_error_name() - translates an error function result to a string 6062306a36Sopenharmony_ci * @code: The function result for which zstd_is_error(code) is true. 6162306a36Sopenharmony_ci * 6262306a36Sopenharmony_ci * Return: An error string corresponding to the error code. 6362306a36Sopenharmony_ci */ 6462306a36Sopenharmony_ciconst char *zstd_get_error_name(size_t code); 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci/** 6762306a36Sopenharmony_ci * zstd_min_clevel() - minimum allowed compression level 6862306a36Sopenharmony_ci * 6962306a36Sopenharmony_ci * Return: The minimum allowed compression level. 7062306a36Sopenharmony_ci */ 7162306a36Sopenharmony_ciint zstd_min_clevel(void); 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci/** 7462306a36Sopenharmony_ci * zstd_max_clevel() - maximum allowed compression level 7562306a36Sopenharmony_ci * 7662306a36Sopenharmony_ci * Return: The maximum allowed compression level. 7762306a36Sopenharmony_ci */ 7862306a36Sopenharmony_ciint zstd_max_clevel(void); 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci/* ====== Parameter Selection ====== */ 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci/** 8362306a36Sopenharmony_ci * enum zstd_strategy - zstd compression search strategy 8462306a36Sopenharmony_ci * 8562306a36Sopenharmony_ci * From faster to stronger. See zstd_lib.h. 8662306a36Sopenharmony_ci */ 8762306a36Sopenharmony_citypedef ZSTD_strategy zstd_strategy; 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci/** 9062306a36Sopenharmony_ci * struct zstd_compression_parameters - zstd compression parameters 9162306a36Sopenharmony_ci * @windowLog: Log of the largest match distance. Larger means more 9262306a36Sopenharmony_ci * compression, and more memory needed during decompression. 9362306a36Sopenharmony_ci * @chainLog: Fully searched segment. Larger means more compression, 9462306a36Sopenharmony_ci * slower, and more memory (useless for fast). 9562306a36Sopenharmony_ci * @hashLog: Dispatch table. Larger means more compression, 9662306a36Sopenharmony_ci * slower, and more memory. 9762306a36Sopenharmony_ci * @searchLog: Number of searches. Larger means more compression and slower. 9862306a36Sopenharmony_ci * @searchLength: Match length searched. Larger means faster decompression, 9962306a36Sopenharmony_ci * sometimes less compression. 10062306a36Sopenharmony_ci * @targetLength: Acceptable match size for optimal parser (only). Larger means 10162306a36Sopenharmony_ci * more compression, and slower. 10262306a36Sopenharmony_ci * @strategy: The zstd compression strategy. 10362306a36Sopenharmony_ci * 10462306a36Sopenharmony_ci * See zstd_lib.h. 10562306a36Sopenharmony_ci */ 10662306a36Sopenharmony_citypedef ZSTD_compressionParameters zstd_compression_parameters; 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci/** 10962306a36Sopenharmony_ci * struct zstd_frame_parameters - zstd frame parameters 11062306a36Sopenharmony_ci * @contentSizeFlag: Controls whether content size will be present in the 11162306a36Sopenharmony_ci * frame header (when known). 11262306a36Sopenharmony_ci * @checksumFlag: Controls whether a 32-bit checksum is generated at the 11362306a36Sopenharmony_ci * end of the frame for error detection. 11462306a36Sopenharmony_ci * @noDictIDFlag: Controls whether dictID will be saved into the frame 11562306a36Sopenharmony_ci * header when using dictionary compression. 11662306a36Sopenharmony_ci * 11762306a36Sopenharmony_ci * The default value is all fields set to 0. See zstd_lib.h. 11862306a36Sopenharmony_ci */ 11962306a36Sopenharmony_citypedef ZSTD_frameParameters zstd_frame_parameters; 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci/** 12262306a36Sopenharmony_ci * struct zstd_parameters - zstd parameters 12362306a36Sopenharmony_ci * @cParams: The compression parameters. 12462306a36Sopenharmony_ci * @fParams: The frame parameters. 12562306a36Sopenharmony_ci */ 12662306a36Sopenharmony_citypedef ZSTD_parameters zstd_parameters; 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci/** 12962306a36Sopenharmony_ci * zstd_get_params() - returns zstd_parameters for selected level 13062306a36Sopenharmony_ci * @level: The compression level 13162306a36Sopenharmony_ci * @estimated_src_size: The estimated source size to compress or 0 13262306a36Sopenharmony_ci * if unknown. 13362306a36Sopenharmony_ci * 13462306a36Sopenharmony_ci * Return: The selected zstd_parameters. 13562306a36Sopenharmony_ci */ 13662306a36Sopenharmony_cizstd_parameters zstd_get_params(int level, 13762306a36Sopenharmony_ci unsigned long long estimated_src_size); 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci/* ====== Single-pass Compression ====== */ 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_citypedef ZSTD_CCtx zstd_cctx; 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci/** 14462306a36Sopenharmony_ci * zstd_cctx_workspace_bound() - max memory needed to initialize a zstd_cctx 14562306a36Sopenharmony_ci * @parameters: The compression parameters to be used. 14662306a36Sopenharmony_ci * 14762306a36Sopenharmony_ci * If multiple compression parameters might be used, the caller must call 14862306a36Sopenharmony_ci * zstd_cctx_workspace_bound() for each set of parameters and use the maximum 14962306a36Sopenharmony_ci * size. 15062306a36Sopenharmony_ci * 15162306a36Sopenharmony_ci * Return: A lower bound on the size of the workspace that is passed to 15262306a36Sopenharmony_ci * zstd_init_cctx(). 15362306a36Sopenharmony_ci */ 15462306a36Sopenharmony_cisize_t zstd_cctx_workspace_bound(const zstd_compression_parameters *parameters); 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci/** 15762306a36Sopenharmony_ci * zstd_init_cctx() - initialize a zstd compression context 15862306a36Sopenharmony_ci * @workspace: The workspace to emplace the context into. It must outlive 15962306a36Sopenharmony_ci * the returned context. 16062306a36Sopenharmony_ci * @workspace_size: The size of workspace. Use zstd_cctx_workspace_bound() to 16162306a36Sopenharmony_ci * determine how large the workspace must be. 16262306a36Sopenharmony_ci * 16362306a36Sopenharmony_ci * Return: A zstd compression context or NULL on error. 16462306a36Sopenharmony_ci */ 16562306a36Sopenharmony_cizstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size); 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci/** 16862306a36Sopenharmony_ci * zstd_compress_cctx() - compress src into dst with the initialized parameters 16962306a36Sopenharmony_ci * @cctx: The context. Must have been initialized with zstd_init_cctx(). 17062306a36Sopenharmony_ci * @dst: The buffer to compress src into. 17162306a36Sopenharmony_ci * @dst_capacity: The size of the destination buffer. May be any size, but 17262306a36Sopenharmony_ci * ZSTD_compressBound(srcSize) is guaranteed to be large enough. 17362306a36Sopenharmony_ci * @src: The data to compress. 17462306a36Sopenharmony_ci * @src_size: The size of the data to compress. 17562306a36Sopenharmony_ci * @parameters: The compression parameters to be used. 17662306a36Sopenharmony_ci * 17762306a36Sopenharmony_ci * Return: The compressed size or an error, which can be checked using 17862306a36Sopenharmony_ci * zstd_is_error(). 17962306a36Sopenharmony_ci */ 18062306a36Sopenharmony_cisize_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity, 18162306a36Sopenharmony_ci const void *src, size_t src_size, const zstd_parameters *parameters); 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_ci/* ====== Single-pass Decompression ====== */ 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_citypedef ZSTD_DCtx zstd_dctx; 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ci/** 18862306a36Sopenharmony_ci * zstd_dctx_workspace_bound() - max memory needed to initialize a zstd_dctx 18962306a36Sopenharmony_ci * 19062306a36Sopenharmony_ci * Return: A lower bound on the size of the workspace that is passed to 19162306a36Sopenharmony_ci * zstd_init_dctx(). 19262306a36Sopenharmony_ci */ 19362306a36Sopenharmony_cisize_t zstd_dctx_workspace_bound(void); 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ci/** 19662306a36Sopenharmony_ci * zstd_init_dctx() - initialize a zstd decompression context 19762306a36Sopenharmony_ci * @workspace: The workspace to emplace the context into. It must outlive 19862306a36Sopenharmony_ci * the returned context. 19962306a36Sopenharmony_ci * @workspace_size: The size of workspace. Use zstd_dctx_workspace_bound() to 20062306a36Sopenharmony_ci * determine how large the workspace must be. 20162306a36Sopenharmony_ci * 20262306a36Sopenharmony_ci * Return: A zstd decompression context or NULL on error. 20362306a36Sopenharmony_ci */ 20462306a36Sopenharmony_cizstd_dctx *zstd_init_dctx(void *workspace, size_t workspace_size); 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_ci/** 20762306a36Sopenharmony_ci * zstd_decompress_dctx() - decompress zstd compressed src into dst 20862306a36Sopenharmony_ci * @dctx: The decompression context. 20962306a36Sopenharmony_ci * @dst: The buffer to decompress src into. 21062306a36Sopenharmony_ci * @dst_capacity: The size of the destination buffer. Must be at least as large 21162306a36Sopenharmony_ci * as the decompressed size. If the caller cannot upper bound the 21262306a36Sopenharmony_ci * decompressed size, then it's better to use the streaming API. 21362306a36Sopenharmony_ci * @src: The zstd compressed data to decompress. Multiple concatenated 21462306a36Sopenharmony_ci * frames and skippable frames are allowed. 21562306a36Sopenharmony_ci * @src_size: The exact size of the data to decompress. 21662306a36Sopenharmony_ci * 21762306a36Sopenharmony_ci * Return: The decompressed size or an error, which can be checked using 21862306a36Sopenharmony_ci * zstd_is_error(). 21962306a36Sopenharmony_ci */ 22062306a36Sopenharmony_cisize_t zstd_decompress_dctx(zstd_dctx *dctx, void *dst, size_t dst_capacity, 22162306a36Sopenharmony_ci const void *src, size_t src_size); 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_ci/* ====== Streaming Buffers ====== */ 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci/** 22662306a36Sopenharmony_ci * struct zstd_in_buffer - input buffer for streaming 22762306a36Sopenharmony_ci * @src: Start of the input buffer. 22862306a36Sopenharmony_ci * @size: Size of the input buffer. 22962306a36Sopenharmony_ci * @pos: Position where reading stopped. Will be updated. 23062306a36Sopenharmony_ci * Necessarily 0 <= pos <= size. 23162306a36Sopenharmony_ci * 23262306a36Sopenharmony_ci * See zstd_lib.h. 23362306a36Sopenharmony_ci */ 23462306a36Sopenharmony_citypedef ZSTD_inBuffer zstd_in_buffer; 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_ci/** 23762306a36Sopenharmony_ci * struct zstd_out_buffer - output buffer for streaming 23862306a36Sopenharmony_ci * @dst: Start of the output buffer. 23962306a36Sopenharmony_ci * @size: Size of the output buffer. 24062306a36Sopenharmony_ci * @pos: Position where writing stopped. Will be updated. 24162306a36Sopenharmony_ci * Necessarily 0 <= pos <= size. 24262306a36Sopenharmony_ci * 24362306a36Sopenharmony_ci * See zstd_lib.h. 24462306a36Sopenharmony_ci */ 24562306a36Sopenharmony_citypedef ZSTD_outBuffer zstd_out_buffer; 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_ci/* ====== Streaming Compression ====== */ 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_citypedef ZSTD_CStream zstd_cstream; 25062306a36Sopenharmony_ci 25162306a36Sopenharmony_ci/** 25262306a36Sopenharmony_ci * zstd_cstream_workspace_bound() - memory needed to initialize a zstd_cstream 25362306a36Sopenharmony_ci * @cparams: The compression parameters to be used for compression. 25462306a36Sopenharmony_ci * 25562306a36Sopenharmony_ci * Return: A lower bound on the size of the workspace that is passed to 25662306a36Sopenharmony_ci * zstd_init_cstream(). 25762306a36Sopenharmony_ci */ 25862306a36Sopenharmony_cisize_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams); 25962306a36Sopenharmony_ci 26062306a36Sopenharmony_ci/** 26162306a36Sopenharmony_ci * zstd_init_cstream() - initialize a zstd streaming compression context 26262306a36Sopenharmony_ci * @parameters The zstd parameters to use for compression. 26362306a36Sopenharmony_ci * @pledged_src_size: If params.fParams.contentSizeFlag == 1 then the caller 26462306a36Sopenharmony_ci * must pass the source size (zero means empty source). 26562306a36Sopenharmony_ci * Otherwise, the caller may optionally pass the source 26662306a36Sopenharmony_ci * size, or zero if unknown. 26762306a36Sopenharmony_ci * @workspace: The workspace to emplace the context into. It must outlive 26862306a36Sopenharmony_ci * the returned context. 26962306a36Sopenharmony_ci * @workspace_size: The size of workspace. 27062306a36Sopenharmony_ci * Use zstd_cstream_workspace_bound(params->cparams) to 27162306a36Sopenharmony_ci * determine how large the workspace must be. 27262306a36Sopenharmony_ci * 27362306a36Sopenharmony_ci * Return: The zstd streaming compression context or NULL on error. 27462306a36Sopenharmony_ci */ 27562306a36Sopenharmony_cizstd_cstream *zstd_init_cstream(const zstd_parameters *parameters, 27662306a36Sopenharmony_ci unsigned long long pledged_src_size, void *workspace, size_t workspace_size); 27762306a36Sopenharmony_ci 27862306a36Sopenharmony_ci/** 27962306a36Sopenharmony_ci * zstd_reset_cstream() - reset the context using parameters from creation 28062306a36Sopenharmony_ci * @cstream: The zstd streaming compression context to reset. 28162306a36Sopenharmony_ci * @pledged_src_size: Optionally the source size, or zero if unknown. 28262306a36Sopenharmony_ci * 28362306a36Sopenharmony_ci * Resets the context using the parameters from creation. Skips dictionary 28462306a36Sopenharmony_ci * loading, since it can be reused. If `pledged_src_size` is non-zero the frame 28562306a36Sopenharmony_ci * content size is always written into the frame header. 28662306a36Sopenharmony_ci * 28762306a36Sopenharmony_ci * Return: Zero or an error, which can be checked using 28862306a36Sopenharmony_ci * zstd_is_error(). 28962306a36Sopenharmony_ci */ 29062306a36Sopenharmony_cisize_t zstd_reset_cstream(zstd_cstream *cstream, 29162306a36Sopenharmony_ci unsigned long long pledged_src_size); 29262306a36Sopenharmony_ci 29362306a36Sopenharmony_ci/** 29462306a36Sopenharmony_ci * zstd_compress_stream() - streaming compress some of input into output 29562306a36Sopenharmony_ci * @cstream: The zstd streaming compression context. 29662306a36Sopenharmony_ci * @output: Destination buffer. `output->pos` is updated to indicate how much 29762306a36Sopenharmony_ci * compressed data was written. 29862306a36Sopenharmony_ci * @input: Source buffer. `input->pos` is updated to indicate how much data 29962306a36Sopenharmony_ci * was read. Note that it may not consume the entire input, in which 30062306a36Sopenharmony_ci * case `input->pos < input->size`, and it's up to the caller to 30162306a36Sopenharmony_ci * present remaining data again. 30262306a36Sopenharmony_ci * 30362306a36Sopenharmony_ci * The `input` and `output` buffers may be any size. Guaranteed to make some 30462306a36Sopenharmony_ci * forward progress if `input` and `output` are not empty. 30562306a36Sopenharmony_ci * 30662306a36Sopenharmony_ci * Return: A hint for the number of bytes to use as the input for the next 30762306a36Sopenharmony_ci * function call or an error, which can be checked using 30862306a36Sopenharmony_ci * zstd_is_error(). 30962306a36Sopenharmony_ci */ 31062306a36Sopenharmony_cisize_t zstd_compress_stream(zstd_cstream *cstream, zstd_out_buffer *output, 31162306a36Sopenharmony_ci zstd_in_buffer *input); 31262306a36Sopenharmony_ci 31362306a36Sopenharmony_ci/** 31462306a36Sopenharmony_ci * zstd_flush_stream() - flush internal buffers into output 31562306a36Sopenharmony_ci * @cstream: The zstd streaming compression context. 31662306a36Sopenharmony_ci * @output: Destination buffer. `output->pos` is updated to indicate how much 31762306a36Sopenharmony_ci * compressed data was written. 31862306a36Sopenharmony_ci * 31962306a36Sopenharmony_ci * zstd_flush_stream() must be called until it returns 0, meaning all the data 32062306a36Sopenharmony_ci * has been flushed. Since zstd_flush_stream() causes a block to be ended, 32162306a36Sopenharmony_ci * calling it too often will degrade the compression ratio. 32262306a36Sopenharmony_ci * 32362306a36Sopenharmony_ci * Return: The number of bytes still present within internal buffers or an 32462306a36Sopenharmony_ci * error, which can be checked using zstd_is_error(). 32562306a36Sopenharmony_ci */ 32662306a36Sopenharmony_cisize_t zstd_flush_stream(zstd_cstream *cstream, zstd_out_buffer *output); 32762306a36Sopenharmony_ci 32862306a36Sopenharmony_ci/** 32962306a36Sopenharmony_ci * zstd_end_stream() - flush internal buffers into output and end the frame 33062306a36Sopenharmony_ci * @cstream: The zstd streaming compression context. 33162306a36Sopenharmony_ci * @output: Destination buffer. `output->pos` is updated to indicate how much 33262306a36Sopenharmony_ci * compressed data was written. 33362306a36Sopenharmony_ci * 33462306a36Sopenharmony_ci * zstd_end_stream() must be called until it returns 0, meaning all the data has 33562306a36Sopenharmony_ci * been flushed and the frame epilogue has been written. 33662306a36Sopenharmony_ci * 33762306a36Sopenharmony_ci * Return: The number of bytes still present within internal buffers or an 33862306a36Sopenharmony_ci * error, which can be checked using zstd_is_error(). 33962306a36Sopenharmony_ci */ 34062306a36Sopenharmony_cisize_t zstd_end_stream(zstd_cstream *cstream, zstd_out_buffer *output); 34162306a36Sopenharmony_ci 34262306a36Sopenharmony_ci/* ====== Streaming Decompression ====== */ 34362306a36Sopenharmony_ci 34462306a36Sopenharmony_citypedef ZSTD_DStream zstd_dstream; 34562306a36Sopenharmony_ci 34662306a36Sopenharmony_ci/** 34762306a36Sopenharmony_ci * zstd_dstream_workspace_bound() - memory needed to initialize a zstd_dstream 34862306a36Sopenharmony_ci * @max_window_size: The maximum window size allowed for compressed frames. 34962306a36Sopenharmony_ci * 35062306a36Sopenharmony_ci * Return: A lower bound on the size of the workspace that is passed 35162306a36Sopenharmony_ci * to zstd_init_dstream(). 35262306a36Sopenharmony_ci */ 35362306a36Sopenharmony_cisize_t zstd_dstream_workspace_bound(size_t max_window_size); 35462306a36Sopenharmony_ci 35562306a36Sopenharmony_ci/** 35662306a36Sopenharmony_ci * zstd_init_dstream() - initialize a zstd streaming decompression context 35762306a36Sopenharmony_ci * @max_window_size: The maximum window size allowed for compressed frames. 35862306a36Sopenharmony_ci * @workspace: The workspace to emplace the context into. It must outlive 35962306a36Sopenharmony_ci * the returned context. 36062306a36Sopenharmony_ci * @workspaceSize: The size of workspace. 36162306a36Sopenharmony_ci * Use zstd_dstream_workspace_bound(max_window_size) to 36262306a36Sopenharmony_ci * determine how large the workspace must be. 36362306a36Sopenharmony_ci * 36462306a36Sopenharmony_ci * Return: The zstd streaming decompression context. 36562306a36Sopenharmony_ci */ 36662306a36Sopenharmony_cizstd_dstream *zstd_init_dstream(size_t max_window_size, void *workspace, 36762306a36Sopenharmony_ci size_t workspace_size); 36862306a36Sopenharmony_ci 36962306a36Sopenharmony_ci/** 37062306a36Sopenharmony_ci * zstd_reset_dstream() - reset the context using parameters from creation 37162306a36Sopenharmony_ci * @dstream: The zstd streaming decompression context to reset. 37262306a36Sopenharmony_ci * 37362306a36Sopenharmony_ci * Resets the context using the parameters from creation. Skips dictionary 37462306a36Sopenharmony_ci * loading, since it can be reused. 37562306a36Sopenharmony_ci * 37662306a36Sopenharmony_ci * Return: Zero or an error, which can be checked using zstd_is_error(). 37762306a36Sopenharmony_ci */ 37862306a36Sopenharmony_cisize_t zstd_reset_dstream(zstd_dstream *dstream); 37962306a36Sopenharmony_ci 38062306a36Sopenharmony_ci/** 38162306a36Sopenharmony_ci * zstd_decompress_stream() - streaming decompress some of input into output 38262306a36Sopenharmony_ci * @dstream: The zstd streaming decompression context. 38362306a36Sopenharmony_ci * @output: Destination buffer. `output.pos` is updated to indicate how much 38462306a36Sopenharmony_ci * decompressed data was written. 38562306a36Sopenharmony_ci * @input: Source buffer. `input.pos` is updated to indicate how much data was 38662306a36Sopenharmony_ci * read. Note that it may not consume the entire input, in which case 38762306a36Sopenharmony_ci * `input.pos < input.size`, and it's up to the caller to present 38862306a36Sopenharmony_ci * remaining data again. 38962306a36Sopenharmony_ci * 39062306a36Sopenharmony_ci * The `input` and `output` buffers may be any size. Guaranteed to make some 39162306a36Sopenharmony_ci * forward progress if `input` and `output` are not empty. 39262306a36Sopenharmony_ci * zstd_decompress_stream() will not consume the last byte of the frame until 39362306a36Sopenharmony_ci * the entire frame is flushed. 39462306a36Sopenharmony_ci * 39562306a36Sopenharmony_ci * Return: Returns 0 iff a frame is completely decoded and fully flushed. 39662306a36Sopenharmony_ci * Otherwise returns a hint for the number of bytes to use as the 39762306a36Sopenharmony_ci * input for the next function call or an error, which can be checked 39862306a36Sopenharmony_ci * using zstd_is_error(). The size hint will never load more than the 39962306a36Sopenharmony_ci * frame. 40062306a36Sopenharmony_ci */ 40162306a36Sopenharmony_cisize_t zstd_decompress_stream(zstd_dstream *dstream, zstd_out_buffer *output, 40262306a36Sopenharmony_ci zstd_in_buffer *input); 40362306a36Sopenharmony_ci 40462306a36Sopenharmony_ci/* ====== Frame Inspection Functions ====== */ 40562306a36Sopenharmony_ci 40662306a36Sopenharmony_ci/** 40762306a36Sopenharmony_ci * zstd_find_frame_compressed_size() - returns the size of a compressed frame 40862306a36Sopenharmony_ci * @src: Source buffer. It should point to the start of a zstd encoded 40962306a36Sopenharmony_ci * frame or a skippable frame. 41062306a36Sopenharmony_ci * @src_size: The size of the source buffer. It must be at least as large as the 41162306a36Sopenharmony_ci * size of the frame. 41262306a36Sopenharmony_ci * 41362306a36Sopenharmony_ci * Return: The compressed size of the frame pointed to by `src` or an error, 41462306a36Sopenharmony_ci * which can be check with zstd_is_error(). 41562306a36Sopenharmony_ci * Suitable to pass to ZSTD_decompress() or similar functions. 41662306a36Sopenharmony_ci */ 41762306a36Sopenharmony_cisize_t zstd_find_frame_compressed_size(const void *src, size_t src_size); 41862306a36Sopenharmony_ci 41962306a36Sopenharmony_ci/** 42062306a36Sopenharmony_ci * struct zstd_frame_params - zstd frame parameters stored in the frame header 42162306a36Sopenharmony_ci * @frameContentSize: The frame content size, or ZSTD_CONTENTSIZE_UNKNOWN if not 42262306a36Sopenharmony_ci * present. 42362306a36Sopenharmony_ci * @windowSize: The window size, or 0 if the frame is a skippable frame. 42462306a36Sopenharmony_ci * @blockSizeMax: The maximum block size. 42562306a36Sopenharmony_ci * @frameType: The frame type (zstd or skippable) 42662306a36Sopenharmony_ci * @headerSize: The size of the frame header. 42762306a36Sopenharmony_ci * @dictID: The dictionary id, or 0 if not present. 42862306a36Sopenharmony_ci * @checksumFlag: Whether a checksum was used. 42962306a36Sopenharmony_ci * 43062306a36Sopenharmony_ci * See zstd_lib.h. 43162306a36Sopenharmony_ci */ 43262306a36Sopenharmony_citypedef ZSTD_frameHeader zstd_frame_header; 43362306a36Sopenharmony_ci 43462306a36Sopenharmony_ci/** 43562306a36Sopenharmony_ci * zstd_get_frame_header() - extracts parameters from a zstd or skippable frame 43662306a36Sopenharmony_ci * @params: On success the frame parameters are written here. 43762306a36Sopenharmony_ci * @src: The source buffer. It must point to a zstd or skippable frame. 43862306a36Sopenharmony_ci * @src_size: The size of the source buffer. 43962306a36Sopenharmony_ci * 44062306a36Sopenharmony_ci * Return: 0 on success. If more data is required it returns how many bytes 44162306a36Sopenharmony_ci * must be provided to make forward progress. Otherwise it returns 44262306a36Sopenharmony_ci * an error, which can be checked using zstd_is_error(). 44362306a36Sopenharmony_ci */ 44462306a36Sopenharmony_cisize_t zstd_get_frame_header(zstd_frame_header *params, const void *src, 44562306a36Sopenharmony_ci size_t src_size); 44662306a36Sopenharmony_ci 44762306a36Sopenharmony_ci#endif /* LINUX_ZSTD_H */ 448