From 4d35c86401b4a947689eac375ec4ba29b3c99d6d Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Sun, 19 Apr 2020 11:27:26 -0400 Subject: Use template system for paths and URLs This probably still needs a lot of cleaning up. --- template.c | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'template.c') 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, """); @@ -66,6 +81,12 @@ int escapeXML(FILE *file, const char *str) { break; case '<': str++; n = fprintf(file, "<"); } 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; } -- cgit 1.4.1