]>
diplodocus.org Git - nmh/blob - sbr/encode_rfc2047.c
3bd22ac38ce7f8cb6868c707ba4e93485c7d63c0
2 * Routines to encode message headers using RFC 2047-encoding.
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.
10 #include <h/mhparse.h>
14 * List of headers that contain addresses and as a result require special
18 static char *address_headers
[] = {
35 * Macros we use for parsing headers
38 #define is_fws(c) (c == '\t' || c == ' ')
40 #define qpspecial(c) (c < ' ' || c == '=' || c == '?' || c == '_')
42 #define ENCODELINELIMIT 76
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 *);
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
56 encode_rfc2047(const char *name
, char **value
, int encoding
,
59 int i
, asciicount
= 0, eightbitcount
= 0, qpspecialcount
= 0;
63 * First, check to see if we even need to encode the header
66 for (p
= *value
; *p
!= '\0'; p
++) {
67 if (isascii((int) *p
)) {
69 if (qpspecial((int) *p
))
75 if (eightbitcount
== 0)
79 * Some rules from RFC 2047:
81 * - Encoded words cannot be more than 75 characters long
82 * - Multiple "long" encoded words must be on new lines.
84 * Also, we're not permitted to encode email addresses, so
85 * we need to actually _parse_ email addresses and only encode
90 * If charset was NULL, then get the value from the locale. But
91 * we reject it if it returns US-ASCII
95 charset
= write_charset_8bit();
97 if (strcasecmp(charset
, "US-ASCII") == 0) {
98 advise(NULL
, "Cannot use US-ASCII with 8 bit characters in header");
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.
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
);
113 * On the encoding we choose, and the specifics of encoding:
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.
121 if (encoding
== CE_UNKNOWN
)
122 encoding
= (eightbitcount
* 10 / (asciicount
+ eightbitcount
) > 5) ?
123 CE_BASE64
: CE_QUOTED
;
125 unfold_header(value
, asciicount
+ eightbitcount
);
131 return field_encode_base64(name
, value
, encoding
, charset
);
135 return field_encode_quoted(name
, value
, charset
, asciicount
,
136 eightbitcount
+ qpspecialcount
);
139 advise(NULL
, "Internal error: unknown RFC-2047 encoding type");
145 * Encode our specified header using quoted-printable
149 field_encode_quoted(const char *name
, char **value
, const char *charset
,
150 int ascii
, int encoded
)
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
;
157 * Right now we just encode the whole thing. Maybe later on we'll
158 * only encode things on a per-atom basis.
163 column
= prefixlen
+ 2; /* Header name plus ": " */
165 utf8
= strcasecmp(charset
, "UTF-8") == 0;
169 * Start a new line, if it's time
173 * If it's the start of the header, we don't need to pad it
175 * The length of the output string is ...
176 * =?charset?Q?...?= so that's 7+strlen(charset) + 2 for \n NUL
178 * plus 1 for every ASCII character and 3 for every eight bit
179 * or special character (eight bit characters are written as =XX).
183 outlen
+= 9 + charsetlen
+ ascii
+ 3 * encoded
;
186 * If output is set, then we're continuing the header. Otherwise
187 * do the initial allocation.
191 int curlen
= q
- output
, i
;
192 outlen
+= prefixlen
+ 1; /* Header plus \n ": " */
193 output
= mh_xrealloc(output
, outlen
);
198 for (i
= 0; i
< prefixlen
; i
++)
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.
206 output
= mh_xmalloc(outlen
);
212 q
+= snprintf(q
, outlen
- (q
- output
), "=?%s?Q?", charset
);
218 * Process each character, encoding if necessary
226 } else if (isascii((int) *p
) && !qpspecial((int) *p
)) {
230 snprintf(q
, outlen
- (q
- output
), "=%02X", *((unsigned char *) p
));
239 * We're not allowed more than ENCODELINELIMIT characters per line,
240 * so reserve some room for the final ?=.
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).
249 if (column
>= ENCODELINELIMIT
- 2) {
253 * Okay, this is a bit weird, but to explain a bit more ...
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.
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.
265 if (column
+ (utf8len(p
) * 3) > ENCODELINELIMIT
- 2) {
281 * Calculate the length of a UTF-8 character.
283 * If it's not a UTF-8 character (or we're in the middle of a multibyte
284 * character) then simply return 0.
288 utf8len(const char *p
)
295 if (isascii((int) *p
) || (*((unsigned char *) p
) & 0xc0) == 0x80)
299 while ((*((unsigned char *) p
++) & 0xc0) == 0x80)
306 * "Unfold" a header, making it a single line (without continuation)
308 * We cheat a bit here; we never make the string longer, so using the
309 * original length here is fine.
313 unfold_header(char **value
, int len
)
315 char *str
= mh_xmalloc(len
+ 1);
316 char *p
= str
, *q
= *value
;
321 * When we get a newline, skip to the next non-whitespace
322 * character and add a space to replace all of the whitespace
324 * This has the side effect of stripping off the final newline
325 * for the header; we put it back in the encoding routine.
345 field_encode_address(const char *name
, char **value
, int encoding
,