Skip to content

Commit 4cb91b8

Browse files
committed
Move common encoding validity checking function to ext-libxml
1 parent 969d2c3 commit 4cb91b8

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

ext/libxml/libxml.c

+16
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,22 @@ PHP_LIBXML_API bool php_libxml_uses_internal_errors(void)
10751075
return xmlStructuredError == php_libxml_structured_error_handler;
10761076
}
10771077

1078+
PHP_LIBXML_API bool php_libxml_is_valid_encoding(const char *encoding)
1079+
{
1080+
if (!encoding) {
1081+
return true;
1082+
}
1083+
1084+
/* Normally we could use xmlTextReaderConstEncoding() afterwards but libxml2 < 2.12.0 has a bug of course
1085+
* where it returns NULL for some valid encodings instead. */
1086+
xmlCharEncodingHandlerPtr handler = xmlFindCharEncodingHandler(encoding);
1087+
if (!handler) {
1088+
return false;
1089+
}
1090+
xmlCharEncCloseFunc(handler);
1091+
return true;
1092+
}
1093+
10781094
/* {{{ Disable libxml errors and allow user to fetch error information as needed */
10791095
PHP_FUNCTION(libxml_use_internal_errors)
10801096
{

ext/libxml/php_libxml.h

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ PHP_LIBXML_API bool php_libxml_disable_entity_loader(bool disable);
214214
PHP_LIBXML_API void php_libxml_set_old_ns(xmlDocPtr doc, xmlNsPtr ns);
215215
PHP_LIBXML_API php_stream_context *php_libxml_get_stream_context(void);
216216
PHP_LIBXML_API bool php_libxml_uses_internal_errors(void);
217+
PHP_LIBXML_API bool php_libxml_is_valid_encoding(const char *encoding);
217218

218219
PHP_LIBXML_API xmlChar *php_libxml_attr_value(const xmlAttr *attr, bool *free);
219220

ext/xmlreader/php_xmlreader.c

+3-19
Original file line numberDiff line numberDiff line change
@@ -881,22 +881,6 @@ PHP_METHOD(XMLReader, next)
881881
}
882882
/* }}} */
883883

884-
static bool xmlreader_valid_encoding(const char *encoding)
885-
{
886-
if (!encoding) {
887-
return true;
888-
}
889-
890-
/* Normally we could use xmlTextReaderConstEncoding() afterwards but libxml2 < 2.12.0 has a bug of course
891-
* where it returns NULL for some valid encodings instead. */
892-
xmlCharEncodingHandlerPtr handler = xmlFindCharEncodingHandler(encoding);
893-
if (!handler) {
894-
return false;
895-
}
896-
xmlCharEncCloseFunc(handler);
897-
return true;
898-
}
899-
900884
/* {{{ Sets the URI that the XMLReader will parse. */
901885
static void xml_reader_from_uri(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *instance_ce, bool use_exceptions)
902886
{
@@ -925,7 +909,7 @@ static void xml_reader_from_uri(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *
925909
RETURN_THROWS();
926910
}
927911

928-
if (!xmlreader_valid_encoding(encoding)) {
912+
if (!php_libxml_is_valid_encoding(encoding)) {
929913
zend_argument_value_error(2, "must be a valid character encoding");
930914
RETURN_THROWS();
931915
}
@@ -1013,7 +997,7 @@ PHP_METHOD(XMLReader, fromStream)
1013997

1014998
php_stream_from_res(stream, Z_RES_P(stream_zv));
1015999

1016-
if (!xmlreader_valid_encoding(encoding_name)) {
1000+
if (!php_libxml_is_valid_encoding(encoding_name)) {
10171001
zend_argument_value_error(2, "must be a valid character encoding");
10181002
RETURN_THROWS();
10191003
}
@@ -1197,7 +1181,7 @@ static void xml_reader_from_string(INTERNAL_FUNCTION_PARAMETERS, zend_class_entr
11971181
RETURN_THROWS();
11981182
}
11991183

1200-
if (!xmlreader_valid_encoding(encoding)) {
1184+
if (!php_libxml_is_valid_encoding(encoding)) {
12011185
zend_argument_value_error(2, "must be a valid character encoding");
12021186
RETURN_THROWS();
12031187
}

0 commit comments

Comments
 (0)