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 62eab86e 2023-09-13 op #include <stdarg.h>
19 2c02675e 2022-12-14 op #include <stdio.h>
20 2c02675e 2022-12-14 op #include <stdlib.h>
21 62eab86e 2023-09-13 op #include <string.h>
22 2c02675e 2022-12-14 op
23 2c02675e 2022-12-14 op #include "tmpl.h"
24 2c02675e 2022-12-14 op
25 2c02675e 2022-12-14 op int
26 62eab86e 2023-09-13 op tp_write(struct template *tp, const char *str, size_t len)
27 62eab86e 2023-09-13 op {
28 62eab86e 2023-09-13 op size_t avail;
29 62eab86e 2023-09-13 op
30 62eab86e 2023-09-13 op while (len > 0) {
31 62eab86e 2023-09-13 op avail = tp->tp_cap - tp->tp_len;
32 62eab86e 2023-09-13 op if (avail == 0) {
33 62eab86e 2023-09-13 op if (template_flush(tp) == -1)
34 62eab86e 2023-09-13 op return (-1);
35 62eab86e 2023-09-13 op avail = tp->tp_cap;
36 62eab86e 2023-09-13 op }
37 62eab86e 2023-09-13 op
38 62eab86e 2023-09-13 op if (len < avail)
39 62eab86e 2023-09-13 op avail = len;
40 62eab86e 2023-09-13 op
41 62eab86e 2023-09-13 op memcpy(tp->tp_buf + tp->tp_len, str, avail);
42 62eab86e 2023-09-13 op tp->tp_len += avail;
43 62eab86e 2023-09-13 op str += avail;
44 62eab86e 2023-09-13 op len -= avail;
45 62eab86e 2023-09-13 op }
46 62eab86e 2023-09-13 op
47 62eab86e 2023-09-13 op return (0);
48 62eab86e 2023-09-13 op }
49 62eab86e 2023-09-13 op
50 62eab86e 2023-09-13 op int
51 62eab86e 2023-09-13 op tp_writes(struct template *tp, const char *str)
52 62eab86e 2023-09-13 op {
53 62eab86e 2023-09-13 op return (tp_write(tp, str, strlen(str)));
54 62eab86e 2023-09-13 op }
55 62eab86e 2023-09-13 op
56 62eab86e 2023-09-13 op int
57 62eab86e 2023-09-13 op tp_writef(struct template *tp, const char *fmt, ...)
58 62eab86e 2023-09-13 op {
59 62eab86e 2023-09-13 op va_list ap;
60 62eab86e 2023-09-13 op char *str;
61 62eab86e 2023-09-13 op int r;
62 62eab86e 2023-09-13 op
63 62eab86e 2023-09-13 op va_start(ap, fmt);
64 62eab86e 2023-09-13 op r = vasprintf(&str, fmt, ap);
65 62eab86e 2023-09-13 op va_end(ap);
66 62eab86e 2023-09-13 op if (r == -1)
67 62eab86e 2023-09-13 op return (-1);
68 62eab86e 2023-09-13 op r = tp_write(tp, str, r);
69 62eab86e 2023-09-13 op free(str);
70 62eab86e 2023-09-13 op return (r);
71 62eab86e 2023-09-13 op }
72 62eab86e 2023-09-13 op
73 62eab86e 2023-09-13 op int
74 2c02675e 2022-12-14 op tp_urlescape(struct template *tp, const char *str)
75 2c02675e 2022-12-14 op {
76 2c02675e 2022-12-14 op int r;
77 2c02675e 2022-12-14 op char tmp[4];
78 2c02675e 2022-12-14 op
79 2c02675e 2022-12-14 op if (str == NULL)
80 2c02675e 2022-12-14 op return (0);
81 2c02675e 2022-12-14 op
82 2c02675e 2022-12-14 op for (; *str; ++str) {
83 2c02675e 2022-12-14 op if (iscntrl((unsigned char)*str) ||
84 2c02675e 2022-12-14 op isspace((unsigned char)*str) ||
85 2c02675e 2022-12-14 op *str == '\'' || *str == '"' || *str == '\\') {
86 2c02675e 2022-12-14 op r = snprintf(tmp, sizeof(tmp), "%%%2X", *str);
87 2c02675e 2022-12-14 op if (r < 0 || (size_t)r >= sizeof(tmp))
88 2c02675e 2022-12-14 op return (0);
89 62eab86e 2023-09-13 op if (tp_write(tp, tmp, r) == -1)
90 2c02675e 2022-12-14 op return (-1);
91 2c02675e 2022-12-14 op } else {
92 62eab86e 2023-09-13 op if (tp_write(tp, str, 1) == -1)
93 2c02675e 2022-12-14 op return (-1);
94 2c02675e 2022-12-14 op }
95 2c02675e 2022-12-14 op }
96 2c02675e 2022-12-14 op
97 2c02675e 2022-12-14 op return (0);
98 2c02675e 2022-12-14 op }
99 2c02675e 2022-12-14 op
100 ac67fee4 2023-12-08 op static inline int
101 ac67fee4 2023-12-08 op htmlescape(struct template *tp, char c)
102 ac67fee4 2023-12-08 op {
103 ac67fee4 2023-12-08 op switch (c) {
104 ac67fee4 2023-12-08 op case '<':
105 ac67fee4 2023-12-08 op return tp_write(tp, "&lt;", 4);
106 ac67fee4 2023-12-08 op case '>':
107 ac67fee4 2023-12-08 op return tp_write(tp, "&gt;", 4);
108 ac67fee4 2023-12-08 op case '&':
109 ac67fee4 2023-12-08 op return tp_write(tp, "&amp;", 5);
110 ac67fee4 2023-12-08 op case '"':
111 ac67fee4 2023-12-08 op return tp_write(tp, "&quot;", 6);
112 ac67fee4 2023-12-08 op case '\'':
113 ac67fee4 2023-12-08 op return tp_write(tp, "&apos;", 6);
114 ac67fee4 2023-12-08 op default:
115 ac67fee4 2023-12-08 op return tp_write(tp, &c, 1);
116 ac67fee4 2023-12-08 op }
117 ac67fee4 2023-12-08 op }
118 ac67fee4 2023-12-08 op
119 2c02675e 2022-12-14 op int
120 2c02675e 2022-12-14 op tp_htmlescape(struct template *tp, const char *str)
121 2c02675e 2022-12-14 op {
122 2c02675e 2022-12-14 op if (str == NULL)
123 2c02675e 2022-12-14 op return (0);
124 2c02675e 2022-12-14 op
125 2c02675e 2022-12-14 op for (; *str; ++str) {
126 ac67fee4 2023-12-08 op if (htmlescape(tp, *str) == -1)
127 ac67fee4 2023-12-08 op return (-1);
128 ac67fee4 2023-12-08 op }
129 2c02675e 2022-12-14 op
130 ac67fee4 2023-12-08 op return (0);
131 ac67fee4 2023-12-08 op }
132 ac67fee4 2023-12-08 op
133 ac67fee4 2023-12-08 op int
134 ac67fee4 2023-12-08 op tp_write_htmlescape(struct template *tp, const char *str, size_t len)
135 ac67fee4 2023-12-08 op {
136 ac67fee4 2023-12-08 op size_t i;
137 ac67fee4 2023-12-08 op
138 ac67fee4 2023-12-08 op for (i = 0; i < len; ++i) {
139 ac67fee4 2023-12-08 op if (htmlescape(tp, str[i]) == -1)
140 2c02675e 2022-12-14 op return (-1);
141 2c02675e 2022-12-14 op }
142 2c02675e 2022-12-14 op
143 2c02675e 2022-12-14 op return (0);
144 2c02675e 2022-12-14 op }
145 2c02675e 2022-12-14 op
146 2c02675e 2022-12-14 op struct template *
147 62eab86e 2023-09-13 op template(void *arg, tmpl_write writefn, char *buf, size_t siz)
148 2c02675e 2022-12-14 op {
149 2c02675e 2022-12-14 op struct template *tp;
150 2c02675e 2022-12-14 op
151 2c02675e 2022-12-14 op if ((tp = calloc(1, sizeof(*tp))) == NULL)
152 2c02675e 2022-12-14 op return (NULL);
153 2c02675e 2022-12-14 op
154 2c02675e 2022-12-14 op tp->tp_arg = arg;
155 62eab86e 2023-09-13 op tp->tp_write = writefn;
156 62eab86e 2023-09-13 op tp->tp_buf = buf;
157 62eab86e 2023-09-13 op tp->tp_cap = siz;
158 2c02675e 2022-12-14 op
159 2c02675e 2022-12-14 op return (tp);
160 2c02675e 2022-12-14 op }
161 2c02675e 2022-12-14 op
162 62eab86e 2023-09-13 op int
163 62eab86e 2023-09-13 op template_flush(struct template *tp)
164 62eab86e 2023-09-13 op {
165 62eab86e 2023-09-13 op if (tp->tp_len == 0)
166 62eab86e 2023-09-13 op return (0);
167 62eab86e 2023-09-13 op
168 62eab86e 2023-09-13 op if (tp->tp_write(tp->tp_arg, tp->tp_buf, tp->tp_len) == -1)
169 62eab86e 2023-09-13 op return (-1);
170 62eab86e 2023-09-13 op tp->tp_len = 0;
171 62eab86e 2023-09-13 op return (0);
172 62eab86e 2023-09-13 op }
173 62eab86e 2023-09-13 op
174 2c02675e 2022-12-14 op void
175 2c02675e 2022-12-14 op template_free(struct template *tp)
176 2c02675e 2022-12-14 op {
177 2c02675e 2022-12-14 op free(tp->tp_tmp);
178 2c02675e 2022-12-14 op free(tp);
179 2c02675e 2022-12-14 op }