-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest.c
180 lines (153 loc) · 3.78 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* test extension for PHP */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "php_test.h"
/* For compatibility with older PHP versions */
#ifndef ZEND_PARSE_PARAMETERS_NONE
#define ZEND_PARSE_PARAMETERS_NONE() \
ZEND_PARSE_PARAMETERS_START(0, 0) \
ZEND_PARSE_PARAMETERS_END()
#endif
ZEND_DECLARE_MODULE_GLOBALS(test)
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("test.scale", "1", PHP_INI_ALL, OnUpdateLong, scale,
zend_test_globals, test_globals)
PHP_INI_END()
/* {{{ void test_test1()
*/
PHP_FUNCTION(test_test1)
{
ZEND_PARSE_PARAMETERS_NONE();
php_printf("The extension %s is loaded and working!\r\n", "test");
}
/* }}} */
/* {{{ string test_test2( [ string $var ] )
*/
PHP_FUNCTION(test_test2)
{
char *var = "World";
size_t var_len = sizeof("World") - 1;
zend_string *retval;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_STRING(var, var_len)
ZEND_PARSE_PARAMETERS_END();
retval = strpprintf(0, "Hello %s", var);
RETURN_STR(retval);
}
/* }}}*/
static int do_scale_ref(zval *x, zend_long factor)
{
ZVAL_DEREF(x);
if (Z_TYPE_P(x) == IS_LONG) {
Z_LVAL_P(x) *= factor;
} else if (Z_TYPE_P(x) == IS_DOUBLE) {
Z_DVAL_P(x) *= factor;
} else if (Z_TYPE_P(x) == IS_STRING) {
size_t len = Z_STRLEN_P(x);
char *p;
ZVAL_STR(x, zend_string_safe_realloc(Z_STR_P(x), len, factor, 0, 0));
p = Z_STRVAL_P(x) + len;
while (--factor > 0) {
memcpy(p, Z_STRVAL_P(x), len);
p += len;
}
*p = '\000';
} else if (Z_TYPE_P(x) == IS_ARRAY) {
zval *val;
SEPARATE_ARRAY(x);
ZEND_HASH_FOREACH_VAL(Z_ARR_P(x), val) {
if (do_scale_ref(val, factor) != SUCCESS) {
return FAILURE;
}
} ZEND_HASH_FOREACH_END();
} else {
php_error_docref(NULL, E_WARNING, "unexpected argument type");
return FAILURE;
}
return SUCCESS;
}
PHP_FUNCTION(test_scale_ref)
{
zval *x;
zend_long factor = TEST_G(scale); // default value
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ZVAL(x)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(factor)
ZEND_PARSE_PARAMETERS_END();
do_scale_ref(x, factor);
}
static PHP_GINIT_FUNCTION(test)
{
#if defined(COMPILE_DL_BCMATH) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
test_globals->scale= 1;
}
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(test)
{
REGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(test)
{
php_info_print_table_start();
php_info_print_table_header(2, "test support", "enabled");
php_info_print_table_end();
}
/* }}} */
/* {{{ arginfo
*/
ZEND_BEGIN_ARG_INFO(arginfo_test_test1, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_test_test2, 0)
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()
/* }}} */
ZEND_BEGIN_ARG_INFO(arginfo_test_scale_ref, 0)
ZEND_ARG_INFO(1, x) // pass by reference
ZEND_ARG_INFO(0, factor)
ZEND_END_ARG_INFO()
/* {{{ test_functions[]
*/
static const zend_function_entry test_functions[] = {
PHP_FE(test_test1, arginfo_test_test1)
PHP_FE(test_test2, arginfo_test_test2)
PHP_FE(test_scale_ref, arginfo_test_scale_ref)
PHP_FE_END
};
/* }}} */
/* {{{ test_module_entry
*/
zend_module_entry test_module_entry = {
STANDARD_MODULE_HEADER,
"test", /* Extension name */
test_functions, /* zend_function_entry */
PHP_MINIT(test), /* PHP_MINIT - Module initialization */
NULL, /* PHP_MSHUTDOWN - Module shutdown */
NULL, /* PHP_RINIT - Request initialization */
NULL, /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(test), /* PHP_MINFO - Module info */
PHP_TEST_VERSION, /* Version */
PHP_MODULE_GLOBALS(test), /* Module globals */
PHP_GINIT(test), /* PHP_GINIT - Globals initialization */
NULL, /* PHP_GSHUTDOWN - Globals shutdown */
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
#ifdef COMPILE_DL_TEST
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(test)
#endif