summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: e9d94ae)
raw | patch | inline | side by side (parent: e9d94ae)
author | Florian Forster <ff@octo.it> | |
Sat, 19 Jun 2010 08:35:13 +0000 (10:35 +0200) | ||
committer | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Sat, 19 Jun 2010 08:35:13 +0000 (10:35 +0200) |
src/utils_cgi.c | patch | blob | history | |
src/utils_cgi.h | patch | blob | history |
diff --git a/src/utils_cgi.c b/src/utils_cgi.c
index 184578869cacda2f8f062084d19835e677d0187a..ac9daca8bf690d6c0108ffaf9b3e467098489185 100644 (file)
--- a/src/utils_cgi.c
+++ b/src/utils_cgi.c
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 3a0105ac7c5d41fbff512b7af5dbfc8537cda23f..a5db9c3f6909d6f9cef470465ac18d311f20de9f 100644 (file)
--- a/src/utils_cgi.h
+++ b/src/utils_cgi.h
int time_to_rfc1123 (time_t t, char *buffer, size_t buffer_size);
+char *html_escape (const char *string);
+
#endif /* UTILS_CGI_H */