according to RFC5322 a header must be passed unfolded to further processings.
If the WSP after CRLF is replaced with =20 by RFC2047 encoding the folding is no longer recognized.
So WSP indicating a folding must not be processed by the RFC2047 encoding.
Because a WSP is not valid in an RFC2047 encoded header-chunk every folded line has to be encoded seperately.
---
pretty.c | 25 ++++++++++++++++++++-----
1 files changed, 20 insertions(+), 5 deletions(-)
Show changes to pretty.c +20 −5
diff --git a/pretty.c b/pretty.c
index f85444b..8a78a4e 100644
--- a/pretty.c
+++ b/pretty.c
@@ -216,7 +216,7 @@ static int is_rfc2047_special(char ch)
static void add_rfc2047(struct strbuf *sb, const char *line, int len,
const char *encoding)
{
- int i, last;
+ int i, last, num_foldings;
for (i = 0; i < len; i++) {
int ch = line[i];
@@ -229,8 +229,14 @@ static void add_rfc2047(struct strbuf *sb, const char *line, int len,
return;
needquote:
- strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
+ num_foldings=0;
+ for (i = 1; i < len; i++)
+ if(line[i]==' '&&line[i]=='\n')
+ num_foldings++;
+
+ strbuf_grow(sb, len * 3 + num_foldings*(7+strlen(encoding)) + 100);
strbuf_addf(sb, "=?%s?q?", encoding);
+ unsigned last_ch=0;
for (i = last = 0; i < len; i++) {
unsigned ch = line[i] & 0xFF;
/*
@@ -240,10 +246,19 @@ needquote:
* leave the underscore in place.
*/
if (is_rfc2047_special(ch) || ch == ' ') {
- strbuf_add(sb, line + last, i - last);
- strbuf_addf(sb, "=%02X", ch);
- last = i + 1;
+ if(!(ch == ' '&& last_ch=='\n')){
+ strbuf_add(sb, line + last, i - last);
+ strbuf_addf(sb, "=%02X", ch);
+ }
+ else{
+ if(i>last+1)
+ strbuf_add(sb, line + last, i - last-1);
+ strbuf_addstr(sb, "?=\n ");
+ strbuf_addf(sb, "=?%s?q?", encoding);
+ }
+ last = i + 1;
}
+ last_ch=ch;
}
strbuf_add(sb, line + last, len - last);
strbuf_addstr(sb, "?=");
--
1.7.1