-
Notifications
You must be signed in to change notification settings - Fork 2
/
bonjour.c
256 lines (217 loc) · 6.8 KB
/
bonjour.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2019 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Vincent JARDIN |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "php_bonjour.h"
#ifndef ZEND_PARSE_PARAMETERS_NONE
#define ZEND_PARSE_PARAMETERS_NONE() \
ZEND_PARSE_PARAMETERS_START(0, 0) \
ZEND_PARSE_PARAMETERS_END()
#endif /* ZEND_PARSE_PARAMETERS_NONE */
/* {{{ void bonjour_test1()
*/
PHP_FUNCTION(bonjour_test1)
{
ZEND_PARSE_PARAMETERS_NONE();
php_printf("The extension %s is loaded and working!\r\n", "bonjour");
}
/* }}} */
/* {{{ string bonjour_test2( [ string $var ] )
*/
PHP_FUNCTION(bonjour_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);
}
/* }}}*/
/* {{{ string bonjour_array1( array $arg1, array $arg2 )
*/
PHP_FUNCTION(bonjour_array1)
{
zval *arg1, *arg2;
zend_string *key;
zval *value;
zend_long h;
char str[BUFSIZ] = "";
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_ARRAY(arg1)
Z_PARAM_ARRAY(arg2)
ZEND_PARSE_PARAMETERS_END();
php_printf("Array size arg1=%d" PHP_EOL, zend_hash_num_elements(Z_ARRVAL_P(arg1)));
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arg1), h, key, value) {
if (Z_TYPE_P(value) != IS_STRING) {
php_printf("%s:%d key[%d]=%s value=Invalid String\n", __func__, __LINE__, (int)h, key ? ZSTR_VAL(key): "NULL");
continue;
}
php_printf("%s:%d key[%d]=%s value=%s\n", __func__, __LINE__, (int)h, key ? ZSTR_VAL(key): "NULL", Z_STRVAL_P(value));
ZVAL_DEREF(value);
strcat(str, Z_STRVAL_P(value)); /* TODO use strlcat() */
} ZEND_HASH_FOREACH_END();
ZVAL_NEW_STR(return_value, zend_string_init(str, strlen(str)+1, 0));
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arg2), h, key, value) {
if (Z_TYPE_P(value) != IS_STRING) {
php_printf("%s:%d key[%d]=%s value=Invalid String\n", __func__, __LINE__, (int)h, key ? ZSTR_VAL(key): "NULL");
continue;
}
php_printf("%s:%d key[%d]=%s value=%s\n", __func__, __LINE__, (int)h, key ? ZSTR_VAL(key): "NULL", Z_STRVAL_P(value));
ZVAL_DEREF(value);
strcat(str, Z_STRVAL_P(value)); /* TODO use strlcat() */
} ZEND_HASH_FOREACH_END();
if (strlen(str) <= 1) {
RETURN_EMPTY_STRING();
return;
}
ZVAL_NEW_STR(return_value, zend_string_init(str, strlen(str)+1, 0));
return;
}
/* }}}*/
/* {{{ array bonjour_array2( array $arg1, string $arg2[, bool $associative] )
*/
PHP_FUNCTION(bonjour_array2)
{
zval *arg1;
zend_string *arg2;
zend_bool associative = 0;
zend_string *key;
zval *value;
zend_long h;
char str_value[BUFSIZ];
int k;
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_ARRAY(arg1)
Z_PARAM_STR(arg2)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(associative)
ZEND_PARSE_PARAMETERS_END();
array_init(return_value); /* zval *return_value */
for (k = 0; k < 5; k++) {
zval zva;
array_init(&zva);
if (associative) {
char str_key[BUFSIZ];
int i = 0;
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arg1), h, key, value) {
if (Z_TYPE_P(value) != IS_STRING)
continue;
snprintf(str_key, sizeof(str_key), "%s-%d", ZSTR_VAL(arg2), i);
snprintf(str_value, sizeof(str_value), "%s-%d", Z_STRVAL_P(value), i);
ZVAL_DEREF(value);
/* return_value[str_key] = str_value; */
add_assoc_stringl(&zva, str_key, str_value, strlen(str_value));
i++;
} ZEND_HASH_FOREACH_END();
} else {
int i = 0;
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arg1), h, key, value) {
if (Z_TYPE_P(value) != IS_STRING)
continue;
snprintf(str_value, sizeof(str_value), "%s-%d", Z_STRVAL_P(value), i);
ZVAL_DEREF(value);
/* return_value[i] = str_value; */
add_index_stringl(&zva, i, str_value, strlen(str_value));
i++;
} ZEND_HASH_FOREACH_END();
}
add_index_zval(return_value, k, &zva);
}
return;
}
/* }}}*/
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(bonjour)
{
#if defined(ZTS) && defined(COMPILE_DL_BONJOUR)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(bonjour)
{
php_info_print_table_start();
php_info_print_table_header(2, "bonjour support", "enabled");
php_info_print_table_end();
}
/* }}} */
/* {{{ arginfo
*/
ZEND_BEGIN_ARG_INFO(arginfo_bonjour_test1, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_bonjour_test2, 0)
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_bonjour_array1_return, IS_STRING, 0)
ZEND_ARG_ARRAY_INFO(0, arg1, 0)
ZEND_ARG_ARRAY_INFO(0, arg2, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_bonjour_array2_return, IS_ARRAY, 0)
ZEND_ARG_ARRAY_INFO(0, arg1, 0)
ZEND_ARG_TYPE_INFO(0, arg2, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, associative, _IS_BOOL, 0)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ bonjour_functions[]
*/
static const zend_function_entry bonjour_functions[] = {
PHP_FE(bonjour_test1, arginfo_bonjour_test1)
PHP_FE(bonjour_test2, arginfo_bonjour_test2)
PHP_FE(bonjour_array1, arginfo_bonjour_array1_return)
PHP_FE(bonjour_array2, arginfo_bonjour_array2_return)
PHP_FE_END
};
/* }}} */
/* {{{ bonjour_module_entry
*/
zend_module_entry bonjour_module_entry = {
STANDARD_MODULE_HEADER,
"bonjour", /* Extension name */
bonjour_functions, /* zend_function_entry */
NULL, /* PHP_MINIT - Module initialization */
NULL, /* PHP_MSHUTDOWN - Module shutdown */
PHP_RINIT(bonjour), /* PHP_RINIT - Request initialization */
NULL, /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(bonjour), /* PHP_MINFO - Module info */
PHP_BONJOUR_VERSION, /* Version */
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_BONJOUR
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(bonjour)
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/