dovecot-2.0: Added str_tabescape*() to write escaped strings to ...

dovecot at dovecot.org dovecot at dovecot.org
Wed May 6 04:28:41 EEST 2009


details:   http://hg.dovecot.org/dovecot-2.0/rev/2dcf2f313329
changeset: 9232:2dcf2f313329
user:      Timo Sirainen <tss at iki.fi>
date:      Tue May 05 20:25:13 2009 -0400
description:
Added str_tabescape*() to write escaped strings to Dovecot's IPC protocols.

diffstat:

3 files changed, 46 insertions(+), 19 deletions(-)
src/auth/auth-stream.c |   21 ++-------------------
src/lib/strescape.c    |   39 +++++++++++++++++++++++++++++++++++++++
src/lib/strescape.h    |    5 +++++

diffs (97 lines):

diff -r 8f770c8ad198 -r 2dcf2f313329 src/auth/auth-stream.c
--- a/src/auth/auth-stream.c	Tue May 05 18:47:54 2009 -0400
+++ b/src/auth/auth-stream.c	Tue May 05 20:25:13 2009 -0400
@@ -2,6 +2,7 @@
 
 #include "auth-common.h"
 #include "str.h"
+#include "strescape.h"
 #include "ostream.h"
 #include "auth-request.h"
 #include "auth-stream.h"
@@ -35,25 +36,7 @@ void auth_stream_reply_add(struct auth_s
 	}
 	if (value != NULL) {
 		/* escape dangerous characters in the value */
-		for (; *value != '\0'; value++) {
-			switch (*value) {
-			case '\001':
-				str_append_c(reply->str, '\001');
-				str_append_c(reply->str, '1');
-				break;
-			case '\t':
-				str_append_c(reply->str, '\001');
-				str_append_c(reply->str, 't');
-				break;
-			case '\n':
-				str_append_c(reply->str, '\001');
-				str_append_c(reply->str, 'n');
-				break;
-			default:
-				str_append_c(reply->str, *value);
-				break;
-			}
-		}
+		str_tabescape_write(reply->str, value);
 	}
 }
 
diff -r 8f770c8ad198 -r 2dcf2f313329 src/lib/strescape.c
--- a/src/lib/strescape.c	Tue May 05 18:47:54 2009 -0400
+++ b/src/lib/strescape.c	Tue May 05 20:25:13 2009 -0400
@@ -71,3 +71,42 @@ char *str_unescape(char *str)
 	*dest = '\0';
 	return start;
 }
+
+void str_tabescape_write(string_t *dest, const char *src)
+{
+	for (; *src != '\0'; src++) {
+		switch (*src) {
+		case '\001':
+			str_append_c(dest, '\001');
+			str_append_c(dest, '1');
+			break;
+		case '\t':
+			str_append_c(dest, '\001');
+			str_append_c(dest, 't');
+			break;
+		case '\n':
+			str_append_c(dest, '\001');
+			str_append_c(dest, 'n');
+			break;
+		default:
+			str_append_c(dest, *src);
+			break;
+		}
+	}
+}
+
+const char *str_tabescape(const char *str)
+{
+	string_t *tmp;
+	const char *p;
+
+	for (p = str; *p != '\0'; p++) {
+		if (*p <= '\n') {
+			tmp = t_str_new(128);
+			str_append_n(tmp, str, p-str);
+			str_tabescape_write(tmp, p);
+			return str_c(tmp);
+		}
+	}
+	return str;
+}
diff -r 8f770c8ad198 -r 2dcf2f313329 src/lib/strescape.h
--- a/src/lib/strescape.h	Tue May 05 18:47:54 2009 -0400
+++ b/src/lib/strescape.h	Tue May 05 20:25:13 2009 -0400
@@ -12,4 +12,9 @@ void str_append_unescaped(string_t *dest
 /* remove all '\' characters */
 char *str_unescape(char *str);
 
+/* For Dovecot's internal protocols: Escape \001, \t and \n characters
+   using \001. */
+const char *str_tabescape(const char *str);
+void str_tabescape_write(string_t *dest, const char *src);
+
 #endif


More information about the dovecot-cvs mailing list