-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathmosquitto_message.c
580 lines (501 loc) · 16.3 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#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;
#ifdef ZEND_ENGINE_3
typedef size_t mosquitto_strlen_type;
#else
# ifndef Z_OBJ_P
# define Z_OBJ_P(pzv) ((zend_object*)zend_object_store_get_object(pzv TSRMLS_CC))
# endif
typedef int mosquitto_strlen_type;
#endif
/* {{{ Arginfo */
ZEND_BEGIN_ARG_INFO(Mosquitto_Message_topicMatchesSub_args, ZEND_SEND_BY_VAL)
ZEND_ARG_INFO(0, topic)
ZEND_ARG_INFO(0, subscription)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(Mosquitto_Message_tokeniseTopic_args, ZEND_SEND_BY_VAL)
ZEND_ARG_INFO(0, topic)
ZEND_END_ARG_INFO()
/* }}} */
PHP_METHOD(Mosquitto_Message, __construct)
{
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters_none() == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
}
/* {{{ Mosquitto\Message::topicMatchesSub() */
PHP_METHOD(Mosquitto_Message, topicMatchesSub)
{
char *topic = NULL, *subscription = NULL;
mosquitto_strlen_type topic_len, subscription_len;
zend_bool result;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss",
&topic, &topic_len, &subscription, &subscription_len) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
mosquitto_topic_matches_sub(subscription, topic, (bool *) &result);
RETURN_BOOL(result);
}
/* }}} */
/* {{{ Mosquitto\Message::tokeniseTopic() */
PHP_METHOD(Mosquitto_Message, tokeniseTopic)
{
char *topic = NULL, **topics = NULL;
mosquitto_strlen_type topic_len = 0, retval = 0, count = 0, i = 0;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &topic, &topic_len) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
retval = mosquitto_sub_topic_tokenise(topic, &topics, (int*)&count);
if (retval == MOSQ_ERR_NOMEM) {
zend_throw_exception_ex(mosquitto_ce_exception, 0 TSRMLS_CC, "Failed to tokenise topic");
return;
}
array_init(return_value);
for (i = 0; i < count; i++) {
if (topics[i] == NULL) {
add_next_index_null(return_value);
} else {
#ifdef ZEND_ENGINE_3
add_next_index_string(return_value, topics[i]);
#else
add_next_index_string(return_value, topics[i], 1);
#endif
}
}
mosquitto_sub_topic_tokens_free(&topics, count);
}
/* }}} */
PHP_MOSQUITTO_MESSAGE_LONG_PROPERTY_READER_FUNCTION(mid);
PHP_MOSQUITTO_MESSAGE_LONG_PROPERTY_READER_FUNCTION(qos);
#ifdef ZEND_ENGINE_3
static int php_mosquitto_message_read_retain(mosquitto_message_object *mosquitto_object, 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)
{
if (mosquitto_object->message.topic != NULL) {
ZVAL_STRING(retval, mosquitto_object->message.topic);
} else {
ZVAL_NULL(retval);
}
return SUCCESS;
}
static int php_mosquitto_message_read_payload(mosquitto_message_object *mosquitto_object, zval *retval)
{
ZVAL_STRINGL(retval, mosquitto_object->message.payload, mosquitto_object->message.payloadlen);
return SUCCESS;
}
#else
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);
if (mosquitto_object->message.topic != NULL) {
ZVAL_STRING(*retval, mosquitto_object->message.topic, 1);
} else {
ZVAL_NULL(*retval);
}
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;
}
#endif
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)
{
mosquitto_object->message.retain = zend_is_true(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;
}
if (mosquitto_object->message.topic && mosquitto_object->owned_topic) {
efree(mosquitto_object->message.topic);
}
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;
}
if (mosquitto_object->message.payload && mosquitto_object->owned_payload) {
efree(mosquitto_object->message.payload);
mosquitto_object->message.payloadlen = 0;
}
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}
};
#ifdef ZEND_ENGINE_3
# define READ_PROPERTY_DC , void **cache_slot, zval *retval
# define READ_PROPERTY_CC , cache_slot, retval
# define WRITE_PROPERTY_DC , void **cache_slot
# define WRITE_PROPERTY_CC , cache_slot
# define HAS_PROPERTY_DC , void **cache_slot
# define HAS_PROPERTY_CC , cache_slot
static php_mosquitto_prop_handler *mosquitto_get_prop_handler(zval *prop) {
zval *ret = zend_hash_find(&php_mosquitto_message_properties, Z_STR_P(prop));
if (!ret || Z_TYPE_P(ret) != IS_PTR) {
return NULL;
}
return (php_mosquitto_prop_handler*)Z_PTR_P(ret);
}
#else
# define READ_PROPERTY_DC ZEND_LITERAL_KEY_DC TSRMLS_DC
# define READ_PROPERTY_CC ZEND_LITERAL_KEY_CC TSRMLS_CC
# define WRITE_PROPERTY_DC ZEND_LITERAL_KEY_DC TSRMLS_DC
# define WRITE_PROPERTY_CC ZEND_LITERAL_KEY_CC TSRMLS_CC
# define HAS_PROPERTY_DC ZEND_LITERAL_KEY_DC TSRMLS_DC
# define HAS_PROPERTY_CC ZEND_LITERAL_KEY_CC TSRMLS_CC
static php_mosquitto_prop_handler *mosquitto_get_prop_handler(zval *prop) {
php_mosquitto_prop_handler *hnd;
if (FAILURE == zend_hash_find(&php_mosquitto_message_properties, Z_STRVAL_P(prop), Z_STRLEN_P(prop)+1, (void**) &hnd)) {
return NULL;
}
return hnd;
}
#endif
zval *php_mosquitto_message_read_property(zval *object, zval *member, int type READ_PROPERTY_DC) {
zval tmp_member;
#ifndef ZEND_ENGINE_3
zval *retval;
#endif
mosquitto_message_object *message_object = mosquitto_message_object_from_zend_object(Z_OBJ_P(object));
php_mosquitto_prop_handler *hnd;
if (Z_TYPE_P(member) != IS_STRING) {
tmp_member = *member;
zval_copy_ctor(&tmp_member);
convert_to_string(&tmp_member);
member = &tmp_member;
}
hnd = mosquitto_get_prop_handler(member);
if (hnd && hnd->read_func) {
#ifdef ZEND_ENGINE_3
if (FAILURE == hnd->read_func(message_object, retval)) {
ZVAL_NULL(retval);
}
#else
if (SUCCESS == hnd->read_func(message_object, &retval TSRMLS_CC)) {
/* ensure we're creating a temporary variable */
Z_SET_REFCOUNT_P(retval, 0);
} else {
retval = EG(uninitialized_zval_ptr);
}
#endif
} else {
zend_object_handlers * std_hnd = zend_get_std_object_handlers();
retval = std_hnd->read_property(object, member, type READ_PROPERTY_CC);
}
if (member == &tmp_member) {
zval_dtor(member);
}
return(retval);
}
void php_mosquitto_message_write_property(zval *object, zval *member, zval *value WRITE_PROPERTY_DC)
{
zval tmp_member;
mosquitto_message_object *obj = mosquitto_message_object_from_zend_object(Z_OBJ_P(object));
php_mosquitto_prop_handler *hnd;
if (Z_TYPE_P(member) != IS_STRING) {
tmp_member = *member;
zval_copy_ctor(&tmp_member);
convert_to_string(&tmp_member);
member = &tmp_member;
}
hnd = mosquitto_get_prop_handler(member);
if (hnd && hnd->write_func) {
hnd->write_func(obj, value TSRMLS_CC);
#ifdef ZEND_ENGINE_3
if (Z_REFCOUNTED_P(value)) {
Z_ADDREF_P(value);
zval_ptr_dtor(value);
}
#else
if (! PZVAL_IS_REF(value) && Z_REFCOUNT_P(value) == 0) {
Z_ADDREF_P(value);
zval_ptr_dtor(&value);
}
#endif
} else {
zend_object_handlers * std_hnd = zend_get_std_object_handlers();
std_hnd->write_property(object, member, value WRITE_PROPERTY_CC);
}
if (member == &tmp_member) {
zval_dtor(member);
}
}
static int php_mosquitto_message_has_property(zval *object, zval *member, int has_set_exists HAS_PROPERTY_DC)
{
php_mosquitto_prop_handler *hnd = mosquitto_get_prop_handler(member);
int ret = 0;
#ifdef ZEND_ENGINE_3
zval rv;
zval *retval = &rv;
#endif
if (hnd) {
switch (has_set_exists) {
case 2:
ret = 1;
break;
case 0: {
zval *value = php_mosquitto_message_read_property(object, member, BP_VAR_IS READ_PROPERTY_CC);
#ifdef ZEND_ENGINE_3
if (Z_REFCOUNTED_P(value)) {
Z_ADDREF_P(value);
zval_ptr_dtor(value);
}
#else
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);
}
#endif
break;
}
default: {
zval *value = php_mosquitto_message_read_property(object, member, BP_VAR_IS READ_PROPERTY_CC);
#ifdef ZEND_ENGINE_3
if (Z_REFCOUNTED_P(value)) {
Z_ADDREF_P(value);
zval_ptr_dtor(value);
}
#else
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);
}
#endif
break;
}
}
} else {
zend_object_handlers * std_hnd = zend_get_std_object_handlers();
ret = std_hnd->has_property(object, member, has_set_exists HAS_PROPERTY_CC);
}
return ret;
}
#ifndef ZEND_ENGINE_3
# ifndef ZEND_HASH_FOREACH_PTR
# define ZEND_HASH_FOREACH_KEY_PTR(ht, idx, key, ptr) \
{ \
HashPosition pos; \
for (zend_hash_internal_pointer_reset_ex(ht, &pos); \
zend_hash_get_current_data_ex(ht, (void**)&ptr, &pos) == SUCCESS; \
zend_hash_move_forward_ex(ht, &pos)) { \
key = NULL; \
zend_hash_get_current_key_ex(ht, &key, &key##_len, &idx, 0, &pos); \
{
# endif
# ifndef ZEND_HASH_FOREACH_END
# define ZEND_HASH_FOREACH_END() \
} \
} \
}
# endif
#endif
static HashTable *php_mosquitto_message_get_properties(zval *object TSRMLS_DC)
{
mosquitto_message_object *obj = mosquitto_message_object_from_zend_object(Z_OBJ_P(object));
php_mosquitto_prop_handler *hnd;
HashTable *props;
#ifdef ZEND_ENGINE_3
zend_string *key;
zend_long num_key;
#else
char *key;
uint key_len;
ulong num_key;
#endif
props = zend_std_get_properties(object TSRMLS_CC);
ZEND_HASH_FOREACH_KEY_PTR(&php_mosquitto_message_properties, num_key, key, hnd) {
#ifdef ZEND_ENGINE_3
zval val;
if (!hnd->read_func || (hnd->read_func(obj, &val) != SUCCESS)) {
ZVAL_NULL(&val);
}
if (key) {
zend_hash_update(props, key, &val);
} else {
zend_hash_index_update(props, num_key, &val);
}
#else
zval *val;
if (!hnd->read_func || hnd->read_func(obj, &val TSRMLS_CC) != SUCCESS) {
val = EG(uninitialized_zval_ptr);
Z_ADDREF_P(val);
}
if (key) {
zend_hash_update(props, key, key_len, (void *)&val, sizeof(zval*), NULL);
} else {
zend_hash_index_update(props, num_key, (void *)&val, sizeof(zval*), NULL);
}
#endif
} ZEND_HASH_FOREACH_END();
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)
{
#ifdef ZEND_ENGINE_3
php_mosquitto_prop_handler *p = (php_mosquitto_prop_handler*)pemalloc(sizeof(php_mosquitto_prop_handler), 1);
#else
php_mosquitto_prop_handler val, *p = &val;
#endif
p->name = (char*) name;
p->name_length = name_length;
p->read_func = read_func;
p->write_func = write_func;
#ifdef ZEND_ENGINE_3
{
zend_string *key = zend_string_init(name, name_length, 1);
zval hnd;
ZVAL_PTR(&hnd, p);
zend_hash_add(h, key, &hnd);
}
#else
zend_hash_add(h, (char *)name, name_length + 1, p, sizeof(php_mosquitto_prop_handler), NULL);
#endif
}
static void mosquitto_message_object_destroy(zend_object *object TSRMLS_DC)
{
mosquitto_message_object *message = mosquitto_message_object_from_zend_object(object);
#ifdef ZEND_ENGINE_3
zend_object_std_dtor(object);
#else
zend_hash_destroy(message->std.properties);
FREE_HASHTABLE(message->std.properties);
#endif
if (message->owned_topic == 1) {
efree(message->message.topic);
}
if (message->owned_payload == 1) {
efree(message->message.payload);
}
#ifndef ZEND_ENGINE_3
efree(object);
#endif
}
#ifdef ZEND_ENGINE_3
static zend_object *mosquitto_message_object_new(zend_class_entry *ce) {
mosquitto_message_object *msg = ecalloc(1, sizeof(mosquitto_message_object) + zend_object_properties_size(ce));
zend_object *ret = mosquitto_message_object_to_zend_object(msg);
#ifdef MOSQUITTO_NEED_TSRMLS
message_obj->TSRMLS_C = TSRMLS_C;
#endif
zend_object_std_init(ret, ce);
ret->handlers = &mosquitto_message_object_handlers;
return ret;
}
#else
static zend_object_value mosquitto_message_object_new(zend_class_entry *ce TSRMLS_DC) {
zend_object_value retval;
mosquitto_message_object *message_obj;
#if PHP_VERSION_ID < 50399
zval *temp;
#endif
message_obj = ecalloc(1, sizeof(mosquitto_message_object));
message_obj->std.ce = ce;
#ifdef MOSQUITTO_NEED_TSRMLS
message_obj->TSRMLS_C = TSRMLS_C;
#endif
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;
}
#endif
const zend_function_entry mosquitto_message_methods[] = {
PHP_ME(Mosquitto_Message, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(Mosquitto_Message, topicMatchesSub, Mosquitto_Message_topicMatchesSub_args, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Mosquitto_Message, tokeniseTopic, Mosquitto_Message_tokeniseTopic_args, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
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;
#ifdef ZEND_ENGINE_3
mosquitto_message_object_handlers.offset = XtOffsetOf(mosquitto_message_object, std);
mosquitto_message_object_handlers.free_obj = mosquitto_message_object_destroy;
#endif
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;
}