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.21 by root, Thu Mar 29 02:45:49 2007 UTC vs.
Revision 1.31 by root, Wed May 9 16:33:53 2007 UTC

3#include "XSUB.h" 3#include "XSUB.h"
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#include "stdio.h"
9
10#if defined(__BORLANDC__) || defined(_MSC_VER)
11# define snprintf _snprintf // C compilers have this in stdio.h
12#endif
8 13
9#define F_ASCII 0x00000001UL 14#define F_ASCII 0x00000001UL
15#define F_LATIN1 0x00000002UL
10#define F_UTF8 0x00000002UL 16#define F_UTF8 0x00000004UL
11#define F_INDENT 0x00000004UL 17#define F_INDENT 0x00000008UL
12#define F_CANONICAL 0x00000008UL 18#define F_CANONICAL 0x00000010UL
13#define F_SPACE_BEFORE 0x00000010UL 19#define F_SPACE_BEFORE 0x00000020UL
14#define F_SPACE_AFTER 0x00000020UL 20#define F_SPACE_AFTER 0x00000040UL
15#define F_ALLOW_NONREF 0x00000080UL 21#define F_ALLOW_NONREF 0x00000100UL
16#define F_SHRINK 0x00000100UL 22#define F_SHRINK 0x00000200UL
17#define F_MAXDEPTH 0xf8000000UL 23#define F_MAXDEPTH 0xf8000000UL
18#define S_MAXDEPTH 27 24#define S_MAXDEPTH 27
19 25
20#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) 26#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
21 27
22// F_SELFCONVERT? <=> to_json/toJson 28// F_SELFCONVERT? <=> to_json/toJson
23// F_BLESSED? <=> { $__class__$ => } 29// F_BLESSED? <=> { $__class__$ => }
24 30
25#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 31#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
26#define F_DEFAULT (12UL << S_MAXDEPTH) 32#define F_DEFAULT (9UL << S_MAXDEPTH)
27 33
28#define INIT_SIZE 32 // initial scalar size to be allocated 34#define INIT_SIZE 32 // initial scalar size to be allocated
29#define INDENT_STEP 3 // spaces per indentation level 35#define INDENT_STEP 3 // spaces per indentation level
30 36
31#define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character
32#define SHORT_STRING_LEN 512 // special-case strings of up to this size 37#define SHORT_STRING_LEN 512 // special-case strings of up to this size
33 38
34#define SB do { 39#define SB do {
35#define SE } while (0) 40#define SE } while (0)
36 41
76 { 81 {
77 *clen = 2; 82 *clen = 2;
78 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); 83 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
79 } 84 }
80 else 85 else
86 {
87 *clen = (STRLEN)-1;
81 return (UV)-1; 88 return (UV)-1;
89 }
82} 90}
83 91
84///////////////////////////////////////////////////////////////////////////// 92/////////////////////////////////////////////////////////////////////////////
85// encoder 93// encoder
86 94
101 if (enc->cur + len >= enc->end) 109 if (enc->cur + len >= enc->end)
102 { 110 {
103 STRLEN cur = enc->cur - SvPVX (enc->sv); 111 STRLEN cur = enc->cur - SvPVX (enc->sv);
104 SvGROW (enc->sv, cur + len + 1); 112 SvGROW (enc->sv, cur + len + 1);
105 enc->cur = SvPVX (enc->sv) + cur; 113 enc->cur = SvPVX (enc->sv) + cur;
106 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); 114 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
107 } 115 }
108} 116}
109 117
110static void 118static void
111encode_ch (enc_t *enc, char ch) 119encode_ch (enc_t *enc, char ch)
173 } 181 }
174 182
175 if (uch > 0x10FFFFUL) 183 if (uch > 0x10FFFFUL)
176 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); 184 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
177 185
178 if (uch < 0x80 || enc->flags & F_ASCII) 186 if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF))
179 { 187 {
180 if (uch > 0xFFFFUL) 188 if (uch > 0xFFFFUL)
181 { 189 {
182 need (enc, len += 11); 190 need (enc, len += 11);
183 sprintf (enc->cur, "\\u%04x\\u%04x", 191 sprintf (enc->cur, "\\u%04x\\u%04x",
197 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 205 *enc->cur++ = hexdigit [(uch >> 0) & 15];
198 } 206 }
199 207
200 str += clen; 208 str += clen;
201 } 209 }
210 else if (enc->flags & F_LATIN1)
211 {
212 *enc->cur++ = uch;
213 str += clen;
214 }
202 else if (is_utf8) 215 else if (is_utf8)
203 { 216 {
204 need (enc, len += clen); 217 need (enc, len += clen);
205 do 218 do
206 { 219 {
208 } 221 }
209 while (--clen); 222 while (--clen);
210 } 223 }
211 else 224 else
212 { 225 {
213 need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed 226 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed
214 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 227 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0);
215 ++str; 228 ++str;
216 } 229 }
217 } 230 }
218 } 231 }
405 418
406 encode_nl (enc); 419 encode_nl (enc);
407 } 420 }
408 else 421 else
409 { 422 {
410 SV *sv;
411 HE *he = hv_iternext (hv); 423 HE *he = hv_iternext (hv);
412 424
413 for (;;) 425 for (;;)
414 { 426 {
415 encode_indent (enc); 427 encode_indent (enc);
430 442
431// encode objects, arrays and special \0=false and \1=true values. 443// encode objects, arrays and special \0=false and \1=true values.
432static void 444static void
433encode_rv (enc_t *enc, SV *sv) 445encode_rv (enc_t *enc, SV *sv)
434{ 446{
447 svtype svt;
448
435 SvGETMAGIC (sv); 449 SvGETMAGIC (sv);
436
437 svtype svt = SvTYPE (sv); 450 svt = SvTYPE (sv);
438 451
439 if (svt == SVt_PVHV) 452 if (svt == SVt_PVHV)
440 encode_hv (enc, (HV *)sv); 453 encode_hv (enc, (HV *)sv);
441 else if (svt == SVt_PVAV) 454 else if (svt == SVt_PVAV)
442 encode_av (enc, (AV *)sv); 455 encode_av (enc, (AV *)sv);
492} 505}
493 506
494static SV * 507static SV *
495encode_json (SV *scalar, U32 flags) 508encode_json (SV *scalar, U32 flags)
496{ 509{
510 enc_t enc;
511
497 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) 512 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar))
498 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 513 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
499 514
500 enc_t enc;
501 enc.flags = flags; 515 enc.flags = flags;
502 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 516 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
503 enc.cur = SvPVX (enc.sv); 517 enc.cur = SvPVX (enc.sv);
504 enc.end = SvEND (enc.sv); 518 enc.end = SvEND (enc.sv);
505 enc.indent = 0; 519 enc.indent = 0;
506 enc.maxdepth = DEC_DEPTH (flags); 520 enc.maxdepth = DEC_DEPTH (flags);
507 521
508 SvPOK_only (enc.sv); 522 SvPOK_only (enc.sv);
509 encode_sv (&enc, scalar); 523 encode_sv (&enc, scalar);
510 524
525 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
526 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
527
511 if (!(flags & (F_ASCII | F_UTF8))) 528 if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8)))
512 SvUTF8_on (enc.sv); 529 SvUTF8_on (enc.sv);
513
514 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
515 530
516 if (enc.flags & F_SHRINK) 531 if (enc.flags & F_SHRINK)
517 shrink (enc.sv); 532 shrink (enc.sv);
518 533
519 return enc.sv; 534 return enc.sv;
591 SV *sv = 0; 606 SV *sv = 0;
592 int utf8 = 0; 607 int utf8 = 0;
593 608
594 do 609 do
595 { 610 {
596 char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; 611 char buf [SHORT_STRING_LEN + UTF8_MAXBYTES];
597 char *cur = buf; 612 char *cur = buf;
598 613
599 do 614 do
600 { 615 {
601 unsigned char ch = *(unsigned char *)dec->cur++; 616 unsigned char ch = *(unsigned char *)dec->cur++;
667 } 682 }
668 else if (ch >= 0x20 && ch <= 0x7f) 683 else if (ch >= 0x20 && ch <= 0x7f)
669 *cur++ = ch; 684 *cur++ = ch;
670 else if (ch >= 0x80) 685 else if (ch >= 0x80)
671 { 686 {
687 STRLEN clen;
688 UV uch;
689
672 --dec->cur; 690 --dec->cur;
673 691
674 STRLEN clen;
675 UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); 692 uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen);
676 if (clen == (STRLEN)-1) 693 if (clen == (STRLEN)-1)
677 ERR ("malformed UTF-8 character in JSON string"); 694 ERR ("malformed UTF-8 character in JSON string");
678 695
679 do 696 do
680 {
681 *cur++ = *dec->cur++; 697 *cur++ = *dec->cur++;
682 }
683 while (--clen); 698 while (--clen);
684 699
685 utf8 = 1; 700 utf8 = 1;
686 } 701 }
687 else if (!ch)
688 ERR ("unexpected end of string while parsing json string");
689 else 702 else
703 {
704 --dec->cur;
705
706 if (!ch)
707 ERR ("unexpected end of string while parsing JSON string");
708 else
690 ERR ("invalid character encountered"); 709 ERR ("invalid character encountered while parsing JSON string");
691 710 }
692 } 711 }
693 while (cur < buf + SHORT_STRING_LEN); 712 while (cur < buf + SHORT_STRING_LEN);
694 713
714 {
695 STRLEN len = cur - buf; 715 STRLEN len = cur - buf;
696 716
697 if (sv) 717 if (sv)
698 { 718 {
699 SvGROW (sv, SvCUR (sv) + len + 1); 719 SvGROW (sv, SvCUR (sv) + len + 1);
700 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 720 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
701 SvCUR_set (sv, SvCUR (sv) + len); 721 SvCUR_set (sv, SvCUR (sv) + len);
702 } 722 }
703 else 723 else
704 sv = newSVpvn (buf, len); 724 sv = newSVpvn (buf, len);
725 }
705 } 726 }
706 while (*dec->cur != '"'); 727 while (*dec->cur != '"');
707 728
708 ++dec->cur; 729 ++dec->cur;
709 730
953 ERR ("'null' expected"); 974 ERR ("'null' expected");
954 975
955 break; 976 break;
956 977
957 default: 978 default:
958 ERR ("malformed json string, neither array, object, number, string or atom"); 979 ERR ("malformed JSON string, neither array, object, number, string or atom");
959 break; 980 break;
960 } 981 }
961 982
962fail: 983fail:
963 return 0; 984 return 0;
964} 985}
965 986
966static SV * 987static SV *
967decode_json (SV *string, U32 flags) 988decode_json (SV *string, U32 flags, UV *offset_return)
968{ 989{
990 dec_t dec;
991 UV offset;
969 SV *sv; 992 SV *sv;
993
994 SvGETMAGIC (string);
995 SvUPGRADE (string, SVt_PV);
970 996
971 if (flags & F_UTF8) 997 if (flags & F_UTF8)
972 sv_utf8_downgrade (string, 0); 998 sv_utf8_downgrade (string, 0);
973 else 999 else
974 sv_utf8_upgrade (string); 1000 sv_utf8_upgrade (string);
975 1001
976 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1002 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
977 1003
978 dec_t dec;
979 dec.flags = flags; 1004 dec.flags = flags;
980 dec.cur = SvPVX (string); 1005 dec.cur = SvPVX (string);
981 dec.end = SvEND (string); 1006 dec.end = SvEND (string);
982 dec.err = 0; 1007 dec.err = 0;
983 dec.depth = 0; 1008 dec.depth = 0;
984 dec.maxdepth = DEC_DEPTH (dec.flags); 1009 dec.maxdepth = DEC_DEPTH (dec.flags);
985 1010
986 *dec.end = 0; // this should basically be a nop, too, but make sure its there 1011 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
987 sv = decode_sv (&dec); 1012 sv = decode_sv (&dec);
988 1013
1014 if (offset_return || !sv)
1015 {
1016 offset = dec.flags & F_UTF8
1017 ? dec.cur - SvPVX (string)
1018 : utf8_distance (dec.cur, SvPVX (string));
1019
1020 if (offset_return)
1021 *offset_return = offset;
1022 }
1023 else
1024 {
1025 // check for trailing garbage
1026 decode_ws (&dec);
1027
1028 if (*dec.cur)
1029 {
1030 dec.err = "garbage after JSON object";
1031 SvREFCNT_dec (sv);
1032 sv = 0;
1033 }
1034 }
1035
989 if (!sv) 1036 if (!sv)
990 { 1037 {
991 IV offset = dec.flags & F_UTF8
992 ? dec.cur - SvPVX (string)
993 : utf8_distance (dec.cur, SvPVX (string));
994 SV *uni = sv_newmortal (); 1038 SV *uni = sv_newmortal ();
995 1039
996 // horrible hack to silence warning inside pv_uni_display 1040 // horrible hack to silence warning inside pv_uni_display
997 COP cop = *PL_curcop; 1041 COP cop = *PL_curcop;
998 cop.cop_warnings = pWARN_NONE; 1042 cop.cop_warnings = pWARN_NONE;
1000 SAVEVPTR (PL_curcop); 1044 SAVEVPTR (PL_curcop);
1001 PL_curcop = &cop; 1045 PL_curcop = &cop;
1002 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1046 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1003 LEAVE; 1047 LEAVE;
1004 1048
1005 croak ("%s, at character offset %d (%s)", 1049 croak ("%s, at character offset %d [\"%s\"]",
1006 dec.err, 1050 dec.err,
1007 (int)offset, 1051 (int)offset,
1008 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1052 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1009 } 1053 }
1010 1054
1046 RETVAL 1090 RETVAL
1047 1091
1048SV *ascii (SV *self, int enable = 1) 1092SV *ascii (SV *self, int enable = 1)
1049 ALIAS: 1093 ALIAS:
1050 ascii = F_ASCII 1094 ascii = F_ASCII
1095 latin1 = F_LATIN1
1051 utf8 = F_UTF8 1096 utf8 = F_UTF8
1052 indent = F_INDENT 1097 indent = F_INDENT
1053 canonical = F_CANONICAL 1098 canonical = F_CANONICAL
1054 space_before = F_SPACE_BEFORE 1099 space_before = F_SPACE_BEFORE
1055 space_after = F_SPACE_AFTER 1100 space_after = F_SPACE_AFTER
1067 RETVAL = newSVsv (self); 1112 RETVAL = newSVsv (self);
1068} 1113}
1069 OUTPUT: 1114 OUTPUT:
1070 RETVAL 1115 RETVAL
1071 1116
1072SV *max_depth (SV *self, int max_depth = 0x80000000UL) 1117SV *max_depth (SV *self, UV max_depth = 0x80000000UL)
1073 CODE: 1118 CODE:
1074{ 1119{
1075 UV *uv = SvJSON (self); 1120 UV *uv = SvJSON (self);
1076 UV log2 = 0; 1121 UV log2 = 0;
1077 1122
1091 PPCODE: 1136 PPCODE:
1092 XPUSHs (encode_json (scalar, *SvJSON (self))); 1137 XPUSHs (encode_json (scalar, *SvJSON (self)));
1093 1138
1094void decode (SV *self, SV *jsonstr) 1139void decode (SV *self, SV *jsonstr)
1095 PPCODE: 1140 PPCODE:
1096 XPUSHs (decode_json (jsonstr, *SvJSON (self))); 1141 XPUSHs (decode_json (jsonstr, *SvJSON (self), 0));
1142
1143void decode_prefix (SV *self, SV *jsonstr)
1144 PPCODE:
1145{
1146 UV offset;
1147 EXTEND (SP, 2);
1148 PUSHs (decode_json (jsonstr, *SvJSON (self), &offset));
1149 PUSHs (sv_2mortal (newSVuv (offset)));
1150}
1097 1151
1098PROTOTYPES: ENABLE 1152PROTOTYPES: ENABLE
1099 1153
1100void to_json (SV *scalar) 1154void to_json (SV *scalar)
1101 ALIAS: 1155 ALIAS:
1105 1159
1106void from_json (SV *jsonstr) 1160void from_json (SV *jsonstr)
1107 ALIAS: 1161 ALIAS:
1108 jsonToObj = 0 1162 jsonToObj = 0
1109 PPCODE: 1163 PPCODE:
1110 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); 1164 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0));
1111 1165

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines