Blame


1 bff58270 2022-09-19 op /*
2 52afe358 2022-12-17 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 bff58270 2022-09-19 op *
4 bff58270 2022-09-19 op * Permission to use, copy, modify, and distribute this software for any
5 bff58270 2022-09-19 op * purpose with or without fee is hereby granted, provided that the above
6 bff58270 2022-09-19 op * copyright notice and this permission notice appear in all copies.
7 bff58270 2022-09-19 op *
8 bff58270 2022-09-19 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 bff58270 2022-09-19 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 bff58270 2022-09-19 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 bff58270 2022-09-19 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 bff58270 2022-09-19 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 bff58270 2022-09-19 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 bff58270 2022-09-19 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 bff58270 2022-09-19 op */
16 bff58270 2022-09-19 op
17 bff58270 2022-09-19 op #include <ctype.h>
18 18906bcc 2023-09-13 op #include <stdarg.h>
19 bff58270 2022-09-19 op #include <stdio.h>
20 bff58270 2022-09-19 op #include <stdlib.h>
21 18906bcc 2023-09-13 op #include <string.h>
22 bff58270 2022-09-19 op
23 bff58270 2022-09-19 op #include "tmpl.h"
24 bff58270 2022-09-19 op
25 bff58270 2022-09-19 op int
26 18906bcc 2023-09-13 op tp_write(struct template *tp, const char *str, size_t len)
27 18906bcc 2023-09-13 op {
28 18906bcc 2023-09-13 op size_t avail;
29 18906bcc 2023-09-13 op
30 18906bcc 2023-09-13 op while (len > 0) {
31 18906bcc 2023-09-13 op avail = tp->tp_cap - tp->tp_len;
32 18906bcc 2023-09-13 op if (avail == 0) {
33 18906bcc 2023-09-13 op if (template_flush(tp) == -1)
34 18906bcc 2023-09-13 op return (-1);
35 18906bcc 2023-09-13 op avail = tp->tp_cap;
36 18906bcc 2023-09-13 op }
37 18906bcc 2023-09-13 op
38 18906bcc 2023-09-13 op if (len < avail)
39 18906bcc 2023-09-13 op avail = len;
40 18906bcc 2023-09-13 op
41 18906bcc 2023-09-13 op memcpy(tp->tp_buf + tp->tp_len, str, avail);
42 18906bcc 2023-09-13 op tp->tp_len += avail;
43 18906bcc 2023-09-13 op str += avail;
44 18906bcc 2023-09-13 op len -= avail;
45 18906bcc 2023-09-13 op }
46 18906bcc 2023-09-13 op
47 18906bcc 2023-09-13 op return (0);
48 18906bcc 2023-09-13 op }
49 18906bcc 2023-09-13 op
50 18906bcc 2023-09-13 op int
51 18906bcc 2023-09-13 op tp_writes(struct template *tp, const char *str)
52 18906bcc 2023-09-13 op {
53 18906bcc 2023-09-13 op return (tp_write(tp, str, strlen(str)));
54 18906bcc 2023-09-13 op }
55 18906bcc 2023-09-13 op
56 18906bcc 2023-09-13 op int
57 18906bcc 2023-09-13 op tp_writef(struct template *tp, const char *fmt, ...)
58 18906bcc 2023-09-13 op {
59 18906bcc 2023-09-13 op va_list ap;
60 18906bcc 2023-09-13 op char *str;
61 18906bcc 2023-09-13 op int r;
62 18906bcc 2023-09-13 op
63 18906bcc 2023-09-13 op va_start(ap, fmt);
64 18906bcc 2023-09-13 op r = vasprintf(&str, fmt, ap);
65 18906bcc 2023-09-13 op va_end(ap);
66 18906bcc 2023-09-13 op if (r == -1)
67 18906bcc 2023-09-13 op return (-1);
68 18906bcc 2023-09-13 op r = tp_write(tp, str, r);
69 18906bcc 2023-09-13 op free(str);
70 18906bcc 2023-09-13 op return (r);
71 18906bcc 2023-09-13 op }
72 18906bcc 2023-09-13 op
73 18906bcc 2023-09-13 op int
74 bff58270 2022-09-19 op tp_urlescape(struct template *tp, const char *str)
75 bff58270 2022-09-19 op {
76 bff58270 2022-09-19 op int r;
77 bff58270 2022-09-19 op char tmp[4];
78 bff58270 2022-09-19 op
79 20c8292e 2022-12-04 op if (str == NULL)
80 20c8292e 2022-12-04 op return (0);
81 20c8292e 2022-12-04 op
82 bff58270 2022-09-19 op for (; *str; ++str) {
83 bff58270 2022-09-19 op if (iscntrl((unsigned char)*str) ||
84 bff58270 2022-09-19 op isspace((unsigned char)*str) ||
85 bff58270 2022-09-19 op *str == '\'' || *str == '"' || *str == '\\') {
86 bff58270 2022-09-19 op r = snprintf(tmp, sizeof(tmp), "%%%2X", *str);
87 95448b62 2022-09-29 op if (r < 0 || (size_t)r >= sizeof(tmp))
88 95448b62 2022-09-29 op return (0);
89 18906bcc 2023-09-13 op if (tp_write(tp, tmp, r) == -1)
90 bff58270 2022-09-19 op return (-1);
91 bff58270 2022-09-19 op } else {
92 18906bcc 2023-09-13 op if (tp_write(tp, str, 1) == -1)
93 bff58270 2022-09-19 op return (-1);
94 bff58270 2022-09-19 op }
95 bff58270 2022-09-19 op }
96 bff58270 2022-09-19 op
97 bff58270 2022-09-19 op return (0);
98 bff58270 2022-09-19 op }
99 bff58270 2022-09-19 op
100 bff58270 2022-09-19 op int
101 bff58270 2022-09-19 op tp_htmlescape(struct template *tp, const char *str)
102 bff58270 2022-09-19 op {
103 bff58270 2022-09-19 op int r;
104 bff58270 2022-09-19 op
105 20c8292e 2022-12-04 op if (str == NULL)
106 20c8292e 2022-12-04 op return (0);
107 20c8292e 2022-12-04 op
108 bff58270 2022-09-19 op for (; *str; ++str) {
109 bff58270 2022-09-19 op switch (*str) {
110 bff58270 2022-09-19 op case '<':
111 18906bcc 2023-09-13 op r = tp_write(tp, "&lt;", 4);
112 bff58270 2022-09-19 op break;
113 bff58270 2022-09-19 op case '>':
114 18906bcc 2023-09-13 op r = tp_write(tp, "&gt;", 4);
115 bff58270 2022-09-19 op break;
116 bff58270 2022-09-19 op case '&':
117 18906bcc 2023-09-13 op r = tp_write(tp, "&amp;", 5);
118 bff58270 2022-09-19 op break;
119 bff58270 2022-09-19 op case '"':
120 18906bcc 2023-09-13 op r = tp_write(tp, "&quot;", 6);
121 bff58270 2022-09-19 op break;
122 bff58270 2022-09-19 op case '\'':
123 18906bcc 2023-09-13 op r = tp_write(tp, "&apos;", 6);
124 bff58270 2022-09-19 op break;
125 bff58270 2022-09-19 op default:
126 18906bcc 2023-09-13 op r = tp_write(tp, str, 1);
127 bff58270 2022-09-19 op break;
128 bff58270 2022-09-19 op }
129 bff58270 2022-09-19 op
130 95448b62 2022-09-29 op if (r == -1)
131 bff58270 2022-09-19 op return (-1);
132 bff58270 2022-09-19 op }
133 bff58270 2022-09-19 op
134 bff58270 2022-09-19 op return (0);
135 bff58270 2022-09-19 op }
136 bff58270 2022-09-19 op
137 bff58270 2022-09-19 op struct template *
138 18906bcc 2023-09-13 op template(void *arg, tmpl_write writefn, char *buf, size_t siz)
139 bff58270 2022-09-19 op {
140 bff58270 2022-09-19 op struct template *tp;
141 bff58270 2022-09-19 op
142 bff58270 2022-09-19 op if ((tp = calloc(1, sizeof(*tp))) == NULL)
143 bff58270 2022-09-19 op return (NULL);
144 bff58270 2022-09-19 op
145 bff58270 2022-09-19 op tp->tp_arg = arg;
146 18906bcc 2023-09-13 op tp->tp_write = writefn;
147 18906bcc 2023-09-13 op tp->tp_buf = buf;
148 18906bcc 2023-09-13 op tp->tp_cap = siz;
149 bff58270 2022-09-19 op
150 bff58270 2022-09-19 op return (tp);
151 bff58270 2022-09-19 op }
152 bff58270 2022-09-19 op
153 18906bcc 2023-09-13 op int
154 18906bcc 2023-09-13 op template_flush(struct template *tp)
155 18906bcc 2023-09-13 op {
156 18906bcc 2023-09-13 op if (tp->tp_len == 0)
157 18906bcc 2023-09-13 op return (0);
158 18906bcc 2023-09-13 op
159 18906bcc 2023-09-13 op if (tp->tp_write(tp->tp_arg, tp->tp_buf, tp->tp_len) == -1)
160 18906bcc 2023-09-13 op return (-1);
161 18906bcc 2023-09-13 op tp->tp_len = 0;
162 18906bcc 2023-09-13 op return (0);
163 18906bcc 2023-09-13 op }
164 18906bcc 2023-09-13 op
165 bff58270 2022-09-19 op void
166 9b929b3a 2022-09-29 op template_free(struct template *tp)
167 9b929b3a 2022-09-29 op {
168 9b929b3a 2022-09-29 op free(tp->tp_tmp);
169 9b929b3a 2022-09-29 op free(tp);
170 9b929b3a 2022-09-29 op }