[gmime] Fixed address quoting logic and IDN2 encoding



commit da168513c91f9ed25d2e22c5b6a23fa50266a3bc
Author: Jeffrey Stedfast <jestedfa microsoft com>
Date:   Sun Jul 22 11:12:31 2018 -0400

    Fixed address quoting logic and IDN2 encoding
    
    The rules for quoting address names should use 'specials'
    instead of 'tspecials' and when encoding domain names via
    IDN2, check if the encoded domain matches the original
    domain name (other than case). If they match, prefer the
    non-encoded domain name since the user may have used
    uppercase characters to enhance readability of the domain
    name.

 gmime/gen-table.c               |  1 +
 gmime/gmime-table-private.h     |  1 +
 gmime/gmime-utils.c             |  8 ++++----
 gmime/internet-address.c        | 11 ++++++++---
 tests/data/mbox/output/jwz.mbox |  2 +-
 tests/test-mime.c               |  2 +-
 6 files changed, 16 insertions(+), 9 deletions(-)
---
diff --git a/gmime/gen-table.c b/gmime/gen-table.c
index cae40eda..57c09e58 100644
--- a/gmime/gen-table.c
+++ b/gmime/gen-table.c
@@ -180,6 +180,7 @@ int main (int argc, char **argv)
        
        printf ("#define is_ctrl(x) ((gmime_special_table[(unsigned char)(x)] & IS_CTRL) != 0)\n");
        printf ("#define is_lwsp(x) ((gmime_special_table[(unsigned char)(x)] & IS_LWSP) != 0)\n");
+       printf ("#define is_special(x) ((gmime_special_table[(unsigned char)(x)] & IS_SPECIAL) != 0)\n");
        printf ("#define is_tspecial(x) ((gmime_special_table[(unsigned char)(x)] & IS_TSPECIAL) != 0)\n");
        printf ("#define is_type(x, t) ((gmime_special_table[(unsigned char)(x)] & (t)) != 0)\n");
        printf ("#define is_ttoken(x) ((gmime_special_table[(unsigned char)(x)] & 
(IS_TSPECIAL|IS_LWSP|IS_CTRL)) == 0)\n");
diff --git a/gmime/gmime-table-private.h b/gmime/gmime-table-private.h
index 5db07011..28acf24c 100644
--- a/gmime/gmime-table-private.h
+++ b/gmime/gmime-table-private.h
@@ -44,6 +44,7 @@ enum {
 
 #define is_ctrl(x) ((gmime_special_table[(unsigned char)(x)] & IS_CTRL) != 0)
 #define is_lwsp(x) ((gmime_special_table[(unsigned char)(x)] & IS_LWSP) != 0)
+#define is_special(x) ((gmime_special_table[(unsigned char)(x)] & IS_SPECIAL) != 0)
 #define is_tspecial(x) ((gmime_special_table[(unsigned char)(x)] & IS_TSPECIAL) != 0)
 #define is_type(x, t) ((gmime_special_table[(unsigned char)(x)] & (t)) != 0)
 #define is_ttoken(x) ((gmime_special_table[(unsigned char)(x)] & (IS_TSPECIAL|IS_LWSP|IS_CTRL)) == 0)
diff --git a/gmime/gmime-utils.c b/gmime/gmime-utils.c
index e0e902ae..f6cc6aa8 100644
--- a/gmime/gmime-utils.c
+++ b/gmime/gmime-utils.c
@@ -902,7 +902,7 @@ need_quotes (const char *string)
                        inptr++;
                else if (*inptr == '"')
                        quoted = !quoted;
-               else if (!quoted && (is_tspecial (*inptr) || *inptr == '.'))
+               else if (!quoted && is_special (*inptr))
                        return TRUE;
                
                if (*inptr)
@@ -916,12 +916,12 @@ need_quotes (const char *string)
  * g_mime_utils_quote_string:
  * @str: input string
  *
- * Quotes @string as needed according to the rules in rfc2045.
+ * Quotes @string as needed according to the rules in rfc2822.
  * 
  * Returns: an allocated string containing the escaped and quoted (if
  * needed to be) input string. The decision to quote the string is
- * based on whether or not the input string contains any 'tspecials'
- * as defined by rfc2045.
+ * based on whether or not the input string contains any 'specials'
+ * as defined by rfc2822.
  **/
 char *
 g_mime_utils_quote_string (const char *str)
diff --git a/gmime/internet-address.c b/gmime/internet-address.c
index afb17bbc..d515088e 100644
--- a/gmime/internet-address.c
+++ b/gmime/internet-address.c
@@ -463,13 +463,18 @@ internet_address_mailbox_get_idn_addr (InternetAddressMailbox *mailbox)
        
 #ifdef LIBIDN
        if (!mailbox->idn_addr && mailbox->at > 0) {
+               const char *domain = mailbox->addr + mailbox->at + 1;
+               
                encoded = g_string_new ("");
                g_string_append_len (encoded, mailbox->addr, mailbox->at + 1);
-               if (idn2_to_ascii_8z (mailbox->addr + mailbox->at + 1, &ascii, 0) == IDN2_OK) {
-                       g_string_append (encoded, ascii);
+               if (idn2_to_ascii_8z (domain, &ascii, 0) == IDN2_OK) {
+                       if (!g_ascii_strcasecmp (domain, ascii))
+                               g_string_append (encoded, domain);
+                       else
+                               g_string_append (encoded, ascii);
                        idn2_free (ascii);
                } else {
-                       g_string_append (encoded, mailbox->addr + mailbox->at + 1);
+                       g_string_append (encoded, domain);
                }
                
                mailbox->idn_addr = g_string_free (encoded, FALSE);
diff --git a/tests/data/mbox/output/jwz.mbox b/tests/data/mbox/output/jwz.mbox
index 1d1fe19b..51d35dbd 100644
--- a/tests/data/mbox/output/jwz.mbox
+++ b/tests/data/mbox/output/jwz.mbox
@@ -1440,7 +1440,7 @@ message offsets: 9021425, 9028449
 header offsets: 9021457, 9022593
 From - Thu Nov 21 13:20:54 1996
 From: Eric Rosenquist <rosenqui strataware com>
-To: "S/MIME Developers" <smime-dev RSA COM>
+To: S/MIME Developers <smime-dev RSA COM>
 Subject: My encryption certificate for S/MIME testing
 Date: Thu, 21 Nov 1996 16:10:23 -0500
 Content-Type: multipart/mixed
diff --git a/tests/test-mime.c b/tests/test-mime.c
index f2b82544..07ba1f6a 100644
--- a/tests/test-mime.c
+++ b/tests/test-mime.c
@@ -125,7 +125,7 @@ static struct {
          "charles broken host com",
          "charles broken host com" },
        { "fpons mandrakesoft com (=?iso-8859-1?q?Fran=E7ois?= Pons likes _'s and       's too)", 
"iso-8859-1",
-         "\"Fran\xc3\xa7ois Pons likes _'s and         's too\" <fpons mandrakesoft com>",
+         "Fran\xc3\xa7ois Pons likes _'s and   's too <fpons mandrakesoft com>",
          "=?iso-8859-1?q?Fran=E7ois?= Pons likes _'s and       's too <fpons mandrakesoft com>" },
        { "T\x81\xf5ivo Leedj\x81\xe4rv <leedjarv interest ee>", NULL,
          "T\xc2\x81\xc3\xb5ivo Leedj\xc2\x81\xc3\xa4rv <leedjarv interest ee>",


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]