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.31 by root, Thu Nov 28 11:43:26 2013 UTC vs.
Revision 1.34 by root, Sat Nov 30 16:19:59 2013 UTC

59 CBOR_TAG_MAGIC = 55799 // self-describe cbor 59 CBOR_TAG_MAGIC = 55799 // self-describe cbor
60}; 60};
61 61
62#define F_SHRINK 0x00000001UL 62#define F_SHRINK 0x00000001UL
63#define F_ALLOW_UNKNOWN 0x00000002UL 63#define F_ALLOW_UNKNOWN 0x00000002UL
64#define F_ALLOW_SHARING 0x00000004UL //TODO 64#define F_ALLOW_SHARING 0x00000004UL
65#define F_ALLOW_STRINGREF 0x00000008UL //TODO 65#define F_PACK_STRINGS 0x00000008UL
66 66
67#define INIT_SIZE 32 // initial scalar size to be allocated 67#define INIT_SIZE 32 // initial scalar size to be allocated
68 68
69#define SB do { 69#define SB do {
70#define SE } while (0) 70#define SE } while (0)
242} 242}
243 243
244static void 244static void
245encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) 245encode_strref (enc_t *enc, int utf8, char *str, STRLEN len)
246{ 246{
247 if (ecb_expect_false (enc->cbor.flags & F_ALLOW_STRINGREF)) 247 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS))
248 { 248 {
249 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); 249 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
250 250
251 if (SvOK (*svp)) 251 if (SvOK (*svp))
252 { 252 {
326// encode objects, arrays and special \0=false and \1=true values. 326// encode objects, arrays and special \0=false and \1=true values.
327static void 327static void
328encode_rv (enc_t *enc, SV *sv) 328encode_rv (enc_t *enc, SV *sv)
329{ 329{
330 SvGETMAGIC (sv); 330 SvGETMAGIC (sv);
331
332 if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)
333 && ecb_expect_false (SvREFCNT (sv) > 1))
334 {
335 if (!enc->shareable)
336 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ());
337
338 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1);
339
340 if (SvOK (*svp))
341 {
342 encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF);
343 encode_uint (enc, 0x00, SvUV (*svp));
344 return;
345 }
346 else
347 {
348 sv_setuv (*svp, enc->shareable_idx);
349 ++enc->shareable_idx;
350 encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE);
351 }
352 }
353 331
354 svtype svt = SvTYPE (sv); 332 svtype svt = SvTYPE (sv);
355 333
356 if (ecb_expect_false (SvOBJECT (sv))) 334 if (ecb_expect_false (SvOBJECT (sv)))
357 { 335 {
364 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 342 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
365 ? cbor_tagged_stash 343 ? cbor_tagged_stash
366 : gv_stashpv ("CBOR::XS::Tagged" , 1); 344 : gv_stashpv ("CBOR::XS::Tagged" , 1);
367 345
368 HV *stash = SvSTASH (sv); 346 HV *stash = SvSTASH (sv);
369 GV *method;
370 347
371 if (stash == boolean_stash) 348 if (stash == boolean_stash)
349 {
372 encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); 350 encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20);
351 return;
352 }
373 else if (stash == error_stash) 353 else if (stash == error_stash)
354 {
374 encode_ch (enc, 0xe0 | 23); 355 encode_ch (enc, 0xe0 | 23);
356 return;
357 }
375 else if (stash == tagged_stash) 358 else if (stash == tagged_stash)
376 { 359 {
377 if (svt != SVt_PVAV) 360 if (svt != SVt_PVAV)
378 croak ("encountered CBOR::XS::Tagged object that isn't an array"); 361 croak ("encountered CBOR::XS::Tagged object that isn't an array");
379 362
380 encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); 363 encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1)));
381 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); 364 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1));
365
366 return;
367 }
368 }
369
370 if (ecb_expect_false (SvREFCNT (sv) > 1)
371 && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING))
372 {
373 if (!enc->shareable)
374 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ());
375
376 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1);
377
378 if (SvOK (*svp))
382 } 379 {
380 encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF);
381 encode_uint (enc, 0x00, SvUV (*svp));
382 return;
383 }
384 else
385 {
386 sv_setuv (*svp, enc->shareable_idx);
387 ++enc->shareable_idx;
388 encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE);
389 }
390 }
391
392 if (ecb_expect_false (SvOBJECT (sv)))
393 {
394 HV *stash = SvSTASH (sv);
395 GV *method;
396
383 else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) 397 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
384 { 398 {
385 dSP; 399 dSP;
386 400
387 ENTER; SAVETMPS; PUSHMARK (SP); 401 ENTER; SAVETMPS; PUSHMARK (SP);
388 // we re-bless the reference to get overload and other niceties right 402 // we re-bless the reference to get overload and other niceties right
526 enc.cur = SvPVX (enc.sv); 540 enc.cur = SvPVX (enc.sv);
527 enc.end = SvEND (enc.sv); 541 enc.end = SvEND (enc.sv);
528 542
529 SvPOK_only (enc.sv); 543 SvPOK_only (enc.sv);
530 544
531 if (cbor->flags & F_ALLOW_STRINGREF) 545 if (cbor->flags & F_PACK_STRINGS)
532 { 546 {
533 encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); 547 encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE);
534 enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); 548 enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ());
535 enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); 549 enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ());
536 } 550 }
600 | ((UV)dec->cur[-1]); 614 | ((UV)dec->cur[-1]);
601 615
602 case 27: 616 case 27:
603 WANT (9); 617 WANT (9);
604 dec->cur += 9; 618 dec->cur += 9;
619
620 return
621#if UVSIZE < 8
622 0
623#else
605 return (((UV)dec->cur[-8]) << 56) 624 (((UV)dec->cur[-8]) << 56)
606 | (((UV)dec->cur[-7]) << 48) 625 | (((UV)dec->cur[-7]) << 48)
607 | (((UV)dec->cur[-6]) << 40) 626 | (((UV)dec->cur[-6]) << 40)
608 | (((UV)dec->cur[-5]) << 32) 627 | (((UV)dec->cur[-5]) << 32)
628#endif
609 | (((UV)dec->cur[-4]) << 24) 629 | (((UV)dec->cur[-4]) << 24)
610 | (((UV)dec->cur[-3]) << 16) 630 | (((UV)dec->cur[-3]) << 16)
611 | (((UV)dec->cur[-2]) << 8) 631 | (((UV)dec->cur[-2]) << 8)
612 | ((UV)dec->cur[-1]); 632 | ((UV)dec->cur[-1]);
613 633
753{ 773{
754 SV *sv = 0; 774 SV *sv = 0;
755 775
756 if ((*dec->cur & 31) == 31) 776 if ((*dec->cur & 31) == 31)
757 { 777 {
778 // indefinite length strings
758 ++dec->cur; 779 ++dec->cur;
759 780
781 unsigned char major = *dec->cur & 0xe0;
782
760 sv = newSVpvn ("", 0); 783 sv = newSVpvn ("", 0);
761 784
762 // not very fast, and certainly not robust against illegal input
763 for (;;) 785 for (;;)
764 { 786 {
765 WANT (1); 787 WANT (1);
766 788
789 if ((*dec->cur ^ major) >= 31)
767 if (*dec->cur == (0xe0 | 31)) 790 if (*dec->cur == (0xe0 | 31))
768 { 791 {
769 ++dec->cur; 792 ++dec->cur;
770 break; 793 break;
771 } 794 }
795 else
796 ERR ("corrupted CBOR data (invalid chunks in indefinite length string)");
772 797
773 sv_catsv (sv, decode_sv (dec)); 798 STRLEN len = decode_uint (dec);
799
800 WANT (len);
801 sv_catpvn (sv, dec->cur, len);
802 dec->cur += len;
774 } 803 }
775 } 804 }
776 else 805 else
777 { 806 {
778 STRLEN len = decode_uint (dec); 807 STRLEN len = decode_uint (dec);
1145void shrink (CBOR *self, int enable = 1) 1174void shrink (CBOR *self, int enable = 1)
1146 ALIAS: 1175 ALIAS:
1147 shrink = F_SHRINK 1176 shrink = F_SHRINK
1148 allow_unknown = F_ALLOW_UNKNOWN 1177 allow_unknown = F_ALLOW_UNKNOWN
1149 allow_sharing = F_ALLOW_SHARING 1178 allow_sharing = F_ALLOW_SHARING
1150 allow_stringref = F_ALLOW_STRINGREF 1179 pack_strings = F_PACK_STRINGS
1151 PPCODE: 1180 PPCODE:
1152{ 1181{
1153 if (enable) 1182 if (enable)
1154 self->flags |= ix; 1183 self->flags |= ix;
1155 else 1184 else
1161void get_shrink (CBOR *self) 1190void get_shrink (CBOR *self)
1162 ALIAS: 1191 ALIAS:
1163 get_shrink = F_SHRINK 1192 get_shrink = F_SHRINK
1164 get_allow_unknown = F_ALLOW_UNKNOWN 1193 get_allow_unknown = F_ALLOW_UNKNOWN
1165 get_allow_sharing = F_ALLOW_SHARING 1194 get_allow_sharing = F_ALLOW_SHARING
1166 get_allow_stringref = F_ALLOW_STRINGREF 1195 get_pack_strings = F_PACK_STRINGS
1167 PPCODE: 1196 PPCODE:
1168 XPUSHs (boolSV (self->flags & ix)); 1197 XPUSHs (boolSV (self->flags & ix));
1169 1198
1170void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1199void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1171 PPCODE: 1200 PPCODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines