-
Notifications
You must be signed in to change notification settings - Fork 40
/
kcgixml.c
287 lines (244 loc) · 5.98 KB
/
kcgixml.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* $Id$ */
/*
* Copyright (c) 2015, 2017, 2020 Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <assert.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "kcgi.h"
#include "kcgixml.h"
enum kcgi_err
kxml_open(struct kxmlreq *r, struct kreq *req,
const char *const *elems, size_t elemsz)
{
memset(r, 0, sizeof(struct kxmlreq));
if (NULL == (r->arg = kcgi_writer_get(req, 0)))
return(KCGI_ENOMEM);
r->elems = elems;
r->elemsz = elemsz;
return(KCGI_OK);
}
enum kcgi_err
kxml_prologue(struct kxmlreq *r)
{
return kcgi_writer_puts(r->arg,
"<?xml version=\"1.0\" "
"encoding=\"utf-8\" ?>");
}
enum kcgi_err
kxml_close(struct kxmlreq *r)
{
enum kcgi_err er;
er = kxml_popall(r);
kcgi_writer_free(r->arg);
r->arg = NULL;
return er;
}
enum kcgi_err
kxml_push(struct kxmlreq *r, size_t elem)
{
enum kcgi_err er;
if (r->stackpos >= KXML_STACK_MAX) {
kutil_warnx(NULL, NULL,
"maximum xml stack size exceeded");
return KCGI_ENOMEM;
} else if (elem >= r->elemsz)
return KCGI_WRITER;
if ((er = kcgi_writer_putc(r->arg, '<')) != KCGI_OK)
return er;
if ((er = kcgi_writer_puts(r->arg, r->elems[elem])) != KCGI_OK)
return er;
if ((er = kcgi_writer_putc(r->arg, '>')) != KCGI_OK)
return er;
r->stack[r->stackpos++] = elem;
return KCGI_OK;
}
enum kcgi_err
kxml_pushnull(struct kxmlreq *r, size_t elem)
{
enum kcgi_err er;
if (r->stackpos >= KXML_STACK_MAX) {
kutil_warnx(NULL, NULL,
"maximum xml stack size exceeded");
return KCGI_ENOMEM;
} else if (elem >= r->elemsz)
return KCGI_WRITER;
if ((er = kcgi_writer_putc(r->arg, '<')) != KCGI_OK)
return er;
if ((er = kcgi_writer_puts(r->arg, r->elems[elem])) != KCGI_OK)
return er;
return kcgi_writer_puts(r->arg, " />");
}
enum kcgi_err
kxml_putc(struct kxmlreq *r, char c)
{
enum kcgi_err er;
switch (c) {
case ('<'):
er = kcgi_writer_puts(r->arg, "<");
break;
case ('>'):
er = kcgi_writer_puts(r->arg, ">");
break;
case ('"'):
er = kcgi_writer_puts(r->arg, """);
break;
case ('&'):
er = kcgi_writer_puts(r->arg, "&");
break;
default:
er = kcgi_writer_putc(r->arg, c);
break;
}
return(er);
}
enum kcgi_err
kxml_write(const char *p, size_t sz, void *arg)
{
struct kxmlreq *r = arg;
size_t i;
enum kcgi_err er;
if (p == NULL || sz == 0)
return KCGI_OK;
for (i = 0; i < sz; i++)
if ((er = kxml_putc(r, p[i])) != KCGI_OK)
return er;
return KCGI_OK;
}
enum kcgi_err
kxml_puts(struct kxmlreq *r, const char *p)
{
if (p == NULL)
return KCGI_OK;
return kxml_write(p, strlen(p), r);
}
enum kcgi_err
kxml_pushattrs(struct kxmlreq *r, size_t elem, ...)
{
va_list ap;
const char *key, *val;
enum kcgi_err er = KCGI_OK;
if (r->stackpos >= KXML_STACK_MAX) {
kutil_warnx(NULL, NULL,
"maximum xml stack size exceeded");
return KCGI_ENOMEM;
} else if (elem >= r->elemsz)
return KCGI_WRITER;
if ((er = kcgi_writer_putc(r->arg, '<')) != KCGI_OK)
return er;
if ((er = kcgi_writer_puts(r->arg, r->elems[elem])) != KCGI_OK)
return er;
va_start(ap, elem);
for (;;) {
if ((key = va_arg(ap, char *)) == NULL)
break;
val = va_arg(ap, char *);
if ((er = kcgi_writer_putc(r->arg, ' ')) != KCGI_OK)
goto out;
if ((er = kcgi_writer_puts(r->arg, key)) != KCGI_OK)
goto out;
if ((er = kcgi_writer_puts(r->arg, "=\"")) != KCGI_OK)
goto out;
if ((er = kxml_puts(r, val)) != KCGI_OK)
goto out;
if ((er = kcgi_writer_putc(r->arg, '"')) != KCGI_OK)
goto out;
}
va_end(ap);
r->stack[r->stackpos++] = elem;
return kcgi_writer_putc(r->arg, '>');
out:
va_end(ap);
return er;
}
enum kcgi_err
kxml_pushnullattrs(struct kxmlreq *r, size_t elem, ...)
{
va_list ap;
const char *key, *val;
enum kcgi_err er;
if (r->stackpos >= KXML_STACK_MAX) {
kutil_warnx(NULL, NULL,
"maximum xml stack size exceeded");
return KCGI_ENOMEM;
} else if (elem >= r->elemsz)
return KCGI_WRITER;
if ((er = kcgi_writer_putc(r->arg, '<')) != KCGI_OK)
return er;
if ((er = kcgi_writer_puts(r->arg, r->elems[elem])) != KCGI_OK)
return er;
va_start(ap, elem);
for (;;) {
if ((key = va_arg(ap, char *)) == NULL)
break;
val = va_arg(ap, char *);
if ((er = kcgi_writer_putc(r->arg, ' ')) != KCGI_OK)
goto out;
if ((er = kcgi_writer_puts(r->arg, key)) != KCGI_OK)
goto out;
if ((er = kcgi_writer_puts(r->arg, "=\"")) != KCGI_OK)
goto out;
if ((er = kxml_puts(r, val)) != KCGI_OK)
goto out;
if ((er = kcgi_writer_putc(r->arg, '"')) != KCGI_OK)
goto out;
}
va_end(ap);
return kcgi_writer_puts(r->arg, " />");
out:
va_end(ap);
return er;
}
static int
kxml_pop_inner(struct kxmlreq *r, enum kcgi_err *er)
{
if (r->stackpos == 0)
return 0;
*er = kcgi_writer_puts(r->arg, "</");
if (*er != KCGI_OK)
return (-1);
*er = kcgi_writer_puts(r->arg,
r->elems[r->stack[--r->stackpos]]);
if (*er != KCGI_OK)
return (-1);
*er = kcgi_writer_putc(r->arg, '>');
if (*er != KCGI_OK)
return (-1);
return 1;
}
enum kcgi_err
kxml_popall(struct kxmlreq *r)
{
enum kcgi_err er = KCGI_OK;
while (kxml_pop_inner(r, &er) > 0)
/* Spin. */ ;
return er;
}
enum kcgi_err
kxml_pop(struct kxmlreq *r)
{
enum kcgi_err er;
int c;
if ((c = kxml_pop_inner(r, &er)) < 0)
return er;
else if (c == 0)
return KCGI_WRITER;
return KCGI_OK;
}