forked from blackav/ejudge
/
html_parse.c
423 lines (390 loc) · 10.8 KB
/
html_parse.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* -*- c -*- */
/* $Id$ */
/* Copyright (C) 2014 Alexander Chernov <cher@ejudge.ru> */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "ejudge/html_parse.h"
#include "ejudge/list_ops.h"
#include "ejudge/misctext.h"
#include "ejudge/xalloc.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
static int
is_xml_element_name_start_char(int c)
{
return isalpha(c) || c == ':' || c == '_';
}
static int
is_xml_element_name_char(int c)
{
return isalnum(c) || c == ':' || c == '_' || c == '-' || c == '.';
}
struct HtmlAttribute *
html_attribute_create(
const unsigned char *ptr,
int len,
unsigned char *value)
{
struct HtmlAttribute *attr = NULL;
XCALLOC(attr, 1);
if (len > 0) {
attr->name = xmemdup(ptr, len);
} else {
attr->name = xstrdup(ptr);
}
// note, the pre-allocated memory for value is used
attr->value = value;
for (unsigned char *s = attr->name; *s; ++s) {
*s = tolower(*s);
}
return attr;
}
struct HtmlAttribute *
html_attribute_free(struct HtmlAttribute *attr)
{
if (!attr) return NULL;
xfree(attr->name);
xfree(attr->value);
xfree(attr);
return NULL;
}
struct HtmlElement *
html_element_create(const unsigned char *ptr, int len)
{
struct HtmlElement *elem = NULL;
XCALLOC(elem, 1);
if (len <= 0) {
elem->name = xstrdup(ptr);
} else {
elem->name = xmemdup(ptr, len);
}
for (unsigned char *s = elem->name; *s; ++s) {
*s = tolower(*s);
}
return elem;
}
struct HtmlElement *
html_element_free(struct HtmlElement *elem)
{
if (!elem) return NULL;
xfree(elem->name);
struct HtmlAttribute *p, *q;
for (p = elem->first_attr; p; p = q) {
q = p->next;
html_attribute_free(p);
}
for (HtmlElement *child = elem->first_child; child; ) {
HtmlElement *tmp = child->next_sibling;
html_element_free(child);
child = tmp;
}
xfree(elem);
return NULL;
}
struct EntityTableEntry
{
const unsigned char *name;
int value;
};
static struct EntityTableEntry html_entities[] =
{
{ "amp", '&' },
{ "quot", '"' },
{ "apos", '\'' },
{ "lt", '<' },
{ "gt", '>' },
{ NULL, 0 },
};
static int
find_html_entity(const unsigned char *beg, const unsigned char *end)
{
int len = (int) (end - beg);
unsigned char buf[128], *s = buf;
const unsigned char *p = beg;
if (len <= 0 || len > 127) return -1;
while (p != end) *s++ = tolower(*p++);
*s = 0;
for (int i = 0; html_entities[i].name; ++i) {
if (!strcmp(html_entities[i].name, buf)) {
return html_entities[i].value;
}
}
return -1;
}
static int
convert_html_entity(const unsigned char *beg, const unsigned char *end, int base)
{
int len = (int) (end - beg);
unsigned char buf[32];
if (len <= 0 || len > 31) return -1;
memcpy(buf, beg, len);
buf[len] = 0;
errno = 0;
int value = strtol(buf, NULL, base);
if (errno || value < 0 || value >= 0x10000) return -1;
return value;
}
static void
parse_html_decode(
unsigned char *buf,
const unsigned char *start,
const unsigned char *end)
{
const unsigned char *p = start;
unsigned char *out = buf;
while (*p && p != end) {
if (*p != '&') {
*out++ = *p++;
continue;
}
++p;
if (!*p || p == end) {
*out++ = '&';
continue;
}
if (*p == '#' && p[1] == 'x') {
const unsigned char *q = p;
p += 2;
while (isxdigit(*p)) ++p;
int e = convert_html_entity(q + 2, p, 16);
if (e >= 0) {
out = ucs4_to_utf8_char(out, e);
if (*p == ';') ++p;
} else {
*out++ = '&';
p = q;
}
} else if (*p == '#') {
const unsigned char *q = p;
++p;
while (isdigit(*p)) ++p;
int e = convert_html_entity(q + 1, p, 10);
if (e >= 0) {
out = ucs4_to_utf8_char(out, e);
if (*p == ';') ++p;
} else {
*out++ = '&';
p = q;
}
} else if (isalpha(*p)) {
// entity expansion is not performed...
const unsigned char *q = p;
while (isalnum(*p)) ++p;
int e = find_html_entity(q, p);
if (e >= 0) {
out = ucs4_to_utf8_char(out, e);
if (*p == ';') ++p;
} else {
*out++ = '&';
p = q;
}
} else {
*out++ = '&';
continue;
}
}
*out = 0;
}
struct HtmlElement *
html_element_parse_start(
const unsigned char *text,
int start_pos,
int *p_end_pos)
{
const unsigned char *p = text + start_pos, *q, *r;
const unsigned char *value_start, *value_end;
struct HtmlElement *elem = NULL;
struct HtmlAttribute *attr = NULL;
unsigned char *value_buf = NULL;
while (isspace(*p)) ++p;
if (!*p) goto fail;
if (*p != '<') goto fail;
++p;
while (isspace(*p)) ++p;
if (!is_xml_element_name_start_char(*p)) goto fail;
q = p;
while (is_xml_element_name_char(*p)) ++p;
if (!isspace(*p) && *p != '>') goto fail;
elem = html_element_create(q, (int) (p - q));
while (1) {
while (isspace(*p)) ++p;
if (*p == '>') {
++p;
break;
}
if (*p == '/' && p[1] == '>') {
elem->no_body = 1;
p += 2;
break;
}
if (!is_xml_element_name_start_char(*p)) goto fail;
q = p;
while (is_xml_element_name_char(*p)) ++p;
r = p;
while (isspace(*p)) ++p;
if (*p != '=') {
// create an empty value attribute
// FIXME: check attribute uniqueness
attr = html_attribute_create(q, (int) (r - q), NULL);
LINK_LAST(attr, elem->first_attr, elem->last_attr, prev, next);
attr = NULL;
continue;
}
++p;
while (isspace(*p)) ++p;
if (*p == '\'') {
++p;
value_start = p;
while (*p && *p != '\'') ++p;
if (!*p) goto fail;
value_end = p;
++p;
} else if (*p == '\"') {
++p;
value_start = p;
while (*p && *p != '\"') ++p;
if (!*p) goto fail;
value_end = p;
++p;
} else if (is_xml_element_name_start_char(*p)) {
value_start = p;
while (*p && !isspace(*p)) ++p;
if (!*p) goto fail;
value_end = p;
} else {
// create an empty value attribute
// FIXME: check attribute uniqueness
attr = html_attribute_create(q, (int) (r - q), NULL);
LINK_LAST(attr, elem->first_attr, elem->last_attr, prev, next);
attr = NULL;
continue;
}
// value is in [value_start; value_end), not entity-decoded...
value_buf = malloc(((int) (value_end - value_start) + 1) * sizeof(*value_buf));
if (!value_buf) goto fail;
parse_html_decode(value_buf, value_start, value_end);
attr = html_attribute_create(q, (int) (r - q), value_buf);
LINK_LAST(attr, elem->first_attr, elem->last_attr, prev, next);
attr = NULL; value_buf = NULL;
}
if (p_end_pos) *p_end_pos = (int)(p - text);
return elem;
fail:
xfree(value_buf);
html_element_free(elem);
if (p_end_pos) *p_end_pos = (int)(p - text);
return NULL;
}
struct HtmlElement *
html_element_parse_end(
const unsigned char *text,
int start_pos,
int *p_end_pos)
{
const unsigned char *p = text + start_pos, *q;
struct HtmlElement *elem = NULL;
while (isspace(*p)) ++p;
if (!*p) goto fail;
if (*p != '<') goto fail;
++p;
while (isspace(*p)) ++p;
if (*p != '/') goto fail;
++p;
while (isspace(*p)) ++p;
if (!is_xml_element_name_start_char(*p)) goto fail;
q = p;
while (is_xml_element_name_char(*p)) ++p;
if (!isspace(*p) && *p != '>') goto fail;
elem = html_element_create(q, (int) (p - q));
while (isspace(*p)) ++p;
if (*p != '>') goto fail;
++p;
if (p_end_pos) *p_end_pos = (int)(p - text);
return elem;
fail:
html_element_free(elem);
if (p_end_pos) *p_end_pos = (int)(p - text);
return NULL;
}
struct HtmlAttribute *
html_element_find_attribute(
const struct HtmlElement *elem,
const unsigned char *name)
{
if (!elem) return NULL;
for (struct HtmlAttribute *p = elem->first_attr; p; p = p->next) {
if (!strcmp(p->name, name))
return p;
}
return NULL;
}
void
html_element_print(
FILE *out,
const struct HtmlElement *elem)
{
if (!elem) return;
fprintf(out, "<%s", elem->name);
for (struct HtmlAttribute *p = elem->first_attr; p; p = p->next) {
// FIXME: do escaping
fprintf(out, " %s=\"%s\"", p->name, p->value);
}
if (elem->no_body) putc('/', out);
putc('>', out);
}
HtmlAttribute *
html_attribute_clone(const HtmlAttribute *attr)
{
HtmlAttribute *res = NULL;
if (attr) {
XCALLOC(res, 1);
if (attr->name) res->name = xstrdup(attr->name);
if (attr->value) res->value = xstrdup(attr->value);
}
return res;
}
HtmlElement *
html_element_clone(const HtmlElement *elem)
{
if (!elem) return NULL;
HtmlElement *res = NULL;
XCALLOC(res, 1);
if (elem->name) res->name = xstrdup(elem->name);
res->no_body = elem->no_body;
for (const HtmlAttribute *asrc = elem->first_attr; asrc; asrc = asrc->next) {
HtmlAttribute *adst = html_attribute_clone(asrc);
LINK_LAST(adst, res->first_attr, res->last_attr, prev, next);
}
return res;
}
void
html_element_add_child(HtmlElement *elem, HtmlElement *child)
{
if (!elem || !child) return;
LINK_LAST(child, elem->first_child, elem->last_child, prev_sibling, next_sibling);
}
const unsigned char *
html_element_find_attribute_value(
const struct HtmlElement *elem,
const unsigned char *name)
{
HtmlAttribute *attr = html_element_find_attribute(elem, name);
if (!attr) return NULL;
return attr->value;
}
/*
* Local variables:
* c-basic-offset: 4
* End:
*/