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.44 by root, Mon Jun 25 04:08:17 2007 UTC vs.
Revision 1.47 by root, Sun Jul 1 14:08:03 2007 UTC

28#define F_SHRINK 0x00000200UL 28#define F_SHRINK 0x00000200UL
29#define F_ALLOW_BLESSED 0x00000400UL 29#define F_ALLOW_BLESSED 0x00000400UL
30#define F_CONV_BLESSED 0x00000800UL // NYI 30#define F_CONV_BLESSED 0x00000800UL // NYI
31#define F_MAXDEPTH 0xf8000000UL 31#define F_MAXDEPTH 0xf8000000UL
32#define S_MAXDEPTH 27 32#define S_MAXDEPTH 27
33#define F_MAXSIZE 0x01f00000UL
34#define S_MAXSIZE 20
33 35
34#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) 36#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
37#define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE ))
35 38
36#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 39#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
37#define F_DEFAULT (9UL << S_MAXDEPTH) 40#define F_DEFAULT (9UL << S_MAXDEPTH)
38 41
39#define INIT_SIZE 32 // initial scalar size to be allocated 42#define INIT_SIZE 32 // initial scalar size to be allocated
56#define expect_true(expr) expect ((expr) != 0, 1) 59#define expect_true(expr) expect ((expr) != 0, 1)
57 60
58static HV *json_stash, *json_boolean_stash; // JSON::XS:: 61static HV *json_stash, *json_boolean_stash; // JSON::XS::
59static SV *json_true, *json_false; 62static SV *json_true, *json_false;
60 63
64typedef struct json {
65 U32 flags;
66} JSON__XS;
67
61///////////////////////////////////////////////////////////////////////////// 68/////////////////////////////////////////////////////////////////////////////
62// utility functions 69// utility functions
63 70
64static UV * 71static UV *
65SvJSON (SV *sv) 72SvJSON (SV *sv)
113typedef struct 120typedef struct
114{ 121{
115 char *cur; // SvPVX (sv) + current output position 122 char *cur; // SvPVX (sv) + current output position
116 char *end; // SvEND (sv) 123 char *end; // SvEND (sv)
117 SV *sv; // result scalar 124 SV *sv; // result scalar
118 U32 flags; // F_* 125 struct json json;
119 U32 indent; // indentation level 126 U32 indent; // indentation level
120 U32 maxdepth; // max. indentation/recursion level 127 U32 maxdepth; // max. indentation/recursion level
121} enc_t; 128} enc_t;
122 129
123inline void 130inline void
197 } 204 }
198 205
199 if (uch > 0x10FFFFUL) 206 if (uch > 0x10FFFFUL)
200 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); 207 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
201 208
202 if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) 209 if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF))
203 { 210 {
204 if (uch > 0xFFFFUL) 211 if (uch > 0xFFFFUL)
205 { 212 {
206 need (enc, len += 11); 213 need (enc, len += 11);
207 sprintf (enc->cur, "\\u%04x\\u%04x", 214 sprintf (enc->cur, "\\u%04x\\u%04x",
221 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 228 *enc->cur++ = hexdigit [(uch >> 0) & 15];
222 } 229 }
223 230
224 str += clen; 231 str += clen;
225 } 232 }
226 else if (enc->flags & F_LATIN1) 233 else if (enc->json.flags & F_LATIN1)
227 { 234 {
228 *enc->cur++ = uch; 235 *enc->cur++ = uch;
229 str += clen; 236 str += clen;
230 } 237 }
231 else if (is_utf8) 238 else if (is_utf8)
252} 259}
253 260
254inline void 261inline void
255encode_indent (enc_t *enc) 262encode_indent (enc_t *enc)
256{ 263{
257 if (enc->flags & F_INDENT) 264 if (enc->json.flags & F_INDENT)
258 { 265 {
259 int spaces = enc->indent * INDENT_STEP; 266 int spaces = enc->indent * INDENT_STEP;
260 267
261 need (enc, spaces); 268 need (enc, spaces);
262 memset (enc->cur, ' ', spaces); 269 memset (enc->cur, ' ', spaces);
272} 279}
273 280
274inline void 281inline void
275encode_nl (enc_t *enc) 282encode_nl (enc_t *enc)
276{ 283{
277 if (enc->flags & F_INDENT) 284 if (enc->json.flags & F_INDENT)
278 { 285 {
279 need (enc, 1); 286 need (enc, 1);
280 encode_ch (enc, '\n'); 287 encode_ch (enc, '\n');
281 } 288 }
282} 289}
284inline void 291inline void
285encode_comma (enc_t *enc) 292encode_comma (enc_t *enc)
286{ 293{
287 encode_ch (enc, ','); 294 encode_ch (enc, ',');
288 295
289 if (enc->flags & F_INDENT) 296 if (enc->json.flags & F_INDENT)
290 encode_nl (enc); 297 encode_nl (enc);
291 else if (enc->flags & F_SPACE_AFTER) 298 else if (enc->json.flags & F_SPACE_AFTER)
292 encode_space (enc); 299 encode_space (enc);
293} 300}
294 301
295static void encode_sv (enc_t *enc, SV *sv); 302static void encode_sv (enc_t *enc, SV *sv);
296 303
339 else 346 else
340 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); 347 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he));
341 348
342 encode_ch (enc, '"'); 349 encode_ch (enc, '"');
343 350
344 if (enc->flags & F_SPACE_BEFORE) encode_space (enc); 351 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
345 encode_ch (enc, ':'); 352 encode_ch (enc, ':');
346 if (enc->flags & F_SPACE_AFTER ) encode_space (enc); 353 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
347 encode_sv (enc, HeVAL (he)); 354 encode_sv (enc, HeVAL (he));
348} 355}
349 356
350// compare hash entries, used when all keys are bytestrings 357// compare hash entries, used when all keys are bytestrings
351static int 358static int
386 { 393 {
387 // for canonical output we have to sort by keys first 394 // for canonical output we have to sort by keys first
388 // actually, this is mostly due to the stupid so-called 395 // actually, this is mostly due to the stupid so-called
389 // security workaround added somewhere in 5.8.x. 396 // security workaround added somewhere in 5.8.x.
390 // that randomises hash orderings 397 // that randomises hash orderings
391 if (enc->flags & F_CANONICAL) 398 if (enc->json.flags & F_CANONICAL)
392 { 399 {
393 int fast = 1; 400 int fast = 1;
394 HE *he; 401 HE *he;
395#if defined(__BORLANDC__) || defined(_MSC_VER) 402#if defined(__BORLANDC__) || defined(_MSC_VER)
396 HE **hes = _alloca (count * sizeof (HE)); 403 HE **hes = _alloca (count * sizeof (HE));
485 if (0 && sv_derived_from (rv, "JSON::Literal")) 492 if (0 && sv_derived_from (rv, "JSON::Literal"))
486 { 493 {
487 // not yet 494 // not yet
488 } 495 }
489#endif 496#endif
490 if (enc->flags & F_CONV_BLESSED) 497 if (enc->json.flags & F_CONV_BLESSED)
491 { 498 {
492 // we re-bless the reference to get overload and other niceties right 499 // we re-bless the reference to get overload and other niceties right
493 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); 500 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1);
494 501
495 if (to_json) 502 if (to_json)
509 encode_sv (enc, POPs); 516 encode_sv (enc, POPs);
510 517
511 FREETMPS; 518 FREETMPS;
512 LEAVE; 519 LEAVE;
513 } 520 }
514 else if (enc->flags & F_ALLOW_BLESSED) 521 else if (enc->json.flags & F_ALLOW_BLESSED)
515 encode_str (enc, "null", 4, 0); 522 encode_str (enc, "null", 4, 0);
516 else 523 else
517 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", 524 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it",
518 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 525 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
519 } 526 }
520 else if (enc->flags & F_ALLOW_BLESSED) 527 else if (enc->json.flags & F_ALLOW_BLESSED)
521 encode_str (enc, "null", 4, 0); 528 encode_str (enc, "null", 4, 0);
522 else 529 else
523 croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", 530 croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled",
524 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 531 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
525 } 532 }
611 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 618 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this",
612 SvPV_nolen (sv), SvFLAGS (sv)); 619 SvPV_nolen (sv), SvFLAGS (sv));
613} 620}
614 621
615static SV * 622static SV *
616encode_json (SV *scalar, U32 flags) 623encode_json (SV *scalar, struct json *json)
617{ 624{
618 enc_t enc; 625 enc_t enc;
619 626
620 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) 627 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar))
621 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 628 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
622 629
623 enc.flags = flags; 630 enc.json = *json;
624 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 631 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
625 enc.cur = SvPVX (enc.sv); 632 enc.cur = SvPVX (enc.sv);
626 enc.end = SvEND (enc.sv); 633 enc.end = SvEND (enc.sv);
627 enc.indent = 0; 634 enc.indent = 0;
628 enc.maxdepth = DEC_DEPTH (flags); 635 enc.maxdepth = DEC_DEPTH (enc.json.flags);
629 636
630 SvPOK_only (enc.sv); 637 SvPOK_only (enc.sv);
631 encode_sv (&enc, scalar); 638 encode_sv (&enc, scalar);
632 639
633 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 640 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
634 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 641 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
635 642
636 if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) 643 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
637 SvUTF8_on (enc.sv); 644 SvUTF8_on (enc.sv);
638 645
639 if (enc.flags & F_SHRINK) 646 if (enc.json.flags & F_SHRINK)
640 shrink (enc.sv); 647 shrink (enc.sv);
641 648
642 return enc.sv; 649 return enc.sv;
643} 650}
644 651
649typedef struct 656typedef struct
650{ 657{
651 char *cur; // current parser pointer 658 char *cur; // current parser pointer
652 char *end; // end of input string 659 char *end; // end of input string
653 const char *err; // parse error, if != 0 660 const char *err; // parse error, if != 0
654 U32 flags; // F_* 661 struct json json;
655 U32 depth; // recursion depth 662 U32 depth; // recursion depth
656 U32 maxdepth; // recursion depth limit 663 U32 maxdepth; // recursion depth limit
657} dec_t; 664} dec_t;
658 665
659inline void 666inline void
1020 if (*dec->cur == '}') 1027 if (*dec->cur == '}')
1021 ++dec->cur; 1028 ++dec->cur;
1022 else 1029 else
1023 for (;;) 1030 for (;;)
1024 { 1031 {
1025 SV *key, *value;
1026
1027 decode_ws (dec); EXPECT_CH ('"'); 1032 decode_ws (dec); EXPECT_CH ('"');
1028 1033
1029 key = decode_str (dec); 1034 // heuristic: assume that
1030 if (!key) 1035 // a) decode_str + hv_store_ent are abysmally slow.
1031 goto fail; 1036 // b) most hash keys are short, simple ascii text.
1037 // => try to "fast-match" such strings to avoid
1038 // the overhead of decode_str + hv_store_ent.
1039 {
1040 SV *value;
1041 char *p = dec->cur;
1042 char *e = p + 24; // only try up to 24 bytes
1032 1043
1033 decode_ws (dec); EXPECT_CH (':'); 1044 for (;;)
1034
1035 value = decode_sv (dec);
1036 if (!value)
1037 { 1045 {
1046 // the >= 0x80 is true on most architectures
1047 if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\')
1048 {
1049 // slow path, back up and use decode_str
1050 SV *key = decode_str (dec);
1051 if (!key)
1052 goto fail;
1053
1054 decode_ws (dec); EXPECT_CH (':');
1055
1056 value = decode_sv (dec);
1057 if (!value)
1058 {
1059 SvREFCNT_dec (key);
1060 goto fail;
1061 }
1062
1063 hv_store_ent (hv, key, value, 0);
1038 SvREFCNT_dec (key); 1064 SvREFCNT_dec (key);
1065
1066 break;
1067 }
1068 else if (*p == '"')
1069 {
1070 // fast path, got a simple key
1071 char *key = dec->cur;
1072 int len = p - key;
1073 dec->cur = p + 1;
1074
1075 decode_ws (dec); EXPECT_CH (':');
1076
1077 value = decode_sv (dec);
1078 if (!value)
1039 goto fail; 1079 goto fail;
1080
1081 hv_store (hv, key, len, value, 0);
1082
1083 break;
1084 }
1085
1086 ++p;
1040 } 1087 }
1041 1088 }
1042 hv_store_ent (hv, key, value, 0);
1043 SvREFCNT_dec (key);
1044 1089
1045 decode_ws (dec); 1090 decode_ws (dec);
1046 1091
1047 if (*dec->cur == '}') 1092 if (*dec->cur == '}')
1048 { 1093 {
1124fail: 1169fail:
1125 return 0; 1170 return 0;
1126} 1171}
1127 1172
1128static SV * 1173static SV *
1129decode_json (SV *string, U32 flags, UV *offset_return) 1174decode_json (SV *string, struct json *json, UV *offset_return)
1130{ 1175{
1131 dec_t dec; 1176 dec_t dec;
1132 UV offset; 1177 UV offset;
1133 SV *sv; 1178 SV *sv;
1134 1179
1135 SvGETMAGIC (string); 1180 SvGETMAGIC (string);
1136 SvUPGRADE (string, SVt_PV); 1181 SvUPGRADE (string, SVt_PV);
1137 1182
1183 if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags))
1184 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1185 (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags));
1186
1138 if (flags & F_UTF8) 1187 if (json->flags & F_UTF8)
1139 sv_utf8_downgrade (string, 0); 1188 sv_utf8_downgrade (string, 0);
1140 else 1189 else
1141 sv_utf8_upgrade (string); 1190 sv_utf8_upgrade (string);
1142 1191
1143 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1192 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1144 1193
1145 dec.flags = flags; 1194 dec.json = *json;
1146 dec.cur = SvPVX (string); 1195 dec.cur = SvPVX (string);
1147 dec.end = SvEND (string); 1196 dec.end = SvEND (string);
1148 dec.err = 0; 1197 dec.err = 0;
1149 dec.depth = 0; 1198 dec.depth = 0;
1150 dec.maxdepth = DEC_DEPTH (dec.flags); 1199 dec.maxdepth = DEC_DEPTH (dec.json.flags);
1151 1200
1152 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1201 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1153 sv = decode_sv (&dec); 1202 sv = decode_sv (&dec);
1154 1203
1155 if (!(offset_return || !sv)) 1204 if (!(offset_return || !sv))
1165 } 1214 }
1166 } 1215 }
1167 1216
1168 if (offset_return || !sv) 1217 if (offset_return || !sv)
1169 { 1218 {
1170 offset = dec.flags & F_UTF8 1219 offset = dec.json.flags & F_UTF8
1171 ? dec.cur - SvPVX (string) 1220 ? dec.cur - SvPVX (string)
1172 : utf8_distance (dec.cur, SvPVX (string)); 1221 : utf8_distance (dec.cur, SvPVX (string));
1173 1222
1174 if (offset_return) 1223 if (offset_return)
1175 *offset_return = offset; 1224 *offset_return = offset;
1194 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1243 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1195 } 1244 }
1196 1245
1197 sv = sv_2mortal (sv); 1246 sv = sv_2mortal (sv);
1198 1247
1199 if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) 1248 if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv))
1200 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); 1249 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)");
1201 1250
1202 return sv; 1251 return sv;
1203} 1252}
1204 1253
1276 RETVAL = newSVsv (self); 1325 RETVAL = newSVsv (self);
1277} 1326}
1278 OUTPUT: 1327 OUTPUT:
1279 RETVAL 1328 RETVAL
1280 1329
1330SV *max_size (SV *self, UV max_size = 0)
1331 CODE:
1332{
1333 UV *uv = SvJSON (self);
1334 UV log2 = 0;
1335
1336 if (max_size > 0x80000000UL) max_size = 0x80000000UL;
1337 if (max_size == 1) max_size = 2;
1338
1339 while ((1UL << log2) < max_size)
1340 ++log2;
1341
1342 *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1343
1344 RETVAL = newSVsv (self);
1345}
1346 OUTPUT:
1347 RETVAL
1348
1281void encode (SV *self, SV *scalar) 1349void encode (SV *self, SV *scalar)
1282 PPCODE: 1350 PPCODE:
1351{
1352 struct json json = { *SvJSON (self) };
1283 XPUSHs (encode_json (scalar, *SvJSON (self))); 1353 XPUSHs (encode_json (scalar, &json));
1354}
1284 1355
1285void decode (SV *self, SV *jsonstr) 1356void decode (SV *self, SV *jsonstr)
1286 PPCODE: 1357 PPCODE:
1358{
1359 struct json json = { *SvJSON (self) };
1287 XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); 1360 XPUSHs (decode_json (jsonstr, &json, 0));
1361}
1288 1362
1289void decode_prefix (SV *self, SV *jsonstr) 1363void decode_prefix (SV *self, SV *jsonstr)
1290 PPCODE: 1364 PPCODE:
1291{ 1365{
1292 UV offset; 1366 UV offset;
1367 struct json json = { *SvJSON (self) };
1293 EXTEND (SP, 2); 1368 EXTEND (SP, 2);
1294 PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); 1369 PUSHs (decode_json (jsonstr, &json, &offset));
1295 PUSHs (sv_2mortal (newSVuv (offset))); 1370 PUSHs (sv_2mortal (newSVuv (offset)));
1296} 1371}
1297 1372
1298PROTOTYPES: ENABLE 1373PROTOTYPES: ENABLE
1299 1374
1300void to_json (SV *scalar) 1375void to_json (SV *scalar)
1301 ALIAS:
1302 objToJson = 0
1303 PPCODE: 1376 PPCODE:
1377{
1378 struct json json = { F_DEFAULT | F_UTF8 };
1304 XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); 1379 XPUSHs (encode_json (scalar, &json));
1380}
1305 1381
1306void from_json (SV *jsonstr) 1382void from_json (SV *jsonstr)
1307 ALIAS:
1308 jsonToObj = 0
1309 PPCODE: 1383 PPCODE:
1384{
1385 struct json json = { F_DEFAULT | F_UTF8 };
1310 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); 1386 XPUSHs (decode_json (jsonstr, &json, 0));
1387}
1311 1388

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines