/*
   +----------------------------------------------------------------------+
   | Copyright © The PHP Group and Contributors.                          |
   +----------------------------------------------------------------------+
   | This source file is subject to the Modified BSD License that is      |
   | bundled with this package in the file LICENSE, and is available      |
   | through the World Wide Web at <https://www.php.net/license/>.        |
   |                                                                      |
   | SPDX-License-Identifier: BSD-3-Clause                                |
   +----------------------------------------------------------------------+
   | Authors: Sterling Hughes <sterling@php.net>                          |
   +----------------------------------------------------------------------+
 */

#include "php.h"
#if defined(HAVE_LIBXML) && (defined(HAVE_XML) || defined(HAVE_XMLRPC)) && !defined(HAVE_LIBEXPAT)
#include "expat_compat.h"
#include "ext/libxml/php_libxml.h"
#include "Zend/zend_smart_string.h"

#ifdef LIBXML_EXPAT_COMPAT

static xmlChar *
qualify_namespace(XML_Parser parser, const xmlChar *name, const xmlChar *URI)
{
	if (URI) {
		smart_string str = {0};
		smart_string_appends(&str, (const char *) URI);
		smart_string_appends(&str, (const char *) parser->_ns_separator);
		smart_string_appends(&str, (const char *) name);
		smart_string_0(&str);
		return BAD_CAST str.c;
	} else {
		return BAD_CAST estrdup((const char *) name);
	}
}

static void start_element_emit_default(XML_Parser parser)
{
	if (parser->h_default) {
		/* Grammar does not allow embedded '<' and '>' in elements, so we can seek to the start and end positions.
		 * Since the parser in the current mode mode is non-progressive, it contains the entire input. */
		const xmlChar *cur = parser->parser->input->cur;
		const xmlChar *end = cur;
		for (const xmlChar *base = parser->parser->input->base; cur > base && *cur != '<'; cur--);
		if (*end == '/') {
			/* BC:   Keep split between start & end element.
			 * TODO: In the future this could be aligned with expat and only emit a start event, or vice versa.
			 *       See gh20439_2.phpt */
			xmlChar *tmp = BAD_CAST estrndup((const char *) cur, end - cur + 1);
			tmp[end - cur] = '>';
			parser->h_default(parser->user, tmp, end - cur + 1);
			efree(tmp);
		} else {
			parser->h_default(parser->user, cur, end - cur + 1);
		}
	}
}

static void
start_element_handler(void *user, const xmlChar *name, const xmlChar **attributes)
{
	XML_Parser  parser = (XML_Parser) user;

	if (parser->h_start_element == NULL) {
		start_element_emit_default(parser);
		return;
	}

	parser->h_start_element(parser->user, name, (const XML_Char **) attributes);
}

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)
{
	XML_Parser  parser = (XML_Parser) user;
	xmlChar    *qualified_name = NULL;
	xmlChar **attrs = NULL;
	int i;
	int z = 0;
	int y = 0;

	if (nb_namespaces > 0 && parser->h_start_ns != NULL) {
		for (i = 0; i < nb_namespaces; i += 1) {
			parser->h_start_ns(parser->user, (const XML_Char *) namespaces[y], (const XML_Char *) namespaces[y+1]);
			y += 2;
		}
		y = 0;
	}

	if (parser->h_start_element == NULL) {
		start_element_emit_default(parser);
		return;
	}
	qualified_name = qualify_namespace(parser, name, URI);

	if (attributes != NULL) {
		xmlChar    *qualified_name_attr = NULL;
		attrs = safe_emalloc(nb_attributes, 2 * sizeof(int *), sizeof(int *));

		for (i = 0; i < nb_attributes; i += 1) {

			if (attributes[y+1] != NULL) {
				qualified_name_attr = qualify_namespace(parser, attributes[y] , attributes[y + 2]);
			} else {
				qualified_name_attr = BAD_CAST estrdup((const char *) attributes[y]);
			}
			attrs[z] = qualified_name_attr;
			attrs[z + 1] = BAD_CAST estrndup((const char *) attributes[y + 3], attributes[y + 4] - attributes[y + 3]);
			z += 2;
			y += 5;
		}

		attrs[z] = NULL;
	}
	parser->h_start_element(parser->user, (const XML_Char *) qualified_name, (const XML_Char **) attrs);
	if (attrs) {
		for (i = 0; i < z; i++) {
			efree(attrs[i]);
		}
		efree(attrs);
	}
	efree(qualified_name);
}

static void
end_element_handler(void *user, const xmlChar *name)
{
	XML_Parser  parser = (XML_Parser) user;

	if (parser->h_end_element == NULL) {
		if (parser->h_default) {
			char *end_element;

			spprintf(&end_element, 0, "</%s>", (char *)name);
			parser->h_default(parser->user, (const XML_Char *) end_element, strlen(end_element));
			efree(end_element);
		}
		return;
	}

	parser->h_end_element(parser->user, (const XML_Char *) name);
}

static void
end_element_handler_ns(void *user, const xmlChar *name, const xmlChar * prefix, const xmlChar *URI)
{
	xmlChar    *qualified_name;
	XML_Parser  parser = (XML_Parser) user;

	if (parser->h_end_element == NULL) {
		if (parser->h_default) {
			char *end_element;
			int end_element_len;

			if (prefix) {
				end_element_len = spprintf(&end_element, 0, "</%s:%s>", (char *) prefix, (char *)name);
			} else {
				end_element_len = spprintf(&end_element, 0, "</%s>", (char *)name);
			}
			parser->h_default(parser->user, (const XML_Char *) end_element, end_element_len);
			efree(end_element);
		}
		return;
	}

	qualified_name = qualify_namespace(parser, name, URI);

	parser->h_end_element(parser->user, (const XML_Char *) qualified_name);

	efree(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) {
		if (parser->h_default) {
			parser->h_default(parser->user, (const XML_Char *) cdata, cdata_len);
		}
		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) {
		if (parser->h_default) {
			char    *full_pi;
			spprintf(&full_pi, 0, "<?%s %s?>", (char *)target, (char *)data);
			parser->h_default(parser->user, (const XML_Char *) full_pi, strlen(full_pi));
			efree(full_pi);
		}
		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 *pub_id,
                              const xmlChar *sys_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 *pub_id, const xmlChar *sys_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 xmlChar *
build_comment(const xmlChar *data, size_t data_len, size_t *comment_len)
{
	*comment_len = data_len + 7;

	xmlChar *comment = emalloc(*comment_len + 1);
	memcpy(comment, "<!--", 4);
	memcpy(comment + 4, data, data_len);
	memcpy(comment + 4 + data_len, "-->", 3);

	comment[*comment_len] = '\0';

	return comment;
}

static void
comment_handler(void *user, const xmlChar *comment)
{
	XML_Parser parser = (XML_Parser) user;

	if (parser->h_default) {
		size_t   d_comment_len;

		xmlChar *d_comment = build_comment(comment, (size_t) xmlStrlen(comment), &d_comment_len);
		parser->h_default(parser->user, d_comment, d_comment_len);
		efree(d_comment);
	}
}

static xmlChar *
build_entity(const xmlChar *name, size_t len, size_t *entity_len)
{
	*entity_len = len + 2;
	xmlChar *entity = emalloc(*entity_len + 1);
	entity[0] = '&';
	memcpy(entity + 1, name, len);
	entity[len + 1] = ';';
	entity[*entity_len] = '\0';
	return entity;
}

static void
external_entity_ref_handler(void *user, const xmlChar *names, const xmlChar *sys_id, const xmlChar *pub_id)
{
	XML_Parser parser = (XML_Parser) user;

	if (parser->h_external_entity_ref == NULL) {
		return;
	}

	if (!parser->h_external_entity_ref(parser, names, (XML_Char *) "", sys_id, pub_id)) {
		xmlStopParser(parser->parser);
		parser->parser->errNo = XML_ERROR_EXTERNAL_ENTITY_HANDLING;
	};
}

static xmlEntityPtr
get_entity(void *user, const xmlChar *name)
{
	XML_Parser parser = (XML_Parser) user;
	xmlEntityPtr ret = NULL;

	if (parser->parser->inSubset == 0) {
		ret = xmlGetPredefinedEntity(name);
		if (ret == NULL)
			ret = xmlGetDocEntity(parser->parser->myDoc, name);

		ZEND_DIAGNOSTIC_IGNORED_START("-Wdeprecated-declarations")
		if (ret == NULL || parser->parser->instate == XML_PARSER_CONTENT) {
		ZEND_DIAGNOSTIC_IGNORED_END
			if (ret == NULL || ret->etype == XML_INTERNAL_GENERAL_ENTITY || ret->etype == XML_INTERNAL_PARAMETER_ENTITY || ret->etype == XML_INTERNAL_PREDEFINED_ENTITY) {
				/* Predefined entities will expand unless no cdata handler is present */
				if (parser->h_default && ! (ret && ret->etype == XML_INTERNAL_PREDEFINED_ENTITY && parser->h_cdata)) {
					size_t   len;

					xmlChar *entity = build_entity(name, (size_t) xmlStrlen(name), &len);
					parser->h_default(parser->user, (const xmlChar *) entity, len);
					efree(entity);
				} else {
					/* expat will not expand internal entities if default handler is present otherwise
					it will expand and pass them to cdata handler */
					if (parser->h_cdata && ret) {
						parser->h_cdata(parser->user, ret->content, xmlStrlen(ret->content));
					}
				}
			} else {
				if (ret->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) {
					external_entity_ref_handler(user, ret->name, ret->SystemID, ret->ExternalID);
				}
			}
		}
	}

	return ret;
}

static const xmlSAXHandler
php_xml_compat_handlers = {
	NULL, /* internalSubset */
	NULL, /* isStandalone */
	NULL, /* hasInternalSubset */
	NULL, /* hasExternalSubset */
	NULL, /* resolveEntity */
	get_entity, /* getEntity */
	NULL, /* entityDecl */
	notation_decl_handler,
	NULL, /* attributeDecl */
	NULL, /* elementDecl */
	unparsed_entity_decl_handler, /* unparsedEntity */
	NULL, /* setDocumentLocator */
	NULL, /* startDocument */
	NULL, /* endDocument */
	start_element_handler, /* startElement */
	end_element_handler, /* endElement */
	NULL, /* reference */
	cdata_handler,
	NULL, /* ignorableWhitespace */
	pi_handler,
	comment_handler, /* comment */
	NULL, /* warning */
	NULL, /* error */
	NULL,  /* fatalError */
	NULL,  /* getParameterEntity */
	cdata_handler, /* cdataBlock */
	NULL, /* externalSubset */
	XML_SAX2_MAGIC,
	NULL,
	start_element_handler_ns,
	end_element_handler_ns,
	NULL
};

PHP_XML_API XML_Parser
XML_ParserCreate(const XML_Char *encoding)
{
	return XML_ParserCreate_MM(encoding, NULL, NULL);
}

PHP_XML_API 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);
}

