summaryrefslogtreecommitdiff
path: root/main/callerid.c
diff options
context:
space:
mode:
authorKinsey Moore <kmoore@digium.com>2014-08-27 15:39:35 +0000
committerKinsey Moore <kmoore@digium.com>2014-08-27 15:39:35 +0000
commitbf850181076c786d2c9e5e7f23e205547c7140cb (patch)
tree3a384bb94539e77298c351ae79dcff9453bc0b25 /main/callerid.c
parentd199536a04690e3034d02243e197071d70fc4f9c (diff)
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 ........ Merged revisions 422154 from http://svn.asterisk.org/svn/asterisk/branches/13 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@422158 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'main/callerid.c')
-rw-r--r--main/callerid.c63
1 files changed, 28 insertions, 35 deletions
diff --git a/main/callerid.c b/main/callerid.c
index e3b2b139e..803d41195 100644
--- a/main/callerid.c
+++ b/main/callerid.c
@@ -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;
}