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.15 by root, Sun Mar 25 02:32:40 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
19
20#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
21
22// F_SELFCONVERT? <=> to_json/toJson
23// F_BLESSED? <=> { $__class__$ => }
17 24
18#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 25#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
19#define F_DEFAULT 0 26#define F_DEFAULT (13UL << S_MAXDEPTH)
20 27
21#define INIT_SIZE 32 // initial scalar size to be allocated 28#define INIT_SIZE 32 // initial scalar size to be allocated
22#define INDENT_STEP 3 // spaces per indentation level 29#define INDENT_STEP 3 // spaces per indentation level
23 30
24#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
81typedef struct 88typedef struct
82{ 89{
83 char *cur; // SvPVX (sv) + current output position 90 char *cur; // SvPVX (sv) + current output position
84 char *end; // SvEND (sv) 91 char *end; // SvEND (sv)
85 SV *sv; // result scalar 92 SV *sv; // result scalar
86 UV flags; // F_* 93 U32 flags; // F_*
87 int indent; // indentation level 94 U32 indent; // indentation level
88 int max_depth; // max. recursion level 95 U32 maxdepth; // max. indentation/recursion level
89} enc_t; 96} enc_t;
90 97
91static void 98static void
92need (enc_t *enc, STRLEN len) 99need (enc_t *enc, STRLEN len)
93{ 100{
444 } 451 }
445 else if (SvROK (sv)) 452 else if (SvROK (sv))
446 { 453 {
447 SV *rv = SvRV (sv); 454 SV *rv = SvRV (sv);
448 455
449 if (enc->indent >= enc->max_depth) 456 if (enc->indent >= enc->maxdepth)
450 croak ("data structure too deep (hit recursion limit)"); 457 croak ("data structure too deep (hit recursion limit)");
451 458
452 switch (SvTYPE (rv)) 459 switch (SvTYPE (rv))
453 { 460 {
454 case SVt_PVAV: encode_av (enc, (AV *)rv); break; 461 case SVt_PVAV: encode_av (enc, (AV *)rv); break;
465 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",
466 SvPV_nolen (sv), SvFLAGS (sv)); 473 SvPV_nolen (sv), SvFLAGS (sv));
467} 474}
468 475
469static SV * 476static SV *
470encode_json (SV *scalar, UV flags) 477encode_json (SV *scalar, U32 flags)
471{ 478{
472 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) 479 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar))
473 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)");
474 481
475 enc_t enc; 482 enc_t enc;
476 enc.flags = flags; 483 enc.flags = flags;
477 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 484 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
478 enc.cur = SvPVX (enc.sv); 485 enc.cur = SvPVX (enc.sv);
479 enc.end = SvEND (enc.sv); 486 enc.end = SvEND (enc.sv);
480 enc.indent = 0; 487 enc.indent = 0;
481 enc.max_depth = 0x7fffffffUL; 488 enc.maxdepth = DEC_DEPTH (flags);
482 489
483 SvPOK_only (enc.sv); 490 SvPOK_only (enc.sv);
484 encode_sv (&enc, scalar); 491 encode_sv (&enc, scalar);
485 492
486 if (!(flags & (F_ASCII | F_UTF8))) 493 if (!(flags & (F_ASCII | F_UTF8)))
501typedef struct 508typedef struct
502{ 509{
503 char *cur; // current parser pointer 510 char *cur; // current parser pointer
504 char *end; // end of input string 511 char *end; // end of input string
505 const char *err; // parse error, if != 0 512 const char *err; // parse error, if != 0
506 UV flags; // F_* 513 U32 flags; // F_*
514 U32 depth; // recursion depth
515 U32 maxdepth; // recursion depth limit
507} dec_t; 516} dec_t;
508 517
509static void 518static void
510decode_ws (dec_t *dec) 519decode_ws (dec_t *dec)
511{ 520{
520 ++dec->cur; 529 ++dec->cur;
521 } 530 }
522} 531}
523 532
524#define ERR(reason) SB dec->err = reason; goto fail; SE 533#define ERR(reason) SB dec->err = reason; goto fail; SE
534
525#define EXPECT_CH(ch) SB \ 535#define EXPECT_CH(ch) SB \
526 if (*dec->cur != ch) \ 536 if (*dec->cur != ch) \
527 ERR (# ch " expected"); \ 537 ERR (# ch " expected"); \
528 ++dec->cur; \ 538 ++dec->cur; \
529 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
530 543
531static SV *decode_sv (dec_t *dec); 544static SV *decode_sv (dec_t *dec);
532 545
533static signed char decode_hexdigit[256]; 546static signed char decode_hexdigit[256];
534 547
778static SV * 791static SV *
779decode_av (dec_t *dec) 792decode_av (dec_t *dec)
780{ 793{
781 AV *av = newAV (); 794 AV *av = newAV ();
782 795
796 DEC_INC_DEPTH;
783 decode_ws (dec); 797 decode_ws (dec);
798
784 if (*dec->cur == ']') 799 if (*dec->cur == ']')
785 ++dec->cur; 800 ++dec->cur;
786 else 801 else
787 for (;;) 802 for (;;)
788 { 803 {
806 ERR (", or ] expected while parsing array"); 821 ERR (", or ] expected while parsing array");
807 822
808 ++dec->cur; 823 ++dec->cur;
809 } 824 }
810 825
826 DEC_DEC_DEPTH;
811 return newRV_noinc ((SV *)av); 827 return newRV_noinc ((SV *)av);
812 828
813fail: 829fail:
814 SvREFCNT_dec (av); 830 SvREFCNT_dec (av);
831 DEC_DEC_DEPTH;
815 return 0; 832 return 0;
816} 833}
817 834
818static SV * 835static SV *
819decode_hv (dec_t *dec) 836decode_hv (dec_t *dec)
820{ 837{
821 HV *hv = newHV (); 838 HV *hv = newHV ();
822 839
840 DEC_INC_DEPTH;
823 decode_ws (dec); 841 decode_ws (dec);
842
824 if (*dec->cur == '}') 843 if (*dec->cur == '}')
825 ++dec->cur; 844 ++dec->cur;
826 else 845 else
827 for (;;) 846 for (;;)
828 { 847 {
841 { 860 {
842 SvREFCNT_dec (key); 861 SvREFCNT_dec (key);
843 goto fail; 862 goto fail;
844 } 863 }
845 864
846 //TODO: optimise
847 hv_store_ent (hv, key, value, 0); 865 hv_store_ent (hv, key, value, 0);
866 SvREFCNT_dec (key);
848 867
849 decode_ws (dec); 868 decode_ws (dec);
850 869
851 if (*dec->cur == '}') 870 if (*dec->cur == '}')
852 { 871 {
858 ERR (", or } expected while parsing object/hash"); 877 ERR (", or } expected while parsing object/hash");
859 878
860 ++dec->cur; 879 ++dec->cur;
861 } 880 }
862 881
882 DEC_DEC_DEPTH;
863 return newRV_noinc ((SV *)hv); 883 return newRV_noinc ((SV *)hv);
864 884
865fail: 885fail:
866 SvREFCNT_dec (hv); 886 SvREFCNT_dec (hv);
887 DEC_DEC_DEPTH;
867 return 0; 888 return 0;
868} 889}
869 890
870static SV * 891static SV *
871decode_sv (dec_t *dec) 892decode_sv (dec_t *dec)
923fail: 944fail:
924 return 0; 945 return 0;
925} 946}
926 947
927static SV * 948static SV *
928decode_json (SV *string, UV flags) 949decode_json (SV *string, U32 flags)
929{ 950{
930 SV *sv; 951 SV *sv;
931 952
932 if (flags & F_UTF8) 953 if (flags & F_UTF8)
933 sv_utf8_downgrade (string, 0); 954 sv_utf8_downgrade (string, 0);
935 sv_utf8_upgrade (string); 956 sv_utf8_upgrade (string);
936 957
937 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 958 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
938 959
939 dec_t dec; 960 dec_t dec;
940 dec.flags = flags; 961 dec.flags = flags;
941 dec.cur = SvPVX (string); 962 dec.cur = SvPVX (string);
942 dec.end = SvEND (string); 963 dec.end = SvEND (string);
943 dec.err = 0; 964 dec.err = 0;
965 dec.depth = 0;
966 dec.maxdepth = DEC_DEPTH (dec.flags);
944 967
968 *SvEND (sv) = 0; // this shou[ld basically be a nop, too
945 sv = decode_sv (&dec); 969 sv = decode_sv (&dec);
946 970
947 if (!sv) 971 if (!sv)
948 { 972 {
949 IV offset = dec.flags & F_UTF8 973 IV offset = dec.flags & F_UTF8
982BOOT: 1006BOOT:
983{ 1007{
984 int i; 1008 int i;
985 1009
986 memset (decode_hexdigit, 0xff, 256); 1010 memset (decode_hexdigit, 0xff, 256);
1011
987 for (i = 10; i--; ) 1012 for (i = 0; i < 256; ++i)
988 decode_hexdigit ['0' + i] = i; 1013 decode_hexdigit [i] =
989 1014 i >= '0' && i <= '9' ? i - '0'
990 for (i = 7; i--; ) 1015 : i >= 'a' && i <= 'f' ? i - 'a' + 10
991 { 1016 : i >= 'A' && i <= 'F' ? i - 'A' + 10
992 decode_hexdigit ['a' + i] = 10 + i; 1017 : -1;
993 decode_hexdigit ['A' + i] = 10 + i;
994 }
995 1018
996 json_stash = gv_stashpv ("JSON::XS", 1); 1019 json_stash = gv_stashpv ("JSON::XS", 1);
997} 1020}
998 1021
999PROTOTYPES: DISABLE 1022PROTOTYPES: DISABLE
1026 RETVAL = newSVsv (self); 1049 RETVAL = newSVsv (self);
1027} 1050}
1028 OUTPUT: 1051 OUTPUT:
1029 RETVAL 1052 RETVAL
1030 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
1031void encode (SV *self, SV *scalar) 1072void encode (SV *self, SV *scalar)
1032 PPCODE: 1073 PPCODE:
1033 XPUSHs (encode_json (scalar, *SvJSON (self))); 1074 XPUSHs (encode_json (scalar, *SvJSON (self)));
1034 1075
1035void decode (SV *self, SV *jsonstr) 1076void decode (SV *self, SV *jsonstr)
1040 1081
1041void to_json (SV *scalar) 1082void to_json (SV *scalar)
1042 ALIAS: 1083 ALIAS:
1043 objToJson = 0 1084 objToJson = 0
1044 PPCODE: 1085 PPCODE:
1045 XPUSHs (encode_json (scalar, F_UTF8)); 1086 XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8));
1046 1087
1047void from_json (SV *jsonstr) 1088void from_json (SV *jsonstr)
1048 ALIAS: 1089 ALIAS:
1049 jsonToObj = 0 1090 jsonToObj = 0
1050 PPCODE: 1091 PPCODE:
1051 XPUSHs (decode_json (jsonstr, F_UTF8)); 1092 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8));
1052 1093

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines