-
Notifications
You must be signed in to change notification settings - Fork 147
/
mosquitto_message.c
339 lines (281 loc) · 10.1 KB
/
mosquitto_message.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "zend_variables.h"
#include "zend_exceptions.h"
#include "zend_API.h"
#include "ext/standard/info.h"
#include "php_mosquitto.h"
zend_class_entry *mosquitto_ce_message;
static zend_object_handlers mosquitto_message_object_handlers;
static HashTable php_mosquitto_message_properties;
PHP_METHOD(Mosquitto_Message, __construct)
{
mosquitto_message_object *object;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters_none() == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
object = (mosquitto_message_object *) zend_object_store_get_object(getThis() TSRMLS_CC);
}
PHP_MOSQUITTO_MESSAGE_LONG_PROPERTY_READER_FUNCTION(mid);
PHP_MOSQUITTO_MESSAGE_LONG_PROPERTY_READER_FUNCTION(qos);
static int php_mosquitto_message_read_retain(mosquitto_message_object *mosquitto_object, zval **retval TSRMLS_DC)
{
MAKE_STD_ZVAL(*retval);
ZVAL_BOOL(*retval, mosquitto_object->message.retain);
return SUCCESS;
}
static int php_mosquitto_message_read_topic(mosquitto_message_object *mosquitto_object, zval **retval TSRMLS_DC)
{
MAKE_STD_ZVAL(*retval);
ZVAL_STRINGL(*retval, mosquitto_object->message.topic, strlen(mosquitto_object->message.topic), 1);
return SUCCESS;
}
static int php_mosquitto_message_read_payload(mosquitto_message_object *mosquitto_object, zval **retval TSRMLS_DC)
{
MAKE_STD_ZVAL(*retval);
ZVAL_STRINGL(*retval, mosquitto_object->message.payload, mosquitto_object->message.payloadlen, 1);
return SUCCESS;
}
PHP_MOSQUITTO_MESSAGE_LONG_PROPERTY_WRITER_FUNCTION(mid);
PHP_MOSQUITTO_MESSAGE_LONG_PROPERTY_WRITER_FUNCTION(qos);
static int php_mosquitto_message_write_retain(mosquitto_message_object *mosquitto_object, zval *newval TSRMLS_DC)
{
zval ztmp;
if (Z_TYPE_P(newval) != IS_BOOL) {
ztmp = *newval;
zval_copy_ctor(&ztmp);
convert_to_boolean(&ztmp);
newval = &ztmp;
}
mosquitto_object->message.retain = Z_LVAL_P(newval);
if (newval == &ztmp) {
zval_dtor(newval);
}
return SUCCESS;
}
static int php_mosquitto_message_write_topic(mosquitto_message_object *mosquitto_object, zval *newval TSRMLS_DC)
{
zval ztmp;
if (Z_TYPE_P(newval) != IS_STRING) {
ztmp = *newval;
zval_copy_ctor(&ztmp);
convert_to_string(&ztmp);
newval = &ztmp;
}
mosquitto_object->message.topic = estrdup(Z_STRVAL_P(newval));
mosquitto_object->owned_topic = 1;
if (newval == &ztmp) {
zval_dtor(newval);
}
return SUCCESS;
}
static int php_mosquitto_message_write_payload(mosquitto_message_object *mosquitto_object, zval *newval TSRMLS_DC)
{
zval ztmp;
if (Z_TYPE_P(newval) != IS_STRING) {
ztmp = *newval;
zval_copy_ctor(&ztmp);
convert_to_string(&ztmp);
newval = &ztmp;
}
mosquitto_object->message.payload = estrdup(Z_STRVAL_P(newval));
mosquitto_object->message.payloadlen = Z_STRLEN_P(newval);
mosquitto_object->owned_payload = 1;
if (newval == &ztmp) {
zval_dtor(newval);
}
return SUCCESS;
}
const php_mosquitto_prop_handler php_mosquitto_message_property_entries[] = {
PHP_MOSQUITTO_MESSAGE_PROPERTY_ENTRY_RECORD(mid),
PHP_MOSQUITTO_MESSAGE_PROPERTY_ENTRY_RECORD(topic),
PHP_MOSQUITTO_MESSAGE_PROPERTY_ENTRY_RECORD(payload),
PHP_MOSQUITTO_MESSAGE_PROPERTY_ENTRY_RECORD(qos),
PHP_MOSQUITTO_MESSAGE_PROPERTY_ENTRY_RECORD(retain),
{NULL, 0, NULL, NULL}
};
zval *php_mosquitto_message_read_property(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC)
{
zval tmp_member;
zval *retval;
mosquitto_message_object *message_object;
php_mosquitto_prop_handler *hnd;
int ret;
message_object = (mosquitto_message_object *) zend_object_store_get_object(object TSRMLS_CC);
if (Z_TYPE_P(member) != IS_STRING) {
tmp_member = *member;
zval_copy_ctor(&tmp_member);
convert_to_string(&tmp_member);
member = &tmp_member;
}
ret = zend_hash_find(&php_mosquitto_message_properties, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void **) &hnd);
if (ret == SUCCESS && hnd->read_func) {
ret = hnd->read_func(message_object, &retval TSRMLS_CC);
if (ret == SUCCESS) {
/* ensure we're creating a temporary variable */
Z_SET_REFCOUNT_P(retval, 0);
} else {
retval = EG(uninitialized_zval_ptr);
}
} else {
zend_object_handlers * std_hnd = zend_get_std_object_handlers();
retval = std_hnd->read_property(object, member, type, key TSRMLS_CC);
}
if (member == &tmp_member) {
zval_dtor(member);
}
return(retval);
}
void php_mosquitto_message_write_property(zval *object, zval *member, zval *value, const zend_literal *key TSRMLS_DC)
{
zval tmp_member;
mosquitto_message_object *obj;
php_mosquitto_prop_handler *hnd;
int ret;
if (Z_TYPE_P(member) != IS_STRING) {
tmp_member = *member;
zval_copy_ctor(&tmp_member);
convert_to_string(&tmp_member);
member = &tmp_member;
}
ret = FAILURE;
obj = (mosquitto_message_object *)zend_objects_get_address(object TSRMLS_CC);
ret = zend_hash_find(&php_mosquitto_message_properties, Z_STRVAL_P(member), Z_STRLEN_P(member) + 1, (void **) &hnd);
if (ret == SUCCESS && hnd->write_func) {
hnd->write_func(obj, value TSRMLS_CC);
if (! PZVAL_IS_REF(value) && Z_REFCOUNT_P(value) == 0) {
Z_ADDREF_P(value);
zval_ptr_dtor(&value);
}
} else {
zend_object_handlers * std_hnd = zend_get_std_object_handlers();
std_hnd->write_property(object, member, value, key TSRMLS_CC);
}
if (member == &tmp_member) {
zval_dtor(member);
}
}
static int php_mosquitto_message_has_property(zval *object, zval *member, int has_set_exists, const zend_literal *key TSRMLS_DC)
{
php_mosquitto_prop_handler *hnd;
int ret = 0;
if (zend_hash_find(&php_mosquitto_message_properties, Z_STRVAL_P(member), Z_STRLEN_P(member) + 1, (void **)&hnd) == SUCCESS) {
switch (has_set_exists) {
case 2:
ret = 1;
break;
case 0: {
zval *value = php_mosquitto_message_read_property(object, member, BP_VAR_IS, key TSRMLS_CC);
if (value != EG(uninitialized_zval_ptr)) {
ret = Z_TYPE_P(value) != IS_NULL? 1:0;
/* refcount is 0 */
Z_ADDREF_P(value);
zval_ptr_dtor(&value);
}
break;
}
default: {
zval *value = php_mosquitto_message_read_property(object, member, BP_VAR_IS, key TSRMLS_CC);
if (value != EG(uninitialized_zval_ptr)) {
convert_to_boolean(value);
ret = Z_BVAL_P(value)? 1:0;
/* refcount is 0 */
Z_ADDREF_P(value);
zval_ptr_dtor(&value);
}
break;
}
}
} else {
zend_object_handlers * std_hnd = zend_get_std_object_handlers();
ret = std_hnd->has_property(object, member, has_set_exists, key TSRMLS_CC);
}
return ret;
}
static HashTable *php_mosquitto_message_get_properties(zval *object TSRMLS_DC)
{
mosquitto_message_object *obj;
php_mosquitto_prop_handler *hnd;
HashTable *props;
zval *val;
char *key;
uint key_len;
HashPosition pos;
ulong num_key;
obj = (mosquitto_message_object *)zend_objects_get_address(object TSRMLS_CC);
props = zend_std_get_properties(object TSRMLS_CC);
zend_hash_internal_pointer_reset_ex(&php_mosquitto_message_properties, &pos);
while (zend_hash_get_current_data_ex(&php_mosquitto_message_properties, (void**)&hnd, &pos) == SUCCESS) {
zend_hash_get_current_key_ex(&php_mosquitto_message_properties, &key, &key_len, &num_key, 0, &pos);
if (!hnd->read_func || hnd->read_func(obj, &val TSRMLS_CC) != SUCCESS) {
val = EG(uninitialized_zval_ptr);
Z_ADDREF_P(val);
}
zend_hash_update(props, key, key_len, (void *)&val, sizeof(zval *), NULL);
zend_hash_move_forward_ex(&php_mosquitto_message_properties, &pos);
}
return obj->std.properties;
}
void php_mosquitto_message_add_property(HashTable *h, const char *name, size_t name_length, php_mosquitto_read_t read_func, php_mosquitto_write_t write_func TSRMLS_DC)
{
php_mosquitto_prop_handler p;
p.name = (char*) name;
p.name_length = name_length;
p.read_func = (read_func) ? read_func : NULL;
p.write_func = (write_func) ? write_func : NULL;
zend_hash_add(h, (char *)name, name_length + 1, &p, sizeof(php_mosquitto_prop_handler), NULL);
}
static void mosquitto_message_object_destroy(void *object TSRMLS_DC)
{
mosquitto_message_object *message = (mosquitto_message_object *) object;
zend_hash_destroy(message->std.properties);
FREE_HASHTABLE(message->std.properties);
if (message->owned_topic == 1) {
efree(message->message.topic);
}
if (message->owned_payload == 1) {
efree(message->message.payload);
}
efree(object);
}
static zend_object_value mosquitto_message_object_new() {
zend_object_value retval;
mosquitto_message_object *message_obj;
zval *temp;
message_obj = ecalloc(1, sizeof(mosquitto_message_object));
message_obj->std.ce = mosquitto_ce_message;
ALLOC_HASHTABLE(message_obj->std.properties);
zend_hash_init(message_obj->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
#if PHP_VERSION_ID < 50399
zend_hash_copy(message_obj->std.properties, &mosquitto_ce_message->default_properties, (copy_ctor_func_t) zval_add_ref,(void *) &temp, sizeof(zval *));
#else
object_properties_init(&message_obj->std, mosquitto_ce_message);
#endif
retval.handle = zend_objects_store_put(message_obj, NULL, (zend_objects_free_object_storage_t) mosquitto_message_object_destroy, NULL TSRMLS_CC);
retval.handlers = &mosquitto_message_object_handlers;
return retval;
}
const zend_function_entry mosquitto_message_methods[] = {
PHP_FE_END
};
PHP_MINIT_FUNCTION(mosquitto_message)
{
zend_class_entry message_ce;
memcpy(&mosquitto_message_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
mosquitto_message_object_handlers.read_property = php_mosquitto_message_read_property;
mosquitto_message_object_handlers.write_property = php_mosquitto_message_write_property;
mosquitto_message_object_handlers.has_property = php_mosquitto_message_has_property;
mosquitto_message_object_handlers.get_properties = php_mosquitto_message_get_properties;
INIT_NS_CLASS_ENTRY(message_ce, "Mosquitto", "Message", mosquitto_message_methods);
mosquitto_ce_message = zend_register_internal_class(&message_ce TSRMLS_CC);
mosquitto_ce_message->create_object = mosquitto_message_object_new;
zend_hash_init(&php_mosquitto_message_properties, 0, NULL, NULL, 1);
PHP_MOSQUITTO_ADD_PROPERTIES(&php_mosquitto_message_properties, php_mosquitto_message_property_entries);
return SUCCESS;
}