forked from neoxic/php-amf3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamf3.c
1170 lines (1068 loc) · 31.4 KB
/
amf3.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
/*
* ========================================================================
* PHP AMF3 encoding/decoding extension
* Work started by Arseny Vakhrushev on 11 Jan 2010
* Copyright (C) 2010 IT Territory, LLC. http://it-territory.ru/
* ========================================================================
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Please read the LICENSE file for license details
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <php.h>
#include <Zend/zend_interfaces.h>
#include <ext/standard/info.h>
#include <ext/date/php_date.h>
#include "php_amf3.h"
#if PHP_VERSION_ID >= 70200
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_amf3_encode, 0, 1, IS_STRING, 1)
#else
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_amf3_encode, 0, 1, IS_STRING, 0, 1)
#endif
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_amf3_decode, 0, ZEND_RETURN_VALUE, 1)
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0)
ZEND_ARG_INFO(1, count)
ZEND_END_ARG_INFO()
static const zend_function_entry amf3_functions[] = {
ZEND_FE(amf3_encode, arginfo_amf3_encode)
ZEND_FE(amf3_decode, arginfo_amf3_decode)
ZEND_FE_END
};
static zend_class_entry *(*sxe_get_element_class_entry)();
static PHP_MINFO_FUNCTION(amf3)
{
php_info_print_table_start();
php_info_print_table_header(2, "AMF3 support", "enabled");
php_info_print_table_row(2, "Version", PHP_AMF3_VERSION);
php_info_print_table_row(2, "Build Date", __DATE__ " " __TIME__);
php_info_print_table_row(2, "XML support", sxe_get_element_class_entry ? "enabled" : "disabled");
php_info_print_table_end();
}
static PHP_MINIT_FUNCTION(amf3)
{
sxe_get_element_class_entry = (zend_class_entry * (*)())DL_FETCH_SYMBOL(NULL, "sxe_get_element_class_entry");
return SUCCESS;
}
static const zend_module_dep amf3_deps[] = {
ZEND_MOD_REQUIRED("date")
ZEND_MOD_OPTIONAL("SimpleXML")
ZEND_MOD_END
};
zend_module_entry amf3_module_entry = {
STANDARD_MODULE_HEADER_EX,
NULL,
amf3_deps,
"amf3",
amf3_functions,
PHP_MINIT(amf3),
NULL,
NULL,
NULL,
PHP_MINFO(amf3),
PHP_AMF3_VERSION,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_AMF3
ZEND_GET_MODULE(amf3)
#endif
/* ============================================================================================================ */
#define AMF3_MAX_INT 268435455 // (2^28)-1
#define AMF3_MIN_INT -268435456 // -(2^28)
typedef enum amf3_type_e amf3_type_t;
typedef struct amf3_chunk_s amf3_chunk_t;
typedef struct amf3_env_s amf3_env_t;
typedef struct amf3_traits_s amf3_traits_t;
enum amf3_type_e {
AMF3_UNDEFINED = 0x00,
AMF3_NULL = 0x01,
AMF3_FALSE = 0x02,
AMF3_TRUE = 0x03,
AMF3_INTEGER = 0x04,
AMF3_DOUBLE = 0x05,
AMF3_STRING = 0x06,
AMF3_XMLDOC = 0x07, // only decoding
AMF3_DATE = 0x08,
AMF3_ARRAY = 0x09,
AMF3_OBJECT = 0x0a, // without externalizable
AMF3_XML = 0x0b,
AMF3_BYTEARRAY = 0x0c, // only decoding
AMF3_VECTOR_INT = 0x0d, // no support
AMF3_VECTOR_UINT = 0x0e, // no support
AMF3_VECTOR_DOUBLE = 0x0f, // no support
AMF3_VECTOR_OBJECT = 0x10, // no support
AMF3_DICTIONARY = 0x11, // no support
};
struct amf3_chunk_s {
char buf[1000]; // some suitable chunk size to store both small and big buffers well enough and to fit 1Kb memory block
int size;
amf3_chunk_t *next;
};
struct amf3_env_s {
HashTable strs;
HashTable objs;
HashTable traits;
};
struct amf3_traits_s {
zend_class_entry *ce;
int memberCount;
char **members;
int *memberLengths;
int dynamic;
};
static int amf3_encodeVal(amf3_chunk_t **chunk, zval *val, amf3_env_t *env);
static int amf3_decodeVal(zval *val, const char *data, int pos, int size, amf3_env_t *env);
/* ============================================================================================================ */
static amf3_chunk_t *amf3_initChunk() {
amf3_chunk_t *chunk;
chunk = (amf3_chunk_t *)ecalloc(1, sizeof(*chunk));
if (!chunk) {
return NULL;
}
return chunk;
}
static amf3_chunk_t *amf3_appendChunk(amf3_chunk_t *chunk, const char *buf, int size) {
if (!chunk) {
return NULL;
}
for ( ;; ) {
int avail = sizeof(chunk->buf) - chunk->size;
if (avail >= size) {
break;
}
memcpy(chunk->buf + chunk->size, buf, avail);
chunk->size += avail;
buf += avail;
size -= avail;
if (!chunk->next) {
chunk->next = amf3_initChunk();
}
chunk = chunk->next;
}
memcpy(chunk->buf + chunk->size, buf, size);
chunk->size += size;
return chunk;
}
static void amf3_freeChunk(amf3_chunk_t *chunk, char *buf) {
amf3_chunk_t *next;
while (chunk) {
next = chunk->next;
memcpy(buf, chunk->buf, chunk->size);
buf += chunk->size;
efree(chunk);
chunk = next;
}
}
static int amf3_getStrIdx(amf3_env_t *env, const char *str, int len) {
if (len <= 0) {
return -1; // empty string is never sent by reference
}
if (len > AMF3_MAX_INT) {
len = AMF3_MAX_INT;
}
zval *oldIdx;
if ((oldIdx = zend_hash_str_find(&env->strs, str, len)) != NULL) {
return Z_LVAL_P(oldIdx);
}
int newIdx = zend_hash_num_elements(&env->strs);
if (newIdx <= AMF3_MAX_INT) {
zval tmp;
ZVAL_LONG(&tmp, newIdx);
zend_hash_str_add(&env->strs, str, len, &tmp);
}
return -1;
}
static int amf3_getObjIdx(amf3_env_t *env, zval *val) {
void *key = NULL;
if (Z_TYPE_P(val) == IS_ARRAY) {
key = Z_ARR_P(val);
} else if (Z_TYPE_P(val) == IS_OBJECT) {
key = Z_OBJ_P(val);
}
zval *oldIdx;
if ((oldIdx = zend_hash_str_find(&env->objs, (char *)&key, sizeof(key))) != NULL) {
return Z_LVAL_P(oldIdx);
}
int newIdx = zend_hash_num_elements(&env->objs);
if (newIdx <= AMF3_MAX_INT) {
zval tmp;
ZVAL_LONG(&tmp, newIdx);
zend_hash_str_add(&env->objs, (char *)&key, sizeof(key), &tmp);
}
return -1;
}
static int amf3_getTraitsIdx(amf3_env_t *env, zend_class_entry *ce) {
zval *oldIdx;
if ((oldIdx = zend_hash_str_find(&env->traits, (char *)&ce, sizeof(ce))) != NULL) {
return Z_LVAL_P(oldIdx);
}
int newIdx = zend_hash_num_elements(&env->traits);
if (newIdx <= AMF3_MAX_INT) {
zval tmp;
ZVAL_LONG(&tmp, newIdx);
zend_hash_str_add(&env->traits, (char *)&ce, sizeof(ce), &tmp);
}
return -1;
}
static zval *amf3_getRef(HashTable *ht, int idx) {
zval *val;
if ((val = zend_hash_index_find(ht, idx)) != NULL) {
Z_TRY_ADDREF_P(val);
return val;
}
return NULL;
}
static void amf3_putRef(HashTable *ht, zval *val) {
Z_TRY_ADDREF_P(val);
zend_hash_index_update(ht, zend_hash_num_elements(ht), val);
}
/* ============================================================================================================ */
static int amf3_encodeChar(amf3_chunk_t **chunk, char c) {
*chunk = amf3_appendChunk(*chunk, &c, 1);
return 1;
}
static int amf3_encodeU29(amf3_chunk_t **chunk, int val) {
char buf[4];
int pos;
val &= 0x1fffffff;
if (val <= 0x7f) {
buf[0] = val;
pos = 1;
} else if (val <= 0x3fff) {
buf[1] = val & 0x7f;
val >>= 7;
buf[0] = val | 0x80;
pos = 2;
} else if (val <= 0x1fffff) {
buf[2] = val & 0x7f;
val >>= 7;
buf[1] = val | 0x80;
val >>= 7;
buf[0] = val | 0x80;
pos = 3;
} else {
buf[3] = val;
val >>= 8;
buf[2] = val | 0x80;
val >>= 7;
buf[1] = val | 0x80;
val >>= 7;
buf[0] = val | 0x80;
pos = 4;
}
*chunk = amf3_appendChunk(*chunk, buf, pos);
return pos;
}
static int amf3_decodeU29(int *val, const char *buf, int size) {
int pos = 0, res = 0, tmp;
do {
if (pos >= size) {
return -1;
}
tmp = buf[pos];
if (pos == 3) {
res <<= 8;
res |= tmp & 0xff;
} else {
res <<= 7;
res |= tmp & 0x7f;
}
} while ((++pos < 4) && (tmp & 0x80));
*val = res;
return pos;
}
static int amf3_encodeDouble(amf3_chunk_t **chunk, double val) {
union {
double d;
uint64_t l;
} u = { val };
uint64_t l = u.l;
char buf[8];
int i;
for (i = 0; i < 8; ++i) {
buf[7 - i] = l;
l >>= 8;
}
*chunk = amf3_appendChunk(*chunk, buf, 8);
return 8;
}
static int amf3_decodeDouble(double *val, const char *buf, int size) {
if (size < 8) {
return -1;
}
uint64_t l = 0;
int i;
for (i = 0; i < 8; ++i) {
l <<= 8;
l |= buf[i] & 0xff;
}
union {
uint64_t l;
double d;
} u = { l };
*val = u.d;
return 8;
}
static int amf3_encodeStr(amf3_chunk_t **chunk, const char *str, int len, amf3_env_t *env) {
int pos = 0, idx = amf3_getStrIdx(env, str, len);
if (idx >= 0) {
pos += amf3_encodeU29(chunk, idx << 1); // encode as a reference
} else {
if (len > AMF3_MAX_INT) {
len = AMF3_MAX_INT;
}
pos += amf3_encodeU29(chunk, (len << 1) | 1) + len;
*chunk = amf3_appendChunk(*chunk, str, len);
}
return pos;
}
static int amf3_decodeStr(const char **str, unsigned int *len, const char *buf, int size, amf3_env_t *env) {
int pfx, pos = amf3_decodeU29(&pfx, buf, size);
if (pos < 0) {
return -1;
}
if (!(pfx & 1)) { // decode as a reference
zval *val;
if ((val = zend_hash_index_find(&env->strs, pfx >> 1)) == NULL) {
return -1;
}
*str = Z_STRVAL_P(val);
*len = Z_STRLEN_P(val);
} else {
pfx >>= 1;
if ((pfx < 0) || ((pos + pfx) > size)) {
return -1;
}
*str = buf + pos;
*len = pfx;
if (pfx > 0) { // empty string is never sent by reference
zval val;
ZVAL_STRINGL(&val, buf + pos, pfx);
zend_hash_index_update(&env->strs, zend_hash_num_elements(&env->strs), &val);
}
pos += pfx;
}
return pos;
}
static int amf3_encodeDate(amf3_chunk_t **chunk, zval *val, amf3_env_t *env) {
int pos = amf3_encodeChar(chunk, AMF3_DATE);
int idx = amf3_getObjIdx(env, val);
if (idx >= 0) {
pos += amf3_encodeU29(chunk, idx << 1); // encode as a reference
} else {
zval return_value;
double date;
zend_call_method_with_0_params(val, NULL, NULL, "getTimestamp", &return_value);
pos += amf3_encodeU29(chunk, 1);
date = Z_LVAL(return_value);
pos += amf3_encodeDouble(chunk, date * 1000.0);
}
return pos;
}
static int amf3_encodeArray(amf3_chunk_t **chunk, zval *val, amf3_env_t *env) {
int pos = amf3_encodeChar(chunk, AMF3_ARRAY);
int idx = amf3_getObjIdx(env, val);
if (idx >= 0) {
pos += amf3_encodeU29(chunk, idx << 1); // encode as a reference
} else {
HashTable *ht = Z_ARRVAL_P(val);
zval *hv;
char keyBuf[22];
zend_string *key;
ulong num = 0;
uint keyLen;
ulong idx;
ZEND_HASH_FOREACH_KEY(ht, idx, key) {
if (key || idx != num) {
break;
}
++num;
} ZEND_HASH_FOREACH_END();
if (num == zend_hash_num_elements(ht)) { // sequence of values with integer indexes starting from zero
if (num > AMF3_MAX_INT) {
num = AMF3_MAX_INT;
}
pos += amf3_encodeU29(chunk, (num << 1) | 1); // dense part size
pos += amf3_encodeChar(chunk, 0x01); // end of associative part
ZEND_HASH_FOREACH_VAL(ht, hv) {
pos += amf3_encodeVal(chunk, hv, env);
} ZEND_HASH_FOREACH_END();
} else { // associative array with mixed keys
pos += amf3_encodeChar(chunk, 0x01); // empty dense part
ZEND_HASH_FOREACH_KEY_VAL(ht, idx, key, hv) {
if (key) {
if (key->len == 0) {
continue; // empty keys can't be represented in AMF3
}
pos += amf3_encodeStr(chunk, key->val, key->len, env);
} else {
keyLen = sprintf(keyBuf, "%ld", idx);
pos += amf3_encodeStr(chunk, keyBuf, keyLen, env);
}
pos += amf3_encodeVal(chunk, hv, env);
} ZEND_HASH_FOREACH_END();
pos += amf3_encodeChar(chunk, 0x01); // end of associative part
}
}
return pos;
}
static int amf3_encodeXml(amf3_chunk_t **chunk, zval *val, amf3_env_t *env) {
int pos = amf3_encodeChar(chunk, AMF3_XML);
int idx = amf3_getObjIdx(env, val);
if (idx >= 0) {
pos += amf3_encodeU29(chunk, idx << 1); // encode as a reference
} else {
zval xml;
int xmlLen;
zend_call_method_with_0_params(val, NULL, NULL, "asXML", &xml);
xmlLen = Z_STRLEN(xml);
if (xmlLen > AMF3_MAX_INT) {
xmlLen = AMF3_MAX_INT;
}
pos += amf3_encodeU29(chunk, (xmlLen << 1) | 1);
*chunk = amf3_appendChunk(*chunk, Z_STRVAL(xml), xmlLen);
pos += xmlLen;
zend_string_release(Z_STR(xml));
}
return pos;
}
static int amf3_encodeObjectTraits(amf3_chunk_t **chunk, zval *val, zend_class_entry *ce, amf3_env_t *env) {
int pos = 0;
int idx = amf3_getTraitsIdx(env, ce);
if (idx >= 0) {
pos += amf3_encodeU29(chunk, (idx << 2) | 1); // encode as a reference
} else {
if (ce == zend_standard_class_def) {
pos += amf3_encodeU29(chunk, 0x0b);
pos += amf3_encodeChar(chunk, 0x01); // empty class name
} else {
HashTable *ht = Z_OBJPROP_P(val);
zend_string *key;
int members = 0;
// count number of properties
ZEND_HASH_FOREACH_STR_KEY(ht, key) {
if (key) {
if (key->len == 0 || key->val[0] == 0) {
continue; // skip empty key and private/protected properties
}
if (!zend_hash_exists(&ce->properties_info, key)) {
continue; // skip dynamic properties
}
members++;
}
} ZEND_HASH_FOREACH_END();
pos += amf3_encodeU29(chunk, (members << 4) | 0x0b);
pos += amf3_encodeStr(chunk, ce->name->val, ce->name->len, env);
// write property names
ZEND_HASH_FOREACH_STR_KEY(ht, key) {
if (key) {
if (key->len == 0 || key->val[0] == 0) {
continue; // skip empty key and private/protected properties
}
if (!zend_hash_exists(&ce->properties_info, key)) {
continue; // skip dynamic properties
}
pos += amf3_encodeStr(chunk, key->val, key->len, env);
}
} ZEND_HASH_FOREACH_END();
}
}
return pos;
}
static int amf3_encodeObject(amf3_chunk_t **chunk, zval *val, amf3_env_t *env) {
int pos = 0, idx;
zend_class_entry *ce = Z_OBJCE_P(val);
if (instanceof_function(ce, php_date_get_date_ce())) {
pos += amf3_encodeDate(chunk, val, env);
return pos;
} else if (instanceof_function(ce, php_date_get_immutable_ce())) {
pos += amf3_encodeDate(chunk, val, env);
return pos;
} else if (sxe_get_element_class_entry && instanceof_function(ce, sxe_get_element_class_entry())) {
pos += amf3_encodeXml(chunk, val, env);
return pos;
}
pos = amf3_encodeChar(chunk, AMF3_OBJECT);
idx = amf3_getObjIdx(env, val);
if (idx >= 0) {
pos += amf3_encodeU29(chunk, idx << 1); // encode as a reference
} else {
HashTable *ht = Z_OBJPROP_P(val);
zval *hv;
char keyBuf[22];
zend_string *key;
uint keyLen;
ulong idx;
pos += amf3_encodeObjectTraits(chunk, val, ce, env);
if (ce == zend_standard_class_def) { // encode as dynamic anonymous object
ZEND_HASH_FOREACH_KEY_VAL(ht, idx, key, hv) {
if (key) {
if (key->len == 0 || key->val[0] == 0) {
continue; // skip empty key and private/protected properties
}
pos += amf3_encodeStr(chunk, key->val, key->len, env);
pos += amf3_encodeVal(chunk, hv, env);
} else {
// arrays with integer indexes when cast to object produce
// objects with integer property names
keyLen = sprintf(keyBuf, "%ld", idx);
pos += amf3_encodeStr(chunk, keyBuf, keyLen, env);
pos += amf3_encodeVal(chunk, hv, env);
}
} ZEND_HASH_FOREACH_END();
pos += amf3_encodeChar(chunk, 0x01); // end of dynamic members
} else { // encode as typed object
// write property values
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, hv) {
if (key) {
if (key->len == 0 || key->val[0] == 0) {
continue; // skip empty key and private/protected properties
}
if (!zend_hash_exists(&ce->properties_info, key)) {
continue; // skip dynamic properties
}
pos += amf3_encodeVal(chunk, hv, env);
}
} ZEND_HASH_FOREACH_END();
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, hv) {
if (key) {
if (key->len == 0 || key->val[0] == 0) {
continue; // skip empty key and private/protected properties
}
if (zend_hash_exists(&ce->properties_info, key)) {
continue;
}
pos += amf3_encodeStr(chunk, key->val, key->len, env);
pos += amf3_encodeVal(chunk, hv, env);
}
} ZEND_HASH_FOREACH_END();
pos += amf3_encodeChar(chunk, 0x01); // end of dynamic members
}
}
return pos;
}
static int amf3_encodeVal(amf3_chunk_t **chunk, zval *val, amf3_env_t *env) {
int pos = 0;
switch (Z_TYPE_P(val)) {
case IS_NULL:
pos += amf3_encodeChar(chunk, AMF3_NULL);
break;
case IS_TRUE:
pos += amf3_encodeChar(chunk, AMF3_TRUE);
break;
case IS_FALSE:
pos += amf3_encodeChar(chunk, AMF3_FALSE);
break;
case IS_LONG:
if ((Z_LVAL_P(val) < AMF3_MIN_INT) || (Z_LVAL_P(val) > AMF3_MAX_INT)) {
pos += amf3_encodeChar(chunk, AMF3_DOUBLE);
pos += amf3_encodeDouble(chunk, Z_LVAL_P(val));
} else {
pos += amf3_encodeChar(chunk, AMF3_INTEGER);
pos += amf3_encodeU29(chunk, Z_LVAL_P(val));
}
break;
case IS_DOUBLE:
pos += amf3_encodeChar(chunk, AMF3_DOUBLE);
pos += amf3_encodeDouble(chunk, Z_DVAL_P(val));
break;
case IS_STRING:
pos += amf3_encodeChar(chunk, AMF3_STRING);
pos += amf3_encodeStr(chunk, Z_STRVAL_P(val), Z_STRLEN_P(val), env);
break;
case IS_ARRAY:
pos += amf3_encodeArray(chunk, val, env);
break;
case IS_OBJECT:
pos += amf3_encodeObject(chunk, val, env);
break;
case IS_REFERENCE:
pos += amf3_encodeVal(chunk, Z_REFVAL_P(val), env);
break;
case IS_INDIRECT:
pos += amf3_encodeVal(chunk, Z_INDIRECT_P(val), env);
break;
default:
php_error_docref(NULL, E_WARNING, "Unable to encode unsupported value type %d", Z_TYPE_P(val));
break;
}
return pos;
}
static int amf3_decodeArray(zval *val, const char *data, int pos, int size, amf3_env_t *env) {
int oldPos = pos;
int pfx, res = amf3_decodeU29(&pfx, data + pos, size - pos);
if (res < 0) {
php_error_docref(NULL, E_WARNING, "Can't decode array prefix at position %d", pos);
return -1;
}
pos += res;
if (!(pfx & 1)) { // decode as a reference
zval *tmp = amf3_getRef(&env->objs, pfx >> 1);
if (!tmp) {
php_error_docref(NULL, E_WARNING, "Missing array reference index at position %d", pos - res);
return -1;
}
*val = *tmp;
} else {
pfx >>= 1;
if ((pfx < 0) || ((pos + pfx) > size)) {
php_error_docref(NULL, E_WARNING, "Invalid dense array portion size at position %d", pos - res);
return -1;
}
array_init(val);
#if PHP_VERSION_ID >= 70200
HT_ALLOW_COW_VIOLATION(Z_ARRVAL_P(val));
#endif
amf3_putRef(&env->objs, val);
const char *key;
unsigned int keyLen;
zval hv;
for ( ;; ) { // associative array portion
res = amf3_decodeStr(&key, &keyLen, data + pos, size - pos, env);
if (res < 0) {
php_error_docref(NULL, E_WARNING, "Can't decode array key at position %d", pos);
return -1;
}
pos += res;
if (!keyLen) {
break;
}
res = amf3_decodeVal(&hv, data, pos, size, env);
if (res > 0) { // need a trailing \0 in the key buffer to do a proper call to 'add_assoc_zval_ex'
add_assoc_zval_ex(val, key, keyLen, &hv);
} else {
return -1; // nested error
}
pos += res;
}
while (pfx-- > 0) {
res = amf3_decodeVal(&hv, data, pos, size, env);
if (res > 0) {
add_next_index_zval(val, &hv);
} else {
return -1; // nested error
}
pos += res;
}
}
return pos - oldPos;
}
static int amf3_decodeObject(zval *val, const char *data, int pos, int size, amf3_env_t *env) {
int oldPos = pos;
int pfx, res = amf3_decodeU29(&pfx, data + pos, size - pos);
if (res < 0) {
php_error_docref(NULL, E_WARNING, "Can't decode object prefix at position %d", pos);
return -1;
}
pos += res;
if (!(pfx & 1)) { // decode as a reference
zval *tmp = amf3_getRef(&env->objs, pfx >> 1);
if (!tmp) {
php_error_docref(NULL, E_WARNING, "Missing object reference index at position %d", pos - res);
return -1;
}
*val = *tmp;
} else {
amf3_traits_t *traits;
int members;
const char *key;
unsigned int keyLen;
zval prop;
if (!(pfx & 2)) { // decode traits as a reference
ulong idx = pfx >> 2;
zval *found;
if ((found = zend_hash_index_find(&env->traits, idx)) == NULL) {
php_error_docref(NULL, E_WARNING, "Missing object traits reference index at position %d", pos - res);
return -1;
}
traits = (amf3_traits_t *)Z_PTR_P(found);
} else {
if (pfx & 4) {
php_error_docref(NULL, E_WARNING, "Can't decode externalizable object at position %d", pos);
return -1;
}
members = pfx >> 4;
if ((members < 0) || ((pos + members) > size)) {
php_error_docref(NULL, E_WARNING, "Invalid number of class members at position %d", pos - res);
return -1;
}
res = amf3_decodeStr(&key, &keyLen, data + pos, size - pos, env); // class name
if (res < 0) {
php_error_docref(NULL, E_WARNING, "Can't decode class name at position %d", pos);
return -1;
}
if (members > 0 && !keyLen) {
php_error_docref(NULL, E_WARNING, "Empty class name at position %d", pos);
return -1;
}
pos += res;
traits = (amf3_traits_t *)ecalloc(1, sizeof(*traits));
zval tmp;
ZVAL_PTR(&tmp, traits);
zend_hash_index_update(&env->traits, zend_hash_num_elements(&env->traits), &tmp);
if (keyLen) {
// do not try to autoload class, autoloading based on user supplied data is a bad idea
zend_string *str = zend_string_init(key, keyLen, 0);
traits->ce = zend_fetch_class(str, ZEND_FETCH_CLASS_NO_AUTOLOAD);
zend_string_release(str);
if (traits->ce == NULL) {
php_error_docref(NULL, E_WARNING, "Unable to find class at position %d", pos - res);
return -1;
}
}
traits->memberCount = members;
if (members > 0) {
traits->members = (char **)ecalloc(traits->memberCount, sizeof(*traits->members));
traits->memberLengths = (int *)ecalloc(traits->memberCount, sizeof(*traits->memberLengths));
for (members = 0; members < traits->memberCount; members++) {
res = amf3_decodeStr(&key, &keyLen, data + pos, size - pos, env); // member names
if (res < 0) {
return -1; // nested error
}
if (!keyLen || key[0] == 0) {
php_error_docref(NULL, E_WARNING, "Invalid member name at position %d", pos);
return -1;
}
if (!zend_hash_str_exists(&traits->ce->properties_info, key, keyLen)) {
php_error_docref(NULL, E_WARNING, "Unknown member name at position %d", pos);
return -1;
}
pos += res;
traits->members[members] = estrndup(key, keyLen);
traits->memberLengths[members] = keyLen;
}
}
traits->dynamic = pfx & 0x08;
}
if (traits->ce) {
object_init_ex(val, traits->ce);
} else {
object_init(val);
}
amf3_putRef(&env->objs, val);
for (members = 0; members < traits->memberCount; members++) { // sealed members
res = amf3_decodeVal(&prop, data, pos, size, env);
if (res > 0) {
add_property_zval_ex(val, traits->members[members], traits->memberLengths[members], &prop);
Z_TRY_DELREF_P(&prop);
} else {
return -1; // nested error
}
pos += res;
}
if (traits->dynamic) { // dynamic members
for ( ;; ) {
res = amf3_decodeStr(&key, &keyLen, data + pos, size - pos, env);
if (res < 0) {
php_error_docref(NULL, E_WARNING, "Can't decode dynamic member name at position %d", pos);
return -1;
}
pos += res;
if (!keyLen) {
break;
}
if (key[0] == 0) {
php_error_docref(NULL, E_WARNING, "Invalid dynamic property name at position %d", pos - res);
return -1;
}
if (traits->ce) {
if (zend_hash_str_exists(&traits->ce->properties_info, key, keyLen)) {
php_error_docref(NULL, E_WARNING, "Invalid dynamic property name at position %d", pos - res);
return -1;
}
}
res = amf3_decodeVal(&prop, data, pos, size, env);
if (res > 0) { // need a trailing \0 in the key buffer to do a proper call to 'add_property_zval_ex'
add_property_zval_ex(val, key, keyLen, &prop);
Z_TRY_DELREF_P(&prop);
} else {
return -1; // nested error
}
pos += res;
}
}
}
return pos - oldPos;
}
static int amf3_decodeXml(zval *val, const char *data, int pos, int size, amf3_env_t *env) {
int oldPos = pos;
int pfx, res = amf3_decodeU29(&pfx, data + pos, size - pos);
if (res < 0) {
php_error_docref(NULL, E_WARNING, "Can't decode XML prefix at position %d", pos);
return -1;
}
pos += res;
if (!(pfx & 1)) { // decode as a reference
zval *tmp = amf3_getRef(&env->objs, pfx >> 1);
if (!tmp) {
php_error_docref(NULL, E_WARNING, "Missing XML reference index at position %d", pos - res);
return -1;
}
*val = *tmp;
} else {
zval simplexml_load_string, xml;
pfx >>= 1;
if ((pfx < 0) || ((pos + pfx) > size)) {
php_error_docref(NULL, E_WARNING, "Can't decode XML at position %d", pos);
return -1;
}
ZVAL_STRINGL(&simplexml_load_string, "simplexml_load_string", sizeof("simplexml_load_string") - 1);
ZVAL_STRINGL(&xml, data + pos, pfx);
if (call_user_function(EG(function_table), NULL, &simplexml_load_string, val, 1, &xml) == FAILURE ||
(Z_TYPE_P(val) == IS_FALSE)) {
zend_string_release(Z_STR(simplexml_load_string));
zend_string_release(Z_STR(xml));
php_error_docref(NULL, E_WARNING, "Can't load XML at position %d", pos);
return -1;
}
amf3_putRef(&env->objs, val);
pos += pfx;
zend_string_release(Z_STR(simplexml_load_string));
zend_string_release(Z_STR(xml));
}
return pos - oldPos;
}
static int amf3_decodeVal(zval *val, const char *data, int pos, int size, amf3_env_t *env) {
if ((pos < 0) || (pos >= size)) {
php_error_docref(NULL, E_WARNING, "Can't decode type specifier at position %d", pos);
return -1;
}
int oldPos = pos;
amf3_type_t type = (uint8_t)data[pos++];
switch (type) {
case AMF3_UNDEFINED:
case AMF3_NULL:
ZVAL_NULL(val);
break;
case AMF3_FALSE:
ZVAL_FALSE(val);
break;
case AMF3_TRUE:
ZVAL_TRUE(val);
break;
case AMF3_INTEGER: {
int i;
int res = amf3_decodeU29(&i, data + pos, size - pos);
if (res < 0) {
php_error_docref(NULL, E_WARNING, "Can't decode integer at position %d", pos);
return -1;
}
if (i & 0x10000000) {
i |= ~0x1fffffff; // prolong sign bits if negative
}
ZVAL_LONG(val, i);
pos += res;
break;
}
case AMF3_DOUBLE: {
double d;
int res = amf3_decodeDouble(&d, data + pos, size - pos);
if (res < 0) {
php_error_docref(NULL, E_WARNING, "Can't decode double at position %d", pos);
return -1;
}
ZVAL_DOUBLE(val, d);
pos += res;
break;
}
case AMF3_STRING: {
int pfx, res = amf3_decodeU29(&pfx, data + pos, size - pos);
if (res < 0) {
php_error_docref(NULL, E_WARNING, "Can't decode string prefix at position %d", pos);
return -1;
}
pos += res;
if (!(pfx & 1)) { // decode as a reference
zval *tmp = amf3_getRef(&env->strs, pfx >> 1);
if (!tmp) {
php_error_docref(NULL, E_WARNING, "Missing string reference index at position %d", pos - res);
return -1;
}
*val = *tmp;
} else {
pfx >>= 1;
if ((pfx < 0) || ((pos + pfx) > size)) {
php_error_docref(NULL, E_WARNING, "Invalid string length at position %d", pos - res);
return -1;
}
ZVAL_STRINGL(val, data + pos, pfx);
pos += pfx;
if (pfx > 0) {
amf3_putRef(&env->strs, val); // empty string is never sent by reference
}
}
break;
}
case AMF3_DATE: {
int pfx, res = amf3_decodeU29(&pfx, data + pos, size - pos);
if (res < 0) {