summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2020-09-14 12:05:49 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2020-09-14 15:50:26 +0200
commitddcd3d43c1132e5754fa91398dd2138a35660c43 (patch)
tree3a1b3600a08fa53b047b749afd733c1a93cdbab1 /lib
parent5fb2a0e8342424665a01d510ee64d742f586f877 (diff)
downloadbarebox-ddcd3d43c1132e5754fa91398dd2138a35660c43.tar.gz
barebox-ddcd3d43c1132e5754fa91398dd2138a35660c43.tar.xz
sandbox: support escaping commas in --image filenames
Some tools like afl-fuzz generate file names containing commas. Allow escaping the commas in the file names, so they can be passed to barebox. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/string.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/string.c b/lib/string.c
index 7548fd3581..50f8e2f87c 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -455,6 +455,49 @@ char * strsep(char **s, const char *ct)
#endif
EXPORT_SYMBOL(strsep);
+/**
+ * strsep_unescaped - Split a string into tokens, while ignoring escaped delimiters
+ * @s: The string to be searched
+ * @ct: The delimiter characters to search for
+ *
+ * strsep_unescaped() behaves like strsep unless it meets an escaped delimiter.
+ * In that case, it shifts the string back in memory to overwrite the escape's
+ * backslash then continues the search until an unescaped delimiter is found.
+ */
+char *strsep_unescaped(char **s, const char *ct)
+{
+ char *sbegin = *s, *hay;
+ const char *needle;
+ size_t shift = 0;
+
+ if (sbegin == NULL)
+ return NULL;
+
+ for (hay = sbegin; *hay != '\0'; ++hay) {
+ *hay = hay[shift];
+
+ if (*hay == '\\') {
+ *hay = hay[++shift];
+ if (*hay != '\\')
+ continue;
+ }
+
+ for (needle = ct; *needle != '\0'; ++needle) {
+ if (*hay == *needle)
+ goto match;
+ }
+ }
+
+ *s = NULL;
+ return sbegin;
+
+match:
+ *hay = '\0';
+ *s = &hay[shift + 1];
+
+ return sbegin;
+}
+
#ifndef __HAVE_ARCH_STRSWAB
/**
* strswab - swap adjacent even and odd bytes in %NUL-terminated string