[PATCH 3/4] perf: zstd: add streaming decompression and format probe functions

From: Ivan Shapovalov

Date: Wed Oct 14 2026 - 13:42:09 EST


Add an actual streaming zstd decompression (fd->fd) entry point,
plus a file->fd wrapper and a helper to detect zstd magic on a file
for compressions[] in dso.c.

Signed-off-by: Ivan Shapovalov <intelfx@xxxxxxxxxxxx>
---
tools/perf/util/compress.h | 18 ++++++++
tools/perf/util/zstd.c | 104 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 122 insertions(+)

diff --git a/tools/perf/util/compress.h b/tools/perf/util/compress.h
index e7aa89d31fc6f..fd6b541358bb6 100644
--- a/tools/perf/util/compress.h
+++ b/tools/perf/util/compress.h
@@ -59,6 +59,9 @@ ssize_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_

size_t zstd_decompress_buffer(struct zstd_data *data, void *src, size_t src_size,
void *dst, size_t dst_size);
+int zstd_decompress_stream(struct zstd_data *data, int input_fd, int output_fd);
+int zstd_decompress_to_fd(const char *input, int output_fd);
+bool zstd_is_compressed(const char *input);
#else /* !HAVE_ZSTD_SUPPORT */

static inline int zstd_init(struct zstd_data *data __maybe_unused, int level __maybe_unused)
@@ -88,6 +91,21 @@ static inline size_t zstd_decompress_buffer(struct zstd_data *data __maybe_unuse
{
return 0;
}
+
+static inline int zstd_decompress_stream(struct zstd_data *data, int input_fd, int output_fd)
+{
+ return -1;
+}
+
+static inline int zstd_decompress_to_fd(const char *input, int output_fd)
+{
+ return -1;
+}
+
+static inline bool zstd_is_compressed(const char *input)
+{
+ return false;
+}
#endif

#endif /* PERF_COMPRESS_H */
diff --git a/tools/perf/util/zstd.c b/tools/perf/util/zstd.c
index 3af8910bb87cf..982655a1f1a51 100644
--- a/tools/perf/util/zstd.c
+++ b/tools/perf/util/zstd.c
@@ -1,7 +1,11 @@
// SPDX-License-Identifier: GPL-2.0

+#include <errno.h>
+#include <fcntl.h>
#include <string.h>
+#include <unistd.h>
#include <linux/perf_event.h>
+#include <internal/lib.h>

#include "util/compress.h"
#include "util/debug.h"
@@ -172,3 +176,103 @@ size_t zstd_decompress_buffer(struct zstd_data *data, void *src, size_t src_size

return output.pos;
}
+
+int zstd_decompress_stream(struct zstd_data *data, int input_fd, int output_fd)
+{
+ ssize_t ret;
+ bool unflushed = false;
+ char buf_in[ZSTD_DStreamInSize()];
+ char buf_out[ZSTD_DStreamOutSize()];
+ ZSTD_inBuffer in = {};
+ ZSTD_outBuffer out = {};
+
+ ret = zstd_init_dstream(data);
+ if (ret < 0)
+ return ret;
+
+ for (;;) {
+ if (in.pos == in.size) {
+ /* out of input, read more */
+ ret = read(input_fd, buf_in, sizeof(buf_in));
+ if (ret < 0) {
+ pr_err("zstd: failed to read() %zu bytes: %s\n",
+ sizeof(buf_in), strerror(errno));
+ return ret;
+ }
+ in = (ZSTD_inBuffer) {
+ .src = &buf_in,
+ .pos = 0,
+ .size = ret,
+ };
+ }
+
+ if (in.pos == in.size && !unflushed) {
+ /* out of input, out of file, and nothing to flush, exit */
+ break;
+ }
+
+ out = (ZSTD_outBuffer) {
+ .dst = &buf_out,
+ .pos = 0,
+ .size = sizeof(buf_out),
+ };
+ ret = ZSTD_decompressStream(data->dstream, &out, &in);
+ if (ZSTD_isError(ret)) {
+ pr_err("zstd: failed to decompress %zu bytes into buffer of %zu bytes (wrote %zu): %s\n",
+ in.size, out.size, out.pos, ZSTD_getErrorName(ret));
+ return -1;
+ }
+ unflushed = ret > 0;
+
+ ret = writen(output_fd, out.dst, out.pos);
+ if (ret < 0) {
+ pr_err("zstd: failed to write() %zu bytes: %s\n",
+ out.pos, strerror(errno));
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+int zstd_decompress_to_fd(const char *input, int output_fd)
+{
+ int input_fd = -1;
+ int ret;
+ struct zstd_data data;
+
+ input_fd = open(input, O_RDONLY);
+ if (input_fd < 0) {
+ pr_err("zstd: failed to open(%s): %s\n", input, strerror(errno));
+ return -1;
+ }
+
+ ret = zstd_init(&data, ZSTD_CLEVEL_DEFAULT);
+ if (ret < 0)
+ goto err;
+
+ ret = zstd_decompress_stream(&data, input_fd, output_fd);
+
+err:
+ zstd_fini(&data);
+ close(input_fd);
+ return ret;
+}
+
+bool zstd_is_compressed(const char *file)
+{
+ int fd = open(file, O_RDONLY);
+ const uint32_t magic_le = htole32(ZSTD_MAGICNUMBER);
+ char buf[4];
+ ssize_t rc;
+
+ if (fd < 0) {
+ pr_err("zstd: failed to open(%s): %s\n", file, strerror(errno));
+ return -1;
+ }
+
+ rc = read(fd, buf, sizeof(buf));
+ close(fd);
+ return rc == sizeof(buf) ?
+ memcmp(buf, (const char *) &magic_le, sizeof(buf)) == 0 : false;
+}

--
2.55.0.8.g37e2a0019f