]>
diplodocus.org Git - nmh/blob - sbr/encode_rfc2047.c
0b11c8b3a7419deaa7157ef93b4f81ad33a456ae
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 **, int, const char *,
48 static int utf8len(const char *);
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
57 encode_rfc2047(const char *name
, char **value
, int encoding
,
60 int i
, asciicount
= 0, eightbitcount
= 0, qpspecialcount
= 0;
64 * First, check to see if we even need to encode the header
67 for (p
= *value
; *p
!= '\0'; p
++) {
68 if (isascii((int) *p
)) {
76 if (eightbitcount
== 0)
80 * Some rules from RFC 2047:
82 * - Encoded words cannot be more than 75 characters long
83 * - Multiple "long" encoded words must be on new lines.
85 * Also, we're not permitted to encode email addresses, so
86 * we need to actually _parse_ email addresses and only encode
91 * If charset was NULL, then get the value from the locale. But
92 * we reject it if it returns US-ASCII
96 charset
= write_charset_8bit();
98 if (strcasecmp(charset
, "US-ASCII") == 0) {
99 advise(NULL
, "Cannot use US-ASCII with 8 bit characters in header");
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.
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
);
114 * On the encoding we choose, and the specifics of encoding:
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.
122 if (encoding
== CE_UNKNOWN
)
123 encoding
= (eightbitcount
* 10 / (asciicount
+ eightbitcount
) > 5) ?
124 CE_BASE64
: CE_QUOTED
;
126 unfold_header(value
, asciicount
+ eightbitcount
);
132 return field_encode_base64(name
, value
, encoding
, charset
);
136 return field_encode_quoted(name
, value
, encoding
, charset
, asciicount
,
137 eightbitcount
+ qpspecialcount
);
140 advise(NULL
, "Internal error: unknown RFC-2047 encoding type");
146 * Encode our specified header using quoted-printable
150 field_encode_quoted(const char *name
, char **value
, int encoding
,
151 const char *charset
, int ascii
, int encoded
)
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
;
158 * Right now we just encode the whole thing. Maybe later on we'll
159 * only encode things on a per-atom basis.
164 column
= prefixlen
+ 2; /* Header name plus ": " */
166 utf8
= strcasecmp(charset
, "UTF-8") == 0;
170 * Start a new line, if it's time
174 * If it's the start of the header, we don't need to pad it
176 * The length of the output string is ...
177 * =?charset?Q?...?= so that's 7+strlen(charset) + 1 for NUL
179 * plus 1 for every ASCII character and 3 for every eight bit
180 * or special character (eight bit characters are written as =XX).
184 outlen
+= 8 + charsetlen
+ ascii
+ 3 * encoded
;
186 int curlen
= q
- output
, i
;
187 outlen
+= prefixlen
+ 1; /* Header plus \n ": " */
188 output
= mh_xrealloc(output
, outlen
);
193 for (i
= 0; i
< prefixlen
; i
++)
196 output
= mh_xmalloc(outlen
);
200 q
+= snprintf(q
, outlen
- (q
- output
), "=?%s?Q?", charset
);
206 * Process each character, encoding if necessary
214 } else if (!qpspecial(*p
)) {
218 snprintf(q
, outlen
- (q
- output
), "=%02X", (unsigned int) *p
);
227 * We're not allowed more than ENCODELINELIMIT characters per line,
228 * so reserve some room for the final ?=.
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).
237 if (column
>= ENCODELINELIMIT
- 2) {
241 * Okay, this is a bit weird, but to explain a bit more ...
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.
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.
253 if (column
+ (utf8len(p
) * 3) > ENCODELINELIMIT
- 2) {
269 * Calculate the length of a UTF-8 character.
271 * If it's not a UTF-8 character (or we're in the middle of a multibyte
272 * character) then simply return 0.
276 utf8len(const char *p
)
283 if (isascii((int) *p
) || (*((unsigned char *) p
) & 0xc0) == 0x80)
287 while ((*((unsigned char *) p
++) & 0xc0) == 0x80)
294 * "Unfold" a header, making it a single line (without continuation)
296 * We cheat a bit here; we never make the string longer, so using the
297 * original length here is fine.
301 unfold_header(char **value
, int len
)
303 char *str
= mh_xmalloc(len
+ 1);
304 char *p
= str
, *q
= *value
;
309 * When we get a newline, skip to the next non-whitespace
310 * character and add a space to replace all of the whitespace
330 field_encode_address(const char *name
, char **value
, int encoding
,