2001-10-23 04:28:57 +08:00
|
|
|
New parameter parsing functions
|
|
|
|
===============================
|
|
|
|
|
|
|
|
It should be easier to parse input parameters to an extension function.
|
|
|
|
Hence, borrowing from Python's example, there are now a set of functions
|
|
|
|
that given the string of type specifiers, can parse the input parameters
|
|
|
|
and store the results in the user specified variables. This avoids most
|
|
|
|
of the IS_* checks and convert_to_* conversions. The functions also
|
|
|
|
check for the appropriate number of parameters, and try to output
|
|
|
|
meaningful error messages.
|
|
|
|
|
|
|
|
|
|
|
|
Prototypes
|
|
|
|
----------
|
|
|
|
/* Implemented. */
|
2001-10-23 04:37:11 +08:00
|
|
|
int zend_parse_parameters(int num_args TSRMLS_DC, char *type_spec, ...);
|
|
|
|
int zend_parse_parameters_ex(int flags, int num_args TSRMLS_DC, char *type_spec, ...);
|
2001-10-23 04:28:57 +08:00
|
|
|
|
|
|
|
The zend_parse_parameters() function takes the number of parameters
|
|
|
|
passed to the extension function, the type specifier string, and the
|
|
|
|
list of pointers to variables to store the results in. The _ex() version
|
|
|
|
also takes 'flags' argument -- current only ZEND_PARSE_PARAMS_QUIET can
|
|
|
|
be used as 'flags' to specify that the function should operate quietly
|
|
|
|
and not output any error messages.
|
|
|
|
|
2001-10-23 04:37:11 +08:00
|
|
|
Both functions return SUCCESS or FAILURE depending on the result.
|
|
|
|
|
2001-10-23 04:28:57 +08:00
|
|
|
The auto-conversions are performed as necessary. Arrays, objects, and
|
2002-10-24 05:32:39 +08:00
|
|
|
resources cannot be auto-converted.
|
2001-10-23 04:28:57 +08:00
|
|
|
|
2013-01-10 00:16:40 +08:00
|
|
|
PHP 5.5 includes a new function:
|
|
|
|
|
|
|
|
int zend_parse_parameter(int flags, int arg_num TSRMLS_DC, zval **arg, const char *spec, ...);
|
|
|
|
|
|
|
|
This function behaves like zend_parse_parameters_ex() except that instead of
|
|
|
|
reading the arguments from the stack, it receives a single zval to convert
|
|
|
|
(passed with double indirection). The passed zval may be changed in place as
|
|
|
|
part of the conversion process.
|
|
|
|
|
|
|
|
See also https://wiki.php.net/rfc/zpp_improv#expose_zend_parse_arg_as_zend_parse_parameter
|
|
|
|
|
2001-10-23 04:28:57 +08:00
|
|
|
|
|
|
|
Type specifiers
|
|
|
|
---------------
|
2007-11-03 03:41:12 +08:00
|
|
|
The following list shows the type specifier, its meaning and the parameter
|
2013-04-22 20:41:27 +08:00
|
|
|
types that need to be passed by address. All passed parameters are set
|
2007-11-03 03:41:12 +08:00
|
|
|
if the PHP parameter is non optional and untouched if optional and the
|
|
|
|
parameter is not present. The only exception is O where the zend_class_entry*
|
|
|
|
has to be provided on input and is used to verify the PHP parameter is an
|
|
|
|
instance of that class.
|
|
|
|
|
|
|
|
a - array (zval*)
|
2014-09-20 09:05:32 +08:00
|
|
|
A - array or object (zval*)
|
2007-11-03 03:41:12 +08:00
|
|
|
b - boolean (zend_bool)
|
|
|
|
C - class (zend_class_entry*)
|
|
|
|
d - double (double)
|
|
|
|
f - function or array containing php method call info (returned as
|
|
|
|
zend_fcall_info and zend_fcall_info_cache)
|
|
|
|
h - array (returned as HashTable*)
|
2008-11-25 02:10:36 +08:00
|
|
|
H - array or HASH_OF(object) (returned as HashTable*)
|
2014-09-20 09:05:32 +08:00
|
|
|
l - long (zend_long)
|
|
|
|
L - long, limits out-of-range numbers to LONG_MAX/LONG_MIN (zend_long)
|
2007-11-03 03:41:12 +08:00
|
|
|
o - object of any type (zval*)
|
|
|
|
O - object of specific type given by class entry (zval*, zend_class_entry)
|
2014-09-20 09:05:32 +08:00
|
|
|
p - valid path (string without null bytes in the middle) and its length (char*, size_t)
|
|
|
|
P - valid path (string without null bytes in the middle) as zend_string (zend_string)
|
2007-11-03 03:41:12 +08:00
|
|
|
r - resource (zval*)
|
2014-09-20 09:05:32 +08:00
|
|
|
s - string (with possible null bytes) and its length (char*, size_t)
|
|
|
|
S - string (with possible null bytes) as zend_string (zend_string)
|
2007-11-03 03:41:12 +08:00
|
|
|
z - the actual zval (zval*)
|
2007-11-06 17:48:24 +08:00
|
|
|
* - variable arguments list (0 or more)
|
|
|
|
+ - variable arguments list (1 or more)
|
2001-10-23 04:28:57 +08:00
|
|
|
|
|
|
|
The following characters also have a meaning in the specifier string:
|
2007-11-03 03:41:12 +08:00
|
|
|
| - indicates that the remaining parameters are optional, they
|
|
|
|
should be initialized to default values by the extension since they
|
|
|
|
will not be touched by the parsing function if they are not
|
|
|
|
passed to it.
|
|
|
|
/ - use SEPARATE_ZVAL_IF_NOT_REF() on the parameter it follows
|
2013-01-10 00:16:40 +08:00
|
|
|
! - the parameter it follows can be of specified type or NULL. If NULL is
|
|
|
|
passed and the output for such type is a pointer, then the output
|
|
|
|
pointer is set to a native NULL pointer.
|
|
|
|
For 'b', 'l' and 'd', an extra argument of type zend_bool* must be
|
2014-09-20 09:05:32 +08:00
|
|
|
passed after the corresponding bool*, zend_long* or double* arguments,
|
|
|
|
respectively. A non-zero value will be written to the zend_bool if a
|
2013-01-10 00:16:40 +08:00
|
|
|
PHP NULL is passed.
|
2001-10-23 04:28:57 +08:00
|
|
|
|
2008-08-18 21:09:41 +08:00
|
|
|
|
|
|
|
Note on 64bit compatibility
|
|
|
|
---------------------------
|
2014-09-20 09:05:32 +08:00
|
|
|
Please note that since version 7 PHP uses zend_long as integer type and
|
|
|
|
zend_string with size_t as length, so make sure you pass zend_longs to "l"
|
|
|
|
and size_t to strings length (i.e. for "s" you need to pass char * and int),
|
2008-08-18 21:09:41 +08:00
|
|
|
not the other way round!
|
|
|
|
|
2014-09-20 09:05:32 +08:00
|
|
|
Both mistakes might cause memory corruptions and segfaults:
|
2008-08-18 21:09:41 +08:00
|
|
|
1)
|
|
|
|
char *str;
|
2014-09-20 09:05:32 +08:00
|
|
|
long str_len; /* XXX THIS IS WRONG!! Use size_t instead. */
|
2008-08-18 21:09:41 +08:00
|
|
|
zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len)
|
|
|
|
|
|
|
|
2)
|
2014-09-20 09:05:32 +08:00
|
|
|
int num; /* XXX THIS IS WRONG!! Use zend_long instead. */
|
2008-08-18 21:09:41 +08:00
|
|
|
zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &num)
|
|
|
|
|
|
|
|
If you're in doubt, use check_parameters.php script to the parameters
|
|
|
|
and their types (it can be found in ./scripts/dev/ directory of PHP sources):
|
|
|
|
|
|
|
|
# php ./scripts/dev/check_parameters.php /path/to/your/sources/
|
|
|
|
|
|
|
|
|
2001-10-23 04:28:57 +08:00
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
/* Gets a long, a string and its length, and a zval */
|
2014-09-20 09:05:32 +08:00
|
|
|
zend_long l;
|
2001-10-23 04:28:57 +08:00
|
|
|
char *s;
|
2014-09-20 09:05:32 +08:00
|
|
|
size_t s_len;
|
2001-10-23 04:28:57 +08:00
|
|
|
zval *param;
|
2001-10-23 04:37:11 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lsz",
|
2007-11-03 03:41:12 +08:00
|
|
|
&l, &s, &s_len, ¶m) == FAILURE) {
|
|
|
|
return;
|
2001-10-23 04:37:11 +08:00
|
|
|
}
|
2001-10-23 04:28:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Gets an object of class specified by my_ce, and an optional double. */
|
|
|
|
zval *obj;
|
|
|
|
double d = 0.5;
|
2007-09-29 06:04:39 +08:00
|
|
|
zend_class_entry *my_ce;
|
2001-10-23 04:37:11 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|d",
|
2007-11-03 03:41:12 +08:00
|
|
|
&obj, my_ce, &d) == FAILURE) {
|
|
|
|
return;
|
2001-10-23 04:37:11 +08:00
|
|
|
}
|
2001-10-23 04:28:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Gets an object or null, and an array.
|
|
|
|
If null is passed for object, obj will be set to NULL. */
|
|
|
|
zval *obj;
|
|
|
|
zval *arr;
|
2001-10-24 03:57:45 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o!a",
|
2007-11-03 03:41:12 +08:00
|
|
|
&obj, &arr) == FAILURE) {
|
|
|
|
return;
|
2001-10-23 04:37:11 +08:00
|
|
|
}
|
2001-10-23 04:28:57 +08:00
|
|
|
|
|
|
|
|
2001-10-24 03:57:45 +08:00
|
|
|
/* Gets a separated array which can also be null. */
|
2001-10-23 04:28:57 +08:00
|
|
|
zval *arr;
|
2001-10-24 03:57:45 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/!",
|
2007-11-03 03:41:12 +08:00
|
|
|
&arr) == FAILURE) {
|
|
|
|
return;
|
2001-10-23 04:37:11 +08:00
|
|
|
}
|
2001-10-23 04:28:57 +08:00
|
|
|
|
|
|
|
/* Get either a set of 3 longs or a string. */
|
2014-09-20 09:05:32 +08:00
|
|
|
zend_long l1, l2, l3;
|
2001-10-23 04:28:57 +08:00
|
|
|
char *s;
|
2002-07-04 02:36:50 +08:00
|
|
|
/*
|
2014-09-20 09:05:32 +08:00
|
|
|
* The function expects a pointer to a size_t in this case, not a long
|
2002-07-04 02:36:50 +08:00
|
|
|
* or any other type. If you specify a type which is larger
|
2014-09-20 09:05:32 +08:00
|
|
|
* than a 'size_t', the upper bits might not be initialized
|
2002-07-04 02:36:50 +08:00
|
|
|
* properly, leading to random crashes on platforms like
|
|
|
|
* Tru64 or Linux/Alpha.
|
|
|
|
*/
|
2014-09-20 09:05:32 +08:00
|
|
|
size_t length;
|
2002-07-03 22:01:18 +08:00
|
|
|
|
2001-10-23 04:37:11 +08:00
|
|
|
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC,
|
2007-11-03 03:41:12 +08:00
|
|
|
"lll", &l1, &l2, &l3) == SUCCESS) {
|
|
|
|
/* manipulate longs */
|
2001-10-23 04:37:11 +08:00
|
|
|
} else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC,
|
2007-11-03 03:41:12 +08:00
|
|
|
"s", &s, &length) == SUCCESS) {
|
|
|
|
/* manipulate string */
|
2001-10-23 04:28:57 +08:00
|
|
|
} else {
|
2007-11-03 03:41:12 +08:00
|
|
|
/* output error */
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Function that accepts only varargs (0 or more) */
|
|
|
|
|
|
|
|
int i, num_varargs;
|
2014-09-20 09:05:32 +08:00
|
|
|
zval *varargs = NULL;
|
2007-11-03 03:41:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "*", &varargs, &num_varargs) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_varargs; i++) {
|
|
|
|
/* do something with varargs[i] */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (varargs) {
|
|
|
|
efree(varargs);
|
|
|
|
}
|
2001-10-23 04:37:11 +08:00
|
|
|
|
2007-11-03 03:41:12 +08:00
|
|
|
|
|
|
|
/* Function that accepts a string, followed by varargs (1 or more) */
|
|
|
|
|
|
|
|
char *str;
|
2014-09-20 09:05:32 +08:00
|
|
|
size_t str_len;
|
2007-11-03 03:41:12 +08:00
|
|
|
int i, num_varargs;
|
2014-09-20 09:05:32 +08:00
|
|
|
zval *varargs = NULL;
|
2007-11-03 03:41:12 +08:00
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s+", &str, &str_len, &varargs, &num_varargs) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_varargs; i++) {
|
|
|
|
/* do something with varargs[i] */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Function that takes an array, followed by varargs, and ending with a long */
|
2014-09-20 09:05:32 +08:00
|
|
|
zend_long num;
|
2007-11-03 03:41:12 +08:00
|
|
|
zval *array;
|
|
|
|
int i, num_varargs;
|
2014-09-20 09:05:32 +08:00
|
|
|
zval *varargs = NULL;
|
2007-11-03 03:41:12 +08:00
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a*l", &array, &varargs, &num_varargs, &num) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_varargs; i++) {
|
|
|
|
/* do something with varargs[i] */
|
|
|
|
}
|