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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
|
/*
+----------------------------------------------------------------------+
| PHP Version 4 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2003 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Sterling Hughes <sterling@php.net> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "expat_compat.h"
#ifdef LIBXML_EXPAT_COMPAT
#define IS_NS_DECL(__ns) \
((__ns) != NULL && strlen(__ns) == 5 && *(__ns) == 'x' && *((__ns)+1) == 'm' && \
*((__ns)+2) == 'l' && *((__ns)+3) == 'n' && *((__ns)+4) == 's')
static void
_find_namespace_decl(XML_Parser parser, const xmlChar *tagname, const xmlChar **attr)
{
xmlChar **attr_p = (xmlChar **) attr;
xmlChar *name;
xmlChar *value;
xmlChar *partial;
xmlChar *namespace;
while (attr_p && *attr_p) {
name = attr_p[0];
value = xmlStrdup(attr_p[1]);
partial = xmlSplitQName(parser->parser, name, &namespace);
if (IS_NS_DECL(namespace)) {
if (parser->h_start_ns) {
parser->h_start_ns(parser->user, partial, (const XML_Char *) value);
}
xmlHashAddEntry(parser->_ns_map, partial, value);
xmlHashAddEntry(parser->_reverse_ns_map, tagname, xmlStrdup(partial));
break;
}
xmlFree(value);
attr_p += 2;
}
}
static void
_qualify_namespace(XML_Parser parser, const xmlChar *name, xmlChar **qualified)
{
xmlChar *partial;
xmlChar *namespace;
int len;
partial = xmlSplitQName(parser->parser, name, &namespace);
if (namespace) {
xmlChar *nsvalue;
nsvalue = xmlHashLookup(parser->_ns_map, namespace);
if (nsvalue) {
len = strlen(nsvalue) + strlen(partial) + 1; /* colon */
*qualified = malloc(len+1);
memcpy(*qualified, nsvalue, strlen(nsvalue));
memcpy(*qualified + strlen(nsvalue), ":", 1);
memcpy(*qualified + strlen(nsvalue) + 1, partial, strlen(partial));
(*qualified)[len] = '\0';
} else {
*qualified = xmlStrdup(name);
}
} else {
*qualified = xmlStrdup(name);
}
}
static void
_start_element_handler(void *user, const xmlChar *name, const xmlChar **attributes)
{
XML_Parser parser = (XML_Parser) user;
xmlChar *qualified_name = NULL;
if (parser->h_start_element == NULL) {
return;
}
if (parser->use_namespace) {
_find_namespace_decl(parser, name, attributes);
_qualify_namespace(parser, name, &qualified_name);
} else {
qualified_name = xmlStrdup(name);
}
parser->h_start_element(parser->user, (const XML_Char *) qualified_name, (const XML_Char **) attributes);
xmlFree(qualified_name);
}
static void
_end_element_handler(void *user, const xmlChar *name)
{
xmlChar *qualified_name;
XML_Parser parser = (XML_Parser) user;
if (parser->h_end_element == NULL) {
return;
}
if (parser->use_namespace) {
xmlChar *nsname;
nsname = xmlHashLookup(parser->_reverse_ns_map, name);
if (nsname && parser->h_end_ns) {
parser->h_end_ns(parser->user, nsname);
}
_qualify_namespace(parser, name, &qualified_name);
} else {
qualified_name = xmlStrdup(name);
}
parser->h_end_element(parser->user, (const XML_Char *) qualified_name);
xmlFree(qualified_name);
}
static void
_cdata_handler(void *user, const xmlChar *cdata, int cdata_len)
{
XML_Parser parser = (XML_Parser) user;
if (parser->h_cdata == NULL) {
return;
}
parser->h_cdata(parser->user, (const XML_Char *) cdata, cdata_len);
}
static void
_pi_handler(void *user, const xmlChar *target, const xmlChar *data)
{
XML_Parser parser = (XML_Parser) user;
if (parser->h_pi == NULL) {
return;
}
parser->h_pi(parser->user, (const XML_Char *) target, (const XML_Char *) data);
}
static void
_unparsed_entity_decl_handler(void *user,
const xmlChar *name,
const xmlChar *sys_id,
const xmlChar *pub_id,
const xmlChar *notation)
{
XML_Parser parser = (XML_Parser) user;
if (parser->h_unparsed_entity_decl == NULL) {
return;
}
parser->h_unparsed_entity_decl(parser->user, name, NULL, sys_id, pub_id, notation);
}
static void
_notation_decl_handler(void *user, const xmlChar *notation, const xmlChar *sys_id, const xmlChar *pub_id)
{
XML_Parser parser = (XML_Parser) user;
if (parser->h_notation_decl == NULL) {
return;
}
parser->h_notation_decl(parser->user, notation, NULL, sys_id, pub_id);
}
static void
_build_comment(const xmlChar *data, int data_len, xmlChar **comment, int *comment_len)
{
*comment_len = data_len + 6;
*comment = xmlMalloc(*comment_len + 1);
memcpy(*comment, "<--", 3);
memcpy(*comment + 3, data, data_len);
memcpy(*comment + 3 + data_len, "-->", 3);
(*comment)[*comment_len] = '\0';
}
static void
_comment_handler(void *user, const xmlChar *comment)
{
XML_Parser parser = (XML_Parser) user;
if (parser->h_default) {
xmlChar *d_comment;
int d_comment_len;
_build_comment(comment, xmlStrlen(comment), &d_comment, &d_comment_len);
parser->h_default(parser->user, d_comment, d_comment_len);
}
}
static void
_build_entity(const xmlChar *name, int len, xmlChar **entity, int *entity_len)
{
*entity_len = len + 2;
*entity = xmlMalloc(*entity_len + 1);
(*entity)[0] = '&';
memcpy(*entity+1, name, len);
(*entity)[len+1] = ';';
(*entity)[*entity_len] = '\0';
}
static xmlEntityPtr
_get_entity(void *user, const xmlChar *name)
{
XML_Parser parser = (XML_Parser) user;
if (parser->h_default) {
xmlChar *entity;
int len;
_build_entity(name, xmlStrlen(name), &entity, &len);
parser->h_default(parser->user, (const xmlChar *) entity, len);
}
return NULL;
}
static void
_external_entity_ref_handler(void *user, const xmlChar *names, int type, const xmlChar *sys_id, const xmlChar *pub_id, xmlChar *content)
{
XML_Parser parser = (XML_Parser) user;
if (parser->h_external_entity_ref == NULL) {
return;
}
parser->h_external_entity_ref(parser, names, "", sys_id, pub_id);
}
static xmlSAXHandler
php_xml_compat_handlers = {
NULL, /* internalSubset */
NULL, /* isStandalone */
NULL, /* hasInternalSubset */
NULL, /* hasExternalSubset */
NULL, /* resolveEntity */
_get_entity, /* getEntity */
_external_entity_ref_handler, /* entityDecl */
_notation_decl_handler,
NULL, /* attributeDecl */
NULL, /* elementDecl */
_unparsed_entity_decl_handler, /* unparsedEntity */
NULL, /* setDocumentLocator */
NULL, /* startDocument */
NULL, /* endDocument */
_start_element_handler,
_end_element_handler,
NULL, /* reference */
_cdata_handler,
NULL, /* ignorableWhitespace */
_pi_handler,
_comment_handler, /* comment */
NULL, /* warning */
NULL, /* error */
NULL /* fatalError */
};
XML_Parser
XML_ParserCreate(const XML_Char *encoding)
{
return XML_ParserCreate_MM(encoding, NULL, NULL);
}
XML_Parser
XML_ParserCreateNS(const XML_Char *encoding, const XML_Char sep)
{
XML_Char tmp[2];
tmp[0] = sep;
tmp[1] = '\0';
return XML_ParserCreate_MM(encoding, NULL, tmp);
}
static void *(*_expat_cpt_malloc_fcn)(size_t sz);
static char *_expat_cpt_intn_strdup(const char *str)
{
size_t len;
char *retval;
len = strlen(str);
if ((retval = _expat_cpt_malloc_fcn(len + 1)) == NULL) {
return NULL;
}
memcpy(retval, str, len + 1);
return retval;
}
XML_Parser
XML_ParserCreate_MM(const XML_Char *encoding, const XML_Memory_Handling_Suite *memsuite, const XML_Char *sep)
{
XML_Parser parser;
static XML_Memory_Handling_Suite mtemp_i = { malloc, realloc, free };
if (memsuite == NULL) {
memsuite = &mtemp_i;
}
_expat_cpt_malloc_fcn = memsuite->malloc_fcn; /* FIXME: not reentrant ! */
xmlMemSetup(memsuite->free_fcn, memsuite->malloc_fcn, memsuite->realloc_fcn, _expat_cpt_intn_strdup); /* WHOCANFIXME: not reentrant ! */
parser = (XML_Parser) memsuite->malloc_fcn(sizeof(struct _XML_Parser));
memset(parser, 0, sizeof(struct _XML_Parser));
parser->use_namespace = 0;
parser->mem_hdlrs = *memsuite;
parser->parser = xmlCreatePushParserCtxt((xmlSAXHandlerPtr) &php_xml_compat_handlers, (void *) parser, NULL, 0, NULL);
if (parser->parser == NULL) {
return NULL;
}
if (encoding != NULL) {
parser->parser->encoding = xmlStrdup(encoding);
} else {
parser->parser->charset = XML_CHAR_ENCODING_NONE;
}
if (sep != NULL) {
parser->use_namespace = 1;
parser->_ns_map = xmlHashCreate(10);
parser->_reverse_ns_map = xmlHashCreate(10);
}
return parser;
}
void
XML_SetUserData(XML_Parser parser, void *user)
{
parser->user = user;
}
void *
XML_GetUserData(XML_Parser parser)
{
return parser->user;
}
void
XML_SetElementHandler(XML_Parser parser, XML_StartElementHandler start, XML_EndElementHandler end)
{
parser->h_start_element = start;
parser->h_end_element = end;
}
void
XML_SetCharacterDataHandler(XML_Parser parser, XML_CharacterDataHandler cdata)
{
parser->h_cdata = cdata;
}
void
XML_SetProcessingInstructionHandler(XML_Parser parser, XML_ProcessingInstructionHandler pi)
{
parser->h_pi = pi;
}
void
XML_SetCommentHandler(XML_Parser parser, XML_CommentHandler comment)
{
parser->h_comment = comment;
}
void
XML_SetDefaultHandler(XML_Parser parser, XML_DefaultHandler d)
{
parser->h_default = d;
}
void
XML_SetUnparsedEntityDeclHandler(XML_Parser parser, XML_UnparsedEntityDeclHandler unparsed_decl)
{
parser->h_unparsed_entity_decl = unparsed_decl;
}
void
XML_SetNotationDeclHandler(XML_Parser parser, XML_NotationDeclHandler notation_decl)
{
parser->h_notation_decl = notation_decl;
}
void
XML_SetExternalEntityRefHandler(XML_Parser parser, XML_ExternalEntityRefHandler ext_entity)
{
parser->h_external_entity_ref = ext_entity;
}
void
XML_SetStartNamespaceDeclHandler(XML_Parser parser, XML_StartNamespaceDeclHandler start_ns)
{
parser->h_start_ns = start_ns;
}
void
XML_SetEndNamespaceDeclHandler(XML_Parser parser, XML_EndNamespaceDeclHandler end_ns)
{
parser->h_end_ns = end_ns;
}
int
XML_Parse(XML_Parser parser, const XML_Char *data, int data_len, int is_final)
{
return !xmlParseChunk(parser->parser, data, data_len, is_final);
}
int
XML_GetErrorCode(XML_Parser parser)
{
return parser->parser->errNo;
}
const XML_Char *error_mapping[] = {
"NO ERROR",
"XML_ERROR_INTERNAL_ERROR",
"XML_NO_MEMORY",
"XML_ERR_DOCUMENT_START",
"XML_ERR_DOCUMENT_EMPTY",
"XML_ERR_DOCUMENT_END",
"XML_ERR_INVALID_HEX_CHARREF",
"XML_ERR_INVALID_DEC_CHARREF",
"XML_ERR_INVALID_CHARREF",
"XML_ERR_INVALID_CHAR",
"XML_ERR_CHARREF_AT_EOF",
"XML_ERR_CHARREF_IN_PROLOG",
"XML_ERR_CHARREF_IN_EPILOG",
"XML_ERR_CHARREF_IN_DTD",
"XML_ERR_ENTITYREF_AT_EOF",
"XML_ERR_ENTITYREF_IN_PROLOG",
"XML_ERR_ENTITYREF_IN_EPILOG",
"XML_ERR_ENTITYREF_IN_DTD",
"XML_ERR_PEREF_AT_EOF",
"XML_ERR_PEREF_IN_PROLOG",
"XML_ERR_PEREF_IN_EPILOG",
"XML_ERR_PEREF_IN_INT_SUBSET",
"XML_ERR_ENTITYREF_NO_NAME",
"XML_ERR_ENTITYREF_SEMICOL_MISSING",
"XML_ERR_PEREF_NO_NAME",
"XML_ERR_PEREF_SEMICOL_MISSING",
"XML_ERR_UNDECLARED_ENTITY",
"XML_WAR_UNDECLARED_ENTITY",
"XML_ERR_UNPARSED_ENTITY",
"XML_ERR_ENTITY_IS_EXTERNAL",
"XML_ERR_ENTITY_IS_PARAMETER",
"XML_ERR_UNKNOWN_ENCODING",
"XML_ERR_UNSUPPORTED_ENCODING",
"XML_ERR_STRING_NOT_STARTED",
"XML_ERR_STRING_NOT_CLOSED",
"XML_ERR_NS_DECL_ERROR",
"XML_ERR_ENTITY_NOT_STARTED",
"XML_ERR_ENTITY_NOT_FINISHED",
"XML_ERR_LT_IN_ATTRIBUTE",
"XML_ERR_ATTRIBUTE_NOT_STARTED",
"XML_ERR_ATTRIBUTE_NOT_FINISHED",
"XML_ERR_ATTRIBUTE_WITHOUT_VALUE",
"XML_ERR_ATTRIBUTE_REDEFINED",
"XML_ERR_LITERAL_NOT_STARTED",
"XML_ERR_LITERAL_NOT_FINISHED",
/* "XML_ERR_COMMENT_NOT_STARTED", <= eliminated on purpose */
"XML_ERR_COMMENT_NOT_FINISHED",
"XML_ERR_PI_NOT_STARTED",
"XML_ERR_PI_NOT_FINISHED",
"XML_ERR_NOTATION_NOT_STARTED",
"XML_ERR_NOTATION_NOT_FINISHED",
"XML_ERR_ATTLIST_NOT_STARTED",
"XML_ERR_ATTLIST_NOT_FINISHED",
"XML_ERR_MIXED_NOT_STARTED",
"XML_ERR_MIXED_NOT_FINISHED",
"XML_ERR_ELEMCONTENT_NOT_STARTED",
"XML_ERR_ELEMCONTENT_NOT_FINISHED",
"XML_ERR_XMLDECL_NOT_STARTED",
"XML_ERR_XMLDECL_NOT_FINISHED",
"XML_ERR_CONDSEC_NOT_STARTED",
"XML_ERR_CONDSEC_NOT_FINISHED",
"XML_ERR_EXT_SUBSET_NOT_FINISHED",
"XML_ERR_DOCTYPE_NOT_FINISHED",
"XML_ERR_MISPLACED_CDATA_END",
"XML_ERR_CDATA_NOT_FINISHED",
"XML_ERR_RESERVED_XML_NAME",
"XML_ERR_SPACE_REQUIRED",
"XML_ERR_SEPARATOR_REQUIRED",
"XML_ERR_NMTOKEN_REQUIRED",
"XML_ERR_NAME_REQUIRED",
"XML_ERR_PCDATA_REQUIRED",
"XML_ERR_URI_REQUIRED",
"XML_ERR_PUBID_REQUIRED",
"XML_ERR_LT_REQUIRED",
"XML_ERR_GT_REQUIRED",
"XML_ERR_LTSLASH_REQUIRED",
"XML_ERR_EQUAL_REQUIRED",
"XML_ERR_TAG_NAME_MISMATCH",
"XML_ERR_TAG_NOT_FINISHED",
"XML_ERR_STANDALONE_VALUE",
"XML_ERR_ENCODING_NAME",
"XML_ERR_HYPHEN_IN_COMMENT",
"XML_ERR_INVALID_ENCODING",
"XML_ERR_EXT_ENTITY_STANDALONE",
"XML_ERR_CONDSEC_INVALID",
"XML_ERR_VALUE_REQUIRED",
"XML_ERR_NOT_WELL_BALANCED",
"XML_ERR_EXTRA_CONTENT",
"XML_ERR_ENTITY_CHAR_ERROR",
"XML_ERR_ENTITY_PE_INTERNAL",
"XML_ERR_ENTITY_LOOP",
"XML_ERR_ENTITY_BOUNDARY",
"XML_ERR_INVALID_URI",
"XML_ERR_URI_FRAGMENT",
"XML_WAR_CATALOG_PI",
"XML_ERR_NO_DTD"
};
const XML_Char *
XML_ErrorString(int code)
{
if (code < 0 || code >= sizeof(error_mapping)) {
return "Unknown";
}
return error_mapping[code];
}
int
XML_GetCurrentLineNumber(XML_Parser parser)
{
return parser->parser->input->line;
}
int
XML_GetCurrentColumnNumber(XML_Parser parser)
{
return parser->parser->input->col;
}
int
XML_GetCurrentByteIndex(XML_Parser parser)
{
return parser->parser->input->consumed;
}
const XML_Char *XML_ExpatVersion(void)
{
return "1.0";
}
static void
_free_ns_name(void *ptr, xmlChar *name)
{
xmlFree(ptr);
}
void
XML_ParserFree(XML_Parser parser)
{
if (parser->use_namespace) {
xmlHashFree(parser->_ns_map, _free_ns_name);
xmlHashFree(parser->_reverse_ns_map, _free_ns_name);
}
xmlFreeParserCtxt(parser->parser);
parser->mem_hdlrs.free_fcn(parser);
}
#endif /* LIBXML_EXPAT_COMPAT */
/**
* Local Variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
* vim600: fdm=marker
* vim: ts=4 noet sw=4
*/
|