README.PARAMETER_PARSING_API 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. New parameter parsing functions
  2. ===============================
  3. It should be easier to parse input parameters to an extension function.
  4. Hence, borrowing from Python's example, there are now a set of functions
  5. that given the string of type specifiers, can parse the input parameters
  6. and store the results in the user specified variables. This avoids most
  7. of the IS_* checks and convert_to_* conversions. The functions also
  8. check for the appropriate number of parameters, and try to output
  9. meaningful error messages.
  10. Prototypes
  11. ----------
  12. /* Implemented. */
  13. int zend_parse_parameters(int num_args, char *type_spec, ...);
  14. int zend_parse_parameters_ex(int flags, int num_args, char *type_spec, ...);
  15. The zend_parse_parameters() function takes the number of parameters
  16. passed to the extension function, the type specifier string, and the
  17. list of pointers to variables to store the results in. The _ex() version
  18. also takes 'flags' argument -- current only ZEND_PARSE_PARAMS_QUIET can
  19. be used as 'flags' to specify that the function should operate quietly
  20. and not output any error messages.
  21. Both functions return SUCCESS or FAILURE depending on the result.
  22. The auto-conversions are performed as necessary. Arrays, objects, and
  23. resources cannot be auto-converted.
  24. PHP 5.3 includes a new function (actually implemented as macro):
  25. int zend_parse_parameters_none();
  26. This returns SUCCESS if no argument has been passed to the function,
  27. FAILURE otherwise.
  28. PHP 5.5 includes a new function:
  29. int zend_parse_parameter(int flags, int arg_num, zval **arg, const char *spec, ...);
  30. This function behaves like zend_parse_parameters_ex() except that instead of
  31. reading the arguments from the stack, it receives a single zval to convert
  32. (passed with double indirection). The passed zval may be changed in place as
  33. part of the conversion process.
  34. See also https://wiki.php.net/rfc/zpp_improv#expose_zend_parse_arg_as_zend_parse_parameter
  35. Type specifiers
  36. ---------------
  37. The following list shows the type specifier, its meaning and the parameter
  38. types that need to be passed by address. All passed parameters are set
  39. if the PHP parameter is non optional and untouched if optional and the
  40. parameter is not present. The only exception is O where the zend_class_entry*
  41. has to be provided on input and is used to verify the PHP parameter is an
  42. instance of that class.
  43. a - array (zval*)
  44. A - array or object (zval*)
  45. b - boolean (zend_bool)
  46. C - class (zend_class_entry*)
  47. d - double (double)
  48. f - function or array containing php method call info (returned as
  49. zend_fcall_info and zend_fcall_info_cache)
  50. h - array (returned as HashTable*)
  51. H - array or HASH_OF(object) (returned as HashTable*)
  52. l - long (zend_long)
  53. L - long, limits out-of-range numbers to LONG_MAX/LONG_MIN (zend_long, ZEND_LONG_MAX/ZEND_LONG_MIN)
  54. o - object of any type (zval*)
  55. O - object of specific type given by class entry (zval*, zend_class_entry)
  56. p - valid path (string without null bytes in the middle) and its length (char*, size_t)
  57. P - valid path (string without null bytes in the middle) as zend_string (zend_string*)
  58. r - resource (zval*)
  59. s - string (with possible null bytes) and its length (char*, size_t)
  60. S - string (with possible null bytes) as zend_string (zend_string*)
  61. z - the actual zval (zval*)
  62. * - variable arguments list (0 or more)
  63. + - variable arguments list (1 or more)
  64. The following characters also have a meaning in the specifier string:
  65. | - indicates that the remaining parameters are optional, they
  66. should be initialized to default values by the extension since they
  67. will not be touched by the parsing function if they are not
  68. passed to it.
  69. / - use SEPARATE_ZVAL_IF_NOT_REF() on the parameter it follows
  70. ! - the parameter it follows can be of specified type or NULL. If NULL is
  71. passed and the output for such type is a pointer, then the output
  72. pointer is set to a native NULL pointer.
  73. For 'b', 'l' and 'd', an extra argument of type zend_bool* must be
  74. passed after the corresponding bool*, zend_long* or double* arguments,
  75. respectively. A non-zero value will be written to the zend_bool if a
  76. PHP NULL is passed.
  77. Note on 64bit compatibility
  78. ---------------------------
  79. Please note that since version 7 PHP uses zend_long as integer type and
  80. zend_string with size_t as length, so make sure you pass zend_longs to "l"
  81. and size_t to strings length (i.e. for "s" you need to pass char * and size_t),
  82. not the other way round!
  83. Both mistakes might cause memory corruptions and segfaults:
  84. 1)
  85. char *str;
  86. long str_len; /* XXX THIS IS WRONG!! Use size_t instead. */
  87. zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str, &str_len)
  88. 2)
  89. int num; /* XXX THIS IS WRONG!! Use zend_long instead. */
  90. zend_parse_parameters(ZEND_NUM_ARGS(), "l", &num)
  91. If you're in doubt, use check_parameters.php script to the parameters
  92. and their types (it can be found in ./scripts/dev/ directory of PHP sources):
  93. # php ./scripts/dev/check_parameters.php /path/to/your/sources/
  94. Examples
  95. --------
  96. /* Gets a long, a string and its length, and a zval */
  97. zend_long l;
  98. char *s;
  99. size_t s_len;
  100. zval *param;
  101. if (zend_parse_parameters(ZEND_NUM_ARGS(), "lsz",
  102. &l, &s, &s_len, &param) == FAILURE) {
  103. return;
  104. }
  105. /* Gets an object of class specified by my_ce, and an optional double. */
  106. zval *obj;
  107. double d = 0.5;
  108. zend_class_entry *my_ce;
  109. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|d",
  110. &obj, my_ce, &d) == FAILURE) {
  111. return;
  112. }
  113. /* Gets an object or null, and an array.
  114. If null is passed for object, obj will be set to NULL. */
  115. zval *obj;
  116. zval *arr;
  117. if (zend_parse_parameters(ZEND_NUM_ARGS(), "o!a",
  118. &obj, &arr) == FAILURE) {
  119. return;
  120. }
  121. /* Gets a separated array which can also be null. */
  122. zval *arr;
  123. if (zend_parse_parameters(ZEND_NUM_ARGS(), "a/!",
  124. &arr) == FAILURE) {
  125. return;
  126. }
  127. /* Get either a set of 3 longs or a string. */
  128. zend_long l1, l2, l3;
  129. char *s;
  130. /*
  131. * The function expects a pointer to a size_t in this case, not a long
  132. * or any other type. If you specify a type which is larger
  133. * than a 'size_t', the upper bits might not be initialized
  134. * properly, leading to random crashes on platforms like
  135. * Tru64 or Linux/Alpha.
  136. */
  137. size_t length;
  138. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
  139. "lll", &l1, &l2, &l3) == SUCCESS) {
  140. /* manipulate longs */
  141. } else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
  142. "s", &s, &length) == SUCCESS) {
  143. /* manipulate string */
  144. } else {
  145. /* output error */
  146. return;
  147. }
  148. /* Function that accepts only varargs (0 or more) */
  149. int i, num_varargs;
  150. zval *varargs = NULL;
  151. if (zend_parse_parameters(ZEND_NUM_ARGS(), "*", &varargs, &num_varargs) == FAILURE) {
  152. return;
  153. }
  154. for (i = 0; i < num_varargs; i++) {
  155. /* do something with varargs[i] */
  156. }
  157. if (varargs) {
  158. efree(varargs);
  159. }
  160. /* Function that accepts a string, followed by varargs (1 or more) */
  161. char *str;
  162. size_t str_len;
  163. int i, num_varargs;
  164. zval *varargs = NULL;
  165. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s+", &str, &str_len, &varargs, &num_varargs) == FAILURE) {
  166. return;
  167. }
  168. for (i = 0; i < num_varargs; i++) {
  169. /* do something with varargs[i] */
  170. }
  171. /* Function that takes an array, followed by varargs, and ending with a long */
  172. zend_long num;
  173. zval *array;
  174. int i, num_varargs;
  175. zval *varargs = NULL;
  176. if (zend_parse_parameters(ZEND_NUM_ARGS(), "a*l", &array, &varargs, &num_varargs, &num) == FAILURE) {
  177. return;
  178. }
  179. for (i = 0; i < num_varargs; i++) {
  180. /* do something with varargs[i] */
  181. }
  182. /* Function that doesn't accept any arguments */
  183. if (zend_parse_parameters_none() == FAILURE) {
  184. return;
  185. }