/
message.c
2732 lines (2378 loc) · 107 KB
/
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
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
990
991
992
993
994
995
996
997
998
999
1000
// Protocol Buffers - Google's data interchange format
// Copyright 2014 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <php.h>
#include <Zend/zend_exceptions.h>
#include <stdlib.h>
#include <inttypes.h>
#if PHP_MAJOR_VERSION < 7
#include <Zend/zend_compile.h>
#else
#include <Zend/zend_inheritance.h>
#endif
#include "protobuf.h"
#include "utf8.h"
zend_class_entry* message_type;
zend_object_handlers* message_handlers;
static const char TYPE_URL_PREFIX[] = "type.googleapis.com/";
static void hex_to_binary(const char* hex, char** binary, int* binary_len);
static zend_function_entry message_methods[] = {
PHP_ME(Message, clear, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Message, discardUnknownFields, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Message, serializeToString, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Message, mergeFromString, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Message, serializeToJsonString, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Message, mergeFromJsonString, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Message, mergeFrom, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Message, readWrapperValue, NULL, ZEND_ACC_PROTECTED)
PHP_ME(Message, writeWrapperValue, NULL, ZEND_ACC_PROTECTED)
PHP_ME(Message, readOneof, NULL, ZEND_ACC_PROTECTED)
PHP_ME(Message, writeOneof, NULL, ZEND_ACC_PROTECTED)
PHP_ME(Message, whichOneof, NULL, ZEND_ACC_PROTECTED)
PHP_ME(Message, __construct, NULL, ZEND_ACC_PROTECTED)
{NULL, NULL, NULL}
};
// Forward declare static functions.
#if PHP_MAJOR_VERSION < 7
static void message_set_property(zval* object, zval* member, zval* value,
php_proto_zend_literal key TSRMLS_DC);
static zval* message_get_property(zval* object, zval* member, int type,
const zend_literal* key TSRMLS_DC);
static zval** message_get_property_ptr_ptr(zval* object, zval* member, int type,
php_proto_zend_literal key TSRMLS_DC);
static HashTable* message_get_gc(zval* object, zval*** table, int* n TSRMLS_DC);
#else
static void message_set_property(zval* object, zval* member, zval* value,
void** cache_slot);
static zval* message_get_property(zval* object, zval* member, int type,
void** cache_slot, zval* rv);
static zval* message_get_property_ptr_ptr(zval* object, zval* member, int type,
void** cache_slot);
static HashTable* message_get_gc(zval* object, zval** table, int* n);
#endif
static HashTable* message_get_properties(zval* object TSRMLS_DC);
// -----------------------------------------------------------------------------
// PHP Message Handlers
// -----------------------------------------------------------------------------
// Define object free method.
PHP_PROTO_OBJECT_FREE_START(MessageHeader, message)
if (intern->data) {
if (*(void**)intern->data != NULL) {
stringsink_uninit_opaque(*(void**)intern->data);
FREE(*(void**)intern->data);
}
FREE(intern->data);
}
PHP_PROTO_OBJECT_FREE_END
PHP_PROTO_OBJECT_DTOR_START(MessageHeader, message)
PHP_PROTO_OBJECT_DTOR_END
// Define object create method.
PHP_PROTO_OBJECT_CREATE_START(MessageHeader, message)
// Because php call this create func before calling the sub-message's
// constructor defined in PHP, it's possible that the decriptor of this class
// hasn't been added to descritpor pool (when the class is first
// instantiated). In that case, we will defer the initialization of the custom
// data to the parent Message's constructor, which will be called by
// sub-message's constructors after the descriptor has been added.
PHP_PROTO_OBJECT_CREATE_END(MessageHeader, message)
// Init class entry.
PHP_PROTO_INIT_CLASS_START("Google\\Protobuf\\Internal\\Message",
MessageHeader, message)
message_handlers->write_property = message_set_property;
message_handlers->read_property = message_get_property;
message_handlers->get_property_ptr_ptr = message_get_property_ptr_ptr;
message_handlers->get_properties = message_get_properties;
message_handlers->get_gc = message_get_gc;
PHP_PROTO_INIT_CLASS_END
static void message_set_property_internal(zval* object, zval* member,
zval* value TSRMLS_DC) {
const upb_fielddef* field;
MessageHeader* self = UNBOX(MessageHeader, object);
field = upb_msgdef_ntofz(self->descriptor->msgdef, Z_STRVAL_P(member));
if (field == NULL) {
zend_error(E_USER_ERROR, "Unknown field: %s", Z_STRVAL_P(member));
}
layout_set(self->descriptor->layout, self, field, value TSRMLS_CC);
}
#if PHP_MAJOR_VERSION < 7
static void message_set_property(zval* object, zval* member, zval* value,
php_proto_zend_literal key TSRMLS_DC) {
#else
static void message_set_property(zval* object, zval* member, zval* value,
void** cache_slot) {
#endif
if (Z_TYPE_P(member) != IS_STRING) {
zend_error(E_USER_ERROR, "Unexpected type for field name");
return;
}
#if PHP_MAJOR_VERSION < 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION == 0)
if (Z_OBJCE_P(object) != EG(scope)) {
#else
if (Z_OBJCE_P(object) != zend_get_executed_scope()) {
#endif
// User cannot set property directly (e.g., $m->a = 1)
zend_error(E_USER_ERROR, "Cannot access private property.");
return;
}
message_set_property_internal(object, member, value TSRMLS_CC);
}
static zval* message_get_property_internal(zval* object,
zval* member TSRMLS_DC) {
MessageHeader* self = UNBOX(MessageHeader, object);
const upb_fielddef* field;
field = upb_msgdef_ntofz(self->descriptor->msgdef, Z_STRVAL_P(member));
if (field == NULL) {
return PHP_PROTO_GLOBAL_UNINITIALIZED_ZVAL;
}
zend_property_info* property_info;
#if PHP_MAJOR_VERSION < 7
property_info =
zend_get_property_info(Z_OBJCE_P(object), member, true TSRMLS_CC);
#else
property_info =
zend_get_property_info(Z_OBJCE_P(object), Z_STR_P(member), true);
#endif
return layout_get(
self->descriptor->layout, self, field,
OBJ_PROP(Z_OBJ_P(object), property_info->offset) TSRMLS_CC);
}
static void message_get_oneof_property_internal(zval* object, zval* member,
zval* return_value TSRMLS_DC) {
MessageHeader* self = UNBOX(MessageHeader, object);
const upb_fielddef* field;
field = upb_msgdef_ntofz(self->descriptor->msgdef, Z_STRVAL_P(member));
if (field == NULL) {
return;
}
layout_get(self->descriptor->layout, self, field,
ZVAL_PTR_TO_CACHED_PTR(return_value) TSRMLS_CC);
}
#if PHP_MAJOR_VERSION < 7
static zval* message_get_property(zval* object, zval* member, int type,
const zend_literal* key TSRMLS_DC) {
#else
static zval* message_get_property(zval* object, zval* member, int type,
void** cache_slot, zval* rv) {
#endif
if (Z_TYPE_P(member) != IS_STRING) {
zend_error(E_USER_ERROR, "Property name has to be a string.");
return PHP_PROTO_GLOBAL_UNINITIALIZED_ZVAL;
}
#if PHP_MAJOR_VERSION < 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION == 0)
if (Z_OBJCE_P(object) != EG(scope)) {
#else
if (Z_OBJCE_P(object) != zend_get_executed_scope()) {
#endif
// User cannot get property directly (e.g., $a = $m->a)
zend_error(E_USER_ERROR, "Cannot access private property.");
return PHP_PROTO_GLOBAL_UNINITIALIZED_ZVAL;
}
return message_get_property_internal(object, member TSRMLS_CC);
}
#if PHP_MAJOR_VERSION < 7
static zval** message_get_property_ptr_ptr(zval* object, zval* member, int type,
php_proto_zend_literal key
TSRMLS_DC) {
#else
static zval* message_get_property_ptr_ptr(zval* object, zval* member, int type,
void** cache_slot) {
#endif
return NULL;
}
static HashTable* message_get_properties(zval* object TSRMLS_DC) {
return NULL;
}
static HashTable* message_get_gc(zval* object, CACHED_VALUE** table,
int* n TSRMLS_DC) {
zend_object* zobj = Z_OBJ_P(object);
*table = zobj->properties_table;
*n = zobj->ce->default_properties_count;
return NULL;
}
// -----------------------------------------------------------------------------
// C Message Utilities
// -----------------------------------------------------------------------------
void* message_data(MessageHeader* msg) {
return msg->data;
}
void custom_data_init(const zend_class_entry* ce,
MessageHeader* intern PHP_PROTO_TSRMLS_DC) {
DescriptorInternal* desc = get_ce_desc(ce);
intern->data = ALLOC_N(uint8_t, desc->layout->size);
// We wrap first so that everything in the message object is GC-rooted in
// case a collection happens during object creation in layout_init().
intern->descriptor = desc;
layout_init(desc->layout, message_data(intern),
&intern->std PHP_PROTO_TSRMLS_CC);
}
#define INIT_MESSAGE_WITH_ARRAY \
{ \
zval* array_wrapper = NULL; \
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \
"|a!", &array_wrapper) == FAILURE) { \
return; \
} \
Message_construct(getThis(), array_wrapper); \
}
void build_class_from_descriptor(
PHP_PROTO_HASHTABLE_VALUE php_descriptor TSRMLS_DC) {
Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, php_descriptor);
// Map entries don't have existing php class.
if (upb_msgdef_mapentry(desc->intern->msgdef)) {
return;
}
zend_class_entry* registered_ce = desc->intern->klass;
if (desc->intern->layout == NULL) {
MessageLayout* layout = create_layout(desc->intern->msgdef);
desc->intern->layout = layout;
}
registered_ce->create_object = message_create;
}
// -----------------------------------------------------------------------------
// PHP Methods
// -----------------------------------------------------------------------------
static void append_wrapper_message(
zend_class_entry* subklass, RepeatedField* intern, zval* value TSRMLS_DC) {
MessageHeader* submsg;
const upb_fielddef* field;
#if PHP_MAJOR_VERSION < 7
zval* val = NULL;
MAKE_STD_ZVAL(val);
ZVAL_OBJ(val, subklass->create_object(subklass TSRMLS_CC));
repeated_field_push_native(intern, &val);
submsg = UNBOX(MessageHeader, val);
#else
zend_object* obj = subklass->create_object(subklass TSRMLS_CC);
repeated_field_push_native(intern, &obj);
submsg = (MessageHeader*)((char*)obj - XtOffsetOf(MessageHeader, std));
#endif
custom_data_init(subklass, submsg PHP_PROTO_TSRMLS_CC);
field = upb_msgdef_itof(submsg->descriptor->msgdef, 1);
layout_set(submsg->descriptor->layout, submsg, field, value TSRMLS_CC);
}
static void set_wrapper_message_as_map_value(
zend_class_entry* subklass, zval* map, zval* key, zval* value TSRMLS_DC) {
MessageHeader* submsg;
const upb_fielddef* field;
#if PHP_MAJOR_VERSION < 7
zval* val = NULL;
MAKE_STD_ZVAL(val);
ZVAL_OBJ(val, subklass->create_object(subklass TSRMLS_CC));
map_field_handlers->write_dimension(
map, key, val TSRMLS_CC);
submsg = UNBOX(MessageHeader, val);
#else
zval val;
zend_object* obj = subklass->create_object(subklass TSRMLS_CC);
ZVAL_OBJ(&val, obj);
map_field_handlers->write_dimension(map, key, &val TSRMLS_CC);
submsg = (MessageHeader*)((char*)obj - XtOffsetOf(MessageHeader, std));
#endif
custom_data_init(subklass, submsg PHP_PROTO_TSRMLS_CC);
field = upb_msgdef_itof(submsg->descriptor->msgdef, 1);
layout_set(submsg->descriptor->layout, submsg, field, value TSRMLS_CC);
}
void Message_construct(zval* msg, zval* array_wrapper) {
TSRMLS_FETCH();
zend_class_entry* ce = Z_OBJCE_P(msg);
MessageHeader* intern = NULL;
if (EXPECTED(class_added(ce))) {
intern = UNBOX(MessageHeader, msg);
custom_data_init(ce, intern PHP_PROTO_TSRMLS_CC);
}
if (array_wrapper == NULL) {
return;
}
HashTable* array = Z_ARRVAL_P(array_wrapper);
HashPosition pointer;
zval key;
void* value;
const upb_fielddef* field;
for (zend_hash_internal_pointer_reset_ex(array, &pointer);
php_proto_zend_hash_get_current_data_ex(array, (void**)&value,
&pointer) == SUCCESS;
zend_hash_move_forward_ex(array, &pointer)) {
zend_hash_get_current_key_zval_ex(array, &key, &pointer);
field = upb_msgdef_ntofz(intern->descriptor->msgdef, Z_STRVAL_P(&key));
#if PHP_MAJOR_VERSION >= 7
if (Z_ISREF_P((CACHED_VALUE*)value)) {
value = Z_REFVAL_P((CACHED_VALUE*)value);
}
#endif
if (field == NULL) {
zend_error(E_USER_ERROR, "Unknown field: %s", Z_STRVAL_P(&key));
}
if (upb_fielddef_ismap(field)) {
PHP_PROTO_FAKE_SCOPE_BEGIN(Z_OBJCE_P(msg));
zval* submap = message_get_property_internal(msg, &key TSRMLS_CC);
PHP_PROTO_FAKE_SCOPE_END;
HashTable* subtable = HASH_OF(
CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)value));
HashPosition subpointer;
zval subkey;
void* memory;
bool is_wrapper = false;
zend_class_entry* subklass = NULL;
const upb_msgdef* mapentry = upb_fielddef_msgsubdef(field);
const upb_fielddef *value_field = upb_msgdef_itof(mapentry, 2);
if (upb_fielddef_issubmsg(value_field)) {
const upb_msgdef* submsgdef = upb_fielddef_msgsubdef(value_field);
upb_wellknowntype_t type = upb_msgdef_wellknowntype(submsgdef);
is_wrapper = is_wrapper_msg(submsgdef);
if (is_wrapper) {
DescriptorInternal* subdesc = get_msgdef_desc(submsgdef);
subklass = subdesc->klass;
}
}
for (zend_hash_internal_pointer_reset_ex(subtable, &subpointer);
php_proto_zend_hash_get_current_data_ex(subtable, (void**)&memory,
&subpointer) == SUCCESS;
zend_hash_move_forward_ex(subtable, &subpointer)) {
zend_hash_get_current_key_zval_ex(subtable, &subkey, &subpointer);
if (is_wrapper &&
Z_TYPE_P(CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory)) != IS_OBJECT) {
set_wrapper_message_as_map_value(
subklass, submap, &subkey,
CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory) TSRMLS_CC);
} else {
map_field_handlers->write_dimension(
submap, &subkey,
CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory) TSRMLS_CC);
}
zval_dtor(&subkey);
}
} else if (upb_fielddef_isseq(field)) {
PHP_PROTO_FAKE_SCOPE_BEGIN(Z_OBJCE_P(msg));
zval* subarray = message_get_property_internal(msg, &key TSRMLS_CC);
PHP_PROTO_FAKE_SCOPE_END;
HashTable* subtable = HASH_OF(
CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)value));
HashPosition subpointer;
void* memory;
bool is_wrapper = false;
zend_class_entry* subklass = NULL;
if (upb_fielddef_issubmsg(field)) {
const upb_msgdef* submsgdef = upb_fielddef_msgsubdef(field);
upb_wellknowntype_t type = upb_msgdef_wellknowntype(submsgdef);
is_wrapper = is_wrapper_msg(submsgdef);
if (is_wrapper) {
DescriptorInternal* subdesc = get_msgdef_desc(submsgdef);
subklass = subdesc->klass;
}
}
for (zend_hash_internal_pointer_reset_ex(subtable, &subpointer);
php_proto_zend_hash_get_current_data_ex(subtable, (void**)&memory,
&subpointer) == SUCCESS;
zend_hash_move_forward_ex(subtable, &subpointer)) {
if (is_wrapper &&
Z_TYPE_P(CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory)) != IS_OBJECT) {
RepeatedField* intern = UNBOX(RepeatedField, subarray);
append_wrapper_message(
subklass, intern,
CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory) TSRMLS_CC);
} else {
repeated_field_handlers->write_dimension(
subarray, NULL,
CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)memory) TSRMLS_CC);
}
}
} else if (upb_fielddef_issubmsg(field)) {
const upb_msgdef* submsgdef = upb_fielddef_msgsubdef(field);
DescriptorInternal* desc = get_msgdef_desc(submsgdef);
CACHED_VALUE* cached = NULL;
if (upb_fielddef_containingoneof(field)) {
void* memory = slot_memory(intern->descriptor->layout,
message_data(intern), field);
uint32_t* oneof_case = slot_oneof_case(intern->descriptor->layout,
message_data(intern), field);
int property_cache_index =
intern->descriptor->layout->fields[upb_fielddef_index(field)]
.cache_index;
cached = OBJ_PROP(Z_OBJ_P(msg), property_cache_index);
*(CACHED_VALUE**)(memory) = cached;
*oneof_case = upb_fielddef_number(field);
} else {
zend_property_info* property_info;
PHP_PROTO_FAKE_SCOPE_BEGIN(Z_OBJCE_P(msg));
#if PHP_MAJOR_VERSION < 7
property_info =
zend_get_property_info(Z_OBJCE_P(msg), &key, true TSRMLS_CC);
#else
property_info =
zend_get_property_info(Z_OBJCE_P(msg), Z_STR_P(&key), true);
#endif
PHP_PROTO_FAKE_SCOPE_END;
cached = OBJ_PROP(Z_OBJ_P(msg), property_info->offset);
}
#if PHP_MAJOR_VERSION < 7
SEPARATE_ZVAL_IF_NOT_REF(cached);
#endif
zval* submsg = CACHED_PTR_TO_ZVAL_PTR(cached);
ZVAL_OBJ(submsg, desc->klass->create_object(desc->klass TSRMLS_CC));
Message_construct(submsg, NULL);
MessageHeader* to = UNBOX(MessageHeader, submsg);
const upb_filedef *file = upb_msgdef_file(submsgdef);
if (!strcmp(upb_filedef_name(file), "google/protobuf/wrappers.proto") &&
Z_TYPE_P(CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)value)) != IS_OBJECT) {
const upb_fielddef *value_field = upb_msgdef_itof(submsgdef, 1);
layout_set(to->descriptor->layout, to,
value_field, CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)value)
TSRMLS_CC);
} else {
MessageHeader* from =
UNBOX(MessageHeader,
CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)value));
if(from->descriptor != to->descriptor) {
zend_error(E_USER_ERROR,
"Cannot merge messages with different class.");
return;
}
layout_merge(from->descriptor->layout, from, to TSRMLS_CC);
}
} else {
message_set_property_internal(msg, &key,
CACHED_PTR_TO_ZVAL_PTR((CACHED_VALUE*)value) TSRMLS_CC);
}
zval_dtor(&key);
}
}
// At the first time the message is created, the class entry hasn't been
// modified. As a result, the first created instance will be a normal zend
// object. Here, we manually modify it to our message in such a case.
PHP_METHOD(Message, __construct) {
INIT_MESSAGE_WITH_ARRAY;
}
PHP_METHOD(Message, clear) {
MessageHeader* msg = UNBOX(MessageHeader, getThis());
DescriptorInternal* desc = msg->descriptor;
zend_class_entry* ce = desc->klass;
zend_object_std_dtor(&msg->std TSRMLS_CC);
object_properties_init(&msg->std, ce);
layout_init(desc->layout, message_data(msg), &msg->std TSRMLS_CC);
}
PHP_METHOD(Message, mergeFrom) {
zval* value;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &value,
message_type) == FAILURE) {
return;
}
MessageHeader* from = UNBOX(MessageHeader, value);
MessageHeader* to = UNBOX(MessageHeader, getThis());
if(from->descriptor != to->descriptor) {
zend_error(E_USER_ERROR, "Cannot merge messages with different class.");
return;
}
layout_merge(from->descriptor->layout, from, to TSRMLS_CC);
}
PHP_METHOD(Message, readWrapperValue) {
char* member;
PHP_PROTO_SIZE length;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &member,
&length) == FAILURE) {
return;
}
MessageHeader* msg = UNBOX(MessageHeader, getThis());
const upb_fielddef* field =
upb_msgdef_ntofz(msg->descriptor->msgdef, member);
if (upb_fielddef_containingoneof(field)) {
uint32_t* oneof_case =
slot_oneof_case(msg->descriptor->layout, message_data(msg), field);
if (*oneof_case != upb_fielddef_number(field)) {
RETURN_NULL();
}
}
zval* cached_zval =
CACHED_PTR_TO_ZVAL_PTR(find_zval_property(msg, field));
if (Z_TYPE_P(cached_zval) == IS_NULL) {
RETURN_NULL();
}
if (Z_TYPE_P(cached_zval) == IS_OBJECT) {
const upb_msgdef* submsgdef = upb_fielddef_msgsubdef(field);
const upb_fielddef* value_field = upb_msgdef_itof(submsgdef, 1);
MessageHeader* submsg = UNBOX(MessageHeader, cached_zval);
CACHED_VALUE* cached_value = find_zval_property(submsg, value_field);
layout_get(submsg->descriptor->layout, submsg, value_field,
cached_value TSRMLS_CC);
RETURN_ZVAL(CACHED_PTR_TO_ZVAL_PTR(cached_value), 1, 0);
} else {
RETURN_ZVAL(cached_zval, 1, 0);
}
}
PHP_METHOD(Message, writeWrapperValue) {
char* member;
PHP_PROTO_SIZE length;
zval* value;
if (zend_parse_parameters(
ZEND_NUM_ARGS() TSRMLS_CC, "sz", &member, &length, &value) ==
FAILURE) {
return;
}
MessageHeader* msg = UNBOX(MessageHeader, getThis());
const upb_fielddef* field = upb_msgdef_ntofz(msg->descriptor->msgdef, member);
zval* cached_zval =
CACHED_PTR_TO_ZVAL_PTR(find_zval_property(msg, field));
if (Z_TYPE_P(value) == IS_NULL) {
MessageHeader* msg = UNBOX(MessageHeader, getThis());
layout_set(msg->descriptor->layout, msg,
field, value TSRMLS_CC);
return;
}
{
// Type Checking
const upb_msgdef* submsgdef = upb_fielddef_msgsubdef(field);
const upb_fielddef* value_field = upb_msgdef_itof(submsgdef, 1);
upb_fieldtype_t type = upb_fielddef_type(value_field);
switch(type) {
case UPB_TYPE_STRING:
case UPB_TYPE_BYTES: {
if (!protobuf_convert_to_string(value)) {
return;
}
if (type == UPB_TYPE_STRING &&
!is_structurally_valid_utf8(Z_STRVAL_P(value), Z_STRLEN_P(value))) {
zend_error(E_USER_ERROR, "Given string is not UTF8 encoded.");
return;
}
}
break;
#define CASE_TYPE(upb_type, type, c_type) \
case UPB_TYPE_##upb_type: { \
c_type type##_value; \
if (!protobuf_convert_to_##type(value, &type##_value)) { \
return; \
} \
break; \
}
CASE_TYPE(INT32, int32, int32_t)
CASE_TYPE(UINT32, uint32, uint32_t)
CASE_TYPE(ENUM, int32, int32_t)
CASE_TYPE(INT64, int64, int64_t)
CASE_TYPE(UINT64, uint64, uint64_t)
CASE_TYPE(FLOAT, float, float)
CASE_TYPE(DOUBLE, double, double)
CASE_TYPE(BOOL, bool, int8_t)
#undef CASE_TYPE
}
}
if (upb_fielddef_containingoneof(field)) {
uint32_t* oneof_case =
slot_oneof_case(msg->descriptor->layout, message_data(msg), field);
if (*oneof_case != upb_fielddef_number(field)) {
zval null_value;
ZVAL_NULL(&null_value);
layout_set(msg->descriptor->layout, msg, field, &null_value TSRMLS_CC);
cached_zval = CACHED_PTR_TO_ZVAL_PTR(find_zval_property(msg, field));
ZVAL_ZVAL(cached_zval, value, 1, 0);
return;
}
}
if (Z_TYPE_P(cached_zval) == IS_OBJECT) {
const upb_msgdef* submsgdef = upb_fielddef_msgsubdef(field);
const upb_fielddef* value_field = upb_msgdef_itof(submsgdef, 1);
MessageHeader* submsg = UNBOX(MessageHeader, cached_zval);
CACHED_VALUE* cached_value = find_zval_property(submsg, value_field);
layout_set(submsg->descriptor->layout, submsg,
value_field, value TSRMLS_CC);
} else {
ZVAL_ZVAL(cached_zval, value, 1, 0);
}
}
PHP_METHOD(Message, readOneof) {
PHP_PROTO_LONG index;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) ==
FAILURE) {
return;
}
MessageHeader* msg = UNBOX(MessageHeader, getThis());
const upb_fielddef* field = upb_msgdef_itof(msg->descriptor->msgdef, index);
// Unlike singular fields, oneof fields share cached property. So we cannot
// let layout_get modify the cached property. Instead, we pass in the return
// value directly.
layout_get(msg->descriptor->layout, msg, field,
ZVAL_PTR_TO_CACHED_PTR(return_value) TSRMLS_CC);
}
PHP_METHOD(Message, writeOneof) {
PHP_PROTO_LONG index;
zval* value;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz", &index, &value) ==
FAILURE) {
return;
}
MessageHeader* msg = UNBOX(MessageHeader, getThis());
const upb_fielddef* field = upb_msgdef_itof(msg->descriptor->msgdef, index);
layout_set(msg->descriptor->layout, msg, field, value TSRMLS_CC);
}
PHP_METHOD(Message, whichOneof) {
char* oneof_name;
PHP_PROTO_SIZE length;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &oneof_name,
&length) == FAILURE) {
return;
}
MessageHeader* msg = UNBOX(MessageHeader, getThis());
const upb_oneofdef* oneof =
upb_msgdef_ntoo(msg->descriptor->msgdef, oneof_name, length);
const char* oneof_case_name = layout_get_oneof_case(
msg->descriptor->layout, message_data(msg), oneof TSRMLS_CC);
PHP_PROTO_RETURN_STRING(oneof_case_name, 1);
}
// -----------------------------------------------------------------------------
// Well Known Types Support
// -----------------------------------------------------------------------------
#define PHP_PROTO_FIELD_ACCESSORS(UPPER_CLASS, LOWER_CLASS, UPPER_FIELD, \
LOWER_FIELD) \
PHP_METHOD(UPPER_CLASS, get##UPPER_FIELD) { \
zval member; \
PHP_PROTO_ZVAL_STRING(&member, LOWER_FIELD, 1); \
PHP_PROTO_FAKE_SCOPE_BEGIN(LOWER_CLASS##_type); \
zval* value = message_get_property_internal(getThis(), &member TSRMLS_CC); \
PHP_PROTO_FAKE_SCOPE_END; \
zval_dtor(&member); \
PHP_PROTO_RETVAL_ZVAL(value); \
} \
PHP_METHOD(UPPER_CLASS, set##UPPER_FIELD) { \
zval* value = NULL; \
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == \
FAILURE) { \
return; \
} \
zval member; \
PHP_PROTO_ZVAL_STRING(&member, LOWER_FIELD, 1); \
message_set_property_internal(getThis(), &member, value TSRMLS_CC); \
zval_dtor(&member); \
PHP_PROTO_RETVAL_ZVAL(getThis()); \
}
#define PHP_PROTO_ONEOF_FIELD_ACCESSORS(UPPER_CLASS, LOWER_CLASS, UPPER_FIELD, \
LOWER_FIELD) \
PHP_METHOD(UPPER_CLASS, get##UPPER_FIELD) { \
zval member; \
PHP_PROTO_ZVAL_STRING(&member, LOWER_FIELD, 1); \
PHP_PROTO_FAKE_SCOPE_BEGIN(LOWER_CLASS##_type); \
message_get_oneof_property_internal(getThis(), &member, \
return_value TSRMLS_CC); \
PHP_PROTO_FAKE_SCOPE_END; \
zval_dtor(&member); \
} \
PHP_METHOD(UPPER_CLASS, set##UPPER_FIELD) { \
zval* value = NULL; \
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == \
FAILURE) { \
return; \
} \
zval member; \
PHP_PROTO_ZVAL_STRING(&member, LOWER_FIELD, 1); \
message_set_property_internal(getThis(), &member, value TSRMLS_CC); \
zval_dtor(&member); \
PHP_PROTO_RETVAL_ZVAL(getThis()); \
}
#define PHP_PROTO_ONEOF_ACCESSORS(UPPER_CLASS, LOWER_CLASS, UPPER_FIELD, \
LOWER_FIELD) \
PHP_METHOD(UPPER_CLASS, get##UPPER_FIELD) { \
MessageHeader* msg = UNBOX(MessageHeader, getThis()); \
PHP_PROTO_FAKE_SCOPE_BEGIN(LOWER_CLASS##_type); \
const upb_oneofdef* oneof = upb_msgdef_ntoo( \
msg->descriptor->msgdef, LOWER_FIELD, strlen(LOWER_FIELD)); \
const char* oneof_case_name = layout_get_oneof_case( \
msg->descriptor->layout, message_data(msg), oneof TSRMLS_CC); \
PHP_PROTO_FAKE_SCOPE_END; \
PHP_PROTO_RETURN_STRING(oneof_case_name, 1); \
}
// Forward declare file init functions
static void init_file_any(TSRMLS_D);
static void init_file_api(TSRMLS_D);
static void init_file_duration(TSRMLS_D);
static void init_file_field_mask(TSRMLS_D);
static void init_file_empty(TSRMLS_D);
static void init_file_source_context(TSRMLS_D);
static void init_file_struct(TSRMLS_D);
static void init_file_timestamp(TSRMLS_D);
static void init_file_type(TSRMLS_D);
static void init_file_wrappers(TSRMLS_D);
// Define file init functions
static void init_file_any(TSRMLS_D) {
if (is_inited_file_any) return;
init_generated_pool_once(TSRMLS_C);
const char* generated_file =
"0acd010a19676f6f676c652f70726f746f6275662f616e792e70726f746f"
"120f676f6f676c652e70726f746f62756622260a03416e7912100a087479"
"70655f75726c180120012809120d0a0576616c756518022001280c426f0a"
"13636f6d2e676f6f676c652e70726f746f6275664208416e7950726f746f"
"50015a256769746875622e636f6d2f676f6c616e672f70726f746f627566"
"2f7074797065732f616e79a20203475042aa021e476f6f676c652e50726f"
"746f6275662e57656c6c4b6e6f776e5479706573620670726f746f33";
char* binary;
int binary_len;
hex_to_binary(generated_file, &binary, &binary_len);
internal_add_generated_file(binary, binary_len,
generated_pool, true TSRMLS_CC);
FREE(binary);
is_inited_file_any = true;
}
static void init_file_api(TSRMLS_D) {
if (is_inited_file_api) return;
init_file_source_context(TSRMLS_C);
init_file_type(TSRMLS_C);
init_generated_pool_once(TSRMLS_C);
const char* generated_file =
"0aee050a19676f6f676c652f70726f746f6275662f6170692e70726f746f"
"120f676f6f676c652e70726f746f6275661a24676f6f676c652f70726f74"
"6f6275662f736f757263655f636f6e746578742e70726f746f1a1a676f6f"
"676c652f70726f746f6275662f747970652e70726f746f2281020a034170"
"69120c0a046e616d6518012001280912280a076d6574686f647318022003"
"280b32172e676f6f676c652e70726f746f6275662e4d6574686f6412280a"
"076f7074696f6e7318032003280b32172e676f6f676c652e70726f746f62"
"75662e4f7074696f6e120f0a0776657273696f6e18042001280912360a0e"
"736f757263655f636f6e7465787418052001280b321e2e676f6f676c652e"
"70726f746f6275662e536f75726365436f6e7465787412260a066d697869"
"6e7318062003280b32162e676f6f676c652e70726f746f6275662e4d6978"
"696e12270a0673796e74617818072001280e32172e676f6f676c652e7072"
"6f746f6275662e53796e74617822d5010a064d6574686f64120c0a046e61"
"6d6518012001280912180a10726571756573745f747970655f75726c1802"
"2001280912190a11726571756573745f73747265616d696e671803200128"
"0812190a11726573706f6e73655f747970655f75726c180420012809121a"
"0a12726573706f6e73655f73747265616d696e6718052001280812280a07"
"6f7074696f6e7318062003280b32172e676f6f676c652e70726f746f6275"
"662e4f7074696f6e12270a0673796e74617818072001280e32172e676f6f"
"676c652e70726f746f6275662e53796e74617822230a054d6978696e120c"
"0a046e616d65180120012809120c0a04726f6f7418022001280942750a13"
"636f6d2e676f6f676c652e70726f746f627566420841706950726f746f50"
"015a2b676f6f676c652e676f6c616e672e6f72672f67656e70726f746f2f"
"70726f746f6275662f6170693b617069a20203475042aa021e476f6f676c"
"652e50726f746f6275662e57656c6c4b6e6f776e5479706573620670726f"
"746f33";
char* binary;
int binary_len;
hex_to_binary(generated_file, &binary, &binary_len);
internal_add_generated_file(binary, binary_len,
generated_pool, true TSRMLS_CC);
FREE(binary);
is_inited_file_api = true;
}
static void init_file_duration(TSRMLS_D) {
if (is_inited_file_duration) return;
init_generated_pool_once(TSRMLS_C);
const char* generated_file =
"0ae3010a1e676f6f676c652f70726f746f6275662f6475726174696f6e2e"
"70726f746f120f676f6f676c652e70726f746f627566222a0a0844757261"
"74696f6e120f0a077365636f6e6473180120012803120d0a056e616e6f73"
"180220012805427c0a13636f6d2e676f6f676c652e70726f746f62756642"
"0d4475726174696f6e50726f746f50015a2a6769746875622e636f6d2f67"
"6f6c616e672f70726f746f6275662f7074797065732f6475726174696f6e"
"f80101a20203475042aa021e476f6f676c652e50726f746f6275662e5765"
"6c6c4b6e6f776e5479706573620670726f746f33";
char* binary;
int binary_len;
hex_to_binary(generated_file, &binary, &binary_len);
internal_add_generated_file(binary, binary_len,
generated_pool, true TSRMLS_CC);
FREE(binary);
is_inited_file_duration = true;
}
static void init_file_field_mask(TSRMLS_D) {
if (is_inited_file_field_mask) return;
init_generated_pool_once(TSRMLS_C);
const char* generated_file =
"0ae3010a20676f6f676c652f70726f746f6275662f6669656c645f6d6173"
"6b2e70726f746f120f676f6f676c652e70726f746f627566221a0a094669"
"656c644d61736b120d0a0570617468731801200328094289010a13636f6d"
"2e676f6f676c652e70726f746f627566420e4669656c644d61736b50726f"
"746f50015a39676f6f676c652e676f6c616e672e6f72672f67656e70726f"
"746f2f70726f746f6275662f6669656c645f6d61736b3b6669656c645f6d"
"61736ba20203475042aa021e476f6f676c652e50726f746f6275662e5765"
"6c6c4b6e6f776e5479706573620670726f746f33";
char* binary;
int binary_len;
hex_to_binary(generated_file, &binary, &binary_len);
internal_add_generated_file(binary, binary_len,
generated_pool, true TSRMLS_CC);
FREE(binary);
is_inited_file_field_mask = true;
}
static void init_file_empty(TSRMLS_D) {
if (is_inited_file_empty) return;
init_generated_pool_once(TSRMLS_C);
const char* generated_file =
"0ab7010a1b676f6f676c652f70726f746f6275662f656d7074792e70726f"
"746f120f676f6f676c652e70726f746f62756622070a05456d7074794276"
"0a13636f6d2e676f6f676c652e70726f746f627566420a456d7074795072"
"6f746f50015a276769746875622e636f6d2f676f6c616e672f70726f746f"
"6275662f7074797065732f656d707479f80101a20203475042aa021e476f"
"6f676c652e50726f746f6275662e57656c6c4b6e6f776e54797065736206"
"70726f746f33";
char* binary;
int binary_len;
hex_to_binary(generated_file, &binary, &binary_len);
internal_add_generated_file(binary, binary_len,
generated_pool, true TSRMLS_CC);
FREE(binary);
is_inited_file_empty = true;
}
static void init_file_source_context(TSRMLS_D) {
if (is_inited_file_source_context) return;
init_generated_pool_once(TSRMLS_C);
const char* generated_file =
"0afb010a24676f6f676c652f70726f746f6275662f736f757263655f636f"
"6e746578742e70726f746f120f676f6f676c652e70726f746f6275662222"
"0a0d536f75726365436f6e7465787412110a0966696c655f6e616d651801"
"200128094295010a13636f6d2e676f6f676c652e70726f746f6275664212"
"536f75726365436f6e7465787450726f746f50015a41676f6f676c652e67"
"6f6c616e672e6f72672f67656e70726f746f2f70726f746f6275662f736f"
"757263655f636f6e746578743b736f757263655f636f6e74657874a20203"
"475042aa021e476f6f676c652e50726f746f6275662e57656c6c4b6e6f77"
"6e5479706573620670726f746f33";
char* binary;
int binary_len;
hex_to_binary(generated_file, &binary, &binary_len);
internal_add_generated_file(binary, binary_len,
generated_pool, true TSRMLS_CC);
FREE(binary);
is_inited_file_source_context = true;
}
static void init_file_struct(TSRMLS_D) {
if (is_inited_file_struct) return;
init_generated_pool_once(TSRMLS_C);
const char* generated_file =
"0a81050a1c676f6f676c652f70726f746f6275662f7374727563742e7072"
"6f746f120f676f6f676c652e70726f746f6275662284010a065374727563"
"7412330a066669656c647318012003280b32232e676f6f676c652e70726f"
"746f6275662e5374727563742e4669656c6473456e7472791a450a0b4669"
"656c6473456e747279120b0a036b657918012001280912250a0576616c75"
"6518022001280b32162e676f6f676c652e70726f746f6275662e56616c75"
"653a02380122ea010a0556616c756512300a0a6e756c6c5f76616c756518"
"012001280e321a2e676f6f676c652e70726f746f6275662e4e756c6c5661"
"6c7565480012160a0c6e756d6265725f76616c7565180220012801480012"
"160a0c737472696e675f76616c7565180320012809480012140a0a626f6f"
"6c5f76616c75651804200128084800122f0a0c7374727563745f76616c75"
"6518052001280b32172e676f6f676c652e70726f746f6275662e53747275"
"6374480012300a0a6c6973745f76616c756518062001280b321a2e676f6f"
"676c652e70726f746f6275662e4c69737456616c7565480042060a046b69"
"6e6422330a094c69737456616c756512260a0676616c7565731801200328"
"0b32162e676f6f676c652e70726f746f6275662e56616c75652a1b0a094e"
"756c6c56616c7565120e0a0a4e554c4c5f56414c554510004281010a1363"
"6f6d2e676f6f676c652e70726f746f627566420b53747275637450726f74"
"6f50015a316769746875622e636f6d2f676f6c616e672f70726f746f6275"
"662f7074797065732f7374727563743b7374727563747062f80101a20203"
"475042aa021e476f6f676c652e50726f746f6275662e57656c6c4b6e6f77"
"6e5479706573620670726f746f33";
char* binary;
int binary_len;
hex_to_binary(generated_file, &binary, &binary_len);
internal_add_generated_file(binary, binary_len,
generated_pool, true TSRMLS_CC);
FREE(binary);
is_inited_file_struct = true;
}
static void init_file_timestamp(TSRMLS_D) {
if (is_inited_file_timestamp) return;
init_generated_pool_once(TSRMLS_C);
const char* generated_file =
"0ae7010a1f676f6f676c652f70726f746f6275662f74696d657374616d70"
"2e70726f746f120f676f6f676c652e70726f746f627566222b0a0954696d"
"657374616d70120f0a077365636f6e6473180120012803120d0a056e616e"