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