]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: add zlog_sanitize function
authorDavid Lamparter <equinox@opensourcerouting.org>
Thu, 2 Mar 2017 16:33:37 +0000 (17:33 +0100)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Tue, 7 Mar 2017 00:40:26 +0000 (19:40 -0500)
Signed-off-by: Christian Franke <chris@opensourcerouting.org>
lib/log.c
lib/log.h

index baf7bdbca13aefdea8c13f31642129bf868664dd..f40029043555a269b50cf354e43dd311247133b7 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
@@ -1151,3 +1151,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;
+}
index 3413cae5a9a86ee4d9159b564941285598016ba4..43fc0130dbdbc1b2250555b00e1285ca4d8e3d08 100644 (file)
--- a/lib/log.h
+++ b/lib/log.h
@@ -188,6 +188,7 @@ extern size_t quagga_timestamp(int timestamp_precision /* # subsecond digits */,
                               char *buf, size_t buflen);
 
 extern void zlog_hexdump(const void *mem, unsigned int len);
+extern const char *zlog_sanitize(char *buf, size_t bufsz, const void *in, size_t inlen);
 
 
 extern int