From: Florian Forster Date: Sat, 19 Jun 2010 08:35:13 +0000 (+0200) Subject: src/utils_cgi.[ch]: Implement "html_escape". X-Git-Tag: v4.0.0~230 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=66e1c8fe2814ddaf803ceee092f1d1dd02a5e981;p=collection4.git src/utils_cgi.[ch]: Implement "html_escape". --- diff --git a/src/utils_cgi.c b/src/utils_cgi.c index 1845788..ac9daca 100644 --- a/src/utils_cgi.c +++ b/src/utils_cgi.c @@ -288,4 +288,50 @@ int time_to_rfc1123 (time_t t, char *buffer, size_t buffer_size) /* {{{ */ return (0); } /* }}} int time_to_rfc1123 */ +#define COPY_ENTITY(e) do { \ + size_t len = strlen (e); \ + if (buffer_size < (len + 1)) \ + break; \ + strcpy (buffer_ptr, (e)); \ + buffer_ptr += len; \ + buffer_size -= len; \ +} while (0) + +char *html_escape (const char *string) /* {{{ */ +{ + char buffer[4096]; + char *buffer_ptr; + size_t buffer_size; + size_t pos; + + buffer[0] = 0; + buffer_ptr = &buffer[0]; + buffer_size = sizeof (buffer); + for (pos = 0; string[pos] != 0; pos++) + { + if (string[pos] == '"') + COPY_ENTITY ("""); + else if (string[pos] == '<') + COPY_ENTITY ("<"); + else if (string[pos] == '>') + COPY_ENTITY (">"); + else if (string[pos] == '&') + COPY_ENTITY ("&"); + else + { + *buffer_ptr = string[pos]; + buffer_ptr++; + buffer_size--; + *buffer_ptr = 0; + } + + if (buffer_size <= 1) + break; + } + + return (strdup (buffer)); +} /* }}} char *html_escape */ + +#undef COPY_ENTITY + /* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/utils_cgi.h b/src/utils_cgi.h index 3a0105a..a5db9c3 100644 --- a/src/utils_cgi.h +++ b/src/utils_cgi.h @@ -14,4 +14,6 @@ const char *script_name (void); int time_to_rfc1123 (time_t t, char *buffer, size_t buffer_size); +char *html_escape (const char *string); + #endif /* UTILS_CGI_H */