]> diplodocus.org Git - nmh/blob - sbr/encode_rfc2047.c
3bd22ac38ce7f8cb6868c707ba4e93485c7d63c0
[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 #define qpspecial(c) (c < ' ' || c == '=' || c == '?' || c == '_')
41
42 #define ENCODELINELIMIT 76
43
44 static void unfold_header(char **, int);
45 static int field_encode_address(const char *, char **, int, const char *);
46 static int field_encode_quoted(const char *, char **, const char *, int, int);
47 static int utf8len(const char *);
48
49 /*
50 * Encode a message header using RFC 2047 encoding. We make the assumption
51 * that all characters < 128 are ASCII and as a consequence don't need any
52 * encoding.
53 */
54
55 int
56 encode_rfc2047(const char *name, char **value, int encoding,
57 const char *charset)
58 {
59 int i, asciicount = 0, eightbitcount = 0, qpspecialcount = 0;
60 char *p;
61
62 /*
63 * First, check to see if we even need to encode the header
64 */
65
66 for (p = *value; *p != '\0'; p++) {
67 if (isascii((int) *p)) {
68 asciicount++;
69 if (qpspecial((int) *p))
70 qpspecialcount++;
71 } else
72 eightbitcount++;
73 }
74
75 if (eightbitcount == 0)
76 return 0;
77
78 /*
79 * Some rules from RFC 2047:
80 *
81 * - Encoded words cannot be more than 75 characters long
82 * - Multiple "long" encoded words must be on new lines.
83 *
84 * Also, we're not permitted to encode email addresses, so
85 * we need to actually _parse_ email addresses and only encode
86 * the right bits.
87 */
88
89 /*
90 * If charset was NULL, then get the value from the locale. But
91 * we reject it if it returns US-ASCII
92 */
93
94 if (charset == NULL)
95 charset = write_charset_8bit();
96
97 if (strcasecmp(charset, "US-ASCII") == 0) {
98 advise(NULL, "Cannot use US-ASCII with 8 bit characters in header");
99 return 1;
100 }
101
102 /*
103 * If we have an address header, then we need to parse the addresses
104 * and only encode the names or comments. Otherwise, handle it normally.
105 */
106
107 for (i = 0; address_headers[i]; i++) {
108 if (strcasecmp(name, address_headers[i]) == 0)
109 return field_encode_address(name, value, encoding, charset);
110 }
111
112 /*
113 * On the encoding we choose, and the specifics of encoding:
114 *
115 * - If a specified encoding is passed in, we use that.
116 * - If more than 50% of the characters are high-bit, we use base64
117 * and encode the whole field as one atom (possibly split).
118 * - Otherwise, we use quoted-printable.
119 */
120
121 if (encoding == CE_UNKNOWN)
122 encoding = (eightbitcount * 10 / (asciicount + eightbitcount) > 5) ?
123 CE_BASE64 : CE_QUOTED;
124
125 unfold_header(value, asciicount + eightbitcount);
126
127 switch (encoding) {
128
129 #if 0
130 case CE_BASE64:
131 return field_encode_base64(name, value, encoding, charset);
132 #endif
133
134 case CE_QUOTED:
135 return field_encode_quoted(name, value, charset, asciicount,
136 eightbitcount + qpspecialcount);
137
138 default:
139 advise(NULL, "Internal error: unknown RFC-2047 encoding type");
140 return 1;
141 }
142 }
143
144 /*
145 * Encode our specified header using quoted-printable
146 */
147
148 static int
149 field_encode_quoted(const char *name, char **value, const char *charset,
150 int ascii, int encoded)
151 {
152 int prefixlen = name ? strlen(name) + 2: 0, outlen = 0, column, newline = 1;
153 int charsetlen = strlen(charset), utf8;
154 char *output = NULL, *p, *q;
155
156 /*
157 * Right now we just encode the whole thing. Maybe later on we'll
158 * only encode things on a per-atom basis.
159 */
160
161 p = *value;
162
163 column = prefixlen + 2; /* Header name plus ": " */
164
165 utf8 = strcasecmp(charset, "UTF-8") == 0;
166
167 while (*p != '\0') {
168 /*
169 * Start a new line, if it's time
170 */
171 if (newline) {
172 /*
173 * If it's the start of the header, we don't need to pad it
174 *
175 * The length of the output string is ...
176 * =?charset?Q?...?= so that's 7+strlen(charset) + 2 for \n NUL
177 *
178 * plus 1 for every ASCII character and 3 for every eight bit
179 * or special character (eight bit characters are written as =XX).
180 *
181 */
182
183 outlen += 9 + charsetlen + ascii + 3 * encoded;
184
185 /*
186 * If output is set, then we're continuing the header. Otherwise
187 * do the initial allocation.
188 */
189
190 if (output) {
191 int curlen = q - output, i;
192 outlen += prefixlen + 1; /* Header plus \n ": " */
193 output = mh_xrealloc(output, outlen);
194 q = output + curlen;
195 *q++ = '?';
196 *q++ = '=';
197 *q++ = '\n';
198 for (i = 0; i < prefixlen; i++)
199 *q++ = ' ';
200 } else {
201 /*
202 * A bit of a hack here; the header can contain multiple
203 * spaces (probably at least one) until we get to the
204 * actual text. Copy until we get to a non-space.
205 */
206 output = mh_xmalloc(outlen);
207 q = output;
208 while (is_fws(*p))
209 *q++ = *p++;
210 }
211
212 q += snprintf(q, outlen - (q - output), "=?%s?Q?", charset);
213 column = prefixlen;
214 newline = 0;
215 }
216
217 /*
218 * Process each character, encoding if necessary
219 */
220
221 column++;
222
223 if (*p == ' ') {
224 *q++ = '_';
225 ascii--;
226 } else if (isascii((int) *p) && !qpspecial((int) *p)) {
227 *q++ = *p;
228 ascii--;
229 } else {
230 snprintf(q, outlen - (q - output), "=%02X", *((unsigned char *) p));
231 q += 3;
232 column += 3;
233 encoded--;
234 }
235
236 p++;
237
238 /*
239 * We're not allowed more than ENCODELINELIMIT characters per line,
240 * so reserve some room for the final ?=.
241 *
242 * If prefixlen == 0, we haven't been passed in a header name, so
243 * don't ever wrap the field (we're likely doing an address).
244 */
245
246 if (prefixlen == 0)
247 continue;
248
249 if (column >= ENCODELINELIMIT - 2) {
250 newline = 1;
251 } else if (utf8) {
252 /*
253 * Okay, this is a bit weird, but to explain a bit more ...
254 *
255 * RFC 2047 prohibits the splitting of multibyte characters
256 * across encoded words. Right now we only handle the case
257 * of UTF-8, the most common multibyte encoding.
258 *
259 * p is now pointing at the next input character. If we're
260 * using UTF-8 _and_ we'd go over ENCODELINELIMIT given the
261 * length of the complete character, then trigger a newline
262 * now. Note that we check the length * 3 since we have to
263 * allow for the encoded output.
264 */
265 if (column + (utf8len(p) * 3) > ENCODELINELIMIT - 2) {
266 newline = 1;
267 }
268 }
269 }
270
271 strcat(q, "?=\n");
272
273 free(*value);
274
275 *value = output;
276
277 return 0;
278 }
279
280 /*
281 * Calculate the length of a UTF-8 character.
282 *
283 * If it's not a UTF-8 character (or we're in the middle of a multibyte
284 * character) then simply return 0.
285 */
286
287 static int
288 utf8len(const char *p)
289 {
290 int len = 1;
291
292 if (*p == '\0')
293 return 0;
294
295 if (isascii((int) *p) || (*((unsigned char *) p) & 0xc0) == 0x80)
296 return 0;
297
298 p++;
299 while ((*((unsigned char *) p++) & 0xc0) == 0x80)
300 len++;
301
302 return len;
303 }
304
305 /*
306 * "Unfold" a header, making it a single line (without continuation)
307 *
308 * We cheat a bit here; we never make the string longer, so using the
309 * original length here is fine.
310 */
311
312 static void
313 unfold_header(char **value, int len)
314 {
315 char *str = mh_xmalloc(len + 1);
316 char *p = str, *q = *value;
317
318 while (*q != '\0') {
319 if (*q == '\n') {
320 /*
321 * When we get a newline, skip to the next non-whitespace
322 * character and add a space to replace all of the whitespace
323 *
324 * This has the side effect of stripping off the final newline
325 * for the header; we put it back in the encoding routine.
326 */
327 while (is_fws(*q++))
328 ;
329 if (*q == '\0')
330 break;
331
332 *p++ = ' ';
333 } else {
334 *p++ = *q++;
335 }
336 }
337
338 *p = '\0';
339
340 free(*value);
341 *value = str;
342 }
343
344 static int
345 field_encode_address(const char *name, char **value, int encoding,
346 const char *charset)
347 {
348 return 0;
349 }