]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
compress: introduce compression_supported() helper function
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 4 Apr 2023 05:34:32 +0000 (14:34 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 4 Apr 2023 09:32:12 +0000 (18:32 +0900)
src/basic/compress.c
src/basic/compress.h

index 0a330ecb55137d1e92b855f3066443ebe45dc0de..59621dc05ba069b1d880181231abfdf0767f517b 100644 (file)
@@ -65,6 +65,16 @@ static const char* const compression_table[_COMPRESSION_MAX] = {
 
 DEFINE_STRING_TABLE_LOOKUP(compression, Compression);
 
+bool compression_supported(Compression c) {
+        static const unsigned supported =
+                (1U << COMPRESSION_NONE) |
+                (1U << COMPRESSION_XZ) * HAVE_XZ |
+                (1U << COMPRESSION_LZ4) * HAVE_LZ4 |
+                (1U << COMPRESSION_ZSTD) * HAVE_ZSTD;
+
+        return c >= 0 && c < _COMPRESSION_MAX && FLAGS_SET(supported, 1U << c);
+}
+
 int compress_blob_xz(const void *src, uint64_t src_size,
                      void *dst, size_t dst_alloc_size, size_t *dst_size) {
 #if HAVE_XZ
index 583b105c66fdeda841f8f096bcb9c6df0ce2028a..2201bca74ca0297df067a99f77cc386fad95ed6d 100644 (file)
@@ -2,6 +2,7 @@
 #pragma once
 
 #include <errno.h>
+#include <stdbool.h>
 #include <stdint.h>
 #include <unistd.h>
 
@@ -17,6 +18,8 @@ typedef enum Compression {
 const char* compression_to_string(Compression compression);
 Compression compression_from_string(const char *compression);
 
+bool compression_supported(Compression c);
+
 int compress_blob_xz(const void *src, uint64_t src_size,
                      void *dst, size_t dst_alloc_size, size_t *dst_size);
 int compress_blob_lz4(const void *src, uint64_t src_size,