From 029790d3ff6e9fccfed2214efac777b8c438e318 Mon Sep 17 00:00:00 2001 From: Jani Nikula Date: Mon, 3 Feb 2014 21:51:43 +0200 Subject: util: make sanitize string available in string util for reuse No functional changes. --- util/string-util.c | 22 ++++++++++++++++++++++ util/string-util.h | 7 +++++++ 2 files changed, 29 insertions(+) (limited to 'util') diff --git a/util/string-util.c b/util/string-util.c index a5622d7a..9e2f728f 100644 --- a/util/string-util.c +++ b/util/string-util.c @@ -37,6 +37,28 @@ strtok_len (char *s, const char *delim, size_t *len) return *len ? s : NULL; } +char * +sanitize_string (const void *ctx, const char *str) +{ + char *out, *loop; + + if (! str) + return NULL; + + out = talloc_strdup (ctx, str); + if (! out) + return NULL; + + for (loop = out; *loop; loop++) { + if (*loop == '\t' || *loop == '\n') + *loop = ' '; + else if ((unsigned char)(*loop) < 32) + *loop = '?'; + } + + return out; +} + static int is_unquoted_terminator (unsigned char c) { diff --git a/util/string-util.h b/util/string-util.h index 0194607e..8a3ad19e 100644 --- a/util/string-util.h +++ b/util/string-util.h @@ -19,6 +19,13 @@ char *strtok_len (char *s, const char *delim, size_t *len); +/* Return a talloced string with str sanitized. + * + * Whitespace characters (tabs and newlines) are replaced with spaces, + * non-printable characters with question marks. + */ +char *sanitize_string (const void *ctx, const char *str); + /* Construct a boolean term query with the specified prefix (e.g., * "id") and search term, quoting term as necessary. Specifically, if * term contains any non-printable ASCII characters, non-ASCII -- cgit v1.2.3