-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathvkext-rpc-tl-serialization.cpp
More file actions
5359 lines (4977 loc) · 148 KB
/
vkext-rpc-tl-serialization.cpp
File metadata and controls
5359 lines (4977 loc) · 148 KB
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
// Compiler for PHP (aka KPHP)
// Copyright (c) 2020 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt
#include "vkext/vkext-rpc-tl-serialization.h"
#include <errno.h>
#include <cinttypes>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "common/algorithms/find.h"
#include "common/c-tree.h"
#include "common/crc32.h"
#include "common/rpc-error-codes.h"
#include "common/tl/compiler/tl-tl.h"
#include "common/tl/constants/common.h"
#include "vkext/vkext-errors.h"
#include "vkext/vkext-rpc-include.h"
#include "vkext/vkext-rpc-req-error.h"
#include "vkext/vkext-rpc.h"
#include "vkext/vkext-tl-parse.h"
#include "vkext/vkext.h"
#ifndef Z_ADDREF_P
#define Z_ADDREF_P(ptr) (ptr)->refcount++;
#define Z_ADDREF_PP(ptr) Z_ADDREF_P(*(ptr))
#endif
#define MEMCACHE_ERROR 0x7ae432f5
#define MEMCACHE_VALUE_NOT_FOUND 0x32c42422
#define MEMCACHE_VALUE_LONG 0x9729c42
#define MEMCACHE_VALUE_STRING 0xa6bebb1a
#define MEMCACHE_FALSE 0xbc799737
#define MEMCACHE_TRUE 0x997275b5
#define MEMCACHE_SET 0xeeeb54c4
#define MEMCACHE_ADD 0xa358f31c
#define MEMCACHE_REPLACE 0x2ecdfaa2
#define MEMCACHE_INCR 0x80e6c950
#define MEMCACHE_DECR 0x6467e0d9
#define MEMCACHE_DELETE 0xab505c0a
#define MEMCACHE_GET 0xd33b13ae
//#define VLOG
#ifdef VLOG
#define Z fprintf (stderr, "%d\n", __LINE__);
#else
#define Z
#endif
int total_ref_cnt;
int persistent_tree_nodes;
int dynamic_tree_nodes;
extern int verbosity;
int total_tl_working;
int total_tree_nodes_existed;
/* HASH Tables {{{ */
static unsigned string_hash(const char *s) {
unsigned h = 0;
while (*s) {
h = h * 17 + *s;
s++;
}
return h;
}
#define tl_type_id_cmp(a, b) (strcmp ((a)->id, (b)->id))
#define tl_type_id_hash(a) (string_hash ((a)->id))
DEFINE_HASH_STDNOZ_ALLOC(tl_type_id, struct tl_type *, tl_type_id_cmp, tl_type_id_hash);
/*struct hash_elem_tl_type_id *tl_type_id_arr[(1 << 12)];
struct hash_table_tl_type_id tl_type_id_hash_table = {
.size = (1 << 12),
.E = tl_type_id_arr,
.mask = (1 << 12) - 1
};*/
#define tl_type_name_cmp(a, b) ((a)->name - (b)->name)
#define tl_type_name_hash(a) ((a)->name)
DEFINE_HASH_STDNOZ_ALLOC(tl_type_name, struct tl_type *, tl_type_name_cmp, tl_type_name_hash);
/*struct hash_elem_tl_type_name *tl_type_name_arr[(1 << 12)];
struct hash_table_tl_type_name tl_type_name_hash_table = {
.size = (1 << 12),
.E = tl_type_name_arr,
.mask = (1 << 12) - 1
};*/
DEFINE_HASH_STDNOZ_ALLOC(tl_fun_id, struct tl_combinator *, tl_type_id_cmp, tl_type_id_hash);
/*struct hash_elem_tl_fun_id *tl_fun_id_arr[(1 << 12)];
struct hash_table_tl_fun_id tl_fun_id_hash_table = {
.size = (1 << 12),
.E = tl_fun_id_arr,
.mask = (1 << 12) - 1
};*/
DEFINE_HASH_STDNOZ_ALLOC(tl_fun_name, struct tl_combinator *, tl_type_name_cmp, tl_type_name_hash);
/*struct hash_elem_tl_fun_name *tl_fun_name_arr[(1 << 12)];
struct hash_table_tl_fun_name tl_fun_name_hash_table = {
.size = (1 << 12),
.E = tl_fun_name_arr,
.mask = (1 << 12) - 1
};*/
struct tl_config {
int fn, cn, tn, pos;
struct tl_type **tps;
struct tl_combinator **fns;
struct hash_table_tl_fun_name *ht_fname;
struct hash_table_tl_fun_id *ht_fid;
struct hash_table_tl_type_name *ht_tname;
struct hash_table_tl_type_id *ht_tid;
int working_queries;
};
struct tl_config *cur_config;
#define CONFIG_LIST_SIZE 10
struct tl_config *config_list[CONFIG_LIST_SIZE];
int config_list_pos;
int typed_mode = 0;
const char *tl_current_function_name;
static int is_tl_type_flat(const struct tl_type *t, int *arg_idx) {
int _arg_idx = -1;
if (!arg_idx) {
arg_idx = &_arg_idx;
}
int not_optional_args_cnt = 0;
if (t->constructors_num != 1) {
return false;
}
const struct tl_combinator *c = t->constructors[0];
for (int i = 0; i < c->args_num; i++) {
if (!(c->args[i]->flags & FLAG_OPT_VAR)) {
*arg_idx = i;
not_optional_args_cnt++;
}
}
if (not_optional_args_cnt != 1) {
return false;
}
if (strcasecmp(t->id, c->id)) {
return false;
}
if (c->args[*arg_idx]->flags & FLAG_OPT_FIELD) {
return false;
}
return true;
}
struct arg *get_first_explicit_arg(struct tl_combinator *c) {
for (int i = 0; i < c->args_num; ++i) {
struct arg *cur_arg = c->args[i];
if (!(cur_arg->flags & FLAG_OPT_VAR)) {
return cur_arg;
}
}
return NULL;
}
static void tl_tree_debug_print(struct tl_tree *tree, int shift) __attribute__((unused));
static void tl_tree_debug_print(struct tl_tree *tree, int shift) {
for (int i = 0; i < shift; ++i) {
fprintf(stderr, " ");
}
switch (TYPE(tree)) {
case NODE_TYPE_TYPE: {
struct tl_tree_type *as_type = (struct tl_tree_type *)tree;
fprintf(stderr, "tl type: %s\n", as_type->type->id);
for (int i = 0; i < as_type->children_num; ++i) {
tl_tree_debug_print(as_type->children[i], shift + 1);
}
break;
}
case NODE_TYPE_NAT_CONST: {
struct tl_tree_nat_const *as_nat_const = (struct tl_tree_nat_const *)tree;
fprintf(stderr, "natural constant: %lld\n", as_nat_const->value);
break;
}
case NODE_TYPE_VAR_TYPE: {
struct tl_tree_var_type *as_type_var = (struct tl_tree_var_type *)tree;
fprintf(stderr, "type variable: points to argument with var_num = %d\n", as_type_var->var_num);
break;
}
case NODE_TYPE_VAR_NUM: {
struct tl_tree_var_num *as_num_var = (struct tl_tree_var_num *)tree;
fprintf(stderr, "numerical variable: points to argument with var_num = %d\n", as_num_var->var_num);
break;
}
case NODE_TYPE_ARRAY: {
struct tl_tree_array *as_type_array = (struct tl_tree_array *)tree;
fprintf(stderr, "tl builtin array: item length = %d; array length:\n", as_type_array->args_num);
tl_tree_debug_print(as_type_array->multiplicity, shift + 1);
for (int i = 0; i < as_type_array->args_num; ++i) {
tl_tree_debug_print(as_type_array->args[i]->type, shift + 1);
}
break;
}
default:
assert(false);
}
}
static const char *reqResult_underscore_class_name = "VK\\TL\\_common\\Types\\rpcResponseOk";
static const char *reqResult_header_class_name = "VK\\TL\\_common\\Types\\rpcResponseHeader";
static const char *reqResult_error_class_name = "VK\\TL\\_common\\Types\\rpcResponseError";
static const char *rpcReqResultExtra_class_name = "VK\\TL\\_common\\Types\\rpcReqResultExtra";
static const char *rpcFunctionReturnResult_class_name = "VK\\TL\\RpcFunctionReturnResult";
static const char *engine_pid_class_name = "VK\\TL\\net\\Types\\net_pid";
static const char *true_type_class_name = "VK\\TL\\_common\\Types\\true";
static const char *vk_tl_prefix = "VK\\TL\\";
static const char *php_common_namespace = "_common";
#define PHP_CLASS_NAME_BUFFER_LENGTH 1000 // for names like "VK\TL\_common\Types\VectorTotal__VectorTotal__VectorTotal__int"
#define PHP_PREFIX_BUFFER_LENGTH 100 // for "VK\TL\_common\Types\" part
void check_buffer_overflow(int len) {
if (len > PHP_CLASS_NAME_BUFFER_LENGTH) {
php_error_docref(NULL, E_ERROR,
"Internal class name buffer overflow (rpc typed mode)");
}
}
#define MAX_DEPTH 10000
struct tl_type *tl_type_get_by_id(const char *s) {
assert (cur_config);
struct tl_type t;
t.id = (char *)s;
//struct hash_elem_tl_type_id *h = hash_lookup_tl_type_id (&tl_type_id_hash_table, &t);
struct hash_elem_tl_type_id *h = hash_lookup_tl_type_id(cur_config->ht_tid, &t);
return h ? h->x : 0;
}
struct tl_combinator *tl_fun_get_by_id(const char *s) {
assert (cur_config);
struct tl_combinator c;
c.id = (char *)s;
//struct hash_elem_tl_fun_id *h = hash_lookup_tl_fun_id (&tl_fun_id_hash_table, &c);
struct hash_elem_tl_fun_id *h = hash_lookup_tl_fun_id(cur_config->ht_fid, &c);
return h ? h->x : 0;
}
struct tl_combinator *tl_fun_get_by_name(int name) {
assert (cur_config);
struct tl_combinator c;
c.name = name;
//struct hash_elem_tl_fun_name *h = hash_lookup_tl_fun_name (&tl_fun_name_hash_table, &c);
struct hash_elem_tl_fun_name *h = hash_lookup_tl_fun_name(cur_config->ht_fname, &c);
return h ? h->x : 0;
}
struct tl_type *tl_type_get_by_name(int name) {
assert (cur_config);
struct tl_type t;
t.name = name;
//struct hash_elem_tl_type_name *h = hash_lookup_tl_type_name (&tl_type_name_hash_table, &t);
struct hash_elem_tl_type_name *h = hash_lookup_tl_type_name(cur_config->ht_tname, &t);
return h ? h->x : 0;
}
void tl_type_insert_name(struct tl_type *t) {
assert (cur_config);
if (t->name) {
assert (!tl_type_get_by_name(t->name));
//hash_insert_tl_type_name (&tl_type_name_hash_table, t);
hash_insert_tl_type_name(cur_config->ht_tname, t);
} else {
if (!tl_type_get_by_name(t->name)) {
//hash_insert_tl_type_name (&tl_type_name_hash_table, t);
hash_insert_tl_type_name(cur_config->ht_tname, t);
}
}
}
void tl_type_insert_id(struct tl_type *t) {
assert (cur_config);
assert (!tl_type_get_by_id(t->id));
//hash_insert_tl_type_id (&tl_type_id_hash_table, t);
hash_insert_tl_type_id(cur_config->ht_tid, t);
}
void tl_fun_insert_id(struct tl_combinator *t) {
assert (cur_config);
assert (!tl_fun_get_by_id(t->id));
//hash_insert_tl_fun_id (&tl_fun_id_hash_table, t);
hash_insert_tl_fun_id(cur_config->ht_fid, t);
}
void tl_fun_insert_name(struct tl_combinator *t) {
assert (cur_config);
assert (!tl_fun_get_by_name(t->name));
//hash_insert_tl_fun_name (&tl_fun_name_hash_table, t);
hash_insert_tl_fun_name(cur_config->ht_fname, t);
}
/* }}} */
/**
* "VK\TL\(namespace)\Function\(namespace)_name" -> namespace
*/
static void get_php_namespace_by_php_class_name(char *dst, const char *php_tl_class_name) {
const char *short_php_tl_class_name = php_tl_class_name + strlen(vk_tl_prefix);
const char *slash_ptr = strchr(short_php_tl_class_name, '\\');
assert(slash_ptr != NULL);
int res_str_len = slash_ptr - short_php_tl_class_name;
strncpy(dst, short_php_tl_class_name, res_str_len);
dst[res_str_len] = 0;
check_buffer_overflow(res_str_len + 1);
}
/**
* "(namespace).name" -> namespace or
* "name" -> "_common"
*/
static void get_php_namespace_by_tl_name(char *dst, const char *tl_name) {
const char *dot_ptr = strchr(tl_name, '.');
if (dot_ptr == NULL) {
strcpy(dst, php_common_namespace);
return;
}
int dot_pos = dot_ptr - tl_name;
strncpy(dst, tl_name, dot_pos);
dst[dot_pos] = 0;
}
enum php_prefix_t {
FUNCTIONS,
TYPES
};
static void get_php_prefix(char *dst, const char *tl_name, enum php_prefix_t php_prefix_type) {
static char php_namespace[50];
get_php_namespace_by_tl_name(php_namespace, tl_name);
strcpy(dst, vk_tl_prefix);
strcat(dst, php_namespace);
if (php_prefix_type == FUNCTIONS) {
strcat(dst, "\\Functions\\");
} else {
strcat(dst, "\\Types\\");
}
}
static int make_tl_class_name(char *dst, const char *prefix, const char *tl_name, const char *suffix, char new_delim) {
strcpy(dst, prefix);
int prefix_len = strlen(prefix);
int tl_name_len = strlen(tl_name);
for (int i = 0; i < tl_name_len; ++i) {
char cur_c = tl_name[i];
dst[i + prefix_len] = (cur_c == '.' ? new_delim : cur_c);
}
strcpy(dst + prefix_len + tl_name_len, suffix);
return prefix_len + tl_name_len + strlen(suffix);
}
/**
* The same as tl_gen::is_tl_type_a_php_array(...)
*/
int is_php_array(struct tl_type *t) {
if (t->name == TL_VECTOR || t->name == TL_TUPLE || t->name == TL_DICTIONARY ||
t->name == TL_INT_KEY_DICTIONARY || t->name == TL_LONG_KEY_DICTIONARY) {
return 1;
} else {
return 0;
}
}
int is_php_builtin(struct tl_type *t) {
if (t->name == TL_INT || t->name == TL_DOUBLE || t->name == TL_FLOAT || t->name == TL_LONG || t->name == TL_STRING ||
!strcmp(t->id, "Bool") || !strcmp(t->id, "Maybe") || !strcmp(t->id, "#")) {
return 1;
} else {
return 0;
}
}
bool is_arg_named_fields_mask_bit(struct arg *arg) {
if (!(arg->flags & FLAG_OPT_FIELD) || TYPE(arg->type) != NODE_TYPE_TYPE) {
return false;
}
struct tl_tree_type *as_type = (struct tl_tree_type *)arg->type;
return as_type->type->name == TL_TRUE && as_type->self.flags & FLAG_BARE;
}
/**
* Generates php tl class name by tl type tree recursively
*/
int get_full_tree_type_name(char *dst, struct tl_tree_type *tree, const char *constructor_name) {
char *start_dst = dst;
struct tl_type *cur_type = tree->type;
// {{
// At this part we make the main part of name, e.g. "vectorTotal", "array", "maybe"
if (constructor_name) {
// construct_name != NULL only in the first call
dst += make_tl_class_name(dst, "", constructor_name, "", '_');
} else {
// in every recursive call it's NULL
// tree is child here!
if (tree->self.flags & FLAG_FORWARDED) {
// if it's got from forwarded function (via !), parent type is parametrized by "RpcFunctionReturnResult"
// instead of whole calculated subtree
dst += make_tl_class_name(dst, "", "RpcFunctionReturnResult", "", '\0');
return dst - start_dst;
}
if (is_php_array(cur_type)) {
// if its type is php array, it's called uniformly
dst += make_tl_class_name(dst, "", "array", "", '\0');
} else if (!strcmp(cur_type->id, "Maybe")) {
// in case of (Maybe T) in php it can be:
// 1) Optional<T::PhpType> -- Maybe_T::PhpType
// 2) T::PhpType -- T::PhpType
struct tl_tree_type *child = (struct tl_tree_type *)tree->children[0];
if (is_php_array(child->type) || vk::any_of_equal(child->type->name, TL_INT, TL_DOUBLE, TL_FLOAT, TL_STRING, TL_LONG)) {
// php_field_type::t_int || php_field_type::t_double
// php_field_type::t_string || php_field_type::t_array
dst += make_tl_class_name(dst, "", "maybe", "", '\0');
// if it's wrapped to Optional, we add "maybe" to name
} else {
// php_field_type::t_class || php_field_type::t_mixed
// php_field_type::t_boolean || php_field_type::t_maybe
return get_full_tree_type_name(dst, child, NULL);
// otherwise, skip current node and just go straight to the maybe inner
}
} else if (is_php_builtin(cur_type)) {
// if cur node is builtin, add builtin type name to res
if (cur_type->name == TL_INT || cur_type->name == TL_FLOAT || cur_type->name == TL_STRING) {
dst += make_tl_class_name(dst, "", cur_type->constructors[0]->id, "", '\0');
} else if (cur_type->name == TL_DOUBLE) {
dst += make_tl_class_name(dst, "", "float", "", '\0');
} else if (cur_type->name == TL_LONG || !strcmp(cur_type->id, "#")) {
dst += make_tl_class_name(dst, "", "int", "", '\0');
} else if (!strcmp(cur_type->id, "Bool")) {
dst += make_tl_class_name(dst, "", "boolean", "", '\0');
} else {
php_error_docref(NULL, E_ERROR,
"Unexpected type %s during creating instance", cur_type->id);
}
return dst - start_dst;
} else if (is_tl_type_flat(cur_type, NULL)) {
// the order is important, because types which are arrays in php can be flat (Tuple, IntKeyDictionary, ...)
struct tl_tree *flat_inner = get_first_explicit_arg(cur_type->constructors[0])->type;
if (TYPE(flat_inner) == NODE_TYPE_TYPE) {
return get_full_tree_type_name(dst, (struct tl_tree_type *)flat_inner, NULL);
}
assert(TYPE(flat_inner) == NODE_TYPE_ARRAY); // Не поддерживаем шаблонные флат типы! (не может быть type_var)
struct tl_tree_array *as_arr = (struct tl_tree_array *)flat_inner;
assert(as_arr->args_num == 1); // запрещаем флатиться массивам, у которых ячейка длины не 1
struct tl_tree *arr_flat_inner = as_arr->args[0]->type;
assert(TYPE(arr_flat_inner) == NODE_TYPE_TYPE); // Не поддерживаем шаблонные флат типы! (не может быть type_var)
const char *str_array = "array_";
dst += make_tl_class_name(dst, "", str_array, "", '\0');
return strlen(str_array) + get_full_tree_type_name(dst, (struct tl_tree_type *)arr_flat_inner, NULL);
} else {
// if it's ordinary tl type, add its name to res
dst += make_tl_class_name(dst, "", cur_type->id, "", '_');
}
}
// }}
// At this part we make the rest part of name, by iterating through all children, e.g.
// "__child1__child2__array_childOfArray__child3__maybe_int"
for (int i = 0; i < tree->children_num; ++i) {
struct tl_tree *child = tree->children[i];
switch (TYPE(child)) {
case NODE_TYPE_TYPE: {
if (is_php_array(cur_type) || !strcmp(cur_type->id, "Maybe")) {
strcpy(dst, "_");
++dst;
} else {
strcpy(dst, "__");
dst += 2;
}
dst += get_full_tree_type_name(dst, (struct tl_tree_type *)child, NULL);
break;
}
case NODE_TYPE_ARRAY:
case NODE_TYPE_VAR_TYPE: {
php_error_docref(NULL, E_ERROR,
"Unexpected node type %d during creating instance", TYPE(child));
break;
}
}
}
return dst - start_dst;
}
/**
* Generates class name for creating instance of this class.
*/
static void get_php_class_name(char *dst, struct tl_tree_type *tree, int num) {
assert(tree);
const char *constructor_name = tree->type->constructors[num]->id;
if (!strcmp(tree->type->id, "ReqResult")) {
if (!strcmp(constructor_name, "_")) {
make_tl_class_name(dst, "", reqResult_underscore_class_name, "", '\0');
} else if (!strcmp(constructor_name, "reqResultHeader")) {
make_tl_class_name(dst, "", reqResult_header_class_name, "", '\0');
} else if (!strcmp(constructor_name, "reqError")) {
make_tl_class_name(dst, "", reqResult_error_class_name, "", '\0');
} else {
php_error_docref(NULL, E_WARNING,
"Unknown constructor of ReqResult: %s", constructor_name);
}
} else {
static char php_prefix[PHP_PREFIX_BUFFER_LENGTH];
get_php_prefix(php_prefix, constructor_name, TYPES);
strcpy(dst, php_prefix);
int prefix_len = strlen(php_prefix);
dst += prefix_len;
// tree->type isn't flat => constructor is correct
int len = get_full_tree_type_name(dst, tree, constructor_name);
check_buffer_overflow(prefix_len + len + 1);
}
}
static zval *create_php_instance(const char *class_name) {
//fprintf(stderr, "creating instance of class %s\n", class_name);
zval *ci;
VK_ALLOC_INIT_ZVAL (ci);
zend_class_entry *ce = vk_get_class(class_name);
object_init_ex(ci, ce);
return ci;
}
static zval *make_query_result_or_error(zval **r, const vkext_rpc::tl::RpcReqError &error, const vkext_rpc::tl::RpcReqResultExtra *header = nullptr, int extra_flags = 0);
/**
* This function extracts ORIGINAL tl name from given php class name.
* It processes cases with changed names of php tl classes, such as: "rpcResponseOk" (original "_") and etc.
*/
void get_current_combinator_name(char *dst, const char *php_class_name) {
if (!strcmp(php_class_name, reqResult_underscore_class_name)) {
strcpy(dst, "_");
return;
} else if (!strcmp(php_class_name, reqResult_header_class_name)) {
strcpy(dst, "reqResultHeader");
return;
} else if (!strcmp(php_class_name, reqResult_error_class_name)) {
strcpy(dst, "reqError");
return;
}
get_php_namespace_by_php_class_name(dst, php_class_name);
int slashes_cnt = 0;
// php_class_name is assumed to be either
// VK\TL\(namespace)\(Types|Functions)\(namespace)_name or
// VK\TL\_common\(Types|Functions)\name
// therefore, we need to skip 4 slashes
while (slashes_cnt < 4) {
if (*php_class_name++ == '\\') {
++slashes_cnt;
}
}
if (!strcmp(dst, php_common_namespace)) {
strcpy(dst, php_class_name);
} else {
// we need to change '_' to '.'
int namespace_len = strlen(dst);
strcpy(dst, php_class_name);
dst[namespace_len] = '.';
}
check_buffer_overflow(strlen(dst) + 1);
// here dst looks like "graph.someComb__int" or "vectorTotal__int"
// and we just need to drop part "__int"
char *template_suffix_start = strstr(dst, "__");
if (template_suffix_start != NULL) {
int template_suffix_pos = template_suffix_start - dst;
dst[template_suffix_pos] = 0;
}
}
/* {{{ PHP arrays interaction */
VK_ZVAL_API_P get_field(zval *arr, const char *id, int num, zval **dst) {
//fprintf(stderr, "@@@@@@@ get_field %s %d\n", id, num);
ADD_CNT (get_field);
START_TIMER (get_field);
assert (arr);
// fprintf (stderr, "arr = %p, type = %d\n", arr, (int)Z_TYPE_PP (arr));
if (Z_TYPE_P (arr) != IS_ARRAY && Z_TYPE_P (arr) != IS_OBJECT) {
// fprintf (stderr, "=(\n");
END_TIMER (get_field);
return 0;
}
VK_ZVAL_API_P t = NULL;
if (id && strlen(id)) {
switch (Z_TYPE_P (arr)) {
case IS_ARRAY:
t = vk_zend_hash_find(Z_ARRVAL_P (arr), id, VK_STR_API_LEN(strlen(id)));
break;
case IS_OBJECT:
if (!strcmp(id, "_")) {
char php_class_name[PHP_CLASS_NAME_BUFFER_LENGTH];
vk_get_class_name(arr, php_class_name);
char current_combinator_name[PHP_CLASS_NAME_BUFFER_LENGTH];
get_current_combinator_name(current_combinator_name, php_class_name);
//fprintf(stderr, "got _ = %s\n", current_combinator_name);
VK_ALLOC_INIT_ZVAL(*dst);
VK_ZVAL_STRING_DUP(*dst, current_combinator_name);
//fprintf(stderr, "######### %s\n", current_combinator_name);
} else {
//fprintf(stderr, "reading instance property %s\n", id);
if (strlen(id)) {
*dst = vk_zend_read_public_property(arr, id);
} else {
const size_t arg_name_size = 30;
char arg_name[arg_name_size];
snprintf(arg_name, arg_name_size, "arg%d", num);
*dst = vk_zend_read_public_property(arr, arg_name);
}
}
t = VK_ZVALP_TO_API_ZVALP(*dst);
break;
default:
t = 0;
}
}
if (Z_TYPE_P(arr) == IS_OBJECT) {
END_TIMER (get_field);
return t;
}
if (t) {
END_TIMER (get_field);
return t;
}
t = vk_zend_hash_index_find(Z_ARRVAL_P (arr), num);
if (t) {
END_TIMER (get_field);
return t;
}
END_TIMER (get_field);
return 0;
}
void array_set_field(zval **arr, zval *val, const char *id, long long num) {
ADD_CNT (array_set_field);
START_TIMER (array_set_field);
if (!*arr) {
php_error_docref(NULL, E_WARNING, "In fetching of the function: %s\nAttempt to array_set_field() of NULL tl object", tl_current_function_name);
VK_ALLOC_INIT_ZVAL (*arr);
array_init (*arr);
}
assert (val);
assert (*arr && Z_TYPE_P(*arr) == IS_ARRAY);
#ifdef VLOG
fprintf (stderr, "set_field: num:%d val_type:%d arr:%p\n", num, Z_TYPE_P (val), *arr);
#endif
if (id && strlen(id)) {
vk_add_assoc_zval_nod (*arr, (char *)id, val);
} else {
vk_add_index_zval_nod (*arr, num, val);
}
END_TIMER (array_set_field);
}
void set_field(zval **arr, zval *val, const char *id, long long num) {
//fprintf(stderr, "~~~ set_field %s %lld\n", id, num);
ADD_CNT (set_field);
START_TIMER (set_field);
if (!*arr) {
php_error_docref(NULL, E_WARNING, "In fetching of the function: %s\nAttempt to set_field() of NULL tl object", tl_current_function_name);
VK_ALLOC_INIT_ZVAL (*arr);
array_init (*arr);
}
switch (typed_mode) {
case 0: {
assert (val);
assert (*arr && Z_TYPE_P(*arr) == IS_ARRAY);
#ifdef VLOG
fprintf (stderr, "set_field: num:%d val_type:%d arr:%p\n", num, Z_TYPE_P (val), *arr);
#endif
if (id && strlen(id)) {
vk_add_assoc_zval_nod (*arr, (char *)id, val);
} else {
vk_add_index_zval_nod (*arr, num, val);
}
break;
}
case 1: {
assert (val);
assert (*arr && Z_TYPE_P(*arr) == IS_OBJECT);
#ifdef VLOG
fprintf (stderr, "set_field: num:%d val_type:%d arr:%p\n", num, Z_TYPE_P (val), *arr);
#endif
if (id && strlen(id)) {
vk_zend_update_public_property_nod(*arr, id, val);
} else {
const size_t arg_name_size = 10;
char arg_name[arg_name_size];
snprintf(arg_name, arg_name_size, "arg%lld", num);
vk_zend_update_public_property_nod(*arr, arg_name, val);
}
break;
}
}
END_TIMER (set_field);
}
void set_field_string(zval **arr, char *val, const char *id, int num) {
ADD_CNT (set_field_string);
START_TIMER (set_field_string);
if (!*arr) {
php_error_docref(NULL, E_WARNING, "In fetching of the function: %s\nAttempt to set_field_string() of NULL tl object", tl_current_function_name);
VK_ALLOC_INIT_ZVAL (*arr);
array_init (*arr);
}
switch (typed_mode) {
case 0:
assert (val);
assert (*arr && Z_TYPE_P(*arr) == IS_ARRAY);
if (id && strlen(id)) {
vk_add_assoc_string_dup (*arr, (char *)id, val);
} else {
vk_add_index_string_dup (*arr, num, val);
}
break;
case 1:
assert (val);
assert (*arr && Z_TYPE_P(*arr) == IS_OBJECT);
if (id && strlen(id)) {
vk_zend_update_public_property_string(*arr, id, val);
} else {
const size_t arg_name_size = 10;
char arg_name[arg_name_size];
snprintf(arg_name, arg_name_size, "arg%d", num);
vk_zend_update_public_property_string(*arr, arg_name, val);
}
break;
}
END_TIMER (set_field_string);
}
void set_field_int(zval **arr, int val, const char *id, int num) {
ADD_CNT (set_field_int);
START_TIMER (set_field_int);
if (!*arr) {
php_error_docref(NULL, E_WARNING, "In fetching of the function: %s\nAttempt to set_field_int() of NULL tl object", tl_current_function_name);
VK_ALLOC_INIT_ZVAL (*arr);
array_init (*arr);
}
switch (typed_mode) {
case 0:
if (id && strlen(id)) {
add_assoc_long (*arr, (char *)id, val);
} else {
add_index_long(*arr, num, val);
}
break;
case 1:
if (id && strlen(id)) {
vk_zend_update_public_property_long(*arr, id, val);
} else {
const size_t arg_name_size = 10;
char arg_name[arg_name_size];
snprintf(arg_name, arg_name_size, "arg%d", num);
vk_zend_update_public_property_long(*arr, arg_name, val);
}
break;
}
END_TIMER (set_field_int);
}
int get_array_size(zval **arr) {
return zend_hash_num_elements (Z_ARRVAL_P(*arr));
}
/* }}} */
static bool wrap_to_function_result_if_needed(zval **tl_obj_fun_res, const char *tl_fun_name, bool dup) {
// if dup is false, *tl_obj_fun_res is a kind of rvalue and its duplication will lead to memory leak
assert(*tl_obj_fun_res);
// if it's already a function result no wrapping is needed (multi exclamation optimization)
if (Z_TYPE_P(*tl_obj_fun_res) == IS_OBJECT) {
zend_class_entry *class_entry = Z_OBJCE_P(*tl_obj_fun_res);
if (class_entry->num_interfaces == 1 && !strcmp(VK_ZSTR_VAL(class_entry->interfaces[0]->name), rpcFunctionReturnResult_class_name)) {
return false;
}
}
static char class_name[PHP_CLASS_NAME_BUFFER_LENGTH];
static char php_prefix[PHP_PREFIX_BUFFER_LENGTH];
get_php_prefix(php_prefix, tl_fun_name, FUNCTIONS);
check_buffer_overflow(make_tl_class_name(class_name, php_prefix, tl_fun_name, "_result", '_'));
zval *f_result_wrapper = create_php_instance(class_name);
if (dup) {
vk_zend_update_public_property_dup(f_result_wrapper, "value", *tl_obj_fun_res);
} else {
vk_zend_update_public_property_nod(f_result_wrapper, "value", *tl_obj_fun_res);
}
*tl_obj_fun_res = f_result_wrapper;
return true;
}
#define use_var_nat_full_form(x) 1
long long var_nat_const_to_int(void *x) {
if (((long)x) & 1) {
return (((long)x) + 0x80000001l) / 2;
} else {
return ((struct tl_tree_nat_const *)x)->value;
}
}
void *int_to_var_nat_const(long long x) {
if (use_var_nat_full_form (x)) {
auto *T = reinterpret_cast<tl_tree_nat_const *>(zzemalloc(sizeof(tl_tree_nat_const)));
T->self.ref_cnt = 1;
T->self.flags = 0;
T->self.methods = &tl_nat_const_full_methods;
#ifdef VLOG
fprintf(stderr, "Creating nat const full %lld at %p\n", x, T);
#endif
T->value = x;
total_ref_cnt++;
dynamic_tree_nodes++;
total_tree_nodes_existed++;
return T;
} else {
return (void *)(long)(x * 2 - 0x80000001l);
}
}
void *int_to_var_nat_const_init(long long x) {
if (use_var_nat_full_form (x)) {
tl_tree_nat_const *T = reinterpret_cast<tl_tree_nat_const *>(zzmalloc(sizeof(*T)));
ADD_PMALLOC (sizeof(*T));
T->self.ref_cnt = 1;
T->self.flags = 0;
T->self.methods = &tl_pnat_const_full_methods;
T->value = x;
total_ref_cnt++;
persistent_tree_nodes++;
total_tree_nodes_existed++;
return T;
} else {
return (void *)(long)(x * 2 - 0x80000001l);
}
}
int get_constructor(struct tl_type *t, const char *id) {
int i;
for (i = 0; i < t->constructors_num; i++) {
if (!strcmp(t->constructors[i]->id, id)) {
return i;
}
}
return -1;
}
int get_constructor_by_name(struct tl_type *t, int name) {
for (int i = 0; i < t->constructors_num; i++) {
if (t->constructors[i]->name == static_cast<unsigned int>(name)) {
return i;
}
}
return -1;
}
#define MAX_VARS 100000
struct tl_tree *__vars[MAX_VARS];
struct tl_tree **last_var_ptr;
struct tl_tree **get_var_space(struct tl_tree **vars, int n) {
// fprintf (stderr, "get_var_space: %d\n", n);
if (vars - n < __vars) {
vkext_reset_error();
vkext_error(VKEXT_ERROR_NOT_ENOUGH_MEMORY, "Not enough memory");
return 0;
}
if (last_var_ptr > vars - n) {
last_var_ptr = vars - n;
}
int i;
for (i = -n; i < 0; i++) {
if (vars[i])
DEC_REF(vars[i]);
}
memset(vars - n, 0, sizeof(*vars) * n);
return vars - n;
}
void tl_parse_on_rinit() {
last_var_ptr = __vars + MAX_VARS;
}
void tl_parse_on_rshutdown() {
while (last_var_ptr < __vars + MAX_VARS) {
if (*last_var_ptr) {
DEC_REF (*last_var_ptr);
*last_var_ptr = 0;
}
last_var_ptr++;
}
}
#define MAX_SIZE 100000
void *_Data[MAX_SIZE];
typedef void *(*fpr_t)(void **IP, void **Data, zval **arr, struct tl_tree **vars);
#define TLUNI_NEXT return (*(fpr_t *) IP) (IP + 1, Data, arr, vars);
#define TLUNI_START(IP, Data, arr, vars) (*(fpr_t *) IP) (IP + 1, Data, arr, vars)
#define TLUNI_OK (void *)1l
#define TLUNI_FAIL (void *)0
void **fIP;
inline void tl_debug(const char *s __attribute__((unused)), int n __attribute__((unused))) {
//fprintf(stderr, "%s\n", s);
}
/* {{{ Interface functions */
struct tl_tree *store_function(VK_ZVAL_API_P arr) {
ADD_CNT(store_function)
START_TIMER(store_function)
assert (arr);
zval *dst;
// IN PHP5 r == &dst => lifetime is limited by scope of this function
VK_ZVAL_API_P r = get_field(VK_ZVAL_API_TO_ZVALP(arr), "_", 0, &dst);
if (!r) {
vkext_reset_error();
vkext_error(VKEXT_ERROR_NO_CONSTRUCTOR, "Can't store function with no name");
END_TIMER(store_function)
return 0;
}
struct tl_combinator *c;
char *s = 0;
int l = 0;
if (VK_Z_API_TYPE (r) == IS_STRING) {
int l;
s = parse_zend_string(r, &l);
c = tl_fun_get_by_id(s);
} else {
l = parse_zend_long(r);
c = tl_fun_get_by_name(l);
}
if (!c) {
#ifdef VLOG
if (Z_TYPE_PP (r) == IS_STRING) {
fprintf (stderr, "Function %s not found\n", s);
} else {
fprintf (stderr, "Function %d not found\n", l);
}
#endif
vkext_reset_error();
if (VK_Z_API_TYPE (r) == IS_STRING) {
vkext_error_format(VKEXT_ERROR_UNKNOWN_CONSTRUCTOR, "Function %s not found", s);
} else {
vkext_error_format(VKEXT_ERROR_UNKNOWN_CONSTRUCTOR, "Function %d not found", l);
}
if (VK_Z_API_TYPE (arr) == IS_OBJECT) {
zval_ptr_dtor(r);
efree(dst);
}
END_TIMER(store_function)
return 0;
}
if (!allow_internal_rpc_queries && (c->flags & COMBINATOR_FLAG_INTERNAL)) {
php_error_docref(NULL, E_WARNING, "### DEPRECATED TL FEATURE ###:\nStoring of internal tl function %s\n", c->id);
if (VK_Z_API_TYPE (arr) == IS_OBJECT) {
zval_ptr_dtor(r);
efree(dst);
}
END_TIMER(store_function)
return 0;
}
#ifdef VLOG
fprintf (stderr, "Storing functions %s\n", c->id);
#endif
tl_current_function_name = c->id;
struct tl_tree **vars = get_var_space(__vars + MAX_VARS, c->var_num);
if (!vars) {
vkext_reset_error();
if (VK_Z_API_TYPE (r) == IS_STRING) {
vkext_error_format(VKEXT_ERROR_NOT_ENOUGH_MEMORY, "Not enough memory to store %s", s);
} else {
vkext_error_format(VKEXT_ERROR_NOT_ENOUGH_MEMORY, "Not enough memory to store %d", l);
}
if (VK_Z_API_TYPE (arr) == IS_OBJECT) {
zval_ptr_dtor(r);
efree(dst);
}
END_TIMER(store_function)
return 0;
}
static zval *_arr[MAX_DEPTH];
_arr[0] = VK_ZVAL_API_TO_ZVALP(arr);