xref: /linux/tools/perf/util/compress.h (revision 4f9786035f9e519db41375818e1d0b5f20da2f10)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef PERF_COMPRESS_H
3 #define PERF_COMPRESS_H
4 
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <linux/compiler.h>
10 #ifdef HAVE_ZSTD_SUPPORT
11 #include <zstd.h>
12 #endif
13 
14 #ifdef HAVE_ZLIB_SUPPORT
15 int gzip_decompress_to_file(const char *input, int output_fd);
16 bool gzip_is_compressed(const char *input);
17 #endif
18 
19 #ifdef HAVE_LZMA_SUPPORT
20 int lzma_decompress_stream_to_file(FILE *input, int output_fd);
21 int lzma_decompress_to_file(const char *input, int output_fd);
22 bool lzma_is_compressed(const char *input);
23 #else
24 static inline
25 int lzma_decompress_stream_to_file(FILE *input __maybe_unused,
26 				   int output_fd __maybe_unused)
27 {
28 	return -1;
29 }
30 static inline
31 int lzma_decompress_to_file(const char *input __maybe_unused,
32 			    int output_fd __maybe_unused)
33 {
34 	return -1;
35 }
36 static inline int lzma_is_compressed(const char *input __maybe_unused)
37 {
38 	return false;
39 }
40 #endif
41 
42 struct zstd_data {
43 #ifdef HAVE_ZSTD_SUPPORT
44 	ZSTD_CStream	*cstream;
45 	ZSTD_DStream	*dstream;
46 	int comp_level;
47 #endif
48 };
49 
50 #ifdef HAVE_ZSTD_SUPPORT
51 
52 int zstd_init(struct zstd_data *data, int level);
53 int zstd_fini(struct zstd_data *data);
54 
55 ssize_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_t dst_size,
56 				       void *src, size_t src_size, size_t max_record_size,
57 				       size_t process_header(void *record, size_t increment));
58 
59 size_t zstd_decompress_stream(struct zstd_data *data, void *src, size_t src_size,
60 			      void *dst, size_t dst_size);
61 #else /* !HAVE_ZSTD_SUPPORT */
62 
63 static inline int zstd_init(struct zstd_data *data __maybe_unused, int level __maybe_unused)
64 {
65 	return 0;
66 }
67 
68 static inline int zstd_fini(struct zstd_data *data __maybe_unused)
69 {
70 	return 0;
71 }
72 
73 static inline
74 ssize_t zstd_compress_stream_to_records(struct zstd_data *data __maybe_unused,
75 				       void *dst __maybe_unused, size_t dst_size __maybe_unused,
76 				       void *src __maybe_unused, size_t src_size __maybe_unused,
77 				       size_t max_record_size __maybe_unused,
78 				       size_t process_header(void *record, size_t increment) __maybe_unused)
79 {
80 	return 0;
81 }
82 
83 static inline size_t zstd_decompress_stream(struct zstd_data *data __maybe_unused, void *src __maybe_unused,
84 					    size_t src_size __maybe_unused, void *dst __maybe_unused,
85 					    size_t dst_size __maybe_unused)
86 {
87 	return 0;
88 }
89 #endif
90 
91 #endif /* PERF_COMPRESS_H */
92