CallerID: Fix parsing of malformed callerid

This allows the callerid parsing function to handle malformed input
strings and strings containing escaped and unescaped double quotes.
This also adds a unittest to cover many of the cases where the parsing
algorithm previously failed.

Review: https://reviewboard.asterisk.org/r/3923/
Review: https://reviewboard.asterisk.org/r/3933/
........

Merged revisions 422112 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........

Merged revisions 422113 from http://svn.asterisk.org/svn/asterisk/branches/11
........

Merged revisions 422114 from http://svn.asterisk.org/svn/asterisk/branches/12


git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/13@422154 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Kinsey Moore
2014-08-27 15:31:35 +00:00
parent e985cb076d
commit a4a58c2771
7 changed files with 342 additions and 41 deletions

View File

@@ -1009,50 +1009,39 @@ int ast_is_shrinkable_phonenumber(const char *exten)
int ast_callerid_parse(char *instr, char **name, char **location)
{
char *ns, *ne, *ls, *le;
char *ls, *le, *name_start;
/* Try "name" <location> format or name <location> format */
if ((ls = strrchr(instr, '<')) && (le = strrchr(ls, '>'))) {
*ls = *le = '\0'; /* location found, trim off the brackets */
*location = ls + 1; /* and this is the result */
if ((ns = strchr(instr, '"')) && (ne = strchr(ns + 1, '"'))) {
*ns = *ne = '\0'; /* trim off the quotes */
*name = ns + 1; /* and this is the name */
} else if (ns) {
/* An opening quote was found but no closing quote was. The closing
* quote may actually be after the end of the bracketed number
*/
if (strchr(le + 1, '\"')) {
*ns = '\0';
*name = ns + 1;
ast_trim_blanks(*name);
} else {
*name = NULL;
}
} else { /* no quotes, trim off leading and trailing spaces */
*name = ast_skip_blanks(instr);
ast_trim_blanks(*name);
/* Handle surrounding quotes */
instr = ast_strip_quoted(instr, "\"", "\"");
/* Try "name" <location> format or name <location> format or with a missing > */
if ((ls = strrchr(instr, '<'))) {
if ((le = strrchr(ls, '>'))) {
*le = '\0'; /* location found, trim off the brackets */
}
*ls = '\0';
*location = ls + 1; /* and this is the result */
name_start = ast_strip_quoted(instr, "\"", "\"");
} else { /* no valid brackets */
char tmp[256];
ast_copy_string(tmp, instr, sizeof(tmp));
ast_shrink_phone_number(tmp);
if (ast_isphonenumber(tmp)) { /* Assume it's just a location */
*name = NULL;
name_start = NULL;
strcpy(instr, tmp); /* safe, because tmp will always be the same size or smaller than instr */
*location = instr;
} else { /* Assume it's just a name. */
*location = NULL;
if ((ns = strchr(instr, '"')) && (ne = strchr(ns + 1, '"'))) {
*ns = *ne = '\0'; /* trim off the quotes */
*name = ns + 1; /* and this is the name */
} else { /* no quotes, trim off leading and trailing spaces */
*name = ast_skip_blanks(instr);
ast_trim_blanks(*name);
}
name_start = ast_strip_quoted(instr, "\"", "\"");
}
}
if (name_start) {
ast_unescape_quoted(name_start);
}
*name = name_start;
return 0;
}
@@ -1079,14 +1068,18 @@ char *ast_callerid_merge(char *buf, int bufsiz, const char *name, const char *nu
{
if (!unknown)
unknown = "<unknown>";
if (name && num)
snprintf(buf, bufsiz, "\"%s\" <%s>", name, num);
else if (name)
if (name && num) {
char name_buf[128];
ast_escape_quoted(name, name_buf, sizeof(name_buf));
snprintf(buf, bufsiz, "\"%s\" <%s>", name_buf, num);
} else if (name) {
ast_copy_string(buf, name, bufsiz);
else if (num)
} else if (num) {
ast_copy_string(buf, num, bufsiz);
else
} else {
ast_copy_string(buf, unknown, bufsiz);
}
return buf;
}

View File

@@ -483,6 +483,29 @@ char *ast_escape_quoted(const char *string, char *outbuf, int buflen)
return outbuf;
}
void ast_unescape_quoted(char *quote_str)
{
int esc_pos;
int unesc_pos;
int quote_str_len = strlen(quote_str);
for (esc_pos = 0, unesc_pos = 0;
esc_pos < quote_str_len;
esc_pos++, unesc_pos++) {
if (quote_str[esc_pos] == '\\') {
/* at least one more char and current is \\ */
esc_pos++;
if (esc_pos >= quote_str_len) {
break;
}
}
quote_str[unesc_pos] = quote_str[esc_pos];
}
quote_str[unesc_pos] = '\0';
}
int ast_xml_escape(const char *string, char * const outbuf, const size_t buflen)
{
char *dst = outbuf;