summary refs log tree commit diff
path: root/template.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--template.c35
1 files changed, 28 insertions, 7 deletions
diff --git a/template.c b/template.c
index afb9173..4f2d9dd 100644
--- a/template.c
+++ b/template.c
@@ -29,6 +29,25 @@ static int escapeNull(FILE *file, const char *str) {
 	return (n ? 0 : -1);
 }
 
+static const char SlashReplacement = ';';
+
+int escapePath(FILE *file, const char *str) {
+	while (*str) {
+		if (*str == '/') {
+			str++;
+			int n = fprintf(file, "%c", SlashReplacement);
+			if (n < 0) return n;
+		}
+		size_t len = strcspn(str, "/");
+		if (len) {
+			size_t n = fwrite(str, len, 1, file);
+			if (!n) return -1;
+		}
+		str += len;
+	}
+	return 0;
+}
+
 int escapeURL(FILE *file, const char *str) {
 	static const char *Safe = {
 		"$-_.+!*'(),"
@@ -44,7 +63,9 @@ int escapeURL(FILE *file, const char *str) {
 		}
 		str += len;
 		if (*str) {
-			int n = fprintf(file, "%%%02X", *str++);
+			char ch = *str++;
+			if (ch == '/') ch = SlashReplacement;
+			int n = fprintf(file, "%%%02X", ch);
 			if (n < 0) return n;
 		}
 	}
@@ -53,12 +74,6 @@ int escapeURL(FILE *file, const char *str) {
 
 int escapeXML(FILE *file, const char *str) {
 	while (*str) {
-		size_t len = strcspn(str, "\"&<");
-		if (len) {
-			size_t n = fwrite(str, len, 1, file);
-			if (!n) return -1;
-		}
-		str += len;
 		int n = 0;
 		switch (*str) {
 			break; case '"': str++; n = fprintf(file, "&quot;");
@@ -66,6 +81,12 @@ int escapeXML(FILE *file, const char *str) {
 			break; case '<': str++; n = fprintf(file, "&lt;");
 		}
 		if (n < 0) return n;
+		size_t len = strcspn(str, "\"&<");
+		if (len) {
+			size_t n = fwrite(str, len, 1, file);
+			if (!n) return -1;
+		}
+		str += len;
 	}
 	return 0;
 }