aboutsummaryrefslogtreecommitdiff
path: root/pretty.c
diff options
context:
space:
mode:
authorJan H. Schönherr <schnhrr@cs.tu-berlin.de>2012-10-18 16:43:30 +0200
committerJunio C Hamano <gitster@pobox.com>2012-10-18 14:23:19 -0700
commit94f6cdf693c10eaec5a7fcc4f0b5cb1e0ea80f1b (patch)
tree835c1bf320e6fff66f48e66318ece2c9a1813950 /pretty.c
parent7a76e68a177720da65e7d9cfa49d702a55e2d9de (diff)
downloadgit-94f6cdf693c10eaec5a7fcc4f0b5cb1e0ea80f1b.tar.gz
git-94f6cdf693c10eaec5a7fcc4f0b5cb1e0ea80f1b.tar.xz
format-patch: do not wrap rfc2047 encoded headers too late
Encoded characters add more than one character at once to an encoded header. Include all characters that are about to be added in the length calculation for wrapping. Additionally, RFC 2047 imposes a maximum line length of 76 characters if that line contains an rfc2047 encoded word. Signed-off-by: Jan H. Schönherr <schnhrr@cs.tu-berlin.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'pretty.c')
-rw-r--r--pretty.c26
1 files changed, 16 insertions, 10 deletions
diff --git a/pretty.c b/pretty.c
index 71e402466..da7587999 100644
--- a/pretty.c
+++ b/pretty.c
@@ -263,6 +263,9 @@ static void add_rfc822_quoted(struct strbuf *out, const char *s, int len)
static int is_rfc2047_special(char ch)
{
+ if (ch == ' ' || ch == '\n')
+ return 1;
+
return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
}
@@ -270,6 +273,7 @@ static void add_rfc2047(struct strbuf *sb, const char *line, int len,
const char *encoding)
{
static const int max_length = 78; /* per rfc2822 */
+ static const int max_encoded_length = 76; /* per rfc2047 */
int i;
int line_len;
@@ -295,23 +299,25 @@ needquote:
line_len += strlen(encoding) + 5; /* 5 for =??q? */
for (i = 0; i < len; i++) {
unsigned ch = line[i] & 0xFF;
+ int is_special = is_rfc2047_special(ch);
+
+ /*
+ * According to RFC 2047, we could encode the special character
+ * ' ' (space) with '_' (underscore) for readability. But many
+ * programs do not understand this and just leave the
+ * underscore in place. Thus, we do nothing special here, which
+ * causes ' ' to be encoded as '=20', avoiding this problem.
+ */
- if (line_len >= max_length - 2) {
+ if (line_len + 2 + (is_special ? 3 : 1) > max_encoded_length) {
strbuf_addf(sb, "?=\n =?%s?q?", encoding);
line_len = strlen(encoding) + 5 + 1; /* =??q? plus SP */
}
- /*
- * We encode ' ' using '=20' even though rfc2047
- * allows using '_' for readability. Unfortunately,
- * many programs do not understand this and just
- * leave the underscore in place.
- */
- if (is_rfc2047_special(ch) || ch == ' ' || ch == '\n') {
+ if (is_special) {
strbuf_addf(sb, "=%02X", ch);
line_len += 3;
- }
- else {
+ } else {
strbuf_addch(sb, ch);
line_len++;
}