summaryrefslogtreecommitdiff
path: root/ext/simplexml
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2019-08-15 11:18:56 +0200
committerNikita Popov <nikita.ppv@gmail.com>2019-08-15 11:18:56 +0200
commit743d27631fa600f071d4b3b1cf061708a9866f5b (patch)
tree4a36e992bd46627d2152654ec8571a2aa06c1b88 /ext/simplexml
parentdeceafbe63da2ca4639c4f0f4c72ec6bfd89211a (diff)
downloadphp-git-743d27631fa600f071d4b3b1cf061708a9866f5b.tar.gz
Normalize SimpleXML::asXML() argument handling
Remove odd manual checks in favor of a standard zpp call.
Diffstat (limited to 'ext/simplexml')
-rw-r--r--ext/simplexml/simplexml.c108
1 files changed, 48 insertions, 60 deletions
diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c
index 8279770b7f..58d2cca910 100644
--- a/ext/simplexml/simplexml.c
+++ b/ext/simplexml/simplexml.c
@@ -1429,88 +1429,76 @@ SXE_METHOD(asXML)
xmlOutputBufferPtr outbuf;
xmlChar *strval;
int strval_len;
- char *filename;
+ char *filename = NULL;
size_t filename_len;
- if (ZEND_NUM_ARGS() > 1) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "|p", &filename, &filename_len) == FAILURE) {
RETURN_FALSE;
}
- if (ZEND_NUM_ARGS() == 1) {
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
- RETURN_FALSE;
- }
-
- sxe = Z_SXEOBJ_P(ZEND_THIS);
- GET_NODE(sxe, node);
- node = php_sxe_get_first_node(sxe, node);
-
- if (node) {
- if (node->parent && (XML_DOCUMENT_NODE == node->parent->type)) {
- int bytes;
- bytes = xmlSaveFile(filename, (xmlDocPtr) sxe->document->ptr);
- if (bytes == -1) {
- RETURN_FALSE;
- } else {
- RETURN_TRUE;
- }
- } else {
- outbuf = xmlOutputBufferCreateFilename(filename, NULL, 0);
-
- if (outbuf == NULL) {
- RETURN_FALSE;
- }
-
- xmlNodeDumpOutput(outbuf, (xmlDocPtr) sxe->document->ptr, node, 0, 0, NULL);
- xmlOutputBufferClose(outbuf);
- RETURN_TRUE;
- }
- } else {
- RETURN_FALSE;
- }
- }
-
sxe = Z_SXEOBJ_P(ZEND_THIS);
GET_NODE(sxe, node);
node = php_sxe_get_first_node(sxe, node);
- if (node) {
+ if (!node) {
+ RETURN_FALSE;
+ }
+
+ if (filename) {
if (node->parent && (XML_DOCUMENT_NODE == node->parent->type)) {
- xmlDocDumpMemoryEnc((xmlDocPtr) sxe->document->ptr, &strval, &strval_len, (const char *) ((xmlDocPtr) sxe->document->ptr)->encoding);
- if (!strval) {
- RETVAL_FALSE;
+ int bytes;
+ bytes = xmlSaveFile(filename, (xmlDocPtr) sxe->document->ptr);
+ if (bytes == -1) {
+ RETURN_FALSE;
} else {
- RETVAL_STRINGL((char *)strval, strval_len);
+ RETURN_TRUE;
}
- xmlFree(strval);
} else {
- char *return_content;
- size_t return_len;
- /* Should we be passing encoding information instead of NULL? */
- outbuf = xmlAllocOutputBuffer(NULL);
+ outbuf = xmlOutputBufferCreateFilename(filename, NULL, 0);
if (outbuf == NULL) {
RETURN_FALSE;
}
- xmlNodeDumpOutput(outbuf, (xmlDocPtr) sxe->document->ptr, node, 0, 0, (const char *) ((xmlDocPtr) sxe->document->ptr)->encoding);
- xmlOutputBufferFlush(outbuf);
+ xmlNodeDumpOutput(outbuf, (xmlDocPtr) sxe->document->ptr, node, 0, 0, NULL);
+ xmlOutputBufferClose(outbuf);
+ RETURN_TRUE;
+ }
+ }
+
+ if (node->parent && (XML_DOCUMENT_NODE == node->parent->type)) {
+ xmlDocDumpMemoryEnc((xmlDocPtr) sxe->document->ptr, &strval, &strval_len, (const char *) ((xmlDocPtr) sxe->document->ptr)->encoding);
+ if (!strval) {
+ RETVAL_FALSE;
+ } else {
+ RETVAL_STRINGL((char *)strval, strval_len);
+ }
+ xmlFree(strval);
+ } else {
+ char *return_content;
+ size_t return_len;
+ /* Should we be passing encoding information instead of NULL? */
+ outbuf = xmlAllocOutputBuffer(NULL);
+
+ if (outbuf == NULL) {
+ RETURN_FALSE;
+ }
+
+ xmlNodeDumpOutput(outbuf, (xmlDocPtr) sxe->document->ptr, node, 0, 0, (const char *) ((xmlDocPtr) sxe->document->ptr)->encoding);
+ xmlOutputBufferFlush(outbuf);
#ifdef LIBXML2_NEW_BUFFER
- return_content = (char *)xmlOutputBufferGetContent(outbuf);
- return_len = xmlOutputBufferGetSize(outbuf);
+ return_content = (char *)xmlOutputBufferGetContent(outbuf);
+ return_len = xmlOutputBufferGetSize(outbuf);
#else
- return_content = (char *)outbuf->buffer->content;
- return_len = outbuf->buffer->use;
+ return_content = (char *)outbuf->buffer->content;
+ return_len = outbuf->buffer->use;
#endif
- if (!return_content) {
- RETVAL_FALSE;
- } else {
- RETVAL_STRINGL(return_content, return_len);
- }
- xmlOutputBufferClose(outbuf);
+ if (!return_content) {
+ RETVAL_FALSE;
+ } else {
+ RETVAL_STRINGL(return_content, return_len);
}
- } else {
- RETVAL_FALSE;
+ xmlOutputBufferClose(outbuf);
}
}
/* }}} */