You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
6.6 KiB

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