PHP_XML_API XML_Parser
XML_ParserCreate_MM(const XML_Char *encoding, const XML_Memory_Handling_Suite *memsuite, const XML_Char *sep)
{
	XML_Parser parser;

	parser = emalloc(sizeof(struct XML_Parser_Struct));
	memset(parser, 0, sizeof(struct XML_Parser_Struct));
	parser->use_namespace = 0;
	parser->_ns_separator = NULL;

	parser->parser = xmlCreatePushParserCtxt((xmlSAXHandlerPtr) &php_xml_compat_handlers, (void *) parser, NULL, 0, NULL);
	if (parser->parser == NULL) {
		efree(parser);
		return NULL;
	}

#if LIBXML_VERSION >= 21300
	xmlCtxtSetOptions(parser->parser, XML_PARSE_OLDSAX | XML_PARSE_NOENT);
#else
	php_libxml_sanitize_parse_ctxt_options(parser->parser);
	xmlCtxtUseOptions(parser->parser, XML_PARSE_OLDSAX | XML_PARSE_NOENT);
#endif

	parser->parser->wellFormed = 0;
	if (sep != NULL) {
		/* Note: sax2 flag will be set due to the magic number in `initialized` in php_xml_compat_handlers */
		ZEND_ASSERT(parser->parser->sax->initialized == XML_SAX2_MAGIC);
		parser->use_namespace = 1;
		parser->_ns_separator = BAD_CAST estrdup((const char *) sep);
	} else {
		/* Reset flag as XML_SAX2_MAGIC is needed for xmlCreatePushParserCtxt
		so must be set in the handlers */
		parser->parser->sax->initialized = 1;
	}
	return parser;
}

