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.16 by root, Sat Apr 20 15:37:27 2019 UTC vs.
Revision 1.29 by root, Tue Apr 23 19:44:12 2019 UTC

58 ASN_CLASS_SHIFT = 6, 58 ASN_CLASS_SHIFT = 6,
59 59
60 // ASN_APPLICATION SNMP 60 // ASN_APPLICATION SNMP
61 SNMP_IPADDRESS = 0x00, 61 SNMP_IPADDRESS = 0x00,
62 SNMP_COUNTER32 = 0x01, 62 SNMP_COUNTER32 = 0x01,
63 SNMP_GAUGE32 = 0x02,
63 SNMP_UNSIGNED32 = 0x02, 64 SNMP_UNSIGNED32 = 0x02,
64 SNMP_TIMETICKS = 0x03, 65 SNMP_TIMETICKS = 0x03,
65 SNMP_OPAQUE = 0x04, 66 SNMP_OPAQUE = 0x04,
66 SNMP_COUNTER64 = 0x06, 67 SNMP_COUNTER64 = 0x06,
67}; 68};
140{ 141{
141 if (!SvOK (profile)) 142 if (!SvOK (profile))
142 return default_profile; 143 return default_profile;
143 144
144 if (!SvROK (profile)) 145 if (!SvROK (profile))
145 croak ("invalid profile"); 146 croak ("Convert::BER::XS::Profile expected");
146 147
147 profile = SvRV (profile); 148 profile = SvRV (profile);
148 149
149 if (SvSTASH (profile) != profile_stash) 150 if (SvSTASH (profile) != profile_stash)
150 croak ("invalid profile object"); 151 croak ("Convert::BER::XS::Profile expected");
151 152
152 return (void *)profile; 153 return (void *)profile;
153} 154}
154 155
155static int 156static int
227} 228}
228 229
229// get_* functions fetch something from the buffer 230// get_* functions fetch something from the buffer
230// decode_* functions use get_* fun ctions to decode ber values 231// decode_* functions use get_* fun ctions to decode ber values
231 232
233// get single octet
234static U8
235get_u8 (void)
236{
237 if (cur == end)
238 error ("unexpected end of message buffer");
239
240 return *cur++;
241}
242
232// get n octets 243// get n octets
233static U8 * 244static U8 *
234get_n (UV count) 245get_n (UV count)
235{ 246{
236 want (count); 247 want (count);
237 U8 *res = cur; 248 U8 *res = cur;
238 cur += count; 249 cur += count;
239 return res; 250 return res;
240} 251}
241 252
242// get single octet
243static U8
244get_u8 (void)
245{
246 if (cur == end)
247 error ("unexpected end of message buffer");
248
249 return *cur++;
250}
251
252// get ber-encoded integer (i.e. pack "w") 253// get ber-encoded integer (i.e. pack "w")
253static UV 254static UV
254get_w (void) 255get_w (void)
255{ 256{
256 UV res = 0; 257 UV res = 0;
258 U8 c = get_u8 ();
259
260 if (expect_false (c == 0x80))
261 error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)");
257 262
258 for (;;) 263 for (;;)
259 { 264 {
260 U8 c = get_u8 (); 265 if (expect_false (res >> UVSIZE * 8 - 7))
266 error ("BER variable length integer overflow");
267
261 res = (res << 7) | (c & 0x7f); 268 res = (res << 7) | (c & 0x7f);
262 269
263 if (!(c & 0x80)) 270 if (expect_true (!(c & 0x80)))
264 return res; 271 return res;
272
273 c = get_u8 ();
265 } 274 }
266} 275}
267 276
268static UV 277static UV
269get_length (void) 278get_length (void)
270{ 279{
271 UV res = get_u8 (); 280 UV res = get_u8 ();
272 281
273 if (res & 0x80) 282 if (expect_false (res & 0x80))
274 { 283 {
275 int cnt = res & 0x7f; 284 U8 cnt = res & 0x7f;
285
286 // this genewrates quite ugly code, but the overhead
287 // of copying the bytes for these lengths is probably so high
288 // that a slightly inefficient get_length won't matter.
289
290 if (expect_false (cnt == 0))
291 error ("indefinite BER value lengths not supported");
292
293 if (expect_false (cnt > UVSIZE))
294 error ("BER value length too long (must fit into UV) or BER reserved value in length (X.690 8.1.3.5)");
295
296 want (cnt);
297
276 res = 0; 298 res = 0;
277 299 do
278 switch (cnt) 300 res = (res << 8) | *cur++;
279 { 301 while (--cnt);
280 case 0:
281 error ("indefinite ASN.1 lengths not supported");
282 return 0;
283
284 default:
285 error ("ASN.1 length too long");
286 return 0;
287
288 case 8: res = (res << 8) | get_u8 ();
289 case 7: res = (res << 8) | get_u8 ();
290 case 6: res = (res << 8) | get_u8 ();
291 case 5: res = (res << 8) | get_u8 ();
292 case 4: res = (res << 8) | get_u8 ();
293 case 3: res = (res << 8) | get_u8 ();
294 case 2: res = (res << 8) | get_u8 ();
295 case 1: res = (res << 8) | get_u8 ();
296 }
297 } 302 }
298 303
299 return res; 304 return res;
300} 305}
301 306
303decode_int (void) 308decode_int (void)
304{ 309{
305 UV len = get_length (); 310 UV len = get_length ();
306 311
307 if (!len) 312 if (!len)
308 { 313 error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)");
309 error ("invalid integer length equal to zero");
310 return 0;
311 }
312 314
313 U8 *data = get_n (len); 315 U8 *data = get_n (len);
314 316
317 if (expect_false (len > 1))
318 {
319 U16 mask = (data [0] << 8) | data [1] & 0xff80;
320
321 if (expect_false (mask == 0xff80 || mask == 0x0000))
322 error ("illegal padding in BER_TYPE_INT (X.690 8.3.2)");
323 }
324
315 int negative = data [0] & 0x80; 325 int negative = data [0] & 0x80;
316 326
317 UV val = negative ? -1 : 0; // copy signbit to all bits 327 UV val = negative ? -1 : 0; // copy signbit to all bits
328
329 if (len > UVSIZE + (!negative && !*data))
330 error ("BER_TYPE_INT overflow");
318 331
319 do 332 do
320 val = (val << 8) | *data++; 333 val = (val << 8) | *data++;
321 while (--len); 334 while (--len);
322 335
370{ 383{
371 UV len = get_length (); 384 UV len = get_length ();
372 385
373 if (len <= 0) 386 if (len <= 0)
374 { 387 {
375 error ("OBJECT IDENTIFIER length equal to zero"); 388 error ("BER_TYPE_OID length must not be zero");
376 return &PL_sv_undef; 389 return &PL_sv_undef;
377 } 390 }
378 391
379 U8 *end = cur + len; 392 U8 *end = cur + len;
380 UV w = get_w (); 393 UV w = get_w ();
381 394
382 static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack 395 static char oid[MAX_OID_STRLEN]; // static, because too large for stack
383 char *app = oid; 396 char *app = oid;
384 397
385 if (relative) 398 if (relative)
386 app = write_uv (app, w); 399 app = write_uv (app, w);
387 else 400 else
388 { 401 {
402 UV w1, w2;
403
404 if (w < 2 * 40)
405 (w1 = w / 40), (w2 = w % 40);
406 else
407 (w1 = 2), (w2 = w - 2 * 40);
408
389 app = write_uv (app, (U8)w / 40); 409 app = write_uv (app, w1);
390 *app++ = '.'; 410 *app++ = '.';
391 app = write_uv (app, (U8)w % 40); 411 app = write_uv (app, w2);
392 } 412 }
393 413
394 while (cur < end) 414 while (cur < end)
395 { 415 {
396 // we assume an oid component is never > 64 digits 416 // we assume an oid component is never > 64 digits
453 int tag = identifier & ASN_TAG_MASK; 473 int tag = identifier & ASN_TAG_MASK;
454 474
455 if (tag == ASN_TAG_BER) 475 if (tag == ASN_TAG_BER)
456 tag = get_w (); 476 tag = get_w ();
457 477
458 if (tag == ASN_TAG_BER)
459 tag = get_w ();
460
461 if (constructed) 478 if (constructed)
462 { 479 {
463 UV len = get_length (); 480 UV len = get_length ();
464 UV seqend = (cur - buf) + len; 481 UV seqend = (cur - buf) + len;
465 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 482 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
466 483
467 while (cur < buf + seqend) 484 while (cur < buf + seqend)
468 av_push (av, decode_ber ()); 485 av_push (av, decode_ber ());
469 486
470 if (cur > buf + seqend) 487 if (cur > buf + seqend)
471 croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); 488 croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend);
472 489
473 res = newRV_inc ((SV *)av); 490 res = newRV_inc ((SV *)av);
474 } 491 }
475 else 492 else
476 switch (profile_lookup (cur_profile, klass, tag)) 493 switch (profile_lookup (cur_profile, klass, tag))
478 case BER_TYPE_NULL: 495 case BER_TYPE_NULL:
479 { 496 {
480 UV len = get_length (); 497 UV len = get_length ();
481 498
482 if (len) 499 if (len)
483 croak ("BER_TYPE_NULL value with non-zero length %d encountered", len); 500 croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len);
484 501
485 res = &PL_sv_undef; 502 res = &PL_sv_undef;
486 } 503 }
487 break; 504 break;
488 505
489 case BER_TYPE_BOOL: 506 case BER_TYPE_BOOL:
490 { 507 {
491 UV len = get_length (); 508 UV len = get_length ();
492 509
493 if (len != 1) 510 if (len != 1)
494 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len); 511 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len);
495 512
496 res = newSVcacheint (!!get_u8 ()); 513 res = newSVcacheint (!!get_u8 ());
497 } 514 }
498 break; 515 break;
499 516
521 case BER_TYPE_IPADDRESS: 538 case BER_TYPE_IPADDRESS:
522 { 539 {
523 UV len = get_length (); 540 UV len = get_length ();
524 541
525 if (len != 4) 542 if (len != 4)
526 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); 543 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len);
527 544
528 U8 c1 = get_u8 (); 545 U8 *data = get_n (4);
529 U8 c2 = get_u8 (); 546 res = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]);
530 U8 c3 = get_u8 ();
531 U8 c4 = get_u8 ();
532
533 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
534 } 547 }
535 break; 548 break;
536 549
537 case BER_TYPE_UCS2: 550 case BER_TYPE_UCS2:
538 res = decode_ucs (2); 551 res = decode_ucs (2);
541 case BER_TYPE_UCS4: 554 case BER_TYPE_UCS4:
542 res = decode_ucs (4); 555 res = decode_ucs (4);
543 break; 556 break;
544 557
545 case BER_TYPE_REAL: 558 case BER_TYPE_REAL:
559 error ("BER_TYPE_REAL not implemented");
560
546 case BER_TYPE_CROAK: 561 case BER_TYPE_CROAK:
562 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
563
547 default: 564 default:
548 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 565 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
549 } 566 }
550 567
551 AV *av = newAV (); 568 AV *av = newAV ();
566strlen_sum (STRLEN l1, STRLEN l2) 583strlen_sum (STRLEN l1, STRLEN l2)
567{ 584{
568 size_t sum = l1 + l2; 585 size_t sum = l1 + l2;
569 586
570 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) 587 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
571 croak ("JSON::XS: string size overflow"); 588 croak ("Convert::BER::XS: string size overflow");
572 589
573 return sum; 590 return sum;
574} 591}
575 592
576static void 593static void
641} 658}
642 659
643static U8 * 660static U8 *
644put_length_at (UV val, U8 *cur) 661put_length_at (UV val, U8 *cur)
645{ 662{
646 if (val < 0x7fU) 663 if (val <= 0x7fU)
647 *cur++ = val; 664 *cur++ = val;
648 else 665 else
649 { 666 {
650 U8 *lenb = cur++; 667 U8 *lenb = cur++;
651 668
652#if UVSIZE > 4 669#if UVSIZE > 4
653 *cur = val >> 56; cur += *cur > 0; 670 *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7));
654 *cur = val >> 48; cur += *cur > 0; 671 *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6));
655 *cur = val >> 40; cur += *cur > 0; 672 *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5));
656 *cur = val >> 32; cur += *cur > 0; 673 *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4));
657#endif 674#endif
658 *cur = val >> 24; cur += *cur > 0; 675 *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3));
659 *cur = val >> 16; cur += *cur > 0; 676 *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2));
660 *cur = val >> 8; cur += *cur > 0; 677 *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1));
661 *cur = val ; cur += 1; 678 *cur = val ; cur += 1;
662 679
663 *lenb = 0x80 + cur - lenb - 1; 680 *lenb = 0x80 + cur - lenb - 1;
664 } 681 }
665 682
667} 684}
668 685
669static void 686static void
670put_length (UV val) 687put_length (UV val)
671{ 688{
672 need (5 + val); 689 need (9 + val);
673 cur = put_length_at (val, cur); 690 cur = put_length_at (val, cur);
674} 691}
675 692
676// return how many bytes the encoded length requires 693// return how many bytes the encoded length requires
677static int length_length (UV val) 694static int length_length (UV val)
678{ 695{
679 return val < 0x7fU 696 // use hashing with a DeBruin sequence, anyone?
697 return expect_true (val <= 0x7fU)
680 ? 1 698 ? 1
681 : 2 699 : 2
682 + (val > 0xffU) 700 + (val > 0x000000000000ffU)
683 + (val > 0xffffU) 701 + (val > 0x0000000000ffffU)
684 + (val > 0xffffffU) 702 + (val > 0x00000000ffffffU)
685#if UVSIZE > 4 703#if UVSIZE > 4
686 + (val > 0xffffffffU) 704 + (val > 0x000000ffffffffU)
687 + (val > 0xffffffffffU) 705 + (val > 0x0000ffffffffffU)
688 + (val > 0xffffffffffffU) 706 + (val > 0x00ffffffffffffU)
689 + (val > 0xffffffffffffffU) 707 + (val > 0xffffffffffffffU)
690#endif 708#endif
691 ; 709 ;
692} 710}
693 711
902 // and adjust later 920 // and adjust later
903 need (1); 921 need (1);
904 STRLEN mark = len_fixup_mark (); 922 STRLEN mark = len_fixup_mark ();
905 923
906 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) 924 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV))
907 croak ("BER constructed data must be array-reference"); 925 croak ("BER CONSTRUCTED data must be array-reference");
908 926
909 AV *av = (AV *)SvRV (data); 927 AV *av = (AV *)SvRV (data);
910 int fill = AvFILL (av); 928 int fill = AvFILL (av);
911 929
912 if (expect_false (SvRMAGICAL (av))) 930 if (expect_false (SvRMAGICAL (av)))
913 croak ("BER constructed data must not be tied"); 931 croak ("BER CONSTRUCTED data must not be tied");
914 932
915 int i; 933 int i;
916 for (i = 0; i <= fill; ++i) 934 for (i = 0; i <= fill; ++i)
917 encode_ber (AvARRAY (av)[i]); 935 encode_ber (AvARRAY (av)[i]);
918 936
925 put_length (0); 943 put_length (0);
926 break; 944 break;
927 945
928 case BER_TYPE_BOOL: 946 case BER_TYPE_BOOL:
929 put_length (1); 947 put_length (1);
930 *cur++ = SvTRUE (data) ? 0xff : 0x00; 948 *cur++ = SvTRUE (data) ? 0xff : 0x00; // 0xff = DER/CER
931 break; 949 break;
932 950
933 case BER_TYPE_OID: 951 case BER_TYPE_OID:
934 encode_oid (data, 0); 952 encode_oid (data, 0);
935 break; 953 break;
973 case BER_TYPE_UCS4: 991 case BER_TYPE_UCS4:
974 encode_ucs (data, 4); 992 encode_ucs (data, 4);
975 break; 993 break;
976 994
977 case BER_TYPE_REAL: 995 case BER_TYPE_REAL:
996 croak ("BER_TYPE_REAL not implemented");
997
978 case BER_TYPE_CROAK: 998 case BER_TYPE_CROAK:
999 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
1000
979 default: 1001 default:
980 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 1002 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
981 } 1003 }
982 1004
983} 1005}
1055 const_iv (BER_TYPE_IPADDRESS) 1077 const_iv (BER_TYPE_IPADDRESS)
1056 const_iv (BER_TYPE_CROAK) 1078 const_iv (BER_TYPE_CROAK)
1057 1079
1058 const_iv (SNMP_IPADDRESS) 1080 const_iv (SNMP_IPADDRESS)
1059 const_iv (SNMP_COUNTER32) 1081 const_iv (SNMP_COUNTER32)
1082 const_iv (SNMP_GAUGE32)
1060 const_iv (SNMP_UNSIGNED32) 1083 const_iv (SNMP_UNSIGNED32)
1061 const_iv (SNMP_TIMETICKS) 1084 const_iv (SNMP_TIMETICKS)
1062 const_iv (SNMP_OPAQUE) 1085 const_iv (SNMP_OPAQUE)
1063 const_iv (SNMP_COUNTER64) 1086 const_iv (SNMP_COUNTER64)
1064 }; 1087 };
1065 1088
1066 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 1089 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
1067 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 1090 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1068} 1091}
1069 1092
1070SV * 1093void
1071ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1094ber_decode (SV *ber, SV *profile = &PL_sv_undef)
1095 ALIAS:
1096 ber_decode_prefix = 1
1072 CODE: 1097 PPCODE:
1073{ 1098{
1074 cur_profile = SvPROFILE (profile); 1099 cur_profile = SvPROFILE (profile);
1075 STRLEN len; 1100 STRLEN len;
1076 buf = (U8 *)SvPVbyte (ber, len); 1101 buf = (U8 *)SvPVbyte (ber, len);
1077 cur = buf; 1102 cur = buf;
1078 end = buf + len; 1103 end = buf + len;
1079 1104
1080 RETVAL = decode_ber (); 1105 SV *tuple = decode_ber ();
1106
1107 EXTEND (SP, 2);
1108 PUSHs (sv_2mortal (tuple));
1109
1110 if (ix)
1111 PUSHs (sv_2mortal (newSViv (cur - buf)));
1112 else if (cur != end)
1113 error ("trailing garbage after BER value");
1081} 1114}
1082 OUTPUT: RETVAL
1083 1115
1084void 1116void
1085ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) 1117ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef)
1086 PPCODE: 1118 PPCODE:
1087{ 1119{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines