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.37 by root, Sat Nov 30 18:13:53 2013 UTC

23#endif 23#endif
24#ifndef SvREFCNT_dec_NN 24#ifndef SvREFCNT_dec_NN
25# define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) 25# define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv)
26#endif 26#endif
27 27
28// known major and minor types
29enum cbor_type
30{
31 MAJOR_SHIFT = 5,
32 MINOR_MASK = 0x1f,
33
34 MAJOR_POS_INT = 0 << MAJOR_SHIFT,
35 MAJOR_NEG_INT = 1 << MAJOR_SHIFT,
36 MAJOR_BYTES = 2 << MAJOR_SHIFT,
37 MAJOR_TEXT = 3 << MAJOR_SHIFT,
38 MAJOR_ARRAY = 4 << MAJOR_SHIFT,
39 MAJOR_MAP = 5 << MAJOR_SHIFT,
40 MAJOR_TAG = 6 << MAJOR_SHIFT,
41 MAJOR_MISC = 7 << MAJOR_SHIFT,
42
43 // INT/STRING/ARRAY/MAP subtypes
44 LENGTH_EXT1 = 24,
45 LENGTH_EXT2 = 25,
46 LENGTH_EXT4 = 26,
47 LENGTH_EXT8 = 27,
48
49 // SIMPLE types (effectively MISC subtypes)
50 SIMPLE_FALSE = 20,
51 SIMPLE_TRUE = 21,
52 SIMPLE_NULL = 22,
53 SIMPLE_UNDEF = 23,
54
55 // MISC subtype (unused)
56 MISC_EXT1 = 24,
57 MISC_FLOAT16 = 25,
58 MISC_FLOAT32 = 26,
59 MISC_FLOAT64 = 27,
60
61 // BYTES/TEXT/ARRAY/MAP
62 MINOR_INDEF = 31,
63};
64
28// known tags 65// known tags
29enum cbor_tag 66enum cbor_tag
30{ 67{
31 // inofficial extensions (pending iana registration) 68 // extensions
69 CBOR_TAG_STRINGREF = 25, // http://cbor.schmorp.de/stringref
32 CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object 70 CBOR_TAG_PERL_OBJECT = 26, // http://cbor.schmorp.de/perl-object
33 CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object 71 CBOR_TAG_GENERIC_OBJECT = 27, // http://cbor.schmorp.de/generic-object
34 CBOR_TAG_VALUE_SHAREABLE = 26, // http://cbor.schmorp.de/value-sharing 72 CBOR_TAG_VALUE_SHAREABLE = 28, // http://cbor.schmorp.de/value-sharing
35 CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing 73 CBOR_TAG_VALUE_SHAREDREF = 29, // http://cbor.schmorp.de/value-sharing
36 CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref 74 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 75 CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection
39 76
40 // rfc7049 77 // rfc7049
41 CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 78 CBOR_TAG_DATETIME = 0, // rfc4287, utf-8
42 CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any 79 CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any
43 CBOR_TAG_POS_BIGNUM = 2, // byte string 80 CBOR_TAG_POS_BIGNUM = 2, // byte string
44 CBOR_TAG_NEG_BIGNUM = 3, // byte string 81 CBOR_TAG_NEG_BIGNUM = 3, // byte string
45 CBOR_TAG_DECIMAL = 4, // decimal fraction, array 82 CBOR_TAG_DECIMAL = 4, // decimal fraction, array
46 CBOR_TAG_BIGFLOAT = 5, // array 83 CBOR_TAG_BIGFLOAT = 5, // array
47 84
48 CBOR_TAG_CONV_B64U = 21, // base64url, any 85 CBOR_TAG_CONV_B64U = 21, // base64url, any
49 CBOR_TAG_CONV_B64 = 22, // base64, any 86 CBOR_TAG_CONV_B64 = 22, // base64, any
50 CBOR_TAG_CONV_HEX = 23, // base16, any 87 CBOR_TAG_CONV_HEX = 23, // base16, any
51 CBOR_TAG_CBOR = 24, // embedded cbor, byte string 88 CBOR_TAG_CBOR = 24, // embedded cbor, byte string
52 89
53 CBOR_TAG_URI = 32, // URI rfc3986, utf-8 90 CBOR_TAG_URI = 32, // URI rfc3986, utf-8
54 CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 91 CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8
55 CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 92 CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8
56 CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 93 CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8
57 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 94 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8
58 95
59 CBOR_TAG_MAGIC = 55799 // self-describe cbor 96 CBOR_TAG_MAGIC = 55799, // self-describe cbor
60}; 97};
61 98
62#define F_SHRINK 0x00000001UL 99#define F_SHRINK 0x00000001UL
63#define F_ALLOW_UNKNOWN 0x00000002UL 100#define F_ALLOW_UNKNOWN 0x00000002UL
64#define F_ALLOW_SHARING 0x00000004UL //TODO 101#define F_ALLOW_SHARING 0x00000004UL
65#define F_ALLOW_STRINGREF 0x00000008UL //TODO 102#define F_ALLOW_CYCLES 0x00000008UL
103#define F_PACK_STRINGS 0x00000010UL
66 104
67#define INIT_SIZE 32 // initial scalar size to be allocated 105#define INIT_SIZE 32 // initial scalar size to be allocated
68 106
69#define SB do { 107#define SB do {
70#define SE } while (0) 108#define SE } while (0)
141{ 179{
142 return idx > 23 180 return idx > 23
143 ? idx > 0xffU 181 ? idx > 0xffU
144 ? idx > 0xffffU 182 ? idx > 0xffffU
145 ? idx > 0xffffffffU 183 ? idx > 0xffffffffU
184 ? 11
146 ? 7 185 : 7
147 : 6
148 : 5 186 : 5
149 : 4 187 : 4
150 : 3; 188 : 3;
151} 189}
152 190
189static void 227static void
190encode_uint (enc_t *enc, int major, UV len) 228encode_uint (enc_t *enc, int major, UV len)
191{ 229{
192 need (enc, 9); 230 need (enc, 9);
193 231
194 if (len < 24) 232 if (ecb_expect_true (len < LENGTH_EXT1))
195 *enc->cur++ = major | len; 233 *enc->cur++ = major | len;
196 else if (len <= 0xff) 234 else if (ecb_expect_true (len <= 0xffU))
197 { 235 {
198 *enc->cur++ = major | 24; 236 *enc->cur++ = major | LENGTH_EXT1;
199 *enc->cur++ = len; 237 *enc->cur++ = len;
200 } 238 }
201 else if (len <= 0xffff) 239 else if (len <= 0xffffU)
202 { 240 {
203 *enc->cur++ = major | 25; 241 *enc->cur++ = major | LENGTH_EXT2;
204 *enc->cur++ = len >> 8; 242 *enc->cur++ = len >> 8;
205 *enc->cur++ = len; 243 *enc->cur++ = len;
206 } 244 }
207 else if (len <= 0xffffffff) 245 else if (len <= 0xffffffffU)
208 { 246 {
209 *enc->cur++ = major | 26; 247 *enc->cur++ = major | LENGTH_EXT4;
210 *enc->cur++ = len >> 24; 248 *enc->cur++ = len >> 24;
211 *enc->cur++ = len >> 16; 249 *enc->cur++ = len >> 16;
212 *enc->cur++ = len >> 8; 250 *enc->cur++ = len >> 8;
213 *enc->cur++ = len; 251 *enc->cur++ = len;
214 } 252 }
215 else 253 else
216 { 254 {
217 *enc->cur++ = major | 27; 255 *enc->cur++ = major | LENGTH_EXT8;
218 *enc->cur++ = len >> 56; 256 *enc->cur++ = len >> 56;
219 *enc->cur++ = len >> 48; 257 *enc->cur++ = len >> 48;
220 *enc->cur++ = len >> 40; 258 *enc->cur++ = len >> 40;
221 *enc->cur++ = len >> 32; 259 *enc->cur++ = len >> 32;
222 *enc->cur++ = len >> 24; 260 *enc->cur++ = len >> 24;
227} 265}
228 266
229ecb_inline void 267ecb_inline void
230encode_tag (enc_t *enc, UV tag) 268encode_tag (enc_t *enc, UV tag)
231{ 269{
232 encode_uint (enc, 0xc0, tag); 270 encode_uint (enc, MAJOR_TAG, tag);
271}
272
273ecb_inline void
274encode_str (enc_t *enc, int utf8, char *str, STRLEN len)
275{
276 encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len);
277 need (enc, len);
278 memcpy (enc->cur, str, len);
279 enc->cur += len;
233} 280}
234 281
235static void 282static void
236encode_str (enc_t *enc, int utf8, char *str, STRLEN len) 283encode_strref (enc_t *enc, int utf8, char *str, STRLEN len)
237{ 284{
238 if (ecb_expect_false (enc->cbor.flags & F_ALLOW_STRINGREF)) 285 if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS))
239 { 286 {
240 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); 287 SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1);
241 288
242 if (SvOK (*svp)) 289 if (SvOK (*svp))
243 { 290 {
244 // already registered, use stringref 291 // already registered, use stringref
245 encode_tag (enc, CBOR_TAG_STRINGREF); 292 encode_tag (enc, CBOR_TAG_STRINGREF);
246 encode_uint (enc, 0x00, SvUV (*svp)); 293 encode_uint (enc, MAJOR_POS_INT, SvUV (*svp));
247 return; 294 return;
248 } 295 }
249 else if (len >= minimum_string_length (enc->stringref_idx)) 296 else if (len >= minimum_string_length (enc->stringref_idx))
250 { 297 {
251 // register only 298 // register only
252 sv_setuv (*svp, enc->stringref_idx); 299 sv_setuv (*svp, enc->stringref_idx);
253 ++enc->stringref_idx; 300 ++enc->stringref_idx;
254 } 301 }
255 } 302 }
256 303
257 encode_uint (enc, utf8 ? 0x60 : 0x40, len); 304 encode_str (enc, utf8, str, len);
258 need (enc, len);
259 memcpy (enc->cur, str, len);
260 enc->cur += len;
261} 305}
262 306
263static void encode_sv (enc_t *enc, SV *sv); 307static void encode_sv (enc_t *enc, SV *sv);
264 308
265static void 309static void
270 if (enc->depth >= enc->cbor.max_depth) 314 if (enc->depth >= enc->cbor.max_depth)
271 croak (ERR_NESTING_EXCEEDED); 315 croak (ERR_NESTING_EXCEEDED);
272 316
273 ++enc->depth; 317 ++enc->depth;
274 318
275 encode_uint (enc, 0x80, len + 1); 319 encode_uint (enc, MAJOR_ARRAY, len + 1);
276 320
277 for (i = 0; i <= len; ++i) 321 for (i = 0; i <= len; ++i)
278 { 322 {
279 SV **svp = av_fetch (av, i, 0); 323 SV **svp = av_fetch (av, i, 0);
280 encode_sv (enc, svp ? *svp : &PL_sv_undef); 324 encode_sv (enc, svp ? *svp : &PL_sv_undef);
295 339
296 int pairs = hv_iterinit (hv); 340 int pairs = hv_iterinit (hv);
297 int mg = SvMAGICAL (hv); 341 int mg = SvMAGICAL (hv);
298 342
299 if (mg) 343 if (mg)
300 encode_ch (enc, 0xa0 | 31); 344 encode_ch (enc, MAJOR_MAP | MINOR_INDEF);
301 else 345 else
302 encode_uint (enc, 0xa0, pairs); 346 encode_uint (enc, MAJOR_MAP, pairs);
303 347
304 while ((he = hv_iternext (hv))) 348 while ((he = hv_iternext (hv)))
305 { 349 {
306 if (HeKLEN (he) == HEf_SVKEY) 350 if (HeKLEN (he) == HEf_SVKEY)
307 encode_sv (enc, HeSVKEY (he)); 351 encode_sv (enc, HeSVKEY (he));
308 else 352 else
309 encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); 353 encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he));
310 354
311 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); 355 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
312 } 356 }
313 357
314 if (mg) 358 if (mg)
315 encode_ch (enc, 0xe0 | 31); 359 encode_ch (enc, MAJOR_MISC | MINOR_INDEF);
316 360
317 --enc->depth; 361 --enc->depth;
318} 362}
319 363
320// encode objects, arrays and special \0=false and \1=true values. 364// encode objects, arrays and special \0=false and \1=true values.
321static void 365static void
322encode_rv (enc_t *enc, SV *sv) 366encode_rv (enc_t *enc, SV *sv)
323{ 367{
324 SvGETMAGIC (sv); 368 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 369
348 svtype svt = SvTYPE (sv); 370 svtype svt = SvTYPE (sv);
349 371
350 if (ecb_expect_false (SvOBJECT (sv))) 372 if (ecb_expect_false (SvOBJECT (sv)))
351 { 373 {
358 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash 380 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
359 ? cbor_tagged_stash 381 ? cbor_tagged_stash
360 : gv_stashpv ("CBOR::XS::Tagged" , 1); 382 : gv_stashpv ("CBOR::XS::Tagged" , 1);
361 383
362 HV *stash = SvSTASH (sv); 384 HV *stash = SvSTASH (sv);
363 GV *method;
364 385
365 if (stash == boolean_stash) 386 if (stash == boolean_stash)
366 encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); 387 {
388 encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE);
389 return;
390 }
367 else if (stash == error_stash) 391 else if (stash == error_stash)
368 encode_ch (enc, 0xe0 | 23); 392 {
393 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF);
394 return;
395 }
369 else if (stash == tagged_stash) 396 else if (stash == tagged_stash)
370 { 397 {
371 if (svt != SVt_PVAV) 398 if (svt != SVt_PVAV)
372 croak ("encountered CBOR::XS::Tagged object that isn't an array"); 399 croak ("encountered CBOR::XS::Tagged object that isn't an array");
373 400
374 encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); 401 encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1)));
375 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); 402 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1));
403
404 return;
405 }
406 }
407
408 if (ecb_expect_false (SvREFCNT (sv) > 1)
409 && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING))
410 {
411 if (!enc->shareable)
412 enc->shareable = (HV *)sv_2mortal ((SV *)newHV ());
413
414 SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1);
415
416 if (SvOK (*svp))
376 } 417 {
418 encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF);
419 encode_uint (enc, MAJOR_POS_INT, SvUV (*svp));
420 return;
421 }
422 else
423 {
424 sv_setuv (*svp, enc->shareable_idx);
425 ++enc->shareable_idx;
426 encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE);
427 }
428 }
429
430 if (ecb_expect_false (SvOBJECT (sv)))
431 {
432 HV *stash = SvSTASH (sv);
433 GV *method;
434
377 else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) 435 if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
378 { 436 {
379 dSP; 437 dSP;
380 438
381 ENTER; SAVETMPS; PUSHMARK (SP); 439 ENTER; SAVETMPS; PUSHMARK (SP);
382 // we re-bless the reference to get overload and other niceties right 440 // we re-bless the reference to get overload and other niceties right
414 // catch this surprisingly common error 472 // catch this surprisingly common error
415 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) 473 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)); 474 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash));
417 475
418 encode_tag (enc, CBOR_TAG_PERL_OBJECT); 476 encode_tag (enc, CBOR_TAG_PERL_OBJECT);
419 encode_uint (enc, 0x80, count + 1); 477 encode_uint (enc, MAJOR_ARRAY, count + 1);
420 encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); 478 encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
421 479
422 while (count) 480 while (count)
423 encode_sv (enc, SP[1 - count--]); 481 encode_sv (enc, SP[1 - count--]);
424 482
425 PUTBACK; 483 PUTBACK;
446{ 504{
447 double nv = SvNVX (sv); 505 double nv = SvNVX (sv);
448 506
449 need (enc, 9); 507 need (enc, 9);
450 508
451 if (ecb_expect_false (nv == (U32)nv)) 509 if (ecb_expect_false (nv == (NV)(U32)nv))
452 encode_uint (enc, 0x00, (U32)nv); 510 encode_uint (enc, MAJOR_POS_INT, (U32)nv);
453 //TODO: maybe I32? 511 //TODO: maybe I32?
454 else if (ecb_expect_false (nv == (float)nv)) 512 else if (ecb_expect_false (nv == (float)nv))
455 { 513 {
456 uint32_t fp = ecb_float_to_binary32 (nv); 514 uint32_t fp = ecb_float_to_binary32 (nv);
457 515
458 *enc->cur++ = 0xe0 | 26; 516 *enc->cur++ = MAJOR_MISC | MISC_FLOAT32;
459 517
460 if (!ecb_big_endian ()) 518 if (!ecb_big_endian ())
461 fp = ecb_bswap32 (fp); 519 fp = ecb_bswap32 (fp);
462 520
463 memcpy (enc->cur, &fp, 4); 521 memcpy (enc->cur, &fp, 4);
465 } 523 }
466 else 524 else
467 { 525 {
468 uint64_t fp = ecb_double_to_binary64 (nv); 526 uint64_t fp = ecb_double_to_binary64 (nv);
469 527
470 *enc->cur++ = 0xe0 | 27; 528 *enc->cur++ = MAJOR_MISC | MISC_FLOAT64;
471 529
472 if (!ecb_big_endian ()) 530 if (!ecb_big_endian ())
473 fp = ecb_bswap64 (fp); 531 fp = ecb_bswap64 (fp);
474 532
475 memcpy (enc->cur, &fp, 8); 533 memcpy (enc->cur, &fp, 8);
484 542
485 if (SvPOKp (sv)) 543 if (SvPOKp (sv))
486 { 544 {
487 STRLEN len; 545 STRLEN len;
488 char *str = SvPV (sv, len); 546 char *str = SvPV (sv, len);
489 encode_str (enc, SvUTF8 (sv), str, len); 547 encode_strref (enc, SvUTF8 (sv), str, len);
490 } 548 }
491 else if (SvNOKp (sv)) 549 else if (SvNOKp (sv))
492 encode_nv (enc, sv); 550 encode_nv (enc, sv);
493 else if (SvIOKp (sv)) 551 else if (SvIOKp (sv))
494 { 552 {
495 if (SvIsUV (sv)) 553 if (SvIsUV (sv))
496 encode_uint (enc, 0x00, SvUVX (sv)); 554 encode_uint (enc, MAJOR_POS_INT, SvUVX (sv));
497 else if (SvIVX (sv) >= 0) 555 else if (SvIVX (sv) >= 0)
498 encode_uint (enc, 0x00, SvIVX (sv)); 556 encode_uint (enc, MAJOR_POS_INT, SvIVX (sv));
499 else 557 else
500 encode_uint (enc, 0x20, -(SvIVX (sv) + 1)); 558 encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1));
501 } 559 }
502 else if (SvROK (sv)) 560 else if (SvROK (sv))
503 encode_rv (enc, SvRV (sv)); 561 encode_rv (enc, SvRV (sv));
504 else if (!SvOK (sv)) 562 else if (!SvOK (sv))
505 encode_ch (enc, 0xe0 | 22); 563 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL);
506 else if (enc->cbor.flags & F_ALLOW_UNKNOWN) 564 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)
507 encode_ch (enc, 0xe0 | 23); 565 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF);
508 else 566 else
509 croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data", 567 croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data",
510 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); 568 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv));
511} 569}
512 570
520 enc.cur = SvPVX (enc.sv); 578 enc.cur = SvPVX (enc.sv);
521 enc.end = SvEND (enc.sv); 579 enc.end = SvEND (enc.sv);
522 580
523 SvPOK_only (enc.sv); 581 SvPOK_only (enc.sv);
524 582
525 if (cbor->flags & F_ALLOW_STRINGREF) 583 if (cbor->flags & F_PACK_STRINGS)
526 { 584 {
527 encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); 585 encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE);
528 enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); 586 enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ());
529 enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); 587 enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ());
530 } 588 }
565#define DEC_DEC_DEPTH --dec->depth 623#define DEC_DEC_DEPTH --dec->depth
566 624
567static UV 625static UV
568decode_uint (dec_t *dec) 626decode_uint (dec_t *dec)
569{ 627{
570 switch (*dec->cur & 31) 628 U8 m = *dec->cur & MINOR_MASK;
571 { 629 ++dec->cur;
572 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
573 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15:
574 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
575 return *dec->cur++ & 31;
576 630
577 case 24: 631 if (ecb_expect_true (m < LENGTH_EXT1))
632 return m;
633 else if (ecb_expect_true (m == LENGTH_EXT1))
634 {
578 WANT (2); 635 WANT (1);
579 dec->cur += 2; 636 dec->cur += 1;
580 return dec->cur[-1]; 637 return dec->cur[-1];
581 638 }
582 case 25: 639 else if (ecb_expect_true (m == LENGTH_EXT2))
640 {
583 WANT (3); 641 WANT (2);
584 dec->cur += 3; 642 dec->cur += 2;
585 return (((UV)dec->cur[-2]) << 8) 643 return (((UV)dec->cur[-2]) << 8)
586 | ((UV)dec->cur[-1]); 644 | ((UV)dec->cur[-1]);
587 645 }
588 case 26: 646 else if (ecb_expect_true (m == LENGTH_EXT4))
647 {
589 WANT (5); 648 WANT (4);
590 dec->cur += 5; 649 dec->cur += 4;
591 return (((UV)dec->cur[-4]) << 24) 650 return (((UV)dec->cur[-4]) << 24)
592 | (((UV)dec->cur[-3]) << 16) 651 | (((UV)dec->cur[-3]) << 16)
593 | (((UV)dec->cur[-2]) << 8) 652 | (((UV)dec->cur[-2]) << 8)
594 | ((UV)dec->cur[-1]); 653 | ((UV)dec->cur[-1]);
595 654 }
596 case 27: 655 else if (ecb_expect_true (m == LENGTH_EXT8))
656 {
597 WANT (9); 657 WANT (8);
598 dec->cur += 9; 658 dec->cur += 8;
659
660 return
661#if UVSIZE < 8
662 0
663#else
599 return (((UV)dec->cur[-8]) << 56) 664 (((UV)dec->cur[-8]) << 56)
600 | (((UV)dec->cur[-7]) << 48) 665 | (((UV)dec->cur[-7]) << 48)
601 | (((UV)dec->cur[-6]) << 40) 666 | (((UV)dec->cur[-6]) << 40)
602 | (((UV)dec->cur[-5]) << 32) 667 | (((UV)dec->cur[-5]) << 32)
668#endif
603 | (((UV)dec->cur[-4]) << 24) 669 | (((UV)dec->cur[-4]) << 24)
604 | (((UV)dec->cur[-3]) << 16) 670 | (((UV)dec->cur[-3]) << 16)
605 | (((UV)dec->cur[-2]) << 8) 671 | (((UV)dec->cur[-2]) << 8)
606 | ((UV)dec->cur[-1]); 672 | ((UV)dec->cur[-1]);
607 673 }
608 default: 674 else
609 ERR ("corrupted CBOR data (unsupported integer minor encoding)"); 675 ERR ("corrupted CBOR data (unsupported integer minor encoding)");
610 }
611 676
612fail: 677fail:
613 return 0; 678 return 0;
614} 679}
615 680
620{ 685{
621 AV *av = newAV (); 686 AV *av = newAV ();
622 687
623 DEC_INC_DEPTH; 688 DEC_INC_DEPTH;
624 689
625 if ((*dec->cur & 31) == 31) 690 if (*dec->cur == (MAJOR_ARRAY | MINOR_INDEF))
626 { 691 {
627 ++dec->cur; 692 ++dec->cur;
628 693
629 for (;;) 694 for (;;)
630 { 695 {
631 WANT (1); 696 WANT (1);
632 697
633 if (*dec->cur == (0xe0 | 31)) 698 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF))
634 { 699 {
635 ++dec->cur; 700 ++dec->cur;
636 break; 701 break;
637 } 702 }
638 703
641 } 706 }
642 else 707 else
643 { 708 {
644 int i, len = decode_uint (dec); 709 int i, len = decode_uint (dec);
645 710
711 WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays
646 av_fill (av, len - 1); 712 av_fill (av, len - 1);
647 713
648 for (i = 0; i < len; ++i) 714 for (i = 0; i < len; ++i)
649 AvARRAY (av)[i] = decode_sv (dec); 715 AvARRAY (av)[i] = decode_sv (dec);
650 } 716 }
663{ 729{
664 // for speed reasons, we specialcase single-string 730 // for speed reasons, we specialcase single-string
665 // byte or utf-8 strings as keys, but only when !stringref 731 // byte or utf-8 strings as keys, but only when !stringref
666 732
667 if (ecb_expect_true (!dec->stringref)) 733 if (ecb_expect_true (!dec->stringref))
668 if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) 734 if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8))
669 { 735 {
670 I32 len = decode_uint (dec); 736 I32 len = decode_uint (dec);
671 char *key = (char *)dec->cur; 737 char *key = (char *)dec->cur;
672 738
673 dec->cur += len; 739 dec->cur += len;
677 743
678 hv_store (hv, key, len, decode_sv (dec), 0); 744 hv_store (hv, key, len, decode_sv (dec), 0);
679 745
680 return; 746 return;
681 } 747 }
682 else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) 748 else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8))
683 { 749 {
684 I32 len = decode_uint (dec); 750 I32 len = decode_uint (dec);
685 char *key = (char *)dec->cur; 751 char *key = (char *)dec->cur;
686 752
687 dec->cur += len; 753 dec->cur += len;
706{ 772{
707 HV *hv = newHV (); 773 HV *hv = newHV ();
708 774
709 DEC_INC_DEPTH; 775 DEC_INC_DEPTH;
710 776
711 if ((*dec->cur & 31) == 31) 777 if (*dec->cur == (MAJOR_MAP | MINOR_INDEF))
712 { 778 {
713 ++dec->cur; 779 ++dec->cur;
714 780
715 for (;;) 781 for (;;)
716 { 782 {
717 WANT (1); 783 WANT (1);
718 784
719 if (*dec->cur == (0xe0 | 31)) 785 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF))
720 { 786 {
721 ++dec->cur; 787 ++dec->cur;
722 break; 788 break;
723 } 789 }
724 790
745static SV * 811static SV *
746decode_str (dec_t *dec, int utf8) 812decode_str (dec_t *dec, int utf8)
747{ 813{
748 SV *sv = 0; 814 SV *sv = 0;
749 815
750 if ((*dec->cur & 31) == 31) 816 if ((*dec->cur & MINOR_MASK) == MINOR_INDEF)
751 { 817 {
818 // indefinite length strings
752 ++dec->cur; 819 ++dec->cur;
753 820
821 U8 major = *dec->cur & MAJOR_MISC;
822
754 sv = newSVpvn ("", 0); 823 sv = newSVpvn ("", 0);
755 824
756 // not very fast, and certainly not robust against illegal input
757 for (;;) 825 for (;;)
758 { 826 {
759 WANT (1); 827 WANT (1);
760 828
761 if (*dec->cur == (0xe0 | 31)) 829 if ((*dec->cur - major) > LENGTH_EXT8)
830 if (*dec->cur == (MAJOR_MISC | MINOR_INDEF))
762 { 831 {
763 ++dec->cur; 832 ++dec->cur;
764 break; 833 break;
765 } 834 }
835 else
836 ERR ("corrupted CBOR data (invalid chunks in indefinite length string)");
766 837
767 sv_catsv (sv, decode_sv (dec)); 838 STRLEN len = decode_uint (dec);
839
840 WANT (len);
841 sv_catpvn (sv, dec->cur, len);
842 dec->cur += len;
768 } 843 }
769 } 844 }
770 else 845 else
771 { 846 {
772 STRLEN len = decode_uint (dec); 847 STRLEN len = decode_uint (dec);
821 } 896 }
822 break; 897 break;
823 898
824 case CBOR_TAG_STRINGREF: 899 case CBOR_TAG_STRINGREF:
825 { 900 {
826 if ((*dec->cur >> 5) != 0) 901 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT))
827 ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); 902 ERR ("corrupted CBOR data (stringref index not an unsigned integer)");
828 903
829 UV idx = decode_uint (dec); 904 UV idx = decode_uint (dec);
830 905
831 if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) 906 if (!dec->stringref || (int)idx > AvFILLp (dec->stringref))
838 case CBOR_TAG_VALUE_SHAREABLE: 913 case CBOR_TAG_VALUE_SHAREABLE:
839 { 914 {
840 if (ecb_expect_false (!dec->shareable)) 915 if (ecb_expect_false (!dec->shareable))
841 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); 916 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
842 917
918 if (dec->cbor.flags & F_ALLOW_CYCLES)
919 {
843 sv = newSV (0); 920 sv = newSV (0);
844 av_push (dec->shareable, SvREFCNT_inc_NN (sv)); 921 av_push (dec->shareable, SvREFCNT_inc_NN (sv));
845 922
846 SV *osv = decode_sv (dec); 923 SV *osv = decode_sv (dec);
847 sv_setsv (sv, osv); 924 sv_setsv (sv, osv);
848 SvREFCNT_dec_NN (osv); 925 SvREFCNT_dec_NN (osv);
926 }
927 else
928 {
929 av_push (dec->shareable, &PL_sv_undef);
930 int idx = AvFILLp (dec->shareable);
931 sv = decode_sv (dec);
932 av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv));
933 }
849 } 934 }
850 break; 935 break;
851 936
852 case CBOR_TAG_VALUE_SHAREDREF: 937 case CBOR_TAG_VALUE_SHAREDREF:
853 { 938 {
854 if ((*dec->cur >> 5) != 0) 939 if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT))
855 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); 940 ERR ("corrupted CBOR data (sharedref index not an unsigned integer)");
856 941
857 UV idx = decode_uint (dec); 942 UV idx = decode_uint (dec);
858 943
859 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) 944 if (!dec->shareable || (int)idx > AvFILLp (dec->shareable))
860 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 945 ERR ("corrupted CBOR data (sharedref index out of bounds)");
861 946
862 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); 947 sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]);
948
949 if (sv == &PL_sv_undef)
950 ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled");
863 } 951 }
864 break; 952 break;
865 953
866 case CBOR_TAG_PERL_OBJECT: 954 case CBOR_TAG_PERL_OBJECT:
867 { 955 {
968static SV * 1056static SV *
969decode_sv (dec_t *dec) 1057decode_sv (dec_t *dec)
970{ 1058{
971 WANT (1); 1059 WANT (1);
972 1060
973 switch (*dec->cur >> 5) 1061 switch (*dec->cur >> MAJOR_SHIFT)
974 { 1062 {
975 case 0: // unsigned int 1063 case MAJOR_POS_INT >> MAJOR_SHIFT: return newSVuv (decode_uint (dec));
976 return newSVuv (decode_uint (dec)); 1064 case MAJOR_NEG_INT >> MAJOR_SHIFT: return newSViv (-1 - (IV)decode_uint (dec));
977 case 1: // negative int 1065 case MAJOR_BYTES >> MAJOR_SHIFT: return decode_str (dec, 0);
978 return newSViv (-1 - (IV)decode_uint (dec)); 1066 case MAJOR_TEXT >> MAJOR_SHIFT: return decode_str (dec, 1);
979 case 2: // octet string 1067 case MAJOR_ARRAY >> MAJOR_SHIFT: return decode_av (dec);
980 return decode_str (dec, 0); 1068 case MAJOR_MAP >> MAJOR_SHIFT: return decode_hv (dec);
981 case 3: // utf-8 string 1069 case MAJOR_TAG >> MAJOR_SHIFT: return decode_tagged (dec);
982 return decode_str (dec, 1); 1070
983 case 4: // array 1071 case MAJOR_MISC >> MAJOR_SHIFT:
984 return decode_av (dec);
985 case 5: // map
986 return decode_hv (dec);
987 case 6: // tag
988 return decode_tagged (dec);
989 case 7: // misc
990 switch (*dec->cur++ & 31) 1072 switch (*dec->cur++ & MINOR_MASK)
991 { 1073 {
992 case 20: 1074 case SIMPLE_FALSE:
993#if CBOR_SLOW 1075#if CBOR_SLOW
994 types_false = get_bool ("Types::Serialiser::false"); 1076 types_false = get_bool ("Types::Serialiser::false");
995#endif 1077#endif
996 return newSVsv (types_false); 1078 return newSVsv (types_false);
997 case 21: 1079 case SIMPLE_TRUE:
998#if CBOR_SLOW 1080#if CBOR_SLOW
999 types_true = get_bool ("Types::Serialiser::true"); 1081 types_true = get_bool ("Types::Serialiser::true");
1000#endif 1082#endif
1001 return newSVsv (types_true); 1083 return newSVsv (types_true);
1002 case 22: 1084 case SIMPLE_NULL:
1003 return newSVsv (&PL_sv_undef); 1085 return newSVsv (&PL_sv_undef);
1004 case 23: 1086 case SIMPLE_UNDEF:
1005#if CBOR_SLOW 1087#if CBOR_SLOW
1006 types_error = get_bool ("Types::Serialiser::error"); 1088 types_error = get_bool ("Types::Serialiser::error");
1007#endif 1089#endif
1008 return newSVsv (types_error); 1090 return newSVsv (types_error);
1009 1091
1010 case 25: 1092 case MISC_FLOAT16:
1011 { 1093 {
1012 WANT (2); 1094 WANT (2);
1013 1095
1014 uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; 1096 uint16_t fp = (dec->cur[0] << 8) | dec->cur[1];
1015 dec->cur += 2; 1097 dec->cur += 2;
1016 1098
1017 return newSVnv (ecb_binary16_to_float (fp)); 1099 return newSVnv (ecb_binary16_to_float (fp));
1018 } 1100 }
1019 1101
1020 case 26: 1102 case MISC_FLOAT32:
1021 { 1103 {
1022 uint32_t fp; 1104 uint32_t fp;
1023 WANT (4); 1105 WANT (4);
1024 memcpy (&fp, dec->cur, 4); 1106 memcpy (&fp, dec->cur, 4);
1025 dec->cur += 4; 1107 dec->cur += 4;
1028 fp = ecb_bswap32 (fp); 1110 fp = ecb_bswap32 (fp);
1029 1111
1030 return newSVnv (ecb_binary32_to_float (fp)); 1112 return newSVnv (ecb_binary32_to_float (fp));
1031 } 1113 }
1032 1114
1033 case 27: 1115 case MISC_FLOAT64:
1034 { 1116 {
1035 uint64_t fp; 1117 uint64_t fp;
1036 WANT (8); 1118 WANT (8);
1037 memcpy (&fp, dec->cur, 8); 1119 memcpy (&fp, dec->cur, 8);
1038 dec->cur += 8; 1120 dec->cur += 8;
1041 fp = ecb_bswap64 (fp); 1123 fp = ecb_bswap64 (fp);
1042 1124
1043 return newSVnv (ecb_binary64_to_double (fp)); 1125 return newSVnv (ecb_binary64_to_double (fp));
1044 } 1126 }
1045 1127
1046 // 0..19 unassigned 1128 // 0..19 unassigned simple
1047 // 24 reserved + unassigned (reserved values are not encodable) 1129 // 24 reserved + unassigned (reserved values are not encodable)
1048 default: 1130 default:
1049 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 1131 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)");
1050 } 1132 }
1051 1133
1139void shrink (CBOR *self, int enable = 1) 1221void shrink (CBOR *self, int enable = 1)
1140 ALIAS: 1222 ALIAS:
1141 shrink = F_SHRINK 1223 shrink = F_SHRINK
1142 allow_unknown = F_ALLOW_UNKNOWN 1224 allow_unknown = F_ALLOW_UNKNOWN
1143 allow_sharing = F_ALLOW_SHARING 1225 allow_sharing = F_ALLOW_SHARING
1144 allow_stringref = F_ALLOW_STRINGREF 1226 allow_cycles = F_ALLOW_CYCLES
1227 pack_strings = F_PACK_STRINGS
1145 PPCODE: 1228 PPCODE:
1146{ 1229{
1147 if (enable) 1230 if (enable)
1148 self->flags |= ix; 1231 self->flags |= ix;
1149 else 1232 else
1155void get_shrink (CBOR *self) 1238void get_shrink (CBOR *self)
1156 ALIAS: 1239 ALIAS:
1157 get_shrink = F_SHRINK 1240 get_shrink = F_SHRINK
1158 get_allow_unknown = F_ALLOW_UNKNOWN 1241 get_allow_unknown = F_ALLOW_UNKNOWN
1159 get_allow_sharing = F_ALLOW_SHARING 1242 get_allow_sharing = F_ALLOW_SHARING
1160 get_allow_stringref = F_ALLOW_STRINGREF 1243 get_allow_cycles = F_ALLOW_CYCLES
1244 get_pack_strings = F_PACK_STRINGS
1161 PPCODE: 1245 PPCODE:
1162 XPUSHs (boolSV (self->flags & ix)); 1246 XPUSHs (boolSV (self->flags & ix));
1163 1247
1164void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) 1248void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
1165 PPCODE: 1249 PPCODE:
1221 cbor_free (self); 1305 cbor_free (self);
1222 1306
1223PROTOTYPES: ENABLE 1307PROTOTYPES: ENABLE
1224 1308
1225void encode_cbor (SV *scalar) 1309void encode_cbor (SV *scalar)
1310 ALIAS:
1311 encode_cbor = 0
1312 encode_cbor_sharing = F_ALLOW_SHARING
1226 PPCODE: 1313 PPCODE:
1227{ 1314{
1228 CBOR cbor; 1315 CBOR cbor;
1229 cbor_init (&cbor); 1316 cbor_init (&cbor);
1317 cbor.flags |= ix;
1230 PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; 1318 PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN;
1231 XPUSHs (scalar); 1319 XPUSHs (scalar);
1232} 1320}
1233 1321
1234void decode_cbor (SV *cborstr) 1322void decode_cbor (SV *cborstr)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines