changeset 2689:631611c2d6e6 HEAD

Added string sanitization functions.
author Timo Sirainen <tss@iki.fi>
date Tue, 05 Oct 2004 18:29:38 +0300
parents 606c79327434
children 7b50711a168d
files src/lib/Makefile.am src/lib/str-sanitize.c src/lib/str-sanitize.h
diffstat 3 files changed, 48 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/Makefile.am	Tue Oct 05 03:50:28 2004 +0300
+++ b/src/lib/Makefile.am	Tue Oct 05 18:29:38 2004 +0300
@@ -59,6 +59,7 @@
 	sendfile-util.c \
 	sha1.c \
 	str.c \
+	str-sanitize.c \
 	strescape.c \
 	strfuncs.c \
 	unlink-directory.c \
@@ -117,6 +118,7 @@
 	sendfile-util.h \
 	sha1.h \
 	str.h \
+	str-sanitize.h \
 	strescape.h \
 	strfuncs.h \
 	unlink-directory.h \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/str-sanitize.c	Tue Oct 05 18:29:38 2004 +0300
@@ -0,0 +1,37 @@
+/* Copyright (c) 2004 Timo Sirainen */
+
+#include "lib.h"
+#include "str.h"
+#include "str-sanitize.h"
+
+void str_sanitize_append(string_t *dest, const char *src, size_t max_len)
+{
+	const char *p;
+
+	for (p = src; *p != '\0'; p++) {
+		if ((unsigned char)*p < 32)
+			break;
+	}
+
+	str_append_n(dest, src, (size_t)(p - src));
+	for (; *p != '\0' && max_len > 0; p++, max_len--) {
+		if ((unsigned char)*p < 32)
+			str_append_c(dest, '?');
+		else
+			str_append_c(dest, *p);
+	}
+
+	if (*p != '\0') {
+		str_truncate(dest, str_len(dest)-3);
+		str_append(dest, "...");
+	}
+}
+
+const char *str_sanitize(const char *src, size_t max_len)
+{
+	string_t *str;
+
+	str = t_str_new(I_MIN(max_len, 256));
+	str_sanitize_append(str, src, max_len);
+	return str_c(str);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/str-sanitize.h	Tue Oct 05 18:29:38 2004 +0300
@@ -0,0 +1,9 @@
+#ifndef __STR_SANITIZE_H
+#define __STR_SANITIZE_H
+
+/* All control characters in src will be appended as '?'. If src is longer
+   than max_len, it's truncated with "..." appended to the end. */
+void str_sanitize_append(string_t *dest, const char *src, size_t max_len);
+const char *str_sanitize(const char *src, size_t max_len);
+
+#endif