-
Notifications
You must be signed in to change notification settings - Fork 1
/
amf.c
3707 lines (3424 loc) · 106 KB
/
amf.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
/**
* amf encoding and decoding of AMF and AMF3 data
*
* @license http://opensource.org/licenses/php.php PHP License Version 3
* @copyright (c) 2006-2007 Emanuele Ruffaldi [email protected]
* @author Emanuele Ruffaldi [email protected]
*
*
* Naming of Functions:
*
* amf_write_ performs low level writing into the buffer
* amf0_write_ writes some C value in AMF0
* amf3_write_ writes some C value in AMF3
* amf0_serialize_ writes a C value in AMF0 with the correct AMF type byte
* amf3_serialize_ writes a C value in AMF0 with the correct AMF type byte
*/
#undef _DEBUG
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "ext/standard/php_string.h"
#include "ext/standard/php_var.h"
#include "ext/standard/php_smart_str.h"
#include "ext/standard/basic_functions.h"
#include "ext/standard/php_incomplete_class.h"
#include "php_amf.h"
#include "php_memory_streams.h"
#include "ext/standard/info.h"
#include "stdlib.h"
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 3) || (PHP_MAJOR_VERSION >= 6)
#undef ZVAL_REFCOUNT
#undef ZVAL_ADDREF
#undef ZVAL_DELREF
#define ZVAL_REFCOUNT Z_REFCOUNT_P
#define ZVAL_ADDREF Z_ADDREF_P
#define ZVAL_DELREF Z_DELREF_P
#endif
/* module Declarations {{{1*/
static function_entry amf_functions[] = {
PHP_FE(amf_encode, NULL)
PHP_FE(amf_decode, NULL)
PHP_FE(amf_join_test, NULL)
PHP_FE(amf_sb_new,NULL)
PHP_FE(amf_sb_append,NULL)
PHP_FE(amf_sb_append_move,NULL)
PHP_FE(amf_sb_length,NULL)
PHP_FE(amf_sb_as_string,NULL)
PHP_FE(amf_sb_write,NULL)
PHP_FE(amf_sb_memusage,NULL)
PHP_FALIAS(amf_sb_flat,amf_sb_as_string,NULL)
PHP_FALIAS(amf_sb_echo,amf_sb_write,NULL)
{NULL, NULL, NULL}
};
static PHP_MINFO_FUNCTION(amf)
{
php_info_print_table_start();
php_info_print_table_header(2, "AMF Native Support", "enabled");
php_info_print_table_row(2, "Compiled Version", PHP_AMF_VERSION);
php_info_print_table_end();
/* DISPLAY_INI_ENTRIES(); */
}
/* resource StringBuilder */
#define PHP_AMF_STRING_BUILDER_RES_NAME "String Builder"
static void php_amf_sb_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC);
int amf_serialize_output_resource_reg;
PHP_MINIT_FUNCTION(amf)
{
amf_serialize_output_resource_reg = zend_register_list_destructors_ex(php_amf_sb_dtor, NULL, PHP_AMF_STRING_BUILDER_RES_NAME, module_number);
return SUCCESS;
}
zend_module_entry amf_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_AMF_WORLD_EXTNAME,
amf_functions,
PHP_MINIT(amf),
NULL,
NULL,
NULL,
PHP_MINFO(amf),
#if ZEND_MODULE_API_NO >= 20010901
PHP_AMF_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_AMF
ZEND_GET_MODULE(amf)
#endif
/* AMF enumeration {{{1*/
/** AMF0 types */
enum AMF0Codes { AMF0_NUMBER, AMF0_BOOLEAN, AMF0_STRING, AMF0_OBJECT, AMF0_MOVIECLIP, AMF0_NULL, AMF0_UNDEFINED,AMF0_REFERENCE,AMF0_MIXEDARRAY,AMF0_ENDOBJECT,AMF0_ARRAY, AMF0_DATE, AMF0_LONGSTRING, AMF0_UNSUPPORTED,AMF0_RECORDSET,AMF0_XML,AMF0_TYPEDOBJECT,AMF0_AMF3};
/** AMF3 types */
enum AMF3Codes { AMF3_UNDEFINED,AMF3_NULL,AMF3_FALSE,AMF3_TRUE,AMF3_INTEGER,AMF3_NUMBER,AMF3_STRING,AMF3_XML, AMF3_DATE, AMF3_ARRAY,AMF3_OBJECT, AMF3_XMLSTRING,AMF3_BYTEARRAY};
/** return values for callbacks */
enum AMFCallbackResult { AMFC_RAW, AMFC_XML, AMFC_OBJECT, AMFC_TYPEDOBJECT, AMFC_ANY, AMFC_ARRAY,AMFC_NONE,AMFC_BYTEARRAY, AMFC_EXTERNAL};
/** flags passed to amf_encode and amf_decode */
enum AMFFlags { AMF_AMF3 = 1, AMF_BIGENDIAN=2,AMF_ASSOC=4,AMF_POST_DECODE = 8,AMF_AS_STRING_BUILDER = 16, AMF_TRANSLATE_CHARSET = 32,AMF_TRANSLATE_CHARSET_FAST = 32|64};
/** events invoked by the callback */
enum AMFEvent { AMFE_MAP = 1, AMFE_POST_OBJECT, AMFE_POST_XML, AMFE_MAP_EXTERNALIZABLE,AMFE_POST_BYTEARRAY,AMFE_TRANSLATE_CHARSET};
/** flags for the recordset _amf_recordset_ */
enum AMFRecordSet { AMFR_NONE = 0, AMFR_ARRAY = 1, AMFR_ARRAY_COLLECTION = 2 };
/** flags for AMF3_OBJECT */
enum AMF3ObjectDecl { AMF_INLINE_ENTITY = 1, AMF_INLINE_CLASS = 2,AMF_CLASS_EXTERNAL = 4,AMF_CLASS_DYNAMIC = 8,AMF_CLASS_MEMBERCOUNT_SHIFT = 4, AMF_CLASS_SHIFT = 2};
/**
* flags for emitting strings that could possibly being translated
* Typically use AMF_STRING_AS_TEXT. When you have bytearrays or XML data it no transformation should be
* made, so use AMF_STRING_AS_BYTE. In some cases internal ASCII strings are sent so just use
* AMF_STRING_AS_SAFE_TEXT that is equivalent to AMF_STRING_AS_BYTE.
*/
enum AMFStringData { AMF_STRING_AS_TEXT = 0, AMF_STRING_AS_BYTE = 1, AMF_STRING_AS_SAFE_TEXT = 1};
enum AMFStringTranslate { AMF_TO_UTF8, AMF_FROM_UTF8};
/* Memory Management {{{1*/
/** deallocates a zval during unserialization of string */
static void amf_zval_dtor(void *p)
{
zval **zval_ptr = (zval **)p;
zval_ptr_dtor(zval_ptr);
}
/** deallocates a class definition during unserialization */
static void amf_class_dtor(void *p)
{
zval **zval_ptr = (zval**)p;
zval_dtor(*zval_ptr);
}
/** context of serialization */
typedef struct
{
HashTable objects0; /* stack of objects, no reference */
HashTable objects; /* stack of objects for AMF3, no reference */
HashTable strings; /* stack of strings for AMF3: string key => inde */
HashTable classes; /* stack of classes for AMF3, allocate */
zval ** callbackTarget;
zval * callbackFx;
zval * zEmpty_string;
int flags;
int nextObjectIndex;
int nextObject0Index;
int nextClassIndex;
int nextStringIndex;
} amf_serialize_data_t,amf_unserialize_data_t;
/**
* The result of the encoder is a string that grows depending on the input. When using memory streams or
* smart_str the allocation of memory is not efficient because these methods allow the generic access of the string.
* Instead in our case a StringBuilder approach is better suited. We have implemented such StringBuilder approach
* in which the resulting string is made of string parts. Each string part has a default length of AMFPARTSIZE
* that eventually can be bigger when long strings are appended. At the end of the processing such sequence of parts
* is joined into the resulting strings.
*
* Note: the AMFTSRMLS_CC and AMFTSRMLS_DC macros are required for supporting the stream method. In the StringBuilder
* method such macros are empty
*
*
* Optimized version: the StringBuilder is made of a sequence of references to string zval and blocks of raw data. In this
* way big strings from PHP are just referenced and copied at the end of the encoding. The memory management is modified
* by allocating a big block of memory in which raw and zval parts are placed. This behaviour is obtained by using a two
* state mechanism
*
* Structure
* |shortlength(2bytes)|rawdata|
* |0(2)|zval|
* |-1|
*/
#define amf_USE_STRING_BUILDER
/* #define amf_DISABLE_OUTPUT */
/*
* This flag controls the use of zval in String Builders:
* Always: #define amf_ZVAL_STRING_BUILDER 1 ||
* Never: #define amf_ZVAL_STRING_BUILDER 0 &&
* Only if with size: #define amf_ZVAL_STRING_BUILDER
*
* The code is:
* if(amf_ZVAL_STRING_BUILDER len > amf_ZVAL_STRING_BUILDER_THRESHOLD)
*/
#define amf_ZVAL_STRING_BUILDER
#define amf_ZVAL_STRING_BUILDER_THRESHOLD 128
/*
#define amf_NO_ZVAL_STRING_BUILDER
#define amf_GUARD_ALLOCATION
*/
#ifdef amf_USE_STRING_BUILDER
typedef struct
{
int size; /* bit 0 = zval, rest is length. Length of 0 is terminato */
union
{
#ifndef amf_NO_ZVAL_STRING_BUILDER
zval * zv; /* zvalue of the strin */
#endif
char data[1];
};
} amf_string_chunk;
/** this structure is placed at the beginning of the data block */
typedef struct amf_string_part_t
{
struct amf_string_part_t * next; /* pointer to the nex */
amf_string_chunk data[1]; /* dummy beginning of the dat */
} amf_string_part;
typedef struct
{
char * data; /* pointer to the data of the current bloc */
int length; /* total length of the strin */
int default_size;
int left_in_part; /* items left in par */
amf_string_chunk * last_chunk;
amf_string_part * last; /* last and current part. The next points to the beginning. Simple lis */
int chunks;
int parts; /* number of parts, useful for debuggin */
int total_allocated; /* total memory allocate */
} amf_serialize_output_t;
typedef amf_serialize_output_t* amf_serialize_output;
#define AMFTSRMLS_CC
#define AMFTSRMLS_DC
#define AMFPARMAXSIZE 32768*4
#define AMFPARTSIZE 64
#define amf_PARTFLAG_ALLOCATED 1
#define amf_PARTFLAG_ZVAL 2
#ifdef amf_GUARD_ALLOCATION
static void *guard_emalloc(int k)
{
void * r = emalloc(k+10); memset(r, 0x7E, k); memset((char*)r+k,0x7F,10); return r;
}
static void guard_memcpy(char * cp, const char * src, int k)
{
while(k-- != 0)
{
if(*cp != 0x7E)
{
printf("guard!!!\n");
break;
}
*cp++ = *src++;
}
}
#else
#define guard_emalloc(k) emalloc(k)
#define guard_memcpy(cp,src,k) memcpy(cp,src,k)
#endif
static inline void amf_write_zstring(amf_serialize_output buf, zval * zstr AMFTSRMLS_DC);
static inline void amf_write_string(amf_serialize_output buf, const char * cp, int length AMFTSRMLS_DC);
/** allocate a block containing the part header and the data */
static amf_string_part * amf_serialize_output_part_ctor(int size)
{
amf_string_part * r = (amf_string_part *)guard_emalloc(size+sizeof(amf_string_part)+sizeof(amf_string_chunk)-sizeof(char));
r->next = r;
r->data->size = 0;
return r;
}
/* closes the current chunk and move the pointer to the next chunk */
static void amf_serialize_output_close_chunk(amf_serialize_output buf)
{
/* close the last chunk if not a zchun */
if(buf->last_chunk->size == 0)
{
buf->last_chunk->size = (buf->data-&buf->last_chunk->data[0]) << 1;
if(buf->last_chunk->size == 0)
return;
/* get another chunk at the en */
buf->last_chunk = (amf_string_chunk*)buf->data;
buf->left_in_part -= sizeof(amf_string_chunk);
buf->chunks++;
}
else
{
buf->last_chunk++;
}
}
static void amf_serialize_output_close_part(amf_serialize_output buf)
{
amf_serialize_output_close_chunk(buf);
buf->last_chunk->size = 0;
}
/** allocates a new StringBuilder with a default buffer */
static void amf_serialize_output_ctor(amf_serialize_output buf)
{
buf->length = 0;
buf->default_size = AMFPARTSIZE;
buf->last = amf_serialize_output_part_ctor(buf->default_size);
buf->last_chunk = &buf->last->data[0];
buf->last_chunk->size = 0;
buf->data = &buf->last_chunk->data[0];
buf->left_in_part = AMFPARTSIZE;
buf->total_allocated = AMFPARTSIZE+sizeof(amf_string_part)+sizeof(amf_string_chunk)-sizeof(char);
buf->parts = 1;
buf->chunks = 0;
}
/**
* appends a block of size specified to the StringBuilder. If the current part is a zpart then take some memory
* from that. The size is not mandatory!
*/
static void amf_serialize_output_part_append(amf_serialize_output buf, int size)
{
amf_string_part * last = buf->last;
amf_string_part * head = last->next;
amf_string_part * cur;
amf_serialize_output_close_part(buf);
if(size == 0)
{
if(buf->default_size < AMFPARMAXSIZE)
buf->default_size *= 2;
size = buf->default_size;
}
else if(size > AMFPARMAXSIZE)
{
size = AMFPARMAXSIZE;
}
cur = amf_serialize_output_part_ctor(size);
buf->parts++; /* number of part */
buf->total_allocated += size+sizeof(amf_string_part)+sizeof(amf_string_chunk)-sizeof(char);
last->next = cur; /* last points to the new las */
cur->next = head; /* new last points to the hea */
buf->last = cur; /* update new las */
buf->last_chunk = &buf->last->data[0];
buf->last_chunk->size = 0;
buf->data = &buf->last_chunk->data[0];
buf->left_in_part = size; /* update the data spac */
}
/** builds a single string from a sequence of strings and places it into a zval */
static void amf_serialize_output_write(amf_serialize_output buf, php_stream * stream TSRMLS_DC)
{
amf_string_part * cur,* head;
if(buf->length == 0)
{
return;
}
head = cur = buf->last->next;
amf_serialize_output_close_part(buf);
/* printf("flattening length:%d parts:%d chunks:%d memory:%d\n", buf->length, buf->parts,buf->chunks,buf->total_allocated) */
do
{
amf_string_chunk * chunk = (amf_string_chunk*)cur->data;
while(chunk->size != 0)
{
#ifndef amf_NO_ZVAL_STRING_BUILDER
if((chunk->size & 1) != 0)
{
if(stream == NULL)
{
zend_write(Z_STRVAL_P(chunk->zv),Z_STRLEN_P(chunk->zv));
}
else
{
php_stream_write(stream, Z_STRVAL_P(chunk->zv),Z_STRLEN_P(chunk->zv));
}
chunk++;
}
else
#endif
{
int len = chunk->size >> 1;
if(stream == NULL)
{
zend_write(chunk->data,len);
}
else
{
php_stream_write(stream, chunk->data,len);
}
chunk = (amf_string_chunk*)(((char*)chunk->data) + len);
}
}
cur = cur->next;
}
while(cur != head);
}
/** appends a sb from another and eventually clean up */
static void amf_serialize_output_append_sb(amf_serialize_output buf,amf_serialize_output inbuf, int copy)
{
amf_string_part * cur,* head,*last;
if(inbuf->length == 0)
{
return;
}
last = inbuf->last;
head = cur = last->next;
if(copy == 1)
{
amf_serialize_output_close_part(inbuf);
do
{
amf_string_chunk * chunk = (amf_string_chunk*)cur->data;
while(chunk->size != 0)
{
#ifndef amf_NO_ZVAL_STRING_BUILDER
if((chunk->size & 1) != 0)
{
amf_write_zstring(buf, chunk->zv);
chunk++;
}
else
#endif
{
int len = chunk->size >> 1;
amf_write_string(buf, chunk->data,len);
chunk = (amf_string_chunk*)(((char*)chunk->data) + len);
}
}
cur = cur->next;
}
while(cur != head);
}
else
{
/* TODO: possibly memory waste in last chun */
amf_string_part * dhead,*dlast;
amf_serialize_output_close_part(buf);
dlast = buf->last;
dhead = dlast->next;
buf->length += inbuf->length;
buf->chunks += inbuf->chunks;
buf->parts += inbuf->parts;
buf->total_allocated += buf->total_allocated;
buf->data = inbuf->data;
dlast->next = head; /* after the last of dst, there is head of sr */
last->next = dhead; /* after the last of src, there is head of ds */
buf->last = last;
buf->last_chunk = inbuf->last_chunk;
buf->left_in_part = inbuf->left_in_part;
/* cleanu */
amf_serialize_output_ctor(inbuf);
}
}
/** builds a single string from a sequence of strings and places it into a zval */
static void amf_serialize_output_get(amf_serialize_output buf, zval * result)
{
amf_string_part * cur,* head;
char * cp,*bcp;
ZVAL_EMPTY_STRING(result);
if(buf->length == 0)
{
return;
}
cp = bcp = guard_emalloc(buf->length);
head = cur = buf->last->next;
amf_serialize_output_close_part(buf);
/* printf("flattening length:%d parts:%d chunks:%d memory:%d\n", buf->length, buf->parts,buf->chunks,buf->total_allocated) */
do
{
amf_string_chunk * chunk = (amf_string_chunk*)cur->data;
while(chunk->size != 0)
{
#ifndef amf_NO_ZVAL_STRING_BUILDER
if((chunk->size & 1) != 0)
{
int len = Z_STRLEN_P(chunk->zv);
guard_memcpy(cp, Z_STRVAL_P(chunk->zv), len);
cp += len;
chunk++;
}
else
#endif
{
int len = chunk->size >> 1;
guard_memcpy(cp, chunk->data, len);
cp += len;
chunk = (amf_string_chunk*)(((char*)chunk->data) + len);
}
}
cur = cur->next;
}
while(cur != head);
ZVAL_STRINGL(result, bcp, buf->length,1);
efree(bcp);
}
/** destructor of the buffer */
static void amf_serialize_output_dtor(amf_serialize_output_t * buf)
{
amf_string_part * head,*cur;
if(buf->last == NULL)
{
return;
}
cur = head = buf->last->next;
do
{
amf_string_part * dt = cur;
cur = cur->next;
efree(dt);
}
while(cur != head);
buf->length = 0;
buf->last = NULL;
}
#else
typedef php_stream amf_serialize_output_t;
typedef amf_serialize_output_t *amf_serialize_output;
#define AMFTSRMLS_CC TSRMLS_CC
#define AMFTSRMLS_DC TSRMLS_DC
#endif
#define amf_SERIALIZE_CTOR(x,cb) amf_serialize_ctor(&x, 1,cb TSRMLS_CC);
#define AMF_UNSERIALIZE_CTOR(x,cb) amf_serialize_ctor(&x, 0, cb TSRMLS_CC);
#define amf_SERIALIZE_DTOR(x,cb)\
zval_ptr_dtor(&(var_hash.zEmpty_string));\
zend_hash_destroy(&(var_hash.objects0));\
zend_hash_destroy(&(var_hash.objects));\
zend_hash_destroy(&(var_hash.strings));\
zend_hash_destroy(&(var_hash.classes));
/* Common {{{1*/
/** initializes a zval to a HashTable of zval with a possible number of items */
static int amf_array_init(zval *arg, int count TSRMLS_DC)
{
ALLOC_HASHTABLE(arg->value.ht);
zend_hash_init(arg->value.ht, count, NULL, ZVAL_PTR_DTOR, 0);
arg->type = IS_ARRAY;
return SUCCESS;
}
/** recevies a pointer to the data and to the callback */
static void amf_serialize_ctor(amf_serialize_data_t * x, int is_serialize, zval** cb TSRMLS_DC)
{
int error = 1;
x->callbackTarget = NULL;
x->callbackFx = NULL;
MAKE_STD_ZVAL(x->zEmpty_string);
ZVAL_EMPTY_STRING(x->zEmpty_string);
if(cb != NULL)
{
if(Z_TYPE_PP(cb) == IS_ARRAY)
{
zval ** tmp1,**tmp2;
HashTable * ht = HASH_OF(*cb);
int n = zend_hash_num_elements(ht);
if(n == 2)
{
if(zend_hash_index_find(ht, 0,(void**)&tmp1) == SUCCESS && Z_TYPE_PP(tmp1) == IS_OBJECT)
{
if(zend_hash_index_find(ht, 1,(void**)&tmp2) == SUCCESS && Z_TYPE_PP(tmp2) == IS_STRING)
{
x->callbackTarget = tmp1;
x->callbackFx = *tmp2;
error = 0;
}
}
}
}
else if(Z_TYPE_PP(cb) == IS_STRING)
{
x->callbackFx = *cb;
error = 0;
}
if(error == 1)
{
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "amf callback requires a string or an array (targetobject,methodname)");
}
}
zend_hash_init(&((x)->objects0), 10, NULL, NULL, 0);
zend_hash_init(&((x)->objects), 10, NULL, NULL, 0);
/* deserializer stores zval of strings for AMF */
zend_hash_init(&((x)->strings), 10, NULL, is_serialize ? NULL : amf_zval_dtor, 0);
(x)->nextObjectIndex = 0;
(x)->nextObject0Index = 0;
(x)->nextClassIndex = 0;
(x)->nextStringIndex = 0;
/* deserializer stores a hash for each class, while serializer is a lon */
zend_hash_init(&((x)->classes), 10, NULL,is_serialize ? NULL: amf_class_dtor, 0);
}
/** returns the i-th element from the array */
static inline int amf_get_index_long(HashTable * ht, int i, int def)
{
zval ** var;
if (zend_hash_index_find(ht, i,(void**)&var) == SUCCESS)
{
if(Z_TYPE_PP(var) == IS_LONG)
{
return Z_LVAL_PP(var);
}
else
{
return def;
}
}
else
{
return def;
}
}
/** returns the i-th element from the array as long and returns default */
static inline int amf_get_assoc_long(HashTable * ht, const char * field, int def)
{
zval ** var;
if (zend_hash_find(ht, (char*)field, strlen(field)+1, (void**)&var) == SUCCESS)
{
if(Z_TYPE_PP(var) == IS_LONG)
{
return Z_LVAL_PP(var);
}
else if(Z_TYPE_PP(var) == IS_DOUBLE)
{
return (int)Z_DVAL_PP(var);
}
else if(Z_TYPE_PP(var) == IS_BOOL)
{
return Z_BVAL_PP(var);
}
else
{
return def;
}
}
else
{
return def;
}
}
/**
* places an object in the cache by using a string representation of its address
* it is not using the direct pointer because the key is not guaranteed to be
* sized as the pointer
* \param old is the pointer to the output code
* \param nextIndex is a pointer to a variable containing the nextIndex used by objects
* \param action if bit 0 is set do not lookup. If bit 1 is set do not add
* \return FAILURE if existent
* code taken from serializer
*/
static inline int amf_cache_zval(HashTable *var_hash, HashTable *var, ulong * old, int * nextIndex, int action)
{
if(sizeof(ulong) >= sizeof(int*))
{
ulong * old_idx = NULL;
ulong idx = (ulong)var;
if((action & 1) == 0)
{
if (zend_hash_index_find(var_hash, idx,(void*)&old_idx) == SUCCESS)
{
*old = *old_idx;
return FAILURE;
}
}
if((action & 2) == 0)
{
/* +1 because otherwise hash will think we are trying to store NULL pointer */
if(nextIndex == NULL)
{
*old = zend_hash_num_elements(var_hash);
}
else
{
*old = *nextIndex;
*nextIndex = *nextIndex+1; /* equal to the number of element */
}
zend_hash_quick_add(var_hash, NULL,0, idx, old, sizeof(*old),NULL);
}
}
else
{
char id[32], *p;
register int len;
/* relies on "(long)" being a perfect hash function for data pointers */
p = smart_str_print_long(id + sizeof(id) - 1, (long) var);
len = id + sizeof(id) - 1 - p;
if((action & 1) == 0)
{
if (zend_hash_find(var_hash, p, len, (void*)&old) == SUCCESS)
{
return FAILURE;
}
}
if((action & 2) == 0)
{
/* +1 because otherwise hash will think we are trying to store NULL pointer */
if(nextIndex == 0)
{
*old = zend_hash_num_elements(var_hash);
}
else
{
*old = *nextIndex;
*nextIndex = *nextIndex+1; /* equal to the number of element */
}
zend_hash_add(var_hash, p, len, old, sizeof(*old), NULL);
}
}
return SUCCESS;
}
static int amf_cache_zval_typed(amf_serialize_data_t*var_hash, zval * val, ulong * old, int version, int action TSRMLS_DC)
{
HashTable *cache = version == 0 ? &(var_hash->objects0) : &(var_hash->objects);
HashTable *obj;
switch(Z_TYPE_P(val))
{
case IS_OBJECT: obj = Z_OBJPROP_P(val); break;
case IS_ARRAY: obj = HASH_OF(val); break;
case IS_RESOURCE: obj = (HashTable*)Z_LVAL_P(val); break;
default: return SUCCESS;
}
return amf_cache_zval(cache,obj,old,version == 0 ? &(var_hash->nextObject0Index) : &(var_hash->nextObjectIndex),action);
}
/* Encode {{{1*/
static void amf0_serialize_var(amf_serialize_output buf, zval **struc, amf_serialize_data_t*var_hash TSRMLS_DC);
static void amf3_serialize_var(amf_serialize_output buf, zval **struc, amf_serialize_data_t *var_hash TSRMLS_DC);
static void amf3_serialize_array(amf_serialize_output buf, HashTable * myht, amf_serialize_data_t *var_hash TSRMLS_DC);
static void amf0_serialize_array(amf_serialize_output buf, HashTable * myht, amf_serialize_data_t* var_hash TSRMLS_DC);
static int amf3_write_string_zval(amf_serialize_output buf, zval * string_zval, enum AMFStringData raw, amf_serialize_data_t*var_hash TSRMLS_DC);
static int amf3_write_string(amf_serialize_output buf, const char * cp, int n, enum AMFStringData raw, amf_serialize_data_t*var_hash TSRMLS_DC);
static void amf3_write_int(amf_serialize_output buf, int num AMFTSRMLS_DC);
#ifdef amf_USE_STRING_BUILDER
/** Writes a single byte into the output buffer */
static inline void amf_write_byte(amf_serialize_output buf, int n)
{
#ifndef amf_DISABLE_OUTPUT
if(buf->left_in_part <= 0)
{
amf_serialize_output_part_append(buf, 0);
}
*buf->data++ = n;
buf->left_in_part--;
buf->length++;
#endif
}
#else
/** Writes a single byte into the output buffer */
static inline void _AMF_write_byte(amf_serialize_output buf, int n TSRMLS_DC)
{
char c = (char)n;
php_stream_write(buf, &c,1);
}
/** Writes a single byte into the output buffer */
#define amf_write_byte(buf,n) _AMF_write_byte((buf),(n) TSRMLS_CC)
#endif
static inline void amf_write_string(amf_serialize_output buf, const char * cp, int length AMFTSRMLS_DC)
{
#ifndef amf_DISABLE_OUTPUT
#ifdef amf_USE_STRING_BUILDER
while(length > 0)
{
int left;
if(buf->left_in_part <= 0)
{
amf_serialize_output_part_append(buf, length > AMFPARTSIZE ? length: 0);
}
left = buf->left_in_part;
if(left > length)
{
left = length;
}
/* printf("append raw %d of %d in buffer of %d\n", left,length,buf->last->length) */
guard_memcpy(buf->data, cp, left);
cp += left;
buf->data += left;
buf->left_in_part -= left;
buf->length += left;
length -= left;
}
#else
php_stream_write(buf, cp,length);
#endif
#endif
}
/** writes a string from a zval. Provides additional optimzation */
static inline void amf_write_zstring(amf_serialize_output buf, zval * zstr AMFTSRMLS_DC)
{
#ifndef amf_DISABLE_OUTPUT
const int len = Z_STRLEN_P(zstr);
if(len == 0)
{
return;
}
#ifdef amf_USE_STRING_BUILDER
#ifndef amf_NO_ZVAL_STRING_BUILDER
else if(amf_ZVAL_STRING_BUILDER len > amf_ZVAL_STRING_BUILDER_THRESHOLD)
{
if(buf->left_in_part < sizeof(amf_string_chunk))
{
amf_serialize_output_part_append(buf, 0 AMFTSRMLS_DC);
}
amf_serialize_output_close_chunk(buf);
buf->last_chunk->size = 1; /* zval chun */
buf->last_chunk->zv = zstr;
ZVAL_ADDREF(zstr);
buf->chunks++;
buf->left_in_part -= sizeof(amf_string_chunk);
/* prepare for a raw chun */
buf->last_chunk++;
buf->last_chunk->size = 0;
buf->data = buf->last_chunk->data;
buf->length += len;
}
#endif
#endif
else
{
amf_write_string(buf, Z_STRVAL_P(zstr),len AMFTSRMLS_CC);
}
#endif
}
/** writes an integer in AMF0 format. It is formatted in Big Endian 4 byte */
static void amf0_write_int(amf_serialize_output buf, int n AMFTSRMLS_DC)
{
char tmp[4] = { (n >> 24) & 0xFF, (n >> 16) & 0xFF, (n >> 8) & 0xFF, (n & 0xFF) };
amf_write_string(buf, tmp,4 AMFTSRMLS_CC);
}
/** writes a short in AMF0 format. It is formatted in Big Endian 2 byte */
static void amf0_write_short(amf_serialize_output buf, int n AMFTSRMLS_DC)
{
amf_write_byte(buf,((n >> 8) & 0xFF));
amf_write_byte(buf,(n & 0xFF));
}
/** writes the end of obejct terminator of AMF0 */
static void amf0_write_endofobject(amf_serialize_output buf AMFTSRMLS_DC)
{
static char endOfObject[] = {0,0,9};
amf_write_string(buf,endOfObject,3 AMFTSRMLS_CC);
}
static zval*amf_translate_charset_zstring(zval * inz, enum AMFStringTranslate direction,amf_serialize_data_t*var_hash TSRMLS_DC);
static zval*amf_translate_charset_string(const char * cp, int length, enum AMFStringTranslate direction,amf_serialize_data_t*var_hash TSRMLS_DC);
/** serializes a zval as zstring in AMF0 using AMF0_STRING or AMF0_LONGSTRING */
static void amf0_serialize_zstring(amf_serialize_output buf, zval* zv,enum AMFStringData raw, amf_serialize_data_t*var_hash TSRMLS_DC)
{
int length;
if(raw == AMF_STRING_AS_TEXT && (var_hash->flags & AMF_TRANSLATE_CHARSET) != 0)
{
zval * zzv;
if((zzv = amf_translate_charset_zstring(zv, AMF_TO_UTF8, var_hash TSRMLS_CC)) != 0)
{
zv = zzv;
}
}
/* AMF string: b(2) w(length) text(utf) if length < 6553 */
/* AMF string: b(12) dw(length) text(utf */
length = Z_STRLEN_P(zv);
if(length < 65536)
{
amf_write_byte(buf,AMF0_STRING);
amf0_write_short(buf,length AMFTSRMLS_CC);
if(length == 0)
{
return;
}
}
else
{
amf_write_byte(buf,AMF0_LONGSTRING);
amf0_write_int(buf,length AMFTSRMLS_CC);
}
amf_write_zstring(buf,zv AMFTSRMLS_CC);
}
static void amf0_serialize_emptystring(amf_serialize_output buf AMFTSRMLS_DC)
{
amf_write_byte(buf,AMF0_STRING);
amf0_write_short(buf,0 AMFTSRMLS_CC);
}
/** serializes a string variable */
static void amf0_serialize_string(amf_serialize_output buf, const char * cp,enum AMFStringData raw, amf_serialize_data_t*var_hash TSRMLS_DC)
{
int length = strlen(cp);
if(length > 0 && raw == AMF_STRING_AS_TEXT && (var_hash->flags & AMF_TRANSLATE_CHARSET) != 0)
{
zval * zzv = 0;
if((zzv = amf_translate_charset_string(cp,length, AMF_TO_UTF8,var_hash TSRMLS_CC)) != 0)
{
amf0_serialize_zstring(buf, zzv,AMF_STRING_AS_BYTE,var_hash TSRMLS_CC);
return;
}
}
length = strlen(cp);
if(length < 65536)
{
amf_write_byte(buf,AMF0_STRING);
amf0_write_short(buf,length AMFTSRMLS_CC);
}
else
{
amf_write_byte(buf,AMF0_LONGSTRING);
amf0_write_int(buf,length AMFTSRMLS_CC);
}
amf_write_string(buf,cp,length AMFTSRMLS_CC);
}
/** sends a short string to AMF */
static void amf0_write_string(amf_serialize_output buf, const char * cp, enum AMFStringData raw, amf_serialize_data_t*var_hash TSRMLS_DC)
{
int length = strlen(cp);
if(length > 0 && raw == AMF_STRING_AS_TEXT && (var_hash->flags & AMF_TRANSLATE_CHARSET) != 0)
{
zval * zzv = 0;
if((zzv = amf_translate_charset_string(cp,length,AMF_TO_UTF8,var_hash TSRMLS_CC)) != 0)
{
int length = Z_STRLEN_P(zzv);
if(length >= 65536)
{
length = 65536-2;
}
amf0_write_short(buf,length AMFTSRMLS_CC);
amf_write_zstring(buf, zzv AMFTSRMLS_CC);
return;
}
}
length = strlen(cp);
amf0_write_short(buf,length AMFTSRMLS_CC);
amf_write_string(buf,cp,length AMFTSRMLS_CC);
}
/** serializes an empty AMF3 string */
static inline void amf3_write_emptystring(amf_serialize_output buf AMFTSRMLS_DC)
{
amf_write_byte(buf, 1);
}
/** writes the AMF3_OBJECT followed by the class information */