diff options
author | Rodrigo Arias Mallo <rodarima@gmail.com> | 2025-04-30 23:54:49 +0200 |
---|---|---|
committer | Rodrigo Arias Mallo <rodarima@gmail.com> | 2025-05-01 00:56:42 +0200 |
commit | 1573dea32bb1a3db7c35db68ed27a0f4e8b24194 (patch) | |
tree | cbd4a7b0332946a201a2aa7bbadf2a9be621b7b7 | |
parent | 0683b2f5935b6df26723bf89d5b71d002f99aaf2 (diff) |
Simplify algorithm to parse filename
Instead of keeping a escaped flag, we simply check again both the
previous and current character for non-backlash and quote. As the
filename can be empty if we don't get a maching quote, we stop there.
-rw-r--r-- | src/misc.h | 25 |
1 files changed, 18 insertions, 7 deletions
@@ -109,21 +109,32 @@ static inline void a_Misc_parse_content_disposition(const char *disposition, cha bool_t escaped = FALSE; const char *c; - unsigned int maxlen = strlen(s); - for (c = s; !(*c == '"' && !escaped); c++) { - if ((len = c - s + 1) > maxlen) { - return; + size_t maxlen = strlen(s); + + for (size_t i = 1; i < maxlen; i++) { + /* Find closing quote not escaped */ + if (s[i - 1] != '\\' && s[i] == '"') { + /* Copy i bytes, skip closing quote */ + len = i; + *filename = dStrndup(s, len); + break; } - escaped = *c == '\\'; } - *filename = dStrndup(s, len); } else { - for ( ; *s == '.'; ++s); + /* Ignore dots at the beginning of the filename */ + while (*s == '.') + s++; + if ((len = strcspn(s, terminators))) { *filename = dStrndup(s, len); } } + /* No filename, stop here */ + if (*filename == NULL) + return; + + /* Otherwise remove invalid characters from filename */ const char invalid_characters[] = "/\\|~"; char *f = *filename, *d = *filename; |