PHP_XML_API void
XML_SetUserData(XML_Parser parser, void *user)
{
	parser->user = user;
}

PHP_XML_API void *
XML_GetUserData(XML_Parser parser)
{
	return parser->user;
}

PHP_XML_API void
XML_SetElementHandler(XML_Parser parser, XML_StartElementHandler start, XML_EndElementHandler end)
{
	parser->h_start_element = start;
	parser->h_end_element = end;
}

PHP_XML_API void
XML_SetCharacterDataHandler(XML_Parser parser, XML_CharacterDataHandler cdata)
{
	parser->h_cdata = cdata;
}

PHP_XML_API void
XML_SetProcessingInstructionHandler(XML_Parser parser, XML_ProcessingInstructionHandler pi)
{
	parser->h_pi = pi;
}

PHP_XML_API void
XML_SetCommentHandler(XML_Parser parser, XML_CommentHandler comment)
{
	parser->h_comment = comment;
}

PHP_XML_API void
XML_SetDefaultHandler(XML_Parser parser, XML_DefaultHandler d)
{
	parser->h_default = d;
}

PHP_XML_API void
XML_SetUnparsedEntityDeclHandler(XML_Parser parser, XML_UnparsedEntityDeclHandler unparsed_decl)
{
	parser->h_unparsed_entity_decl = unparsed_decl;
}

PHP_XML_API void
XML_SetNotationDeclHandler(XML_Parser parser, XML_NotationDeclHandler notation_decl)
{
	parser->h_notation_decl = notation_decl;
}

PHP_XML_API void
XML_SetExternalEntityRefHandler(XML_Parser parser, XML_ExternalEntityRefHandler ext_entity)
{
	parser->h_external_entity_ref = ext_entity;
}

PHP_XML_API void
XML_SetStartNamespaceDeclHandler(XML_Parser parser, XML_StartNamespaceDeclHandler start_ns)
{
	parser->h_start_ns = start_ns;
}

PHP_XML_API void
XML_SetEndNamespaceDeclHandler(XML_Parser parser, XML_EndNamespaceDeclHandler end_ns)
{
	parser->h_end_ns = end_ns;
}

PHP_XML_API int
XML_Parse(XML_Parser parser, const XML_Char *data, int data_len, int is_final)
{
	int error = xmlParseChunk(parser->parser, (char *) data, data_len, is_final);

	if (!error) {
		const xmlError *error_data = xmlCtxtGetLastError(parser->parser);
		return !error_data || error_data->level <= XML_ERR_WARNING;
	}

	return 0;
}

PHP_XML_API int
XML_GetErrorCode(XML_Parser parser)
{
	return parser->parser->errNo;
}

static const XML_Char *const error_mapping[] = {
	(const XML_Char *)"No error",
	(const XML_Char *)"No memory",
	(const XML_Char *)"Invalid document start",
	(const XML_Char *)"Empty document",
	(const XML_Char *)"Not well-formed (invalid token)",
	(const XML_Char *)"Invalid document end",
	(const XML_Char *)"Invalid hexadecimal character reference",
	(const XML_Char *)"Invalid decimal character reference",
	(const XML_Char *)"Invalid character reference",
	(const XML_Char *)"Invalid character",
	(const XML_Char *)"XML_ERR_CHARREF_AT_EOF",
	(const XML_Char *)"XML_ERR_CHARREF_IN_PROLOG",
	(const XML_Char *)"XML_ERR_CHARREF_IN_EPILOG",
	(const XML_Char *)"XML_ERR_CHARREF_IN_DTD",
	(const XML_Char *)"XML_ERR_ENTITYREF_AT_EOF",
	(const XML_Char *)"XML_ERR_ENTITYREF_IN_PROLOG",
	(const XML_Char *)"XML_ERR_ENTITYREF_IN_EPILOG",
	(const XML_Char *)"XML_ERR_ENTITYREF_IN_DTD",
	(const XML_Char *)"PEReference at end of document",
	(const XML_Char *)"PEReference in prolog",
	(const XML_Char *)"PEReference in epilog",
	(const XML_Char *)"PEReference: forbidden within markup decl in internal subset",
	(const XML_Char *)"XML_ERR_ENTITYREF_NO_NAME",
	(const XML_Char *)"EntityRef: expecting ';'",
	(const XML_Char *)"PEReference: no name",
	(const XML_Char *)"PEReference: expecting ';'",
	(const XML_Char *)"Undeclared entity error",
	(const XML_Char *)"Undeclared entity warning",
	(const XML_Char *)"Unparsed Entity",
	(const XML_Char *)"XML_ERR_ENTITY_IS_EXTERNAL",
	(const XML_Char *)"XML_ERR_ENTITY_IS_PARAMETER",
	(const XML_Char *)"Unknown encoding",
	(const XML_Char *)"Unsupported encoding",
	(const XML_Char *)"String not started expecting ' or \"",
	(const XML_Char *)"String not closed expecting \" or '",
	(const XML_Char *)"Namespace declaration error",
	(const XML_Char *)"EntityValue: \" or ' expected",
	(const XML_Char *)"EntityValue: \" or ' expected",
	(const XML_Char *)"< in attribute",
	(const XML_Char *)"Attribute not started",
	(const XML_Char *)"Attribute not finished",
	(const XML_Char *)"Attribute without value",
	(const XML_Char *)"Attribute redefined",
	(const XML_Char *)"SystemLiteral \" or ' expected",
	(const XML_Char *)"SystemLiteral \" or ' expected",
	/* (const XML_Char *)"XML_ERR_COMMENT_NOT_STARTED", <= eliminated on purpose */
	(const XML_Char *)"Comment not finished",
	(const XML_Char *)"Processing Instruction not started",
	(const XML_Char *)"Processing Instruction not finished",
	(const XML_Char *)"NOTATION: Name expected here",
	(const XML_Char *)"'>' required to close NOTATION declaration",
	(const XML_Char *)"'(' required to start ATTLIST enumeration",
	(const XML_Char *)"'(' required to start ATTLIST enumeration",
	(const XML_Char *)"MixedContentDecl : '|' or ')*' expected",
	(const XML_Char *)"XML_ERR_MIXED_NOT_FINISHED",
	(const XML_Char *)"ELEMENT in DTD not started",
	(const XML_Char *)"ELEMENT in DTD not finished",
	(const XML_Char *)"XML declaration not started",
	(const XML_Char *)"XML declaration not finished",
	(const XML_Char *)"XML_ERR_CONDSEC_NOT_STARTED",
	(const XML_Char *)"XML conditional section not closed",
	(const XML_Char *)"Content error in the external subset",
	(const XML_Char *)"DOCTYPE not finished",
	(const XML_Char *)"Sequence ']]>' not allowed in content",
	(const XML_Char *)"CDATA not finished",
	(const XML_Char *)"Reserved XML Name",
	(const XML_Char *)"Space required",
	(const XML_Char *)"XML_ERR_SEPARATOR_REQUIRED",
	(const XML_Char *)"NmToken expected in ATTLIST enumeration",
	(const XML_Char *)"XML_ERR_NAME_REQUIRED",
	(const XML_Char *)"MixedContentDecl : '#PCDATA' expected",
	(const XML_Char *)"SYSTEM or PUBLIC, the URI is missing",
	(const XML_Char *)"PUBLIC, the Public Identifier is missing",
	(const XML_Char *)"< required",
	(const XML_Char *)"> required",
	(const XML_Char *)"</ required",
	(const XML_Char *)"= required",
	(const XML_Char *)"Mismatched tag",
	(const XML_Char *)"Tag not finished",
	(const XML_Char *)"standalone accepts only 'yes' or 'no'",
	(const XML_Char *)"Invalid XML encoding name",
	(const XML_Char *)"Comment must not contain '--' (double-hyphen)",
	(const XML_Char *)"Invalid encoding",
	(const XML_Char *)"external parsed entities cannot be standalone",
	(const XML_Char *)"XML conditional section '[' expected",
	(const XML_Char *)"Entity value required",
	(const XML_Char *)"chunk is not well balanced",
	(const XML_Char *)"extra content at the end of well balanced chunk",
	(const XML_Char *)"XML_ERR_ENTITY_CHAR_ERROR",
	(const XML_Char *)"PEReferences forbidden in internal subset",
	(const XML_Char *)"Detected an entity reference loop",
	(const XML_Char *)"XML_ERR_ENTITY_BOUNDARY",
	(const XML_Char *)"Invalid URI",
	(const XML_Char *)"Fragment not allowed",
	(const XML_Char *)"XML_WAR_CATALOG_PI",
	(const XML_Char *)"XML_ERR_NO_DTD",
	(const XML_Char *)"conditional section INCLUDE or IGNORE keyword expected", /* 95 */
	(const XML_Char *)"Version in XML Declaration missing", /* 96 */
	(const XML_Char *)"XML_WAR_UNKNOWN_VERSION", /* 97 */
	(const XML_Char *)"XML_WAR_LANG_VALUE", /* 98 */
	(const XML_Char *)"XML_WAR_NS_URI", /* 99 */
	(const XML_Char *)"XML_WAR_NS_URI_RELATIVE", /* 100 */
	(const XML_Char *)"Missing encoding in text declaration" /* 101 */
};

PHP_XML_API const XML_Char *
XML_ErrorString(int code)
{
	if (code < 0 || code >= (int)(sizeof(error_mapping) / sizeof(error_mapping[0]))) {
		return (const XML_Char *) "Unknown";
	}
	return error_mapping[code];
}

PHP_XML_API int
XML_GetCurrentLineNumber(XML_Parser parser)
{
	return parser->parser->input->line;
}

PHP_XML_API int
XML_GetCurrentColumnNumber(XML_Parser parser)
{
	return parser->parser->input->col;
}

PHP_XML_API long
XML_GetCurrentByteIndex(XML_Parser parser)
{
	return parser->parser->input->consumed +
			(parser->parser->input->cur - parser->parser->input->base);
}

PHP_XML_API void
XML_ParserFree(XML_Parser parser)
{
	if (parser->use_namespace) {
		if (parser->_ns_separator) {
			efree(parser->_ns_separator);
		}
	}
	if (parser->parser->myDoc) {
		xmlFreeDoc(parser->parser->myDoc);
		parser->parser->myDoc = NULL;
	}
	xmlFreeParserCtxt(parser->parser);
	efree(parser);
}

#endif /* LIBXML_EXPAT_COMPAT */
#endif
