-
Notifications
You must be signed in to change notification settings - Fork 38
/
core.c
989 lines (816 loc) · 31 KB
/
core.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
#include "protocolbuffers.h"
#include "core.h"
#include "serializer.h"
#include "json_serializer.h"
#include "unknown_field.h"
#include "unknown_field_set.h"
static char *single_property_name_default = "_properties";
static int single_property_name_default_len = sizeof("_properties");
static char *unknown_property_name_default = "_unknown";
static int unknown_property_name_default_len = sizeof("_unknown");
const char* php_protocolbuffers_decode_message(INTERNAL_FUNCTION_PARAMETERS, const char *data, const char *data_end, php_protocolbuffers_scheme_container *container, int native_scalars, zval **result);
int php_protocolbuffers_read_protected_property(zval *instance, char *name, size_t name_len, zval **result TSRMLS_DC)
{
zval **tmp;
char *prop_name;
int prop_name_len = 0;
int retval = 0;
zend_mangle_property_name(&prop_name, &prop_name_len, (char*)"*", 1, name, name_len, 0);
if (zend_hash_find(Z_OBJPROP_P(instance), prop_name, prop_name_len, (void **)&tmp) == SUCCESS) {
*result = *tmp;
retval = 1;
}
efree(prop_name);
return retval;
}
int php_protocolbuffers_set_protected_property(zval *instance, char *name, size_t name_len, zval *value TSRMLS_DC)
{
char *prop_name;
int prop_name_len = 0;
zend_mangle_property_name(&prop_name, &prop_name_len, (char*)"*", 1, name, name_len, 0);
zend_hash_update(Z_OBJPROP_P(instance), prop_name, prop_name_len, (void **)&value, sizeof(zval*), NULL);
efree(prop_name);
return 1;
}
char *php_protocolbuffers_get_default_single_property_name()
{
return single_property_name_default;
}
int php_protocolbuffers_get_default_single_property_name_len()
{
return single_property_name_default_len;
}
char *php_protocolbuffers_get_default_unknown_property_name()
{
return unknown_property_name_default;
}
int php_protocolbuffers_get_default_unknown_property_name_len()
{
return unknown_property_name_default_len;
}
void php_protocolbuffers_scheme_container_init(php_protocolbuffers_scheme_container *container)
{
memset(container, '\0', sizeof(php_protocolbuffers_scheme_container));
container->size = 0;
container->scheme = NULL;
container->use_single_property = 0;
container->process_unknown_fields = 0;
container->use_wakeup_and_sleep = 0;
container->extension_cnt = 0;
container->extensions = NULL;
container->use_wakeup_and_sleep = 0;
container->single_property_h = 0;
container->orig_single_property_name = php_protocolbuffers_get_default_single_property_name();
container->orig_single_property_name_len = php_protocolbuffers_get_default_single_property_name_len();
zend_mangle_property_name(&container->single_property_name,
&container->single_property_name_len,
(char*)"*",
1,
(char*)container->orig_single_property_name,
container->orig_single_property_name_len,
0
);
container->single_property_h = zend_inline_hash_func(container->single_property_name, container->single_property_name_len);
}
int php_protocolbuffers_get_scheme_container(const char *klass, size_t klass_len, php_protocolbuffers_scheme_container **result TSRMLS_DC)
{
return php_protocolbuffers_get_scheme_container_ex(klass, klass_len, 1, result TSRMLS_CC);
}
int php_protocolbuffers_get_scheme_container_ex(const char *klass, size_t klass_len, int throws_exception, php_protocolbuffers_scheme_container **result TSRMLS_DC)
{
php_protocolbuffers_scheme_container *container, **cn;
if (zend_hash_find(PBG(messages), (char*)klass, klass_len, (void **)&cn) != SUCCESS) {
zval *ret = NULL;
zend_class_entry **ce = NULL;
if (zend_lookup_class((char*)klass, klass_len, &ce TSRMLS_CC) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "php_protocolbuffers_get_scheme_container failed. %s does find", klass);
return 1;
}
if (zend_call_method(NULL, *ce, NULL, ZEND_STRS("getdescriptor")-1, &ret, 0, NULL, NULL TSRMLS_CC)) {
if (Z_TYPE_P(ret) == IS_ARRAY) {
/* TODO(chobie): move this block after release. */
zval_ptr_dtor(&ret);
php_error_docref(NULL TSRMLS_CC, E_ERROR, "php_protocolbuffers_get_scheme_container no longer support array based descriptor");
return 1;
} else if (Z_TYPE_P(ret) == IS_OBJECT) {
zend_class_entry *entry;
php_protocolbuffers_descriptor *desc;
entry = Z_OBJCE_P(ret);
if (entry == php_protocol_buffers_descriptor_class_entry) {
desc = PHP_PROTOCOLBUFFERS_GET_OBJECT(php_protocolbuffers_descriptor, ret);
desc->free_container = 1;
zend_hash_add(PBG(messages), (char*)klass, klass_len, (void**)&desc->container, sizeof(php_protocolbuffers_scheme_container*), NULL);
} else {
zend_throw_exception_ex(php_protocol_buffers_invalid_protocolbuffers_exception_class_entry, 0 TSRMLS_CC, "getDescriptor returns unexpected class");
if (ret != NULL) {
zval_ptr_dtor(&ret);
}
return 1;
}
zval_ptr_dtor(&ret);
*result = desc->container;
return 0;
} else {
if (ret != NULL) {
zval_ptr_dtor(&ret);
}
zend_throw_exception_ex(php_protocol_buffers_invalid_protocolbuffers_exception_class_entry, 0 TSRMLS_CC, "getDescriptor returns unexpected class");
return 1;
}
} else {
if (EG(exception)) {
PHP_PROTOCOLBUFFERS_EXCEPTION_ERROR(EG(exception));
} else {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "php_protocolbuffers_get_scheme_container failed. %s does not have getDescriptor method", klass);
}
return 1;
}
} else {
container = *cn;
}
*result = container;
return 0;
}
void php_protocolbuffers_process_unknown_field(INTERNAL_FUNCTION_PARAMETERS, php_protocolbuffers_scheme_container *container, HashTable *hresult, zval *dz, int tag, int wiretype, int64_t value)
{
char *unknown_name = {0};
int unknown_name_len = 0;
zval **un = NULL;
unknown_value *val;
php_protocolbuffers_unknown_field *p = NULL;
if (container->use_single_property > 0) {
unknown_name = php_protocolbuffers_get_default_unknown_property_name();
unknown_name_len = php_protocolbuffers_get_default_unknown_property_name_len();
} else {
zend_mangle_property_name(&unknown_name, &unknown_name_len, (char*)"*", 1, (char*)php_protocolbuffers_get_default_unknown_property_name(), php_protocolbuffers_get_default_unknown_property_name_len(), 0);
}
if (zend_hash_find(hresult, (char*)unknown_name, unknown_name_len, (void **)&un) == SUCCESS) {
if (php_protocolbuffers_unknown_field_get_field(INTERNAL_FUNCTION_PARAM_PASSTHRU, *un, tag, unknown_name, unknown_name_len, &p)) {
val = (unknown_value*)emalloc(sizeof(val));
val->varint = value;
zend_hash_next_index_insert(p->ht, (void *)&val, sizeof(unknown_value), NULL);
} else {
object_init_ex(dz, php_protocol_buffers_unknown_field_class_entry);
php_protocolbuffers_unknown_field_properties_init(dz TSRMLS_CC);
php_protocolbuffers_unknown_field_set_number(dz, tag TSRMLS_CC);
php_protocolbuffers_unknown_field_set_type(dz, wiretype TSRMLS_CC);
p = PHP_PROTOCOLBUFFERS_GET_OBJECT(php_protocolbuffers_unknown_field, dz);
val = (unknown_value*)emalloc(sizeof(unknown_value));
val->varint = value;
zend_hash_next_index_insert(p->ht, (void *)&val, sizeof(unknown_value), NULL);
php_protocolbuffers_unknown_field_set_add_field(INTERNAL_FUNCTION_PARAM_PASSTHRU, *un, tag, dz);
}
}
if (container->use_single_property < 1) {
efree(unknown_name);
}
}
void php_protocolbuffers_process_unknown_field_bytes(INTERNAL_FUNCTION_PARAMETERS, php_protocolbuffers_scheme_container *container, HashTable *hresult, int tag, int wiretype, uint8_t *bytes, int length)
{
char *unknown_name = {0};
int unknown_name_len = 0;
zval *dz = NULL, **unknown_fieldset = NULL;
unknown_value *val = NULL;
php_protocolbuffers_unknown_field *p = NULL;
uint8_t *buffer = NULL;
if (container->use_single_property > 0) {
unknown_name = php_protocolbuffers_get_default_unknown_property_name();
unknown_name_len = php_protocolbuffers_get_default_unknown_property_name_len();
} else {
zend_mangle_property_name(&unknown_name, &unknown_name_len, (char*)"*", 1, (char*)php_protocolbuffers_get_default_unknown_property_name(), php_protocolbuffers_get_default_unknown_property_name_len(), 0);
}
if (zend_hash_find(hresult, (char*)unknown_name, unknown_name_len, (void **)&unknown_fieldset) == SUCCESS) {
if (Z_OBJCE_PP(unknown_fieldset) != php_protocol_buffers_unknown_field_set_class_entry) {
return;
}
if (php_protocolbuffers_unknown_field_get_field(INTERNAL_FUNCTION_PARAM_PASSTHRU, *unknown_fieldset, tag, unknown_name, unknown_name_len, &p)) {
val = (unknown_value*)emalloc(sizeof(unknown_value));
buffer = (uint8_t*)emalloc(length);
memcpy(buffer, bytes, length);
val->buffer.val = buffer;
val->buffer.len = length;
zend_hash_next_index_insert(p->ht, (void *)&val, sizeof(val), NULL);
} else {
MAKE_STD_ZVAL(dz);
object_init_ex(dz, php_protocol_buffers_unknown_field_class_entry);
php_protocolbuffers_unknown_field_properties_init(dz TSRMLS_CC);
php_protocolbuffers_unknown_field_set_number(dz, tag TSRMLS_CC);
php_protocolbuffers_unknown_field_set_type(dz, wiretype TSRMLS_CC);
p = PHP_PROTOCOLBUFFERS_GET_OBJECT(php_protocolbuffers_unknown_field, dz);
val = (unknown_value*)emalloc(sizeof(unknown_value));
buffer = (uint8_t*)emalloc(length);
memcpy(buffer, bytes, length);
val->buffer.val = buffer;
val->buffer.len = length;
zend_hash_next_index_insert(p->ht, (void *)&val, sizeof(val), NULL);
php_protocolbuffers_unknown_field_set_add_field(INTERNAL_FUNCTION_PARAM_PASSTHRU, *unknown_fieldset, tag, dz);
}
}
if (container->use_single_property < 1) {
efree(unknown_name);
}
}
void php_protocolbuffers_format_string(zval *result, pbf *payload, int native_scalars TSRMLS_DC)
{
char __buffer[64] = {0};
char *buffer = __buffer;
int free = 0;
size_t size = 0;
if (native_scalars == 1) {
switch (payload->type) {
case TYPE_SINT32:
case TYPE_INT32:
ZVAL_LONG(result, payload->value.int32);
break;
case TYPE_UINT32:
ZVAL_LONG(result, payload->value.uint32);
break;
case TYPE_UINT64:
ZVAL_LONG(result, payload->value.uint64);
break;
case TYPE_SINT64:
case TYPE_INT64:
ZVAL_LONG(result, payload->value.int64);
break;
break;
case TYPE_FLOAT:
ZVAL_DOUBLE(result, payload->value.f);
break;
case TYPE_DOUBLE:
ZVAL_DOUBLE(result, payload->value.d);
break;
break;
default:
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "the type %d does not support. maybe this is bug", payload->type);
return;
break;
}
} else {
switch (payload->type) {
case TYPE_SINT32:
case TYPE_INT32:
size = snprintf(buffer, sizeof(__buffer), "%d", payload->value.int32);
break;
case TYPE_UINT32:
size = snprintf(buffer, sizeof(__buffer), "%u", payload->value.uint32);
break;
case TYPE_UINT64:
size = snprintf(buffer, sizeof(__buffer), "%llu" , payload->value.uint64);
break;
case TYPE_SINT64:
case TYPE_INT64:
size = snprintf(buffer, sizeof(__buffer), "%lld" , payload->value.int64);
break;
case TYPE_FLOAT:{
char *p = 0;
free = 1;
/* Note: this is safe */
buffer = emalloc(MAX_LENGTH_OF_DOUBLE + EG(precision) + 1);
size = zend_sprintf(buffer, "%f", payload->value.f);
// remove trailing zeros
p = strchr(buffer,'.');
if(p != NULL) {
while(*++p);
while('0' == *--p){
*p = '\0';
size--;
}
if(*p == '.'){
*p = '\0';
size--;
}
break;
}
}
break;
case TYPE_DOUBLE:{
free = 1;
/* Note: this is safe */
buffer = emalloc(MAX_LENGTH_OF_DOUBLE + EG(precision) + 1);
size = zend_sprintf(buffer, "%.*G", (int)EG(precision), payload->value.d);
}
break;
default:
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "the type %d does not support. maybe this is bug", payload->type);
return;
break;
}
if (buffer != NULL) {
ZVAL_STRINGL(result, buffer, size, 1);
}
if (free) {
efree(buffer);
}
}
}
const char* php_protocolbuffers_decode_message(INTERNAL_FUNCTION_PARAMETERS, const char *data, const char *data_end, php_protocolbuffers_scheme_container *container, int native_scalars, zval **result)
{
uint32_t payload = 0, tag = 0, wiretype = 0;
uint64_t value = 0;
zval *dz = NULL;
HashTable *hresult;
pbf __payload = {0};
php_protocolbuffers_scheme *prior_scheme = NULL;
if (container->use_single_property > 0) {
zval **tmp = NULL;
if (zend_hash_quick_find(Z_OBJPROP_PP(result), container->single_property_name, container->single_property_name_len+1, container->single_property_h, (void **)&tmp) == SUCCESS) {
if (Z_TYPE_PP(tmp) != IS_ARRAY) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "single property is not an array.");
return NULL;
}
hresult = Z_ARRVAL_PP(tmp);
} else {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "the class does not have property.");
return NULL;
}
} else {
hresult = Z_OBJPROP_PP(result);
}
while (data < data_end) {
php_protocolbuffers_scheme *s = NULL;
data = ReadVarint32FromArray(data, &payload, data_end);
if (data == NULL) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "php_protocolbuffers_decode_message failed. ReadVarint32FromArray returns NULL.");
return NULL;
}
tag = (payload >> 0x03);
wiretype = (payload & 0x07);
if (tag < 1 || tag > ktagmax) {
return NULL;
}
if (wiretype > WIRETYPE_FIXED32) {
return NULL;
}
s = php_protocolbuffers_search_scheme_by_tag(container->scheme, container->size, tag);
switch (wiretype) {
case WIRETYPE_VARINT:
{
data = ReadVarint64FromArray(data, &value, data_end);
if (!php_protocolbuffers_process_varint(INTERNAL_FUNCTION_PARAM_PASSTHRU, wiretype, tag, container, s, value, hresult, native_scalars)) {
return NULL;
}
}
break;
case WIRETYPE_FIXED64:
if (!php_protocolbuffers_process_fixed64(INTERNAL_FUNCTION_PARAM_PASSTHRU, wiretype, tag, container, s, data, hresult, native_scalars)) {
return NULL;
}
data += 8;
break;
case WIRETYPE_LENGTH_DELIMITED:
data = ReadVarint32FromArray(data, &payload, data_end);
if ((data+payload) > data_end) {
return NULL;
}
if (s == NULL) {
if (container->process_unknown_fields > 0) {
php_protocolbuffers_process_unknown_field_bytes(INTERNAL_FUNCTION_PARAM_PASSTHRU, container, hresult, tag, wiretype, (uint8_t*)data, payload);
} else {
/* skip unknown field */
}
} else if (s->type == TYPE_STRING) {
MAKE_STD_ZVAL(dz);
ZVAL_STRINGL(dz, (char*)data, payload, 1);
php_protocolbuffers_decode_add_value_and_consider_repeated(container, s, hresult, dz TSRMLS_CC);
} else if (s->type == TYPE_BYTES) {
MAKE_STD_ZVAL(dz);
ZVAL_STRINGL(dz, (char*)data, payload, 1);
php_protocolbuffers_decode_add_value_and_consider_repeated(container, s, hresult, dz TSRMLS_CC);
} else if (s->type == TYPE_MESSAGE) {
const char *n_buffer_end = data + payload;
zval *z_obj = NULL;
php_protocolbuffers_scheme_container *c_container = NULL;
char *name = {0};
int name_length = 0;
ulong name_hash = 0;
php_protocolbuffers_get_scheme_container(s->ce->name, s->ce->name_length, &c_container TSRMLS_CC);
if (container->use_single_property < 1) {
name = s->mangled_name;
name_length = s->mangled_name_len;
name_hash = s->mangled_name_h;
} else {
name = s->name;
name_length = s->name_len;
name_hash = s->name_h;
}
if (prior_scheme != NULL && prior_scheme == s && !s->repeated) {
/* NOTE: some protobuf implementation will split child message into each filed. */
zval **tt;
if (zend_hash_quick_find(hresult, name, name_length, name_hash, (void **)&tt) == SUCCESS) {
z_obj = *tt;
Z_ADDREF_P(z_obj);
} else {
MAKE_STD_ZVAL(z_obj);
object_init_ex(z_obj, s->ce);
php_protocolbuffers_properties_init(z_obj, s->ce TSRMLS_CC);
}
} else {
MAKE_STD_ZVAL(z_obj);
object_init_ex(z_obj, s->ce);
php_protocolbuffers_properties_init(z_obj, s->ce TSRMLS_CC);
if (instanceof_function_ex(s->ce, php_protocol_buffers_message_class_entry, 0 TSRMLS_CC)) {
php_protocolbuffers_message *m;
m = PHP_PROTOCOLBUFFERS_GET_OBJECT(php_protocolbuffers_message, z_obj);
// point to parent.
ZVAL_ZVAL(m->container, *result, 0, 0);
}
}
php_protocolbuffers_decode_message(INTERNAL_FUNCTION_PARAM_PASSTHRU, data, n_buffer_end, c_container, PBG(native_scalars), &z_obj);
if (c_container->use_wakeup_and_sleep > 0) {
php_protocolbuffers_execute_wakeup(z_obj, c_container TSRMLS_CC);
}
if (s->repeated) {
if (!zend_hash_quick_exists(hresult, name, name_length, name_hash)) {
zval *arr = NULL;
MAKE_STD_ZVAL(arr);
array_init(arr);
Z_ADDREF_P(z_obj);
zend_hash_next_index_insert(Z_ARRVAL_P(arr), (void *)&z_obj, sizeof(z_obj), NULL);
Z_ADDREF_P(arr);
zend_hash_update(hresult, name, name_length, (void **)&arr, sizeof(arr), NULL);
zval_ptr_dtor(&arr);
} else {
zval **arr2 = NULL;
if (zend_hash_quick_find(hresult, name, name_length, name_hash, (void **)&arr2) == SUCCESS) {
Z_ADDREF_P(z_obj);
zend_hash_next_index_insert(Z_ARRVAL_PP(arr2), (void *)&z_obj, sizeof(zval *), NULL);
}
}
} else {
zend_hash_quick_update(hresult, name, name_length, name_hash, (void **)&z_obj, sizeof(zval *), NULL);
Z_ADDREF_P(z_obj);
}
zval_ptr_dtor(&z_obj);
} else if (s->packed) {
const char *packed_data_end, *last_data_offset;
last_data_offset = data;
packed_data_end = data + payload;
do {
switch (s->type) {
case TYPE_DOUBLE:
{
uint64_t _v;
double d;
memcpy(&_v, data, 8);
d = decode_double(_v);
MAKE_STD_ZVAL(dz);
__payload.type = TYPE_DOUBLE;__payload.value.d = d;
php_protocolbuffers_format_string(dz, &__payload, native_scalars TSRMLS_CC);
php_protocolbuffers_decode_add_value_and_consider_repeated(container, s, hresult, dz TSRMLS_CC);
data += 8;
}
break;
case TYPE_FLOAT:
{
uint32_t _v;
float a = 0;
memcpy(&_v, data, 4);
a = decode_float(_v);
MAKE_STD_ZVAL(dz);
__payload.type = TYPE_DOUBLE;__payload.value.d = a;
php_protocolbuffers_format_string(dz, &__payload, native_scalars TSRMLS_CC);
php_protocolbuffers_decode_add_value_and_consider_repeated(container, s, hresult, dz TSRMLS_CC);
data += 4;
}
break;
case TYPE_INT64:
{
uint64_t v2;
data = ReadVarint64FromArray(data, &v2, data_end);
MAKE_STD_ZVAL(dz);
__payload.type = TYPE_INT64;__payload.value.int64 = v2;
php_protocolbuffers_format_string(dz, &__payload, native_scalars TSRMLS_CC);
php_protocolbuffers_decode_add_value_and_consider_repeated(container, s, hresult, dz TSRMLS_CC);
}
break;
case TYPE_UINT64:
{
uint64_t v2;
data = ReadVarint64FromArray(data, &v2, data_end);
MAKE_STD_ZVAL(dz);
__payload.type = TYPE_UINT64;__payload.value.uint64 = v2;
php_protocolbuffers_format_string(dz, &__payload, native_scalars TSRMLS_CC);
php_protocolbuffers_decode_add_value_and_consider_repeated(container, s, hresult, dz TSRMLS_CC);
}
break;
break;
case TYPE_INT32:
data = ReadVarint32FromArray(data, &payload, data_end);
MAKE_STD_ZVAL(dz);
__payload.type = TYPE_INT32;__payload.value.int32 = (int32_t)payload;
php_protocolbuffers_format_string(dz, &__payload, native_scalars TSRMLS_CC);
php_protocolbuffers_decode_add_value_and_consider_repeated(container, s, hresult, dz TSRMLS_CC);
break;
case TYPE_FIXED64:
{
uint64_t l;
memcpy(&l, data, 8);
MAKE_STD_ZVAL(dz);
__payload.type = TYPE_UINT64;__payload.value.uint64 = l;
php_protocolbuffers_format_string(dz, &__payload, native_scalars TSRMLS_CC);
php_protocolbuffers_decode_add_value_and_consider_repeated(container, s, hresult, dz TSRMLS_CC);
data += 8;
}
break;
case TYPE_FIXED32:
{
uint32_t l = 0;
memcpy(&l, data, 4);
MAKE_STD_ZVAL(dz);
__payload.type = TYPE_UINT32;__payload.value.uint32 = l;
php_protocolbuffers_format_string(dz, &__payload, native_scalars TSRMLS_CC);
php_protocolbuffers_decode_add_value_and_consider_repeated(container, s, hresult, dz TSRMLS_CC);
data += 4;
}
break;
case TYPE_BOOL:
data = ReadVarint32FromArray(data, &payload, data_end);
MAKE_STD_ZVAL(dz);
ZVAL_BOOL(dz, payload);
php_protocolbuffers_decode_add_value_and_consider_repeated(container, s, hresult, dz TSRMLS_CC);
break;
case TYPE_UINT32:
data = ReadVarint32FromArray(data, &payload, data_end);
MAKE_STD_ZVAL(dz);
__payload.type = TYPE_UINT32;__payload.value.uint32 = (uint32_t)payload;
php_protocolbuffers_format_string(dz, &__payload, native_scalars TSRMLS_CC);
php_protocolbuffers_decode_add_value_and_consider_repeated(container, s, hresult, dz TSRMLS_CC);
break;
case TYPE_ENUM:
data = ReadVarint32FromArray(data, &payload, data_end);
MAKE_STD_ZVAL(dz);
ZVAL_LONG(dz, (int32_t)payload);
php_protocolbuffers_decode_add_value_and_consider_repeated(container, s, hresult, dz TSRMLS_CC);
break;
case TYPE_SFIXED32:
{
int32_t l = 0;
memcpy(&l, data, 4);
MAKE_STD_ZVAL(dz);
__payload.type = TYPE_INT32;__payload.value.int32 = l;
php_protocolbuffers_format_string(dz, &__payload, native_scalars TSRMLS_CC);
php_protocolbuffers_decode_add_value_and_consider_repeated(container, s, hresult, dz TSRMLS_CC);
data += 4;
}
break;
case TYPE_SFIXED64:
{
int64_t l;
memcpy(&l, data, 8);
MAKE_STD_ZVAL(dz);
__payload.type = TYPE_INT64;__payload.value.int64 = l;
php_protocolbuffers_format_string(dz, &__payload, native_scalars TSRMLS_CC);
php_protocolbuffers_decode_add_value_and_consider_repeated(container, s, hresult, dz TSRMLS_CC);
data += 8;
}
break;
case TYPE_SINT32:
data = ReadVarint32FromArray(data, &payload, data_end);
MAKE_STD_ZVAL(dz);
__payload.type = TYPE_INT32;__payload.value.int32 = (int32_t)zigzag_decode32(payload);
php_protocolbuffers_format_string(dz, &__payload, native_scalars TSRMLS_CC);
php_protocolbuffers_decode_add_value_and_consider_repeated(container, s, hresult, dz TSRMLS_CC);
break;
case TYPE_SINT64:
{
uint64_t v2;
data = ReadVarint64FromArray(data, &v2, data_end);
MAKE_STD_ZVAL(dz);
__payload.type = TYPE_SINT64;__payload.value.int64 = (int64_t)zigzag_decode64(v2);
php_protocolbuffers_format_string(dz, &__payload, native_scalars TSRMLS_CC);
php_protocolbuffers_decode_add_value_and_consider_repeated(container, s, hresult, dz TSRMLS_CC);
}
break;
}
if (last_data_offset == data) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "php_protocolbuffers_decode_message: detect infinite loop when processing repeated packed field.");
break;
}
last_data_offset = data;
} while(data < packed_data_end);
break;
}
data += payload;
break;
case WIRETYPE_FIXED32: {
if (!php_protocolbuffers_process_fixed32(INTERNAL_FUNCTION_PARAM_PASSTHRU, wiretype, tag, container, s, data, hresult, native_scalars)) {
return NULL;
}
data += 4;
}
break;
}
prior_scheme = s;
}
return data;
}
int php_protocolbuffers_jsonserialize(INTERNAL_FUNCTION_PARAMETERS, int throws_exception, zend_class_entry *ce, zval *klass, zval **result)
{
int err = 0;
zval *tmp = NULL;
php_protocolbuffers_scheme_container *container;
err = php_protocolbuffers_get_scheme_container_ex(ce->name, ce->name_length, throws_exception, &container TSRMLS_CC);
if (err) {
if (EG(exception)) {
return err;
} else {
php_protocolbuffers_raise_error_or_exception(php_protocolbuffers_get_exception_base(TSRMLS_C), E_ERROR, throws_exception, "php_protocolbuffers_get_scheme_container failed. %s does not have getDescriptor method", ce->name);
return err;
}
}
MAKE_STD_ZVAL(tmp);
array_init(tmp);
if (php_protocolbuffers_encode_jsonserialize(klass, container, throws_exception, &tmp TSRMLS_CC) != 0) {
zval_ptr_dtor(&tmp);
return 1;
}
*result = tmp;
return err;
}
int php_protocolbuffers_encode(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *ce, zval *klass)
{
int err = 0;
php_protocolbuffers_serializer *ser = NULL;
php_protocolbuffers_scheme_container *container;
err = php_protocolbuffers_get_scheme_container(ce->name, ce->name_length, &container TSRMLS_CC);
if (err) {
if (EG(exception)) {
return 1;
} else {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "php_protocolbuffers_get_scheme_container failed. %s does not have getDescriptor method", ce->name);
return 1;
}
}
if (php_protocolbuffers_encode_message(INTERNAL_FUNCTION_PARAM_PASSTHRU, klass, container, &ser)) {
return 1;
}
if (ser == NULL) {
return 1;
}
if (ser->buffer_size > 0) {
RETVAL_STRINGL((char*)ser->buffer, ser->buffer_size, 1);
}
php_protocolbuffers_serializer_destroy(ser);
return 0;
}
int php_protocolbuffers_decode(INTERNAL_FUNCTION_PARAMETERS, const char *data, int data_len, const char *klass, int klass_len)
{
zval *obj = NULL;
php_protocolbuffers_scheme_container *container;
const char *data_end, *res;
int err = 0;
zend_class_entry **ce = NULL;
if (data_len < 1) {
zend_throw_exception_ex(php_protocol_buffers_invalid_protocolbuffers_exception_class_entry, 0 TSRMLS_CC, "passed variable seems null");
return 1;
}
err = php_protocolbuffers_get_scheme_container(klass, klass_len, &container TSRMLS_CC);
if (err) {
if (EG(exception)) {
// do nothing
} else {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "php_protocolbuffers_get_scheme_container failed. %s does not have getDescriptor method", klass);
}
return 1;
}
data_end = data + data_len;
if (PBG(classes)) {
/* Memo: fast lookup */
if (zend_hash_find(PBG(classes), (char*)klass, klass_len, (void **)&ce) == FAILURE) {
zend_lookup_class((char*)klass, klass_len, &ce TSRMLS_CC);
if (ce != NULL) {
zend_hash_update(PBG(classes), (char*)klass, klass_len, (void **)ce, sizeof(ce), NULL);
} else {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "class lookup failed. %s does exist", klass);
return 1;
}
}
}
MAKE_STD_ZVAL(obj);
object_init_ex(obj, *ce);
php_protocolbuffers_properties_init(obj, *ce TSRMLS_CC);
/* add unknown fields */
if (container->process_unknown_fields > 0) {
zval **un = NULL, *unknown = NULL;
char *unknown_name = {0};
int unknown_name_len = 0;
MAKE_STD_ZVAL(unknown);
object_init_ex(unknown, php_protocol_buffers_unknown_field_set_class_entry);
php_protocolbuffers_unknown_field_set_properties_init(unknown TSRMLS_CC);
zend_mangle_property_name(&unknown_name, &unknown_name_len, (char*)"*", 1, (char*)"_unknown", sizeof("_unknown"), 0);
if (zend_hash_find(Z_OBJPROP_P(obj), (char*)unknown_name, unknown_name_len, (void **)&un) == FAILURE) {
zend_hash_update(Z_OBJPROP_P(obj), unknown_name, unknown_name_len, (void **)&unknown, sizeof(unknown), NULL);
} else {
if (Z_TYPE_PP(un) == IS_NULL) {
zend_hash_update(Z_OBJPROP_P(obj), unknown_name, unknown_name_len, (void **)&unknown, sizeof(unknown), NULL);
} else {
efree(unknown);
}
}
efree(unknown_name);
}
res = php_protocolbuffers_decode_message(INTERNAL_FUNCTION_PARAM_PASSTHRU, data, data_end, container, PBG(native_scalars), &obj);
if (res == NULL) {
if (obj != NULL) {
zval_ptr_dtor(&obj);
}
zend_throw_exception_ex(php_protocol_buffers_invalid_protocolbuffers_exception_class_entry, 0 TSRMLS_CC, "passed variable contains malformed byte sequence. or it contains unsupported tag");
return 0;
}
if (container->use_wakeup_and_sleep > 0) {
php_protocolbuffers_execute_wakeup(obj, container TSRMLS_CC);
}
RETVAL_ZVAL(obj, 0, 1);
return 0;
}
void php_protocolbuffers_execute_wakeup(zval *obj, php_protocolbuffers_scheme_container *container TSRMLS_DC)
{
zval fname, *retval_ptr = NULL;
if (Z_OBJCE_P(obj) != PHP_IC_ENTRY &&
zend_hash_exists(&Z_OBJCE_P(obj)->function_table, "__wakeup", sizeof("__wakeup"))) {
INIT_PZVAL(&fname);
ZVAL_STRINGL(&fname, "__wakeup", sizeof("__wakeup") -1, 0);
call_user_function_ex(CG(function_table), &obj, &fname, &retval_ptr, 0, 0, 1, NULL TSRMLS_CC);
}
if (retval_ptr) {
zval_ptr_dtor(&retval_ptr);
}
}
void php_protocolbuffers_execute_sleep(zval *obj, php_protocolbuffers_scheme_container *container TSRMLS_DC)
{
zval fname, *retval_ptr = NULL;
if (Z_OBJCE_P(obj) != PHP_IC_ENTRY &&
zend_hash_exists(&Z_OBJCE_P(obj)->function_table, "__sleep", sizeof("__sleep"))) {
INIT_PZVAL(&fname);
ZVAL_STRINGL(&fname, "__sleep", sizeof("__sleep") -1, 0);
call_user_function_ex(CG(function_table), &obj, &fname, &retval_ptr, 0, 0, 1, NULL TSRMLS_CC);
if (retval_ptr) {
if (Z_TYPE_P(retval_ptr) != IS_ARRAY) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "php_protocolbuffers_execute_sleep failed. __sleep method have to return an array");
zval_ptr_dtor(&retval_ptr);
retval_ptr = NULL;
}
} else {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "php_protocolbuffers_execute_sleep failed. __sleep method have to return an array");
}
}
if (retval_ptr) {
zval **entry = NULL;
HashPosition pos;
php_protocolbuffers_scheme *scheme;
int i;
for (i = 0; i < container->size; i++) {
scheme = &(container->scheme[i]);
scheme->skip = 1;
}
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(retval_ptr), &pos);
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(retval_ptr), (void **)&entry, &pos) == SUCCESS) {
for (i = 0; i < container->size; i++) {
if (Z_TYPE_PP(entry) != IS_STRING) {
convert_to_string(*entry);
}
scheme = &(container->scheme[i]);
if (strcmp(scheme->name, Z_STRVAL_PP(entry)) == 0) {
scheme->skip = 0;
}
}
zend_hash_move_forward_ex(Z_ARRVAL_P(retval_ptr), &pos);
}
zval_ptr_dtor(&retval_ptr);
}
}
int php_protocolbuffers_properties_init(zval *object, zend_class_entry *ce TSRMLS_DC)
{
zval *pp = NULL;
int j = 0;
php_protocolbuffers_scheme_container *container = NULL;
php_protocolbuffers_scheme *scheme = NULL;
HashTable *properties = NULL;
if (php_protocolbuffers_get_scheme_container(ce->name, ce->name_length, &container TSRMLS_CC)) {
return 1;
}
ALLOC_HASHTABLE(properties);
zend_hash_init(properties, 0, NULL, ZVAL_PTR_DTOR, 0);
if (container->use_single_property > 0) {
MAKE_STD_ZVAL(pp);
array_init(pp);
zend_hash_update(properties, container->orig_single_property_name, container->orig_single_property_name_len, (void **)&pp, sizeof(zval), NULL);
} else {
for (j = 0; j < container->size; j++) {
scheme= &container->scheme[j];
MAKE_STD_ZVAL(pp);
if (scheme->repeated > 0) {
array_init(pp);
} else {
if (Z_TYPE_P(scheme->default_value) != IS_NULL) {
ZVAL_ZVAL(pp, scheme->default_value, 1, 0);
} else {
ZVAL_NULL(pp);
}
}
zend_hash_update(properties, scheme->original_name, scheme->original_name_len, (void **)&pp, sizeof(zval), NULL);
}
}
zend_merge_properties(object, properties, 1 TSRMLS_CC);
return 0;
}