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.19 by root, Sat Apr 20 16:34:34 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;
265 }
266}
267 272
268// get_w, but disallow padding 273 c = get_u8 ();
269static UV 274 }
270get_w_nopad (void)
271{
272 U8 first = get_u8 ();
273
274 if (first == 0x80)
275 error ("illegal BER padding");
276
277 --cur;
278
279 return get_w ();
280} 275}
281 276
282static UV 277static UV
283get_length (void) 278get_length (void)
284{ 279{
285 UV res = get_u8 (); 280 UV res = get_u8 ();
286 281
287 if (res & 0x80) 282 if (expect_false (res & 0x80))
288 { 283 {
289 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
290 res = 0; 298 res = 0;
291 299 do
292 switch (cnt) 300 res = (res << 8) | *cur++;
293 { 301 while (--cnt);
294 case 0:
295 error ("indefinite ASN.1 lengths not supported");
296 return 0;
297
298 //case 0x80: // indefinite length
299
300 //case 0xff: reserved
301 default:
302 error ("ASN.1 length too long");
303 return 0;
304
305 case 8: res = (res << 8) | get_u8 ();
306 case 7: res = (res << 8) | get_u8 ();
307 case 6: res = (res << 8) | get_u8 ();
308 case 5: res = (res << 8) | get_u8 ();
309 case 4: res = (res << 8) | get_u8 ();
310 case 3: res = (res << 8) | get_u8 ();
311 case 2: res = (res << 8) | get_u8 ();
312 case 1: res = (res << 8) | get_u8 ();
313 }
314 } 302 }
315 303
316 return res; 304 return res;
317} 305}
318 306
320decode_int (void) 308decode_int (void)
321{ 309{
322 UV len = get_length (); 310 UV len = get_length ();
323 311
324 if (!len) 312 if (!len)
325 { 313 error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)");
326 error ("invalid integer length equal to zero");
327 return 0;
328 }
329 314
330 U8 *data = get_n (len); 315 U8 *data = get_n (len);
331 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
332 int negative = data [0] & 0x80; 325 int negative = data [0] & 0x80;
333 326
334 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");
335 331
336 do 332 do
337 val = (val << 8) | *data++; 333 val = (val << 8) | *data++;
338 while (--len); 334 while (--len);
339 335
387{ 383{
388 UV len = get_length (); 384 UV len = get_length ();
389 385
390 if (len <= 0) 386 if (len <= 0)
391 { 387 {
392 error ("OBJECT IDENTIFIER length equal to zero"); 388 error ("BER_TYPE_OID length must not be zero");
393 return &PL_sv_undef; 389 return &PL_sv_undef;
394 } 390 }
395 391
396 U8 *end = cur + len; 392 U8 *end = cur + len;
397 UV w = get_w_nopad (); 393 UV w = get_w ();
398 394
399 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
400 char *app = oid; 396 char *app = oid;
401 397
402 if (relative) 398 if (relative)
403 app = write_uv (app, w); 399 app = write_uv (app, w);
400 else
401 {
402 UV w1, w2;
403
404 else if (w < 2 * 40) 404 if (w < 2 * 40)
405 { 405 (w1 = w / 40), (w2 = w % 40);
406 else
407 (w1 = 2), (w2 = w - 2 * 40);
408
406 app = write_uv (app, (U8)w / 40); 409 app = write_uv (app, w1);
407 *app++ = '.'; 410 *app++ = '.';
408 app = write_uv (app, (U8)w % 40);
409 }
410 else
411 {
412 app = write_uv (app, 2); 411 app = write_uv (app, w2);
413 *app++ = '.';
414 app = write_uv (app, w - 2 * 40);
415 } 412 }
416 413
417 while (cur < end) 414 while (cur < end)
418 { 415 {
419 // we assume an oid component is never > 64 digits 416 // we assume an oid component is never > 64 digits
420 if (oid + sizeof (oid) - app < 64) 417 if (oid + sizeof (oid) - app < 64)
421 croak ("BER_TYPE_OID to long to decode"); 418 croak ("BER_TYPE_OID to long to decode");
422 419
423 w = get_w_nopad (); 420 w = get_w ();
424 *app++ = '.'; 421 *app++ = '.';
425 app = write_uv (app, w); 422 app = write_uv (app, w);
426 } 423 }
427 424
428 return newSVpvn (oid, app - oid); 425 return newSVpvn (oid, app - oid);
476 int tag = identifier & ASN_TAG_MASK; 473 int tag = identifier & ASN_TAG_MASK;
477 474
478 if (tag == ASN_TAG_BER) 475 if (tag == ASN_TAG_BER)
479 tag = get_w (); 476 tag = get_w ();
480 477
481 if (tag == ASN_TAG_BER)
482 tag = get_w ();
483
484 if (constructed) 478 if (constructed)
485 { 479 {
486 UV len = get_length (); 480 UV len = get_length ();
487 UV seqend = (cur - buf) + len; 481 UV seqend = (cur - buf) + len;
488 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 482 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
489 483
490 while (cur < buf + seqend) 484 while (cur < buf + seqend)
491 av_push (av, decode_ber ()); 485 av_push (av, decode_ber ());
492 486
493 if (cur > buf + seqend) 487 if (cur > buf + seqend)
494 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);
495 489
496 res = newRV_inc ((SV *)av); 490 res = newRV_inc ((SV *)av);
497 } 491 }
498 else 492 else
499 switch (profile_lookup (cur_profile, klass, tag)) 493 switch (profile_lookup (cur_profile, klass, tag))
501 case BER_TYPE_NULL: 495 case BER_TYPE_NULL:
502 { 496 {
503 UV len = get_length (); 497 UV len = get_length ();
504 498
505 if (len) 499 if (len)
506 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);
507 501
508 res = &PL_sv_undef; 502 res = &PL_sv_undef;
509 } 503 }
510 break; 504 break;
511 505
512 case BER_TYPE_BOOL: 506 case BER_TYPE_BOOL:
513 { 507 {
514 UV len = get_length (); 508 UV len = get_length ();
515 509
516 if (len != 1) 510 if (len != 1)
517 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);
518 512
519 res = newSVcacheint (!!get_u8 ()); 513 res = newSVcacheint (!!get_u8 ());
520 } 514 }
521 break; 515 break;
522 516
544 case BER_TYPE_IPADDRESS: 538 case BER_TYPE_IPADDRESS:
545 { 539 {
546 UV len = get_length (); 540 UV len = get_length ();
547 541
548 if (len != 4) 542 if (len != 4)
549 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);
550 544
551 U8 c1 = get_u8 (); 545 U8 *data = get_n (4);
552 U8 c2 = get_u8 (); 546 res = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]);
553 U8 c3 = get_u8 ();
554 U8 c4 = get_u8 ();
555
556 res = newSVpvf ("%d.%d.%d.%d", c1, c2, c3, c4);
557 } 547 }
558 break; 548 break;
559 549
560 case BER_TYPE_UCS2: 550 case BER_TYPE_UCS2:
561 res = decode_ucs (2); 551 res = decode_ucs (2);
564 case BER_TYPE_UCS4: 554 case BER_TYPE_UCS4:
565 res = decode_ucs (4); 555 res = decode_ucs (4);
566 break; 556 break;
567 557
568 case BER_TYPE_REAL: 558 case BER_TYPE_REAL:
559 error ("BER_TYPE_REAL not implemented");
560
569 case BER_TYPE_CROAK: 561 case BER_TYPE_CROAK:
562 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
563
570 default: 564 default:
571 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 565 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
572 } 566 }
573 567
574 AV *av = newAV (); 568 AV *av = newAV ();
589strlen_sum (STRLEN l1, STRLEN l2) 583strlen_sum (STRLEN l1, STRLEN l2)
590{ 584{
591 size_t sum = l1 + l2; 585 size_t sum = l1 + l2;
592 586
593 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) 587 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
594 croak ("JSON::XS: string size overflow"); 588 croak ("Convert::BER::XS: string size overflow");
595 589
596 return sum; 590 return sum;
597} 591}
598 592
599static void 593static void
664} 658}
665 659
666static U8 * 660static U8 *
667put_length_at (UV val, U8 *cur) 661put_length_at (UV val, U8 *cur)
668{ 662{
669 if (val < 0x7fU) 663 if (val <= 0x7fU)
670 *cur++ = val; 664 *cur++ = val;
671 else 665 else
672 { 666 {
673 U8 *lenb = cur++; 667 U8 *lenb = cur++;
674 668
675#if UVSIZE > 4 669#if UVSIZE > 4
676 *cur = val >> 56; cur += *cur > 0; 670 *cur = val >> 56; cur += val >= ((UV)1 << (8 * 7));
677 *cur = val >> 48; cur += *cur > 0; 671 *cur = val >> 48; cur += val >= ((UV)1 << (8 * 6));
678 *cur = val >> 40; cur += *cur > 0; 672 *cur = val >> 40; cur += val >= ((UV)1 << (8 * 5));
679 *cur = val >> 32; cur += *cur > 0; 673 *cur = val >> 32; cur += val >= ((UV)1 << (8 * 4));
680#endif 674#endif
681 *cur = val >> 24; cur += *cur > 0; 675 *cur = val >> 24; cur += val >= ((UV)1 << (8 * 3));
682 *cur = val >> 16; cur += *cur > 0; 676 *cur = val >> 16; cur += val >= ((UV)1 << (8 * 2));
683 *cur = val >> 8; cur += *cur > 0; 677 *cur = val >> 8; cur += val >= ((UV)1 << (8 * 1));
684 *cur = val ; cur += 1; 678 *cur = val ; cur += 1;
685 679
686 *lenb = 0x80 + cur - lenb - 1; 680 *lenb = 0x80 + cur - lenb - 1;
687 } 681 }
688 682
690} 684}
691 685
692static void 686static void
693put_length (UV val) 687put_length (UV val)
694{ 688{
695 need (5 + val); 689 need (9 + val);
696 cur = put_length_at (val, cur); 690 cur = put_length_at (val, cur);
697} 691}
698 692
699// return how many bytes the encoded length requires 693// return how many bytes the encoded length requires
700static int length_length (UV val) 694static int length_length (UV val)
701{ 695{
702 return val < 0x7fU 696 // use hashing with a DeBruin sequence, anyone?
697 return expect_true (val <= 0x7fU)
703 ? 1 698 ? 1
704 : 2 699 : 2
705 + (val > 0xffU) 700 + (val > 0x000000000000ffU)
706 + (val > 0xffffU) 701 + (val > 0x0000000000ffffU)
707 + (val > 0xffffffU) 702 + (val > 0x00000000ffffffU)
708#if UVSIZE > 4 703#if UVSIZE > 4
709 + (val > 0xffffffffU) 704 + (val > 0x000000ffffffffU)
710 + (val > 0xffffffffffU) 705 + (val > 0x0000ffffffffffU)
711 + (val > 0xffffffffffffU) 706 + (val > 0x00ffffffffffffU)
712 + (val > 0xffffffffffffffU) 707 + (val > 0xffffffffffffffU)
713#endif 708#endif
714 ; 709 ;
715} 710}
716 711
925 // and adjust later 920 // and adjust later
926 need (1); 921 need (1);
927 STRLEN mark = len_fixup_mark (); 922 STRLEN mark = len_fixup_mark ();
928 923
929 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) 924 if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV))
930 croak ("BER constructed data must be array-reference"); 925 croak ("BER CONSTRUCTED data must be array-reference");
931 926
932 AV *av = (AV *)SvRV (data); 927 AV *av = (AV *)SvRV (data);
933 int fill = AvFILL (av); 928 int fill = AvFILL (av);
934 929
935 if (expect_false (SvRMAGICAL (av))) 930 if (expect_false (SvRMAGICAL (av)))
936 croak ("BER constructed data must not be tied"); 931 croak ("BER CONSTRUCTED data must not be tied");
937 932
938 int i; 933 int i;
939 for (i = 0; i <= fill; ++i) 934 for (i = 0; i <= fill; ++i)
940 encode_ber (AvARRAY (av)[i]); 935 encode_ber (AvARRAY (av)[i]);
941 936
996 case BER_TYPE_UCS4: 991 case BER_TYPE_UCS4:
997 encode_ucs (data, 4); 992 encode_ucs (data, 4);
998 break; 993 break;
999 994
1000 case BER_TYPE_REAL: 995 case BER_TYPE_REAL:
996 croak ("BER_TYPE_REAL not implemented");
997
1001 case BER_TYPE_CROAK: 998 case BER_TYPE_CROAK:
999 croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag);
1000
1002 default: 1001 default:
1003 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 1002 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
1004 } 1003 }
1005 1004
1006} 1005}
1078 const_iv (BER_TYPE_IPADDRESS) 1077 const_iv (BER_TYPE_IPADDRESS)
1079 const_iv (BER_TYPE_CROAK) 1078 const_iv (BER_TYPE_CROAK)
1080 1079
1081 const_iv (SNMP_IPADDRESS) 1080 const_iv (SNMP_IPADDRESS)
1082 const_iv (SNMP_COUNTER32) 1081 const_iv (SNMP_COUNTER32)
1082 const_iv (SNMP_GAUGE32)
1083 const_iv (SNMP_UNSIGNED32) 1083 const_iv (SNMP_UNSIGNED32)
1084 const_iv (SNMP_TIMETICKS) 1084 const_iv (SNMP_TIMETICKS)
1085 const_iv (SNMP_OPAQUE) 1085 const_iv (SNMP_OPAQUE)
1086 const_iv (SNMP_COUNTER64) 1086 const_iv (SNMP_COUNTER64)
1087 }; 1087 };
1088 1088
1089 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--)
1090 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); 1090 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1091} 1091}
1092 1092
1093SV * 1093void
1094ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1094ber_decode (SV *ber, SV *profile = &PL_sv_undef)
1095 ALIAS:
1096 ber_decode_prefix = 1
1095 CODE: 1097 PPCODE:
1096{ 1098{
1097 cur_profile = SvPROFILE (profile); 1099 cur_profile = SvPROFILE (profile);
1098 STRLEN len; 1100 STRLEN len;
1099 buf = (U8 *)SvPVbyte (ber, len); 1101 buf = (U8 *)SvPVbyte (ber, len);
1100 cur = buf; 1102 cur = buf;
1101 end = buf + len; 1103 end = buf + len;
1102 1104
1103 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");
1104} 1114}
1105 OUTPUT: RETVAL
1106 1115
1107void 1116void
1108ber_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)
1109 PPCODE: 1118 PPCODE:
1110{ 1119{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines