-
Notifications
You must be signed in to change notification settings - Fork 2
/
AMF.xs
3181 lines (2929 loc) · 90 KB
/
AMF.xs
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
/*
* vim: ts=8 sw=4 sts=4 et
* */
#define _CRT_SECURE_NO_DEPRECATE /* Win32 compilers close eyes... */
#define PERL_NO_GET_CONTEXT
#undef PERL_IMPLICIT_SYS /* Sigsetjmp will not work under this */
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#define NEED_mg_findext
#define NEED_grok_number
#define NEED_grok_numeric_radix
#define NEED_newRV_noinc
#define NEED_sv_2pv_flags
#include "ppport.h"
#ifndef PERL_UNUSED_VAR
# define PERL_UNUSED_VAR(var) if (0) var = var
#endif
#ifndef STATIC_INLINE /* a public perl API from 5.13.4 */
# if defined(__GNUC__) || defined(__cplusplus__) || (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L))
# define STATIC_INLINE static inline
# else
# define STATIC_INLINE static
# endif
#endif /* STATIC_INLINE */
#ifndef inline /* don't like borgs definitions */ /* inline is keyword for STDC compiler */
# if defined(__GNUC__) || defined(__cplusplus__) || (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L))
# if defined(__APPLE__)
# define inline
# endif
# else
# if defined(WIN32) && defined(_MSV) /* Microsoft Compiler */
# define inline _inline
# else
# define inline
# endif
# endif
#endif /* inline */
/* Stupid FreeBSD compiler failed with plain inline */
#define FREE_INLINE STATIC_INLINE
#define MARKER3_UNDEF '\x00'
#define MARKER3_NULL '\x01'
#define MARKER3_FALSE '\x02'
#define MARKER3_TRUE '\x03'
#define MARKER3_INTEGER '\x04'
#define MARKER3_DOUBLE '\x05'
#define MARKER3_STRING '\x06'
#define MARKER3_XML_DOC '\x07'
#define MARKER3_DATE '\x08'
#define MARKER3_ARRAY '\x09'
#define MARKER3_OBJECT '\x0a'
#define MARKER3_XML '\x0b'
#define MARKER3_BYTEARRAY '\x0c'
#define MARKER3_AMF_PLUS '\x11'
#define MARKER0_NUMBER '\x00'
#define MARKER0_BOOLEAN '\x01'
#define MARKER0_STRING '\x02'
#define MARKER0_OBJECT '\x03'
#define MARKER0_CLIP '\x04'
#define MARKER0_UNDEFINED '\x05'
#define MARKER0_NULL '\x06'
#define MARKER0_REFERENCE '\x07'
#define MARKER0_ECMA_ARRAY '\x08'
#define MARKER0_OBJECT_END '\x09'
#define MARKER0_STRICT_ARRAY '\x0a'
#define MARKER0_DATE '\x0b'
#define MARKER0_LONG_STRING '\x0c'
#define MARKER0_UNSUPPORTED '\x0d'
#define MARKER0_RECORDSET '\x0e'
#define MARKER0_XML_DOCUMENT '\x0f'
#define MARKER0_TYPED_OBJECT '\x10'
#define MARKER0_AMF_PLUS '\x11'
#define ERR_EOF 1
#define ERR_AMF0_REF 2
#define ERR_MARKER 3
#define ERR_BAD_OBJECT 4
#define ERR_OVERFLOW 5
#define ERR_UNIMPLEMENTED 6
#define ERR_BAD_STRING_REF 7
#define ERR_BAD_DATE_REF 8
#define ERR_BAD_OBJECT_REF 9
#define ERR_BAD_ARRAY_REF 10
#define ERR_BAD_STRING_REF_UNUSED 11
#define ERR_BAD_TRAIT_REF 12
#define ERR_BAD_XML_REF 13
#define ERR_BAD_BYTEARRAY_REF 14
#define ERR_EXTRA_BYTE 15
#define ERR_INT_OVERFLOW 16
#define ERR_RECURRENT_OBJECT 17
#define ERR_BAD_REFVAL 18
#define ERR_INTERNAL 19
#define ERR_ARRAY_TOO_BIG 20
#define ERR_BAD_OPTION 21
#define OPT_STRICT 1
#define OPT_DECODE_UTF8 2
#define OPT_ENCODE_UTF8 4
#define OPT_RAISE_ERROR 8
#define OPT_MILLSEC_DATE 16
#define OPT_PREFER_NUMBER 32
#define OPT_JSON_BOOLEAN 64
#define OPT_MAPPER 128
#define OPT_TARG 256
#define OPT_SKIP_BAD 512
#define STR_EMPTY '\x01'
#define EXPERIMENT1
#define AMF0_VERSION 0
#define AMF3_VERSION 3
#if BYTEORDER == 0x1234
#define GAX "LIT"
#define GET_NBYTE(ALL, IPOS, TYPE) (ALL - 1 - IPOS)
#else
#if BYTEORDER == 0x12345678
#define GAX "LIT"
#define GET_NBYTE(ALL, IPOS, TYPE) (ALL - 1 - IPOS)
#else
#if BYTEORDER == 0x87654321
#define GAX "BIG"
#define GET_NBYTE(ALL, IPOS, TYPE) (sizeof(TYPE) -ALL + IPOS)
#else
#if BYTEORDER == 0x4321
#define GAX "BIG"
#define GET_NBYTE(ALL, IPOS, TYPE) (sizeof(TYPE) -ALL + IPOS)
#else
#error Unknown byteorder. Please append your byteorder to Storable/AMF.xs
#endif
#endif
#endif
#endif
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#define SIGN_BOOL_APPLY( obj, sign, mask ) ( sign > 0 ? obj|=mask : sign <0 ? obj&=~mask : 0 )
#define DEFAULT_MASK (OPT_PREFER_NUMBER|OPT_TARG)
STATIC MGVTBL my_vtbl_empty = {0, 0, 0, 0, 0, 0, 0};
char *error_messages[] = {
"ERR_EOF",
"ERR_BAD_AMF0_REF",
"ERR_MARKER",
"ERR_BAD_OBJECT",
"ERR_OVERFLOW",
"ERR_UNIMPLEMENTED",
"ERR_BAD_STRING_REF",
"ERR_BAD_DATE_REF",
"ERR_BAD_OBJECT_REF",
"ERR_BAD_ARRAY_REF",
"ERR_BAD_STRING_REF_UNUSED",
"ERR_BAD_TRAIT_REF",
"ERR_BAD_XML_REF",
"ERR_BAD_BYTEARRAY_REF",
"ERR_EXTRA_BYTE",
"ERR_INT_OVERFLOW",
"ERR_RECURRENT_OBJECT",
"ERR_BAD_REFVAL",
"ERR_INTERNAL",
"ERR_ARRAY_TOO_BIG",
"ERR_BAD_OPTION",
0
};
struct io_amf_option;
/*#define TRACE0 */
struct amf3_restore_point{
int offset_buffer;
int offset_object;
int offset_trait;
int offset_string;
int arr_max;
};
struct io_struct{
unsigned char * ptr;
unsigned char * pos;
unsigned char * end;
SV *sv_buffer;
AV *arr_object;
AV *arr_string;
AV *arr_trait;
HV *hv_object;
HV *hv_string;
HV *hv_trait;
SV *sv_buffer2;
AV *arr_object2;
AV *arr_string2;
AV *arr_trait2;
HV *hv_object2;
HV *hv_string2;
HV *hv_trait2;
int rc_object;
int rc_string;
int rc_trait;
int version;
int final_version;
int buffer_step_inc;
int arr_max;
int error_code;
Sigjmp_buf target_error;
SV * (*parse_one_object)(pTHX_ struct io_struct * io);
char *subname;
int options;
int default_options;
SV * Bool[2];
int bool_init;
char status;
bool reuse;
};
FREE_INLINE void ref_clear(pTHX_ HV * go_once, SV *sv);
FREE_INLINE SV* tmpstorage_create_sv( pTHX_ CV *cv, SV *option );
FREE_INLINE void tmpstorage_destroy_sv( pTHX_ SV *self);
FREE_INLINE struct io_struct * tmpstorage_create_io( pTHX_ void * );
FREE_INLINE void tmpstorage_destroy_io( pTHX_ struct io_struct *io);
STATIC_INLINE SV * amf0_parse_one(pTHX_ struct io_struct * io);
STATIC_INLINE SV * amf3_parse_one(pTHX_ struct io_struct * io);
FREE_INLINE void io_in_destroy(pTHX_ struct io_struct * io, AV *);
FREE_INLINE void io_in_cleanup(pTHX_ struct io_struct *io);
FREE_INLINE void io_out_cleanup(pTHX_ struct io_struct * io);
FREE_INLINE void io_test_eof(pTHX_ struct io_struct *io);
FREE_INLINE void io_register_error(struct io_struct *io, int);
FREE_INLINE void io_register_error_and_free(pTHX_ struct io_struct *io, int, SV *);
FREE_INLINE int
io_position(struct io_struct *io){
return io->pos-io->ptr;
}
FREE_INLINE void
io_set_position(struct io_struct *io, int pos){
io->pos = io->ptr + pos;
}
FREE_INLINE void
io_savepoint(pTHX_ struct io_struct *io, struct amf3_restore_point *p){
p->offset_buffer = io_position(io);
p->offset_object = av_len(io->arr_object);
p->offset_trait = av_len(io->arr_trait);
p->offset_string = av_len(io->arr_string);
}
FREE_INLINE void
io_restorepoint(pTHX_ struct io_struct *io, struct amf3_restore_point *p){
io_set_position(io, p->offset_buffer);
while(av_len(io->arr_object) > p->offset_object){
SV * abc = av_pop(io->arr_object);
sv_2mortal(abc);
}
while(av_len(io->arr_trait) > p->offset_trait){
sv_2mortal(av_pop(io->arr_trait));
}
while(av_len(io->arr_string) > p->offset_string){
sv_2mortal(av_pop(io->arr_string));
}
}
FREE_INLINE void
io_move_backward(struct io_struct *io, int step){
io->pos-= step;
}
FREE_INLINE void
io_move_forward(struct io_struct *io, int len){
io->pos+=len;
}
FREE_INLINE void
io_require(struct io_struct *io, int len){
if (io->end - io->pos < len){
io_register_error(io, ERR_EOF);
}
}
FREE_INLINE void
io_reserve(pTHX_ struct io_struct *io, int len){
if (io->end - io->pos< len){
unsigned int ipos = io->pos - io->ptr;
unsigned int buf_len;
SvCUR_set(io->sv_buffer, ipos);
buf_len = SvLEN(io->sv_buffer);
while( buf_len < ipos + len + io->buffer_step_inc){
buf_len *= 4;
buf_len += len+io->buffer_step_inc;
}
io->ptr = (unsigned char *) SvGROW(io->sv_buffer, buf_len);
io->pos = io->ptr + ipos;
io->end = io->ptr + SvLEN(io->sv_buffer);
}
}
FREE_INLINE void io_register_error(struct io_struct *io, int errtype){
io->error_code = errtype;
Siglongjmp(io->target_error, errtype);
}
FREE_INLINE void io_test_eof(pTHX_ struct io_struct *io){
if (io->pos!=io->end){
io_register_error(io, ERR_EOF );
}
}
void io_format_error(pTHX_ struct io_struct *io ){
int error_code = io->error_code;
char *message;
if ( error_code < 1 || error_code >= ARRAY_SIZE( error_messages )){
error_code = ERR_INTERNAL;
};
message = error_messages[ error_code - 1];
if ( io->status == 'r' ){
io_in_destroy(aTHX_ io, 0); /* all objects */
if (io->options & OPT_RAISE_ERROR){
croak("Parse AMF%d: %s (ERR-%d)", io->version, message, error_code);
}
else {
sv_setiv(ERRSV, error_code);
sv_setpvf(ERRSV, "Parse AMF%d: %s (ERR-%d)", io->version, message, error_code);
SvIOK_on(ERRSV);
}
}
else { /* io->status == 'w' */
io_out_cleanup(aTHX_ io);
if (io->options & OPT_RAISE_ERROR){
croak("Format AMF%d: %s (ERR-%d)", io->version, message, error_code);
}
else {
sv_setiv(ERRSV, error_code);
sv_setpvf(ERRSV, "Format AMF%d: %s (ERR-%d)", io->version, message, error_code);
SvIOK_on(ERRSV);
}
}
}
FREE_INLINE void io_register_error_and_free(pTHX_ struct io_struct *io, int errtype, SV *pointer){
if (pointer)
sv_2mortal((SV*) pointer);
Siglongjmp(io->target_error, errtype);
}
FREE_INLINE struct io_struct * tmpstorage_create_io(pTHX_ void*ignore){
int ibuf_size = 10240;
struct io_struct * io;
SV *sv ;
PERL_UNUSED_VAR(ignore);
Newxz( io, 1, struct io_struct );
io->arr_object2 = newAV();
io->arr_string2 = newAV();
io->arr_trait2 = newAV();
io->arr_object = io->arr_object2;
io->arr_string = io->arr_string2;
io->arr_trait = io->arr_trait2;
av_extend( io->arr_object, 32 );
av_extend( io->arr_string, 32 );
av_extend( io->arr_trait, 32 );
io->hv_object = newHV();
HvSHAREKEYS_off( io->hv_object );
io->hv_string = newHV();
HvSHAREKEYS_off( io->hv_string );
io->hv_trait = newHV();
HvSHAREKEYS_off( io->hv_trait );
io->hv_object2 = io->hv_object;
io->hv_string2 = io->hv_string;
io->hv_trait2 = io->hv_trait;
io->sv_buffer2 = newSV(0);
(void)SvUPGRADE(io->sv_buffer2, SVt_PV);
SvPOK_on( io->sv_buffer2);
SvGROW( io->sv_buffer2, ibuf_size );
io->default_options = DEFAULT_MASK;
io->options = DEFAULT_MASK;
io->reuse = 1;
return io;
}
FREE_INLINE struct io_struct * tmpstorage_create_and_cache(pTHX_ CV *cv){
MAGIC *mg;
struct io_struct *io;
SV *cache_sv;
mg = mg_findext( (SV *)cv, PERL_MAGIC_ext, &my_vtbl_empty);
if (mg){
/* fprintf(stderr, "Found magic=%p\n", mg->mg_ptr); */
io = (struct io_struct *)mg->mg_ptr;
return io;
}
cache_sv = get_sv("Storable::AMF0::CacheIO", GV_ADDMULTI | GV_ADD);
mg = SvTYPE(cache_sv) ? mg_findext( (SV *)cache_sv, PERL_MAGIC_ext, &my_vtbl_empty) : NULL;
if (mg){
/* fprintf(stderr, "Found with var magic=%p\n", mg->mg_ptr); */
io = (struct io_struct *)mg->mg_ptr;
}
else {
/* fprintf(stderr, "Not Found magic=%p\n", io); */
io = tmpstorage_create_io(aTHX_ NULL);
sv_magicext( (SV *)cache_sv, NULL, PERL_MAGIC_ext, &my_vtbl_empty, (const char * const)io, 0);
}
sv_magicext( (SV *)cv, NULL, PERL_MAGIC_ext, &my_vtbl_empty, (const char * const)io, 0);
return io;
}
FREE_INLINE SV* tmpstorage_create_sv(pTHX_ CV *cv, SV* option){
struct io_struct * io;
SV *sv ;
io = tmpstorage_create_io(aTHX_ NULL);
if ( option ){
io->options = SvIV(option);
io->default_options= SvIV(option);
}
else {
io->options = DEFAULT_MASK;
io->default_options= DEFAULT_MASK;
}
sv = sv_newmortal();
sv_setref_iv( sv, "Storable::AMF0::TemporaryStorage", PTR2IV( io ) );
return sv;
}
FREE_INLINE void tmpstorage_destroy_io( pTHX_ struct io_struct *io ){
SvREFCNT_dec( (SV *) io->arr_object2 );
SvREFCNT_dec( (SV *) io->arr_string2 );
SvREFCNT_dec( (SV *) io->arr_trait2 );
SvREFCNT_dec( (SV *) io->hv_object2 );
SvREFCNT_dec( (SV *) io->hv_string2 );
SvREFCNT_dec( (SV *) io->hv_trait2 );
SvREFCNT_dec( (SV *) io->sv_buffer2 );
Safefree( io );
/* fprintf( stderr, "Destroy\n"); */
}
FREE_INLINE void tmpstorage_destroy_sv( pTHX_ SV *self ){
if ( ! SvROK( self )) {
croak( "Bad Storable::AMF0::TemporaryStorage" );
}
else {
tmpstorage_destroy_io( aTHX_ INT2PTR( struct io_struct*, SvIV( SvRV( self ) ) ) );
}
}
FREE_INLINE void io_in_cleanup(pTHX_ struct io_struct *io){
av_clear( io->arr_object );
if ( AMF3_VERSION == io->final_version ){
av_clear( io->arr_string );
av_clear( io->arr_trait );
};
}
FREE_INLINE void io_out_cleanup(pTHX_ struct io_struct *io){
hv_clear( io->hv_object );
if ( AMF3_VERSION == io->version ){
hv_clear( io->hv_string );
hv_clear( io->hv_trait );
};
}
FREE_INLINE void io_in_init(pTHX_ struct io_struct * io, SV* data, int amf_version, SV * sv_option){
struct io_struct *reuse_storage_ptr=io;
bool reuse_storage = 1;
if ( sv_option ){
if (! SvIOK(sv_option)){
if ( ! sv_isobject( sv_option )){
warn( "options are not integer" );
io_register_error(io, ERR_BAD_OPTION );
return;
}
else {
reuse_storage_ptr = INT2PTR( struct io_struct *, SvIV( SvRV( sv_option )));
io->options = reuse_storage_ptr->options;
}
}
else {
io->options = SvIV(sv_option);
io->bool_init = 0;
}
}
else {
io->options = io->default_options;
}
io->reuse = reuse_storage_ptr != io;
if (SvMAGICAL(data))
mg_get(data);
if (!SvPOKp(data))
croak("%s. data must be a string", io->subname);
if (SvUTF8(data))
croak("%s: data is utf8. Can't process utf8", io->subname);
io->ptr = (unsigned char *) SvPVX(data);
io->end = io->ptr + SvCUR(data);
io->pos = io->ptr;
io->status = 'r';
io->version = amf_version;
if ( amf_version == AMF0_VERSION && (io->ptr[0] == MARKER0_AMF_PLUS) ){
amf_version = AMF3_VERSION;
++io->pos;
};
io->final_version = amf_version;
/* Support when array extend is too big */
io->arr_max = SvCUR( data );
if (amf_version == AMF3_VERSION) {
if ( reuse_storage ){
io->arr_object = reuse_storage_ptr->arr_object2;
io->arr_string = reuse_storage_ptr->arr_string2;
io->arr_trait = reuse_storage_ptr->arr_trait2;
io->reuse = 1;
}
else {
io->arr_object = newAV();
sv_2mortal( (SV *) (io->arr_object) );
io->arr_string = newAV();
sv_2mortal((SV*) io->arr_string);
io->arr_trait = newAV();
sv_2mortal((SV*) io->arr_trait);
}
}
else {
if ( reuse_storage ){
io->arr_object = reuse_storage_ptr->arr_object2;
io->reuse = 1;
}
else {
io->arr_object = newAV();
sv_2mortal( (SV *) (io->arr_object) );
}
}
if (amf_version == AMF3_VERSION) {
io->parse_one_object = amf3_parse_one;
}
else {
io->parse_one_object = amf0_parse_one;
}
return;
}
FREE_INLINE void io_in_destroy(pTHX_ struct io_struct * io, AV *a){
int i;
SV **ref_item;
int alen;
SV *item;
if (a) {
alen = av_len(a);
for(i = 0; i<= alen; ++i){
ref_item = av_fetch(a,i,0);
if (ref_item){
if (SvROK(*ref_item)){
item = SvRV(*ref_item);
if (SvTYPE(item) == SVt_PVAV){
av_clear((AV*) item);
}
else if (SvTYPE(item) == SVt_PVHV){
HV * h = (HV*) item;
hv_clear(h);
}
}
}
}
av_clear(a); /* cleaning array */
}
else {
if (io->final_version == AMF0_VERSION){
io_in_destroy(aTHX_ io, io->arr_object);
}
else if (io->final_version == AMF3_VERSION) {
io_in_destroy(aTHX_ io, io->arr_object);
io_in_destroy(aTHX_ io, io->arr_trait); /* May be not needed */
io_in_destroy(aTHX_ io, io->arr_string);
}
else {
croak("bad version at destroy");
}
}
}
STATIC_INLINE void io_out_init(pTHX_ struct io_struct *io, SV*sv_option, int amf_version){
unsigned int ibuf_size = 10240;
unsigned int ibuf_step = 20480;
struct io_struct *reuse_storage_ptr = io;
SV *sv_buffer;
io->version = amf_version;
io->reuse = 1;
if ( sv_option ){
if ( SvROK(sv_option) && sv_isobject( sv_option )){
reuse_storage_ptr = INT2PTR( struct io_struct *, SvIV( SvRV( sv_option )));
io->options = reuse_storage_ptr->options;
}
else if ( !SvIOK(sv_option)){
io_register_error(io, ERR_BAD_OPTION );
}
else {
io->options = SvIV( sv_option );
}
}
else {
io->options = io->default_options;
};
if ( io->options & OPT_TARG ){
dXSTARG;
sv_buffer = TARG;
(void)SvUPGRADE(sv_buffer, SVt_PV);
SvPOK_on(sv_buffer);
SvGROW( sv_buffer, 7 );
if (SvLEN(sv_buffer) <= 64 ){
sv_buffer = reuse_storage_ptr->sv_buffer2;
}
}
else {
if (io->reuse){
sv_buffer = reuse_storage_ptr->sv_buffer2;
}
else {
sv_buffer = sv_2mortal(newSVpvn("",0));
SvGROW(sv_buffer, ibuf_size);
}
}
io->sv_buffer = sv_buffer;
if (amf_version) {
if (io->reuse){
io->hv_object = reuse_storage_ptr->hv_object2;
io->hv_string = reuse_storage_ptr->hv_string2;
io->hv_trait = reuse_storage_ptr->hv_trait2;
}
else {
io->hv_object = newHV();
io->hv_string = newHV();
io->hv_trait = newHV();
HvSHAREKEYS_off( io->hv_object );
HvSHAREKEYS_off( io->hv_string );
HvSHAREKEYS_off( io->hv_trait );
sv_2mortal((SV *)io->hv_object);
sv_2mortal((SV *)io->hv_string);
sv_2mortal((SV *)io->hv_trait);
}
io->rc_object = 0;
io->rc_string = 0;
io->rc_trait = 0;
}
else {
if (io->reuse ){
io->hv_object = reuse_storage_ptr->hv_object2;
}
else {
io->hv_object = newHV();
HvSHAREKEYS_off( io->hv_object );
sv_2mortal((SV*)io->hv_object);
}
io->rc_object = 0;
}
io->buffer_step_inc = ibuf_step;
io->ptr = (unsigned char *) SvPV_nolen(sv_buffer);
io->pos = io->ptr;
io->end = (unsigned char *) SvEND(sv_buffer);
io->status = 'w';
}
FREE_INLINE SV * io_buffer(struct io_struct *io){
SvCUR_set(io->sv_buffer, io->pos - io->ptr);
return io->sv_buffer;
}
FREE_INLINE double io_read_double(struct io_struct *io);
FREE_INLINE unsigned char io_read_marker(struct io_struct * io);
FREE_INLINE int io_read_u8(struct io_struct * io);
FREE_INLINE int io_read_u16(struct io_struct * io);
FREE_INLINE int io_read_u32(struct io_struct * io);
FREE_INLINE int io_read_u24(struct io_struct * io);
#define MOVERFLOW(VALUE, MAXVALUE, PROC)\
if (VALUE > MAXVALUE) { \
fprintf( stderr, "Overflow in %s. expected less %d. got %d\n", PROC, MAXVALUE, VALUE); \
io_register_error(io, ERR_OVERFLOW); \
}
FREE_INLINE void io_write_double(pTHX_ struct io_struct *io, double value){
const int step = 8;
union {
signed int iv;
unsigned int uv;
double nv;
char c[8];
} v;
io_reserve(aTHX_ io, step );
v.nv = value;
io->pos[0] = v.c[GET_NBYTE(step, 0, value)];
io->pos[1] = v.c[GET_NBYTE(step, 1, value)];
io->pos[2] = v.c[GET_NBYTE(step, 2, value)];
io->pos[3] = v.c[GET_NBYTE(step, 3, value)];
io->pos[4] = v.c[GET_NBYTE(step, 4, value)];
io->pos[5] = v.c[GET_NBYTE(step, 5, value)];
io->pos[6] = v.c[GET_NBYTE(step, 6, value)];
io->pos[7] = v.c[GET_NBYTE(step, 7, value)];
io->pos+= step ;
return;
}
FREE_INLINE void io_write_marker(pTHX_ struct io_struct * io, char value) {
const int step = 1;
io_reserve(aTHX_ io, 1);
io->pos[0]= value;
io->pos+=step;
return;
}
FREE_INLINE void io_write_uchar (pTHX_ struct io_struct * io, unsigned char value) {
const int step = 1;
io_reserve(aTHX_ io, 1);
io->pos[0]= value;
io->pos+=step;
return;
}
FREE_INLINE void io_write_u8(pTHX_ struct io_struct * io, unsigned int value){
const int step = 1;
union {
signed int iv;
unsigned int uv;
double nv;
char c[8];
} v;
v.uv = value;
MOVERFLOW(value, 255, "write_u8");
io_reserve(aTHX_ io, 1);
io->pos[0]= v.c[GET_NBYTE(step, 0,value)];
io->pos+=step ;
return;
}
FREE_INLINE void io_write_s16(pTHX_ struct io_struct * io, signed int value){
const int step = 2;
union {
signed int iv;
unsigned int uv;
double nv;
char c[8];
} v;
v.iv = value;
MOVERFLOW(value, 32767, "write_s16");
io_reserve(aTHX_ io, step);
io->pos[0]= v.c[GET_NBYTE(step, 0, value)];
io->pos[1]= v.c[GET_NBYTE(step, 1, value)];
io->pos+=step;
return;
}
FREE_INLINE void io_write_u16(pTHX_ struct io_struct * io, unsigned int value){
const int step = 2;
union {
signed int iv;
unsigned int uv;
double nv;
char c[8];
} v;
io_reserve(aTHX_ io,step);
MOVERFLOW(value, 65535 , "write_u16");
v.uv = value;
io->pos[0] = v.c[GET_NBYTE(step, 0, value)];
io->pos[1] = v.c[GET_NBYTE(step, 1, value)];
io->pos+=step;
return;
}
FREE_INLINE void io_write_u32(pTHX_ struct io_struct * io, unsigned int value){
const int step = 4;
union {
signed int iv;
unsigned int uv;
double nv;
char c[8];
} v;
io_reserve(aTHX_ io,step);
v.uv = value;
io->pos[0] = v.c[GET_NBYTE(step, 0, value)];
io->pos[1] = v.c[GET_NBYTE(step, 1, value)];
io->pos[2] = v.c[GET_NBYTE(step, 2, value)];
io->pos[3] = v.c[GET_NBYTE(step, 3, value)];
io->pos+=step;
return;
}
FREE_INLINE void io_write_u24(pTHX_ struct io_struct * io, unsigned int value){
const int step = 3;
union {
signed int iv;
unsigned int uv;
double nv;
char c[8];
} v;
io_reserve(aTHX_ io,step);
MOVERFLOW(value,16777215 , "write_u16");
v.uv = value;
io->pos[0] = v.c[GET_NBYTE(step, 0, value)];
io->pos[1] = v.c[GET_NBYTE(step, 1, value)];
io->pos[2] = v.c[GET_NBYTE(step, 2, value)];
io->pos+=step;
return;
}
FREE_INLINE void io_write_bytes(pTHX_ struct io_struct* io, const char * const buffer, int len){
io_reserve(aTHX_ io, len);
Copy(buffer, io->pos, len, char);
io->pos+=len;
}
/* Date checking */
FREE_INLINE bool util_is_date(SV *one);
FREE_INLINE double util_date_time(SV *one);
STATIC_INLINE void amf0_format_one(pTHX_ struct io_struct *io, SV * one);
STATIC_INLINE void amf0_format_number(pTHX_ struct io_struct *io, SV * one);
STATIC_INLINE void amf0_format_string(pTHX_ struct io_struct *io, SV * one);
STATIC_INLINE void amf0_format_strict_array(pTHX_ struct io_struct *io, AV * one);
STATIC_INLINE void amf0_format_object(pTHX_ struct io_struct *io, HV * one);
STATIC_INLINE void amf0_format_null(pTHX_ struct io_struct *io);
STATIC_INLINE void amf0_format_typed_object(pTHX_ struct io_struct *io, HV * one);
FREE_INLINE bool util_is_date(SV *one){
if (SvNOKp(one)){
HV* stash = SvSTASH(one);
char *class_name = HvNAME(stash);
if (*class_name == '*' && class_name[1] == 0){
return 1;
}
else {
return 0;
}
}
else {
return 0;
}
}
FREE_INLINE double util_date_time(SV *one){
return (SvNVX(one)*1000);
}
FREE_INLINE void amf0_format_reference(pTHX_ struct io_struct * io, SV *ref_sv){
io_write_marker(aTHX_ io, MARKER0_REFERENCE);
io_write_u16(aTHX_ io, SvIV(ref_sv));
}
FREE_INLINE void amf0_format_scalar_ref(pTHX_ struct io_struct * io, SV *ref_sv){
const char *const reftype = "REFVAL";
io_write_marker(aTHX_ io, MARKER0_TYPED_OBJECT);
/* special type */
io_write_u16(aTHX_ io, 6);
io_write_bytes(aTHX_ io, reftype, 6);
/* type */
io_write_u16(aTHX_ io, 6);
io_write_bytes(aTHX_ io, reftype, 6);
amf0_format_one(aTHX_ io, ref_sv);
/* end marker */
io_write_u16(aTHX_ io, 0);
io_write_marker(aTHX_ io, MARKER0_OBJECT_END);
}
FREE_INLINE void amf0_format_one(pTHX_ struct io_struct *io, SV * one){
SV *rv = 0;
bool is_perl_bool = 0;
if (SvROK(one)){
rv = (SV*) SvRV(one);
if ( sv_isobject( one )){
HV* stash = SvSTASH(rv);
char *class_name = HvNAME(stash);
if ( class_name[0] == 'J' ){
if ( sv_isa(one, "JSON::PP::Boolean")){
is_perl_bool = 1;
}
else if ( sv_isa(one, "JSON::XS::Boolean") ){
is_perl_bool = 1;
}
}
else if ( class_name[0] == 'b' ){
if ( sv_isa(one, "boolean" )){
is_perl_bool = 1;
}
}
if ( is_perl_bool ){
io_write_marker(aTHX_ io, MARKER0_BOOLEAN );
/* TODO SvTRUE can call die or something like */
io_write_uchar(aTHX_ io, SvTRUE( SvRV( one )) ? 1 : 0);
return ;
}
}
}
if (rv){
/* test has stored */
SV **OK = hv_fetch(io->hv_object, (char *)(&rv), sizeof (rv), 1);
if (SvOK(*OK)) {
amf0_format_reference(aTHX_ io, *OK);
}
else {
int type = SvTYPE(rv);
sv_setiv(*OK, io->rc_object);
++io->rc_object;
if (sv_isobject(one)) {
if ( io->options & OPT_MAPPER ){
GV *to_amf = gv_fetchmethod_autoload (SvSTASH (rv), "TO_AMF", 0);
if ( to_amf ) {
dSP;
ENTER; SAVETMPS; PUSHMARK (SP);
XPUSHs (sv_bless (sv_2mortal (newRV_inc (rv)), SvSTASH (rv)));
/* calling with G_SCALAR ensures that we always get a 1 return value */
PUTBACK;
call_sv ((SV *)GvCV (to_amf), G_SCALAR);
SPAGAIN;
/* catch this surprisingly common error */
if (SvROK (TOPs) && SvRV (TOPs) == rv)
croak ("%s::TO_AMF method returned same object as was passed instead of a new one", HvNAME (SvSTASH (rv)));
rv = POPs;
PUTBACK;
amf0_format_one( aTHX_ io, rv);
FREETMPS; LEAVE;
return ;
}
}
if (SvTYPE(rv) == SVt_PVHV){
amf0_format_typed_object(aTHX_ io, (HV *) rv);
}
else if ( util_is_date( rv ) ) {
io_write_marker(aTHX_ io, MARKER0_DATE );
io_write_double(aTHX_ io, util_date_time( rv ));
io_write_s16(aTHX_ io, 0 );
}
else {
/* may be i has to format as undef */
if ( io->options & OPT_SKIP_BAD ){
io_write_marker( aTHX_ io, MARKER0_UNDEFINED );
}
else
io_register_error(io, ERR_BAD_OBJECT);
}
}
else if (SvTYPE(rv) == SVt_PVAV)
amf0_format_strict_array(aTHX_ io, (AV*) rv);
else if (SvTYPE(rv) == SVt_PVHV) {
io_write_marker(aTHX_ io, MARKER0_OBJECT);
amf0_format_object(aTHX_ io, (HV*) rv);
}
else if ( type != SVt_PVCV && type != SVt_PVGV ) {
amf0_format_scalar_ref(aTHX_ io, (SV*) rv);
}
else {
if ( io->options & OPT_SKIP_BAD )
io_write_marker( aTHX_ io, MARKER0_UNDEFINED );
else
io_register_error(io, ERR_BAD_OBJECT);
}
}
}
else {
if (SvOK(one)){
#if defined( EXPERIMENT1 )
if ( (io->options & OPT_PREFER_NUMBER )){
if (SvNIOK(one)){
amf0_format_number(aTHX_ io, one);
}
else {
amf0_format_string(aTHX_ io, one);
}
}
else
#endif
if (SvPOK(one)){
amf0_format_string(aTHX_ io, one);
}
else if ( SvNIOK(one) ){
amf0_format_number(aTHX_ io, one);
}
else {
if (io->options & OPT_SKIP_BAD ){
io_write_marker(aTHX_ io, MARKER0_UNDEFINED );
}
else
io_register_error( io, ERR_BAD_OBJECT);
}
}
else {
amf0_format_null(aTHX_ io);
}
}
}