forked from protocolbuffers/protobuf
/
encode_decode.c
2315 lines (1978 loc) · 77.6 KB
/
encode_decode.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 2008 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 "protobuf.h"
#include "utf8.h"
/* stringsink *****************************************************************/
static void *stringsink_start(void *_sink, const void *hd, size_t size_hint) {
stringsink *sink = _sink;
sink->len = 0;
return sink;
}
size_t stringsink_string(void *_sink, const void *hd, const char *ptr,
size_t len, const upb_bufhandle *handle) {
stringsink *sink = _sink;
size_t new_size = sink->size;
PHP_PROTO_UNUSED(hd);
PHP_PROTO_UNUSED(handle);
while (sink->len + len > new_size) {
new_size *= 2;
}
if (new_size != sink->size) {
sink->ptr = realloc(sink->ptr, new_size);
sink->size = new_size;
}
memcpy(sink->ptr + sink->len, ptr, len);
sink->len += len;
return len;
}
void stringsink_init(stringsink *sink) {
upb_byteshandler_init(&sink->handler);
upb_byteshandler_setstartstr(&sink->handler, stringsink_start, NULL);
upb_byteshandler_setstring(&sink->handler, stringsink_string, NULL);
upb_bytessink_reset(&sink->sink, &sink->handler, sink);
sink->size = 32;
sink->ptr = malloc(sink->size);
PHP_PROTO_ASSERT(sink->ptr != NULL);
sink->len = 0;
}
void stringsink_uninit(stringsink *sink) { free(sink->ptr); }
void stringsink_uninit_opaque(void *sink) { stringsink_uninit(sink); }
/* stackenv *****************************************************************/
// Stack-allocated context during an encode/decode operation. Contains the upb
// environment and its stack-based allocator, an initial buffer for allocations
// to avoid malloc() when possible, and a template for PHP exception messages
// if any error occurs.
#define STACK_ENV_STACKBYTES 4096
typedef struct {
upb_arena *arena;
upb_status status;
const char *php_error_template;
char allocbuf[STACK_ENV_STACKBYTES];
} stackenv;
static void stackenv_init(stackenv* se, const char* errmsg);
static void stackenv_uninit(stackenv* se);
static void stackenv_init(stackenv* se, const char* errmsg) {
se->php_error_template = errmsg;
se->arena = upb_arena_new();
upb_status_clear(&se->status);
}
static void stackenv_uninit(stackenv* se) {
upb_arena_free(se->arena);
if (!upb_ok(&se->status)) {
// TODO(teboring): have a way to verify that this is actually a parse error,
// instead of just throwing "parse error" unconditionally.
TSRMLS_FETCH();
zend_throw_exception_ex(NULL, 0 TSRMLS_CC, se->php_error_template,
upb_status_errmsg(&se->status));
}
}
// -----------------------------------------------------------------------------
// Parsing.
// -----------------------------------------------------------------------------
bool is_wrapper_msg(const upb_msgdef* m) {
switch (upb_msgdef_wellknowntype(m)) {
case UPB_WELLKNOWN_DOUBLEVALUE:
case UPB_WELLKNOWN_FLOATVALUE:
case UPB_WELLKNOWN_INT64VALUE:
case UPB_WELLKNOWN_UINT64VALUE:
case UPB_WELLKNOWN_INT32VALUE:
case UPB_WELLKNOWN_UINT32VALUE:
case UPB_WELLKNOWN_STRINGVALUE:
case UPB_WELLKNOWN_BYTESVALUE:
case UPB_WELLKNOWN_BOOLVALUE:
return true;
default:
return false;
}
}
typedef struct {
void* closure;
void* submsg;
bool is_msg;
} wrapperfields_parseframe_t;
#define DEREF(msg, ofs, type) *(type*)(((uint8_t *)msg) + ofs)
// Creates a handlerdata that simply contains the offset for this field.
static const void* newhandlerdata(upb_handlers* h, uint32_t ofs) {
size_t* hd_ofs = (size_t*)malloc(sizeof(size_t));
PHP_PROTO_ASSERT(hd_ofs != NULL);
*hd_ofs = ofs;
upb_handlers_addcleanup(h, hd_ofs, free);
return hd_ofs;
}
static const void* newhandlerfielddata(
upb_handlers* h, const upb_fielddef* field) {
const void** hd_field = malloc(sizeof(void*));
PHP_PROTO_ASSERT(hd_field != NULL);
*hd_field = field;
upb_handlers_addcleanup(h, hd_field, free);
return hd_field;
}
typedef struct {
void* closure;
stringsink sink;
} stringfields_parseframe_t;
typedef size_t (*encodeunknown_handlerfunc)(void* _sink, const void* hd,
const char* ptr, size_t len,
const upb_bufhandle* handle);
typedef struct {
encodeunknown_handlerfunc handler;
} unknownfields_handlerdata_t;
// Creates a handlerdata for unknown fields.
static const void *newunknownfieldshandlerdata(upb_handlers* h) {
unknownfields_handlerdata_t* hd =
(unknownfields_handlerdata_t*)malloc(sizeof(unknownfields_handlerdata_t));
PHP_PROTO_ASSERT(hd != NULL);
hd->handler = stringsink_string;
upb_handlers_addcleanup(h, hd, free);
return hd;
}
typedef struct {
const upb_fielddef *fd;
size_t ofs;
const upb_msgdef *md;
} submsg_handlerdata_t;
// Creates a handlerdata that contains field and submessage type information.
static const void *newsubmsghandlerdata(upb_handlers* h, uint32_t ofs,
const upb_fielddef* f) {
submsg_handlerdata_t* hd =
(submsg_handlerdata_t*)malloc(sizeof(submsg_handlerdata_t));
PHP_PROTO_ASSERT(hd != NULL);
hd->fd = f;
hd->ofs = ofs;
hd->md = upb_fielddef_msgsubdef(f);
upb_handlers_addcleanup(h, hd, free);
return hd;
}
typedef struct {
size_t ofs; // union data slot
size_t case_ofs; // oneof_case field
int property_ofs; // properties table cache
uint32_t oneof_case_num; // oneof-case number to place in oneof_case field
const upb_msgdef *md; // msgdef, for oneof submessage handler
const upb_msgdef *parent_md; // msgdef, for parent submessage
} oneof_handlerdata_t;
static const void *newoneofhandlerdata(upb_handlers *h,
uint32_t ofs,
uint32_t case_ofs,
int property_ofs,
const upb_msgdef *m,
const upb_fielddef *f) {
oneof_handlerdata_t* hd =
(oneof_handlerdata_t*)malloc(sizeof(oneof_handlerdata_t));
PHP_PROTO_ASSERT(hd != NULL);
hd->ofs = ofs;
hd->case_ofs = case_ofs;
hd->property_ofs = property_ofs;
hd->parent_md = m;
// We reuse the field tag number as a oneof union discriminant tag. Note that
// we don't expose these numbers to the user, so the only requirement is that
// we have some unique ID for each union case/possibility. The field tag
// numbers are already present and are easy to use so there's no reason to
// create a separate ID space. In addition, using the field tag number here
// lets us easily look up the field in the oneof accessor.
hd->oneof_case_num = upb_fielddef_number(f);
if (upb_fielddef_type(f) == UPB_TYPE_MESSAGE) {
hd->md = upb_fielddef_msgsubdef(f);
} else {
hd->md = NULL;
}
upb_handlers_addcleanup(h, hd, free);
return hd;
}
// A handler that starts a repeated field. Gets the Repeated*Field instance for
// this field (such an instance always exists even in an empty message).
static void *startseq_handler(void* closure, const void* hd) {
MessageHeader* msg = closure;
const upb_fielddef** field = (const upb_fielddef**) hd;
CACHED_VALUE* cache = find_zval_property(msg, *field);
TSRMLS_FETCH();
repeated_field_ensure_created(*field, cache PHP_PROTO_TSRMLS_CC);
return CACHED_PTR_TO_ZVAL_PTR(cache);
}
// Handlers that append primitive values to a repeated field.
#define DEFINE_APPEND_HANDLER(type, ctype) \
static bool append##type##_handler(void* closure, const void* hd, \
ctype val) { \
zval* array = (zval*)closure; \
TSRMLS_FETCH(); \
RepeatedField* intern = UNBOX(RepeatedField, array); \
repeated_field_push_native(intern, &val); \
return true; \
}
DEFINE_APPEND_HANDLER(bool, bool)
DEFINE_APPEND_HANDLER(int32, int32_t)
DEFINE_APPEND_HANDLER(uint32, uint32_t)
DEFINE_APPEND_HANDLER(float, float)
DEFINE_APPEND_HANDLER(int64, int64_t)
DEFINE_APPEND_HANDLER(uint64, uint64_t)
DEFINE_APPEND_HANDLER(double, double)
// Appends a string or 'bytes' string to a repeated field.
static void* appendstr_handler(void *closure,
const void *hd,
size_t size_hint) {
PHP_PROTO_UNUSED(hd);
stringfields_parseframe_t* frame =
(stringfields_parseframe_t*)malloc(sizeof(stringfields_parseframe_t));
PHP_PROTO_ASSERT(frame != NULL);
frame->closure = closure;
stringsink_init(&frame->sink);
return frame;
}
static bool appendstr_end_handler(void *closure, const void *hd) {
stringfields_parseframe_t* frame = closure;
zval* array = (zval*)frame->closure;
TSRMLS_FETCH();
RepeatedField* intern = UNBOX(RepeatedField, array);
#if PHP_MAJOR_VERSION < 7
zval* str;
MAKE_STD_ZVAL(str);
PHP_PROTO_ZVAL_STRINGL(str, frame->sink.ptr, frame->sink.len, 1);
repeated_field_push_native(intern, &str);
#else
zend_string* str = zend_string_init(frame->sink.ptr, frame->sink.len, 1);
repeated_field_push_native(intern, &str);
#endif
stringsink_uninit(&frame->sink);
free(frame);
return true;
}
// Handlers that append primitive values to a repeated field.
#define DEFINE_SINGULAR_HANDLER(type, ctype) \
static bool type##_handler(void* closure, const void* hd, \
ctype val) { \
MessageHeader* msg = (MessageHeader*)closure; \
const size_t *ofs = hd; \
DEREF(message_data(msg), *ofs, ctype) = val; \
return true; \
}
DEFINE_SINGULAR_HANDLER(bool, bool)
DEFINE_SINGULAR_HANDLER(int32, int32_t)
DEFINE_SINGULAR_HANDLER(uint32, uint32_t)
DEFINE_SINGULAR_HANDLER(float, float)
DEFINE_SINGULAR_HANDLER(int64, int64_t)
DEFINE_SINGULAR_HANDLER(uint64, uint64_t)
DEFINE_SINGULAR_HANDLER(double, double)
#undef DEFINE_SINGULAR_HANDLER
#if PHP_MAJOR_VERSION < 7
static void *empty_php_string(zval** value_ptr) {
SEPARATE_ZVAL_IF_NOT_REF(value_ptr);
if (Z_TYPE_PP(value_ptr) == IS_STRING &&
!IS_INTERNED(Z_STRVAL_PP(value_ptr))) {
FREE(Z_STRVAL_PP(value_ptr));
}
ZVAL_EMPTY_STRING(*value_ptr);
return (void*)(*value_ptr);
}
#else
static void *empty_php_string(zval* value_ptr) {
if (Z_TYPE_P(value_ptr) == IS_STRING) {
zend_string_release(Z_STR_P(value_ptr));
}
ZVAL_EMPTY_STRING(value_ptr);
return value_ptr;
}
#endif
#if PHP_MAJOR_VERSION < 7
static void new_php_string(zval** value_ptr, const char* str, size_t len) {
SEPARATE_ZVAL_IF_NOT_REF(value_ptr);
if (Z_TYPE_PP(value_ptr) == IS_STRING &&
!IS_INTERNED(Z_STRVAL_PP(value_ptr))) {
FREE(Z_STRVAL_PP(value_ptr));
}
ZVAL_STRINGL(*value_ptr, str, len, 1);
}
#else
static void new_php_string(zval* value_ptr, const char* str, size_t len) {
if (Z_TYPE_P(value_ptr) == IS_STRING) {
zend_string_release(Z_STR_P(value_ptr));
}
ZVAL_NEW_STR(value_ptr, zend_string_init(str, len, 0));
}
#endif
// Sets a non-repeated string/bytes field in a message.
static void* str_handler(void *closure,
const void *hd,
size_t size_hint) {
PHP_PROTO_UNUSED(hd);
stringfields_parseframe_t* frame =
(stringfields_parseframe_t*)malloc(sizeof(stringfields_parseframe_t));
PHP_PROTO_ASSERT(frame != NULL);
frame->closure = closure;
stringsink_init(&frame->sink);
return frame;
}
static bool str_end_handler(void *closure, const void *hd) {
stringfields_parseframe_t* frame = closure;
const upb_fielddef **field = (const upb_fielddef **) hd;
MessageHeader* msg = (MessageHeader*)frame->closure;
CACHED_VALUE* cached = find_zval_property(msg, *field);
new_php_string(cached, frame->sink.ptr, frame->sink.len);
stringsink_uninit(&frame->sink);
free(frame);
return true;
}
static bool map_str_end_handler(void *closure, const void *hd) {
stringfields_parseframe_t* frame = closure;
const size_t *ofs = hd;
MessageHeader* msg = (MessageHeader*)frame->closure;
new_php_string(DEREF(message_data(msg), *ofs, CACHED_VALUE*),
frame->sink.ptr, frame->sink.len);
stringsink_uninit(&frame->sink);
free(frame);
return true;
}
static size_t stringdata_handler(void* closure, const void* hd,
const char* str, size_t len,
const upb_bufhandle* handle) {
stringfields_parseframe_t* frame = closure;
return stringsink_string(&frame->sink, hd, str, len, handle);
}
// Appends a submessage to a repeated field.
static void *appendsubmsg_handler(void *closure, const void *hd) {
zval* array = (zval*)closure;
TSRMLS_FETCH();
RepeatedField* intern = UNBOX(RepeatedField, array);
const submsg_handlerdata_t *submsgdata = hd;
DescriptorInternal* subdesc = get_msgdef_desc(submsgdata->md);
register_class(subdesc, false TSRMLS_CC);
zend_class_entry* subklass = subdesc->klass;
MessageHeader* submsg;
#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);
return submsg;
}
// Appends a wrapper submessage to a repeated field.
static void *appendwrappersubmsg_handler(void *closure, const void *hd) {
zval* array = (zval*)closure;
TSRMLS_FETCH();
RepeatedField* intern = UNBOX(RepeatedField, array);
const submsg_handlerdata_t *submsgdata = hd;
DescriptorInternal* subdesc = get_msgdef_desc(submsgdata->md);
register_class(subdesc, false TSRMLS_CC);
zend_class_entry* subklass = subdesc->klass;
MessageHeader* submsg;
wrapperfields_parseframe_t* frame =
(wrapperfields_parseframe_t*)malloc(sizeof(wrapperfields_parseframe_t));
#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);
frame->closure = closure;
frame->submsg = submsg;
frame->is_msg = true;
return frame;
}
// Sets a non-repeated submessage field in a message.
static void *submsg_handler(void *closure, const void *hd) {
MessageHeader* msg = closure;
const submsg_handlerdata_t* submsgdata = hd;
TSRMLS_FETCH();
DescriptorInternal* subdesc = get_msgdef_desc(submsgdata->md);
register_class(subdesc, false TSRMLS_CC);
zend_class_entry* subklass = subdesc->klass;
zval* submsg_php;
MessageHeader* submsg;
CACHED_VALUE* cached = find_zval_property(msg, submsgdata->fd);
if (Z_TYPE_P(CACHED_PTR_TO_ZVAL_PTR(cached)) == IS_NULL) {
#if PHP_MAJOR_VERSION < 7
zval val;
ZVAL_OBJ(&val, subklass->create_object(subklass TSRMLS_CC));
MessageHeader* intern = UNBOX(MessageHeader, &val);
custom_data_init(subklass, intern PHP_PROTO_TSRMLS_CC);
REPLACE_ZVAL_VALUE(cached, &val, 1);
zval_dtor(&val);
#else
zend_object* obj = subklass->create_object(subklass TSRMLS_CC);
ZVAL_OBJ(cached, obj);
MessageHeader* intern = UNBOX_HASHTABLE_VALUE(MessageHeader, obj);
custom_data_init(subklass, intern PHP_PROTO_TSRMLS_CC);
#endif
}
submsg_php = CACHED_PTR_TO_ZVAL_PTR(cached);
submsg = UNBOX(MessageHeader, submsg_php);
return submsg;
}
static void *map_submsg_handler(void *closure, const void *hd) {
MessageHeader* msg = closure;
const submsg_handlerdata_t* submsgdata = hd;
TSRMLS_FETCH();
DescriptorInternal* subdesc = get_msgdef_desc(submsgdata->md);
register_class(subdesc, false TSRMLS_CC);
zend_class_entry* subklass = subdesc->klass;
zval* submsg_php;
MessageHeader* submsg;
CACHED_VALUE* cached =
DEREF(message_data(msg), submsgdata->ofs, CACHED_VALUE*);
if (Z_TYPE_P(CACHED_PTR_TO_ZVAL_PTR(cached)) == IS_NULL) {
#if PHP_MAJOR_VERSION < 7
zval val;
ZVAL_OBJ(&val, subklass->create_object(subklass TSRMLS_CC));
MessageHeader* intern = UNBOX(MessageHeader, &val);
custom_data_init(subklass, intern PHP_PROTO_TSRMLS_CC);
REPLACE_ZVAL_VALUE(cached, &val, 1);
zval_dtor(&val);
#else
zend_object* obj = subklass->create_object(subklass TSRMLS_CC);
ZVAL_OBJ(cached, obj);
MessageHeader* intern = UNBOX_HASHTABLE_VALUE(MessageHeader, obj);
custom_data_init(subklass, intern PHP_PROTO_TSRMLS_CC);
#endif
}
submsg_php = CACHED_PTR_TO_ZVAL_PTR(cached);
submsg = UNBOX(MessageHeader, submsg_php);
return submsg;
}
static void *map_wrapper_submsg_handler(void *closure, const void *hd) {
MessageHeader* msg = closure;
const submsg_handlerdata_t* submsgdata = hd;
TSRMLS_FETCH();
DescriptorInternal* subdesc = get_msgdef_desc(submsgdata->md);
register_class(subdesc, false TSRMLS_CC);
zend_class_entry* subklass = subdesc->klass;
zval* submsg_php;
MessageHeader* submsg;
wrapperfields_parseframe_t* frame =
(wrapperfields_parseframe_t*)malloc(sizeof(wrapperfields_parseframe_t));
CACHED_VALUE* cached =
DEREF(message_data(msg), submsgdata->ofs, CACHED_VALUE*);
if (Z_TYPE_P(CACHED_PTR_TO_ZVAL_PTR(cached)) == IS_NULL) {
#if PHP_MAJOR_VERSION < 7
zval val;
ZVAL_OBJ(&val, subklass->create_object(subklass TSRMLS_CC));
MessageHeader* intern = UNBOX(MessageHeader, &val);
custom_data_init(subklass, intern PHP_PROTO_TSRMLS_CC);
REPLACE_ZVAL_VALUE(cached, &val, 1);
zval_dtor(&val);
#else
zend_object* obj = subklass->create_object(subklass TSRMLS_CC);
ZVAL_OBJ(cached, obj);
MessageHeader* intern = UNBOX_HASHTABLE_VALUE(MessageHeader, obj);
custom_data_init(subklass, intern PHP_PROTO_TSRMLS_CC);
#endif
}
submsg_php = CACHED_PTR_TO_ZVAL_PTR(cached);
submsg = UNBOX(MessageHeader, submsg_php);
frame->closure = closure;
frame->submsg = submsg;
frame->is_msg = true;
return frame;
}
// Handler data for startmap/endmap handlers.
typedef struct {
const upb_fielddef* fd;
const upb_msgdef* value_md;
upb_fieldtype_t key_field_type;
upb_fieldtype_t value_field_type;
} map_handlerdata_t;
// Temporary frame for map parsing: at the beginning of a map entry message, a
// submsg handler allocates a frame to hold (i) a reference to the Map object
// into which this message will be inserted and (ii) storage slots to
// temporarily hold the key and value for this map entry until the end of the
// submessage. When the submessage ends, another handler is called to insert the
// value into the map.
typedef struct {
char key_storage[NATIVE_SLOT_MAX_SIZE];
char value_storage[NATIVE_SLOT_MAX_SIZE];
} map_parse_frame_data_t;
PHP_PROTO_WRAP_OBJECT_START(map_parse_frame_t)
map_parse_frame_data_t* data; // Place needs to be consistent with
// MessageHeader.
zval* map;
// In php7, we cannot allocate zval dynamically. So we need to add zval here
// to help decoding.
zval key_zval;
zval value_zval;
PHP_PROTO_WRAP_OBJECT_END
typedef struct map_parse_frame_t map_parse_frame_t;
static void map_slot_init(
void* memory, upb_fieldtype_t type, zval* cache,
const upb_msgdef* value_msg PHP_PROTO_TSRMLS_DC) {
switch (type) {
case UPB_TYPE_STRING:
case UPB_TYPE_BYTES: {
#if PHP_MAJOR_VERSION < 7
// Store zval** in memory in order to be consistent with the layout of
// singular fields.
zval** holder = ALLOC(zval*);
*(zval***)memory = holder;
zval* tmp;
MAKE_STD_ZVAL(tmp);
PHP_PROTO_ZVAL_STRINGL(tmp, "", 0, 1);
*holder = tmp;
#else
*(zval**)memory = cache;
PHP_PROTO_ZVAL_STRINGL(*(zval**)memory, "", 0, 1);
#endif
break;
}
case UPB_TYPE_MESSAGE: {
DescriptorInternal* subdesc = get_msgdef_desc(value_msg);
register_class(subdesc, false TSRMLS_CC);
zend_class_entry* subklass = subdesc->klass;
MessageHeader* submsg;
#if PHP_MAJOR_VERSION < 7
zval** holder = ALLOC(zval*);
zval* tmp;
MAKE_STD_ZVAL(tmp);
ZVAL_OBJ(tmp, subklass->create_object(subklass TSRMLS_CC));
submsg = UNBOX(MessageHeader, tmp);
custom_data_init(subklass, submsg PHP_PROTO_TSRMLS_CC);
*holder = tmp;
*(zval***)memory = holder;
#else
*(zval**)memory = cache;
ZVAL_OBJ(*(zval**)memory, subklass->create_object(subklass TSRMLS_CC));
submsg = UNBOX(MessageHeader, cache);
custom_data_init(subklass, submsg PHP_PROTO_TSRMLS_CC);
#endif
break;
}
default:
native_slot_init(type, memory, NULL);
}
}
static void map_slot_uninit(void* memory, upb_fieldtype_t type) {
switch (type) {
case UPB_TYPE_MESSAGE:
case UPB_TYPE_STRING:
case UPB_TYPE_BYTES: {
#if PHP_MAJOR_VERSION < 7
zval** holder = *(zval***)memory;
zval_ptr_dtor(holder);
FREE(holder);
#else
php_proto_zval_ptr_dtor(*(zval**)memory);
#endif
break;
}
default:
break;
}
}
static void map_slot_key(upb_fieldtype_t type, const void* from,
const char** keyval,
size_t* length) {
if (type == UPB_TYPE_STRING) {
#if PHP_MAJOR_VERSION < 7
zval* key_php = **(zval***)from;
#else
zval* key_php = *(zval**)from;
#endif
*keyval = Z_STRVAL_P(key_php);
*length = Z_STRLEN_P(key_php);
} else {
*keyval = from;
*length = native_slot_size(type);
}
}
static void map_slot_value(upb_fieldtype_t type, const void* from,
upb_value* v) {
size_t len;
void* to = upb_value_memory(v);
#ifndef NDEBUG
v->ctype = UPB_CTYPE_UINT64;
#endif
memset(to, 0, native_slot_size(type));
switch (type) {
#if PHP_MAJOR_VERSION < 7
case UPB_TYPE_STRING:
case UPB_TYPE_BYTES:
case UPB_TYPE_MESSAGE: {
*(zval**)to = **(zval***)from;
Z_ADDREF_PP((zval**)to);
break;
}
#else
case UPB_TYPE_STRING:
case UPB_TYPE_BYTES:
*(zend_string**)to = Z_STR_P(*(zval**)from);
zend_string_addref(*(zend_string**)to);
break;
case UPB_TYPE_MESSAGE:
if (!ZVAL_IS_NULL(*(zval**)from)) {
*(zend_object**)to = Z_OBJ_P(*(zval**)from);
GC_ADDREF(*(zend_object**)to);
}
break;
#endif
default:
len = native_slot_size(type);
memcpy(to, from, len);
}
}
// Handler to begin a map entry: allocates a temporary frame. This is the
// 'startsubmsg' handler on the msgdef that contains the map field.
static void *startmapentry_handler(void *closure, const void *hd) {
MessageHeader* msg = closure;
const map_handlerdata_t* mapdata = hd;
CACHED_VALUE* cache = find_zval_property(msg, mapdata->fd);
TSRMLS_FETCH();
map_field_ensure_created(mapdata->fd, cache PHP_PROTO_TSRMLS_CC);
zval* map = CACHED_PTR_TO_ZVAL_PTR(cache);
map_parse_frame_t* frame = ALLOC(map_parse_frame_t);
frame->data = ALLOC(map_parse_frame_data_t);
frame->map = map;
map_slot_init(&frame->data->key_storage, mapdata->key_field_type,
&frame->key_zval, NULL PHP_PROTO_TSRMLS_CC);
map_slot_init(&frame->data->value_storage, mapdata->value_field_type,
&frame->value_zval, mapdata->value_md PHP_PROTO_TSRMLS_CC);
return frame;
}
// Handler to end a map entry: inserts the value defined during the message into
// the map. This is the 'endmsg' handler on the map entry msgdef.
static bool endmap_handler(void* closure, const void* hd, upb_status* s) {
map_parse_frame_t* frame = closure;
const map_handlerdata_t* mapdata = hd;
TSRMLS_FETCH();
Map *map = UNBOX(Map, frame->map);
const char* keyval = NULL;
upb_value v;
size_t length;
map_slot_key(map->key_type, &frame->data->key_storage, &keyval, &length);
map_slot_value(map->value_type, &frame->data->value_storage, &v);
map_index_set(map, keyval, length, v);
map_slot_uninit(&frame->data->key_storage, mapdata->key_field_type);
map_slot_uninit(&frame->data->value_storage, mapdata->value_field_type);
FREE(frame->data);
FREE(frame);
return true;
}
// Allocates a new map_handlerdata_t given the map entry message definition. If
// the offset of the field within the parent message is also given, that is
// added to the handler data as well. Note that this is called *twice* per map
// field: once in the parent message handler setup when setting the startsubmsg
// handler and once in the map entry message handler setup when setting the
// key/value and endmsg handlers. The reason is that there is no easy way to
// pass the handlerdata down to the sub-message handler setup.
static map_handlerdata_t* new_map_handlerdata(
const upb_fielddef* field,
const upb_msgdef* mapentry_def) {
const upb_fielddef* key_field;
const upb_fielddef* value_field;
// TODO(teboring): Use emalloc and efree.
map_handlerdata_t* hd =
(map_handlerdata_t*)malloc(sizeof(map_handlerdata_t));
PHP_PROTO_ASSERT(hd != NULL);
hd->fd = field;
key_field = upb_msgdef_itof(mapentry_def, MAP_KEY_FIELD);
PHP_PROTO_ASSERT(key_field != NULL);
hd->key_field_type = upb_fielddef_type(key_field);
value_field = upb_msgdef_itof(mapentry_def, MAP_VALUE_FIELD);
PHP_PROTO_ASSERT(value_field != NULL);
hd->value_field_type = upb_fielddef_type(value_field);
if (upb_fielddef_type(value_field) == UPB_TYPE_MESSAGE) {
hd->value_md = upb_fielddef_msgsubdef(value_field);
} else {
hd->value_md = NULL;
}
return hd;
}
// Handlers that set primitive values in oneofs.
#define DEFINE_ONEOF_HANDLER(type, ctype) \
static bool oneof##type##_handler(void* closure, const void* hd, \
ctype val) { \
const oneof_handlerdata_t* oneofdata = hd; \
MessageHeader* msg = (MessageHeader*)closure; \
DEREF(message_data(closure), oneofdata->case_ofs, uint32_t) = \
oneofdata->oneof_case_num; \
DEREF(message_data(closure), oneofdata->ofs, ctype) = val; \
return true; \
}
DEFINE_ONEOF_HANDLER(bool, bool)
DEFINE_ONEOF_HANDLER(int32, int32_t)
DEFINE_ONEOF_HANDLER(uint32, uint32_t)
DEFINE_ONEOF_HANDLER(float, float)
DEFINE_ONEOF_HANDLER(int64, int64_t)
DEFINE_ONEOF_HANDLER(uint64, uint64_t)
DEFINE_ONEOF_HANDLER(double, double)
#undef DEFINE_ONEOF_HANDLER
static void oneof_cleanup(MessageHeader* msg,
const oneof_handlerdata_t* oneofdata) {
uint32_t old_case_num =
DEREF(message_data(msg), oneofdata->case_ofs, uint32_t);
if (old_case_num == 0) {
return;
}
const upb_fielddef* old_field =
upb_msgdef_itof(oneofdata->parent_md, old_case_num);
bool need_clean = false;
switch (upb_fielddef_type(old_field)) {
case UPB_TYPE_STRING:
case UPB_TYPE_BYTES:
need_clean = true;
break;
case UPB_TYPE_MESSAGE:
if (oneofdata->oneof_case_num != old_case_num) {
need_clean = true;
}
break;
default:
break;
}
if (need_clean) {
#if PHP_MAJOR_VERSION < 7
SEPARATE_ZVAL_IF_NOT_REF(
DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*));
php_proto_zval_ptr_dtor(
*DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*));
MAKE_STD_ZVAL(*DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*));
ZVAL_NULL(*DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*));
#endif
}
}
// Handlers for string/bytes in a oneof.
static void *oneofbytes_handler(void *closure,
const void *hd,
size_t size_hint) {
MessageHeader* msg = closure;
const oneof_handlerdata_t *oneofdata = hd;
oneof_cleanup(msg, oneofdata);
DEREF(message_data(msg), oneofdata->case_ofs, uint32_t) =
oneofdata->oneof_case_num;
DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*) =
OBJ_PROP(&msg->std, oneofdata->property_ofs);
return empty_php_string(DEREF(
message_data(msg), oneofdata->ofs, CACHED_VALUE*));
}
static bool oneofstr_end_handler(void *closure, const void *hd) {
stringfields_parseframe_t* frame = closure;
MessageHeader* msg = (MessageHeader*)frame->closure;
const oneof_handlerdata_t *oneofdata = hd;
oneof_cleanup(msg, oneofdata);
DEREF(message_data(msg), oneofdata->case_ofs, uint32_t) =
oneofdata->oneof_case_num;
DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*) =
OBJ_PROP(&msg->std, oneofdata->property_ofs);
new_php_string(DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*),
frame->sink.ptr, frame->sink.len);
stringsink_uninit(&frame->sink);
free(frame);
return true;
}
static void *oneofstr_handler(void *closure,
const void *hd,
size_t size_hint) {
PHP_PROTO_UNUSED(hd);
stringfields_parseframe_t* frame =
(stringfields_parseframe_t*)malloc(sizeof(stringfields_parseframe_t));
PHP_PROTO_ASSERT(frame != NULL);
frame->closure = closure;
stringsink_init(&frame->sink);
return frame;
}
// Handler for a submessage field in a oneof.
static void* oneofsubmsg_handler(void* closure, const void* hd) {
MessageHeader* msg = closure;
const oneof_handlerdata_t *oneofdata = hd;
uint32_t oldcase = DEREF(message_data(msg), oneofdata->case_ofs, uint32_t);
TSRMLS_FETCH();
DescriptorInternal* subdesc = get_msgdef_desc(oneofdata->md);
register_class(subdesc, false TSRMLS_CC);
zend_class_entry* subklass = subdesc->klass;
zval* submsg_php;
MessageHeader* submsg;
if (oldcase != oneofdata->oneof_case_num) {
oneof_cleanup(msg, oneofdata);
// Create new message.
DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*) =
OBJ_PROP(&msg->std, oneofdata->property_ofs);
#if PHP_MAJOR_VERSION < 7
zval val;
ZVAL_OBJ(&val, subklass->create_object(subklass TSRMLS_CC));
REPLACE_ZVAL_VALUE(DEREF(message_data(msg), oneofdata->ofs, zval**),
&val, 1);
zval_dtor(&val);
#else
zend_object* obj = subklass->create_object(subklass TSRMLS_CC);
ZVAL_OBJ(DEREF(message_data(msg), oneofdata->ofs, zval*), obj);
#endif
}
DEREF(message_data(msg), oneofdata->case_ofs, uint32_t) =
oneofdata->oneof_case_num;
submsg_php = CACHED_PTR_TO_ZVAL_PTR(
DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*));
submsg = UNBOX(MessageHeader, submsg_php);
custom_data_init(subklass, submsg PHP_PROTO_TSRMLS_CC);
return submsg;
}
// Sets a non-repeated wrapper submessage field in a message.
static void* wrapper_submsg_handler(void* closure, const void* hd) {
MessageHeader* msg = closure;
const submsg_handlerdata_t* submsgdata = hd;
TSRMLS_FETCH();
DescriptorInternal* subdesc = get_msgdef_desc(submsgdata->md);
register_class(subdesc, false TSRMLS_CC);
zend_class_entry* subklass = subdesc->klass;
zval* submsg_php;
MessageHeader* submsg;
wrapperfields_parseframe_t* frame =
(wrapperfields_parseframe_t*)malloc(sizeof(wrapperfields_parseframe_t));
CACHED_VALUE* cached = find_zval_property(msg, submsgdata->fd);
submsg_php = CACHED_PTR_TO_ZVAL_PTR(cached);
frame->closure = closure;
if (Z_TYPE_P(CACHED_PTR_TO_ZVAL_PTR(cached)) == IS_OBJECT) {
submsg = UNBOX(MessageHeader, submsg_php);
frame->submsg = submsg;
frame->is_msg = true;