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.21 by root, Sat Apr 20 17:23:21 2019 UTC vs.
Revision 1.31 by root, Tue Apr 23 21:03:12 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,
141{ 143{
142 if (!SvOK (profile)) 144 if (!SvOK (profile))
143 return default_profile; 145 return default_profile;
144 146
145 if (!SvROK (profile)) 147 if (!SvROK (profile))
146 croak ("invalid profile"); 148 croak ("Convert::BER::XS::Profile expected");
147 149
148 profile = SvRV (profile); 150 profile = SvRV (profile);
149 151
150 if (SvSTASH (profile) != profile_stash) 152 if (SvSTASH (profile) != profile_stash)
151 croak ("invalid profile object"); 153 croak ("Convert::BER::XS::Profile expected");
152 154
153 return (void *)profile; 155 return (void *)profile;
154} 156}
155 157
156static int 158static int
228} 230}
229 231
230// get_* functions fetch something from the buffer 232// get_* functions fetch something from the buffer
231// decode_* functions use get_* fun ctions to decode ber values 233// decode_* functions use get_* fun ctions to decode ber values
232 234
235// get single octet
236static U8
237get_u8 (void)
238{
239 if (cur == end)
240 error ("unexpected end of message buffer");
241
242 return *cur++;
243}
244
233// get n octets 245// get n octets
234static U8 * 246static U8 *
235get_n (UV count) 247get_n (UV count)
236{ 248{
237 want (count); 249 want (count);
238 U8 *res = cur; 250 U8 *res = cur;
239 cur += count; 251 cur += count;
240 return res; 252 return res;
241} 253}
242 254
243// get single octet
244static U8
245get_u8 (void)
246{
247 if (cur == end)
248 error ("unexpected end of message buffer");
249
250 return *cur++;
251}
252
253// get ber-encoded integer (i.e. pack "w") 255// get ber-encoded integer (i.e. pack "w")
254static UV 256static UV
255get_w (void) 257get_w (void)
256{ 258{
257 UV res = 0; 259 UV res = 0;
260 if (expect_false (c == 0x80)) 262 if (expect_false (c == 0x80))
261 error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)"); 263 error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)");
262 264
263 for (;;) 265 for (;;)
264 { 266 {
267 if (expect_false (res >> UVSIZE * 8 - 7))
268 error ("BER variable length integer overflow");
269
265 res = (res << 7) | (c & 0x7f); 270 res = (res << 7) | (c & 0x7f);
266 271
267 if (!(c & 0x80)) 272 if (expect_true (!(c & 0x80)))
268 return res; 273 return res;
269 274
270 c = get_u8 (); 275 c = get_u8 ();
271 } 276 }
272} 277}
274static UV 279static UV
275get_length (void) 280get_length (void)
276{ 281{
277 UV res = get_u8 (); 282 UV res = get_u8 ();
278 283
279 if (res & 0x80) 284 if (expect_false (res & 0x80))
280 { 285 {
281 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 ("indefinite BER value lengths not supported");
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
282 res = 0; 300 res = 0;
283 301 do
284 switch (cnt) 302 res = (res << 8) | *cur++;
285 { 303 while (--cnt);
286 case 0:
287 error ("indefinite ASN.1 lengths not supported");
288
289 case 0x7f:
290 error ("ASN.1 reserved value in length (X.690 8.1.3.5)");
291
292 default:
293 error ("ASN.1 length too long (only up to 2**64 octets supported)");
294
295 case 8: res = (res << 8) | get_u8 ();
296 case 7: res = (res << 8) | get_u8 ();
297 case 6: res = (res << 8) | get_u8 ();
298 case 5: res = (res << 8) | get_u8 ();
299 case 4: res = (res << 8) | get_u8 ();
300 case 3: res = (res << 8) | get_u8 ();
301 case 2: res = (res << 8) | get_u8 ();
302 case 1: res = (res << 8) | get_u8 ();
303 }
304 } 304 }
305 305
306 return res; 306 return res;
307} 307}
308 308
309static SV * 309static SV *
310decode_int (void) 310decode_int (UV len)
311{ 311{
312 UV len = get_length ();
313
314 if (!len) 312 if (!len)
315 error ("invalid integer length equal to zero (X.690 8.3.1)"); 313 error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)");
316 314
317 U8 *data = get_n (len); 315 U8 *data = get_n (len);
318 316
319 if (expect_false (len > 1)) 317 if (expect_false (len > 1))
320 { 318 {
321 U16 mask = (data [0] << 8) | data [1] & 0xff80; 319 U16 mask = (data [0] << 8) | data [1] & 0xff80;
322 320
323 if (expect_false (mask == 0xff80 || mask == 0x0000)) 321 if (expect_false (mask == 0xff80 || mask == 0x0000))
324 error ("illegal padding in integer (X.690 8.3.2)"); 322 error ("illegal padding in BER_TYPE_INT (X.690 8.3.2)");
325 } 323 }
326 324
327 int negative = data [0] & 0x80; 325 int negative = data [0] & 0x80;
328 326
329 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");
330 331
331 do 332 do
332 val = (val << 8) | *data++; 333 val = (val << 8) | *data++;
333 while (--len); 334 while (--len);
334 335
336 // but that's ok, as perl relies on it as well. 337 // but that's ok, as perl relies on it as well.
337 return negative ? newSViv ((IV)val) : newSVuv (val); 338 return negative ? newSViv ((IV)val) : newSVuv (val);
338} 339}
339 340
340static SV * 341static SV *
341decode_data (void) 342decode_data (UV len)
342{ 343{
343 UV len = get_length ();
344 return newSVpvn ((char *)get_n (len), len); 344 return newSVpvn ((char *)get_n (len), len);
345} 345}
346 346
347// helper for decode_object_identifier 347// helper for decode_object_identifier
348static char * 348static char *
376 376
377 return buf; 377 return buf;
378} 378}
379 379
380static SV * 380static SV *
381decode_oid (int relative) 381decode_oid (UV len, int relative)
382{ 382{
383 UV len = get_length ();
384
385 if (len <= 0) 383 if (len <= 0)
386 { 384 {
387 error ("OBJECT IDENTIFIER length equal to zero"); 385 error ("BER_TYPE_OID length must not be zero");
388 return &PL_sv_undef; 386 return &PL_sv_undef;
389 } 387 }
390 388
391 U8 *end = cur + len; 389 U8 *end = cur + len;
392 UV w = get_w (); 390 UV w = get_w ();
393 391
394 static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack 392 static char oid[MAX_OID_STRLEN]; // static, because too large for stack
395 char *app = oid; 393 char *app = oid;
396 394
397 if (relative) 395 if (relative)
398 app = write_uv (app, w); 396 app = write_uv (app, w);
397 else
398 {
399 UV w1, w2;
400
399 else if (w < 2 * 40) 401 if (w < 2 * 40)
400 { 402 (w1 = w / 40), (w2 = w % 40);
403 else
404 (w1 = 2), (w2 = w - 2 * 40);
405
401 app = write_uv (app, (U8)w / 40); 406 app = write_uv (app, w1);
402 *app++ = '.'; 407 *app++ = '.';
403 app = write_uv (app, (U8)w % 40);
404 }
405 else
406 {
407 app = write_uv (app, 2); 408 app = write_uv (app, w2);
408 *app++ = '.';
409 app = write_uv (app, w - 2 * 40);
410 } 409 }
411 410
412 while (cur < end) 411 while (cur < end)
413 { 412 {
414 // we assume an oid component is never > 64 digits 413 // we assume an oid component is never > 64 digits
423 return newSVpvn (oid, app - oid); 422 return newSVpvn (oid, app - oid);
424} 423}
425 424
426// TODO: this is unacceptably slow 425// TODO: this is unacceptably slow
427static SV * 426static SV *
428decode_ucs (int chrsize) 427decode_ucs (UV len, int chrsize)
429{ 428{
430 SV *res = NEWSV (0, 0);
431
432 UV len = get_length ();
433
434 if (len & (chrsize - 1)) 429 if (len & (chrsize - 1))
435 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);
436 433
437 while (len) 434 while (len)
438 { 435 {
439 U8 b1 = get_u8 (); 436 U8 b1 = get_u8 ();
440 U8 b2 = get_u8 (); 437 U8 b2 = get_u8 ();
473 if (tag == ASN_TAG_BER) 470 if (tag == ASN_TAG_BER)
474 tag = get_w (); 471 tag = get_w ();
475 472
476 if (constructed) 473 if (constructed)
477 { 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 {
478 UV len = get_length (); 511 UV len = get_length ();
479 UV seqend = (cur - buf) + len;
480 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
481 512
482 while (cur < buf + seqend)
483 av_push (av, decode_ber ());
484
485 if (cur > buf + seqend)
486 croak ("constructed type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend);
487
488 res = newRV_inc ((SV *)av);
489 }
490 else
491 switch (profile_lookup (cur_profile, klass, tag)) 513 switch (profile_lookup (cur_profile, klass, tag))
492 { 514 {
493 case BER_TYPE_NULL: 515 case BER_TYPE_NULL:
494 { 516 if (expect_false (len))
495 UV len = get_length ();
496
497 if (len)
498 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);
499 518
500 res = &PL_sv_undef; 519 res = &PL_sv_undef;
501 }
502 break; 520 break;
503 521
504 case BER_TYPE_BOOL: 522 case BER_TYPE_BOOL:
505 {
506 UV len = get_length ();
507
508 if (len != 1) 523 if (expect_false (len != 1))
509 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);
510 525
511 res = newSVcacheint (!!get_u8 ()); 526 res = newSVcacheint (!!get_u8 ());
512 }
513 break; 527 break;
514 528
515 case BER_TYPE_OID: 529 case BER_TYPE_OID:
516 res = decode_oid (0); 530 res = decode_oid (len, 0);
517 break; 531 break;
518 532
519 case BER_TYPE_RELOID: 533 case BER_TYPE_RELOID:
520 res = decode_oid (1); 534 res = decode_oid (len, 1);
521 break; 535 break;
522 536
523 case BER_TYPE_INT: 537 case BER_TYPE_INT:
524 res = decode_int (); 538 res = decode_int (len);
525 break; 539 break;
526 540
527 case BER_TYPE_UTF8: 541 case BER_TYPE_UTF8:
528 res = decode_data (); 542 res = decode_data (len);
529 SvUTF8_on (res); 543 SvUTF8_on (res);
530 break; 544 break;
531 545
532 case BER_TYPE_BYTES: 546 case BER_TYPE_BYTES:
533 res = decode_data (); 547 res = decode_data (len);
534 break; 548 break;
535 549
536 case BER_TYPE_IPADDRESS: 550 case BER_TYPE_IPADDRESS:
537 { 551 {
538 UV len = get_length ();
539
540 if (len != 4) 552 if (len != 4)
541 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);
542 554
543 U8 c1 = get_u8 (); 555 U8 *data = get_n (4);
544 U8 c2 = get_u8 (); 556 res = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]);
545 U8 c3 = get_u8 ();
546 U8 c4 = get_u8 ();
547
548 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
549 } 557 }
550 break; 558 break;
551 559
552 case BER_TYPE_UCS2: 560 case BER_TYPE_UCS2:
553 res = decode_ucs (2); 561 res = decode_ucs (len, 2);
554 break; 562 break;
555 563
556 case BER_TYPE_UCS4: 564 case BER_TYPE_UCS4:
557 res = decode_ucs (4); 565 res = decode_ucs (len, 4);
558 break; 566 break;
559 567
560 case BER_TYPE_REAL: 568 case BER_TYPE_REAL:
569 error ("BER_TYPE_REAL not implemented");
570
561 case BER_TYPE_CROAK: 571 case BER_TYPE_CROAK:
572 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
573
562 default: 574 default:
563 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 575 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
564 } 576 }
577 }
565 578
566 AV *av = newAV (); 579 AV *av = newAV ();
567 av_fill (av, BER_ARRAYSIZE - 1); 580 av_fill (av, BER_ARRAYSIZE - 1);
568 AvARRAY (av)[BER_CLASS] = newSVcacheint (klass); 581 AvARRAY (av)[BER_CLASS] = newSVcacheint (klass);
569 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); 582 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag);
656} 669}
657 670
658static U8 * 671static U8 *
659put_length_at (UV val, U8 *cur) 672put_length_at (UV val, U8 *cur)
660{ 673{
661 if (val < 0x7fU) 674 if (val <= 0x7fU)
662 *cur++ = val; 675 *cur++ = val;
663 else 676 else
664 { 677 {
665 U8 *lenb = cur++; 678 U8 *lenb = cur++;
666 679
667#if UVSIZE > 4 680#if UVSIZE > 4
668 *cur = val >> 56; cur += *cur > 0; 681 *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7));
669 *cur = val >> 48; cur += *cur > 0; 682 *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6));
670 *cur = val >> 40; cur += *cur > 0; 683 *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5));
671 *cur = val >> 32; cur += *cur > 0; 684 *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4));
672#endif 685#endif
673 *cur = val >> 24; cur += *cur > 0; 686 *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3));
674 *cur = val >> 16; cur += *cur > 0; 687 *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2));
675 *cur = val >> 8; cur += *cur > 0; 688 *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1));
676 *cur = val ; cur += 1; 689 *cur = val ; cur += 1;
677 690
678 *lenb = 0x80 + cur - lenb - 1; 691 *lenb = 0x80 + cur - lenb - 1;
679 } 692 }
680 693
682} 695}
683 696
684static void 697static void
685put_length (UV val) 698put_length (UV val)
686{ 699{
687 need (5 + val); 700 need (9 + val);
688 cur = put_length_at (val, cur); 701 cur = put_length_at (val, cur);
689} 702}
690 703
691// return how many bytes the encoded length requires 704// return how many bytes the encoded length requires
692static int length_length (UV val) 705static int length_length (UV val)
693{ 706{
694 return val < 0x7fU 707 // use hashing with a DeBruin sequence, anyone?
708 return expect_true (val <= 0x7fU)
695 ? 1 709 ? 1
696 : 2 710 : 2
697 + (val > 0xffU) 711 + (val > 0x000000000000ffU)
698 + (val > 0xffffU) 712 + (val > 0x0000000000ffffU)
699 + (val > 0xffffffU) 713 + (val > 0x00000000ffffffU)
700#if UVSIZE > 4 714#if UVSIZE > 4
701 + (val > 0xffffffffU) 715 + (val > 0x000000ffffffffU)
702 + (val > 0xffffffffffU) 716 + (val > 0x0000ffffffffffU)
703 + (val > 0xffffffffffffU) 717 + (val > 0x00ffffffffffffU)
704 + (val > 0xffffffffffffffU) 718 + (val > 0xffffffffffffffU)
705#endif 719#endif
706 ; 720 ;
707} 721}
708 722
917 // and adjust later 931 // and adjust later
918 need (1); 932 need (1);
919 STRLEN mark = len_fixup_mark (); 933 STRLEN mark = len_fixup_mark ();
920 934
921 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) 935 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV))
922 croak ("BER constructed data must be array-reference"); 936 croak ("BER CONSTRUCTED data must be array-reference");
923 937
924 AV *av = (AV *)SvRV (data); 938 AV *av = (AV *)SvRV (data);
925 int fill = AvFILL (av); 939 int fill = AvFILL (av);
926 940
927 if (expect_false (SvRMAGICAL (av))) 941 if (expect_false (SvRMAGICAL (av)))
928 croak ("BER constructed data must not be tied"); 942 croak ("BER CONSTRUCTED data must not be tied");
929 943
930 int i; 944 int i;
931 for (i = 0; i <= fill; ++i) 945 for (i = 0; i <= fill; ++i)
932 encode_ber (AvARRAY (av)[i]); 946 encode_ber (AvARRAY (av)[i]);
933 947
988 case BER_TYPE_UCS4: 1002 case BER_TYPE_UCS4:
989 encode_ucs (data, 4); 1003 encode_ucs (data, 4);
990 break; 1004 break;
991 1005
992 case BER_TYPE_REAL: 1006 case BER_TYPE_REAL:
1007 croak ("BER_TYPE_REAL not implemented");
1008
993 case BER_TYPE_CROAK: 1009 case BER_TYPE_CROAK:
1010 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
1011
994 default: 1012 default:
995 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 1013 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
996 } 1014 }
997 1015
998} 1016}
1081 1099
1082 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 1100 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
1083 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 1101 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1084} 1102}
1085 1103
1086SV * 1104void
1087ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1105ber_decode (SV *ber, SV *profile = &PL_sv_undef)
1106 ALIAS:
1107 ber_decode_prefix = 1
1088 CODE: 1108 PPCODE:
1089{ 1109{
1090 cur_profile = SvPROFILE (profile); 1110 cur_profile = SvPROFILE (profile);
1091 STRLEN len; 1111 STRLEN len;
1092 buf = (U8 *)SvPVbyte (ber, len); 1112 buf = (U8 *)SvPVbyte (ber, len);
1093 cur = buf; 1113 cur = buf;
1094 end = buf + len; 1114 end = buf + len;
1095 1115
1096 RETVAL = decode_ber (); 1116 SV *tuple = decode_ber ();
1117
1118 EXTEND (SP, 2);
1119 PUSHs (sv_2mortal (tuple));
1120
1121 if (ix)
1122 PUSHs (sv_2mortal (newSViv (cur - buf)));
1123 else if (cur != end)
1124 error ("trailing garbage after BER value");
1097} 1125}
1098 OUTPUT: RETVAL
1099 1126
1100void 1127void
1101ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) 1128ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef)
1102 PPCODE: 1129 PPCODE:
1103{ 1130{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines