forked from allegro/php-protobuf
-
Notifications
You must be signed in to change notification settings - Fork 2
/
protobuf.c
1258 lines (991 loc) · 33.9 KB
/
protobuf.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
#include <php.h>
#include <ext/standard/php_string.h>
#include <Zend/zend_exceptions.h>
#include "php_protobuf.h"
#include "protobuf.h"
#include "reader.h"
#include "writer.h"
#define PB_COMPILE_ERROR(message, ...) PB_COMPILE_ERROR_EX(getThis(), message, __VA_ARGS__)
#define PB_COMPILE_ERROR_EX(this, message, ...) \
zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "%s: compile error - " #message, ZSTR_VAL(Z_OBJCE_P(this)->name), __VA_ARGS__)
#define PB_CONSTANT(name) \
zend_declare_class_constant_long(pb_entry, #name, sizeof(#name) - 1, name TSRMLS_CC)
#define PB_PARSE_ERROR(message, ...) \
PB_PARSE_ERROR_EX(getThis(), message, __VA_ARGS__)
#define PB_PARSE_ERROR_EX(this, message, ...) \
zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "%s: parse error - " #message, ZSTR_VAL(Z_OBJCE_P(this)->name), __VA_ARGS__)
#define IS_BOOL(zval) ((Z_TYPE(zval) == IS_FALSE) || (Z_TYPE(zval) == IS_TRUE))
#define PB_RESET_METHOD "reset"
#define PB_DUMP_METHOD "dump"
#define PB_FIELDS_METHOD "fields"
#define PB_PARSE_FROM_STRING_METHOD "parseFromString"
#define PB_SERIALIZE_TO_STRING_METHOD "serializeToString"
#define PB_PRINT_DEBUG_STRING_METHOD "printDebugString"
#define PB_FIELD_NAME "name"
#define PB_FIELD_PACKED "packed"
#define PB_FIELD_REQUIRED "required"
#define PB_FIELD_TYPE "type"
#define PB_VALUES_PROPERTY "values"
#define RETURN_THIS() RETURN_ZVAL(getThis(), 1, 0);
// when opcache is enabled initial array could be stored in
// shared memory as an immutable one, in this case it should be separated
#define ARRAY_ADD_NEXT_INDEX(array, zval_p) \
if (Z_IMMUTABLE_P(array) || Z_REFCOUNTED_P(array)) SEPARATE_ARRAY(array); \
add_next_index_zval((array), (zval_p))
#define IS_32_BIT (sizeof(zend_long) < sizeof(int64_t))
#define ZVAL_INT64(zval, value) \
if ((value) > ZEND_LONG_MAX || (value) < ZEND_LONG_MIN) { \
ZVAL_DOUBLE(zval, (double)(value)); \
} else { \
ZVAL_LONG(zval, (zend_long)(value)); \
}
#define Z_LVAL_INT64(zval, int64_out_p) \
if (Z_TYPE_P(zval) == IS_DOUBLE) { \
*int64_out_p = (int64_t)Z_DVAL_P(zval); \
} else { \
*int64_out_p = (int64_t)Z_LVAL_P(zval); \
}
enum
{
PB_TYPE_DOUBLE = 1,
PB_TYPE_FIXED32,
PB_TYPE_FIXED64,
PB_TYPE_FLOAT,
PB_TYPE_INT,
PB_TYPE_SIGNED_INT,
PB_TYPE_STRING,
PB_TYPE_BOOL
};
zend_class_entry *pb_entry;
static int pb_assign_value(zval *this, zval *dst, zval *src, zend_ulong field_number);
static int pb_print_field_value(zval *value, zend_long level, zend_bool only_set);
static int pb_dump_field_value(zval *value, zend_long level, zend_bool only_set);
static int pb_print_debug_field_value(zval *value, zend_long level);
static zval *pb_get_field_type(zval *this, zval *field_descriptors, zend_ulong field_number);
static zval *pb_get_field_descriptor(zval *this, zval *field_descriptors, zend_ulong field_number);
static int pb_get_field_descriptors(zval *this, zval* return_value);
static const char *pb_get_field_name(zval *this, zend_ulong field_number);
static int pb_get_wire_type(int field_type);
static const char *pb_get_wire_type_name(int wire_type);
static zval *pb_get_value(zval *this, zval *values, zend_ulong field_number);
static zval *pb_get_values(zval *this);
static int pb_parse_field_value(zval *this, reader_t *reader, zend_ulong field_number, zend_long field_type, zval *value);
static int pb_serialize_field_value(zval *this, writer_t *writer, zend_ulong field_number, zval *type, zval *value);
static int pb_serialize_packed_field(zval *this, writer_t *writer, zend_ulong field_number, zend_long field_type, zval *values);
static int pb_is_field_packed(zval *field_descriptor);
PHP_METHOD(ProtobufMessage, __construct)
{
zval values_array;
array_init(&values_array);
zend_update_property(pb_entry, getThis(), PB_VALUES_PROPERTY, sizeof(PB_VALUES_PROPERTY) - 1, &values_array);
}
PHP_METHOD(ProtobufMessage, append)
{
zend_long field_number;
zval *array, *value, *values, val;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz", &field_number, &value) == FAILURE) {
RETURN_THIS();
}
if (Z_TYPE_P(value) == IS_NULL)
RETURN_THIS();
if ((values = pb_get_values(getThis())) == NULL)
RETURN_THIS();
if ((array = pb_get_value(getThis(), values, (zend_ulong)field_number)) == NULL)
RETURN_THIS();
if (pb_assign_value(getThis(), &val, value, (zend_ulong)field_number) != 0) {
RETURN_THIS();
}
ARRAY_ADD_NEXT_INDEX(array, &val);
RETURN_THIS();
}
PHP_METHOD(ProtobufMessage, clear)
{
zend_long field_number;
zval *array, *values;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &field_number) == FAILURE) {
return;
}
if ((values = pb_get_values(getThis())) == NULL)
RETURN_THIS();
if ((array = pb_get_value(getThis(), values, (zend_ulong)field_number)) == NULL)
RETURN_THIS();
if (Z_TYPE_P(array) != IS_ARRAY) {
PB_COMPILE_ERROR("'%s' field internal type should be an array", pb_get_field_name(getThis(), (zend_ulong)field_number));
RETURN_THIS();
}
zend_hash_clean(Z_ARRVAL_P(array));
RETURN_THIS();
}
PHP_METHOD(ProtobufMessage, printDebugString)
{
int indent;
char indent_char;
zend_long level = 0;
const char *field_name;
zend_ulong field_number, index;
HashPosition i, j;
zval *field_descriptor, field_descriptors, *val, *value, *values;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &level) == FAILURE || level < 0) {
return;
}
indent = ((int)level) * 2;
indent_char = indent ? ' ': '\0';
if ((values = pb_get_values(getThis())) == NULL)
return;
if (pb_get_field_descriptors(getThis(), &field_descriptors))
return;
int is_loop_finished = 0;
PB_FOREACH(&i, Z_ARRVAL_P(values)) {
if (is_loop_finished)
break;
zend_hash_get_current_key_ex(Z_ARRVAL_P(values), NULL, &field_number, &i);
value = zend_hash_get_current_data_ex(Z_ARRVAL_P(values), &i);
if ((field_descriptor = pb_get_field_descriptor(getThis(), &field_descriptors, field_number)) == NULL)
break;
if ((field_name = pb_get_field_name(getThis(), field_number)) == NULL)
break;
if (Z_TYPE_P(value) == IS_ARRAY) {
if (zend_hash_num_elements(Z_ARRVAL_P(value)) == 0)
continue;
zval *field_type;
if ((field_type = pb_get_field_type(getThis(), field_descriptor, field_number)) == NULL)
break;
int wire = pb_get_wire_type(Z_LVAL_P(field_type));
PB_FOREACH(&j, Z_ARRVAL_P(value)) {
zend_hash_get_current_key_ex(Z_ARRVAL_P(value), NULL, &index, &j);
val = zend_hash_get_current_data_ex(Z_ARRVAL_P(value), &j);
if (Z_TYPE_P(value) == IS_NULL) // make sure it's not Z_TYPE_P(val)
continue;
if ((wire == WIRE_TYPE_LENGTH_DELIMITED && Z_TYPE_P(val) != IS_STRING) || wire == -1) {
php_printf("%*c%s {", indent, indent_char, field_name);
if (pb_print_debug_field_value(val, level + 1) != 0) {
is_loop_finished = 1;
break;
}
php_printf("%*c}\n", indent, indent_char);
} else {
php_printf("%*c%s:", indent, indent_char, field_name);
if (pb_print_debug_field_value(val, level + 1) != 0) {
is_loop_finished = 1;
break;
}
}
}
} else if (Z_TYPE_P(value) == IS_OBJECT) {
php_printf("%*c%s {", indent, indent_char, field_name);
if (pb_print_debug_field_value(value, level + 1) != 0)
break;
php_printf("%*c}\n", indent, indent_char);
} else if (Z_TYPE_P(value) != IS_NULL) {
php_printf("%*c%s:", indent, indent_char, field_name);
if (pb_print_debug_field_value(value, level + 1) != 0)
break;
}
}
zval_ptr_dtor(&field_descriptors);
}
PHP_METHOD(ProtobufMessage, dump)
{
zend_bool only_set = 1;
zend_long level = 0;
const char *field_name;
zend_ulong field_number, index;
HashPosition i, j;
zval *field_descriptor, field_descriptors, *val, *value, *values;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bl", &only_set, &level) == FAILURE || level < 0) {
return;
}
if (pb_get_field_descriptors(getThis(), &field_descriptors))
return;
if ((values = pb_get_values(getThis())) == NULL)
goto fail0;
if (level > 0)
php_printf("%*c%s {\n", 2 * (int) level, ' ', ZSTR_VAL(Z_OBJCE_P(getThis())->name));
else
php_printf("%s {\n", ZSTR_VAL(Z_OBJCE_P(getThis())->name));
PB_FOREACH(&i, Z_ARRVAL_P(values)) {
zend_hash_get_current_key_ex(Z_ARRVAL_P(values), NULL, &field_number, &i);
value = zend_hash_get_current_data_ex(Z_ARRVAL_P(values), &i);
if ((field_descriptor = pb_get_field_descriptor(getThis(), &field_descriptors, field_number)) == NULL)
goto fail0;
if ((field_name = pb_get_field_name(getThis(), field_number)) == NULL)
goto fail0;
if (Z_TYPE_P(value) == IS_ARRAY) {
if (zend_hash_num_elements(Z_ARRVAL_P(value)) > 0 || !only_set) {
php_printf("%*c%lu: %s(%u) => \n", ((int) level + 1) * 2, ' ', (uint64_t)field_number, field_name, zend_hash_num_elements(Z_ARRVAL_P(value)));
if (zend_hash_num_elements(Z_ARRVAL_P(value)) > 0) {
PB_FOREACH(&j, Z_ARRVAL_P(value)) {
zend_hash_get_current_key_ex(Z_ARRVAL_P(value), NULL, &index, &j);
val = zend_hash_get_current_data_ex(Z_ARRVAL_P(value), &j);
php_printf("%*c[%lu] =>", ((int) level + 2) * 2, ' ', (uint64_t)index);
if (pb_dump_field_value(val, level + 3, only_set) != 0)
goto fail0;
}
} else
php_printf("%*cempty\n", ((int) level + 2) * 2, ' ');
}
} else if (Z_TYPE_P(value) != IS_NULL || !only_set) {
php_printf("%*c%lu: %s =>", 2 * ((int) level + 1), ' ', (uint64_t)field_number, field_name);
if (pb_dump_field_value(value, level + 1, only_set) != 0)
goto fail0;
}
}
if (level > 0)
php_printf("%*c}\n", 2 * (int) level, ' ');
else
php_printf("}\n");
fail0:
zval_ptr_dtor(&field_descriptors);
}
PHP_METHOD(ProtobufMessage, count)
{
zend_long field_number;
zval *value, *values;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &field_number) == FAILURE) {
return;
}
if ((values = pb_get_values(getThis())) == NULL)
return;
if ((value = pb_get_value(getThis(), values, (zend_ulong)field_number)) == NULL)
return;
if (Z_TYPE_P(value) == IS_ARRAY) {
RETURN_LONG(zend_hash_num_elements(Z_ARRVAL_P(value)));
} else {
PB_COMPILE_ERROR("'%s' field internal type should be an array", pb_get_field_name(getThis(), (zend_ulong)field_number));
return;
}
}
PHP_METHOD(ProtobufMessage, get)
{
zend_long field_number, index = -1;
zval *val, *value, *values;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &field_number, &index) == FAILURE) {
return;
}
if ((values = pb_get_values(getThis())) == NULL)
return;
if ((value = pb_get_value(getThis(), values, (zend_ulong)field_number)) == NULL)
return;
if (index != -1) {
if (Z_TYPE_P(value) != IS_ARRAY) {
PB_COMPILE_ERROR("'%s' field internal type should be an array", pb_get_field_name(getThis(), (zend_ulong)field_number));
return;
}
val = zend_hash_index_find(Z_ARRVAL_P(value), index);
if (val == NULL)
return;
value = val;
}
RETURN_ZVAL(value, 1, 0);
}
PHP_METHOD(ProtobufMessage, parseFromString)
{
char *pack, *str, *subpack;
zend_class_entry *sub_ce;
reader_t reader, packed_reader;
uint8_t wire_type;
zend_ulong field_number;
uint64_t next_field_number;
int32_t int32_value;
int64_t int64_value;
int expected_wire_type, str_size, subpack_size, ret, field_repeated;
size_t pack_size;
zval arg, *args, *field_descriptor, *field_type, field_descriptors, name, *old_value, value, *values, zret;
int bool_value;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &pack, &pack_size) == FAILURE)
return;
ZVAL_STRINGL(&name, PB_RESET_METHOD, sizeof(PB_RESET_METHOD) - 1);
if (call_user_function(NULL, getThis(), &name, &zret, 0, NULL TSRMLS_CC) == FAILURE) {
zval_ptr_dtor(&name);
return;
}
zval_ptr_dtor(&name);
zval_ptr_dtor(&zret);
if (pb_get_field_descriptors(getThis(), &field_descriptors))
return;
if ((values = pb_get_values(getThis())) == NULL)
goto fail0;
reader_init(&reader, pack, pack_size);
while (reader_has_more(&reader)) {
if (reader_read_tag(&reader, &next_field_number, &wire_type) != 0)
break;
field_number = (zend_ulong)next_field_number;
if ((field_descriptor = zend_hash_index_find(Z_ARRVAL(field_descriptors), field_number)) == NULL) {
switch (wire_type)
{
case WIRE_TYPE_VARINT:
ret = reader_skip_varint(&reader);
break;
case WIRE_TYPE_64BIT:
ret = reader_skip_64bit(&reader);
break;
case WIRE_TYPE_LENGTH_DELIMITED:
ret = reader_skip_length_delimited(&reader);
break;
case WIRE_TYPE_32BIT:
ret = reader_skip_32bit(&reader);
break;
default:
PB_PARSE_ERROR("unexpected wire type %ld for unexpected %lu field", wire_type, (uint64_t)field_number);
goto fail0;
}
if (ret != 0) {
PB_PARSE_ERROR("parse unexpected %lu field of wire type %s fail", (uint64_t)field_number, pb_get_wire_type_name(wire_type));
goto fail0;
}
continue;
}
if ((field_type = pb_get_field_type(getThis(), field_descriptor, field_number)) == NULL)
goto fail0;
if ((old_value = pb_get_value(getThis(), values, field_number)) == NULL)
goto fail0;
ZVAL_NULL(&value);
field_repeated = (Z_TYPE_P(old_value) == IS_ARRAY);
if (Z_TYPE_P(field_type) == IS_STRING) {
if (wire_type != WIRE_TYPE_LENGTH_DELIMITED) {
PB_PARSE_ERROR("'%s' field wire type is %s but should be %s", pb_get_field_name(getThis(), field_number), pb_get_wire_type_name(wire_type), pb_get_wire_type_name(WIRE_TYPE_LENGTH_DELIMITED));
goto fail0;
}
if ((sub_ce = zend_lookup_class_ex(Z_STR_P(field_type), NULL, 1)) == NULL) {
PB_COMPILE_ERROR("class %s for '%s' does not exist", Z_STRVAL_P(field_type), pb_get_field_name(getThis(), field_number));
goto fail0;
}
if ((ret = reader_read_string(&reader, &subpack, &subpack_size)) == 0) {
object_init_ex(&value, sub_ce);
ZVAL_STRINGL(&name, ZEND_CONSTRUCTOR_FUNC_NAME, sizeof(ZEND_CONSTRUCTOR_FUNC_NAME) - 1);
if (call_user_function(NULL, &value, &name, &zret, 0, NULL TSRMLS_CC) == FAILURE) {
zval_ptr_dtor(&name);
goto fail0;
}
zval_ptr_dtor(&name);
zval_ptr_dtor(&zret);
ZVAL_STRINGL(&name, PB_PARSE_FROM_STRING_METHOD, sizeof(PB_PARSE_FROM_STRING_METHOD) - 1);
ZVAL_STRINGL(&arg, subpack, subpack_size);
args = &arg;
if (call_user_function(NULL, &value, &name, &zret, 1, args TSRMLS_CC) == FAILURE) {
zval_ptr_dtor(&name);
goto fail0;
}
zval_ptr_dtor(&arg);
zval_ptr_dtor(&name);
bool_value = (Z_TYPE(zret) != IS_TRUE) && (Z_TYPE(zret) != IS_FALSE);
zval_ptr_dtor(&zret);
if (bool_value) {
goto fail0;
}
}
} else if (Z_TYPE_P(field_type) == IS_LONG) {
expected_wire_type = pb_get_wire_type(Z_LVAL_P(field_type));
if (field_repeated && wire_type == WIRE_TYPE_LENGTH_DELIMITED && expected_wire_type != WIRE_TYPE_LENGTH_DELIMITED) {
if (reader_read_string(&reader, &str, &str_size) != 0) {
PB_PARSE_ERROR("parse '%s' field fail", pb_get_field_name(getThis(), field_number));
goto fail0;
}
reader_init(&packed_reader, str, str_size);
while (reader_has_more(&packed_reader)) {
if (pb_parse_field_value(getThis(), &packed_reader, field_number, Z_LVAL_P(field_type), &value) != 0) {
return;
}
if (Z_REFCOUNTED(value))
Z_ADDREF(value);
ARRAY_ADD_NEXT_INDEX(old_value, &value);
zval_ptr_dtor(&value);
}
continue;
} else {
if (expected_wire_type != wire_type) {
PB_PARSE_ERROR("'%s' field wire type is %s but should be %s", pb_get_field_name(getThis(), field_number), pb_get_wire_type_name(wire_type), pb_get_wire_type_name(expected_wire_type));
goto fail0;
}
if (pb_parse_field_value(getThis(), &reader, field_number, Z_LVAL_P(field_type), &value) != 0) {
goto fail0;
}
}
} else {
PB_COMPILE_ERROR("unexpected %s type of '%s' field type in field descriptor", zend_get_type_by_const(Z_TYPE_P(field_type)), pb_get_field_name(getThis(), field_number));
return;
}
if (field_repeated) {
if (Z_REFCOUNTED(value))
Z_ADDREF(value);
ARRAY_ADD_NEXT_INDEX(old_value, &value);
} else {
zval_ptr_dtor(old_value);
ZVAL_COPY(old_value, &value);
}
zval_ptr_dtor(&value);
}
zval_ptr_dtor(&field_descriptors);
RETURN_TRUE;
fail0:
zval_ptr_dtor(&field_descriptors);
return;
}
PHP_METHOD(ProtobufMessage, serializeToString)
{
writer_t writer;
char *pack;
size_t pack_size;
zend_ulong field_number;
HashPosition i, j;
zval *array, *field_descriptor, field_descriptors, *required, *type, *value, *values;
if ((values = pb_get_values(getThis())) == NULL)
return;
if (pb_get_field_descriptors(getThis(), &field_descriptors))
return;
writer_init(&writer);
PB_FOREACH(&i, Z_ARRVAL(field_descriptors)) {
zend_hash_get_current_key_ex(Z_ARRVAL(field_descriptors), NULL, &field_number, &i);
field_descriptor = zend_hash_get_current_data_ex(Z_ARRVAL(field_descriptors), &i);
if (Z_TYPE_P(field_descriptor) != IS_ARRAY) {
zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "%s: '%s' field descriptor must be an array", ZSTR_VAL(Z_OBJCE_P(getThis())->name), pb_get_field_name(getThis(), field_number));
goto fail;
}
if ((value = zend_hash_index_find(Z_ARRVAL_P(values), field_number)) == NULL) {
PB_COMPILE_ERROR("missing '%s' field value", pb_get_field_name(getThis(), field_number));
goto fail;
}
if ((type = pb_get_field_type(getThis(), field_descriptor, field_number)) == NULL)
goto fail;
if (Z_TYPE_P(value) == IS_NULL) {
if ((required = zend_hash_str_find(Z_ARRVAL_P(field_descriptor), PB_FIELD_REQUIRED, sizeof(PB_FIELD_REQUIRED) - 1)) == NULL) {
zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "%s: '%s' field is required and must be set", ZSTR_VAL(Z_OBJCE_P(getThis())->name), pb_get_field_name(getThis(), field_number));
}
if (Z_TYPE_P(required) == IS_TRUE) {
zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "%s: '%s' field is required and must be set", ZSTR_VAL(Z_OBJCE_P(getThis())->name), pb_get_field_name(getThis(), field_number));
goto fail;
}
continue;
}
if (Z_TYPE_P(value) == IS_ARRAY) {
array = value;
if (pb_is_field_packed(field_descriptor)) {
if (pb_serialize_packed_field(getThis(), &writer, field_number, Z_LVAL_P(type), array) != 0)
goto fail;
} else {
PB_FOREACH(&j, Z_ARRVAL_P(array)) {
value = zend_hash_get_current_data_ex(Z_ARRVAL_P(array), &j);
if (pb_serialize_field_value(getThis(), &writer, field_number, type, value) != 0)
goto fail;
}
}
} else if (pb_serialize_field_value(getThis(), &writer, field_number, type, value) != 0)
goto fail;
}
pack = writer_get_pack(&writer, &pack_size);
RETVAL_STRINGL(pack, pack_size);
zval_ptr_dtor(&field_descriptors);
writer_free_pack(&writer);
return;
fail:
zval_ptr_dtor(&field_descriptors);
writer_free_pack(&writer);
return;
}
PHP_METHOD(ProtobufMessage, set)
{
zend_long field_number = -1;
zval *old_value, *value, *values;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz", &field_number, &value) == FAILURE) {
RETURN_THIS();
}
if ((values = pb_get_values(getThis())) == NULL)
RETURN_THIS();
if ((old_value = pb_get_value(getThis(), values, (zend_ulong)field_number)) == NULL)
RETURN_THIS();
if (Z_TYPE_P(value) == IS_NULL) {
if (Z_TYPE_P(old_value) != IS_NULL) {
zval_ptr_dtor(old_value);
ZVAL_NULL(old_value);
}
} else {
zval_ptr_dtor(old_value);
pb_assign_value(getThis(), old_value, value, (zend_ulong)field_number);
}
RETURN_THIS();
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_reset, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_append, 0, 0, 2)
ZEND_ARG_INFO(0, position)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_clear, 0, 0, 1)
ZEND_ARG_INFO(0, position)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_printDebugString, 0, 0, 0)
ZEND_ARG_INFO(0, level)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_dump, 0, 0, 0)
ZEND_ARG_INFO(0, onlySet)
ZEND_ARG_INFO(0, level)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_count, 0, 0, 1)
ZEND_ARG_INFO(0, position)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_get, 0, 0, 1)
ZEND_ARG_INFO(0, position)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_parseFromString, 0, 0, 1)
ZEND_ARG_INFO(0, packed)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_serializeToString, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_set, 0, 0, 2)
ZEND_ARG_INFO(0, position)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
static zend_function_entry pb_methods[] = {
PHP_ME(ProtobufMessage, __construct, arginfo_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ABSTRACT_ME(ProtobufMessage, reset, arginfo_reset)
PHP_ME(ProtobufMessage, append, arginfo_append, ZEND_ACC_PUBLIC)
PHP_ME(ProtobufMessage, clear, arginfo_clear, ZEND_ACC_PUBLIC)
PHP_ME(ProtobufMessage, dump, arginfo_dump, ZEND_ACC_PUBLIC)
PHP_ME(ProtobufMessage, count, arginfo_count, ZEND_ACC_PUBLIC)
PHP_ME(ProtobufMessage, get, arginfo_get, ZEND_ACC_PUBLIC)
PHP_ME(ProtobufMessage, parseFromString, arginfo_parseFromString, ZEND_ACC_PUBLIC)
PHP_ME(ProtobufMessage, serializeToString, arginfo_serializeToString, ZEND_ACC_PUBLIC)
PHP_ME(ProtobufMessage, set, arginfo_set, ZEND_ACC_PUBLIC)
PHP_ME(ProtobufMessage, printDebugString, arginfo_printDebugString, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL, 0, 0}
};
PHP_MINIT_FUNCTION(protobuf)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "ProtobufMessage", pb_methods);
pb_entry = zend_register_internal_class(&ce TSRMLS_CC);
zend_declare_property_null(pb_entry, PB_VALUES_PROPERTY, sizeof(PB_VALUES_PROPERTY) - 1, ZEND_ACC_PUBLIC);
PB_CONSTANT(PB_TYPE_DOUBLE);
PB_CONSTANT(PB_TYPE_FIXED32);
PB_CONSTANT(PB_TYPE_FIXED64);
PB_CONSTANT(PB_TYPE_FLOAT);
PB_CONSTANT(PB_TYPE_INT);
PB_CONSTANT(PB_TYPE_SIGNED_INT);
PB_CONSTANT(PB_TYPE_STRING);
PB_CONSTANT(PB_TYPE_BOOL);
return SUCCESS;
}
zend_module_entry protobuf_module_entry = {
STANDARD_MODULE_HEADER,
PHP_PROTOBUF_EXTNAME,
NULL,
PHP_MINIT(protobuf),
NULL,
NULL,
NULL,
NULL,
PHP_PROTOBUF_VERSION,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_PROTOBUF
ZEND_GET_MODULE(protobuf)
#endif
static int pb_assign_value(zval *this, zval *dst, zval *src, zend_ulong field_number)
{
zval *field_descriptor, field_descriptors, tmp, *type;
TSRMLS_FETCH();
if (pb_get_field_descriptors(this, &field_descriptors))
goto fail0;
if ((field_descriptor = pb_get_field_descriptor(this, &field_descriptors, field_number)) == NULL)
goto fail1;
if ((type = pb_get_field_type(this, field_descriptor, field_number)) == NULL)
goto fail1;
ZVAL_DUP(&tmp, src);
if (Z_TYPE_P(type) == IS_LONG) {
switch (Z_LVAL_P(type))
{
case PB_TYPE_DOUBLE:
case PB_TYPE_FLOAT:
if (Z_TYPE(tmp) != IS_DOUBLE)
convert_to_explicit_type(&tmp, IS_DOUBLE);
break;
case PB_TYPE_FIXED32:
if (Z_TYPE(tmp) != IS_LONG)
convert_to_explicit_type(&tmp, IS_LONG);
break;
case PB_TYPE_BOOL:
if (!IS_BOOL(tmp)) {
convert_to_boolean(&tmp);
}
break;
case PB_TYPE_INT:
case PB_TYPE_FIXED64:
case PB_TYPE_SIGNED_INT:
if ((Z_TYPE(tmp) == IS_DOUBLE) && IS_32_BIT) {
if ((double)ZEND_LONG_MAX < Z_DVAL(tmp)) {
// store big value for 32bit systems as double
break;
}
}
convert_to_explicit_type(&tmp, IS_LONG);
break;
case PB_TYPE_STRING:
if (Z_TYPE(tmp) != IS_STRING)
convert_to_explicit_type(&tmp, IS_STRING);
break;
default:
PB_COMPILE_ERROR_EX(this, "unexpected '%s' field type %d in field descriptor", pb_get_field_name(this, field_number), zend_get_type_by_const(Z_LVAL_P(type)));
goto fail2;
}
} else if (Z_TYPE_P(type) != IS_STRING) {
PB_COMPILE_ERROR_EX(this, "unexpected %s type of '%s' field type in field descriptor", zend_get_type_by_const(Z_TYPE_P(type)), pb_get_field_name(this, field_number));
goto fail2;
}
ZVAL_COPY_VALUE(dst, &tmp);
zval_ptr_dtor(&field_descriptors);
return 0;
fail2:
zval_ptr_dtor(&tmp);
fail1:
zval_ptr_dtor(&field_descriptors);
fail0:
return -1;
}
static int pb_print_field_value(zval *value, zend_long level, zend_bool only_set)
{
const char *string_value;
zval tmp;
TSRMLS_FETCH();
if (Z_TYPE_P(value) == IS_NULL)
string_value = "null";
else if (Z_TYPE_P(value) == IS_TRUE)
string_value = "true";
else if (Z_TYPE_P(value) == IS_FALSE)
string_value = "false";
else {
ZVAL_DUP(&tmp, value);
convert_to_string(&tmp);
string_value = Z_STRVAL(tmp);
}
if (Z_TYPE_P(value) == IS_STRING)
php_printf(" \"%s\"\n", string_value);
else
php_printf(" %s\n", string_value);
zval_dtor(&tmp);
return 0;
}
static int pb_print_debug_field_value(zval *value, zend_long level)
{
zval tmp, ret, arg0, args[1];
int dump_result;
TSRMLS_FETCH();
ZVAL_UNDEF(&ret);
if (Z_TYPE_P(value) == IS_OBJECT) {
php_printf("\n");
ZVAL_LONG(&arg0, level);
args[0] = arg0;
ZVAL_STRINGL(&tmp, PB_PRINT_DEBUG_STRING_METHOD, sizeof(PB_PRINT_DEBUG_STRING_METHOD) - 1);
dump_result = call_user_function(NULL, value, &tmp, &ret, 1, args TSRMLS_CC);
zval_ptr_dtor(&tmp);
if (dump_result == FAILURE)
return -1;
else
return 0;
zval_ptr_dtor(&ret);
}
return pb_print_field_value(value, level, 1);
}
static int pb_dump_field_value(zval *value, zend_long level, zend_bool only_set)
{
const char *string_value;
zval tmp, ret, arg0, arg1, args[2];
int dump_result;
TSRMLS_FETCH();
ZVAL_UNDEF(&ret); // call_user_function will segfault without this init
if (Z_TYPE_P(value) == IS_OBJECT) {
php_printf("\n");
ZVAL_BOOL(&arg0, only_set);
ZVAL_LONG(&arg1, level);
args[0] = arg0;
args[1] = arg1;
ZVAL_STRINGL(&tmp, PB_DUMP_METHOD, sizeof(PB_DUMP_METHOD) - 1);
dump_result = call_user_function(NULL, value, &tmp, &ret, 2, args TSRMLS_CC);
zval_ptr_dtor(&tmp);
if (dump_result == FAILURE)
return -1;
else
return 0;
}
return pb_print_field_value(value, level, only_set);
}
static zval *pb_get_field_descriptor(zval *this, zval *field_descriptors, zend_ulong field_number)
{
zval *field_descriptor = NULL;
TSRMLS_FETCH();
field_descriptor = zend_hash_index_find(Z_ARRVAL_P(field_descriptors), field_number);
if (field_descriptor == NULL)
PB_COMPILE_ERROR_EX(this, "missing %lu field descriptor", (uint64_t)field_number);
return field_descriptor;
}
static zval *pb_get_field_type(zval *this, zval *field_descriptor, zend_ulong field_number)
{
zval *field_type;
TSRMLS_FETCH();
field_type = zend_hash_str_find(Z_ARRVAL_P(field_descriptor), PB_FIELD_TYPE, sizeof(PB_FIELD_TYPE) - 1);
if (field_type == NULL)
PB_COMPILE_ERROR_EX(this, "missing '%s' field type property in field descriptor", pb_get_field_name(this, field_number));
return field_type;
}
static int pb_get_field_descriptors(zval *this, zval* return_value)
{
zval descriptors, method;
TSRMLS_FETCH();
ZVAL_STRINGL(&method, PB_FIELDS_METHOD, sizeof(PB_FIELDS_METHOD) - 1);
if (call_user_function(NULL, this, &method, &descriptors, 0, NULL) == FAILURE) {
return -1;
}
*return_value = descriptors;
zval_ptr_dtor(&method);
return 0;
}
static const char *pb_get_field_name(zval *this, zend_ulong field_number)
{
zval *field_descriptor, field_descriptors, *field_name;
TSRMLS_FETCH();
if (pb_get_field_descriptors(this, &field_descriptors))
return NULL;
if ((field_descriptor = pb_get_field_descriptor(this, &field_descriptors, field_number)) == NULL)
goto fail0;
field_name = zend_hash_str_find(Z_ARRVAL_P(field_descriptor), PB_FIELD_NAME, sizeof(PB_FIELD_NAME) - 1);
if (field_name == NULL) {
PB_COMPILE_ERROR_EX(this, "missing %lu field name property in field descriptor", (uint64_t)field_number);
goto fail0;
}
zval_ptr_dtor(&field_descriptors);
return (const char *) Z_STRVAL_P(field_name);
fail0:
zval_ptr_dtor(&field_descriptors);
return NULL;
}
static int pb_get_wire_type(int field_type)
{
int ret;
switch (field_type)
{
case PB_TYPE_DOUBLE:
case PB_TYPE_FIXED64:
ret = WIRE_TYPE_64BIT;
break;
case PB_TYPE_FIXED32:
case PB_TYPE_FLOAT:
ret = WIRE_TYPE_32BIT;
break;
case PB_TYPE_INT:
case PB_TYPE_SIGNED_INT:
case PB_TYPE_BOOL:
ret = WIRE_TYPE_VARINT;
break;
case PB_TYPE_STRING:
ret = WIRE_TYPE_LENGTH_DELIMITED;
break;
default:
ret = -1;
break;
}
return ret;