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