1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <errno.h>
5 #include <stdint.h>
6 #include <unistd.h>
7 
8 typedef enum Compression {
9         COMPRESSION_NONE,
10         COMPRESSION_XZ,
11         COMPRESSION_LZ4,
12         COMPRESSION_ZSTD,
13         _COMPRESSION_MAX,
14         _COMPRESSION_INVALID = -EINVAL,
15 } Compression;
16 
17 const char* compression_to_string(Compression compression);
18 Compression compression_from_string(const char *compression);
19 
20 int compress_blob_xz(const void *src, uint64_t src_size,
21                      void *dst, size_t dst_alloc_size, size_t *dst_size);
22 int compress_blob_lz4(const void *src, uint64_t src_size,
23                       void *dst, size_t dst_alloc_size, size_t *dst_size);
24 int compress_blob_zstd(const void *src, uint64_t src_size,
25                        void *dst, size_t dst_alloc_size, size_t *dst_size);
26 
27 int decompress_blob_xz(const void *src, uint64_t src_size,
28                        void **dst, size_t* dst_size, size_t dst_max);
29 int decompress_blob_lz4(const void *src, uint64_t src_size,
30                         void **dst, size_t* dst_size, size_t dst_max);
31 int decompress_blob_zstd(const void *src, uint64_t src_size,
32                         void **dst, size_t* dst_size, size_t dst_max);
33 int decompress_blob(Compression compression,
34                     const void *src, uint64_t src_size,
35                     void **dst, size_t* dst_size, size_t dst_max);
36 
37 int decompress_startswith_xz(const void *src, uint64_t src_size,
38                              void **buffer,
39                              const void *prefix, size_t prefix_len,
40                              uint8_t extra);
41 int decompress_startswith_lz4(const void *src, uint64_t src_size,
42                               void **buffer,
43                               const void *prefix, size_t prefix_len,
44                               uint8_t extra);
45 int decompress_startswith_zstd(const void *src, uint64_t src_size,
46                                void **buffer,
47                                const void *prefix, size_t prefix_len,
48                                uint8_t extra);
49 int decompress_startswith(Compression compression,
50                           const void *src, uint64_t src_size,
51                           void **buffer,
52                           const void *prefix, size_t prefix_len,
53                           uint8_t extra);
54 
55 int compress_stream_xz(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size);
56 int compress_stream_lz4(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size);
57 int compress_stream_zstd(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size);
58 
59 int decompress_stream_xz(int fdf, int fdt, uint64_t max_size);
60 int decompress_stream_lz4(int fdf, int fdt, uint64_t max_size);
61 int decompress_stream_zstd(int fdf, int fdt, uint64_t max_size);
62 
compress_blob_explicit(Compression compression,const void * src,uint64_t src_size,void * dst,size_t dst_alloc_size,size_t * dst_size)63 static inline int compress_blob_explicit(
64                 Compression compression,
65                 const void *src, uint64_t src_size,
66                 void *dst, size_t dst_alloc_size, size_t *dst_size) {
67 
68         switch (compression) {
69         case COMPRESSION_ZSTD:
70                 return compress_blob_zstd(src, src_size, dst, dst_alloc_size, dst_size);
71         case COMPRESSION_LZ4:
72                 return compress_blob_lz4(src, src_size, dst, dst_alloc_size, dst_size);
73         case COMPRESSION_XZ:
74                 return compress_blob_xz(src, src_size, dst, dst_alloc_size, dst_size);
75         default:
76                 return -EOPNOTSUPP;
77         }
78 }
79 
80 #define compress_blob(src, src_size, dst, dst_alloc_size, dst_size) \
81         compress_blob_explicit(                                     \
82                 DEFAULT_COMPRESSION,                                \
83                 src, src_size,                                      \
84                 dst, dst_alloc_size, dst_size)
85 
compress_stream(int fdf,int fdt,uint64_t max_bytes,uint64_t * ret_uncompressed_size)86 static inline int compress_stream(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size) {
87         switch (DEFAULT_COMPRESSION) {
88         case COMPRESSION_ZSTD:
89                 return compress_stream_zstd(fdf, fdt, max_bytes, ret_uncompressed_size);
90         case COMPRESSION_LZ4:
91                 return compress_stream_lz4(fdf, fdt, max_bytes, ret_uncompressed_size);
92         case COMPRESSION_XZ:
93                 return compress_stream_xz(fdf, fdt, max_bytes, ret_uncompressed_size);
94         default:
95                 return -EOPNOTSUPP;
96         }
97 }
98 
default_compression_extension(void)99 static inline const char* default_compression_extension(void) {
100         switch (DEFAULT_COMPRESSION) {
101         case COMPRESSION_ZSTD:
102                 return ".zst";
103         case COMPRESSION_LZ4:
104                 return ".lz4";
105         case COMPRESSION_XZ:
106                 return ".xz";
107         default:
108                 return "";
109         }
110 }
111 
112 int decompress_stream(const char *filename, int fdf, int fdt, uint64_t max_bytes);
113