Skip to content

Commit 743d276

Browse files
committed
Normalize SimpleXML::asXML() argument handling
Remove odd manual checks in favor of a standard zpp call.
1 parent deceafb commit 743d276

File tree

1 file changed

+48
-60
lines changed

1 file changed

+48
-60
lines changed

ext/simplexml/simplexml.c

Lines changed: 48 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,88 +1429,76 @@ SXE_METHOD(asXML)
14291429
xmlOutputBufferPtr outbuf;
14301430
xmlChar *strval;
14311431
int strval_len;
1432-
char *filename;
1432+
char *filename = NULL;
14331433
size_t filename_len;
14341434

1435-
if (ZEND_NUM_ARGS() > 1) {
1435+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|p", &filename, &filename_len) == FAILURE) {
14361436
RETURN_FALSE;
14371437
}
14381438

1439-
if (ZEND_NUM_ARGS() == 1) {
1440-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
1441-
RETURN_FALSE;
1442-
}
1443-
1444-
sxe = Z_SXEOBJ_P(ZEND_THIS);
1445-
GET_NODE(sxe, node);
1446-
node = php_sxe_get_first_node(sxe, node);
1447-
1448-
if (node) {
1449-
if (node->parent && (XML_DOCUMENT_NODE == node->parent->type)) {
1450-
int bytes;
1451-
bytes = xmlSaveFile(filename, (xmlDocPtr) sxe->document->ptr);
1452-
if (bytes == -1) {
1453-
RETURN_FALSE;
1454-
} else {
1455-
RETURN_TRUE;
1456-
}
1457-
} else {
1458-
outbuf = xmlOutputBufferCreateFilename(filename, NULL, 0);
1459-
1460-
if (outbuf == NULL) {
1461-
RETURN_FALSE;
1462-
}
1463-
1464-
xmlNodeDumpOutput(outbuf, (xmlDocPtr) sxe->document->ptr, node, 0, 0, NULL);
1465-
xmlOutputBufferClose(outbuf);
1466-
RETURN_TRUE;
1467-
}
1468-
} else {
1469-
RETURN_FALSE;
1470-
}
1471-
}
1472-
14731439
sxe = Z_SXEOBJ_P(ZEND_THIS);
14741440
GET_NODE(sxe, node);
14751441
node = php_sxe_get_first_node(sxe, node);
14761442

1477-
if (node) {
1443+
if (!node) {
1444+
RETURN_FALSE;
1445+
}
1446+
1447+
if (filename) {
14781448
if (node->parent && (XML_DOCUMENT_NODE == node->parent->type)) {
1479-
xmlDocDumpMemoryEnc((xmlDocPtr) sxe->document->ptr, &strval, &strval_len, (const char *) ((xmlDocPtr) sxe->document->ptr)->encoding);
1480-
if (!strval) {
1481-
RETVAL_FALSE;
1449+
int bytes;
1450+
bytes = xmlSaveFile(filename, (xmlDocPtr) sxe->document->ptr);
1451+
if (bytes == -1) {
1452+
RETURN_FALSE;
14821453
} else {
1483-
RETVAL_STRINGL((char *)strval, strval_len);
1454+
RETURN_TRUE;
14841455
}
1485-
xmlFree(strval);
14861456
} else {
1487-
char *return_content;
1488-
size_t return_len;
1489-
/* Should we be passing encoding information instead of NULL? */
1490-
outbuf = xmlAllocOutputBuffer(NULL);
1457+
outbuf = xmlOutputBufferCreateFilename(filename, NULL, 0);
14911458

14921459
if (outbuf == NULL) {
14931460
RETURN_FALSE;
14941461
}
14951462

1496-
xmlNodeDumpOutput(outbuf, (xmlDocPtr) sxe->document->ptr, node, 0, 0, (const char *) ((xmlDocPtr) sxe->document->ptr)->encoding);
1497-
xmlOutputBufferFlush(outbuf);
1463+
xmlNodeDumpOutput(outbuf, (xmlDocPtr) sxe->document->ptr, node, 0, 0, NULL);
1464+
xmlOutputBufferClose(outbuf);
1465+
RETURN_TRUE;
1466+
}
1467+
}
1468+
1469+
if (node->parent && (XML_DOCUMENT_NODE == node->parent->type)) {
1470+
xmlDocDumpMemoryEnc((xmlDocPtr) sxe->document->ptr, &strval, &strval_len, (const char *) ((xmlDocPtr) sxe->document->ptr)->encoding);
1471+
if (!strval) {
1472+
RETVAL_FALSE;
1473+
} else {
1474+
RETVAL_STRINGL((char *)strval, strval_len);
1475+
}
1476+
xmlFree(strval);
1477+
} else {
1478+
char *return_content;
1479+
size_t return_len;
1480+
/* Should we be passing encoding information instead of NULL? */
1481+
outbuf = xmlAllocOutputBuffer(NULL);
1482+
1483+
if (outbuf == NULL) {
1484+
RETURN_FALSE;
1485+
}
1486+
1487+
xmlNodeDumpOutput(outbuf, (xmlDocPtr) sxe->document->ptr, node, 0, 0, (const char *) ((xmlDocPtr) sxe->document->ptr)->encoding);
1488+
xmlOutputBufferFlush(outbuf);
14981489
#ifdef LIBXML2_NEW_BUFFER
1499-
return_content = (char *)xmlOutputBufferGetContent(outbuf);
1500-
return_len = xmlOutputBufferGetSize(outbuf);
1490+
return_content = (char *)xmlOutputBufferGetContent(outbuf);
1491+
return_len = xmlOutputBufferGetSize(outbuf);
15011492
#else
1502-
return_content = (char *)outbuf->buffer->content;
1503-
return_len = outbuf->buffer->use;
1493+
return_content = (char *)outbuf->buffer->content;
1494+
return_len = outbuf->buffer->use;
15041495
#endif
1505-
if (!return_content) {
1506-
RETVAL_FALSE;
1507-
} else {
1508-
RETVAL_STRINGL(return_content, return_len);
1509-
}
1510-
xmlOutputBufferClose(outbuf);
1496+
if (!return_content) {
1497+
RETVAL_FALSE;
1498+
} else {
1499+
RETVAL_STRINGL(return_content, return_len);
15111500
}
1512-
} else {
1513-
RETVAL_FALSE;
1501+
xmlOutputBufferClose(outbuf);
15141502
}
15151503
}
15161504
/* }}} */

0 commit comments

Comments
 (0)