README.PARAMETER_PARSING_API 7.5 KB

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