1 /* escape_addresses.c -- Escape address components, hopefully per RFC 5322.
3 * This code is Copyright (c) 2012, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
11 static void escape_component (char *name
, size_t namesize
, char *chars
);
15 escape_display_name (char *name
, size_t namesize
)
17 char *specials
= "\"(),.:;<>@[\\]";
18 escape_component (name
, namesize
, specials
);
23 escape_local_part (char *name
, size_t namesize
)
25 /* wsp (whitespace) is horizontal tab or space, according to
27 char *specials_less_dot_plus_wsp
= " \"(),:;<>@[\\]";
28 escape_component (name
, namesize
, specials_less_dot_plus_wsp
);
32 /* Escape an address component, hopefully per RFC 5322. Assumes
33 one-byte characters. The char array pointed to by the name
34 argument is modified in place. Its size is specified by the
35 namesize argument. The need_escape argument is a string of
36 characters that require that name be escaped. */
38 escape_component (char *name
, size_t namesize
, char *chars_to_escape
)
40 /* If name contains any chars_to_escape:
42 2) escape any embedded "
44 if (strpbrk(name
, chars_to_escape
)) {
46 /* Maximum space requirement would be if each character had
47 to be escaped, plus enclosing double quotes, plus NUL terminator.
48 E.g., 2 characters, "", would require 7, "\"\""0, where that 0
50 char *tmp
= mh_xmalloc (2*strlen(name
) + 3);
52 for (destp
= tmp
, srcp
= name
; *srcp
; ++srcp
) {
54 /* Insert initial double quote, if needed. */
59 /* Escape embedded, unescaped double quote. */
60 if (*srcp
== '"' && *(srcp
+1) != '\0' && *(srcp
-1) != '\\') {
68 if (*(srcp
+1) == '\0') {
69 /* Insert final double quote, if needed. */
78 if (strcmp (tmp
, "\"")) {
79 size_t len
= destp
- tmp
;
80 assert ((ssize_t
) strlen(tmp
) + 1 == destp
- tmp
);
81 strncpy (name
, tmp
, min(len
, namesize
));
83 /* Handle just " as special case here instead of above. */
84 strncpy (name
, "\"\\\"\"", namesize
);
87 name
[namesize
- 1] = '\0';