commit - 50d24d45982e590e07486cdd9d7f179ffb4ff36e
commit + e6c0de4de8004e5fd84119c0bc64da0e7cab9789
blob - d3d9f5b576990bd6c961932089e6f40541d8d481
blob + 0b616bc1fd469cf0cb48bc2a8e48e025840129f1
--- template/tmpl.c
+++ template/tmpl.c
return (0);
}
+static inline int
+htmlescape(struct template *tp, char c)
+{
+ switch (c) {
+ case '<':
+ return tp_write(tp, "<", 4);
+ case '>':
+ return tp_write(tp, ">", 4);
+ case '&':
+ return tp_write(tp, "&", 5);
+ case '"':
+ return tp_write(tp, """, 6);
+ case '\'':
+ return tp_write(tp, "'", 6);
+ default:
+ return tp_write(tp, &c, 1);
+ }
+}
+
int
tp_htmlescape(struct template *tp, const char *str)
{
- int r;
-
if (str == NULL)
return (0);
for (; *str; ++str) {
- switch (*str) {
- case '<':
- r = tp_write(tp, "<", 4);
- break;
- case '>':
- r = tp_write(tp, ">", 4);
- break;
- case '&':
- r = tp_write(tp, "&", 5);
- break;
- case '"':
- r = tp_write(tp, """, 6);
- break;
- case '\'':
- r = tp_write(tp, "'", 6);
- break;
- default:
- r = tp_write(tp, str, 1);
- break;
- }
+ if (htmlescape(tp, *str) == -1)
+ return (-1);
+ }
- if (r == -1)
+ return (0);
+}
+
+int
+tp_write_htmlescape(struct template *tp, const char *str, size_t len)
+{
+ size_t i;
+
+ for (i = 0; i < len; ++i) {
+ if (htmlescape(tp, str[i]) == -1)
return (-1);
}
blob - df3b74c2696e16f2a591fb656220ac7957c59a56
blob + 3246735b8276b8bcf62702651f4c61e069bf7158
--- template/tmpl.h
+++ template/tmpl.h
__attribute__((__format__ (printf, 2, 3)));
int tp_urlescape(struct template *, const char *);
int tp_htmlescape(struct template *, const char *);
+int tp_write_htmlescape(struct template *, const char *, size_t);
struct template *template(void *, tmpl_write, char *, size_t);
int template_flush(struct template *);