Blame


1 6509b181 2022-12-30 thomas /*
2 96b8c570 2022-12-30 thomas * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 6509b181 2022-12-30 thomas *
4 6509b181 2022-12-30 thomas * Permission to use, copy, modify, and distribute this software for any
5 6509b181 2022-12-30 thomas * purpose with or without fee is hereby granted, provided that the above
6 6509b181 2022-12-30 thomas * copyright notice and this permission notice appear in all copies.
7 6509b181 2022-12-30 thomas *
8 6509b181 2022-12-30 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 6509b181 2022-12-30 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 6509b181 2022-12-30 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 6509b181 2022-12-30 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 6509b181 2022-12-30 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 6509b181 2022-12-30 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 6509b181 2022-12-30 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 6509b181 2022-12-30 thomas */
16 6509b181 2022-12-30 thomas
17 6509b181 2022-12-30 thomas #include <ctype.h>
18 6509b181 2022-12-30 thomas #include <stdio.h>
19 6509b181 2022-12-30 thomas #include <stdlib.h>
20 6509b181 2022-12-30 thomas
21 6509b181 2022-12-30 thomas #include "tmpl.h"
22 6509b181 2022-12-30 thomas
23 6509b181 2022-12-30 thomas int
24 6509b181 2022-12-30 thomas tp_urlescape(struct template *tp, const char *str)
25 6509b181 2022-12-30 thomas {
26 6509b181 2022-12-30 thomas int r;
27 6509b181 2022-12-30 thomas char tmp[4];
28 6509b181 2022-12-30 thomas
29 6509b181 2022-12-30 thomas if (str == NULL)
30 6509b181 2022-12-30 thomas return (0);
31 6509b181 2022-12-30 thomas
32 6509b181 2022-12-30 thomas for (; *str; ++str) {
33 6509b181 2022-12-30 thomas if (iscntrl((unsigned char)*str) ||
34 6509b181 2022-12-30 thomas isspace((unsigned char)*str) ||
35 6509b181 2022-12-30 thomas *str == '\'' || *str == '"' || *str == '\\') {
36 6509b181 2022-12-30 thomas r = snprintf(tmp, sizeof(tmp), "%%%2X", *str);
37 6509b181 2022-12-30 thomas if (r < 0 || (size_t)r >= sizeof(tmp))
38 6509b181 2022-12-30 thomas return (0);
39 6509b181 2022-12-30 thomas if (tp->tp_puts(tp, tmp) == -1)
40 6509b181 2022-12-30 thomas return (-1);
41 6509b181 2022-12-30 thomas } else {
42 6509b181 2022-12-30 thomas if (tp->tp_putc(tp, *str) == -1)
43 6509b181 2022-12-30 thomas return (-1);
44 6509b181 2022-12-30 thomas }
45 6509b181 2022-12-30 thomas }
46 6509b181 2022-12-30 thomas
47 6509b181 2022-12-30 thomas return (0);
48 6509b181 2022-12-30 thomas }
49 6509b181 2022-12-30 thomas
50 6509b181 2022-12-30 thomas int
51 6509b181 2022-12-30 thomas tp_htmlescape(struct template *tp, const char *str)
52 6509b181 2022-12-30 thomas {
53 6509b181 2022-12-30 thomas int r;
54 6509b181 2022-12-30 thomas
55 6509b181 2022-12-30 thomas if (str == NULL)
56 6509b181 2022-12-30 thomas return (0);
57 6509b181 2022-12-30 thomas
58 6509b181 2022-12-30 thomas for (; *str; ++str) {
59 6509b181 2022-12-30 thomas switch (*str) {
60 6509b181 2022-12-30 thomas case '<':
61 6509b181 2022-12-30 thomas r = tp->tp_puts(tp, "&lt;");
62 6509b181 2022-12-30 thomas break;
63 6509b181 2022-12-30 thomas case '>':
64 6509b181 2022-12-30 thomas r = tp->tp_puts(tp, "&gt;");
65 6509b181 2022-12-30 thomas break;
66 6509b181 2022-12-30 thomas case '&':
67 6509b181 2022-12-30 thomas r = tp->tp_puts(tp, "&amp;");
68 6509b181 2022-12-30 thomas break;
69 6509b181 2022-12-30 thomas case '"':
70 6509b181 2022-12-30 thomas r = tp->tp_puts(tp, "&quot;");
71 6509b181 2022-12-30 thomas break;
72 6509b181 2022-12-30 thomas case '\'':
73 6509b181 2022-12-30 thomas r = tp->tp_puts(tp, "&apos;");
74 6509b181 2022-12-30 thomas break;
75 6509b181 2022-12-30 thomas default:
76 6509b181 2022-12-30 thomas r = tp->tp_putc(tp, *str);
77 6509b181 2022-12-30 thomas break;
78 6509b181 2022-12-30 thomas }
79 6509b181 2022-12-30 thomas
80 6509b181 2022-12-30 thomas if (r == -1)
81 6509b181 2022-12-30 thomas return (-1);
82 6509b181 2022-12-30 thomas }
83 6509b181 2022-12-30 thomas
84 6509b181 2022-12-30 thomas return (0);
85 6509b181 2022-12-30 thomas }
86 6509b181 2022-12-30 thomas
87 6509b181 2022-12-30 thomas struct template *
88 6509b181 2022-12-30 thomas template(void *arg, tmpl_puts putsfn, tmpl_putc putcfn)
89 6509b181 2022-12-30 thomas {
90 6509b181 2022-12-30 thomas struct template *tp;
91 6509b181 2022-12-30 thomas
92 6509b181 2022-12-30 thomas if ((tp = calloc(1, sizeof(*tp))) == NULL)
93 6509b181 2022-12-30 thomas return (NULL);
94 6509b181 2022-12-30 thomas
95 6509b181 2022-12-30 thomas tp->tp_arg = arg;
96 6509b181 2022-12-30 thomas tp->tp_escape = tp_htmlescape;
97 6509b181 2022-12-30 thomas tp->tp_puts = putsfn;
98 6509b181 2022-12-30 thomas tp->tp_putc = putcfn;
99 6509b181 2022-12-30 thomas
100 6509b181 2022-12-30 thomas return (tp);
101 6509b181 2022-12-30 thomas }
102 6509b181 2022-12-30 thomas
103 6509b181 2022-12-30 thomas void
104 6509b181 2022-12-30 thomas template_free(struct template *tp)
105 6509b181 2022-12-30 thomas {
106 6509b181 2022-12-30 thomas free(tp->tp_tmp);
107 6509b181 2022-12-30 thomas free(tp);
108 6509b181 2022-12-30 thomas }