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 2c02675e 2022-12-14 op int
101 2c02675e 2022-12-14 op tp_htmlescape(struct template *tp, const char *str)
102 2c02675e 2022-12-14 op {
103 2c02675e 2022-12-14 op int r;
104 2c02675e 2022-12-14 op
105 2c02675e 2022-12-14 op if (str == NULL)
106 2c02675e 2022-12-14 op return (0);
107 2c02675e 2022-12-14 op
108 2c02675e 2022-12-14 op for (; *str; ++str) {
109 2c02675e 2022-12-14 op switch (*str) {
110 2c02675e 2022-12-14 op case '<':
111 62eab86e 2023-09-13 op r = tp_write(tp, "&lt;", 4);
112 2c02675e 2022-12-14 op break;
113 2c02675e 2022-12-14 op case '>':
114 62eab86e 2023-09-13 op r = tp_write(tp, "&gt;", 4);
115 2c02675e 2022-12-14 op break;
116 2c02675e 2022-12-14 op case '&':
117 62eab86e 2023-09-13 op r = tp_write(tp, "&amp;", 5);
118 2c02675e 2022-12-14 op break;
119 2c02675e 2022-12-14 op case '"':
120 62eab86e 2023-09-13 op r = tp_write(tp, "&quot;", 6);
121 2c02675e 2022-12-14 op break;
122 2c02675e 2022-12-14 op case '\'':
123 62eab86e 2023-09-13 op r = tp_write(tp, "&apos;", 6);
124 2c02675e 2022-12-14 op break;
125 2c02675e 2022-12-14 op default:
126 62eab86e 2023-09-13 op r = tp_write(tp, str, 1);
127 2c02675e 2022-12-14 op break;
128 2c02675e 2022-12-14 op }
129 2c02675e 2022-12-14 op
130 2c02675e 2022-12-14 op if (r == -1)
131 2c02675e 2022-12-14 op return (-1);
132 2c02675e 2022-12-14 op }
133 2c02675e 2022-12-14 op
134 2c02675e 2022-12-14 op return (0);
135 2c02675e 2022-12-14 op }
136 2c02675e 2022-12-14 op
137 2c02675e 2022-12-14 op struct template *
138 62eab86e 2023-09-13 op template(void *arg, tmpl_write writefn, char *buf, size_t siz)
139 2c02675e 2022-12-14 op {
140 2c02675e 2022-12-14 op struct template *tp;
141 2c02675e 2022-12-14 op
142 2c02675e 2022-12-14 op if ((tp = calloc(1, sizeof(*tp))) == NULL)
143 2c02675e 2022-12-14 op return (NULL);
144 2c02675e 2022-12-14 op
145 2c02675e 2022-12-14 op tp->tp_arg = arg;
146 62eab86e 2023-09-13 op tp->tp_write = writefn;
147 62eab86e 2023-09-13 op tp->tp_buf = buf;
148 62eab86e 2023-09-13 op tp->tp_cap = siz;
149 2c02675e 2022-12-14 op
150 2c02675e 2022-12-14 op return (tp);
151 2c02675e 2022-12-14 op }
152 2c02675e 2022-12-14 op
153 62eab86e 2023-09-13 op int
154 62eab86e 2023-09-13 op template_flush(struct template *tp)
155 62eab86e 2023-09-13 op {
156 62eab86e 2023-09-13 op if (tp->tp_len == 0)
157 62eab86e 2023-09-13 op return (0);
158 62eab86e 2023-09-13 op
159 62eab86e 2023-09-13 op if (tp->tp_write(tp->tp_arg, tp->tp_buf, tp->tp_len) == -1)
160 62eab86e 2023-09-13 op return (-1);
161 62eab86e 2023-09-13 op tp->tp_len = 0;
162 62eab86e 2023-09-13 op return (0);
163 62eab86e 2023-09-13 op }
164 62eab86e 2023-09-13 op
165 2c02675e 2022-12-14 op void
166 2c02675e 2022-12-14 op template_free(struct template *tp)
167 2c02675e 2022-12-14 op {
168 2c02675e 2022-12-14 op free(tp->tp_tmp);
169 2c02675e 2022-12-14 op free(tp);
170 2c02675e 2022-12-14 op }