Blame


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