Debian Codegraph @

src/basic/compress.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "basic-forward.h"
5
6 typedef enum Compression {
7 COMPRESSION_NONE,
8 COMPRESSION_XZ,
9 COMPRESSION_LZ4,
10 COMPRESSION_ZSTD,
11 COMPRESSION_GZIP,
12 COMPRESSION_BZIP2,
13 _COMPRESSION_MAX,
14 _COMPRESSION_INVALID = -EINVAL,
15 } Compression;
16
17 DECLARE_STRING_TABLE_LOOKUP(compression, Compression);
18 DECLARE_STRING_TABLE_LOOKUP(compression_uppercase, Compression);
19 DECLARE_STRING_TABLE_LOOKUP(compression_extension, Compression);
20
21 /* Try the lowercase string table first, fall back to the uppercase one. Useful for parsing user input
22 * where both forms (e.g. "xz" and "XZ") have historically been accepted. */
23 Compression compression_from_string_harder(const char *s);
24
25 /* Derives the compression type from a filename's extension, defaulting to COMPRESSION_NONE if the
26 * filename does not carry a recognized compression suffix. */
27 Compression compression_from_filename(const char *filename);
28
29 bool compression_supported(Compression c);
30
31 /* Buffer size used by streaming compression APIs and pipeline stages that feed into them. Sized to
32 * match the typical Linux pipe buffer so that pipeline stages don't lose throughput due to small
33 * intermediate buffers. */
34 #define COMPRESS_PIPE_BUFFER_SIZE (128U*1024U)
35
36 #define COMPRESSION_MAGIC_BYTES_MAX 6U
37 Compression compression_detect_from_magic(const uint8_t data[static COMPRESSION_MAGIC_BYTES_MAX]);
38
39 /* Compressor / Decompressor — opaque push-based streaming compression context */
40
41 typedef struct Compressor Compressor;
42 typedef Compressor Decompressor;
43
44 typedef int (*DecompressorCallback)(const void *data, size_t size, void *userdata);
45
46 Compressor* compressor_free(Compressor *c);
47 DEFINE_TRIVIAL_CLEANUP_FUNC(Compressor*, compressor_free);
48
49 int compressor_new(Compressor **ret, Compression type);
50 int compressor_start(Compressor *c, const void *data, size_t size, void **buffer, size_t *buffer_size, size_t *buffer_allocated);
51 int compressor_finish(Compressor *c, void **buffer, size_t *buffer_size, size_t *buffer_allocated);
52
53 int decompressor_detect(Decompressor **ret, const void *data, size_t size);
54 int decompressor_force_off(Decompressor **ret);
55 int decompressor_push(Decompressor *c, const void *data, size_t size, DecompressorCallback callback, void *userdata);
56
57 Compression compressor_type(const Compressor *c);
58
59 /* Blob compression/decompression */
60
61 int compress_blob(Compression compression,
62 const void *src, uint64_t src_size,
63 void *dst, size_t dst_alloc_size, size_t *dst_size, int level);
64 int decompress_blob(Compression compression,
65 const void *src, uint64_t src_size,
66 void **dst, size_t *dst_size, size_t dst_max);
67
68 int decompress_zlib_raw(const void *src, uint64_t src_size,
69 void *dst, size_t dst_size, int wbits);
70
71 int decompress_startswith(Compression compression,
72 const void *src, uint64_t src_size,
73 void **buffer,
74 const void *prefix, size_t prefix_len,
75 uint8_t extra);
76
77 /* Stream compression/decompression (fd-to-fd) */
78
79 int compress_stream(Compression type, int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size);
80 int decompress_stream(Compression type, int fdf, int fdt, uint64_t max_bytes);
81 int decompress_stream_by_filename(const char *filename, int fdf, int fdt, uint64_t max_bytes);
82
83 int dlopen_xz(int log_level);
84 int dlopen_lz4(int log_level);
85 int dlopen_zstd(int log_level);
86 int dlopen_zlib(int log_level);
87 int dlopen_bzip2(int log_level);
88
89 static inline const char* default_compression_extension(void) {
90 return compression_extension_to_string(DEFAULT_COMPRESSION) ?: "";
91 }