Skip to content

Commit 15d057d

Browse files
committed
- New parameter parsing API
1 parent 1c77f59 commit 15d057d

File tree

1 file changed

+85
-98
lines changed

1 file changed

+85
-98
lines changed

ext/xmlrpc/xmlrpc-epi-php.c

+85-98
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ typedef struct _xmlrpc_callback_data {
189189
XMLRPC_VALUE_TYPE get_zval_xmlrpc_type(zval* value, zval** newvalue);
190190
static void php_xmlrpc_introspection_callback(XMLRPC_SERVER server, void* data);
191191
int sset_zval_xmlrpc_type(zval* value, XMLRPC_VALUE_TYPE type);
192-
zval* decode_request_worker(zval* xml_in, zval* encoding_in, zval* method_name_out);
192+
zval* decode_request_worker(char *xml_in, int xml_in_len, char *encoding_in, zval* method_name_out);
193193
const char* xmlrpc_type_as_str(XMLRPC_VALUE_TYPE type, XMLRPC_VECTOR_TYPE vtype);
194194
XMLRPC_VALUE_TYPE xmlrpc_str_as_type(const char* str);
195195
XMLRPC_VECTOR_TYPE xmlrpc_str_as_vector_type(const char* str);
@@ -612,20 +612,20 @@ static zval* XMLRPC_to_PHP(XMLRPC_VALUE el)
612612
return elem;
613613
}
614614

615-
/* {{{ proto string xmlrpc_encode_request(string method, mixed params)
615+
/* {{{ proto string xmlrpc_encode_request(string method, mixed params [, array output_options])
616616
Generates XML for a method request */
617617
PHP_FUNCTION(xmlrpc_encode_request)
618618
{
619619
XMLRPC_REQUEST xRequest = NULL;
620-
zval **method, **vals, **out_opts;
621-
char* outBuf;
620+
char *outBuf;
621+
zval **method, **vals, *out_opts;
622622
php_output_options out;
623623

624-
if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > 3 || (zend_get_parameters_ex(ZEND_NUM_ARGS(), &method, &vals, &out_opts) == FAILURE)) {
625-
WRONG_PARAM_COUNT; /* prints/logs a warning and returns */
624+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ZZ|a", &method, &vals, &out_opts) == FAILURE) {
625+
return;
626626
}
627627

628-
set_output_options(&out, (ZEND_NUM_ARGS() == 3) ? *out_opts : 0);
628+
set_output_options(&out, (ZEND_NUM_ARGS() == 3) ? out_opts : 0);
629629

630630
if(return_value_used) {
631631
xRequest = XMLRPC_RequestNew();
@@ -666,8 +666,8 @@ PHP_FUNCTION(xmlrpc_encode)
666666
zval **arg1;
667667
char *outBuf;
668668

669-
if (ZEND_NUM_ARGS() != 1 || (zend_get_parameters_ex(1, &arg1) == FAILURE)) {
670-
WRONG_PARAM_COUNT;
669+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg1) == FAILURE) {
670+
return;
671671
}
672672

673673
if( return_value_used ) {
@@ -690,15 +690,15 @@ PHP_FUNCTION(xmlrpc_encode)
690690
/* }}} */
691691

692692

693-
zval* decode_request_worker (zval* xml_in, zval* encoding_in, zval* method_name_out)
693+
zval* decode_request_worker(char *xml_in, int xml_in_len, char *encoding_in, zval* method_name_out)
694694
{
695695
zval* retval = NULL;
696696
XMLRPC_REQUEST response;
697697
STRUCT_XMLRPC_REQUEST_INPUT_OPTIONS opts = {{0}};
698-
opts.xml_elem_opts.encoding = encoding_in ? utf8_get_encoding_id_from_string(Z_STRVAL_P(encoding_in)) : ENCODING_DEFAULT;
698+
opts.xml_elem_opts.encoding = encoding_in ? utf8_get_encoding_id_from_string(encoding_in) : ENCODING_DEFAULT;
699699

700700
/* generate XMLRPC_REQUEST from raw xml */
701-
response = XMLRPC_REQUEST_FromXML(Z_STRVAL_P(xml_in), Z_STRLEN_P(xml_in), &opts);
701+
response = XMLRPC_REQUEST_FromXML(xml_in, xml_in_len, &opts);
702702
if (response) {
703703
/* convert xmlrpc data to native php types */
704704
retval = XMLRPC_to_PHP(XMLRPC_RequestGetData(response));
@@ -722,21 +722,18 @@ zval* decode_request_worker (zval* xml_in, zval* encoding_in, zval* method_name_
722722
Decodes XML into native PHP types */
723723
PHP_FUNCTION(xmlrpc_decode_request)
724724
{
725-
zval **xml, **method, **encoding = NULL;
726-
int argc = ZEND_NUM_ARGS();
725+
char *xml, *encoding = NULL;
726+
zval **method;
727+
int xml_len, encoding_len = 0;
727728

728-
if (argc < 2 || argc > 3 || (zend_get_parameters_ex(argc, &xml, &method, &encoding) == FAILURE)) {
729-
WRONG_PARAM_COUNT;
729+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sZ|s", &xml, &xml_len, &method, &encoding, &encoding_len) == FAILURE) {
730+
return;
730731
}
731732

732-
convert_to_string_ex(xml);
733733
convert_to_string_ex(method);
734-
if(argc == 3) {
735-
convert_to_string_ex(encoding);
736-
}
737734

738735
if(return_value_used) {
739-
zval* retval = decode_request_worker(*xml, encoding ? *encoding : NULL, *method);
736+
zval* retval = decode_request_worker(xml, xml_len, encoding_len ? encoding : NULL, *method);
740737
if(retval) {
741738
*return_value = *retval;
742739
FREE_ZVAL(retval);
@@ -750,20 +747,15 @@ PHP_FUNCTION(xmlrpc_decode_request)
750747
Decodes XML into native PHP types */
751748
PHP_FUNCTION(xmlrpc_decode)
752749
{
753-
zval **arg1, **arg2 = NULL;
754-
int argc = ZEND_NUM_ARGS();
755-
756-
if (argc < 1 || argc > 2 || (zend_get_parameters_ex(argc, &arg1, &arg2) == FAILURE)) {
757-
WRONG_PARAM_COUNT;
758-
}
750+
char *arg1, *arg2 = NULL;
751+
int arg1_len, arg2_len = 0;
759752

760-
convert_to_string_ex(arg1);
761-
if(argc == 2) {
762-
convert_to_string_ex(arg2);
753+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &arg1, &arg1_len, &arg2, &arg2_len) == FAILURE) {
754+
return;
763755
}
764756

765757
if(return_value_used) {
766-
zval* retval = decode_request_worker(*arg1, arg2 ? *arg2 : NULL, NULL);
758+
zval* retval = decode_request_worker(arg1, arg1_len, arg2_len ? arg2 : NULL, NULL);
767759
if(retval) {
768760
*return_value = *retval;
769761
FREE_ZVAL(retval);
@@ -811,25 +803,21 @@ PHP_FUNCTION(xmlrpc_server_create)
811803
Destroys server resources */
812804
PHP_FUNCTION(xmlrpc_server_destroy)
813805
{
814-
zval **arg1;
815-
int bSuccess = FAILURE;
806+
zval *arg1;
807+
int bSuccess = FAILURE, type;
816808

817-
if (ZEND_NUM_ARGS() != 1 || (zend_get_parameters_ex(1, &arg1) == FAILURE)) {
818-
WRONG_PARAM_COUNT;
809+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
810+
return;
819811
}
820812

821-
if(Z_TYPE_PP(arg1) == IS_RESOURCE) {
822-
int type;
823-
824-
xmlrpc_server_data *server = zend_list_find(Z_LVAL_PP(arg1), &type);
813+
xmlrpc_server_data *server = zend_list_find(Z_LVAL_P(arg1), &type);
825814

826-
if(server && type == le_xmlrpc_server) {
827-
bSuccess = zend_list_delete(Z_LVAL_PP(arg1));
815+
if (server && type == le_xmlrpc_server) {
816+
bSuccess = zend_list_delete(Z_LVAL_P (arg1));
828817

829-
/* called by hashtable destructor
830-
* destroy_server_data(server);
831-
*/
832-
}
818+
/* called by hashtable destructor
819+
* destroy_server_data(server);
820+
*/
833821
}
834822
RETVAL_LONG(bSuccess == SUCCESS);
835823
}
@@ -933,28 +921,30 @@ static void php_xmlrpc_introspection_callback(XMLRPC_SERVER server, void* data)
933921
Register a PHP function to handle method matching method_name */
934922
PHP_FUNCTION(xmlrpc_server_register_method)
935923
{
936-
zval **method_key, **method_name, **handle, *method_name_save;
924+
char *method_key;
925+
int method_key_len;
926+
zval *handle, *method_name_save, **method_name;
937927
int type;
938928
xmlrpc_server_data* server;
939929

940-
if (ZEND_NUM_ARGS() != 3 || (zend_get_parameters_ex(3, &handle, &method_key, &method_name) == FAILURE)) {
941-
WRONG_PARAM_COUNT;
930+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsZ", &handle, &method_key, &method_key_len, &method_name) == FAILURE) {
931+
return;
942932
}
943933

944-
server = zend_list_find(Z_LVAL_PP(handle), &type);
934+
server = zend_list_find(Z_LVAL_P(handle), &type);
945935

946936
if(type == le_xmlrpc_server) {
947937
/* register with C engine. every method just calls our standard callback,
948938
* and it then dispatches to php as necessary
949939
*/
950-
if(XMLRPC_ServerRegisterMethod(server->server_ptr, Z_STRVAL_PP(method_key), php_xmlrpc_callback)) {
940+
if(XMLRPC_ServerRegisterMethod(server->server_ptr, method_key, php_xmlrpc_callback)) {
951941
/* save for later use */
952942
MAKE_STD_ZVAL(method_name_save);
953943
*method_name_save = **method_name;
954944
zval_copy_ctor(method_name_save);
955945

956946
/* register our php method */
957-
add_zval(server->method_map, Z_STRVAL_PP(method_key), &method_name_save);
947+
add_zval(server->method_map, method_key, &method_name_save);
958948

959949
RETURN_BOOL(1);
960950
}
@@ -968,15 +958,15 @@ PHP_FUNCTION(xmlrpc_server_register_method)
968958
Register a PHP function to generate documentation */
969959
PHP_FUNCTION(xmlrpc_server_register_introspection_callback)
970960
{
971-
zval **method_name, **handle, *method_name_save;
961+
zval **method_name, *handle, *method_name_save;
972962
int type;
973963
xmlrpc_server_data* server;
974964

975-
if (ZEND_NUM_ARGS() != 2 || (zend_get_parameters_ex(2, &handle, &method_name) == FAILURE)) {
976-
WRONG_PARAM_COUNT;
965+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rZ", &handle, &method_name) == FAILURE) {
966+
return;
977967
}
978968

979-
server = zend_list_find(Z_LVAL_PP(handle), &type);
969+
server = zend_list_find(Z_LVAL_P(handle), &type);
980970

981971
if(type == le_xmlrpc_server) {
982972
/* save for later use */
@@ -1004,30 +994,30 @@ PHP_FUNCTION(xmlrpc_server_call_method)
1004994
XMLRPC_REQUEST xRequest;
1005995
STRUCT_XMLRPC_REQUEST_INPUT_OPTIONS input_opts;
1006996
xmlrpc_server_data* server;
1007-
zval **rawxml, **caller_params, **handle, **output_opts = NULL;
1008-
int type;
997+
zval **caller_params, *handle, **output_opts = NULL;
998+
char *rawxml;
999+
int rawxml_len, type;
10091000
php_output_options out;
10101001
int argc =ZEND_NUM_ARGS();
10111002

1012-
if (argc < 3 || argc > 4 || (zend_get_parameters_ex(argc, &handle, &rawxml, &caller_params, &output_opts) != SUCCESS)) {
1013-
WRONG_PARAM_COUNT;
1003+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsZ|Z", &handle, &rawxml, &rawxml_len, &caller_params, &output_opts) != SUCCESS) {
1004+
return;
10141005
}
10151006
/* user output options */
10161007
if (argc == 3) {
10171008
set_output_options(&out, NULL);
1018-
}
1019-
else {
1009+
} else {
10201010
set_output_options(&out, *output_opts);
10211011
}
10221012

1023-
server = zend_list_find(Z_LVAL_PP(handle), &type);
1013+
server = zend_list_find(Z_LVAL_P(handle), &type);
10241014

10251015
if(type == le_xmlrpc_server) {
10261016
/* HACK: use output encoding for now */
10271017
input_opts.xml_elem_opts.encoding = utf8_get_encoding_id_from_string(out.xmlrpc_out.xml_elem_opts.encoding);
10281018

10291019
/* generate an XMLRPC_REQUEST from the raw xml input */
1030-
xRequest = XMLRPC_REQUEST_FromXML(Z_STRVAL_PP(rawxml), Z_STRLEN_PP(rawxml), &input_opts);
1020+
xRequest = XMLRPC_REQUEST_FromXML(rawxml, rawxml_len, &input_opts);
10311021

10321022
if(xRequest) {
10331023
const char* methodname = XMLRPC_RequestGetMethodName(xRequest);
@@ -1127,18 +1117,18 @@ PHP_FUNCTION(xmlrpc_server_call_method)
11271117
Adds introspection documentation */
11281118
PHP_FUNCTION(xmlrpc_server_add_introspection_data)
11291119
{
1130-
zval **handle, **desc;
1120+
zval *handle, *desc;
11311121
int type;
11321122
xmlrpc_server_data* server;
11331123

1134-
if (ZEND_NUM_ARGS() != 2 || (zend_get_parameters_ex(2, &handle, &desc) == FAILURE)) {
1135-
WRONG_PARAM_COUNT;
1124+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra", &handle, &desc) == FAILURE) {
1125+
return;
11361126
}
11371127

1138-
server = zend_list_find(Z_LVAL_PP(handle), &type);
1128+
server = zend_list_find(Z_LVAL_P(handle), &type);
11391129

11401130
if (type == le_xmlrpc_server) {
1141-
XMLRPC_VALUE xDesc = PHP_to_XMLRPC(*desc TSRMLS_CC);
1131+
XMLRPC_VALUE xDesc = PHP_to_XMLRPC(desc TSRMLS_CC);
11421132
if (xDesc) {
11431133
int retval = XMLRPC_ServerAddIntrospectionData(server->server_ptr, xDesc);
11441134
XMLRPC_CleanupValue(xDesc);
@@ -1154,17 +1144,17 @@ PHP_FUNCTION(xmlrpc_server_add_introspection_data)
11541144
Decodes XML into a list of method descriptions */
11551145
PHP_FUNCTION(xmlrpc_parse_method_descriptions)
11561146
{
1157-
zval **arg1, *retval;
1147+
zval *retval;
1148+
char *arg1;
1149+
int arg1_len;
11581150

1159-
if (ZEND_NUM_ARGS() != 1 || (zend_get_parameters_ex(1, &arg1) == FAILURE)) {
1160-
WRONG_PARAM_COUNT;
1151+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg1, &arg1_len) == FAILURE) {
1152+
return;
11611153
}
11621154

1163-
convert_to_string_ex(arg1);
1164-
11651155
if(return_value_used) {
11661156
STRUCT_XMLRPC_ERROR err = {0};
1167-
XMLRPC_VALUE xVal = XMLRPC_IntrospectionCreateDescription(Z_STRVAL_PP(arg1), &err);
1157+
XMLRPC_VALUE xVal = XMLRPC_IntrospectionCreateDescription(arg1, &err);
11681158
if(xVal) {
11691159
retval = XMLRPC_to_PHP(xVal);
11701160

@@ -1397,21 +1387,22 @@ XMLRPC_VALUE_TYPE get_zval_xmlrpc_type(zval* value, zval** newvalue)
13971387
Sets xmlrpc type, base64 or datetime, for a PHP string value */
13981388
PHP_FUNCTION(xmlrpc_set_type)
13991389
{
1400-
zval **arg, **type;
1390+
zval **arg;
1391+
char *type;
1392+
int type_len;
14011393
XMLRPC_VALUE_TYPE vtype;
14021394

1403-
if (ZEND_NUM_ARGS() != 2 || (zend_get_parameters_ex(2, &arg, &type) == FAILURE)) {
1404-
WRONG_PARAM_COUNT;
1395+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zs", &arg, &type, &type_len) == FAILURE) {
1396+
return;
14051397
}
14061398

1407-
convert_to_string_ex(type);
1408-
vtype = xmlrpc_str_as_type(Z_STRVAL_PP(type));
1399+
vtype = xmlrpc_str_as_type(type);
14091400
if (vtype != xmlrpc_none) {
14101401
if (set_zval_xmlrpc_type(*arg, vtype) == SUCCESS) {
14111402
RETURN_TRUE;
14121403
}
14131404
} else {
1414-
zend_error(E_WARNING,"invalid type '%s' passed to xmlrpc_set_type()", Z_STRVAL_PP(type));
1405+
zend_error(E_WARNING,"invalid type '%s' passed to xmlrpc_set_type()", type);
14151406
}
14161407
RETURN_FALSE;
14171408
}
@@ -1425,8 +1416,8 @@ PHP_FUNCTION(xmlrpc_get_type)
14251416
XMLRPC_VALUE_TYPE type;
14261417
XMLRPC_VECTOR_TYPE vtype = xmlrpc_vector_none;
14271418

1428-
if (ZEND_NUM_ARGS() != 1 || (zend_get_parameters_ex(1, &arg) == FAILURE)) {
1429-
WRONG_PARAM_COUNT;
1419+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
1420+
return;
14301421
}
14311422

14321423
type = get_zval_xmlrpc_type(*arg, 0);
@@ -1442,25 +1433,21 @@ PHP_FUNCTION(xmlrpc_get_type)
14421433
Determines if an array value represents an XMLRPC fault. */
14431434
PHP_FUNCTION(xmlrpc_is_fault)
14441435
{
1445-
zval **arg, **val;
1436+
zval *arg, **val;
14461437

1447-
if (ZEND_NUM_ARGS() != 1 || (zend_get_parameters_ex(1, &arg) == FAILURE)) {
1448-
WRONG_PARAM_COUNT;
1438+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &arg) == FAILURE) {
1439+
return;
14491440
}
14501441

1451-
if (Z_TYPE_PP(arg) != IS_ARRAY) {
1452-
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array argument expected");
1453-
} else {
1454-
/* The "correct" way to do this would be to call the xmlrpc
1455-
* library XMLRPC_ValueIsFault() func. However, doing that
1456-
* would require us to create an xmlrpc value from the php
1457-
* array, which is rather expensive, especially if it was
1458-
* a big array. Thus, we resort to this not so clever hackery.
1459-
*/
1460-
if (zend_hash_find(Z_ARRVAL_PP(arg), FAULT_CODE, FAULT_CODE_LEN + 1, (void**) &val) == SUCCESS &&
1461-
zend_hash_find(Z_ARRVAL_PP(arg), FAULT_STRING, FAULT_STRING_LEN + 1, (void**) &val) == SUCCESS) {
1462-
RETURN_TRUE;
1463-
}
1442+
/* The "correct" way to do this would be to call the xmlrpc
1443+
* library XMLRPC_ValueIsFault() func. However, doing that
1444+
* would require us to create an xmlrpc value from the php
1445+
* array, which is rather expensive, especially if it was
1446+
* a big array. Thus, we resort to this not so clever hackery.
1447+
*/
1448+
if (zend_hash_find(Z_ARRVAL_P(arg), FAULT_CODE, FAULT_CODE_LEN + 1, (void**) &val) == SUCCESS &&
1449+
zend_hash_find(Z_ARRVAL_P(arg), FAULT_STRING, FAULT_STRING_LEN + 1, (void**) &val) == SUCCESS) {
1450+
RETURN_TRUE;
14641451
}
14651452

14661453
RETURN_FALSE;

0 commit comments

Comments
 (0)