diff src/readpst.c @ 407:24871e6cdd69

Stuart C. Naifeh - fix rfc2231 encoding when saving messages to both .eml and msg formats
author Carl Byington <carl@five-ten-sg.com>
date Sat, 27 Mar 2021 14:53:01 -0700
parents c1b1bbd42696
children
line wrap: on
line diff
--- a/src/readpst.c	Tue Jun 16 17:18:28 2020 -0700
+++ b/src/readpst.c	Sat Mar 27 14:53:01 2021 -0700
@@ -1179,6 +1179,41 @@
     return res;
 }
 
+/** Convert inp to rfc2231 encoding of string
+ *
+ *  @param inp   pointer to the string of interest
+ *  @return      pointer to converted string -- caller must free
+ */
+char *rfc2231_string(char *inp) {
+    int needs = 0;
+    const int8_t *x = (int8_t *)inp;
+    while (*x) {
+        if (*x <= 32) needs++;
+        x++;
+    }
+    int n = strlen(inp) + 2*needs + 15;
+    char *buffer = pst_malloc(n);
+    strcpy(buffer, "utf-8''");
+    x = (int8_t *)inp;
+    const uint8_t *y = (uint8_t *)inp;
+    uint8_t *z = (uint8_t *)buffer;
+    z += strlen(buffer);    // skip the utf8 prefix
+    while (*y) {
+        if (*x <= 32) {
+            *(z++) = (uint8_t)'%';
+            snprintf(z, 3, "%2x", *y);
+            z += 2;
+        }
+        else {
+            *(z++) = *y;
+        }
+        x++;
+        y++;
+    }
+    *z = '\0';
+    return buffer;
+}
+
 void write_inline_attachment(FILE* f_output, pst_item_attach* attach, char *boundary, pst_file* pst)
 {
     DEBUG_ENT("write_inline_attachment");
@@ -1210,8 +1245,10 @@
         // use the long filename, converted to proper encoding if needed.
         // it is already utf8
         char *escaped = quote_string(attach->filename2.str);
-        pst_rfc2231(&attach->filename2);
-        fprintf(f_output, "Content-Disposition: attachment; \n        filename*=%s;\n", attach->filename2.str);
+        // encode long filename as rfc2231 without modifying original -- we may still need the original long filename
+        char *rfc2231 = rfc2231_string(attach->filename2.str);
+        fprintf(f_output, "Content-Disposition: attachment; \n        filename*=%s;\n", rfc2231);
+        free (rfc2231);
         // Also include the (escaped) utf8 filename in the 'filename' header directly - this is not strictly valid
         // (since this header should be ASCII) but is almost always handled correctly (and in fact this is the only
         // way to get MS Outlook to correctly read a UTF8 filename, AFAICT, which is why we're doing it).