ESCAPE_SPECIAL escapes '\\', '\a', '\e' and '"' as one group. A caller
that wants '\\' escaped but needs '"' left untouched, because it has
its own handling for quotes or because quoting is not meaningful in
its output, currently has no way to pull just the backslash case out
of that set.

Add ESCAPE_BACKSLASH, which escapes only '\\'. It can be combined with
ESCAPE_SPACE, or used on its own, without pulling in ESCAPE_SPECIAL's
quote and control-character handling.

This only adds a new opt-in flag; no existing caller changes
behavior.

Suggested-by: Ilpo Järvinen <[email protected]>
Signed-off-by: Muhammad Bilal <[email protected]>
---
 include/linux/string_helpers.h |  3 ++-
 lib/string_helpers.c           | 23 +++++++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h
index 3fb88a1..111114d 100644
--- a/include/linux/string_helpers.h
+++ b/include/linux/string_helpers.h
@@ -72,8 +72,9 @@ static inline int string_unescape_any_inplace(char *buf)
 #define ESCAPE_NA              BIT(6)
 #define ESCAPE_NAP             BIT(7)
 #define ESCAPE_APPEND          BIT(8)
+#define ESCAPE_BACKSLASH       BIT(9)
 
-#define ESCAPE_ALL_MASK                GENMASK(8, 0)
+#define ESCAPE_ALL_MASK                GENMASK(9, 0)
 
 int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
                unsigned int flags, const char *only);
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 98d6ed0..f35234a 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -438,6 +438,24 @@ static bool escape_special(unsigned char c, char **dst, 
char *end)
        return true;
 }
 
+static bool escape_backslash(unsigned char c, char **dst, char *end)
+{
+       char *out = *dst;
+
+       if (c != '\\')
+               return false;
+
+       if (out < end)
+               *out = '\\';
+       ++out;
+       if (out < end)
+               *out = '\\';
+       ++out;
+
+       *dst = out;
+       return true;
+}
+
 static bool escape_null(unsigned char c, char **dst, char *end)
 {
        char *out = *dst;
@@ -558,6 +576,8 @@ static bool escape_hex(unsigned char c, char **dst, char 
*end)
  *             escape only non-printable or non-ascii characters
  *     %ESCAPE_APPEND:
  *             append characters from @only to be escaped by the given classes
+ *     %ESCAPE_BACKSLASH:
+ *             '\\' - backslash, without the rest of %ESCAPE_SPECIAL
  *
  * %ESCAPE_APPEND would help to pass additional characters to the escaped, when
  * one of %ESCAPE_NP, %ESCAPE_NA, or %ESCAPE_NAP is provided.
@@ -628,6 +648,9 @@ int string_escape_mem(const char *src, size_t isz, char 
*dst, size_t osz,
                if (flags & ESCAPE_SPECIAL && escape_special(c, &p, end))
                        continue;
 
+               if (flags & ESCAPE_BACKSLASH && escape_backslash(c, &p, end))
+                       continue;
+
                if (flags & ESCAPE_NULL && escape_null(c, &p, end))
                        continue;
 
-- 
2.43.0


Reply via email to