{"thread":{"id":"25529","subject":"[PATCH] correct folding of email-headers in conjunction with RFC2047","startedAt":"2010-10-23T10:35:54Z","lastAt":"2010-10-23T10:35:54Z","messageCount":1,"participants":["Martin Krüger"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"154233","messageId":"1287830154-12205-1-git-send-email-martin.krueger@gmx.com","threadId":"25529","inReplyTo":null,"subject":"[PATCH] correct folding of email-headers in conjunction with RFC2047","fromName":"Martin Krüger","fromEmail":"martin.krueger@gmx.com","sentAt":"2010-10-23T10:35:54Z","receivedAt":"2010-10-23T10:35:54Z","isPatch":true,"sender":{"key":"martin.krueger@gmx.com","avatar":null},"body":"according to RFC5322 a header must be passed unfolded to further processings.\nIf the WSP after CRLF is replaced with =20 by RFC2047 encoding the folding is no longer recognized.\nSo WSP indicating a folding must not be processed by the RFC2047 encoding.\nBecause a WSP is not valid in an RFC2047 encoded header-chunk every folded line has to be encoded seperately.\n---\n pretty.c |   25 ++++++++++++++++++++-----\n 1 files changed, 20 insertions(+), 5 deletions(-)\n\ndiff --git a/pretty.c b/pretty.c\nindex f85444b..8a78a4e 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -216,7 +216,7 @@ static int is_rfc2047_special(char ch)\n static void add_rfc2047(struct strbuf *sb, const char *line, int len,\n \t\t       const char *encoding)\n {\n-\tint i, last;\n+\tint i, last, num_foldings;\n \n \tfor (i = 0; i < len; i++) {\n \t\tint ch = line[i];\n@@ -229,8 +229,14 @@ static void add_rfc2047(struct strbuf *sb, const char *line, int len,\n \treturn;\n \n needquote:\n-\tstrbuf_grow(sb, len * 3 + strlen(encoding) + 100);\n+        num_foldings=0;\n+        for (i = 1; i < len; i++)\n+          if(line[i]==' '&&line[i]=='\\n')\n+           num_foldings++;  \n+ \n+\tstrbuf_grow(sb, len * 3 + num_foldings*(7+strlen(encoding)) + 100);\n \tstrbuf_addf(sb, \"=?%s?q?\", encoding);\n+        unsigned last_ch=0;\n \tfor (i = last = 0; i < len; i++) {\n \t\tunsigned ch = line[i] & 0xFF;\n \t\t/*\n@@ -240,10 +246,19 @@ needquote:\n \t\t * leave the underscore in place.\n \t\t */\n \t\tif (is_rfc2047_special(ch) || ch == ' ') {\n-\t\t\tstrbuf_add(sb, line + last, i - last);\n-\t\t\tstrbuf_addf(sb, \"=%02X\", ch);\n-\t\t\tlast = i + 1;\n+                    if(!(ch == ' '&& last_ch=='\\n')){\n+                        strbuf_add(sb, line + last, i - last);\n+\t\t\tstrbuf_addf(sb, \"=%02X\", ch);   \n+                    }\n+                    else{\n+                     if(i>last+1)\n+                      strbuf_add(sb, line + last, i - last-1);\n+                     strbuf_addstr(sb, \"?=\\n \");\n+                     strbuf_addf(sb, \"=?%s?q?\", encoding);\n+                    }\n+                  last = i + 1;\n \t\t}\n+           last_ch=ch;\n \t}\n \tstrbuf_add(sb, line + last, len - last);\n \tstrbuf_addstr(sb, \"?=\");\n-- \n1.7.1\n"}]}