From 957cec9843b96c7c484195b8955c78e58cf1b685 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Tue, 30 May 2023 13:19:57 -0700 Subject: [PATCH] libsysprof-analyze: decompress if necessary in dup_bytes() If we are trying to get the file bytes and they are compressed, we may need to transparently decompress those bytes. That way if the API requested "/proc/cpuinfo" but really got "/proc/cpuinfo.gz" it still gets the same bytes to consume. --- src/libsysprof-analyze/sysprof-document-file.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/libsysprof-analyze/sysprof-document-file.c b/src/libsysprof-analyze/sysprof-document-file.c index dd7526c8..2aa39b62 100644 --- a/src/libsysprof-analyze/sysprof-document-file.c +++ b/src/libsysprof-analyze/sysprof-document-file.c @@ -157,6 +157,23 @@ sysprof_document_file_dup_bytes (SysprofDocumentFile *self) len = ar->len; + if (self->compressed) + { + guint8 *data = (guint8 *)g_array_free (ar, FALSE); + g_autoptr(GInputStream) input = g_memory_input_stream_new_from_data (data, len, g_free); + g_autoptr(GOutputStream) memory_output = g_memory_output_stream_new_resizable (); + g_autoptr(GZlibDecompressor) zlib = g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP); + g_autoptr(GOutputStream) zlib_output = g_converter_output_stream_new (memory_output, G_CONVERTER (zlib)); + + g_output_stream_splice (zlib_output, + input, + (G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | + G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET), + NULL, NULL); + + return g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (memory_output)); + } + return g_bytes_new_take (g_array_free (ar, FALSE), len); }