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.50 by root, Thu Feb 25 02:29:22 2016 UTC vs.
Revision 1.54 by root, Sun Apr 24 20:08:41 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
106#define F_TEXT_STRINGS 0x00000040UL
105#define F_VALIDATE_UTF8 0x00000020UL 107#define F_VALIDATE_UTF8 0x00000080UL
106 108
107#define INIT_SIZE 32 // initial scalar size to be allocated 109#define INIT_SIZE 32 // initial scalar size to be allocated
108 110
109#define SB do { 111#define SB do {
110#define SE } while (0) 112#define SE } while (0)
276encode_tag (enc_t *enc, UV tag) 278encode_tag (enc_t *enc, UV tag)
277{ 279{
278 encode_uint (enc, MAJOR_TAG, tag); 280 encode_uint (enc, MAJOR_TAG, tag);
279} 281}
280 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
281ecb_inline void 306ecb_inline void
282encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 307encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
283{ 308{
309 if (ecb_expect_false (upgrade_utf8))
310 if (!utf8)
311 {
312 encode_str_utf8 (enc, utf8, str, len);
313 return;
314 }
315
284 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); 316 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
285 need (enc, len); 317 need (enc, len);
286 memcpy (enc->cur, str, len); 318 memcpy (enc->cur, str, len);
287 enc->cur += len; 319 enc->cur += len;
288} 320}
289 321
290static void 322ecb_inline void
291encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) 323encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len)
292{ 324{
293 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) 325 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS))
294 { 326 {
295 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); 327 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
296 328
307 sv_setuv (*svp, enc->stringref_idx); 339 sv_setuv (*svp, enc->stringref_idx);
308 ++enc->stringref_idx; 340 ++enc->stringref_idx;
309 } 341 }
310 } 342 }
311 343
312 encode_str (enc, utf8, str, len); 344 encode_str (enc, upgrade_utf8, utf8, str, len);
313} 345}
314 346
315static void encode_sv (enc_t *enc, SV *sv); 347static void encode_sv (enc_t *enc, SV *sv);
316 348
317static void 349static void
363 while ((he = hv_iternext (hv))) 395 while ((he = hv_iternext (hv)))
364 { 396 {
365 if (HeKLEN (he) == HEf_SVKEY) 397 if (HeKLEN (he) == HEf_SVKEY)
366 encode_sv (enc, HeSVKEY (he)); 398 encode_sv (enc, HeSVKEY (he));
367 else 399 else
368 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));
369 401
370 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));
371 } 403 }
372 404
373 if (mg) 405 if (mg)
491 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) 523 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv)
492 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));
493 525
494 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 526 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
495 encode_uint (enc, MAJOR_ARRAY, count + 1); 527 encode_uint (enc, MAJOR_ARRAY, count + 1);
496 encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 528 encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
497 529
498 while (count) 530 while (count)
499 encode_sv (enc, SP[1 - count--]); 531 encode_sv (enc, SP[1 - count--]);
500 532
501 PUTBACK; 533 PUTBACK;
560 592
561 if (SvPOKp (sv)) 593 if (SvPOKp (sv))
562 { 594 {
563 STRLEN len; 595 STRLEN len;
564 char *str = SvPV (sv, len); 596 char *str = SvPV (sv, len);
565 encode_strref (enc, SvUTF8 (sv), str, len); 597 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
566 } 598 }
567 else if (SvNOKp (sv)) 599 else if (SvNOKp (sv))
568 encode_nv (enc, sv); 600 encode_nv (enc, sv);
569 else if (SvIOKp (sv)) 601 else if (SvIOKp (sv))
570 { 602 {
1390 shrink = F_SHRINK 1422 shrink = F_SHRINK
1391 allow_unknown = F_ALLOW_UNKNOWN 1423 allow_unknown = F_ALLOW_UNKNOWN
1392 allow_sharing = F_ALLOW_SHARING 1424 allow_sharing = F_ALLOW_SHARING
1393 allow_cycles = F_ALLOW_CYCLES 1425 allow_cycles = F_ALLOW_CYCLES
1394 pack_strings = F_PACK_STRINGS 1426 pack_strings = F_PACK_STRINGS
1427 text_keys = F_TEXT_KEYS
1428 text_strings = F_TEXT_STRINGS
1395 validate_utf8 = F_VALIDATE_UTF8 1429 validate_utf8 = F_VALIDATE_UTF8
1396 PPCODE: 1430 PPCODE:
1397{ 1431{
1398 if (enable) 1432 if (enable)
1399 self->flags |= ix; 1433 self->flags |= ix;
1408 get_shrink = F_SHRINK 1442 get_shrink = F_SHRINK
1409 get_allow_unknown = F_ALLOW_UNKNOWN 1443 get_allow_unknown = F_ALLOW_UNKNOWN
1410 get_allow_sharing = F_ALLOW_SHARING 1444 get_allow_sharing = F_ALLOW_SHARING
1411 get_allow_cycles = F_ALLOW_CYCLES 1445 get_allow_cycles = F_ALLOW_CYCLES
1412 get_pack_strings = F_PACK_STRINGS 1446 get_pack_strings = F_PACK_STRINGS
1447 get_text_keys = F_TEXT_KEYS
1448 get_text_strings = F_TEXT_STRINGS
1413 get_validate_utf8 = F_VALIDATE_UTF8 1449 get_validate_utf8 = F_VALIDATE_UTF8
1414 PPCODE: 1450 PPCODE:
1415 XPUSHs (boolSV (self->flags & ix)); 1451 XPUSHs (boolSV (self->flags & ix));
1416 1452
1417void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1453void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines