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.28 by root, Sat Nov 23 18:30:59 2013 UTC vs.
Revision 1.33 by root, Sat Nov 30 15:23:59 2013 UTC

26#endif 26#endif
27 27
28// known tags 28// known tags
29enum cbor_tag 29enum cbor_tag
30{ 30{
31 // inofficial extensions (pending iana registration) 31 // extensions
32 CBOR_TAG_STRINGREF = 25, // http://cbor.schmorp.de/stringref
32 CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object 33 CBOR_TAG_PERL_OBJECT = 26, // http://cbor.schmorp.de/perl-object
33 CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object 34 CBOR_TAG_GENERIC_OBJECT = 27, // http://cbor.schmorp.de/generic-object
34 CBOR_TAG_VALUE_SHAREABLE = 26, // http://cbor.schmorp.de/value-sharing 35 CBOR_TAG_VALUE_SHAREABLE = 28, // http://cbor.schmorp.de/value-sharing
35 CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing 36 CBOR_TAG_VALUE_SHAREDREF = 29, // http://cbor.schmorp.de/value-sharing
36 CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref 37 CBOR_TAG_STRINGREF_NAMESPACE = 256, // http://cbor.schmorp.de/stringref
37 CBOR_TAG_STRINGREF = 28, // http://cbor.schmorp.de/stringref
38 CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection 38 CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection
39 39
40 // rfc7049 40 // rfc7049
41 CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 41 CBOR_TAG_DATETIME = 0, // rfc4287, utf-8
42 CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any 42 CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any
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)
141{ 141{
142 return idx > 23 142 return idx > 23
143 ? idx > 0xffU 143 ? idx > 0xffU
144 ? idx > 0xffffU 144 ? idx > 0xffffU
145 ? idx > 0xffffffffU 145 ? idx > 0xffffffffU
146 ? 11
146 ? 7 147 : 7
147 : 6
148 : 5 148 : 5
149 : 4 149 : 4
150 : 3; 150 : 3;
151} 151}
152 152
189static void 189static void
190encode_uint (enc_t *enc, int major, UV len) 190encode_uint (enc_t *enc, int major, UV len)
191{ 191{
192 need (enc, 9); 192 need (enc, 9);
193 193
194 if (len < 24) 194 if (ecb_expect_true (len < 24))
195 *enc->cur++ = major | len; 195 *enc->cur++ = major | len;
196 else if (len <= 0xff) 196 else if (ecb_expect_true (len <= 0xff))
197 { 197 {
198 *enc->cur++ = major | 24; 198 *enc->cur++ = major | 24;
199 *enc->cur++ = len; 199 *enc->cur++ = len;
200 } 200 }
201 else if (len <= 0xffff) 201 else if (len <= 0xffff)
230encode_tag (enc_t *enc, UV tag) 230encode_tag (enc_t *enc, UV tag)
231{ 231{
232 encode_uint (enc, 0xc0, tag); 232 encode_uint (enc, 0xc0, tag);
233} 233}
234 234
235ecb_inline void
236encode_str (enc_t *enc, int utf8, char *str, STRLEN len)
237{
238 encode_uint (enc, utf8 ? 0x60 : 0x40, len);
239 need (enc, len);
240 memcpy (enc->cur, str, len);
241 enc->cur += len;
242}
243
235static void 244static void
236encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 245encode_strref (enc_t *enc, int utf8, char *str, STRLEN len)
237{ 246{
238 if (ecb_expect_false (enc->cbor.flags & F_ALLOW_STRINGREF)) 247 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS))
239 { 248 {
240 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); 249 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
241 250
242 if (SvOK (*svp)) 251 if (SvOK (*svp))
243 { 252 {
252 sv_setuv (*svp, enc->stringref_idx); 261 sv_setuv (*svp, enc->stringref_idx);
253 ++enc->stringref_idx; 262 ++enc->stringref_idx;
254 } 263 }
255 } 264 }
256 265
257 encode_uint (enc, utf8 ? 0x60 : 0x40, len); 266 encode_str (enc, utf8, str, len);
258 need (enc, len);
259 memcpy (enc->cur, str, len);
260 enc->cur += len;
261} 267}
262 268
263static void encode_sv (enc_t *enc, SV *sv); 269static void encode_sv (enc_t *enc, SV *sv);
264 270
265static void 271static void
304 while ((he = hv_iternext (hv))) 310 while ((he = hv_iternext (hv)))
305 { 311 {
306 if (HeKLEN (he) == HEf_SVKEY) 312 if (HeKLEN (he) == HEf_SVKEY)
307 encode_sv (enc, HeSVKEY (he)); 313 encode_sv (enc, HeSVKEY (he));
308 else 314 else
309 encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); 315 encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he));
310 316
311 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); 317 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
312 } 318 }
313 319
314 if (mg) 320 if (mg)
320// encode objects, arrays and special \0=false and \1=true values. 326// encode objects, arrays and special \0=false and \1=true values.
321static void 327static void
322encode_rv (enc_t *enc, SV *sv) 328encode_rv (enc_t *enc, SV *sv)
323{ 329{
324 SvGETMAGIC (sv); 330 SvGETMAGIC (sv);
325
326 if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)
327 && ecb_expect_false (SvREFCNT (sv) > 1))
328 {
329 if (!enc->shareable)
330 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ());
331
332 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1);
333
334 if (SvOK (*svp))
335 {
336 encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF);
337 encode_uint (enc, 0x00, SvUV (*svp));
338 return;
339 }
340 else
341 {
342 sv_setuv (*svp, enc->shareable_idx);
343 ++enc->shareable_idx;
344 encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE);
345 }
346 }
347 331
348 svtype svt = SvTYPE (sv); 332 svtype svt = SvTYPE (sv);
349 333
350 if (ecb_expect_false (SvOBJECT (sv))) 334 if (ecb_expect_false (SvOBJECT (sv)))
351 { 335 {
415 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) 399 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv)
416 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); 400 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash));
417 401
418 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 402 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
419 encode_uint (enc, 0x80, count + 1); 403 encode_uint (enc, 0x80, count + 1);
420 encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 404 encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
421 405
422 while (count) 406 while (count)
423 encode_sv (enc, SP[1 - count--]); 407 encode_sv (enc, SP[1 - count--]);
424 408
425 PUTBACK; 409 PUTBACK;
434 encode_hv (enc, (HV *)sv); 418 encode_hv (enc, (HV *)sv);
435 else if (svt == SVt_PVAV) 419 else if (svt == SVt_PVAV)
436 encode_av (enc, (AV *)sv); 420 encode_av (enc, (AV *)sv);
437 else 421 else
438 { 422 {
423 if (ecb_expect_false (SvREFCNT (sv) > 1)
424 && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING))
425 {
426 if (!enc->shareable)
427 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ());
428
429 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1);
430
431 if (SvOK (*svp))
432 {
433 encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF);
434 encode_uint (enc, 0x00, SvUV (*svp));
435 return;
436 }
437 else
438 {
439 sv_setuv (*svp, enc->shareable_idx);
440 ++enc->shareable_idx;
441 encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE);
442 }
443 }
444
439 encode_tag (enc, CBOR_TAG_INDIRECTION); 445 encode_tag (enc, CBOR_TAG_INDIRECTION);
440 encode_sv (enc, sv); 446 encode_sv (enc, sv);
441 } 447 }
442} 448}
443 449
484 490
485 if (SvPOKp (sv)) 491 if (SvPOKp (sv))
486 { 492 {
487 STRLEN len; 493 STRLEN len;
488 char *str = SvPV (sv, len); 494 char *str = SvPV (sv, len);
489 encode_str (enc, SvUTF8 (sv), str, len); 495 encode_strref (enc, SvUTF8 (sv), str, len);
490 } 496 }
491 else if (SvNOKp (sv)) 497 else if (SvNOKp (sv))
492 encode_nv (enc, sv); 498 encode_nv (enc, sv);
493 else if (SvIOKp (sv)) 499 else if (SvIOKp (sv))
494 { 500 {
520 enc.cur = SvPVX (enc.sv); 526 enc.cur = SvPVX (enc.sv);
521 enc.end = SvEND (enc.sv); 527 enc.end = SvEND (enc.sv);
522 528
523 SvPOK_only (enc.sv); 529 SvPOK_only (enc.sv);
524 530
525 if (cbor->flags & F_ALLOW_STRINGREF) 531 if (cbor->flags & F_PACK_STRINGS)
526 { 532 {
527 encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); 533 encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE);
528 enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); 534 enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ());
529 enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); 535 enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ());
530 } 536 }
747{ 753{
748 SV *sv = 0; 754 SV *sv = 0;
749 755
750 if ((*dec->cur & 31) == 31) 756 if ((*dec->cur & 31) == 31)
751 { 757 {
758 // indefinite length strings
752 ++dec->cur; 759 ++dec->cur;
753 760
761 unsigned char major = *dec->cur & 0xe0;
762
754 sv = newSVpvn ("", 0); 763 sv = newSVpvn ("", 0);
755 764
756 // not very fast, and certainly not robust against illegal input
757 for (;;) 765 for (;;)
758 { 766 {
759 WANT (1); 767 WANT (1);
760 768
769 if ((*dec->cur ^ major) >= 31)
761 if (*dec->cur == (0xe0 | 31)) 770 if (*dec->cur == (0xe0 | 31))
762 { 771 {
763 ++dec->cur; 772 ++dec->cur;
764 break; 773 break;
765 } 774 }
775 else
776 ERR ("corrupted CBOR data (invalid chunks in indefinite length string)");
766 777
767 sv_catsv (sv, decode_sv (dec)); 778 STRLEN len = decode_uint (dec);
779
780 WANT (len);
781 sv_catpvn (sv, dec->cur, len);
782 dec->cur += len;
768 } 783 }
769 } 784 }
770 else 785 else
771 { 786 {
772 STRLEN len = decode_uint (dec); 787 STRLEN len = decode_uint (dec);
1139void shrink (CBOR *self, int enable = 1) 1154void shrink (CBOR *self, int enable = 1)
1140 ALIAS: 1155 ALIAS:
1141 shrink = F_SHRINK 1156 shrink = F_SHRINK
1142 allow_unknown = F_ALLOW_UNKNOWN 1157 allow_unknown = F_ALLOW_UNKNOWN
1143 allow_sharing = F_ALLOW_SHARING 1158 allow_sharing = F_ALLOW_SHARING
1144 allow_stringref = F_ALLOW_STRINGREF 1159 pack_strings = F_PACK_STRINGS
1145 PPCODE: 1160 PPCODE:
1146{ 1161{
1147 if (enable) 1162 if (enable)
1148 self->flags |= ix; 1163 self->flags |= ix;
1149 else 1164 else
1155void get_shrink (CBOR *self) 1170void get_shrink (CBOR *self)
1156 ALIAS: 1171 ALIAS:
1157 get_shrink = F_SHRINK 1172 get_shrink = F_SHRINK
1158 get_allow_unknown = F_ALLOW_UNKNOWN 1173 get_allow_unknown = F_ALLOW_UNKNOWN
1159 get_allow_sharing = F_ALLOW_SHARING 1174 get_allow_sharing = F_ALLOW_SHARING
1160 get_allow_stringref = F_ALLOW_STRINGREF 1175 get_pack_strings = F_PACK_STRINGS
1161 PPCODE: 1176 PPCODE:
1162 XPUSHs (boolSV (self->flags & ix)); 1177 XPUSHs (boolSV (self->flags & ix));
1163 1178
1164void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1179void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1165 PPCODE: 1180 PPCODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines