diff --git a/inotify.c b/inotify.c index a021ce0..ae0b3aa 100644 --- a/inotify.c +++ b/inotify.c @@ -704,7 +704,7 @@ start_inotify() i += EVENT_SIZE + event->len; continue; } - esc_name = modifyString(strdup(event->name), "&", "&amp;"); + esc_name = modifyString(strdup(event->name), "&", "&amp;", 0); snprintf(path_buf, sizeof(path_buf), "%s/%s", get_path_from_wd(event->wd), event->name); if ( event->mask & IN_ISDIR && (event->mask & (IN_CREATE|IN_MOVED_TO)) ) { diff --git a/playlist.c b/playlist.c index 508c4c3..b9fe8ad 100644 --- a/playlist.c +++ b/playlist.c @@ -236,7 +236,7 @@ found: DPRINTF(E_DEBUG, L_SCANNER, "- %s not found in db\n", fname); if( strchr(fname, '\\') ) { - fname = modifyString(fname, "\\", "/"); + fname = modifyString(fname, "\\", "/", 1); goto retry; } else if( (fname = strchr(fname, '/')) ) diff --git a/tivo_commands.c b/tivo_commands.c index 2af72f9..617d2f1 100644 --- a/tivo_commands.c +++ b/tivo_commands.c @@ -118,11 +118,11 @@ SendFormats(struct upnphttp *h, const char *sformat) static char * tivo_unescape_tag(char *tag) { - modifyString(tag, "&amp;", "&"); - modifyString(tag, "&amp;lt;", "<"); - modifyString(tag, "&lt;", "<"); - modifyString(tag, "&amp;gt;", ">"); - modifyString(tag, "&gt;", ">"); + modifyString(tag, "&amp;", "&", 1); + modifyString(tag, "&amp;lt;", "<", 1); + modifyString(tag, "&lt;", "<", 1); + modifyString(tag, "&amp;gt;", ">", 1); + modifyString(tag, "&gt;", ">", 1); return tag; } diff --git a/utils.c b/utils.c index 328254c..da6e2c6 100644 --- a/utils.c +++ b/utils.c @@ -31,6 +31,7 @@ #include "minidlnatypes.h" #include "upnpglobalvars.h" +#include "utils.h" #include "log.h" int @@ -143,20 +144,27 @@ strcasestrc(const char *s, const char *p, const char t) } char * -modifyString(char * string, const char * before, const char * after) +modifyString(char *string, const char *before, const char *after, int noalloc) { int oldlen, newlen, chgcnt = 0; char *s, *p; + /* If there is no match, just return */ + s = strstr(string, before); + if (!s) + return string; + oldlen = strlen(before); newlen = strlen(after); - if( newlen > oldlen ) + if (newlen > oldlen) { - s = string; - while( (p = strstr(s, before)) ) + if (noalloc) + return string; + + while ((p = strstr(s, before))) { chgcnt++; - s = p+oldlen; + s = p + oldlen; } s = realloc(string, strlen(string)+((newlen-oldlen)*chgcnt)+1); /* If we failed to realloc, return the original alloc'd string */ @@ -167,10 +175,10 @@ modifyString(char * string, const char * before, const char * after) } s = string; - while( s ) + while (s) { - p = strcasestr(s, before); - if( !p ) + p = strstr(s, before); + if (!p) return string; memmove(p + newlen, p + oldlen, strlen(p + oldlen) + 1); memcpy(p, after, newlen); @@ -189,10 +197,10 @@ unescape_tag(const char *tag, int force_alloc) || strstr(tag, """) ) { esc_tag = strdup(tag); - esc_tag = modifyString(esc_tag, "&", "&"); - esc_tag = modifyString(esc_tag, "<", "<"); - esc_tag = modifyString(esc_tag, ">", ">"); - esc_tag = modifyString(esc_tag, """, "\""); + esc_tag = modifyString(esc_tag, "&", "&", 1); + esc_tag = modifyString(esc_tag, "<", "<", 1); + esc_tag = modifyString(esc_tag, ">", ">", 1); + esc_tag = modifyString(esc_tag, """, "\"", 1); } else if( force_alloc ) esc_tag = strdup(tag); @@ -208,10 +216,10 @@ escape_tag(const char *tag, int force_alloc) if( strchr(tag, '&') || strchr(tag, '<') || strchr(tag, '>') || strchr(tag, '"') ) { esc_tag = strdup(tag); - esc_tag = modifyString(esc_tag, "&", "&amp;"); - esc_tag = modifyString(esc_tag, "<", "&lt;"); - esc_tag = modifyString(esc_tag, ">", "&gt;"); - esc_tag = modifyString(esc_tag, "\"", "&quot;"); + esc_tag = modifyString(esc_tag, "&", "&amp;", 0); + esc_tag = modifyString(esc_tag, "<", "&lt;", 0); + esc_tag = modifyString(esc_tag, ">", "&gt;", 0); + esc_tag = modifyString(esc_tag, "\"", "&quot;", 0); } else if( force_alloc ) esc_tag = strdup(tag); diff --git a/utils.h b/utils.h index 9fa5fb8..64724a6 100644 --- a/utils.h +++ b/utils.h @@ -60,7 +60,7 @@ int ends_with(const char * haystack, const char * needle); char *trim(char *str); char *strstrc(const char *s, const char *p, const char t); char *strcasestrc(const char *s, const char *p, const char t); -char *modifyString(char * string, const char * before, const char * after); +char *modifyString(char *string, const char *before, const char *after, int noalloc); char *escape_tag(const char *tag, int force_alloc); char *unescape_tag(const char *tag, int force_alloc); void strip_ext(char * name);