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.18 by root, Sat Apr 20 16:12:53 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 //case 0x80: // indefinite length
285
286 //case 0xff: reserved
287 default:
288 error ("ASN.1 length too long");
289 return 0;
290
291 case 8: res = (res << 8) | get_u8 ();
292 case 7: res = (res << 8) | get_u8 ();
293 case 6: res = (res << 8) | get_u8 ();
294 case 5: res = (res << 8) | get_u8 ();
295 case 4: res = (res << 8) | get_u8 ();
296 case 3: res = (res << 8) | get_u8 ();
297 case 2: res = (res << 8) | get_u8 ();
298 case 1: res = (res << 8) | get_u8 ();
299 }
300 } 302 }
301 303
302 return res; 304 return res;
303} 305}
304 306
306decode_int (void) 308decode_int (void)
307{ 309{
308 UV len = get_length (); 310 UV len = get_length ();
309 311
310 if (!len) 312 if (!len)
311 { 313 error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)");
312 error ("invalid integer length equal to zero");
313 return 0;
314 }
315 314
316 U8 *data = get_n (len); 315 U8 *data = get_n (len);
317 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
318 int negative = data [0] & 0x80; 325 int negative = data [0] & 0x80;
319 326
320 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");
321 331
322 do 332 do
323 val = (val << 8) | *data++; 333 val = (val << 8) | *data++;
324 while (--len); 334 while (--len);
325 335
373{ 383{
374 UV len = get_length (); 384 UV len = get_length ();
375 385
376 if (len <= 0) 386 if (len <= 0)
377 { 387 {
378 error ("OBJECT IDENTIFIER length equal to zero"); 388 error ("BER_TYPE_OID length must not be zero");
379 return &PL_sv_undef; 389 return &PL_sv_undef;
380 } 390 }
381 391
382 U8 *end = cur + len; 392 U8 *end = cur + len;
383 UV w = get_w (); 393 UV w = get_w ();
384 394
385 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
386 char *app = oid; 396 char *app = oid;
387 397
388 if (relative) 398 if (relative)
389 app = write_uv (app, w); 399 app = write_uv (app, w);
400 else
401 {
402 UV w1, w2;
403
390 else if (w < 2 * 40) 404 if (w < 2 * 40)
391 { 405 (w1 = w / 40), (w2 = w % 40);
406 else
407 (w1 = 2), (w2 = w - 2 * 40);
408
392 app = write_uv (app, (U8)w / 40); 409 app = write_uv (app, w1);
393 *app++ = '.'; 410 *app++ = '.';
394 app = write_uv (app, (U8)w % 40);
395 }
396 else
397 {
398 app = write_uv (app, 2); 411 app = write_uv (app, w2);
399 *app++ = '.';
400 app = write_uv (app, w - 2 * 40);
401 } 412 }
402 413
403 while (cur < end) 414 while (cur < end)
404 { 415 {
405 // we assume an oid component is never > 64 digits 416 // we assume an oid component is never > 64 digits
462 int tag = identifier & ASN_TAG_MASK; 473 int tag = identifier & ASN_TAG_MASK;
463 474
464 if (tag == ASN_TAG_BER) 475 if (tag == ASN_TAG_BER)
465 tag = get_w (); 476 tag = get_w ();
466 477
467 if (tag == ASN_TAG_BER)
468 tag = get_w ();
469
470 if (constructed) 478 if (constructed)
471 { 479 {
472 UV len = get_length (); 480 UV len = get_length ();
473 UV seqend = (cur - buf) + len; 481 UV seqend = (cur - buf) + len;
474 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 482 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
475 483
476 while (cur < buf + seqend) 484 while (cur < buf + seqend)
477 av_push (av, decode_ber ()); 485 av_push (av, decode_ber ());
478 486
479 if (cur > buf + seqend) 487 if (cur > buf + seqend)
480 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);
481 489
482 res = newRV_inc ((SV *)av); 490 res = newRV_inc ((SV *)av);
483 } 491 }
484 else 492 else
485 switch (profile_lookup (cur_profile, klass, tag)) 493 switch (profile_lookup (cur_profile, klass, tag))
487 case BER_TYPE_NULL: 495 case BER_TYPE_NULL:
488 { 496 {
489 UV len = get_length (); 497 UV len = get_length ();
490 498
491 if (len) 499 if (len)
492 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);
493 501
494 res = &PL_sv_undef; 502 res = &PL_sv_undef;
495 } 503 }
496 break; 504 break;
497 505
498 case BER_TYPE_BOOL: 506 case BER_TYPE_BOOL:
499 { 507 {
500 UV len = get_length (); 508 UV len = get_length ();
501 509
502 if (len != 1) 510 if (len != 1)
503 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);
504 512
505 res = newSVcacheint (!!get_u8 ()); 513 res = newSVcacheint (!!get_u8 ());
506 } 514 }
507 break; 515 break;
508 516
530 case BER_TYPE_IPADDRESS: 538 case BER_TYPE_IPADDRESS:
531 { 539 {
532 UV len = get_length (); 540 UV len = get_length ();
533 541
534 if (len != 4) 542 if (len != 4)
535 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);
536 544
537 U8 c1 = get_u8 (); 545 U8 *data = get_n (4);
538 U8 c2 = get_u8 (); 546 res = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]);
539 U8 c3 = get_u8 ();
540 U8 c4 = get_u8 ();
541
542 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
543 } 547 }
544 break; 548 break;
545 549
546 case BER_TYPE_UCS2: 550 case BER_TYPE_UCS2:
547 res = decode_ucs (2); 551 res = decode_ucs (2);
550 case BER_TYPE_UCS4: 554 case BER_TYPE_UCS4:
551 res = decode_ucs (4); 555 res = decode_ucs (4);
552 break; 556 break;
553 557
554 case BER_TYPE_REAL: 558 case BER_TYPE_REAL:
559 error ("BER_TYPE_REAL not implemented");
560
555 case BER_TYPE_CROAK: 561 case BER_TYPE_CROAK:
562 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
563
556 default: 564 default:
557 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 565 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
558 } 566 }
559 567
560 AV *av = newAV (); 568 AV *av = newAV ();
575strlen_sum (STRLEN l1, STRLEN l2) 583strlen_sum (STRLEN l1, STRLEN l2)
576{ 584{
577 size_t sum = l1 + l2; 585 size_t sum = l1 + l2;
578 586
579 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) 587 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
580 croak ("JSON::XS: string size overflow"); 588 croak ("Convert::BER::XS: string size overflow");
581 589
582 return sum; 590 return sum;
583} 591}
584 592
585static void 593static void
650} 658}
651 659
652static U8 * 660static U8 *
653put_length_at (UV val, U8 *cur) 661put_length_at (UV val, U8 *cur)
654{ 662{
655 if (val < 0x7fU) 663 if (val <= 0x7fU)
656 *cur++ = val; 664 *cur++ = val;
657 else 665 else
658 { 666 {
659 U8 *lenb = cur++; 667 U8 *lenb = cur++;
660 668
661#if UVSIZE > 4 669#if UVSIZE > 4
662 *cur = val >> 56; cur += *cur > 0; 670 *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7));
663 *cur = val >> 48; cur += *cur > 0; 671 *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6));
664 *cur = val >> 40; cur += *cur > 0; 672 *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5));
665 *cur = val >> 32; cur += *cur > 0; 673 *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4));
666#endif 674#endif
667 *cur = val >> 24; cur += *cur > 0; 675 *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3));
668 *cur = val >> 16; cur += *cur > 0; 676 *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2));
669 *cur = val >> 8; cur += *cur > 0; 677 *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1));
670 *cur = val ; cur += 1; 678 *cur = val ; cur += 1;
671 679
672 *lenb = 0x80 + cur - lenb - 1; 680 *lenb = 0x80 + cur - lenb - 1;
673 } 681 }
674 682
676} 684}
677 685
678static void 686static void
679put_length (UV val) 687put_length (UV val)
680{ 688{
681 need (5 + val); 689 need (9 + val);
682 cur = put_length_at (val, cur); 690 cur = put_length_at (val, cur);
683} 691}
684 692
685// return how many bytes the encoded length requires 693// return how many bytes the encoded length requires
686static int length_length (UV val) 694static int length_length (UV val)
687{ 695{
688 return val < 0x7fU 696 // use hashing with a DeBruin sequence, anyone?
697 return expect_true (val <= 0x7fU)
689 ? 1 698 ? 1
690 : 2 699 : 2
691 + (val > 0xffU) 700 + (val > 0x000000000000ffU)
692 + (val > 0xffffU) 701 + (val > 0x0000000000ffffU)
693 + (val > 0xffffffU) 702 + (val > 0x00000000ffffffU)
694#if UVSIZE > 4 703#if UVSIZE > 4
695 + (val > 0xffffffffU) 704 + (val > 0x000000ffffffffU)
696 + (val > 0xffffffffffU) 705 + (val > 0x0000ffffffffffU)
697 + (val > 0xffffffffffffU) 706 + (val > 0x00ffffffffffffU)
698 + (val > 0xffffffffffffffU) 707 + (val > 0xffffffffffffffU)
699#endif 708#endif
700 ; 709 ;
701} 710}
702 711
911 // and adjust later 920 // and adjust later
912 need (1); 921 need (1);
913 STRLEN mark = len_fixup_mark (); 922 STRLEN mark = len_fixup_mark ();
914 923
915 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) 924 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV))
916 croak ("BER constructed data must be array-reference"); 925 croak ("BER CONSTRUCTED data must be array-reference");
917 926
918 AV *av = (AV *)SvRV (data); 927 AV *av = (AV *)SvRV (data);
919 int fill = AvFILL (av); 928 int fill = AvFILL (av);
920 929
921 if (expect_false (SvRMAGICAL (av))) 930 if (expect_false (SvRMAGICAL (av)))
922 croak ("BER constructed data must not be tied"); 931 croak ("BER CONSTRUCTED data must not be tied");
923 932
924 int i; 933 int i;
925 for (i = 0; i <= fill; ++i) 934 for (i = 0; i <= fill; ++i)
926 encode_ber (AvARRAY (av)[i]); 935 encode_ber (AvARRAY (av)[i]);
927 936
982 case BER_TYPE_UCS4: 991 case BER_TYPE_UCS4:
983 encode_ucs (data, 4); 992 encode_ucs (data, 4);
984 break; 993 break;
985 994
986 case BER_TYPE_REAL: 995 case BER_TYPE_REAL:
996 croak ("BER_TYPE_REAL not implemented");
997
987 case BER_TYPE_CROAK: 998 case BER_TYPE_CROAK:
999 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
1000
988 default: 1001 default:
989 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 1002 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
990 } 1003 }
991 1004
992} 1005}
1064 const_iv (BER_TYPE_IPADDRESS) 1077 const_iv (BER_TYPE_IPADDRESS)
1065 const_iv (BER_TYPE_CROAK) 1078 const_iv (BER_TYPE_CROAK)
1066 1079
1067 const_iv (SNMP_IPADDRESS) 1080 const_iv (SNMP_IPADDRESS)
1068 const_iv (SNMP_COUNTER32) 1081 const_iv (SNMP_COUNTER32)
1082 const_iv (SNMP_GAUGE32)
1069 const_iv (SNMP_UNSIGNED32) 1083 const_iv (SNMP_UNSIGNED32)
1070 const_iv (SNMP_TIMETICKS) 1084 const_iv (SNMP_TIMETICKS)
1071 const_iv (SNMP_OPAQUE) 1085 const_iv (SNMP_OPAQUE)
1072 const_iv (SNMP_COUNTER64) 1086 const_iv (SNMP_COUNTER64)
1073 }; 1087 };
1074 1088
1075 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--)
1076 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 1090 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1077} 1091}
1078 1092
1079SV * 1093void
1080ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1094ber_decode (SV *ber, SV *profile = &PL_sv_undef)
1095 ALIAS:
1096 ber_decode_prefix = 1
1081 CODE: 1097 PPCODE:
1082{ 1098{
1083 cur_profile = SvPROFILE (profile); 1099 cur_profile = SvPROFILE (profile);
1084 STRLEN len; 1100 STRLEN len;
1085 buf = (U8 *)SvPVbyte (ber, len); 1101 buf = (U8 *)SvPVbyte (ber, len);
1086 cur = buf; 1102 cur = buf;
1087 end = buf + len; 1103 end = buf + len;
1088 1104
1089 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");
1090} 1114}
1091 OUTPUT: RETVAL
1092 1115
1093void 1116void
1094ber_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)
1095 PPCODE: 1118 PPCODE:
1096{ 1119{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines