]> diplodocus.org Git - nmh/blob - sbr/encode_rfc2047.c
0d3c2784b7f37a0b37f83834ea81eced45d97e92
[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(*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) + 1 for 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 += 8 + charsetlen + ascii + 3 * encoded;
184 if (output) {
185 int curlen = q - output, i;
186 outlen += prefixlen + 1; /* Header plus \n ": " */
187 output = mh_xrealloc(output, outlen);
188 q = output + curlen;
189 *q++ = '?';
190 *q++ = '=';
191 *q++ = '\n';
192 for (i = 0; i < prefixlen; i++)
193 *q++ = ' ';
194 } else {
195 output = mh_xmalloc(outlen);
196 q = output;
197 }
198
199 q += snprintf(q, outlen - (q - output), "=?%s?Q?", charset);
200 column = prefixlen;
201 newline = 0;
202 }
203
204 /*
205 * Process each character, encoding if necessary
206 */
207
208 column++;
209
210 if (*p == ' ') {
211 *q++ = '_';
212 ascii--;
213 } else if (!qpspecial(*p)) {
214 *q++ = *p;
215 ascii--;
216 } else {
217 snprintf(q, outlen - (q - output), "=%02X", (unsigned int) *p);
218 q += 3;
219 column += 3;
220 encoded--;
221 }
222
223 p++;
224
225 /*
226 * We're not allowed more than ENCODELINELIMIT characters per line,
227 * so reserve some room for the final ?=.
228 *
229 * If prefixlen == 0, we haven't been passed in a header name, so
230 * don't ever wrap the field (we're likely doing an address).
231 */
232
233 if (prefixlen == 0)
234 continue;
235
236 if (column >= ENCODELINELIMIT - 2) {
237 newline = 1;
238 } else if (utf8) {
239 /*
240 * Okay, this is a bit weird, but to explain a bit more ...
241 *
242 * RFC 2047 prohibits the splitting of multibyte characters
243 * across encoded words. Right now we only handle the case
244 * of UTF-8, the most common multibyte encoding.
245 *
246 * p is now pointing at the next input character. If we're
247 * using UTF-8 _and_ we'd go over ENCODELINELIMIT given the
248 * length of the complete character, then trigger a newline
249 * now. Note that we check the length * 3 since we have to
250 * allow for the encoded output.
251 */
252 if (column + (utf8len(p) * 3) > ENCODELINELIMIT - 2) {
253 newline = 1;
254 }
255 }
256 }
257
258 strcat(q, "?=");
259
260 free(*value);
261
262 *value = output;
263
264 return 0;
265 }
266
267 /*
268 * Calculate the length of a UTF-8 character.
269 *
270 * If it's not a UTF-8 character (or we're in the middle of a multibyte
271 * character) then simply return 0.
272 */
273
274 static int
275 utf8len(const char *p)
276 {
277 int len = 1;
278
279 if (*p == '\0')
280 return 0;
281
282 if (isascii((int) *p) || (*((unsigned char *) p) & 0xc0) == 0x80)
283 return 0;
284
285 p++;
286 while ((*((unsigned char *) p++) & 0xc0) == 0x80)
287 len++;
288
289 return len;
290 }
291
292 /*
293 * "Unfold" a header, making it a single line (without continuation)
294 *
295 * We cheat a bit here; we never make the string longer, so using the
296 * original length here is fine.
297 */
298
299 static void
300 unfold_header(char **value, int len)
301 {
302 char *str = mh_xmalloc(len + 1);
303 char *p = str, *q = *value;
304
305 while (*q != '\0') {
306 if (*q == '\n') {
307 /*
308 * When we get a newline, skip to the next non-whitespace
309 * character and add a space to replace all of the whitespace
310 */
311 while (is_fws(*q))
312 q++;
313 if (*q == '\0')
314 break;
315
316 *p++ = ' ';
317 } else {
318 *p++ = *q++;
319 }
320 }
321
322 *p = '\0';
323
324 free(*value);
325 *value = str;
326 }
327
328 static int
329 field_encode_address(const char *name, char **value, int encoding,
330 const char *charset)
331 {
332 return 0;
333 }