xref: /PHP-8.4/ext/xml/compat.c (revision 19358d63)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Sterling Hughes <sterling@php.net>                          |
14    +----------------------------------------------------------------------+
15  */
16 
17 #include "php.h"
18 #if defined(HAVE_LIBXML) && (defined(HAVE_XML) || defined(HAVE_XMLRPC)) && !defined(HAVE_LIBEXPAT)
19 #include "expat_compat.h"
20 #include "ext/libxml/php_libxml.h"
21 
22 #ifdef LIBXML_EXPAT_COMPAT
23 
24 static void
qualify_namespace(XML_Parser parser,const xmlChar * name,const xmlChar * URI,xmlChar ** qualified)25 qualify_namespace(XML_Parser parser, const xmlChar *name, const xmlChar *URI, xmlChar **qualified)
26 {
27 	if (URI) {
28 			/* Use libxml functions otherwise its memory deallocation is screwed up */
29 			*qualified = xmlStrdup(URI);
30 			*qualified = xmlStrncat(*qualified, parser->_ns_separator, 1);
31 			*qualified = xmlStrncat(*qualified, name, xmlStrlen(name));
32 	} else {
33 		*qualified = xmlStrdup(name);
34 	}
35 }
36 
37 static void
start_element_handler(void * user,const xmlChar * name,const xmlChar ** attributes)38 start_element_handler(void *user, const xmlChar *name, const xmlChar **attributes)
39 {
40 	XML_Parser  parser = (XML_Parser) user;
41 	xmlChar    *qualified_name = NULL;
42 
43 	if (parser->h_start_element == NULL) {
44 		if (parser->h_default) {
45 			int attno = 0;
46 
47 			qualified_name = xmlStrncatNew((xmlChar *)"<", name, xmlStrlen(name));
48 			if (attributes) {
49 				while (attributes[attno] != NULL) {
50 					int att_len;
51 					char *att_string, *att_name, *att_value;
52 
53 					att_name = (char *)attributes[attno++];
54 					att_value = (char *)attributes[attno++];
55 
56 					att_len = spprintf(&att_string, 0, " %s=\"%s\"", att_name, att_value);
57 
58 					qualified_name = xmlStrncat(qualified_name, (xmlChar *)att_string, att_len);
59 					efree(att_string);
60 				}
61 
62 			}
63 			qualified_name = xmlStrncat(qualified_name, (xmlChar *)">", 1);
64 			parser->h_default(parser->user, (const XML_Char *) qualified_name, xmlStrlen(qualified_name));
65 			xmlFree(qualified_name);
66 		}
67 		return;
68 	}
69 
70 	parser->h_start_element(parser->user, name, (const XML_Char **) attributes);
71 }
72 
73 static void
start_element_handler_ns(void * user,const xmlChar * name,const xmlChar * prefix,const xmlChar * URI,int nb_namespaces,const xmlChar ** namespaces,int nb_attributes,int nb_defaulted,const xmlChar ** attributes)74 start_element_handler_ns(void *user, const xmlChar *name, const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, const xmlChar ** namespaces, int nb_attributes, int nb_defaulted, const xmlChar ** attributes)
75 {
76 	XML_Parser  parser = (XML_Parser) user;
77 	xmlChar    *qualified_name = NULL;
78 	xmlChar **attrs = NULL;
79 	int i;
80 	int z = 0;
81 	int y = 0;
82 
83 	if (nb_namespaces > 0 && parser->h_start_ns != NULL) {
84 		for (i = 0; i < nb_namespaces; i += 1) {
85 			parser->h_start_ns(parser->user, (const XML_Char *) namespaces[y], (const XML_Char *) namespaces[y+1]);
86 			y += 2;
87 		}
88 		y = 0;
89 	}
90 
91 	if (parser->h_start_element == NULL) {
92 	 	if (parser->h_default) {
93 
94 			if (prefix) {
95 				qualified_name = xmlStrncatNew((xmlChar *)"<", prefix, xmlStrlen(prefix));
96 				qualified_name = xmlStrncat(qualified_name, (xmlChar *)":", 1);
97 				qualified_name = xmlStrncat(qualified_name, name, xmlStrlen(name));
98 			} else {
99 				qualified_name = xmlStrncatNew((xmlChar *)"<", name, xmlStrlen(name));
100 			}
101 
102 			if (namespaces) {
103 				int i, j;
104 				for (i = 0,j = 0;j < nb_namespaces;j++) {
105 					int ns_len;
106 					char *ns_string, *ns_prefix, *ns_url;
107 
108 					ns_prefix = (char *) namespaces[i++];
109 					ns_url = (char *) namespaces[i++];
110 
111 					if (ns_prefix) {
112 						ns_len = spprintf(&ns_string, 0, " xmlns:%s=\"%s\"", ns_prefix, ns_url);
113 					} else {
114 						ns_len = spprintf(&ns_string, 0, " xmlns=\"%s\"", ns_url);
115 					}
116 					qualified_name = xmlStrncat(qualified_name, (xmlChar *)ns_string, ns_len);
117 
118 					efree(ns_string);
119 				}
120 			}
121 
122 			if (attributes) {
123 				for (i = 0; i < nb_attributes; i += 1) {
124 					int att_len;
125 					char *att_string, *att_name, *att_value, *att_prefix, *att_valueend;
126 
127 					att_name = (char *) attributes[y++];
128 					att_prefix = (char *)attributes[y++];
129 					y++;
130 					att_value = (char *)attributes[y++];
131 					att_valueend = (char *)attributes[y++];
132 
133 					if (att_prefix) {
134 						att_len = spprintf(&att_string, 0, " %s:%s=\"", att_prefix, att_name);
135 					} else {
136 						att_len = spprintf(&att_string, 0, " %s=\"", att_name);
137 					}
138 
139 					qualified_name = xmlStrncat(qualified_name, (xmlChar *)att_string, att_len);
140 					qualified_name = xmlStrncat(qualified_name, (xmlChar *)att_value, att_valueend - att_value);
141 					qualified_name = xmlStrncat(qualified_name, (xmlChar *)"\"", 1);
142 
143 					efree(att_string);
144 				}
145 
146 			}
147 			qualified_name = xmlStrncat(qualified_name, (xmlChar *)">", 1);
148 			parser->h_default(parser->user, (const XML_Char *) qualified_name, xmlStrlen(qualified_name));
149 			xmlFree(qualified_name);
150 		}
151 		return;
152 	}
153 	qualify_namespace(parser, name, URI, &qualified_name);
154 
155 	if (attributes != NULL) {
156 		xmlChar    *qualified_name_attr = NULL;
157 		attrs = safe_emalloc((nb_attributes  * 2) + 1, sizeof(int *), 0);
158 
159 		for (i = 0; i < nb_attributes; i += 1) {
160 
161 			if (attributes[y+1] != NULL) {
162 				qualify_namespace(parser, attributes[y] , attributes[y + 2], &qualified_name_attr);
163 			} else {
164 				qualified_name_attr = xmlStrdup(attributes[y]);
165 			}
166 			attrs[z] = qualified_name_attr;
167 			attrs[z + 1] = xmlStrndup(attributes[y + 3] , (int) (attributes[y + 4] - attributes[y + 3]));
168 			z += 2;
169 			y += 5;
170 		}
171 
172 		attrs[z] = NULL;
173 	}
174 	parser->h_start_element(parser->user, (const XML_Char *) qualified_name, (const XML_Char **) attrs);
175 	if (attrs) {
176 		for (i = 0; i < z; i++) {
177 			xmlFree(attrs[i]);
178 		}
179 		efree(attrs);
180 	}
181 	xmlFree(qualified_name);
182 }
183 
184 static void
end_element_handler(void * user,const xmlChar * name)185 end_element_handler(void *user, const xmlChar *name)
186 {
187 	XML_Parser  parser = (XML_Parser) user;
188 
189 	if (parser->h_end_element == NULL) {
190 		if (parser->h_default) {
191 			char *end_element;
192 
193 			spprintf(&end_element, 0, "</%s>", (char *)name);
194 			parser->h_default(parser->user, (const XML_Char *) end_element, strlen(end_element));
195 			efree(end_element);
196 		}
197 		return;
198 	}
199 
200 	parser->h_end_element(parser->user, (const XML_Char *) name);
201 }
202 
203 static void
end_element_handler_ns(void * user,const xmlChar * name,const xmlChar * prefix,const xmlChar * URI)204 end_element_handler_ns(void *user, const xmlChar *name, const xmlChar * prefix, const xmlChar *URI)
205 {
206 	xmlChar    *qualified_name;
207 	XML_Parser  parser = (XML_Parser) user;
208 
209 	if (parser->h_end_element == NULL) {
210 		if (parser->h_default) {
211 			char *end_element;
212 			int end_element_len;
213 
214 			if (prefix) {
215 				end_element_len = spprintf(&end_element, 0, "</%s:%s>", (char *) prefix, (char *)name);
216 			} else {
217 				end_element_len = spprintf(&end_element, 0, "</%s>", (char *)name);
218 			}
219 			parser->h_default(parser->user, (const XML_Char *) end_element, end_element_len);
220 			efree(end_element);
221 		}
222 		return;
223 	}
224 
225 	qualify_namespace(parser, name, URI,  &qualified_name);
226 
227 	parser->h_end_element(parser->user, (const XML_Char *) qualified_name);
228 
229 	xmlFree(qualified_name);
230 }
231 
232 static void
cdata_handler(void * user,const xmlChar * cdata,int cdata_len)233 cdata_handler(void *user, const xmlChar *cdata, int cdata_len)
234 {
235 	XML_Parser parser = (XML_Parser) user;
236 
237 	if (parser->h_cdata == NULL) {
238 		if (parser->h_default) {
239 			parser->h_default(parser->user, (const XML_Char *) cdata, cdata_len);
240 		}
241 		return;
242 	}
243 
244 	parser->h_cdata(parser->user, (const XML_Char *) cdata, cdata_len);
245 }
246 
247 static void
pi_handler(void * user,const xmlChar * target,const xmlChar * data)248 pi_handler(void *user, const xmlChar *target, const xmlChar *data)
249 {
250 	XML_Parser parser = (XML_Parser) user;
251 
252 	if (parser->h_pi == NULL) {
253 		if (parser->h_default) {
254 			char    *full_pi;
255 			spprintf(&full_pi, 0, "<?%s %s?>", (char *)target, (char *)data);
256 			parser->h_default(parser->user, (const XML_Char *) full_pi, strlen(full_pi));
257 			efree(full_pi);
258 		}
259 		return;
260 	}
261 
262 	parser->h_pi(parser->user, (const XML_Char *) target, (const XML_Char *) data);
263 }
264 
265 static void
unparsed_entity_decl_handler(void * user,const xmlChar * name,const xmlChar * pub_id,const xmlChar * sys_id,const xmlChar * notation)266 unparsed_entity_decl_handler(void *user,
267                               const xmlChar *name,
268                               const xmlChar *pub_id,
269                               const xmlChar *sys_id,
270                               const xmlChar *notation)
271 {
272 	XML_Parser parser = (XML_Parser) user;
273 
274 	if (parser->h_unparsed_entity_decl == NULL) {
275 		return;
276 	}
277 
278 	parser->h_unparsed_entity_decl(parser->user, name, NULL, sys_id, pub_id, notation);
279 }
280 
281 static void
notation_decl_handler(void * user,const xmlChar * notation,const xmlChar * pub_id,const xmlChar * sys_id)282 notation_decl_handler(void *user, const xmlChar *notation, const xmlChar *pub_id, const xmlChar *sys_id)
283 {
284 	XML_Parser parser = (XML_Parser) user;
285 
286 	if (parser->h_notation_decl == NULL) {
287 		return;
288 	}
289 
290 	parser->h_notation_decl(parser->user, notation, NULL, sys_id, pub_id);
291 }
292 
293 static void
build_comment(const xmlChar * data,size_t data_len,xmlChar ** comment,size_t * comment_len)294 build_comment(const xmlChar *data, size_t data_len, xmlChar **comment, size_t *comment_len)
295 {
296 	*comment_len = data_len + 7;
297 
298 	*comment = xmlMalloc(*comment_len + 1);
299 	memcpy(*comment, "<!--", 4);
300 	memcpy(*comment + 4, data, data_len);
301 	memcpy(*comment + 4 + data_len, "-->", 3);
302 
303 	(*comment)[*comment_len] = '\0';
304 }
305 
306 static void
comment_handler(void * user,const xmlChar * comment)307 comment_handler(void *user, const xmlChar *comment)
308 {
309 	XML_Parser parser = (XML_Parser) user;
310 
311 	if (parser->h_default) {
312 		xmlChar *d_comment;
313 		size_t   d_comment_len;
314 
315 		build_comment(comment, (size_t) xmlStrlen(comment), &d_comment, &d_comment_len);
316 		parser->h_default(parser->user, d_comment, d_comment_len);
317 		xmlFree(d_comment);
318 	}
319 }
320 
321 static void
build_entity(const xmlChar * name,size_t len,xmlChar ** entity,size_t * entity_len)322 build_entity(const xmlChar *name, size_t len, xmlChar **entity, size_t *entity_len)
323 {
324 	*entity_len = len + 2;
325 	*entity = xmlMalloc(*entity_len + 1);
326 	(*entity)[0] = '&';
327 	memcpy(*entity+1, name, len);
328 	(*entity)[len+1] = ';';
329 	(*entity)[*entity_len] = '\0';
330 }
331 
332 static void
external_entity_ref_handler(void * user,const xmlChar * names,const xmlChar * sys_id,const xmlChar * pub_id)333 external_entity_ref_handler(void *user, const xmlChar *names, const xmlChar *sys_id, const xmlChar *pub_id)
334 {
335 	XML_Parser parser = (XML_Parser) user;
336 
337 	if (parser->h_external_entity_ref == NULL) {
338 		return;
339 	}
340 
341 	if (!parser->h_external_entity_ref(parser, names, (XML_Char *) "", sys_id, pub_id)) {
342 		xmlStopParser(parser->parser);
343 		parser->parser->errNo = XML_ERROR_EXTERNAL_ENTITY_HANDLING;
344 	};
345 }
346 
347 static xmlEntityPtr
get_entity(void * user,const xmlChar * name)348 get_entity(void *user, const xmlChar *name)
349 {
350 	XML_Parser parser = (XML_Parser) user;
351 	xmlEntityPtr ret = NULL;
352 
353 	if (parser->parser->inSubset == 0) {
354 		ret = xmlGetPredefinedEntity(name);
355 		if (ret == NULL)
356 			ret = xmlGetDocEntity(parser->parser->myDoc, name);
357 
358 		if (ret == NULL || parser->parser->instate == XML_PARSER_CONTENT) {
359 			if (ret == NULL || ret->etype == XML_INTERNAL_GENERAL_ENTITY || ret->etype == XML_INTERNAL_PARAMETER_ENTITY || ret->etype == XML_INTERNAL_PREDEFINED_ENTITY) {
360 				/* Predefined entities will expand unless no cdata handler is present */
361 				if (parser->h_default && ! (ret && ret->etype == XML_INTERNAL_PREDEFINED_ENTITY && parser->h_cdata)) {
362 					xmlChar *entity;
363 					size_t   len;
364 
365 					build_entity(name, (size_t) xmlStrlen(name), &entity, &len);
366 					parser->h_default(parser->user, (const xmlChar *) entity, len);
367 					xmlFree(entity);
368 				} else {
369 					/* expat will not expand internal entities if default handler is present otherwise
370 					it will expand and pass them to cdata handler */
371 					if (parser->h_cdata && ret) {
372 						parser->h_cdata(parser->user, ret->content, xmlStrlen(ret->content));
373 					}
374 				}
375 			} else {
376 				if (ret->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) {
377 					external_entity_ref_handler(user, ret->name, ret->SystemID, ret->ExternalID);
378 				}
379 			}
380 		}
381 	}
382 
383 	return ret;
384 }
385 
386 static const xmlSAXHandler
387 php_xml_compat_handlers = {
388 	NULL, /* internalSubset */
389 	NULL, /* isStandalone */
390 	NULL, /* hasInternalSubset */
391 	NULL, /* hasExternalSubset */
392 	NULL, /* resolveEntity */
393 	get_entity, /* getEntity */
394 	NULL, /* entityDecl */
395 	notation_decl_handler,
396 	NULL, /* attributeDecl */
397 	NULL, /* elementDecl */
398 	unparsed_entity_decl_handler, /* unparsedEntity */
399 	NULL, /* setDocumentLocator */
400 	NULL, /* startDocument */
401 	NULL, /* endDocument */
402 	start_element_handler, /* startElement */
403 	end_element_handler, /* endElement */
404 	NULL, /* reference */
405 	cdata_handler,
406 	NULL, /* ignorableWhitespace */
407 	pi_handler,
408 	comment_handler, /* comment */
409 	NULL, /* warning */
410 	NULL, /* error */
411 	NULL,  /* fatalError */
412 	NULL,  /* getParameterEntity */
413 	cdata_handler, /* cdataBlock */
414 	NULL, /* externalSubset */
415 	XML_SAX2_MAGIC,
416 	NULL,
417 	start_element_handler_ns,
418 	end_element_handler_ns,
419 	NULL
420 };
421 
422 PHP_XML_API XML_Parser
XML_ParserCreate(const XML_Char * encoding)423 XML_ParserCreate(const XML_Char *encoding)
424 {
425 	return XML_ParserCreate_MM(encoding, NULL, NULL);
426 }
427 
428 PHP_XML_API XML_Parser
XML_ParserCreateNS(const XML_Char * encoding,const XML_Char sep)429 XML_ParserCreateNS(const XML_Char *encoding, const XML_Char sep)
430 {
431 	XML_Char tmp[2];
432 	tmp[0] = sep;
433 	tmp[1] = '\0';
434 	return XML_ParserCreate_MM(encoding, NULL, tmp);
435 }
436 
437 PHP_XML_API XML_Parser
XML_ParserCreate_MM(const XML_Char * encoding,const XML_Memory_Handling_Suite * memsuite,const XML_Char * sep)438 XML_ParserCreate_MM(const XML_Char *encoding, const XML_Memory_Handling_Suite *memsuite, const XML_Char *sep)
439 {
440 	XML_Parser parser;
441 
442 	parser = emalloc(sizeof(struct XML_Parser_Struct));
443 	memset(parser, 0, sizeof(struct XML_Parser_Struct));
444 	parser->use_namespace = 0;
445 	parser->_ns_separator = NULL;
446 
447 	parser->parser = xmlCreatePushParserCtxt((xmlSAXHandlerPtr) &php_xml_compat_handlers, (void *) parser, NULL, 0, NULL);
448 	if (parser->parser == NULL) {
449 		efree(parser);
450 		return NULL;
451 	}
452 
453 	php_libxml_sanitize_parse_ctxt_options(parser->parser);
454 	xmlCtxtUseOptions(parser->parser, XML_PARSE_OLDSAX | XML_PARSE_NOENT);
455 
456 	parser->parser->wellFormed = 0;
457 	if (sep != NULL) {
458 		/* Note: sax2 flag will be set due to the magic number in `initialized` in php_xml_compat_handlers */
459 		ZEND_ASSERT(parser->parser->sax->initialized == XML_SAX2_MAGIC);
460 		parser->use_namespace = 1;
461 		parser->_ns_separator = xmlStrdup(sep);
462 	} else {
463 		/* Reset flag as XML_SAX2_MAGIC is needed for xmlCreatePushParserCtxt
464 		so must be set in the handlers */
465 		parser->parser->sax->initialized = 1;
466 	}
467 	return parser;
468 }
469 
470 PHP_XML_API void
XML_SetUserData(XML_Parser parser,void * user)471 XML_SetUserData(XML_Parser parser, void *user)
472 {
473 	parser->user = user;
474 }
475 
476 PHP_XML_API void *
XML_GetUserData(XML_Parser parser)477 XML_GetUserData(XML_Parser parser)
478 {
479 	return parser->user;
480 }
481 
482 PHP_XML_API void
XML_SetElementHandler(XML_Parser parser,XML_StartElementHandler start,XML_EndElementHandler end)483 XML_SetElementHandler(XML_Parser parser, XML_StartElementHandler start, XML_EndElementHandler end)
484 {
485 	parser->h_start_element = start;
486 	parser->h_end_element = end;
487 }
488 
489 PHP_XML_API void
XML_SetCharacterDataHandler(XML_Parser parser,XML_CharacterDataHandler cdata)490 XML_SetCharacterDataHandler(XML_Parser parser, XML_CharacterDataHandler cdata)
491 {
492 	parser->h_cdata = cdata;
493 }
494 
495 PHP_XML_API void
XML_SetProcessingInstructionHandler(XML_Parser parser,XML_ProcessingInstructionHandler pi)496 XML_SetProcessingInstructionHandler(XML_Parser parser, XML_ProcessingInstructionHandler pi)
497 {
498 	parser->h_pi = pi;
499 }
500 
501 PHP_XML_API void
XML_SetCommentHandler(XML_Parser parser,XML_CommentHandler comment)502 XML_SetCommentHandler(XML_Parser parser, XML_CommentHandler comment)
503 {
504 	parser->h_comment = comment;
505 }
506 
507 PHP_XML_API void
XML_SetDefaultHandler(XML_Parser parser,XML_DefaultHandler d)508 XML_SetDefaultHandler(XML_Parser parser, XML_DefaultHandler d)
509 {
510 	parser->h_default = d;
511 }
512 
513 PHP_XML_API void
XML_SetUnparsedEntityDeclHandler(XML_Parser parser,XML_UnparsedEntityDeclHandler unparsed_decl)514 XML_SetUnparsedEntityDeclHandler(XML_Parser parser, XML_UnparsedEntityDeclHandler unparsed_decl)
515 {
516 	parser->h_unparsed_entity_decl = unparsed_decl;
517 }
518 
519 PHP_XML_API void
XML_SetNotationDeclHandler(XML_Parser parser,XML_NotationDeclHandler notation_decl)520 XML_SetNotationDeclHandler(XML_Parser parser, XML_NotationDeclHandler notation_decl)
521 {
522 	parser->h_notation_decl = notation_decl;
523 }
524 
525 PHP_XML_API void
XML_SetExternalEntityRefHandler(XML_Parser parser,XML_ExternalEntityRefHandler ext_entity)526 XML_SetExternalEntityRefHandler(XML_Parser parser, XML_ExternalEntityRefHandler ext_entity)
527 {
528 	parser->h_external_entity_ref = ext_entity;
529 }
530 
531 PHP_XML_API void
XML_SetStartNamespaceDeclHandler(XML_Parser parser,XML_StartNamespaceDeclHandler start_ns)532 XML_SetStartNamespaceDeclHandler(XML_Parser parser, XML_StartNamespaceDeclHandler start_ns)
533 {
534 	parser->h_start_ns = start_ns;
535 }
536 
537 PHP_XML_API void
XML_SetEndNamespaceDeclHandler(XML_Parser parser,XML_EndNamespaceDeclHandler end_ns)538 XML_SetEndNamespaceDeclHandler(XML_Parser parser, XML_EndNamespaceDeclHandler end_ns)
539 {
540 	parser->h_end_ns = end_ns;
541 }
542 
543 PHP_XML_API int
XML_Parse(XML_Parser parser,const XML_Char * data,int data_len,int is_final)544 XML_Parse(XML_Parser parser, const XML_Char *data, int data_len, int is_final)
545 {
546 	int error = xmlParseChunk(parser->parser, (char *) data, data_len, is_final);
547 
548 	if (!error) {
549 		const xmlError *error_data = xmlCtxtGetLastError(parser->parser);
550 		return !error_data || error_data->level <= XML_ERR_WARNING;
551 	}
552 
553 	return 0;
554 }
555 
556 PHP_XML_API int
XML_GetErrorCode(XML_Parser parser)557 XML_GetErrorCode(XML_Parser parser)
558 {
559 	return parser->parser->errNo;
560 }
561 
562 static const XML_Char *const error_mapping[] = {
563 	(const XML_Char *)"No error",
564 	(const XML_Char *)"No memory",
565 	(const XML_Char *)"Invalid document start",
566 	(const XML_Char *)"Empty document",
567 	(const XML_Char *)"Not well-formed (invalid token)",
568 	(const XML_Char *)"Invalid document end",
569 	(const XML_Char *)"Invalid hexadecimal character reference",
570 	(const XML_Char *)"Invalid decimal character reference",
571 	(const XML_Char *)"Invalid character reference",
572 	(const XML_Char *)"Invalid character",
573 	(const XML_Char *)"XML_ERR_CHARREF_AT_EOF",
574 	(const XML_Char *)"XML_ERR_CHARREF_IN_PROLOG",
575 	(const XML_Char *)"XML_ERR_CHARREF_IN_EPILOG",
576 	(const XML_Char *)"XML_ERR_CHARREF_IN_DTD",
577 	(const XML_Char *)"XML_ERR_ENTITYREF_AT_EOF",
578 	(const XML_Char *)"XML_ERR_ENTITYREF_IN_PROLOG",
579 	(const XML_Char *)"XML_ERR_ENTITYREF_IN_EPILOG",
580 	(const XML_Char *)"XML_ERR_ENTITYREF_IN_DTD",
581 	(const XML_Char *)"PEReference at end of document",
582 	(const XML_Char *)"PEReference in prolog",
583 	(const XML_Char *)"PEReference in epilog",
584 	(const XML_Char *)"PEReference: forbidden within markup decl in internal subset",
585 	(const XML_Char *)"XML_ERR_ENTITYREF_NO_NAME",
586 	(const XML_Char *)"EntityRef: expecting ';'",
587 	(const XML_Char *)"PEReference: no name",
588 	(const XML_Char *)"PEReference: expecting ';'",
589 	(const XML_Char *)"Undeclared entity error",
590 	(const XML_Char *)"Undeclared entity warning",
591 	(const XML_Char *)"Unparsed Entity",
592 	(const XML_Char *)"XML_ERR_ENTITY_IS_EXTERNAL",
593 	(const XML_Char *)"XML_ERR_ENTITY_IS_PARAMETER",
594 	(const XML_Char *)"Unknown encoding",
595 	(const XML_Char *)"Unsupported encoding",
596 	(const XML_Char *)"String not started expecting ' or \"",
597 	(const XML_Char *)"String not closed expecting \" or '",
598 	(const XML_Char *)"Namespace declaration error",
599 	(const XML_Char *)"EntityValue: \" or ' expected",
600 	(const XML_Char *)"EntityValue: \" or ' expected",
601 	(const XML_Char *)"< in attribute",
602 	(const XML_Char *)"Attribute not started",
603 	(const XML_Char *)"Attribute not finished",
604 	(const XML_Char *)"Attribute without value",
605 	(const XML_Char *)"Attribute redefined",
606 	(const XML_Char *)"SystemLiteral \" or ' expected",
607 	(const XML_Char *)"SystemLiteral \" or ' expected",
608 	/* (const XML_Char *)"XML_ERR_COMMENT_NOT_STARTED", <= eliminated on purpose */
609 	(const XML_Char *)"Comment not finished",
610 	(const XML_Char *)"Processing Instruction not started",
611 	(const XML_Char *)"Processing Instruction not finished",
612 	(const XML_Char *)"NOTATION: Name expected here",
613 	(const XML_Char *)"'>' required to close NOTATION declaration",
614 	(const XML_Char *)"'(' required to start ATTLIST enumeration",
615 	(const XML_Char *)"'(' required to start ATTLIST enumeration",
616 	(const XML_Char *)"MixedContentDecl : '|' or ')*' expected",
617 	(const XML_Char *)"XML_ERR_MIXED_NOT_FINISHED",
618 	(const XML_Char *)"ELEMENT in DTD not started",
619 	(const XML_Char *)"ELEMENT in DTD not finished",
620 	(const XML_Char *)"XML declaration not started",
621 	(const XML_Char *)"XML declaration not finished",
622 	(const XML_Char *)"XML_ERR_CONDSEC_NOT_STARTED",
623 	(const XML_Char *)"XML conditional section not closed",
624 	(const XML_Char *)"Content error in the external subset",
625 	(const XML_Char *)"DOCTYPE not finished",
626 	(const XML_Char *)"Sequence ']]>' not allowed in content",
627 	(const XML_Char *)"CDATA not finished",
628 	(const XML_Char *)"Reserved XML Name",
629 	(const XML_Char *)"Space required",
630 	(const XML_Char *)"XML_ERR_SEPARATOR_REQUIRED",
631 	(const XML_Char *)"NmToken expected in ATTLIST enumeration",
632 	(const XML_Char *)"XML_ERR_NAME_REQUIRED",
633 	(const XML_Char *)"MixedContentDecl : '#PCDATA' expected",
634 	(const XML_Char *)"SYSTEM or PUBLIC, the URI is missing",
635 	(const XML_Char *)"PUBLIC, the Public Identifier is missing",
636 	(const XML_Char *)"< required",
637 	(const XML_Char *)"> required",
638 	(const XML_Char *)"</ required",
639 	(const XML_Char *)"= required",
640 	(const XML_Char *)"Mismatched tag",
641 	(const XML_Char *)"Tag not finished",
642 	(const XML_Char *)"standalone accepts only 'yes' or 'no'",
643 	(const XML_Char *)"Invalid XML encoding name",
644 	(const XML_Char *)"Comment must not contain '--' (double-hyphen)",
645 	(const XML_Char *)"Invalid encoding",
646 	(const XML_Char *)"external parsed entities cannot be standalone",
647 	(const XML_Char *)"XML conditional section '[' expected",
648 	(const XML_Char *)"Entity value required",
649 	(const XML_Char *)"chunk is not well balanced",
650 	(const XML_Char *)"extra content at the end of well balanced chunk",
651 	(const XML_Char *)"XML_ERR_ENTITY_CHAR_ERROR",
652 	(const XML_Char *)"PEReferences forbidden in internal subset",
653 	(const XML_Char *)"Detected an entity reference loop",
654 	(const XML_Char *)"XML_ERR_ENTITY_BOUNDARY",
655 	(const XML_Char *)"Invalid URI",
656 	(const XML_Char *)"Fragment not allowed",
657 	(const XML_Char *)"XML_WAR_CATALOG_PI",
658 	(const XML_Char *)"XML_ERR_NO_DTD",
659 	(const XML_Char *)"conditional section INCLUDE or IGNORE keyword expected", /* 95 */
660 	(const XML_Char *)"Version in XML Declaration missing", /* 96 */
661 	(const XML_Char *)"XML_WAR_UNKNOWN_VERSION", /* 97 */
662 	(const XML_Char *)"XML_WAR_LANG_VALUE", /* 98 */
663 	(const XML_Char *)"XML_WAR_NS_URI", /* 99 */
664 	(const XML_Char *)"XML_WAR_NS_URI_RELATIVE", /* 100 */
665 	(const XML_Char *)"Missing encoding in text declaration" /* 101 */
666 };
667 
668 PHP_XML_API const XML_Char *
XML_ErrorString(int code)669 XML_ErrorString(int code)
670 {
671 	if (code < 0 || code >= (int)(sizeof(error_mapping) / sizeof(error_mapping[0]))) {
672 		return (const XML_Char *) "Unknown";
673 	}
674 	return error_mapping[code];
675 }
676 
677 PHP_XML_API int
XML_GetCurrentLineNumber(XML_Parser parser)678 XML_GetCurrentLineNumber(XML_Parser parser)
679 {
680 	return parser->parser->input->line;
681 }
682 
683 PHP_XML_API int
XML_GetCurrentColumnNumber(XML_Parser parser)684 XML_GetCurrentColumnNumber(XML_Parser parser)
685 {
686 	return parser->parser->input->col;
687 }
688 
689 PHP_XML_API long
XML_GetCurrentByteIndex(XML_Parser parser)690 XML_GetCurrentByteIndex(XML_Parser parser)
691 {
692 	/* We have to temporarily disable the encoder to satisfy the note from the manual:
693 	 * "This function returns byte index according to UTF-8 encoded text disregarding if input is in another encoding."
694 	 * Although that should probably be corrected at one point? (TODO) */
695 	xmlCharEncodingHandlerPtr encoder = NULL;
696 	xmlParserInputPtr input = parser->parser->input;
697 	if (input->buf) {
698 		encoder = input->buf->encoder;
699 		input->buf->encoder = NULL;
700 	}
701 	long result = xmlByteConsumed(parser->parser);
702 	if (encoder) {
703 		input->buf->encoder = encoder;
704 	}
705 	return result;
706 }
707 
708 PHP_XML_API int
XML_GetCurrentByteCount(XML_Parser parser)709 XML_GetCurrentByteCount(XML_Parser parser)
710 {
711 	/* TODO: this is identical to ByteIndex; it should probably
712 	 * be different */
713 	return (int) XML_GetCurrentByteIndex(parser);
714 }
715 
XML_ExpatVersion(void)716 PHP_XML_API const XML_Char *XML_ExpatVersion(void)
717 {
718 	return (const XML_Char *) "1.0";
719 }
720 
721 PHP_XML_API void
XML_ParserFree(XML_Parser parser)722 XML_ParserFree(XML_Parser parser)
723 {
724 	if (parser->use_namespace) {
725 		if (parser->_ns_separator) {
726 			xmlFree(parser->_ns_separator);
727 		}
728 	}
729 	if (parser->parser->myDoc) {
730 		xmlFreeDoc(parser->parser->myDoc);
731 		parser->parser->myDoc = NULL;
732 	}
733 	xmlFreeParserCtxt(parser->parser);
734 	efree(parser);
735 }
736 
737 #endif /* LIBXML_EXPAT_COMPAT */
738 #endif
739