static void display_part(GMimeObject *part, const GMimeContentType *ct) { GMimeStream *ostream, *fstream; GMimeFilter *basic; GMimeDataWrapper *content; GMimeFilter *charset, *html; GMimePartEncodingType encoding; encoding = g_mime_part_get_encoding(GMIME_PART(part)); fstream = g_mime_stream_file_new(stdout); ostream = g_mime_stream_filter_new_with_stream(fstream); g_mime_stream_unref(fstream); /* Encoding filter, always on */ if (charset = g_mime_filter_charset_new(g_mime_content_type_get_parameter(ct, "charset"), "utf-8")) { g_mime_stream_filter_add(GMIME_STREAM_FILTER(ostream), charset); g_object_unref(charset); } if (g_mime_content_type_is_type(ct, "text", "plain")) { if (text_only == 0) { html = g_mime_filter_html_new ( GMIME_FILTER_HTML_CONVERT_SPACES | GMIME_FILTER_HTML_CONVERT_URLS | GMIME_FILTER_HTML_MARK_CITATION | GMIME_FILTER_HTML_CITE, 0); g_mime_stream_filter_add(GMIME_STREAM_FILTER(ostream), html); g_object_unref(html); } content = g_mime_part_get_content_object(GMIME_PART(part)); g_mime_data_wrapper_write_to_stream(content, ostream); g_mime_stream_flush(ostream); g_object_unref(content); // GMimeFilterBasic (base64, quopri) // GMimeFilterCharset // GMimeFilterHTML // GMimeFilterEnriched (text/enriched, text/rtf) } else if (g_mime_content_type_is_type(ct, "text", "html")) { content = g_mime_part_get_content_object(GMIME_PART(part)); g_mime_data_wrapper_write_to_stream(content, ostream); g_mime_stream_flush(ostream); g_object_unref(content); } else if (strcmp(ct->type, "image") == 0) { display_image(part); } }
static void post_raw(GMimePart *part, const char *post_dir, const char *fn) { char filename[PATH_MAX]; GMimeDataWrapper *content; GMimeStream *stream; int fd; snprintf(filename, sizeof(filename), "%s/%s", post_dir, fn); ast_debug(1, "Posting raw data to %s\n", filename); if ((fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666)) == -1) { ast_log(LOG_WARNING, "Unable to open %s for writing file from a POST!\n", filename); return; } stream = g_mime_stream_fs_new(fd); content = g_mime_part_get_content_object(part); g_mime_data_wrapper_write_to_stream(content, stream); g_mime_stream_flush(stream); #ifndef AST_GMIME_VER_24 g_object_unref(content); #endif g_object_unref(stream); }
static void pump_data_through_filter (GMimeFilter *filter, const char *path, GMimeStream *ostream) { GMimeStream *onebyte, *filtered, *stream; GMimeFilter *unix2dos, *dos2unix; filtered = g_mime_stream_filter_new (ostream); /* convert to DOS format before piping through the OpenPGP filter to maximize testing area */ unix2dos = g_mime_filter_unix2dos_new (FALSE); g_mime_stream_filter_add ((GMimeStreamFilter *) filtered, unix2dos); g_object_unref (unix2dos); g_mime_stream_filter_add ((GMimeStreamFilter *) filtered, filter); /* convert back to UNIX format after filtering */ dos2unix = g_mime_filter_dos2unix_new (FALSE); g_mime_stream_filter_add ((GMimeStreamFilter *) filtered, dos2unix); g_object_unref (dos2unix); onebyte = test_stream_onebyte_new (filtered); g_object_unref (filtered); stream = g_mime_stream_fs_open (path, O_RDONLY, 0644, NULL); g_mime_stream_write_to_stream (stream, onebyte); g_mime_stream_flush (onebyte); g_object_unref (onebyte); g_object_unref (stream); }
static GByteArray * read_all_bytes (const char *path, gboolean is_text) { GMimeStream *filtered, *stream, *mem; GMimeFilter *filter; GByteArray *buffer; buffer = g_byte_array_new (); stream = g_mime_stream_fs_open (path, O_RDONLY, 0644, NULL); mem = g_mime_stream_mem_new_with_byte_array (buffer); g_mime_stream_mem_set_owner ((GMimeStreamMem *) mem, FALSE); if (is_text) { filtered = g_mime_stream_filter_new (mem); filter = g_mime_filter_dos2unix_new (FALSE); g_mime_stream_filter_add ((GMimeStreamFilter *) filtered, filter); g_object_unref (filter); g_object_unref (mem); } else { filtered = mem; } g_mime_stream_write_to_stream (stream, filtered); g_mime_stream_flush (filtered); g_object_unref (filtered); g_object_unref (stream); return buffer; }
static void pump_data_through_filter (GMimeFilter *filter, const char *path, GMimeStream *ostream, gboolean is_text, gboolean inc) { GMimeStream *onebyte, *filtered, *stream; GMimeFilter *dos2unix; filtered = g_mime_stream_filter_new (ostream); if (is_text) { /* canonicalize text input */ dos2unix = g_mime_filter_dos2unix_new (FALSE); g_mime_stream_filter_add ((GMimeStreamFilter *) filtered, dos2unix); g_object_unref (dos2unix); } g_mime_stream_filter_add ((GMimeStreamFilter *) filtered, filter); if (inc) { onebyte = test_stream_onebyte_new (filtered); g_object_unref (filtered); } else { onebyte = filtered; } stream = g_mime_stream_fs_open (path, O_RDONLY, 0644, NULL); g_mime_stream_write_to_stream (stream, onebyte); g_mime_stream_flush (onebyte); g_object_unref (onebyte); g_object_unref (stream); }
/** * g_mime_part_get_best_content_encoding: * @mime_part: a #GMimePart object * @constraint: a #GMimeEncodingConstraint * * Calculates the most efficient content encoding for the @mime_part * given the @constraint. * * Returns: the best content encoding for the specified mime part. **/ GMimeContentEncoding g_mime_part_get_best_content_encoding (GMimePart *mime_part, GMimeEncodingConstraint constraint) { GMimeStream *filtered, *stream; GMimeContentEncoding encoding; GMimeFilterBest *best; GMimeFilter *filter; g_return_val_if_fail (GMIME_IS_PART (mime_part), GMIME_CONTENT_ENCODING_DEFAULT); stream = g_mime_stream_null_new (); filtered = g_mime_stream_filter_new (stream); g_object_unref (stream); filter = g_mime_filter_best_new (GMIME_FILTER_BEST_ENCODING); g_mime_stream_filter_add ((GMimeStreamFilter *) filtered, filter); best = (GMimeFilterBest *) filter; g_mime_data_wrapper_write_to_stream (mime_part->content, filtered); g_mime_stream_flush (filtered); g_object_unref (filtered); encoding = g_mime_filter_best_encoding (best, constraint); g_object_unref (best); return encoding; }
static void test_html (const char *datadir, const char *input, const char *output, guint32 citation) { guint32 flags = GMIME_FILTER_HTML_CONVERT_NL | GMIME_FILTER_HTML_CONVERT_SPACES | GMIME_FILTER_HTML_CONVERT_URLS | GMIME_FILTER_HTML_CONVERT_ADDRESSES; const char *what = "GMimeFilterHtml", *mode; GByteArray *expected, *actual; GMimeStream *stream; GMimeFilter *filter; char *path; switch (citation) { case GMIME_FILTER_HTML_BLOCKQUOTE_CITATION: mode = "blockquote"; break; case GMIME_FILTER_HTML_MARK_CITATION: mode = "mark"; break; case GMIME_FILTER_HTML_CITE: mode = "cite"; break; default: mode = "none"; break; } testsuite_check ("%s (%s %s)", what, input, mode); actual = g_byte_array_new (); stream = g_mime_stream_mem_new_with_byte_array (actual); g_mime_stream_mem_set_owner ((GMimeStreamMem *) stream, FALSE); filter = g_mime_filter_html_new (flags | citation, 0x008888); path = g_build_filename (datadir, input, NULL); pump_data_through_filter (filter, path, stream, TRUE, TRUE); g_mime_filter_reset (filter); g_object_unref (stream); g_object_unref (filter); g_free (path); path = g_build_filename (datadir, output, NULL); expected = read_all_bytes (path, TRUE); g_free (path); if (actual->len != expected->len) { testsuite_check_failed ("%s failed: stream lengths do not match: expected=%u; actual=%u", what, expected->len, actual->len); stream = g_mime_stream_fs_open (output, O_WRONLY | O_CREAT, 0644, NULL); g_mime_stream_write (stream, (const char *) actual->data, actual->len); g_mime_stream_flush (stream); g_object_unref (stream); goto error; } if (memcmp (actual->data, expected->data, actual->len) != 0) { testsuite_check_failed ("%s failed: stream contents do not match", what); goto error; } testsuite_check_passed (); error: g_byte_array_free (expected, TRUE); g_byte_array_free (actual, TRUE); }
static void extract_part(GMimeObject *part, PartExtractorData *a_data) { GMimeDataWrapper *attachment_wrapper = g_mime_part_get_content_object(GMIME_PART(part)); GMimeStream *attachment_mem_stream = g_mime_stream_mem_new(); g_mime_stream_mem_set_owner(GMIME_STREAM_MEM(attachment_mem_stream), FALSE); g_mime_data_wrapper_write_to_stream(attachment_wrapper, attachment_mem_stream); g_mime_stream_flush(attachment_mem_stream); a_data->content = g_mime_stream_mem_get_byte_array(GMIME_STREAM_MEM(attachment_mem_stream)); g_object_unref(attachment_mem_stream); }
static void test_gzip (const char *datadir, const char *filename) { char *name = g_strdup_printf ("%s.gz", filename); char *path = g_build_filename (datadir, filename, NULL); const char *what = "GMimeFilterGzip::zip"; GByteArray *actual, *expected; GMimeStream *stream; GMimeFilter *filter; testsuite_check ("%s", what); actual = g_byte_array_new (); stream = g_mime_stream_mem_new_with_byte_array (actual); g_mime_stream_mem_set_owner ((GMimeStreamMem *) stream, FALSE); filter = g_mime_filter_gzip_new (GMIME_FILTER_GZIP_MODE_ZIP, 9); g_mime_filter_gzip_set_filename ((GMimeFilterGZip *) filter, filename); g_mime_filter_gzip_set_comment ((GMimeFilterGZip *) filter, "This is a comment."); pump_data_through_filter (filter, path, stream, TRUE, TRUE); g_mime_filter_reset (filter); g_object_unref (stream); g_object_unref (filter); g_free (path); path = g_build_filename (datadir, name, NULL); expected = read_all_bytes (path, FALSE); g_free (name); g_free (path); if (actual->len != 1233 && (actual->len != expected->len || memcmp (actual->data, expected->data, actual->len) != 0)) { if (actual->len != expected->len) testsuite_check_failed ("%s failed: streams are not the same length: %u", what, actual->len); else testsuite_check_failed ("%s failed: streams do not match", what); name = g_strdup_printf ("%s.1.gz", filename); path = g_build_filename (datadir, name, NULL); g_free (name); stream = g_mime_stream_fs_open (path, O_WRONLY | O_CREAT | O_TRUNC, 0644, NULL); g_free (path); g_mime_stream_write (stream, (const char *) actual->data, actual->len); g_mime_stream_flush (stream); g_object_unref (stream); } else { testsuite_check_passed (); } g_byte_array_free (expected, TRUE); g_byte_array_free (actual, TRUE); }
static GMimeStream * random_whole_stream (const char *datadir, char **filename) { size_t nwritten, buflen, total = 0, size, i; GMimeStream *stream; char buf[4096]; ssize_t n; int fd; /* read between 4k and 14k bytes */ size = 4096 + (size_t) (10240.0 * (rand () / (RAND_MAX + 1.0))); v(fprintf (stdout, "Generating %zu bytes of random data... ", size)); v(fflush (stdout)); g_mkdir_with_parents (datadir, 0755); g_snprintf (buf, sizeof (buf), "%s%cstream.%u", datadir, G_DIR_SEPARATOR, getpid ()); if ((fd = open (buf, O_CREAT | O_TRUNC | O_RDWR, 0666)) == -1) { fprintf (stderr, "Error: Cannot create `%s': %s\n", buf, g_strerror (errno)); exit (EXIT_FAILURE); } *filename = g_strdup (buf); stream = g_mime_stream_fs_new (fd); while (total < size) { buflen = size - total > sizeof (buf) ? sizeof (buf) : size - total; for (i = 0; i < buflen; i++) buf[i] = (char) (255 * (rand () / (RAND_MAX + 1.0))); nwritten = 0; do { if ((n = g_mime_stream_write (stream, buf + nwritten, buflen - nwritten)) <= 0) break; nwritten += n; total += n; } while (nwritten < buflen); if (nwritten < buflen) break; } g_mime_stream_flush (stream); g_mime_stream_reset (stream); v(fputs ("done\n", stdout)); return stream; }
int main (int argc, char *argv[]) { gboolean rv; GMimeStream *out_stream; out_stream = g_mime_stream_file_new (stdout); if (argc < 2) { g_printerr ("usage: %s <msg-files>\n", argv[0]); return 1; } setlocale (LC_ALL, ""); g_mime_init(GMIME_ENABLE_RFC2047_WORKAROUNDS); if (argc == 2) { g_mime_stream_printf (out_stream, "%%{"); g_mime_stream_printf (out_stream, "path: \"%s\", ", argv[1]); rv = test_file (argv[1], out_stream); g_mime_stream_printf (out_stream, "}"); } else { g_mime_stream_printf (out_stream, "["); int x; for (x = 1; x < argc; x++ ) { /* printf("File: %s\n", argv[x]); */ if (x > 1) { g_mime_stream_printf (out_stream, ", "); } g_mime_stream_printf (out_stream, "%%{"); g_mime_stream_printf (out_stream, "path: \"%s\", ", argv[x]); rv = test_file (argv[x], out_stream); g_mime_stream_printf (out_stream, "}"); } g_mime_stream_printf (out_stream, "]"); } g_mime_shutdown (); /* flush stdout */ g_mime_stream_flush (out_stream); /* free/close the stream */ g_object_unref (out_stream); return rv ? 0 : 1; }
gboolean mux_message_part_write (MuxMessagePart *self, GMimeStream *ostream, GError **err) { ssize_t bytes; g_return_val_if_fail (MUX_IS_MESSAGE_PART(self), FALSE); g_return_val_if_fail (GMIME_IS_STREAM(ostream), FALSE); if (GMIME_IS_PART(self->mime_object)) { GMimeDataWrapper *wrapper; wrapper = get_data_wrapper (self->mime_object, err); if (!wrapper) return FALSE; bytes = g_mime_data_wrapper_write_to_stream ( wrapper, ostream); } else { GMimeStream *istream; istream = get_mime_stream (self->mime_object, err); if (!istream) return FALSE; bytes = g_mime_stream_write_to_stream (istream, ostream); } if (bytes == -1) { g_set_error (err, G_IO_ERROR, G_IO_ERROR_FAILED, "failed to write part to stream"); return FALSE; } bytes = g_mime_stream_flush (ostream); if (bytes == -1) { g_set_error (err, G_IO_ERROR, G_IO_ERROR_FAILED, "failed to flush stream"); return FALSE; } return TRUE; }
static void read_msg (SpruceFolder *folder, GPtrArray *uids, int cursor) { GMimeMessage *message; GMimeStream *stream; GError *err = NULL; if (!(message = spruce_folder_get_message (folder, uids->pdata[cursor], &err))) { fprintf (stderr, "can't get message: %s\n", err ? err->message : "(null)"); g_error_free (err); return; } stream = g_mime_stream_fs_new (dup (fileno (stdout))); g_mime_object_write_to_stream ((GMimeObject *) message, stream); g_mime_stream_write (stream, "\n", 1); g_mime_stream_flush (stream); g_object_unref (message); g_object_unref (stream); mark_msgs (folder, uids, NULL, cursor, SPRUCE_MESSAGE_SEEN, SPRUCE_MESSAGE_SEEN); }
static size_t gen_random_stream (GMimeStream *stream) { size_t nwritten, buflen, total = 0, size, i; char buf[4096]; ssize_t n; /* read between 4k and 14k bytes */ size = 4096 + (size_t) (10240.0 * (rand () / (RAND_MAX + 1.0))); v(fprintf (stdout, "Generating %" G_GSIZE_FORMAT " bytes of random data... ", size)); v(fflush (stdout)); while (total < size) { buflen = size - total > sizeof (buf) ? sizeof (buf) : size - total; for (i = 0; i < buflen; i++) buf[i] = (char) (255 * (rand () / (RAND_MAX + 1.0))); nwritten = 0; do { if ((n = g_mime_stream_write (stream, buf + nwritten, buflen - nwritten)) <= 0) break; nwritten += n; total += n; } while (nwritten < buflen); if (nwritten < buflen) break; } g_mime_stream_flush (stream); g_mime_stream_reset (stream); v(fputs ("done\n", stdout)); return size; }
static gboolean password_requester (GMimeCryptoContext *ctx, const char *user_id, const char* prompt_ctx, gboolean reprompt, GMimeStream *response, GError **err) { CallbackData *cbdata; gchar *password; ssize_t written; cbdata = g_object_get_data (G_OBJECT(ctx), CALLBACK_DATA); if (!cbdata || !cbdata->pw_func) return FALSE; password = cbdata->pw_func (user_id, prompt_ctx, reprompt, cbdata->user_data); if (!password) { mu_util_g_set_error (err, MU_ERROR_CRYPTO, "failed to get password"); return FALSE; } written = g_mime_stream_write_string (response, password); if (written != -1) written = g_mime_stream_write_string (response, "\n"); if (written == -1) mu_util_g_set_error (err, MU_ERROR_CRYPTO, "writing password to mime stream failed"); if (g_mime_stream_flush (response) != 0) g_printerr ("error flushing stream\n"); memset (password, 0, strlen(password)); g_free (password); return written != -1 ? TRUE : FALSE; }
GMimeGpgmeSigstat * g_mime_gpgme_mps_verify(GMimeMultipartSigned * mps, GError ** error) { const gchar *protocol; gpgme_protocol_t crypto_prot; gchar *content_type; GMimeObject *content; GMimeObject *signature; GMimeStream *stream; GMimeStream *filtered_stream; GMimeFilter *crlf_filter; GMimeDataWrapper *wrapper; GMimeStream *sigstream; GMimeGpgmeSigstat *result; g_return_val_if_fail(GMIME_IS_MULTIPART_SIGNED(mps), NULL); if (g_mime_multipart_get_count(GMIME_MULTIPART(mps)) < 2) { g_set_error(error, GMIME_ERROR, GMIME_ERROR_PARSE_ERROR, "%s", _ ("Cannot verify multipart/signed part due to missing subparts.")); return NULL; } /* grab the protocol so we can configure the GpgME context */ protocol = g_mime_object_get_content_type_parameter(GMIME_OBJECT(mps), "protocol"); if (protocol) { if (g_ascii_strcasecmp("application/pgp-signature", protocol) == 0) crypto_prot = GPGME_PROTOCOL_OpenPGP; else if (g_ascii_strcasecmp ("application/pkcs7-signature", protocol) == 0 || g_ascii_strcasecmp("application/x-pkcs7-signature", protocol) == 0) crypto_prot = GPGME_PROTOCOL_CMS; else crypto_prot = GPGME_PROTOCOL_UNKNOWN; } else crypto_prot = GPGME_PROTOCOL_UNKNOWN; /* eject on unknown protocols */ if (crypto_prot == GPGME_PROTOCOL_UNKNOWN) { g_set_error(error, GPGME_ERROR_QUARK, GPG_ERR_INV_VALUE, _("unsupported protocol “%s”"), protocol); return NULL; } signature = g_mime_multipart_get_part(GMIME_MULTIPART(mps), GMIME_MULTIPART_SIGNED_SIGNATURE); /* make sure the protocol matches the signature content-type */ content_type = g_mime_content_type_to_string(signature->content_type); if (g_ascii_strcasecmp(content_type, protocol) != 0) { g_set_error(error, GMIME_ERROR, GMIME_ERROR_PARSE_ERROR, "%s", _ ("Cannot verify multipart/signed part: signature content-type does not match protocol.")); g_free(content_type); return NULL; } g_free(content_type); content = g_mime_multipart_get_part(GMIME_MULTIPART(mps), GMIME_MULTIPART_SIGNED_CONTENT); /* get the content stream */ stream = g_mime_stream_mem_new(); filtered_stream = g_mime_stream_filter_new(stream); /* Note: see rfc2015 or rfc3156, section 5.1 */ crlf_filter = g_mime_filter_crlf_new(TRUE, FALSE); g_mime_stream_filter_add(GMIME_STREAM_FILTER(filtered_stream), crlf_filter); g_object_unref(crlf_filter); g_mime_object_write_to_stream(content, filtered_stream); g_mime_stream_flush(filtered_stream); g_object_unref(filtered_stream); g_mime_stream_reset(stream); /* get the signature stream */ wrapper = g_mime_part_get_content_object(GMIME_PART(signature)); /* a s/mime signature is always encoded, a pgp signature shouldn't, * but there exist implementations which encode it... */ sigstream = g_mime_stream_mem_new(); g_mime_data_wrapper_write_to_stream(wrapper, sigstream); g_mime_stream_reset(sigstream); /* verify the signature */ result = libbalsa_gpgme_verify(stream, sigstream, crypto_prot, FALSE, error); g_object_unref(stream); g_object_unref(sigstream); return result; }
static int gen_test_data (const char *datadir, char **stream_name) { GMimeStream *istream, *ostream, *stream; char input[256], output[256], *name, *p; gint64 start, end, len, size; int fd, i; srand (time (NULL)); name = g_stpcpy (input, datadir); *name++ = G_DIR_SEPARATOR; name = g_stpcpy (name, "input"); p = g_stpcpy (output, datadir); *p++ = G_DIR_SEPARATOR; p = g_stpcpy (p, "output"); g_mkdir_with_parents (input, 0755); g_mkdir_with_parents (output, 0755); *name++ = G_DIR_SEPARATOR; strcpy (name, "streamXXXXXX"); if ((fd = g_mkstemp (input)) == -1) return -1; *stream_name = g_strdup (name); *p++ = G_DIR_SEPARATOR; p = g_stpcpy (p, name); *p++ = '_'; istream = g_mime_stream_fs_new (fd); size = gen_random_stream (istream); for (i = 0; i < 64; i++) { retry: start = (gint64) (size * (rand () / (RAND_MAX + 1.0))); len = (gint64) (size * (rand () / (RAND_MAX + 1.0))); if (start + len > size) { len = size - start; end = -1; } else { end = start + len; } sprintf (p, "%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT, start, end); if ((fd = open (output, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0666)) == -1) goto retry; ostream = g_mime_stream_fs_new (fd); stream = g_mime_stream_substream (istream, start, end); g_mime_stream_write_to_stream (stream, ostream); g_mime_stream_flush (ostream); g_object_unref (ostream); g_object_unref (stream); } return 0; }
GMimeObject * g_mime_gpgme_mpe_decrypt(GMimeMultipartEncrypted * mpe, GMimeGpgmeSigstat ** signature, GtkWindow * parent, GError ** err) { GMimeObject *decrypted, *version, *encrypted; GMimeStream *stream, *ciphertext; GMimeStream *filtered_stream; GMimeContentType *mime_type; GMimeGpgmeSigstat *sigstat; GMimeDataWrapper *wrapper; GMimeFilter *crlf_filter; GMimeParser *parser; const char *protocol; char *content_type; g_return_val_if_fail(GMIME_IS_MULTIPART_ENCRYPTED(mpe), NULL); if (signature && *signature) { g_object_unref(G_OBJECT(*signature)); *signature = NULL; } protocol = g_mime_object_get_content_type_parameter(GMIME_OBJECT(mpe), "protocol"); /* make sure the protocol is present and matches the cipher encrypt protocol */ if (!protocol || g_ascii_strcasecmp("application/pgp-encrypted", protocol) != 0) { g_set_error(err, GMIME_ERROR, GMIME_ERROR_PROTOCOL_ERROR, _ ("Cannot decrypt multipart/encrypted part: unsupported encryption protocol “%s”."), protocol ? protocol : _("(none)")); return NULL; } version = g_mime_multipart_get_part(GMIME_MULTIPART(mpe), GMIME_MULTIPART_ENCRYPTED_VERSION); /* make sure the protocol matches the version part's content-type */ content_type = g_mime_content_type_to_string(version->content_type); if (g_ascii_strcasecmp(content_type, protocol) != 0) { g_set_error(err, GMIME_ERROR, GMIME_ERROR_PROTOCOL_ERROR, "%s", _ ("Cannot decrypt multipart/encrypted part: content-type does not match protocol.")); g_free(content_type); return NULL; } g_free(content_type); /* get the encrypted part and check that it is of type application/octet-stream */ encrypted = g_mime_multipart_get_part(GMIME_MULTIPART(mpe), GMIME_MULTIPART_ENCRYPTED_CONTENT); mime_type = g_mime_object_get_content_type(encrypted); if (!g_mime_content_type_is_type (mime_type, "application", "octet-stream")) { g_set_error(err, GMIME_ERROR, GMIME_ERROR_PROTOCOL_ERROR, "%s", _ ("Cannot decrypt multipart/encrypted part: unexpected content type")); return NULL; } /* get the ciphertext stream */ wrapper = g_mime_part_get_content_object(GMIME_PART(encrypted)); ciphertext = g_mime_data_wrapper_get_decoded_stream(wrapper); g_mime_stream_reset(ciphertext); stream = g_mime_stream_mem_new(); filtered_stream = g_mime_stream_filter_new(stream); crlf_filter = g_mime_filter_crlf_new(FALSE, FALSE); g_mime_stream_filter_add(GMIME_STREAM_FILTER(filtered_stream), crlf_filter); g_object_unref(crlf_filter); /* get the cleartext */ sigstat = libbalsa_gpgme_decrypt(ciphertext, filtered_stream, GPGME_PROTOCOL_OpenPGP, parent, err); if (!sigstat) { g_object_unref(filtered_stream); g_object_unref(ciphertext); g_object_unref(stream); return NULL; } g_mime_stream_flush(filtered_stream); g_object_unref(filtered_stream); g_object_unref(ciphertext); g_mime_stream_reset(stream); parser = g_mime_parser_new(); g_mime_parser_init_with_stream(parser, stream); g_object_unref(stream); decrypted = g_mime_parser_construct_part(parser); g_object_unref(parser); if (!decrypted) { g_set_error(err, GMIME_ERROR, GMIME_ERROR_PARSE_ERROR, "%s", _ ("Cannot decrypt multipart/encrypted part: failed to parse decrypted content")); g_object_unref(G_OBJECT(sigstat)); return NULL; } /* cache the decrypted part */ if (signature) { if (sigstat->status != GPG_ERR_NOT_SIGNED) *signature = sigstat; else g_object_unref(G_OBJECT(sigstat)); } return decrypted; }
gboolean g_mime_gpgme_mps_sign(GMimeMultipartSigned * mps, GMimeObject * content, const gchar * userid, gpgme_protocol_t protocol, GtkWindow * parent, GError ** err) { GMimeStream *stream; GMimeStream *filtered; GMimeStream *sigstream; GMimeFilter *filter; GMimeContentType *content_type; GMimeDataWrapper *wrapper; GMimeParser *parser; GMimePart *signature; gchar *micalg; const gchar *proto_type; const gchar *sig_subtype; gpgme_hash_algo_t hash_algo; g_return_val_if_fail(GMIME_IS_MULTIPART_SIGNED(mps), FALSE); g_return_val_if_fail(GMIME_IS_OBJECT(content), FALSE); /* Prepare all the parts for signing... */ sign_prepare(content); /* get the cleartext */ stream = g_mime_stream_mem_new(); filtered = g_mime_stream_filter_new(stream); /* Note: see rfc3156, section 3 - second note */ filter = g_mime_filter_from_new(GMIME_FILTER_FROM_MODE_ARMOR); g_mime_stream_filter_add(GMIME_STREAM_FILTER(filtered), filter); g_object_unref(filter); /* Note: see rfc3156, section 5.4 (this is the main difference between rfc2015 and rfc3156) */ filter = g_mime_filter_strip_new(); g_mime_stream_filter_add(GMIME_STREAM_FILTER(filtered), filter); g_object_unref(filter); g_mime_object_write_to_stream(content, filtered); g_mime_stream_flush(filtered); g_object_unref(filtered); g_mime_stream_reset(stream); /* Note: see rfc2015 or rfc3156, section 5.1 */ filtered = g_mime_stream_filter_new(stream); filter = g_mime_filter_crlf_new(TRUE, FALSE); g_mime_stream_filter_add(GMIME_STREAM_FILTER(filtered), filter); g_object_unref(filter); /* construct the signature stream */ sigstream = g_mime_stream_mem_new(); /* sign the content stream */ hash_algo = libbalsa_gpgme_sign(userid, filtered, sigstream, protocol, FALSE, parent, err); g_object_unref(filtered); if (hash_algo == GPGME_MD_NONE) { g_object_unref(sigstream); g_object_unref(stream); return FALSE; } g_mime_stream_reset(sigstream); g_mime_stream_reset(stream); /* set the multipart/signed protocol and micalg */ content_type = g_mime_object_get_content_type(GMIME_OBJECT(mps)); if (protocol == GPGME_PROTOCOL_OpenPGP) { micalg = g_strdup_printf("PGP-%s", gpgme_hash_algo_name(hash_algo)); proto_type = "application/pgp-signature"; sig_subtype = "pgp-signature"; } else { micalg = g_strdup(gpgme_hash_algo_name(hash_algo)); proto_type = "application/pkcs7-signature"; sig_subtype = "pkcs7-signature"; } g_mime_content_type_set_parameter(content_type, "micalg", micalg); g_free(micalg); g_mime_content_type_set_parameter(content_type, "protocol", proto_type); g_mime_multipart_set_boundary(GMIME_MULTIPART(mps), NULL); /* construct the content part */ parser = g_mime_parser_new_with_stream(stream); content = g_mime_parser_construct_part(parser); g_object_unref(stream); g_object_unref(parser); /* construct the signature part */ signature = g_mime_part_new_with_type("application", sig_subtype); wrapper = g_mime_data_wrapper_new(); g_mime_data_wrapper_set_stream(wrapper, sigstream); g_mime_part_set_content_object(signature, wrapper); g_object_unref(sigstream); g_object_unref(wrapper); /* FIXME: temporary hack, this info should probably be set in * the CipherContext class - maybe ::sign can take/output a * GMimePart instead. */ if (protocol == GPGME_PROTOCOL_CMS) { g_mime_part_set_content_encoding(signature, GMIME_CONTENT_ENCODING_BASE64); g_mime_part_set_filename(signature, "smime.p7m"); } /* save the content and signature parts */ /* FIXME: make sure there aren't any other parts?? */ g_mime_multipart_add(GMIME_MULTIPART(mps), content); g_mime_multipart_add(GMIME_MULTIPART(mps), (GMimeObject *) signature); g_object_unref(signature); g_object_unref(content); return TRUE; }
int main (int argc, char **argv) { const char *datadir = "data/mbox"; char input[256], output[256], *tmp, *p, *q; GMimeStream *istream, *ostream, *mstream, *pstream; GMimeParser *parser; const char *dent; const char *path; struct stat st; GDir *dir; int i; #ifdef ENABLE_MBOX_MATCH int fd; if (mkdir ("./tmp", 0755) == -1 && errno != EEXIST) return 0; #endif g_mime_init (); testsuite_init (argc, argv); path = datadir; for (i = 1; i < argc; i++) { if (argv[i][0] != '-') { path = argv[i]; break; } } testsuite_start ("Mbox parser"); if (stat (path, &st) == -1) goto exit; if (S_ISDIR (st.st_mode)) { /* automated testsuite */ p = g_stpcpy (input, path); *p++ = G_DIR_SEPARATOR; p = g_stpcpy (p, "input"); if (!(dir = g_dir_open (input, 0, NULL))) goto exit; *p++ = G_DIR_SEPARATOR; *p = '\0'; q = g_stpcpy (output, path); *q++ = G_DIR_SEPARATOR; q = g_stpcpy (q, "output"); *q++ = G_DIR_SEPARATOR; *q = '\0'; while ((dent = g_dir_read_name (dir))) { if (!g_str_has_suffix (dent, ".mbox")) continue; strcpy (p, dent); strcpy (q, dent); tmp = NULL; parser = NULL; istream = NULL; ostream = NULL; mstream = NULL; pstream = NULL; testsuite_check ("%s", dent); try { if (!(istream = g_mime_stream_fs_open (input, O_RDONLY, 0, NULL))) { throw (exception_new ("could not open `%s': %s", input, g_strerror (errno))); } if (!(ostream = g_mime_stream_fs_open (output, O_RDONLY, 0, NULL))) { throw (exception_new ("could not open `%s': %s", output, g_strerror (errno))); } #ifdef ENABLE_MBOX_MATCH tmp = g_strdup_printf ("./tmp/%s", dent); if ((fd = open (tmp, O_CREAT | O_RDWR | O_TRUNC, 0644)) == -1) { throw (exception_new ("could not open `%s': %s", tmp, g_strerror (errno))); } mstream = g_mime_stream_fs_new (fd); #endif parser = g_mime_parser_new_with_stream (istream); g_mime_parser_set_persist_stream (parser, TRUE); g_mime_parser_set_format (parser, GMIME_FORMAT_MBOX); if (!g_mime_parser_get_persist_stream (parser)) throw (exception_new ("persist stream check failed")); if (g_mime_parser_get_format (parser) != GMIME_FORMAT_MBOX) throw (exception_new ("format check failed")); if (strstr (dent, "content-length") != NULL) { g_mime_parser_set_respect_content_length (parser, TRUE); if (!g_mime_parser_get_respect_content_length (parser)) throw (exception_new ("respect content-length check failed")); } else { g_mime_parser_set_respect_content_length (parser, FALSE); if (g_mime_parser_get_respect_content_length (parser)) throw (exception_new ("respect content-length check failed")); } g_mime_parser_set_header_regex (parser, "^X-Evolution", xevcb, NULL); pstream = g_mime_stream_mem_new (); test_parser (parser, mstream, pstream); #ifdef ENABLE_MBOX_MATCH g_mime_stream_flush (mstream); g_mime_stream_reset (istream); g_mime_stream_reset (mstream); if (!streams_match (istream, mstream)) throw (exception_new ("mboxes do not match for `%s'", dent)); #endif g_mime_stream_reset (ostream); g_mime_stream_reset (pstream); if (!streams_match (ostream, pstream)) throw (exception_new ("summaries do not match for `%s'", dent)); testsuite_check_passed (); #ifdef ENABLE_MBOX_MATCH unlink (tmp); #endif } catch (ex) { if (parser != NULL) testsuite_check_failed ("%s: %s", dent, ex->message); else testsuite_check_warn ("%s: %s", dent, ex->message); } finally; if (mstream != NULL) g_object_unref (mstream); if (pstream != NULL) g_object_unref (pstream); if (istream != NULL) g_object_unref (istream); if (ostream != NULL) g_object_unref (ostream); if (parser != NULL) g_object_unref (parser); g_free (tmp); } g_dir_close (dir); } else if (S_ISREG (st.st_mode)) { /* manually run test on a single file */ if (!(istream = g_mime_stream_fs_open (path, O_RDONLY, 0, NULL))) goto exit; parser = g_mime_parser_new_with_stream (istream); g_mime_parser_set_format (parser, GMIME_FORMAT_MBOX); #ifdef ENABLE_MBOX_MATCH tmp = g_strdup ("./tmp/mbox-test.XXXXXX"); if ((fd = g_mkstemp (tmp)) == -1) { g_object_unref (istream); g_object_unref (parser); g_free (tmp); goto exit; } mstream = g_mime_stream_fs_new (fd); #else mstream = NULL; #endif ostream = g_mime_stream_file_new (stdout); g_mime_stream_file_set_owner ((GMimeStreamFile *) ostream, FALSE); testsuite_check ("user-input mbox: `%s'", path); try { test_parser (parser, mstream, ostream); #ifdef ENABLE_MBOX_MATCH g_mime_stream_reset (istream); g_mime_stream_reset (mstream); if (!streams_match (istream, mstream)) throw (exception_new ("`%s' does not match `%s'", tmp, path)); unlink (tmp); #endif testsuite_check_passed (); } catch (ex) { testsuite_check_failed ("user-input mbox `%s': %s", path, ex->message); } finally; g_object_unref (istream); g_object_unref (ostream); g_object_unref (parser); #ifdef ENABLE_MBOX_MATCH g_object_unref (mstream); g_free (tmp); #endif } else { goto exit; } exit: #ifdef ENABLE_MBOX_MATCH //if ((dir = g_dir_open ("./tmp", 0, NULL))) { // p = g_stpcpy (input, "./tmp"); // *p++ = G_DIR_SEPARATOR; // // while ((dent = g_dir_read_name (dir))) { // strcpy (p, dent); // unlink (input); // } // // g_dir_close (dir); //} //rmdir ("./tmp"); #endif testsuite_end (); g_mime_shutdown (); return testsuite_exit (); }
static int uuencode (const char *progname, int argc, char **argv) { GMimeStream *istream, *ostream, *fstream; GMimeContentEncoding encoding; const char *filename, *name; GMimeFilter *filter; gboolean base64; struct stat st; int fd, opt; base64 = FALSE; encoding = GMIME_CONTENT_ENCODING_UUENCODE; while ((opt = getopt_long (argc, argv, "hvm", longopts, NULL)) != -1) { switch (opt) { case 'h': usage (progname); return 0; case 'v': version (progname); return 0; case 'm': base64 = TRUE; encoding = GMIME_CONTENT_ENCODING_BASE64; break; default: printf ("Try `%s --help' for more information.\n", progname); return -1; } } if (optind >= argc) { printf ("Try `%s --help' for more information.\n", progname); return -1; } if (optind + 1 < argc) filename = argv[optind++]; else filename = NULL; name = argv[optind]; /* open our input file... */ if ((fd = filename ? open (filename, O_RDONLY, 0) : dup (0)) == -1) { fprintf (stderr, "%s: %s: %s\n", progname, filename ? filename : "stdin", g_strerror (errno)); return -1; } /* stat() our input file for file mode permissions */ if (fstat (fd, &st) == -1) { fprintf (stderr, "%s: %s: %s\n", progname, filename ? filename : "stdin", g_strerror (errno)); close (fd); return -1; } printf ("begin%s %.3o %s\n", base64 ? "-base64" : "", st.st_mode & 0777, name); fflush (stdout); istream = g_mime_stream_fs_new (fd); /* open our output stream */ ostream = g_mime_stream_fs_new (1); g_mime_stream_fs_set_owner ((GMimeStreamFs *) ostream, FALSE); fstream = g_mime_stream_filter_new (ostream); /* attach an encode filter */ filter = g_mime_filter_basic_new (encoding, TRUE); g_mime_stream_filter_add ((GMimeStreamFilter *) fstream, filter); g_object_unref (filter); if (g_mime_stream_write_to_stream (istream, fstream) == -1) { fprintf (stderr, "%s: %s\n", progname, g_strerror (errno)); g_object_unref (fstream); g_object_unref (istream); g_object_unref (ostream); return -1; } g_mime_stream_flush (fstream); g_object_unref (fstream); g_object_unref (istream); if (g_mime_stream_write_string (ostream, base64 ? "====\n" : "end\n") == -1) { fprintf (stderr, "%s: %s\n", progname, g_strerror (errno)); g_object_unref (ostream); return -1; } g_object_unref (ostream); return 0; }
static void collect_part(GMimeObject *part, PartCollectorData *fdata, gboolean multipart_parent) { GMimeContentType *content_type = g_mime_object_get_content_type(part); GMimeContentDisposition *disposition = g_mime_object_get_content_disposition(part); if (!content_type) return; GMimeDataWrapper *wrapper = g_mime_part_get_content_object(GMIME_PART(part)); if (!wrapper) return; // All the information will be collected in the CollectedPart CollectedPart *c_part = new_collected_part(fdata->part_id); gboolean is_attachment = FALSE; if (disposition) { c_part->disposition = g_ascii_strdown(disposition->disposition, -1); is_attachment = !g_ascii_strcasecmp(disposition->disposition, GMIME_DISPOSITION_ATTACHMENT); } // If a filename is given, collect it always const gchar *filename = g_mime_part_get_filename(GMIME_PART(part)); if (filename) c_part->filename = g_strdup(filename); // If a contentID is given, collect it always const char* content_id = g_mime_part_get_content_id (GMIME_PART(part)); if (content_id) c_part->content_id = g_strdup(content_id); // Get the contentType in lowercase gchar *content_type_str = g_mime_content_type_to_string(content_type); c_part->content_type = g_ascii_strdown(content_type_str, -1); g_free(content_type_str); // To qualify as a message body, a MIME entity MUST NOT have a Content-Disposition header with the value "attachment". if (!is_attachment && g_mime_content_type_is_type(content_type, "text", "*")) { gboolean is_text_plain = g_mime_content_type_is_type(content_type, "text", "plain"); gboolean is_text_html = g_mime_content_type_is_type(content_type, "text", "html"); gboolean is_text_rtf = g_mime_content_type_is_type(content_type, "text", "rtf"); gboolean is_text_enriched = g_mime_content_type_is_type(content_type, "text", "enriched"); gboolean is_new_text = !fdata->text_part && is_text_plain; gboolean is_new_html = !fdata->html_part && (is_text_html || is_text_enriched || is_text_rtf); GMimeStream *mem_stream = g_mime_stream_mem_new(); g_mime_stream_mem_set_owner(GMIME_STREAM_MEM(mem_stream), FALSE); GMimeStream *filtered_mem_stream = g_mime_stream_filter_new(mem_stream); const gchar *charset = g_mime_object_get_content_type_parameter(part, "charset"); if (charset && g_ascii_strcasecmp(charset, UTF8_CHARSET)) { GMimeFilter *utf8_charset_filter = g_mime_filter_charset_new(charset, UTF8_CHARSET); g_mime_stream_filter_add(GMIME_STREAM_FILTER(filtered_mem_stream), utf8_charset_filter); g_object_unref(utf8_charset_filter); } if (!fdata->raw && is_new_text) { GMimeFilter *strip_filter = g_mime_filter_strip_new(); g_mime_stream_filter_add(GMIME_STREAM_FILTER(filtered_mem_stream), strip_filter); g_object_unref(strip_filter); GMimeFilter *crlf_filter = g_mime_filter_crlf_new(FALSE, FALSE); g_mime_stream_filter_add(GMIME_STREAM_FILTER(filtered_mem_stream), crlf_filter); g_object_unref(crlf_filter); GMimeFilter *html_filter = g_mime_filter_html_new( GMIME_FILTER_HTML_CONVERT_NL | GMIME_FILTER_HTML_CONVERT_SPACES | GMIME_FILTER_HTML_CONVERT_URLS | GMIME_FILTER_HTML_MARK_CITATION | GMIME_FILTER_HTML_CONVERT_ADDRESSES | GMIME_FILTER_HTML_CITE, CITATION_COLOUR); g_mime_stream_filter_add(GMIME_STREAM_FILTER(filtered_mem_stream), html_filter); g_object_unref(html_filter); } if (!fdata->raw && (is_new_text || is_new_html)) { GMimeFilter *from_filter = g_mime_filter_from_new(GMIME_FILTER_FROM_MODE_ESCAPE); g_mime_stream_filter_add(GMIME_STREAM_FILTER(filtered_mem_stream), from_filter); g_object_unref(from_filter); } // Add Enriched/RTF filter for this content if (!fdata->raw && (is_new_html && (is_text_enriched || is_text_rtf))) { guint flags = 0; if (is_text_rtf) flags = GMIME_FILTER_ENRICHED_IS_RICHTEXT; GMimeFilter *enriched_filter = g_mime_filter_enriched_new(flags); g_mime_stream_filter_add(GMIME_STREAM_FILTER(filtered_mem_stream), enriched_filter); g_object_unref(enriched_filter); } g_mime_data_wrapper_write_to_stream(wrapper, filtered_mem_stream); // Very important! Flush the the stream and get all content through. g_mime_stream_flush(filtered_mem_stream); // Freed by the mem_stream on its own (owner) [transfer none] c_part->content = g_mime_stream_mem_get_byte_array(GMIME_STREAM_MEM(mem_stream)); // After we unref the mem_stream, part_content is NOT available anymore g_object_unref(filtered_mem_stream); g_object_unref(mem_stream); // Without content, the collected body part is of no use, so we ignore it. if (c_part->content->len == 0) { free_collected_part(c_part); return; } // We accept only the first text and first html content, everything // else is considered an alternative body if (is_new_text) { fdata->text_part = c_part; } else if (is_new_html) { fdata->html_part = c_part; } else { g_ptr_array_add(fdata->alternative_bodies, c_part); } } else { GMimeStream *attachment_mem_stream = g_mime_stream_mem_new(); g_mime_stream_mem_set_owner (GMIME_STREAM_MEM(attachment_mem_stream), FALSE); g_mime_data_wrapper_write_to_stream(wrapper, attachment_mem_stream); g_mime_stream_flush(attachment_mem_stream); c_part->content = g_mime_stream_mem_get_byte_array(GMIME_STREAM_MEM(attachment_mem_stream)); g_object_unref(attachment_mem_stream); // Some content may not have disposition defined so we need to determine better what it is if ((disposition && !g_ascii_strcasecmp(disposition->disposition, GMIME_DISPOSITION_INLINE)) || g_mime_part_get_content_id(GMIME_PART(part))) { g_ptr_array_add(fdata->inlines, c_part); } else { // All other disposition should be kept within attachments g_ptr_array_add(fdata->attachments, c_part); } } }
static void test_openpgp_filter (GMimeFilterOpenPGP *filter, const char *path, GMimeOpenPGPData data_type, gint64 begin, gint64 end) { GMimeStream *filtered, *stream, *expected, *ostream; GMimeFilter *dos2unix; GMimeOpenPGPData type; Exception *ex = NULL; GByteArray *buf[2]; char *filename; struct stat st; gint64 offset; ostream = g_mime_stream_mem_new (); pump_data_through_filter ((GMimeFilter *) filter, path, ostream); if ((type = g_mime_filter_openpgp_get_data_type (filter)) != data_type) { g_object_unref (ostream); throw (exception_new ("Incorrect OpenPGP data type detected: %s", openpgp_data_types[type])); } if ((offset = g_mime_filter_openpgp_get_begin_offset (filter)) != begin) { g_object_unref (ostream); throw (exception_new ("Incorrect begin offset: %ld", (long) offset)); } if ((offset = g_mime_filter_openpgp_get_end_offset (filter)) != end) { g_object_unref (ostream); throw (exception_new ("Incorrect end offset: %ld", (long) offset)); } filename = g_strdup_printf ("%s.openpgp-block", path); if (stat (filename, &st) == -1) { stream = g_mime_stream_fs_open (filename, O_RDWR | O_CREAT, 0644, NULL); g_mime_stream_reset (ostream); g_mime_stream_write_to_stream (ostream, stream); g_mime_stream_flush (stream); g_mime_stream_reset (stream); } else { stream = g_mime_stream_fs_open (filename, O_RDONLY, 0644, NULL); } g_free (filename); /* make sure the data is in UNIX format before comparing (might be running tests on Windows) */ expected = g_mime_stream_mem_new (); filtered = g_mime_stream_filter_new (expected); dos2unix = g_mime_filter_dos2unix_new (FALSE); g_mime_stream_filter_add ((GMimeStreamFilter *) filtered, dos2unix); g_object_unref (dos2unix); g_mime_stream_write_to_stream (stream, filtered); g_mime_stream_flush (filtered); g_object_unref (filtered); g_object_unref (stream); buf[0] = GMIME_STREAM_MEM (expected)->buffer; buf[1] = GMIME_STREAM_MEM (ostream)->buffer; if (buf[0]->len != buf[1]->len || memcmp (buf[0]->data, buf[1]->data, buf[0]->len) != 0) ex = exception_new ("filtered data does not match the expected result"); g_object_unref (expected); g_object_unref (ostream); if (ex != NULL) throw (ex); }
static ssize_t write_content (GMimePart *part, GMimeStream *stream) { ssize_t nwritten, total = 0; if (!part->content) return 0; /* Evil Genius's "slight" optimization: Since GMimeDataWrapper::write_to_stream() * decodes its content stream to the raw format, we can cheat by requesting its * content stream and not doing any encoding on the data if the source and * destination encodings are identical. */ if (part->encoding != g_mime_data_wrapper_get_encoding (part->content)) { GMimeStream *filtered_stream; const char *filename; GMimeFilter *filter; switch (part->encoding) { case GMIME_CONTENT_ENCODING_UUENCODE: filename = g_mime_part_get_filename (part); nwritten = g_mime_stream_printf (stream, "begin 0644 %s\n", filename ? filename : "unknown"); if (nwritten == -1) return -1; total += nwritten; /* fall thru... */ case GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE: case GMIME_CONTENT_ENCODING_BASE64: filtered_stream = g_mime_stream_filter_new (stream); filter = g_mime_filter_basic_new (part->encoding, TRUE); g_mime_stream_filter_add (GMIME_STREAM_FILTER (filtered_stream), filter); g_object_unref (filter); break; default: filtered_stream = stream; g_object_ref (stream); break; } nwritten = g_mime_data_wrapper_write_to_stream (part->content, filtered_stream); g_mime_stream_flush (filtered_stream); g_object_unref (filtered_stream); if (nwritten == -1) return -1; total += nwritten; if (part->encoding == GMIME_CONTENT_ENCODING_UUENCODE) { /* FIXME: get rid of this special-case x-uuencode crap */ nwritten = g_mime_stream_write (stream, "end\n", 4); if (nwritten == -1) return -1; total += nwritten; } } else { GMimeStream *content_stream; content_stream = g_mime_data_wrapper_get_stream (part->content); g_mime_stream_reset (content_stream); nwritten = g_mime_stream_write_to_stream (content_stream, stream); g_mime_stream_reset (content_stream); if (nwritten == -1) return -1; total += nwritten; } return total; }
gboolean g_mime_gpgme_mpe_encrypt(GMimeMultipartEncrypted * mpe, GMimeObject * content, GPtrArray * recipients, gboolean trust_all, GtkWindow * parent, GError ** err) { GMimeStream *filtered_stream; GMimeStream *ciphertext; GMimeStream *stream; GMimePart *version_part; GMimePart *encrypted_part; GMimeDataWrapper *wrapper; GMimeFilter *crlf_filter; g_return_val_if_fail(GMIME_IS_MULTIPART_ENCRYPTED(mpe), FALSE); g_return_val_if_fail(GMIME_IS_OBJECT(content), FALSE); /* get the cleartext */ stream = g_mime_stream_mem_new(); filtered_stream = g_mime_stream_filter_new(stream); crlf_filter = g_mime_filter_crlf_new(TRUE, FALSE); g_mime_stream_filter_add(GMIME_STREAM_FILTER(filtered_stream), crlf_filter); g_object_unref(crlf_filter); g_mime_object_write_to_stream(content, filtered_stream); g_mime_stream_flush(filtered_stream); g_object_unref(filtered_stream); /* reset the content stream */ g_mime_stream_reset(stream); /* encrypt the content stream */ ciphertext = g_mime_stream_mem_new(); if (!libbalsa_gpgme_encrypt (recipients, NULL, stream, ciphertext, GPGME_PROTOCOL_OpenPGP, FALSE, trust_all, parent, err)) { g_object_unref(ciphertext); g_object_unref(stream); return FALSE; } g_object_unref(stream); g_mime_stream_reset(ciphertext); /* construct the version part */ version_part = g_mime_part_new_with_type("application", "pgp-encrypted"); g_mime_part_set_content_encoding(version_part, GMIME_CONTENT_ENCODING_7BIT); stream = g_mime_stream_mem_new_with_buffer("Version: 1\n", strlen("Version: 1\n")); wrapper = g_mime_data_wrapper_new_with_stream(stream, GMIME_CONTENT_ENCODING_7BIT); g_mime_part_set_content_object(version_part, wrapper); g_object_unref(wrapper); g_object_unref(stream); /* construct the encrypted mime part */ encrypted_part = g_mime_part_new_with_type("application", "octet-stream"); g_mime_part_set_content_encoding(encrypted_part, GMIME_CONTENT_ENCODING_7BIT); wrapper = g_mime_data_wrapper_new_with_stream(ciphertext, GMIME_CONTENT_ENCODING_7BIT); g_mime_part_set_content_object(encrypted_part, wrapper); g_object_unref(ciphertext); g_object_unref(wrapper); /* save the version and encrypted parts */ /* FIXME: make sure there aren't any other parts?? */ g_mime_multipart_add(GMIME_MULTIPART(mpe), GMIME_OBJECT(version_part)); g_mime_multipart_add(GMIME_MULTIPART(mpe), GMIME_OBJECT(encrypted_part)); g_object_unref(encrypted_part); g_object_unref(version_part); /* set the content-type params for this multipart/encrypted part */ g_mime_object_set_content_type_parameter(GMIME_OBJECT(mpe), "protocol", "application/pgp-encrypted"); g_mime_multipart_set_boundary(GMIME_MULTIPART(mpe), NULL); return TRUE; }