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.64 by root, Mon Aug 27 02:03:23 2007 UTC vs.
Revision 1.71 by root, Wed Mar 19 03:17:38 2008 UTC

50 50
51#define SB do { 51#define SB do {
52#define SE } while (0) 52#define SE } while (0)
53 53
54#if __GNUC__ >= 3 54#if __GNUC__ >= 3
55# define expect(expr,value) __builtin_expect ((expr),(value)) 55# define expect(expr,value) __builtin_expect ((expr), (value))
56# define inline inline 56# define INLINE static inline
57#else 57#else
58# define expect(expr,value) (expr) 58# define expect(expr,value) (expr)
59# define inline static 59# define INLINE static
60#endif 60#endif
61 61
62#define expect_false(expr) expect ((expr) != 0, 0) 62#define expect_false(expr) expect ((expr) != 0, 0)
63#define expect_true(expr) expect ((expr) != 0, 1) 63#define expect_true(expr) expect ((expr) != 0, 1)
64 64
80} JSON; 80} JSON;
81 81
82///////////////////////////////////////////////////////////////////////////// 82/////////////////////////////////////////////////////////////////////////////
83// utility functions 83// utility functions
84 84
85inline void 85INLINE void
86shrink (SV *sv) 86shrink (SV *sv)
87{ 87{
88 sv_utf8_downgrade (sv, 1); 88 sv_utf8_downgrade (sv, 1);
89 if (SvLEN (sv) > SvCUR (sv) + 1) 89 if (SvLEN (sv) > SvCUR (sv) + 1)
90 { 90 {
99// decode an utf-8 character and return it, or (UV)-1 in 99// decode an utf-8 character and return it, or (UV)-1 in
100// case of an error. 100// case of an error.
101// we special-case "safe" characters from U+80 .. U+7FF, 101// we special-case "safe" characters from U+80 .. U+7FF,
102// but use the very good perl function to parse anything else. 102// but use the very good perl function to parse anything else.
103// note that we never call this function for a ascii codepoints 103// note that we never call this function for a ascii codepoints
104inline UV 104INLINE UV
105decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 105decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
106{ 106{
107 if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) 107 if (expect_false (s[0] > 0xdf || s[0] < 0xc2))
108 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 108 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
109 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 109 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf)
128 char *end; // SvEND (sv) 128 char *end; // SvEND (sv)
129 SV *sv; // result scalar 129 SV *sv; // result scalar
130 JSON json; 130 JSON json;
131 U32 indent; // indentation level 131 U32 indent; // indentation level
132 U32 maxdepth; // max. indentation/recursion level 132 U32 maxdepth; // max. indentation/recursion level
133 UV limit; // escape character values >= this value when encoding
133} enc_t; 134} enc_t;
134 135
135inline void 136INLINE void
136need (enc_t *enc, STRLEN len) 137need (enc_t *enc, STRLEN len)
137{ 138{
138 if (expect_false (enc->cur + len >= enc->end)) 139 if (expect_false (enc->cur + len >= enc->end))
139 { 140 {
140 STRLEN cur = enc->cur - SvPVX (enc->sv); 141 STRLEN cur = enc->cur - SvPVX (enc->sv);
142 enc->cur = SvPVX (enc->sv) + cur; 143 enc->cur = SvPVX (enc->sv) + cur;
143 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 144 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
144 } 145 }
145} 146}
146 147
147inline void 148INLINE void
148encode_ch (enc_t *enc, char ch) 149encode_ch (enc_t *enc, char ch)
149{ 150{
150 need (enc, 1); 151 need (enc, 1);
151 *enc->cur++ = ch; 152 *enc->cur++ = ch;
152} 153}
206 { 207 {
207 uch = ch; 208 uch = ch;
208 clen = 1; 209 clen = 1;
209 } 210 }
210 211
211 if (uch > 0x10FFFFUL) 212 if (uch < 0x20 || uch >= enc->limit)
212 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
213
214 if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF))
215 { 213 {
216 if (uch > 0xFFFFUL) 214 if (uch > 0xFFFFUL)
217 { 215 {
216 if (uch > 0x10FFFFUL)
217 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
218
218 need (enc, len += 11); 219 need (enc, len += 11);
219 sprintf (enc->cur, "\\u%04x\\u%04x", 220 sprintf (enc->cur, "\\u%04x\\u%04x",
220 (int)((uch - 0x10000) / 0x400 + 0xD800), 221 (int)((uch - 0x10000) / 0x400 + 0xD800),
221 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 222 (int)((uch - 0x10000) % 0x400 + 0xDC00));
222 enc->cur += 12; 223 enc->cur += 12;
261 262
262 --len; 263 --len;
263 } 264 }
264} 265}
265 266
266inline void 267INLINE void
267encode_indent (enc_t *enc) 268encode_indent (enc_t *enc)
268{ 269{
269 if (enc->json.flags & F_INDENT) 270 if (enc->json.flags & F_INDENT)
270 { 271 {
271 int spaces = enc->indent * INDENT_STEP; 272 int spaces = enc->indent * INDENT_STEP;
274 memset (enc->cur, ' ', spaces); 275 memset (enc->cur, ' ', spaces);
275 enc->cur += spaces; 276 enc->cur += spaces;
276 } 277 }
277} 278}
278 279
279inline void 280INLINE void
280encode_space (enc_t *enc) 281encode_space (enc_t *enc)
281{ 282{
282 need (enc, 1); 283 need (enc, 1);
283 encode_ch (enc, ' '); 284 encode_ch (enc, ' ');
284} 285}
285 286
286inline void 287INLINE void
287encode_nl (enc_t *enc) 288encode_nl (enc_t *enc)
288{ 289{
289 if (enc->json.flags & F_INDENT) 290 if (enc->json.flags & F_INDENT)
290 { 291 {
291 need (enc, 1); 292 need (enc, 1);
292 encode_ch (enc, '\n'); 293 encode_ch (enc, '\n');
293 } 294 }
294} 295}
295 296
296inline void 297INLINE void
297encode_comma (enc_t *enc) 298encode_comma (enc_t *enc)
298{ 299{
299 encode_ch (enc, ','); 300 encode_ch (enc, ',');
300 301
301 if (enc->json.flags & F_INDENT) 302 if (enc->json.flags & F_INDENT)
312 int i, len = av_len (av); 313 int i, len = av_len (av);
313 314
314 if (enc->indent >= enc->maxdepth) 315 if (enc->indent >= enc->maxdepth)
315 croak ("data structure too deep (hit recursion limit)"); 316 croak ("data structure too deep (hit recursion limit)");
316 317
317 encode_ch (enc, '['); encode_nl (enc); 318 encode_ch (enc, '[');
318 ++enc->indent; 319
320 if (len >= 0)
321 {
322 encode_nl (enc); ++enc->indent;
319 323
320 for (i = 0; i <= len; ++i) 324 for (i = 0; i <= len; ++i)
321 { 325 {
322 SV **svp = av_fetch (av, i, 0); 326 SV **svp = av_fetch (av, i, 0);
323 327
324 encode_indent (enc); 328 encode_indent (enc);
325 329
326 if (svp) 330 if (svp)
327 encode_sv (enc, *svp); 331 encode_sv (enc, *svp);
328 else 332 else
329 encode_str (enc, "null", 4, 0); 333 encode_str (enc, "null", 4, 0);
330 334
331 if (i < len) 335 if (i < len)
332 encode_comma (enc); 336 encode_comma (enc);
333 } 337 }
334 338
339 encode_nl (enc); --enc->indent; encode_indent (enc);
340 }
341
335 encode_nl (enc); 342 encode_ch (enc, ']');
336
337 --enc->indent;
338 encode_indent (enc); encode_ch (enc, ']');
339} 343}
340 344
341static void 345static void
342encode_hk (enc_t *enc, HE *he) 346encode_hk (enc_t *enc, HE *he)
343{ 347{
396 int count; 400 int count;
397 401
398 if (enc->indent >= enc->maxdepth) 402 if (enc->indent >= enc->maxdepth)
399 croak ("data structure too deep (hit recursion limit)"); 403 croak ("data structure too deep (hit recursion limit)");
400 404
401 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 405 encode_ch (enc, '{');
402 406
403 // for canonical output we have to sort by keys first 407 // for canonical output we have to sort by keys first
404 // actually, this is mostly due to the stupid so-called 408 // actually, this is mostly due to the stupid so-called
405 // security workaround added somewhere in 5.8.x. 409 // security workaround added somewhere in 5.8.x.
406 // that randomises hash orderings 410 // that randomises hash orderings
459 463
460 FREETMPS; 464 FREETMPS;
461 LEAVE; 465 LEAVE;
462 } 466 }
463 467
468 encode_nl (enc); ++enc->indent;
469
464 while (count--) 470 while (count--)
465 { 471 {
466 encode_indent (enc); 472 encode_indent (enc);
467 he = hes [count]; 473 he = hes [count];
468 encode_hk (enc, he); 474 encode_hk (enc, he);
469 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); 475 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
470 476
471 if (count) 477 if (count)
472 encode_comma (enc); 478 encode_comma (enc);
473 } 479 }
480
481 encode_nl (enc); --enc->indent; encode_indent (enc);
474 } 482 }
475 } 483 }
476 else 484 else
477 { 485 {
478 if (hv_iterinit (hv) || SvMAGICAL (hv)) 486 if (hv_iterinit (hv) || SvMAGICAL (hv))
479 if ((he = hv_iternext (hv))) 487 if ((he = hv_iternext (hv)))
488 {
489 encode_nl (enc); ++enc->indent;
490
480 for (;;) 491 for (;;)
481 { 492 {
482 encode_indent (enc); 493 encode_indent (enc);
483 encode_hk (enc, he); 494 encode_hk (enc, he);
484 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); 495 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
485 496
486 if (!(he = hv_iternext (hv))) 497 if (!(he = hv_iternext (hv)))
487 break; 498 break;
488 499
489 encode_comma (enc); 500 encode_comma (enc);
490 } 501 }
491 }
492 502
503 encode_nl (enc); --enc->indent; encode_indent (enc);
504 }
505 }
506
493 encode_nl (enc); 507 encode_ch (enc, '}');
494
495 --enc->indent; encode_indent (enc); encode_ch (enc, '}');
496} 508}
497 509
498// encode objects, arrays and special \0=false and \1=true values. 510// encode objects, arrays and special \0=false and \1=true values.
499static void 511static void
500encode_rv (enc_t *enc, SV *sv) 512encode_rv (enc_t *enc, SV *sv)
669 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 681 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
670 enc.cur = SvPVX (enc.sv); 682 enc.cur = SvPVX (enc.sv);
671 enc.end = SvEND (enc.sv); 683 enc.end = SvEND (enc.sv);
672 enc.indent = 0; 684 enc.indent = 0;
673 enc.maxdepth = DEC_DEPTH (enc.json.flags); 685 enc.maxdepth = DEC_DEPTH (enc.json.flags);
686 enc.limit = enc.json.flags & F_ASCII ? 0x000080UL
687 : enc.json.flags & F_LATIN1 ? 0x000100UL
688 : 0x10FFFFUL;
674 689
675 SvPOK_only (enc.sv); 690 SvPOK_only (enc.sv);
676 encode_sv (&enc, scalar); 691 encode_sv (&enc, scalar);
677 692
678 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 693 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
699 JSON json; 714 JSON json;
700 U32 depth; // recursion depth 715 U32 depth; // recursion depth
701 U32 maxdepth; // recursion depth limit 716 U32 maxdepth; // recursion depth limit
702} dec_t; 717} dec_t;
703 718
704inline void 719INLINE void
705decode_comment (dec_t *dec) 720decode_comment (dec_t *dec)
706{ 721{
707 // only '#'-style comments allowed a.t.m. 722 // only '#'-style comments allowed a.t.m.
708 723
709 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) 724 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
710 ++dec->cur; 725 ++dec->cur;
711} 726}
712 727
713inline void 728INLINE void
714decode_ws (dec_t *dec) 729decode_ws (dec_t *dec)
715{ 730{
716 for (;;) 731 for (;;)
717 { 732 {
718 char ch = *dec->cur; 733 char ch = *dec->cur;
989 1004
990 if (!is_nv) 1005 if (!is_nv)
991 { 1006 {
992 int len = dec->cur - start; 1007 int len = dec->cur - start;
993 1008
994 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 1009 // special case the rather common 1..5-digit-int case
995 if (*start == '-') 1010 if (*start == '-')
996 switch (len) 1011 switch (len)
997 { 1012 {
998 case 2: return newSViv (-( start [1] - '0' * 1)); 1013 case 2: return newSViv (-( start [1] - '0' * 1));
999 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1014 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
1000 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 1015 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
1001 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 1016 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1017 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
1002 } 1018 }
1003 else 1019 else
1004 switch (len) 1020 switch (len)
1005 { 1021 {
1006 case 1: return newSViv ( start [0] - '0' * 1); 1022 case 1: return newSViv ( start [0] - '0' * 1);
1007 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1023 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
1008 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 1024 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
1009 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 1025 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1026 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
1010 } 1027 }
1011 1028
1012 { 1029 {
1013 UV uv; 1030 UV uv;
1014 int numtype = grok_number (start, len, &uv); 1031 int numtype = grok_number (start, len, &uv);
1446{ 1463{
1447 SV *pv = NEWSV (0, sizeof (JSON)); 1464 SV *pv = NEWSV (0, sizeof (JSON));
1448 SvPOK_only (pv); 1465 SvPOK_only (pv);
1449 Zero (SvPVX (pv), 1, JSON); 1466 Zero (SvPVX (pv), 1, JSON);
1450 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1467 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1451 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); 1468 XPUSHs (sv_2mortal (sv_bless (
1469 newRV_noinc (pv),
1470 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1471 )));
1452} 1472}
1453 1473
1454void ascii (JSON *self, int enable = 1) 1474void ascii (JSON *self, int enable = 1)
1455 ALIAS: 1475 ALIAS:
1456 ascii = F_ASCII 1476 ascii = F_ASCII
1474 self->flags &= ~ix; 1494 self->flags &= ~ix;
1475 1495
1476 XPUSHs (ST (0)); 1496 XPUSHs (ST (0));
1477} 1497}
1478 1498
1499void get_ascii (JSON *self)
1500 ALIAS:
1501 get_ascii = F_ASCII
1502 get_latin1 = F_LATIN1
1503 get_utf8 = F_UTF8
1504 get_indent = F_INDENT
1505 get_canonical = F_CANONICAL
1506 get_space_before = F_SPACE_BEFORE
1507 get_space_after = F_SPACE_AFTER
1508 get_allow_nonref = F_ALLOW_NONREF
1509 get_shrink = F_SHRINK
1510 get_allow_blessed = F_ALLOW_BLESSED
1511 get_convert_blessed = F_CONV_BLESSED
1512 get_relaxed = F_RELAXED
1513 PPCODE:
1514 XPUSHs (boolSV (self->flags & ix));
1515
1479void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1516void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1480 PPCODE: 1517 PPCODE:
1481{ 1518{
1482 UV log2 = 0; 1519 UV log2 = 0;
1483 1520
1489 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1526 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1490 1527
1491 XPUSHs (ST (0)); 1528 XPUSHs (ST (0));
1492} 1529}
1493 1530
1531U32 get_max_depth (JSON *self)
1532 CODE:
1533 RETVAL = DEC_DEPTH (self->flags);
1534 OUTPUT:
1535 RETVAL
1536
1494void max_size (JSON *self, UV max_size = 0) 1537void max_size (JSON *self, UV max_size = 0)
1495 PPCODE: 1538 PPCODE:
1496{ 1539{
1497 UV log2 = 0; 1540 UV log2 = 0;
1498 1541
1504 1547
1505 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1548 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1506 1549
1507 XPUSHs (ST (0)); 1550 XPUSHs (ST (0));
1508} 1551}
1552
1553int get_max_size (JSON *self)
1554 CODE:
1555 RETVAL = DEC_SIZE (self->flags);
1556 OUTPUT:
1557 RETVAL
1509 1558
1510void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1559void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1511 PPCODE: 1560 PPCODE:
1512{ 1561{
1513 SvREFCNT_dec (self->cb_object); 1562 SvREFCNT_dec (self->cb_object);
1560 SvREFCNT_dec (self->cb_sk_object); 1609 SvREFCNT_dec (self->cb_sk_object);
1561 SvREFCNT_dec (self->cb_object); 1610 SvREFCNT_dec (self->cb_object);
1562 1611
1563PROTOTYPES: ENABLE 1612PROTOTYPES: ENABLE
1564 1613
1565void to_json (SV *scalar) 1614void encode_json (SV *scalar)
1566 PPCODE: 1615 PPCODE:
1567{ 1616{
1568 JSON json = { F_DEFAULT | F_UTF8 }; 1617 JSON json = { F_DEFAULT | F_UTF8 };
1569 XPUSHs (encode_json (scalar, &json)); 1618 XPUSHs (encode_json (scalar, &json));
1570} 1619}
1571 1620
1572void from_json (SV *jsonstr) 1621void decode_json (SV *jsonstr)
1573 PPCODE: 1622 PPCODE:
1574{ 1623{
1575 JSON json = { F_DEFAULT | F_UTF8 }; 1624 JSON json = { F_DEFAULT | F_UTF8 };
1576 XPUSHs (decode_json (jsonstr, &json, 0)); 1625 XPUSHs (decode_json (jsonstr, &json, 0));
1577} 1626}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines