summaryrefslogtreecommitdiff
path: root/lib/log.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/log.c')
-rw-r--r--lib/log.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/log.c b/lib/log.c
index 8f14462b93..5821965478 100644
--- a/lib/log.c
+++ b/lib/log.c
@@ -1156,3 +1156,33 @@ zlog_hexdump (const void *mem, unsigned int len) {
}
zlog_debug("\n%s", buf);
}
+
+const char *
+zlog_sanitize (char *buf, size_t bufsz, const void *in, size_t inlen)
+{
+ const char *inbuf = in;
+ char *pos = buf, *end = buf + bufsz;
+ const char *iend = inbuf + inlen;
+
+ memset (buf, 0, bufsz);
+ for (; inbuf < iend; inbuf++)
+ {
+ /* don't write partial escape sequence */
+ if (end - pos < 5)
+ break;
+
+ if (*inbuf == '\n')
+ snprintf (pos, end - pos, "\\n");
+ else if (*inbuf == '\r')
+ snprintf (pos, end - pos, "\\r");
+ else if (*inbuf == '\t')
+ snprintf (pos, end - pos, "\\t");
+ else if (*inbuf < ' ' || *inbuf == '"' || *inbuf >= 127)
+ snprintf (pos, end - pos, "\\x%02hhx", *inbuf);
+ else
+ *pos = *inbuf;
+
+ pos += strlen (pos);
+ }
+ return buf;
+}