[PATCH 1/3] util/gmime-extra: add g_mime_stream_gzfile_{new, open}
David Bremner
david at tethera.net
Sat Mar 30 07:06:31 PDT 2019
These are usable as standard GMime streams, and transparently
decompress gzipped files.
---
util/gmime-extra.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++
util/gmime-extra.h | 8 +++++++
2 files changed, 64 insertions(+)
diff --git a/util/gmime-extra.c b/util/gmime-extra.c
index bc1e3c4d..b3f91c55 100644
--- a/util/gmime-extra.c
+++ b/util/gmime-extra.c
@@ -1,6 +1,62 @@
#include "gmime-extra.h"
#include <string.h>
+static
+GMimeStream *
+_gzfile_maybe_filter (GMimeStream *file_stream) {
+ char buf[4];
+ int bytes_read;
+
+ if ((bytes_read = g_mime_stream_read (file_stream, buf, sizeof (buf))) < 0)
+ return NULL;
+
+ if (g_mime_stream_reset (file_stream))
+ return NULL;
+
+ /* check for gzipped input */
+ if (bytes_read >= 2 && buf[0] == 0x1f && (unsigned char)buf[1] == 0x8b) {
+ GMimeStream *gzstream;
+ GMimeFilter *gzfilter;
+
+ gzfilter = g_mime_filter_gzip_new (GMIME_FILTER_GZIP_MODE_UNZIP, 0);
+ if (! gzfilter)
+ return NULL;
+
+ gzstream = g_mime_stream_filter_new (file_stream);
+ if (! gzstream)
+ return NULL;
+
+ g_mime_stream_filter_add ((GMimeStreamFilter *)gzstream, gzfilter);
+ return gzstream;
+ } else {
+ return file_stream;
+ }
+}
+
+GMimeStream *
+g_mime_stream_gzfile_new (int fd)
+{
+ GMimeStream *file_stream;
+
+ file_stream = g_mime_stream_fs_new (fd);
+ if (! file_stream)
+ return NULL;
+
+ return _gzfile_maybe_filter (file_stream);
+}
+
+GMimeStream *
+g_mime_stream_gzfile_open (const char *filename)
+{
+ GMimeStream *file_stream;
+
+ file_stream = g_mime_stream_fs_open (filename, 0, 0, NULL);
+ if (! file_stream)
+ return NULL;
+
+ return _gzfile_maybe_filter (file_stream);
+}
+
GMimeStream *
g_mime_stream_stdout_new()
{
diff --git a/util/gmime-extra.h b/util/gmime-extra.h
index 5d8c52f7..cb8f5e33 100644
--- a/util/gmime-extra.h
+++ b/util/gmime-extra.h
@@ -9,6 +9,14 @@ extern "C" {
GMimeStream *g_mime_stream_stdout_new(void);
+/* Return a GMime stream for this open file descriptor, un-gzipping if
+ * necessary */
+GMimeStream *g_mime_stream_gzfile_new (int fd);
+
+/* Return a GMime stream for this path, un-gzipping if
+ * necessary */
+GMimeStream *g_mime_stream_gzfile_open (const char *filename);
+
#if (GMIME_MAJOR_VERSION < 3)
#define GMIME_ADDRESS_TYPE_TO GMIME_RECIPIENT_TYPE_TO
--
2.20.1
More information about the notmuch
mailing list