ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
(Generate patch)

Comparing JSON-XS/XS.xs (file contents):
Revision 1.17 by root, Sun Mar 25 02:46:41 2007 UTC vs.
Revision 1.18 by root, Sun Mar 25 21:19:13 2007 UTC

4 4
5#include "assert.h" 5#include "assert.h"
6#include "string.h" 6#include "string.h"
7#include "stdlib.h" 7#include "stdlib.h"
8 8
9#define F_ASCII 0x00000001 9#define F_ASCII 0x00000001UL
10#define F_UTF8 0x00000002 10#define F_UTF8 0x00000002UL
11#define F_INDENT 0x00000004 11#define F_INDENT 0x00000004UL
12#define F_CANONICAL 0x00000008 12#define F_CANONICAL 0x00000008UL
13#define F_SPACE_BEFORE 0x00000010 13#define F_SPACE_BEFORE 0x00000010UL
14#define F_SPACE_AFTER 0x00000020 14#define F_SPACE_AFTER 0x00000020UL
15#define F_ALLOW_NONREF 0x00000080 15#define F_ALLOW_NONREF 0x00000080UL
16#define F_SHRINK 0x00000100 16#define F_SHRINK 0x00000100UL
17#define F_MAXDEPTH 0xf8000000UL
18#define S_MAXDEPTH 27
17 19
18// F_SKIPINVALID? 20#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
19// F_EXECCODEREF? 21
20// F_SELFCONVERT? <=> { &__class__ => } 22// F_SELFCONVERT? <=> to_json/toJson
23// F_BLESSED? <=> { $__class__$ => }
21 24
22#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 25#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
23#define F_DEFAULT 0 26#define F_DEFAULT (13UL << S_MAXDEPTH)
24 27
25#define INIT_SIZE 32 // initial scalar size to be allocated 28#define INIT_SIZE 32 // initial scalar size to be allocated
26#define INDENT_STEP 3 // spaces per indentation level 29#define INDENT_STEP 3 // spaces per indentation level
27 30
28#define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character 31#define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character
85typedef struct 88typedef struct
86{ 89{
87 char *cur; // SvPVX (sv) + current output position 90 char *cur; // SvPVX (sv) + current output position
88 char *end; // SvEND (sv) 91 char *end; // SvEND (sv)
89 SV *sv; // result scalar 92 SV *sv; // result scalar
90 UV flags; // F_* 93 U32 flags; // F_*
91 int indent; // indentation level 94 U32 indent; // indentation level
92 int max_depth; // max. recursion level 95 U32 maxdepth; // max. indentation/recursion level
93} enc_t; 96} enc_t;
94 97
95static void 98static void
96need (enc_t *enc, STRLEN len) 99need (enc_t *enc, STRLEN len)
97{ 100{
448 } 451 }
449 else if (SvROK (sv)) 452 else if (SvROK (sv))
450 { 453 {
451 SV *rv = SvRV (sv); 454 SV *rv = SvRV (sv);
452 455
453 if (enc->indent >= enc->max_depth) 456 if (enc->indent >= enc->maxdepth)
454 croak ("data structure too deep (hit recursion limit)"); 457 croak ("data structure too deep (hit recursion limit)");
455 458
456 switch (SvTYPE (rv)) 459 switch (SvTYPE (rv))
457 { 460 {
458 case SVt_PVAV: encode_av (enc, (AV *)rv); break; 461 case SVt_PVAV: encode_av (enc, (AV *)rv); break;
469 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 472 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this",
470 SvPV_nolen (sv), SvFLAGS (sv)); 473 SvPV_nolen (sv), SvFLAGS (sv));
471} 474}
472 475
473static SV * 476static SV *
474encode_json (SV *scalar, UV flags) 477encode_json (SV *scalar, U32 flags)
475{ 478{
476 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) 479 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar))
477 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 480 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
478 481
479 enc_t enc; 482 enc_t enc;
480 enc.flags = flags; 483 enc.flags = flags;
481 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 484 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
482 enc.cur = SvPVX (enc.sv); 485 enc.cur = SvPVX (enc.sv);
483 enc.end = SvEND (enc.sv); 486 enc.end = SvEND (enc.sv);
484 enc.indent = 0; 487 enc.indent = 0;
485 enc.max_depth = 0x7fffffffUL; 488 enc.maxdepth = DEC_DEPTH (flags);
486 489
487 SvPOK_only (enc.sv); 490 SvPOK_only (enc.sv);
488 encode_sv (&enc, scalar); 491 encode_sv (&enc, scalar);
489 492
490 if (!(flags & (F_ASCII | F_UTF8))) 493 if (!(flags & (F_ASCII | F_UTF8)))
505typedef struct 508typedef struct
506{ 509{
507 char *cur; // current parser pointer 510 char *cur; // current parser pointer
508 char *end; // end of input string 511 char *end; // end of input string
509 const char *err; // parse error, if != 0 512 const char *err; // parse error, if != 0
510 UV flags; // F_* 513 U32 flags; // F_*
514 U32 depth; // recursion depth
515 U32 maxdepth; // recursion depth limit
511} dec_t; 516} dec_t;
512 517
513static void 518static void
514decode_ws (dec_t *dec) 519decode_ws (dec_t *dec)
515{ 520{
524 ++dec->cur; 529 ++dec->cur;
525 } 530 }
526} 531}
527 532
528#define ERR(reason) SB dec->err = reason; goto fail; SE 533#define ERR(reason) SB dec->err = reason; goto fail; SE
534
529#define EXPECT_CH(ch) SB \ 535#define EXPECT_CH(ch) SB \
530 if (*dec->cur != ch) \ 536 if (*dec->cur != ch) \
531 ERR (# ch " expected"); \ 537 ERR (# ch " expected"); \
532 ++dec->cur; \ 538 ++dec->cur; \
533 SE 539 SE
540
541#define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)")
542#define DEC_DEC_DEPTH --dec->depth
534 543
535static SV *decode_sv (dec_t *dec); 544static SV *decode_sv (dec_t *dec);
536 545
537static signed char decode_hexdigit[256]; 546static signed char decode_hexdigit[256];
538 547
782static SV * 791static SV *
783decode_av (dec_t *dec) 792decode_av (dec_t *dec)
784{ 793{
785 AV *av = newAV (); 794 AV *av = newAV ();
786 795
796 DEC_INC_DEPTH;
787 decode_ws (dec); 797 decode_ws (dec);
798
788 if (*dec->cur == ']') 799 if (*dec->cur == ']')
789 ++dec->cur; 800 ++dec->cur;
790 else 801 else
791 for (;;) 802 for (;;)
792 { 803 {
810 ERR (", or ] expected while parsing array"); 821 ERR (", or ] expected while parsing array");
811 822
812 ++dec->cur; 823 ++dec->cur;
813 } 824 }
814 825
826 DEC_DEC_DEPTH;
815 return newRV_noinc ((SV *)av); 827 return newRV_noinc ((SV *)av);
816 828
817fail: 829fail:
818 SvREFCNT_dec (av); 830 SvREFCNT_dec (av);
831 DEC_DEC_DEPTH;
819 return 0; 832 return 0;
820} 833}
821 834
822static SV * 835static SV *
823decode_hv (dec_t *dec) 836decode_hv (dec_t *dec)
824{ 837{
825 HV *hv = newHV (); 838 HV *hv = newHV ();
826 839
840 DEC_INC_DEPTH;
827 decode_ws (dec); 841 decode_ws (dec);
842
828 if (*dec->cur == '}') 843 if (*dec->cur == '}')
829 ++dec->cur; 844 ++dec->cur;
830 else 845 else
831 for (;;) 846 for (;;)
832 { 847 {
845 { 860 {
846 SvREFCNT_dec (key); 861 SvREFCNT_dec (key);
847 goto fail; 862 goto fail;
848 } 863 }
849 864
850 //TODO: optimise
851 hv_store_ent (hv, key, value, 0); 865 hv_store_ent (hv, key, value, 0);
866 SvREFCNT_dec (key);
852 867
853 decode_ws (dec); 868 decode_ws (dec);
854 869
855 if (*dec->cur == '}') 870 if (*dec->cur == '}')
856 { 871 {
862 ERR (", or } expected while parsing object/hash"); 877 ERR (", or } expected while parsing object/hash");
863 878
864 ++dec->cur; 879 ++dec->cur;
865 } 880 }
866 881
882 DEC_DEC_DEPTH;
867 return newRV_noinc ((SV *)hv); 883 return newRV_noinc ((SV *)hv);
868 884
869fail: 885fail:
870 SvREFCNT_dec (hv); 886 SvREFCNT_dec (hv);
887 DEC_DEC_DEPTH;
871 return 0; 888 return 0;
872} 889}
873 890
874static SV * 891static SV *
875decode_sv (dec_t *dec) 892decode_sv (dec_t *dec)
927fail: 944fail:
928 return 0; 945 return 0;
929} 946}
930 947
931static SV * 948static SV *
932decode_json (SV *string, UV flags) 949decode_json (SV *string, U32 flags)
933{ 950{
934 SV *sv; 951 SV *sv;
935 952
936 if (flags & F_UTF8) 953 if (flags & F_UTF8)
937 sv_utf8_downgrade (string, 0); 954 sv_utf8_downgrade (string, 0);
939 sv_utf8_upgrade (string); 956 sv_utf8_upgrade (string);
940 957
941 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 958 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
942 959
943 dec_t dec; 960 dec_t dec;
944 dec.flags = flags; 961 dec.flags = flags;
945 dec.cur = SvPVX (string); 962 dec.cur = SvPVX (string);
946 dec.end = SvEND (string); 963 dec.end = SvEND (string);
947 dec.err = 0; 964 dec.err = 0;
965 dec.depth = 0;
966 dec.maxdepth = DEC_DEPTH (dec.flags);
948 967
968 *SvEND (sv) = 0; // this shou[ld basically be a nop, too
949 sv = decode_sv (&dec); 969 sv = decode_sv (&dec);
950 970
951 if (!sv) 971 if (!sv)
952 { 972 {
953 IV offset = dec.flags & F_UTF8 973 IV offset = dec.flags & F_UTF8
986BOOT: 1006BOOT:
987{ 1007{
988 int i; 1008 int i;
989 1009
990 memset (decode_hexdigit, 0xff, 256); 1010 memset (decode_hexdigit, 0xff, 256);
1011
991 for (i = 10; i--; ) 1012 for (i = 0; i < 256; ++i)
992 decode_hexdigit ['0' + i] = i; 1013 decode_hexdigit [i] =
993 1014 i >= '0' && i <= '9' ? i - '0'
994 for (i = 7; i--; ) 1015 : i >= 'a' && i <= 'f' ? i - 'a' + 10
995 { 1016 : i >= 'A' && i <= 'F' ? i - 'A' + 10
996 decode_hexdigit ['a' + i] = 10 + i; 1017 : -1;
997 decode_hexdigit ['A' + i] = 10 + i;
998 }
999 1018
1000 json_stash = gv_stashpv ("JSON::XS", 1); 1019 json_stash = gv_stashpv ("JSON::XS", 1);
1001} 1020}
1002 1021
1003PROTOTYPES: DISABLE 1022PROTOTYPES: DISABLE
1030 RETVAL = newSVsv (self); 1049 RETVAL = newSVsv (self);
1031} 1050}
1032 OUTPUT: 1051 OUTPUT:
1033 RETVAL 1052 RETVAL
1034 1053
1054SV *max_depth (SV *self, int max_depth = 0x80000000UL)
1055 CODE:
1056{
1057 UV *uv = SvJSON (self);
1058 UV log2 = 0;
1059
1060 if (max_depth > 0x80000000UL) max_depth = 0x80000000UL;
1061
1062 while ((1UL << log2) < max_depth)
1063 ++log2;
1064
1065 *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1066
1067 RETVAL = newSVsv (self);
1068}
1069 OUTPUT:
1070 RETVAL
1071
1035void encode (SV *self, SV *scalar) 1072void encode (SV *self, SV *scalar)
1036 PPCODE: 1073 PPCODE:
1037 XPUSHs (encode_json (scalar, *SvJSON (self))); 1074 XPUSHs (encode_json (scalar, *SvJSON (self)));
1038 1075
1039void decode (SV *self, SV *jsonstr) 1076void decode (SV *self, SV *jsonstr)
1044 1081
1045void to_json (SV *scalar) 1082void to_json (SV *scalar)
1046 ALIAS: 1083 ALIAS:
1047 objToJson = 0 1084 objToJson = 0
1048 PPCODE: 1085 PPCODE:
1049 XPUSHs (encode_json (scalar, F_UTF8)); 1086 XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8));
1050 1087
1051void from_json (SV *jsonstr) 1088void from_json (SV *jsonstr)
1052 ALIAS: 1089 ALIAS:
1053 jsonToObj = 0 1090 jsonToObj = 0
1054 PPCODE: 1091 PPCODE:
1055 XPUSHs (decode_json (jsonstr, F_UTF8)); 1092 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8));
1056 1093

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines