forked from Grian/Storable-AMF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AMF.xs
3058 lines (2795 loc) · 85.5 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
#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"
#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 */
#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 EXPERIMENT1
#define AMF0_VERSION 0
#define AMF3_VERSION 3
#define STR_EMPTY '\x01'
#define TRACE(ELEM) PerlIO_printf( PerlIO_stderr(), ELEM);
#undef TRACE
#define TRACE(ELEM) ;
#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)
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;
int buffer_step_inc;
int arr_max;
Sigjmp_buf target_error;
int error_code;
AV *arr_string;
AV *arr_object;
AV *arr_trait;
HV *hv_string;
HV *hv_object;
HV *hv_trait;
int rc_string;
int rc_object;
int rc_trait;
int version;
int final_version;
int options;
struct io_amf_option* ext_option;
SV * (*parse_one_object)(pTHX_ struct io_struct * io);
char *subname;
char status;
bool reuse;
};
inline SV* get_tmp_storage( pTHX_ SV *option );
inline void destroy_tmp_storage( pTHX_ SV *self);
STATIC_INLINE SV * amf0_parse_one(pTHX_ struct io_struct * io);
STATIC_INLINE SV * amf3_parse_one(pTHX_ struct io_struct * io);
inline void io_in_destroy(pTHX_ struct io_struct * io, AV *);
inline void io_out_cleanup(pTHX_ struct io_struct * io);
inline void io_test_eof(pTHX_ struct io_struct *io);
inline void io_register_error(struct io_struct *io, int);
inline void io_register_error_and_free(pTHX_ struct io_struct *io, int, void *);
inline int
io_position(struct io_struct *io){
return io->pos-io->ptr;
}
inline void
io_set_position(struct io_struct *io, int pos){
io->pos = io->ptr + pos;
}
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);
}
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));
}
}
inline void
io_move_backward(struct io_struct *io, int step){
io->pos-= step;
}
inline void
io_move_forward(struct io_struct *io, int len){
io->pos+=len;
}
inline void
io_require(struct io_struct *io, int len){
if (io->end - io->pos < len){
io_register_error(io, ERR_EOF);
}
}
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;
}
io->ptr = (unsigned char *) SvGROW(io->sv_buffer, buf_len);
io->pos = io->ptr + ipos;
io->end = io->ptr + SvLEN(io->sv_buffer);
}
}
inline void io_register_error(struct io_struct *io, int errtype){
io->error_code = errtype;
Siglongjmp(io->target_error, errtype);
}
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);
}
}
}
inline void io_register_error_and_free(pTHX_ struct io_struct *io, int errtype, void *pointer){
if (pointer)
sv_2mortal((SV*) pointer);
Siglongjmp(io->target_error, errtype);
}
inline SV* get_tmp_storage(pTHX_ SV* option){
struct io_struct * io;
SV *sv ;
Newxz( io, 1, struct io_struct );
sv = sv_newmortal();
sv_setref_iv( sv, "Storable::AMF0::TemporaryStorage", PTR2IV( io ) );
io->arr_trait = newAV();
io->arr_string = newAV();
io->arr_object = newAV();
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->sv_buffer = newSV(0);
(void)SvUPGRADE(io->sv_buffer, SVt_PV);
SvPOK_on( io->sv_buffer );
SvGROW( io->sv_buffer, 512 ); */
if ( option ){
io->options = SvIV(option);
}
else {
io->options = DEFAULT_MASK;
}
return sv;
}
inline void destroy_tmp_storage( pTHX_ SV *self ){
if ( ! SvROK( self )) {
croak( "Bad Storable::AMF0::TemporaryStorage" );
}
else {
struct io_struct *io;
io = INT2PTR( struct io_struct*, SvIV( SvRV( self )) );
SvREFCNT_dec( (SV *) io->arr_trait );
SvREFCNT_dec( (SV *) io->arr_string );
SvREFCNT_dec( (SV *) io->arr_object );
SvREFCNT_dec( (SV *) io->hv_object );
SvREFCNT_dec( (SV *) io->hv_string );
SvREFCNT_dec( (SV *) io->hv_trait );
// SvREFCNT_dec( (SV *) io->sv_buffer );
Safefree( io );
/* fprintf( stderr, "Destroy\n"); */
}
}
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 );
};
}
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 );
};
}
inline void io_in_init(pTHX_ struct io_struct * io, SV* data, int amf_version, SV * sv_option){
struct io_struct *reuse_storage_ptr;
bool reuse_storage;
/* PerlInterpreter *my_perl = io->interpreter; */
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 = 1;
reuse_storage_ptr = INT2PTR( struct io_struct *, SvIV( SvRV( sv_option )));
io->options = reuse_storage_ptr->options;
}
}
else {
reuse_storage = 0;
io->options = SvIV(sv_option);
}
}
else {
io->options = DEFAULT_MASK;
reuse_storage = 0;
}
io->reuse = reuse_storage;
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 ( reuse_storage ){
io->arr_object = reuse_storage_ptr->arr_object;
if (amf_version == AMF3_VERSION) {
io->arr_string = reuse_storage_ptr->arr_string;
io->arr_trait = reuse_storage_ptr->arr_trait;
io->parse_one_object = amf3_parse_one;
}
else {
io->parse_one_object = amf0_parse_one;
}
}
else {
sv_2mortal( (SV *) (io->arr_object = newAV()) );
av_extend( io->arr_object, 32 );
if (amf_version == AMF3_VERSION) {
io->arr_string = newAV();
sv_2mortal((SV*) io->arr_string);
io->arr_trait = newAV();
sv_2mortal((SV*) io->arr_trait);
io->parse_one_object = amf3_parse_one;
}
else {
io->parse_one_object = amf0_parse_one;
}
}
}
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");
}
}
}
inline void io_out_init(pTHX_ struct io_struct *io, SV*sv_option, int amf_version){
SV *sbuffer;
unsigned int ibuf_size ;
unsigned int ibuf_step ;
io->version = amf_version;
if ( sv_option ){
if ( SvROK(sv_option) && sv_isobject( sv_option )){
struct io_struct *reuse_storage_ptr;
reuse_storage_ptr = INT2PTR( struct io_struct *, SvIV( SvRV( sv_option )));
io->options = reuse_storage_ptr->options;
io->reuse = 1;
io->rc_string = 0;
io->rc_trait = 0;
io->rc_object = 0;
/*io->sv_buffer = reuse_storage_ptr->sv_buffer; */
io->hv_string = reuse_storage_ptr->hv_string;
io->hv_object = reuse_storage_ptr->hv_object;
io->hv_trait = reuse_storage_ptr->hv_trait;
ibuf_size = 10240;
ibuf_step = 20480;
if ( io->options & OPT_TARG ){
dXSTARG;
io->sv_buffer = sbuffer = TARG;
(void)SvUPGRADE(sbuffer, SVt_PV);
SvPOK_on(sbuffer);
SvGROW( sbuffer, 512 );
}
else {
sbuffer = sv_2mortal(newSVpvn("",0));
SvGROW(sbuffer, ibuf_size);
io->sv_buffer = sbuffer;
}
io->buffer_step_inc = 1024;
io->ptr = (unsigned char *) SvPV_nolen(io->sv_buffer);
io->pos = io->ptr;
io->end = (unsigned char *) SvEND(io->sv_buffer);
io->status = 'w';
return ;
}
else if ( !SvIOK(sv_option)){
io_register_error( io, ERR_BAD_OPTION );
}
else {
io->options = SvIV( sv_option );
}
}
else {
io->options = DEFAULT_MASK;
};
io->reuse = 0;
/* FIXME */
ibuf_size = 10240;
ibuf_step = 20480;
if ( io->options & OPT_TARG ){
dXSTARG;
io->sv_buffer = sbuffer = TARG;
(void)SvUPGRADE(sbuffer, SVt_PV);
SvPOK_on(sbuffer);
SvGROW( sbuffer, 512 );
}
else {
sbuffer = sv_2mortal(newSVpvn("",0));
SvGROW(sbuffer, ibuf_size);
io->sv_buffer = sbuffer;
}
if (amf_version) {
io->hv_string = newHV();
io->hv_trait = newHV();
io->hv_object = newHV();
io->rc_string = 0;
io->rc_trait = 0;
io->rc_object = 0;
HvSHAREKEYS_off( io->hv_object );
HvSHAREKEYS_off( io->hv_trait );
HvSHAREKEYS_off( io->hv_string );
sv_2mortal((SV *)io->hv_string);
sv_2mortal((SV *)io->hv_object);
sv_2mortal((SV *)io->hv_trait);
}
else {
io->hv_object = newHV();
io->rc_object = 0;
HvSHAREKEYS_off( io->hv_object );
sv_2mortal((SV*)io->hv_object);
}
io->buffer_step_inc = ibuf_step;
io->ptr = (unsigned char *) SvPV_nolen(io->sv_buffer);
io->pos = io->ptr;
io->end = (unsigned char *) SvEND(io->sv_buffer);
io->status = 'w';
}
inline SV * io_buffer(struct io_struct *io){
SvCUR_set(io->sv_buffer, io->pos - io->ptr);
return io->sv_buffer;
}
inline double io_read_double(struct io_struct *io);
inline unsigned char io_read_marker(struct io_struct * io);
inline int io_read_u8(struct io_struct * io);
inline int io_read_u16(struct io_struct * io);
inline int io_read_u32(struct io_struct * io);
inline int io_read_u24(struct io_struct * io);
#define MOVERFLOW(VALUE, MAXVALUE, PROC)\
if (VALUE > MAXVALUE) { \
PerlIO_printf( PerlIO_stderr(), "Overflow in %s. expected less %d. got %d\n", PROC, MAXVALUE, VALUE); \
io_register_error(io, ERR_OVERFLOW); \
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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 */
inline bool util_is_date(SV *one);
inline double util_date_time(SV *one);
inline void amf0_format_one(pTHX_ struct io_struct *io, SV * one);
inline void amf0_format_number(pTHX_ struct io_struct *io, SV * one);
inline void amf0_format_string(pTHX_ struct io_struct *io, SV * one);
inline void amf0_format_strict_array(pTHX_ struct io_struct *io, AV * one);
inline void amf0_format_object(pTHX_ struct io_struct *io, HV * one);
inline void amf0_format_null(pTHX_ struct io_struct *io);
inline void amf0_format_typed_object(pTHX_ struct io_struct *io, HV * one);
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;
}
}
inline double util_date_time(SV *one){
return (SvNVX(one)*1000);
}
inline void amf0_format_reference(pTHX_ struct io_struct * io, SV *ref){
io_write_marker(aTHX_ io, MARKER0_REFERENCE);
io_write_u16(aTHX_ io, SvIV(ref));
}
inline void amf0_format_scalar_ref(pTHX_ struct io_struct * io, SV *ref){
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);
/* end marker */
io_write_u16(aTHX_ io, 0);
io_write_marker(aTHX_ io, MARKER0_OBJECT_END);
}
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);
}
}
}
inline void amf0_format_number(pTHX_ struct io_struct *io, SV * one){
io_write_marker(aTHX_ io, MARKER0_NUMBER);
io_write_double(aTHX_ io, SvNV(one));
}
inline void amf0_format_string(pTHX_ struct io_struct *io, SV * one){
/* TODO: process long string */
if (SvPOK(one)){
STRLEN str_len;
char * pv;
pv = SvPV(one, str_len);
if (str_len > 65500){
io_write_marker(aTHX_ io, MARKER0_LONG_STRING);
io_write_u32(aTHX_ io, str_len);
io_write_bytes(aTHX_ io, pv, str_len);
}
else {
io_write_marker(aTHX_ io, MARKER0_STRING);
io_write_u16(aTHX_ io, SvCUR(one));
io_write_bytes(aTHX_ io, SvPV_nolen(one), SvCUR(one));
}
}else{
amf0_format_null(aTHX_ io);
}
}
inline void amf0_format_strict_array(pTHX_ struct io_struct *io, AV * one){
int i, len;
AV * one_array;
one_array = one;
len = av_len(one_array);
io_write_marker(aTHX_ io, '\012');
io_write_u32(aTHX_ io, len + 1);
for(i = 0; i <= len; ++i){
SV ** ref_value = av_fetch(one_array, i, 0);
if (ref_value) {
amf0_format_one(aTHX_ io, *ref_value);
}
else {
amf0_format_null(aTHX_ io);
}
}
}
inline void amf0_format_object(pTHX_ struct io_struct *io, HV * one){
I32 key_len;
SV * value;