From c5575f297c9e68bb0eaad671b4afb25ebe441186 Mon Sep 17 00:00:00 2001 From: hiro Date: Tue, 15 May 2007 04:34:58 +0000 Subject: use quoted-printable or base64 to protect trailing spaces on PGP/MIME signing except for ISO-2022-JP. git-svn-id: svn://sylpheed.sraoss.jp/sylpheed/trunk@1693 ee746299-78ed-0310-b773-934348b2243d --- libsylph/procmime.c | 37 ++++++++++++++++++++++++++++++++++++- libsylph/procmime.h | 3 ++- 2 files changed, 38 insertions(+), 2 deletions(-) (limited to 'libsylph') diff --git a/libsylph/procmime.c b/libsylph/procmime.c index de5c92ba..24fa4441 100644 --- a/libsylph/procmime.c +++ b/libsylph/procmime.c @@ -1,6 +1,6 @@ /* * LibSylph -- E-Mail client library - * Copyright (C) 1999-2006 Hiroyuki Yamamoto + * Copyright (C) 1999-2007 Hiroyuki Yamamoto * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -1671,6 +1671,41 @@ EncodingType procmime_get_encoding_for_text_file(const gchar *file) } } +EncodingType procmime_get_encoding_for_str(const gchar *str) +{ + const guchar *p; + size_t octet_chars = 0; + size_t total_len = 0; + gfloat octet_percentage; + + total_len = strlen(str); + + for (p = str; *p != '\0'; ++p) { + if (*p & 0x80) + ++octet_chars; + } + + if (total_len > 0) + octet_percentage = (gfloat)octet_chars / (gfloat)total_len; + else + octet_percentage = 0.0; + + debug_print("procmime_get_encoding_for_str(): " + "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len, + 100.0 * octet_percentage); + + if (octet_percentage > 0.20) { + debug_print("using BASE64\n"); + return ENC_BASE64; + } else if (octet_chars > 0) { + debug_print("using quoted-printable\n"); + return ENC_QUOTED_PRINTABLE; + } else { + debug_print("using 7bit\n"); + return ENC_7BIT; + } +} + const gchar *procmime_get_encoding_str(EncodingType encoding) { static const gchar *encoding_str[] = { diff --git a/libsylph/procmime.h b/libsylph/procmime.h index 735cef68..8d5b1855 100644 --- a/libsylph/procmime.h +++ b/libsylph/procmime.h @@ -1,6 +1,6 @@ /* * LibSylph -- E-Mail client library - * Copyright (C) 1999-2006 Hiroyuki Yamamoto + * Copyright (C) 1999-2007 Hiroyuki Yamamoto * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -197,6 +197,7 @@ gint procmime_execute_open_file (const gchar *file, EncodingType procmime_get_encoding_for_charset (const gchar *charset); EncodingType procmime_get_encoding_for_text_file(const gchar *file); +EncodingType procmime_get_encoding_for_str (const gchar *str); const gchar *procmime_get_encoding_str (EncodingType encoding); #endif /* __PROCMIME_H__ */ -- cgit v1.2.3