Write procmime_get_part_as_inputstream()
authorAndrej Kacian <ticho@claws-mail.org>
Tue, 5 Mar 2019 21:59:10 +0000 (22:59 +0100)
committerAndrej Kacian <ticho@claws-mail.org>
Tue, 5 Mar 2019 21:59:10 +0000 (22:59 +0100)
We also change procmime_get_part_as_string() to return
a void* instead of a gchar*, and add an option to not
null-terminate the returned buffer.

src/procmime.c
src/procmime.h

index e18215f..6c682a6 100644 (file)
@@ -2662,7 +2662,8 @@ gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
        return base;
 }
 
-gchar *procmime_get_part_as_string(MimeInfo *mimeinfo)
+void *procmime_get_part_as_string(MimeInfo *mimeinfo,
+               gboolean null_terminate)
 {
        FILE *infp;
        gchar *data;
@@ -2689,7 +2690,8 @@ gchar *procmime_get_part_as_string(MimeInfo *mimeinfo)
        }
 
        length = mimeinfo->length;
-       data = g_malloc(length + 1);
+
+       data = g_malloc(null_terminate ? length + 1 : length);
        cm_return_val_if_fail(data != NULL, NULL);
 
        readlength = claws_fread(data, length, 1, infp);
@@ -2702,8 +2704,32 @@ gchar *procmime_get_part_as_string(MimeInfo *mimeinfo)
 
        claws_fclose(infp);
 
-       data[length] = '\0';
+       if (null_terminate)
+               data[length] = '\0';
 
        return data;
 }
 
+/* Returns an open GInputStream. The caller should just
+ * read mimeinfo->length bytes from it and then release it. */
+GInputStream *procmime_get_part_as_inputstream(MimeInfo *mimeinfo,
+               GError **error)
+{
+       cm_return_val_if_fail(mimeinfo != NULL, NULL);
+
+       if (mimeinfo->encoding_type != ENC_BINARY &&
+                       !procmime_decode_content(mimeinfo))
+               return NULL;
+
+       if (mimeinfo->content == MIMECONTENT_MEM) {
+               /* NULL for destroy func, since we're not copying
+                * the data for the stream. */
+               return g_memory_input_stream_new_from_data(
+                               mimeinfo->data.mem,
+                               mimeinfo->length, NULL);
+       } else {
+               return g_memory_input_stream_new_from_data(
+                               procmime_get_part_as_string(mimeinfo, FALSE),
+                               mimeinfo->length, g_free);
+       }
+}
index ae8b0e6..b21dc94 100644 (file)
@@ -24,6 +24,8 @@
 #include "claws-features.h"
 #endif
 
+#include <gio/gio.h>
+
 #include "utils.h"
 #include "proctypes.h"
 typedef enum
@@ -238,7 +240,10 @@ FILE *procmime_get_binary_content(MimeInfo *mimeinfo);
 gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
                gboolean (*scan_callback)(const gchar *str, gpointer cb_data),
                gpointer cb_data);
-gchar *procmime_get_part_as_string(MimeInfo *mimeinfo);
+void *procmime_get_part_as_string(MimeInfo *mimeinfo,
+               gboolean null_terminate);
+GInputStream *procmime_get_part_as_inputstream(MimeInfo *mimeinfo,
+               GError **error);
 
 #ifdef __cplusplus
 }