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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "xml.h"
void
xmlparser_init(XMLParser *x) {
memset(x, 0, sizeof(XMLParser));
x->fp = stdin;
}
static int /* like getc(), but do some smart buffering */
xmlparser_getnext(XMLParser *x) {
if(x->readoffset >= x->readlastbytes) {
if(feof(x->fp))
return EOF;
x->readoffset = 0;
if(!(x->readlastbytes = fread(x->readbuf, 1, sizeof(x->readbuf), x->fp)))
return EOF; /* 0 bytes read, assume EOF */
}
return (int)x->readbuf[x->readoffset++];
}
static int
xmlparser_parsedata(XMLParser *x, int c) {
size_t datalen = 0;
if(x->xmldatastart)
x->xmldatastart(x);
do {
if(x->xmldataentity && c == '&') { /* TODO: test this, entity handler */
x->data[datalen] = '\0';
x->xmldata(x, x->data, datalen);
x->data[0] = c;
datalen = 1;
while((c = xmlparser_getnext(x)) != EOF) {
if(c == '<') { /* TODO: simplify? duplicate code. */
goto parsedataend;
/* x->data[datalen] = '\0';
if(x->xmldata)
x->xmldata(x, x->data, datalen);
if(x->xmldataend)
x->xmldataend(x);
return c;*/
}
if(datalen < sizeof(x->data) - 1)
x->data[datalen++] = c;
if(isspace(c))
break;
else if(c == ';') {
x->data[datalen] = '\0';
x->xmldataentity(x, x->data, datalen);
datalen = 0;
break;
}
}
} else if(c == '<') { /* TODO: simplify? duplicate code. */
parsedataend:
x->data[datalen] = '\0';
if(x->xmldata)
x->xmldata(x, x->data, datalen);
if(x->xmldataend)
x->xmldataend(x);
return c;
} else {
if(datalen < sizeof(x->data) - 1) {
x->data[datalen++] = c;
} else {
x->data[datalen] = '\0'; /* TODO: overflow */
if(x->xmldata)
x->xmldata(x, x->data, datalen);
x->data[0] = c;
datalen = 1;
}
}
} while((c = xmlparser_getnext(x)) != EOF);
return EOF;
}
static void
xmlparser_parseattrvalue(XMLParser *x, const char *name, size_t namelen, int end) {
size_t valuelen = 0;
int c;
if(x->xmlattrstart)
x->xmlattrstart(x, x->tag, x->taglen, name, namelen);
for(valuelen = 0; (c = xmlparser_getnext(x)) != EOF;) {
if(x->xmlattrentity && c == '&') { /* entities */
x->data[valuelen] = '\0';
/* call data function with data before entity if there is data */
if(x->xmlattr && valuelen)
x->xmlattr(x, x->tag, x->taglen, name, namelen, x->data, valuelen);
x->data[0] = c;
valuelen = 1;
while((c = xmlparser_getnext(x)) != EOF) {
if(c == end) { /* TODO: simplify? duplicate code. */
goto parseattrvalueend;
/* x->data[valuelen] = '\0';
if(x->xmlattr)
x->xmlattr(x, x->tag, x->taglen, name, namelen, x->data, valuelen);
if(x->xmlattrend)
x->xmlattrend(x, x->tag, x->taglen, name, namelen);
return;*/
}
if(valuelen < sizeof(x->data) - 1)
x->data[valuelen++] = c;
else { /* TODO: entity too long? this should be very strange. */
x->data[valuelen] = '\0';
if(x->xmlattr)
x->xmlattr(x, x->tag, x->taglen, name, namelen, x->data, valuelen);
valuelen = 0; /* TODO: incorrect ? ';' is read in c below? */
/* x->data[0] = '\0'; */
break;
}
if(c == ';') {
x->data[valuelen] = '\0';
x->xmlattrentity(x, x->tag, x->taglen, name, namelen, x->data, valuelen);
valuelen = 0; /* TODO: incorrect ? ';' is read in c below? */
break;
}
}
} else if(c == end) { /* TODO: simplify? duplicate code. */
parseattrvalueend:
x->data[valuelen] = '\0';
if(x->xmlattr)
x->xmlattr(x, x->tag, x->taglen, name, namelen, x->data, valuelen);
if(x->xmlattrend)
x->xmlattrend(x, x->tag, x->taglen, name, namelen);
return;
} else {
if(valuelen < sizeof(x->data) - 1) { /* TODO: overflow */
x->data[valuelen++] = c;
} else {
x->data[valuelen] = '\0';
if(x->xmlattr)
x->xmlattr(x, x->tag, x->taglen, name, namelen, x->data, valuelen);
x->data[0] = c;
valuelen = 1;
}
}
}
}
static int
xmlparser_parseattrs(XMLParser *x) {
char name[1024]; /* TODO: dont overflow this bitch, also make it bigger perhaps? */
size_t namelen = 0, valuelen = 0;
int c, shorttag = 0, endname = 0;
while((c = xmlparser_getnext(x)) != EOF) {
if(isspace(c) && namelen) /* Do nothing */
endname = 1;
if(isspace(c) || c == '?') { /* Do nothing */
} else if(c == '=') {
name[namelen] = '\0';
} else if(namelen && ((endname && isalpha(c)) || (c == '>' || c == '/'))) {
/* attribute without value */
name[namelen] = '\0';
if(x->xmlattrstart)
x->xmlattrstart(x, x->tag, x->taglen, name, namelen);
if(x->xmlattr)
x->xmlattr(x, x->tag, x->taglen, name, namelen, "", 0);
if(x->xmlattrend)
x->xmlattrend(x, x->tag, x->taglen, name, namelen);
endname = 0;
name[0] = c;
namelen = 1;
} else if(namelen && (c == '\'' || c == '"')) {
/* attribute with value */
xmlparser_parseattrvalue(x, name, namelen, c);
namelen = 0;
valuelen = 0;
endname = 0;
} else if(namelen < sizeof(name) - 1)
name[namelen++] = c;
if(c == '>') {
break;
} else if(c == '/') { /* TODO: cleanup, ugly. */
shorttag = 1;
namelen = 0;
name[0] = '\0';
}
}
return shorttag; /* TODO: cleanup, ugly. */
}
static void
xmlparser_parsecomment(XMLParser *x) {
size_t datalen = 0, i = 0;
int c;
if(x->xmlcommentstart)
x->xmlcommentstart(x);
while((c = xmlparser_getnext(x)) != EOF) {
if(c == '-' && i < 2)
i++;
else if(c == '>') {
if(i == 2) { /* (!memcmp(cd, "-->", strlen("-->"))) { */
if(datalen >= 2)
datalen -= 2;
else
datalen = 0;
x->data[datalen] = '\0'; /* TODO: possible buffer underflow < 0 */
if(x->xmlcomment)
x->xmlcomment(x, x->data, datalen); /* TODO: possible buffer underflow < 0 */
if(x->xmlcommentend)
x->xmlcommentend(x);
break;
}
i = 0;
}
if(datalen < sizeof(x->data) - 1) { /* || (c == '-' && d >= sizeof(x->data) - 4)) { */ /* TODO: what if the end has --, and its cut on the boundary, test this. */
x->data[datalen++] = c;
} else {
x->data[datalen] = '\0'; /* TODO: overflow */
if(x->xmlcomment)
x->xmlcomment(x, x->data, datalen);
x->data[0] = c;
datalen = 1;
}
}
}
static void
xmlparser_parsecdata(XMLParser *x) {
size_t datalen = 0, i = 0;
int c;
if(x->xmlcdatastart)
x->xmlcdatastart(x);
while((c = xmlparser_getnext(x)) != EOF) {
if(c == ']' && i < 2) {
i++;
} else if(c == '>') {
if(i == 2) { /* (!memcmp(cd, "]]", strlen("]]"))) { */
if(datalen >= 2)
datalen -= 2;
else
datalen = 0;
x->data[datalen] = '\0'; /* TODO: check d >= 3 */ /* TODO: buffer underflow */
if(x->xmlcdata && datalen)
x->xmlcdata(x, x->data, datalen); /* TODO: buffer underflow */
if(x->xmlcdataend)
x->xmlcdataend(x);
break;
}
i = 0;
}
if(datalen < sizeof(x->data) - 1) { /* TODO: what if the end has ]>, and its cut on the boundary */
x->data[datalen++] = c;
} else {
x->data[datalen] = '\0';
if(x->xmlcdata)
x->xmlcdata(x, x->data, datalen);
x->data[0] = c;
datalen = 1;
}
}
}
static void
xmlparser_parsetag(XMLParser *x) {
size_t datalen, taglen;
int c, s, isshorttag = 0;
x->tag[0] = '\0';
x->taglen = 0;
while((c = xmlparser_getnext(x)) != EOF && isspace(c));
if(c == '!') {
for(datalen = 0; (c = xmlparser_getnext(x)) != EOF;) {
if(datalen <= strlen("[CDATA[")) /* if(d < sizeof(x->data)) */
x->data[datalen++] = c; /* TODO: prevent overflow */
if(c == '>')
break;
else if(c == '-' && !memcmp(x->data, "--", strlen("--"))) { /* comment */ /* TODO: optimize this bitch */
xmlparser_parsecomment(x);
break;
} else if(c == '[' && !memcmp(x->data, "[CDATA[", strlen("[CDATA["))) { /* cdata */ /* TODO: optimize this bitch */
xmlparser_parsecdata(x);
break;
}
}
} else if(c == '?') {
while((c = xmlparser_getnext(x)) != EOF) {
if(c == '"' || c == '\'') {
s = c;
while((c = xmlparser_getnext(x)) != EOF) {
if(c == s)
break;
}
} else if(c == '>')
break;
}
} else if(c != EOF && c != '>') {
x->tag[0] = c;
taglen = 1;
while((c = xmlparser_getnext(x)) != EOF) {
if(c == '/')
isshorttag = 1; /* short tag */
else if(isspace(c) || c == '>') {
x->tag[taglen] = '\0';
if(x->tag[0] == '/') { /* end tag */
x->taglen = --taglen; /* len -1 because of / */
if(x->xmltagend)
x->xmltagend(x, &(x->tag)[1], x->taglen, 0);
} else {
x->taglen = taglen;
if(x->xmltagstart)
x->xmltagstart(x, x->tag, x->taglen); /* start tag */
if(isspace(c) && xmlparser_parseattrs(x))
isshorttag = 1;
if(x->xmltagstartparsed)
x->xmltagstartparsed(x, x->tag, x->taglen, isshorttag);
}
if(isshorttag && x->xmltagend)
x->xmltagend(x, x->tag, x->taglen, 1);
break;
} else if(taglen < sizeof(x->tag) - 1)
x->tag[taglen++] = c; /* TODO: prevent overflow */
}
}
}
void
xmlparser_parse(XMLParser *x) {
int c;
while((c = xmlparser_getnext(x)) != EOF) {
/*if(isspace(c));*/ /* Do nothing */
/*else*/ if(c == '<') /* tag */
xmlparser_parsetag(x);
else {
xmlparser_parsedata(x, c);
xmlparser_parsetag(x);
}
}
return;
}
|