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

Comparing Convert-BER-XS/XS.xs (file contents):
Revision 1.28 by root, Tue Apr 23 17:55:54 2019 UTC vs.
Revision 1.32 by root, Tue Apr 23 21:20:25 2019 UTC

65 SNMP_TIMETICKS = 0x03, 65 SNMP_TIMETICKS = 0x03,
66 SNMP_OPAQUE = 0x04, 66 SNMP_OPAQUE = 0x04,
67 SNMP_COUNTER64 = 0x06, 67 SNMP_COUNTER64 = 0x06,
68}; 68};
69 69
70// tlow-level types this module can ecode the above (and more) into
70enum { 71enum {
71 BER_TYPE_BYTES, 72 BER_TYPE_BYTES,
72 BER_TYPE_UTF8, 73 BER_TYPE_UTF8,
73 BER_TYPE_UCS2, 74 BER_TYPE_UCS2,
74 BER_TYPE_UCS4, 75 BER_TYPE_UCS4,
80 BER_TYPE_REAL, 81 BER_TYPE_REAL,
81 BER_TYPE_IPADDRESS, 82 BER_TYPE_IPADDRESS,
82 BER_TYPE_CROAK, 83 BER_TYPE_CROAK,
83}; 84};
84 85
86// tuple array indices
85enum { 87enum {
86 BER_CLASS = 0, 88 BER_CLASS = 0,
87 BER_TAG = 1, 89 BER_TAG = 1,
88 BER_FLAGS = 2, 90 BER_FLAGS = 2,
89 BER_DATA = 3, 91 BER_DATA = 3,
265 if (expect_false (res >> UVSIZE * 8 - 7)) 267 if (expect_false (res >> UVSIZE * 8 - 7))
266 error ("BER variable length integer overflow"); 268 error ("BER variable length integer overflow");
267 269
268 res = (res << 7) | (c & 0x7f); 270 res = (res << 7) | (c & 0x7f);
269 271
270 if (!(c & 0x80)) 272 if (expect_true (!(c & 0x80)))
271 return res; 273 return res;
272 274
273 c = get_u8 (); 275 c = get_u8 ();
274 } 276 }
275} 277}
277static UV 279static UV
278get_length (void) 280get_length (void)
279{ 281{
280 UV res = get_u8 (); 282 UV res = get_u8 ();
281 283
282 if (res & 0x80) 284 if (expect_false (res & 0x80))
283 { 285 {
284 int cnt = res & 0x7f; 286 U8 cnt = res & 0x7f;
287
288 // this genewrates quite ugly code, but the overhead
289 // of copying the bytes for these lengths is probably so high
290 // that a slightly inefficient get_length won't matter.
291
292 if (expect_false (cnt == 0))
293 error ("illegal use of indefinite BER length form in primitive encoding (X.690 8.1.3.2)");
294
295 if (expect_false (cnt > UVSIZE))
296 error ("BER value length too long (must fit into UV) or BER reserved value in length (X.690 8.1.3.5)");
297
298 want (cnt);
299
285 res = 0; 300 res = 0;
286 301 do
287 switch (cnt) 302 res = (res << 8) | *cur++;
288 { 303 while (--cnt);
289 case 0:
290 error ("indefinite BER value lengths not supported");
291
292 case 0x7f:
293 error ("BER reserved value in length (X.690 8.1.3.5)");
294
295 default:
296 error ("BER value length too long (must fit into UV)");
297
298#if UVSIZE > 4
299 case 8: res = (res << 8) | get_u8 ();
300 case 7: res = (res << 8) | get_u8 ();
301 case 6: res = (res << 8) | get_u8 ();
302 case 5: res = (res << 8) | get_u8 ();
303#endif
304 case 4: res = (res << 8) | get_u8 ();
305 case 3: res = (res << 8) | get_u8 ();
306 case 2: res = (res << 8) | get_u8 ();
307 case 1: res = (res << 8) | get_u8 ();
308 }
309 } 304 }
310 305
311 return res; 306 return res;
312} 307}
313 308
314static SV * 309static SV *
315decode_int (void) 310decode_int (UV len)
316{ 311{
317 UV len = get_length ();
318
319 if (!len) 312 if (!len)
320 error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)"); 313 error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)");
321 314
322 U8 *data = get_n (len); 315 U8 *data = get_n (len);
323 316
344 // but that's ok, as perl relies on it as well. 337 // but that's ok, as perl relies on it as well.
345 return negative ? newSViv ((IV)val) : newSVuv (val); 338 return negative ? newSViv ((IV)val) : newSVuv (val);
346} 339}
347 340
348static SV * 341static SV *
349decode_data (void) 342decode_data (UV len)
350{ 343{
351 UV len = get_length ();
352 return newSVpvn ((char *)get_n (len), len); 344 return newSVpvn ((char *)get_n (len), len);
353} 345}
354 346
355// helper for decode_object_identifier 347// helper for decode_object_identifier
356static char * 348static char *
384 376
385 return buf; 377 return buf;
386} 378}
387 379
388static SV * 380static SV *
389decode_oid (int relative) 381decode_oid (UV len, int relative)
390{ 382{
391 UV len = get_length ();
392
393 if (len <= 0) 383 if (len <= 0)
394 { 384 {
395 error ("BER_TYPE_OID length must not be zero"); 385 error ("BER_TYPE_OID length must not be zero");
396 return &PL_sv_undef; 386 return &PL_sv_undef;
397 } 387 }
432 return newSVpvn (oid, app - oid); 422 return newSVpvn (oid, app - oid);
433} 423}
434 424
435// TODO: this is unacceptably slow 425// TODO: this is unacceptably slow
436static SV * 426static SV *
437decode_ucs (int chrsize) 427decode_ucs (UV len, int chrsize)
438{ 428{
439 SV *res = NEWSV (0, 0);
440
441 UV len = get_length ();
442
443 if (len & (chrsize - 1)) 429 if (len & (chrsize - 1))
444 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len); 430 croak ("BER_TYPE_UCS has an invalid number of octets (%d)", len);
431
432 SV *res = NEWSV (0, 0);
445 433
446 while (len) 434 while (len)
447 { 435 {
448 U8 b1 = get_u8 (); 436 U8 b1 = get_u8 ();
449 U8 b2 = get_u8 (); 437 U8 b2 = get_u8 ();
482 if (tag == ASN_TAG_BER) 470 if (tag == ASN_TAG_BER)
483 tag = get_w (); 471 tag = get_w ();
484 472
485 if (constructed) 473 if (constructed)
486 { 474 {
475 want (1);
476 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
477
478 if (expect_false (*cur == 0x80))
479 {
480 // indefinite length
481 ++cur;
482
483 for (;;)
484 {
485 want (2);
486 if (!cur [0] && !cur [1])
487 {
488 cur += 2;
489 break;
490 }
491
492 av_push (av, decode_ber ());
493 }
494 }
495 else
496 {
497 UV len = get_length ();
498 UV seqend = (cur - buf) + len;
499
500 while (cur < buf + seqend)
501 av_push (av, decode_ber ());
502
503 if (expect_false (cur > buf + seqend))
504 croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend);
505 }
506
507 res = newRV_inc ((SV *)av);
508 }
509 else
510 {
487 UV len = get_length (); 511 UV len = get_length ();
488 UV seqend = (cur - buf) + len;
489 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
490 512
491 while (cur < buf + seqend)
492 av_push (av, decode_ber ());
493
494 if (cur > buf + seqend)
495 croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend);
496
497 res = newRV_inc ((SV *)av);
498 }
499 else
500 switch (profile_lookup (cur_profile, klass, tag)) 513 switch (profile_lookup (cur_profile, klass, tag))
501 { 514 {
502 case BER_TYPE_NULL: 515 case BER_TYPE_NULL:
503 { 516 if (expect_false (len))
504 UV len = get_length ();
505
506 if (len)
507 croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len); 517 croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len);
508 518
509 res = &PL_sv_undef; 519 res = &PL_sv_undef;
510 }
511 break; 520 break;
512 521
513 case BER_TYPE_BOOL: 522 case BER_TYPE_BOOL:
514 {
515 UV len = get_length ();
516
517 if (len != 1) 523 if (expect_false (len != 1))
518 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len); 524 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len);
519 525
520 res = newSVcacheint (!!get_u8 ()); 526 res = newSVcacheint (!!get_u8 ());
521 }
522 break; 527 break;
523 528
524 case BER_TYPE_OID: 529 case BER_TYPE_OID:
525 res = decode_oid (0); 530 res = decode_oid (len, 0);
526 break; 531 break;
527 532
528 case BER_TYPE_RELOID: 533 case BER_TYPE_RELOID:
529 res = decode_oid (1); 534 res = decode_oid (len, 1);
530 break; 535 break;
531 536
532 case BER_TYPE_INT: 537 case BER_TYPE_INT:
533 res = decode_int (); 538 res = decode_int (len);
534 break; 539 break;
535 540
536 case BER_TYPE_UTF8: 541 case BER_TYPE_UTF8:
537 res = decode_data (); 542 res = decode_data (len);
538 SvUTF8_on (res); 543 SvUTF8_on (res);
539 break; 544 break;
540 545
541 case BER_TYPE_BYTES: 546 case BER_TYPE_BYTES:
542 res = decode_data (); 547 res = decode_data (len);
543 break; 548 break;
544 549
545 case BER_TYPE_IPADDRESS: 550 case BER_TYPE_IPADDRESS:
546 { 551 {
547 UV len = get_length ();
548
549 if (len != 4) 552 if (len != 4)
550 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len); 553 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len);
551 554
552 U8 c1 = get_u8 (); 555 U8 *data = get_n (4);
553 U8 c2 = get_u8 (); 556 res = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]);
554 U8 c3 = get_u8 ();
555 U8 c4 = get_u8 ();
556
557 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
558 } 557 }
559 break; 558 break;
560 559
561 case BER_TYPE_UCS2: 560 case BER_TYPE_UCS2:
562 res = decode_ucs (2); 561 res = decode_ucs (len, 2);
563 break; 562 break;
564 563
565 case BER_TYPE_UCS4: 564 case BER_TYPE_UCS4:
566 res = decode_ucs (4); 565 res = decode_ucs (len, 4);
567 break; 566 break;
568 567
569 case BER_TYPE_REAL: 568 case BER_TYPE_REAL:
570 error ("BER_TYPE_REAL not implemented"); 569 error ("BER_TYPE_REAL not implemented");
571 570
572 case BER_TYPE_CROAK: 571 case BER_TYPE_CROAK:
573 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); 572 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
574 573
575 default: 574 default:
576 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 575 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
577 } 576 }
577 }
578 578
579 AV *av = newAV (); 579 AV *av = newAV ();
580 av_fill (av, BER_ARRAYSIZE - 1); 580 av_fill (av, BER_ARRAYSIZE - 1);
581 AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); 581 AvARRAY (av)[BER_CLASS] = newSVcacheint (klass);
582 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); 582 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag);
669} 669}
670 670
671static U8 * 671static U8 *
672put_length_at (UV val, U8 *cur) 672put_length_at (UV val, U8 *cur)
673{ 673{
674 if (val < 0x7fU) 674 if (val <= 0x7fU)
675 *cur++ = val; 675 *cur++ = val;
676 else 676 else
677 { 677 {
678 U8 *lenb = cur++; 678 U8 *lenb = cur++;
679 679
695} 695}
696 696
697static void 697static void
698put_length (UV val) 698put_length (UV val)
699{ 699{
700 need (5 + val); 700 need (9 + val);
701 cur = put_length_at (val, cur); 701 cur = put_length_at (val, cur);
702} 702}
703 703
704// return how many bytes the encoded length requires 704// return how many bytes the encoded length requires
705static int length_length (UV val) 705static int length_length (UV val)
706{ 706{
707 return val < 0x7fU 707 // use hashing with a DeBruin sequence, anyone?
708 return expect_true (val <= 0x7fU)
708 ? 1 709 ? 1
709 : 2 710 : 2
710 + (val > 0xffU) 711 + (val > 0x000000000000ffU)
711 + (val > 0xffffU) 712 + (val > 0x0000000000ffffU)
712 + (val > 0xffffffU) 713 + (val > 0x00000000ffffffU)
713#if UVSIZE > 4 714#if UVSIZE > 4
714 + (val > 0xffffffffU) 715 + (val > 0x000000ffffffffU)
715 + (val > 0xffffffffffU) 716 + (val > 0x0000ffffffffffU)
716 + (val > 0xffffffffffffU) 717 + (val > 0x00ffffffffffffU)
717 + (val > 0xffffffffffffffU) 718 + (val > 0xffffffffffffffU)
718#endif 719#endif
719 ; 720 ;
720} 721}
721 722

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines