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