]>
diplodocus.org Git - nmh/blob - sbr/encode_rfc2047.c
0f040bc9e7dfce490c5f06b304abad903f9ea7e9
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 static void unfold_header(char **, int);
41 static int field_encode_address(const char *, char **, int, const char *);
44 * Encode a message header using RFC 2047 encoding. We make the assumption
45 * that all characters < 128 are ASCII and as a consequence don't need any
50 encode_rfc2047(const char *name
, char **value
, int encoding
,
53 int i
, count
= 0, len
;
57 * First, check to see if we even need to encode the header
60 for (p
= *value
; *p
!= '\0'; p
++) {
61 if (! isascii((int) *p
))
69 * Some rules from RFC 2047:
71 * - Encoded words cannot be more than 75 characters long
72 * - Multiple "long" encoded words must be on new lines.
74 * Also, we're not permitted to encode email addresses, so
75 * we need to actually _parse_ email addresses and only encode
80 * If charset was NULL, then get the value from the locale. But
81 * we reject it if it returns US-ASCII
85 charset
= write_charset_8bit();
87 if (strcasecmp(charset
, "US-ASCII") == 0) {
88 advise(NULL
, "Cannot use US-ASCII with 8 bit characters in header");
93 * If we have an address header, then we need to parse the addresses
94 * and only encode the names or comments. Otherwise, handle it normally.
97 for (i
= 0; address_headers
[i
]; i
++) {
98 if (strcasecmp(name
, address_headers
[i
]) == 0)
99 return field_encode_address(name
, value
, encoding
, charset
);
103 * On the encoding we choose, and the specifics of encoding:
105 * - If a specified encoding is passed in, we use that.
106 * - If more than 50% of the characters are high-bit, we use base64
107 * and encode the whole field as one atom (possibly split).
108 * Otherwise, we use quoted-printable.
109 * - If more than 10% of the characters are high-bit, then we encode
110 * the entire header as one (possibly split) atom. Otherwise,
111 * take each atom as they come and encode it on a per-atom basis.
114 len
= strlen(*value
);
116 if (encoding
== CE_UNKNOWN
)
117 encoding
= (count
* 10 / len
> 5) ? CE_BASE64
: CE_QUOTED
;
122 return field_encode_base64(value
, charset
, len
, NULL
);
125 if (count
* 100 / len
> 10) {
126 return field_encode_quoted(value
, charset
, len
, NULL
);
129 * Break it down by atoms.
132 unfold_header(value
, len
);
135 advise(NULL
, "Internal error: unknown RFC-2047 encoding type");
143 * "Unfold" a header, making it a single line (without continuation)
145 * We cheat a bit here; we never make the string longer, so using the
146 * original length here is fine.
150 unfold_header(char **value
, int len
)
152 char *str
= mh_xmalloc(len
+ 1);
153 char *p
= str
, *q
= *value
;
158 * When we get a newline, skip to the next non-whitespace
159 * character and add a space to replace all of the whitespace