]> diplodocus.org Git - nmh/blob - sbr/encode_rfc2047.c
0f040bc9e7dfce490c5f06b304abad903f9ea7e9
[nmh] / sbr / encode_rfc2047.c
1 /*
2 * Routines to encode message headers using RFC 2047-encoding.
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/mhparse.h>
11 #include <h/utils.h>
12
13 /*
14 * List of headers that contain addresses and as a result require special
15 * handling
16 */
17
18 static char *address_headers[] = {
19 "To",
20 "From",
21 "cc",
22 "Bcc",
23 "Reply-To",
24 "Sender",
25 "Resent-To",
26 "Resent-From",
27 "Resent-cc",
28 "Resent-Bcc",
29 "Resent-Reply-To",
30 "Resent-Sender",
31 NULL,
32 };
33
34 /*
35 * Macros we use for parsing headers
36 */
37
38 #define is_fws(c) (c == '\t' || c == ' ')
39
40 static void unfold_header(char **, int);
41 static int field_encode_address(const char *, char **, int, const char *);
42
43 /*
44 * Encode a message header using RFC 2047 encoding. We make the assumption
45 * that all characters < 128 are ASCII and as a consequence don't need any
46 * encoding.
47 */
48
49 int
50 encode_rfc2047(const char *name, char **value, int encoding,
51 const char *charset)
52 {
53 int i, count = 0, len;
54 char *p;
55
56 /*
57 * First, check to see if we even need to encode the header
58 */
59
60 for (p = *value; *p != '\0'; p++) {
61 if (! isascii((int) *p))
62 count++;
63 }
64
65 if (count == 0)
66 return 0;
67
68 /*
69 * Some rules from RFC 2047:
70 *
71 * - Encoded words cannot be more than 75 characters long
72 * - Multiple "long" encoded words must be on new lines.
73 *
74 * Also, we're not permitted to encode email addresses, so
75 * we need to actually _parse_ email addresses and only encode
76 * the right bits.
77 */
78
79 /*
80 * If charset was NULL, then get the value from the locale. But
81 * we reject it if it returns US-ASCII
82 */
83
84 if (charset == NULL)
85 charset = write_charset_8bit();
86
87 if (strcasecmp(charset, "US-ASCII") == 0) {
88 advise(NULL, "Cannot use US-ASCII with 8 bit characters in header");
89 return 1;
90 }
91
92 /*
93 * If we have an address header, then we need to parse the addresses
94 * and only encode the names or comments. Otherwise, handle it normally.
95 */
96
97 for (i = 0; address_headers[i]; i++) {
98 if (strcasecmp(name, address_headers[i]) == 0)
99 return field_encode_address(name, value, encoding, charset);
100 }
101
102 /*
103 * On the encoding we choose, and the specifics of encoding:
104 *
105 * - If a specified encoding is passed in, we use that.
106 * - If more than 50% of the characters are high-bit, we use base64
107 * and encode the whole field as one atom (possibly split).
108 * Otherwise, we use quoted-printable.
109 * - If more than 10% of the characters are high-bit, then we encode
110 * the entire header as one (possibly split) atom. Otherwise,
111 * take each atom as they come and encode it on a per-atom basis.
112 */
113
114 len = strlen(*value);
115
116 if (encoding == CE_UNKNOWN)
117 encoding = (count * 10 / len > 5) ? CE_BASE64 : CE_QUOTED;
118
119 switch (encoding) {
120
121 case CE_BASE64:
122 return field_encode_base64(value, charset, len, NULL);
123
124 case CE_QUOTED:
125 if (count * 100 / len > 10) {
126 return field_encode_quoted(value, charset, len, NULL);
127 } else {
128 /*
129 * Break it down by atoms.
130 */
131
132 unfold_header(value, len);
133 }
134 default:
135 advise(NULL, "Internal error: unknown RFC-2047 encoding type");
136 return 1;
137 }
138
139 return 0;
140 }
141
142 /*
143 * "Unfold" a header, making it a single line (without continuation)
144 *
145 * We cheat a bit here; we never make the string longer, so using the
146 * original length here is fine.
147 */
148
149 static void
150 unfold_header(char **value, int len)
151 {
152 char *str = mh_xmalloc(len + 1);
153 char *p = str, *q = *value;
154
155 while (*q != '\0') {
156 if (*q == '\n') {
157 /*
158 * When we get a newline, skip to the next non-whitespace
159 * character and add a space to replace all of the whitespace
160 */
161 while (is_fws(*q))
162 q++;
163 if (*q == '\0')
164 break;
165
166 *p++ = ' ';
167 } else {
168 *p++ = *q++;
169 }
170 }
171
172 *p = '\0';
173
174 free(*value);
175 *value = str;
176 }