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

Comparing CBOR-XS/XS.xs (file contents):
Revision 1.51 by root, Sun Apr 24 13:15:19 2016 UTC vs.
Revision 1.55 by root, Fri Nov 25 06:13:16 2016 UTC

100#define F_SHRINK 0x00000001UL 100#define F_SHRINK 0x00000001UL
101#define F_ALLOW_UNKNOWN 0x00000002UL 101#define F_ALLOW_UNKNOWN 0x00000002UL
102#define F_ALLOW_SHARING 0x00000004UL 102#define F_ALLOW_SHARING 0x00000004UL
103#define F_ALLOW_CYCLES 0x00000008UL 103#define F_ALLOW_CYCLES 0x00000008UL
104#define F_PACK_STRINGS 0x00000010UL 104#define F_PACK_STRINGS 0x00000010UL
105#define F_TEXT_KEYS 0x00000020UL
105#define F_UTF8_STRINGS 0x00000020UL 106#define F_TEXT_STRINGS 0x00000040UL
106#define F_VALIDATE_UTF8 0x00000040UL 107#define F_VALIDATE_UTF8 0x00000080UL
107 108
108#define INIT_SIZE 32 // initial scalar size to be allocated 109#define INIT_SIZE 32 // initial scalar size to be allocated
109 110
110#define SB do { 111#define SB do {
111#define SE } while (0) 112#define SE } while (0)
277encode_tag (enc_t *enc, UV tag) 278encode_tag (enc_t *enc, UV tag)
278{ 279{
279 encode_uint (enc, MAJOR_TAG, tag); 280 encode_uint (enc, MAJOR_TAG, tag);
280} 281}
281 282
283// exceptional (hopefully) slow path for byte strings that need to be utf8-encoded
284ecb_noinline static void
285encode_str_utf8 (enc_t *enc, int utf8, char *str, STRLEN len)
286{
287 STRLEN ulen = len;
288 U8 *p, *pend = (U8 *)str + len;
289
290 for (p = (U8 *)str; p < pend; ++p)
291 ulen += *p >> 7; // count set high bits
292
293 encode_uint (enc, MAJOR_TEXT, ulen);
294
295 need (enc, ulen);
296 for (p = (U8 *)str; p < pend; ++p)
297 if (*p < 0x80)
298 *enc->cur++ = *p;
299 else
300 {
301 *enc->cur++ = 0xc0 + (*p >> 6);
302 *enc->cur++ = 0x80 + (*p & 63);
303 }
304}
305
282ecb_inline void 306ecb_inline void
283encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 307encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
284{ 308{
285 if (ecb_expect_false (enc->cbor.flags & F_UTF8_STRINGS)) 309 if (ecb_expect_false (upgrade_utf8))
286 if (!utf8) 310 if (!utf8)
287 { 311 {
288 SV *sv = sv_newmortal ();
289 char *s; STRLEN l;
290
291 sv_setpvn (sv, str, len);
292
293 s = SvPVutf8 (sv, l);
294 encode_str (enc, 1, s, l); 312 encode_str_utf8 (enc, utf8, str, len);
295 return; 313 return;
296 } 314 }
297 315
298 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); 316 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
299 need (enc, len); 317 need (enc, len);
300 memcpy (enc->cur, str, len); 318 memcpy (enc->cur, str, len);
301 enc->cur += len; 319 enc->cur += len;
302} 320}
303 321
304static void 322ecb_inline void
305encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) 323encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
306{ 324{
307 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) 325 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS))
308 { 326 {
309 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); 327 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
310 328
321 sv_setuv (*svp, enc->stringref_idx); 339 sv_setuv (*svp, enc->stringref_idx);
322 ++enc->stringref_idx; 340 ++enc->stringref_idx;
323 } 341 }
324 } 342 }
325 343
326 encode_str (enc, utf8, str, len); 344 encode_str (enc, upgrade_utf8, utf8, str, len);
327} 345}
328 346
329static void encode_sv (enc_t *enc, SV *sv); 347static void encode_sv (enc_t *enc, SV *sv);
330 348
331static void 349static void
377 while ((he = hv_iternext (hv))) 395 while ((he = hv_iternext (hv)))
378 { 396 {
379 if (HeKLEN (he) == HEf_SVKEY) 397 if (HeKLEN (he) == HEf_SVKEY)
380 encode_sv (enc, HeSVKEY (he)); 398 encode_sv (enc, HeSVKEY (he));
381 else 399 else
382 encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); 400 encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he));
383 401
384 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); 402 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
385 } 403 }
386 404
387 if (mg) 405 if (mg)
505 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) 523 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv)
506 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); 524 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash));
507 525
508 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 526 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
509 encode_uint (enc, MAJOR_ARRAY, count + 1); 527 encode_uint (enc, MAJOR_ARRAY, count + 1);
510 encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 528 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
511 529
512 while (count) 530 while (count)
513 encode_sv (enc, SP[1 - count--]); 531 encode_sv (enc, SP[1 - count--]);
514 532
515 PUTBACK; 533 PUTBACK;
574 592
575 if (SvPOKp (sv)) 593 if (SvPOKp (sv))
576 { 594 {
577 STRLEN len; 595 STRLEN len;
578 char *str = SvPV (sv, len); 596 char *str = SvPV (sv, len);
579 encode_strref (enc, SvUTF8 (sv), str, len); 597 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
580 } 598 }
581 else if (SvNOKp (sv)) 599 else if (SvNOKp (sv))
582 encode_nv (enc, sv); 600 encode_nv (enc, sv);
583 else if (SvIOKp (sv)) 601 else if (SvIOKp (sv))
584 { 602 {
924 sv = newRV_noinc (decode_sv (dec)); 942 sv = newRV_noinc (decode_sv (dec));
925 break; 943 break;
926 944
927 case CBOR_TAG_STRINGREF_NAMESPACE: 945 case CBOR_TAG_STRINGREF_NAMESPACE:
928 { 946 {
947 // do nmot use SAVETMPS/FREETMPS, as these will
948 // erase mortalised caches, e.g. "shareable"
929 ENTER; SAVETMPS; 949 ENTER;
930 950
931 SAVESPTR (dec->stringref); 951 SAVESPTR (dec->stringref);
932 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); 952 dec->stringref = (AV *)sv_2mortal ((SV *)newAV ());
933 953
934 sv = decode_sv (dec); 954 sv = decode_sv (dec);
935 955
936 FREETMPS; LEAVE; 956 LEAVE;
937 } 957 }
938 break; 958 break;
939 959
940 case CBOR_TAG_STRINGREF: 960 case CBOR_TAG_STRINGREF:
941 { 961 {
1404 shrink = F_SHRINK 1424 shrink = F_SHRINK
1405 allow_unknown = F_ALLOW_UNKNOWN 1425 allow_unknown = F_ALLOW_UNKNOWN
1406 allow_sharing = F_ALLOW_SHARING 1426 allow_sharing = F_ALLOW_SHARING
1407 allow_cycles = F_ALLOW_CYCLES 1427 allow_cycles = F_ALLOW_CYCLES
1408 pack_strings = F_PACK_STRINGS 1428 pack_strings = F_PACK_STRINGS
1429 text_keys = F_TEXT_KEYS
1409 utf8_strings = F_UTF8_STRINGS 1430 text_strings = F_TEXT_STRINGS
1410 validate_utf8 = F_VALIDATE_UTF8 1431 validate_utf8 = F_VALIDATE_UTF8
1411 PPCODE: 1432 PPCODE:
1412{ 1433{
1413 if (enable) 1434 if (enable)
1414 self->flags |= ix; 1435 self->flags |= ix;
1423 get_shrink = F_SHRINK 1444 get_shrink = F_SHRINK
1424 get_allow_unknown = F_ALLOW_UNKNOWN 1445 get_allow_unknown = F_ALLOW_UNKNOWN
1425 get_allow_sharing = F_ALLOW_SHARING 1446 get_allow_sharing = F_ALLOW_SHARING
1426 get_allow_cycles = F_ALLOW_CYCLES 1447 get_allow_cycles = F_ALLOW_CYCLES
1427 get_pack_strings = F_PACK_STRINGS 1448 get_pack_strings = F_PACK_STRINGS
1449 get_text_keys = F_TEXT_KEYS
1450 get_text_strings = F_TEXT_STRINGS
1428 get_validate_utf8 = F_VALIDATE_UTF8 1451 get_validate_utf8 = F_VALIDATE_UTF8
1429 PPCODE: 1452 PPCODE:
1430 XPUSHs (boolSV (self->flags & ix)); 1453 XPUSHs (boolSV (self->flags & ix));
1431 1454
1432void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1455void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines