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