github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/compress/libdeflate/libdeflate.h (about) 1 /* 2 * libdeflate.h - public header for libdeflate 3 */ 4 5 #ifndef LIBDEFLATE_H 6 #define LIBDEFLATE_H 7 8 #ifdef __cplusplus 9 extern "C" { 10 #endif 11 12 #define LIBDEFLATE_VERSION_MAJOR 1 13 #define LIBDEFLATE_VERSION_MINOR 0 14 #define LIBDEFLATE_VERSION_STRING "1.0" 15 16 #include <stddef.h> 17 #include <stdint.h> 18 19 /* 20 * On Windows, if you want to link to the DLL version of libdeflate, then 21 * #define LIBDEFLATE_DLL. Note that the calling convention is cdecl. 22 */ 23 #ifdef LIBDEFLATE_DLL 24 # ifdef BUILDING_LIBDEFLATE 25 # define LIBDEFLATEAPI LIBEXPORT 26 # elif defined(_WIN32) || defined(__CYGWIN__) 27 # define LIBDEFLATEAPI __declspec(dllimport) 28 # endif 29 #endif 30 #ifndef LIBDEFLATEAPI 31 # define LIBDEFLATEAPI 32 #endif 33 34 /* ========================================================================== */ 35 /* Compression */ 36 /* ========================================================================== */ 37 38 struct libdeflate_compressor; 39 40 /* 41 * libdeflate_alloc_compressor() allocates a new compressor that supports 42 * DEFLATE, zlib, and gzip compression. 'compression_level' is the compression 43 * level on a zlib-like scale but with a higher maximum value (1 = fastest, 6 = 44 * medium/default, 9 = slow, 12 = slowest). The return value is a pointer to 45 * the new compressor, or NULL if out of memory. 46 * 47 * Note: for compression, the sliding window size is defined at compilation time 48 * to 32768, the largest size permissible in the DEFLATE format. It cannot be 49 * changed at runtime. 50 * 51 * A single compressor is not safe to use by multiple threads concurrently. 52 * However, different threads may use different compressors concurrently. 53 */ 54 LIBDEFLATEAPI struct libdeflate_compressor * 55 libdeflate_alloc_compressor(int compression_level); 56 57 /* 58 * libdeflate_deflate_compress() performs raw DEFLATE compression on a buffer of 59 * data. The function attempts to compress 'in_nbytes' bytes of data located at 60 * 'in' and write the results to 'out', which has space for 'out_nbytes_avail' 61 * bytes. The return value is the compressed size in bytes, or 0 if the data 62 * could not be compressed to 'out_nbytes_avail' bytes or fewer. 63 */ 64 LIBDEFLATEAPI size_t 65 libdeflate_deflate_compress(struct libdeflate_compressor *compressor, 66 const void *in, size_t in_nbytes, 67 void *out, size_t out_nbytes_avail); 68 69 /* 70 * libdeflate_deflate_compress_bound() returns a worst-case upper bound on the 71 * number of bytes of compressed data that may be produced by compressing any 72 * buffer of length less than or equal to 'in_nbytes' using 73 * libdeflate_deflate_compress() with the specified compressor. Mathematically, 74 * this bound will necessarily be a number greater than or equal to 'in_nbytes'. 75 * It may be an overestimate of the true upper bound. The return value is 76 * guaranteed to be the same for all invocations with the same compressor and 77 * same 'in_nbytes'. 78 * 79 * As a special case, 'compressor' may be NULL. This causes the bound to be 80 * taken across *any* libdeflate_compressor that could ever be allocated with 81 * this build of the library, with any options. 82 * 83 * Note that this function is not necessary in many applications. With 84 * block-based compression, it is usually preferable to separately store the 85 * uncompressed size of each block and to store any blocks that did not compress 86 * to less than their original size uncompressed. In that scenario, there is no 87 * need to know the worst-case compressed size, since the maximum number of 88 * bytes of compressed data that may be used would always be one less than the 89 * input length. You can just pass a buffer of that size to 90 * libdeflate_deflate_compress() and store the data uncompressed if 91 * libdeflate_deflate_compress() returns 0, indicating that the compressed data 92 * did not fit into the provided output buffer. 93 */ 94 LIBDEFLATEAPI size_t 95 libdeflate_deflate_compress_bound(struct libdeflate_compressor *compressor, 96 size_t in_nbytes); 97 98 /* 99 * Like libdeflate_deflate_compress(), but stores the data in the zlib wrapper 100 * format. 101 */ 102 LIBDEFLATEAPI size_t 103 libdeflate_zlib_compress(struct libdeflate_compressor *compressor, 104 const void *in, size_t in_nbytes, 105 void *out, size_t out_nbytes_avail); 106 107 /* 108 * Like libdeflate_deflate_compress_bound(), but assumes the data will be 109 * compressed with libdeflate_zlib_compress() rather than with 110 * libdeflate_deflate_compress(). 111 */ 112 LIBDEFLATEAPI size_t 113 libdeflate_zlib_compress_bound(struct libdeflate_compressor *compressor, 114 size_t in_nbytes); 115 116 /* 117 * Like libdeflate_deflate_compress(), but stores the data in the gzip wrapper 118 * format. 119 */ 120 LIBDEFLATEAPI size_t 121 libdeflate_gzip_compress(struct libdeflate_compressor *compressor, 122 const void *in, size_t in_nbytes, 123 void *out, size_t out_nbytes_avail); 124 125 /* 126 * Like libdeflate_deflate_compress_bound(), but assumes the data will be 127 * compressed with libdeflate_gzip_compress() rather than with 128 * libdeflate_deflate_compress(). 129 */ 130 LIBDEFLATEAPI size_t 131 libdeflate_gzip_compress_bound(struct libdeflate_compressor *compressor, 132 size_t in_nbytes); 133 134 /* 135 * libdeflate_free_compressor() frees a compressor that was allocated with 136 * libdeflate_alloc_compressor(). If a NULL pointer is passed in, no action is 137 * taken. 138 */ 139 LIBDEFLATEAPI void 140 libdeflate_free_compressor(struct libdeflate_compressor *compressor); 141 142 /* ========================================================================== */ 143 /* Decompression */ 144 /* ========================================================================== */ 145 146 struct libdeflate_decompressor; 147 148 /* 149 * libdeflate_alloc_decompressor() allocates a new decompressor that can be used 150 * for DEFLATE, zlib, and gzip decompression. The return value is a pointer to 151 * the new decompressor, or NULL if out of memory. 152 * 153 * This function takes no parameters, and the returned decompressor is valid for 154 * decompressing data that was compressed at any compression level and with any 155 * sliding window size. 156 * 157 * A single decompressor is not safe to use by multiple threads concurrently. 158 * However, different threads may use different decompressors concurrently. 159 */ 160 LIBDEFLATEAPI struct libdeflate_decompressor * 161 libdeflate_alloc_decompressor(void); 162 163 /* 164 * Result of a call to libdeflate_deflate_decompress(), 165 * libdeflate_zlib_decompress(), or libdeflate_gzip_decompress(). 166 */ 167 enum libdeflate_result { 168 /* Decompression was successful. */ 169 LIBDEFLATE_SUCCESS = 0, 170 171 /* Decompressed failed because the compressed data was invalid, corrupt, 172 * or otherwise unsupported. */ 173 LIBDEFLATE_BAD_DATA = 1, 174 175 /* A NULL 'actual_out_nbytes_ret' was provided, but the data would have 176 * decompressed to fewer than 'out_nbytes_avail' bytes. */ 177 LIBDEFLATE_SHORT_OUTPUT = 2, 178 179 /* The data would have decompressed to more than 'out_nbytes_avail' 180 * bytes. */ 181 LIBDEFLATE_INSUFFICIENT_SPACE = 3, 182 }; 183 184 /* 185 * libdeflate_deflate_decompress() decompresses the DEFLATE-compressed stream 186 * from the buffer 'in' with compressed size up to 'in_nbytes' bytes. The 187 * uncompressed data is written to 'out', a buffer with size 'out_nbytes_avail' 188 * bytes. If decompression succeeds, then 0 (LIBDEFLATE_SUCCESS) is returned. 189 * Otherwise, a nonzero result code such as LIBDEFLATE_BAD_DATA is returned. If 190 * a nonzero result code is returned, then the contents of the output buffer are 191 * undefined. 192 * 193 * Decompression stops at the end of the DEFLATE stream (as indicated by the 194 * BFINAL flag), even if it is actually shorter than 'in_nbytes' bytes. 195 * 196 * libdeflate_deflate_decompress() can be used in cases where the actual 197 * uncompressed size is known (recommended) or unknown (not recommended): 198 * 199 * - If the actual uncompressed size is known, then pass the actual 200 * uncompressed size as 'out_nbytes_avail' and pass NULL for 201 * 'actual_out_nbytes_ret'. This makes libdeflate_deflate_decompress() fail 202 * with LIBDEFLATE_SHORT_OUTPUT if the data decompressed to fewer than the 203 * specified number of bytes. 204 * 205 * - If the actual uncompressed size is unknown, then provide a non-NULL 206 * 'actual_out_nbytes_ret' and provide a buffer with some size 207 * 'out_nbytes_avail' that you think is large enough to hold all the 208 * uncompressed data. In this case, if the data decompresses to less than 209 * or equal to 'out_nbytes_avail' bytes, then 210 * libdeflate_deflate_decompress() will write the actual uncompressed size 211 * to *actual_out_nbytes_ret and return 0 (LIBDEFLATE_SUCCESS). Otherwise, 212 * it will return LIBDEFLATE_INSUFFICIENT_SPACE if the provided buffer was 213 * not large enough but no other problems were encountered, or another 214 * nonzero result code if decompression failed for another reason. 215 */ 216 LIBDEFLATEAPI enum libdeflate_result 217 libdeflate_deflate_decompress(struct libdeflate_decompressor *decompressor, 218 const void *in, size_t in_nbytes, 219 void *out, size_t out_nbytes_avail, 220 size_t *actual_out_nbytes_ret); 221 222 /* 223 * Like libdeflate_deflate_decompress(), but adds the 'actual_in_nbytes_ret' 224 * argument. If decompression succeeds and 'actual_in_nbytes_ret' is not NULL, 225 * then the actual compressed size of the DEFLATE stream (aligned to the next 226 * byte boundary) is written to *actual_in_nbytes_ret. 227 */ 228 LIBDEFLATEAPI enum libdeflate_result 229 libdeflate_deflate_decompress_ex(struct libdeflate_decompressor *decompressor, 230 const void *in, size_t in_nbytes, 231 void *out, size_t out_nbytes_avail, 232 size_t *actual_in_nbytes_ret, 233 size_t *actual_out_nbytes_ret); 234 235 /* 236 * Like libdeflate_deflate_decompress(), but assumes the zlib wrapper format 237 * instead of raw DEFLATE. 238 */ 239 LIBDEFLATEAPI enum libdeflate_result 240 libdeflate_zlib_decompress(struct libdeflate_decompressor *decompressor, 241 const void *in, size_t in_nbytes, 242 void *out, size_t out_nbytes_avail, 243 size_t *actual_out_nbytes_ret); 244 245 /* 246 * Like libdeflate_deflate_decompress(), but assumes the gzip wrapper format 247 * instead of raw DEFLATE. 248 * 249 * If multiple gzip-compressed members are concatenated, then only the first 250 * will be decompressed. Use libdeflate_gzip_decompress_ex() if you need 251 * multi-member support. 252 */ 253 LIBDEFLATEAPI enum libdeflate_result 254 libdeflate_gzip_decompress(struct libdeflate_decompressor *decompressor, 255 const void *in, size_t in_nbytes, 256 void *out, size_t out_nbytes_avail, 257 size_t *actual_out_nbytes_ret); 258 259 /* 260 * Like libdeflate_gzip_decompress(), but adds the 'actual_in_nbytes_ret' 261 * argument. If 'actual_in_nbytes_ret' is not NULL and the decompression 262 * succeeds (indicating that the first gzip-compressed member in the input 263 * buffer was decompressed), then the actual number of input bytes consumed is 264 * written to *actual_in_nbytes_ret. 265 */ 266 LIBDEFLATEAPI enum libdeflate_result 267 libdeflate_gzip_decompress_ex(struct libdeflate_decompressor *decompressor, 268 const void *in, size_t in_nbytes, 269 void *out, size_t out_nbytes_avail, 270 size_t *actual_in_nbytes_ret, 271 size_t *actual_out_nbytes_ret); 272 273 /* 274 * libdeflate_free_decompressor() frees a decompressor that was allocated with 275 * libdeflate_alloc_decompressor(). If a NULL pointer is passed in, no action 276 * is taken. 277 */ 278 LIBDEFLATEAPI void 279 libdeflate_free_decompressor(struct libdeflate_decompressor *decompressor); 280 281 /* ========================================================================== */ 282 /* Checksums */ 283 /* ========================================================================== */ 284 285 /* 286 * libdeflate_adler32() updates a running Adler-32 checksum with 'len' bytes of 287 * data and returns the updated checksum. When starting a new checksum, the 288 * required initial value for 'adler' is 1. This value is also returned when 289 * 'buffer' is specified as NULL. 290 */ 291 LIBDEFLATEAPI uint32_t 292 libdeflate_adler32(uint32_t adler32, const void *buffer, size_t len); 293 294 295 /* 296 * libdeflate_crc32() updates a running CRC-32 checksum with 'len' bytes of data 297 * and returns the updated checksum. When starting a new checksum, the required 298 * initial value for 'crc' is 0. This value is also returned when 'buffer' is 299 * specified as NULL. 300 */ 301 LIBDEFLATEAPI uint32_t 302 libdeflate_crc32(uint32_t crc, const void *buffer, size_t len); 303 304 #ifdef __cplusplus 305 } 306 #endif 307 308 #endif /* LIBDEFLATE_H */