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.24 by root, Sun Apr 21 01:58:15 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};
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 integer overflow");
267
261 res = (res << 7) | (c & 0x7f); 268 res = (res << 7) | (c & 0x7f);
262 269
263 if (!(c & 0x80)) 270 if (!(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{
291 286
292 switch (cnt) 287 switch (cnt)
293 { 288 {
294 case 0: 289 case 0:
295 error ("indefinite ASN.1 lengths not supported"); 290 error ("indefinite ASN.1 lengths not supported");
296 return 0;
297 291
298 //case 0x80: // indefinite length 292 case 0x7f:
293 error ("ASN.1 reserved value in length (X.690 8.1.3.5)");
299 294
300 //case 0xff: reserved
301 default: 295 default:
302 error ("ASN.1 length too long"); 296 error ("ASN.1 length too long (only up to 2**64 octets supported)");
303 return 0;
304 297
305 case 8: res = (res << 8) | get_u8 (); 298 case 8: res = (res << 8) | get_u8 ();
306 case 7: res = (res << 8) | get_u8 (); 299 case 7: res = (res << 8) | get_u8 ();
307 case 6: res = (res << 8) | get_u8 (); 300 case 6: res = (res << 8) | get_u8 ();
308 case 5: res = (res << 8) | get_u8 (); 301 case 5: res = (res << 8) | get_u8 ();
320decode_int (void) 313decode_int (void)
321{ 314{
322 UV len = get_length (); 315 UV len = get_length ();
323 316
324 if (!len) 317 if (!len)
325 {
326 error ("invalid integer length equal to zero"); 318 error ("invalid integer length equal to zero (X.690 8.3.1)");
327 return 0;
328 }
329 319
330 U8 *data = get_n (len); 320 U8 *data = get_n (len);
331 321
322 if (expect_false (len > 1))
323 {
324 U16 mask = (data [0] << 8) | data [1] & 0xff80;
325
326 if (expect_false (mask == 0xff80 || mask == 0x0000))
327 error ("illegal padding in integer (X.690 8.3.2)");
328 }
329
332 int negative = data [0] & 0x80; 330 int negative = data [0] & 0x80;
333 331
334 UV val = negative ? -1 : 0; // copy signbit to all bits 332 UV val = negative ? -1 : 0; // copy signbit to all bits
333
334 if (len > UVSIZE + (!negative && !*data))
335 error ("INTEGER overflow");
335 336
336 do 337 do
337 val = (val << 8) | *data++; 338 val = (val << 8) | *data++;
338 while (--len); 339 while (--len);
339 340
392 error ("OBJECT IDENTIFIER length equal to zero"); 393 error ("OBJECT IDENTIFIER length equal to zero");
393 return &PL_sv_undef; 394 return &PL_sv_undef;
394 } 395 }
395 396
396 U8 *end = cur + len; 397 U8 *end = cur + len;
397 UV w = get_w_nopad (); 398 UV w = get_w ();
398 399
399 static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack 400 static char oid[MAX_OID_STRLEN]; // static, because too large for stack
400 char *app = oid; 401 char *app = oid;
401 402
402 if (relative) 403 if (relative)
403 app = write_uv (app, w); 404 app = write_uv (app, w);
404 else if (w < 2 * 40) 405 else if (w < 2 * 40)
418 { 419 {
419 // we assume an oid component is never > 64 digits 420 // we assume an oid component is never > 64 digits
420 if (oid + sizeof (oid) - app < 64) 421 if (oid + sizeof (oid) - app < 64)
421 croak ("BER_TYPE_OID to long to decode"); 422 croak ("BER_TYPE_OID to long to decode");
422 423
423 w = get_w_nopad (); 424 w = get_w ();
424 *app++ = '.'; 425 *app++ = '.';
425 app = write_uv (app, w); 426 app = write_uv (app, w);
426 } 427 }
427 428
428 return newSVpvn (oid, app - oid); 429 return newSVpvn (oid, app - oid);
476 int tag = identifier & ASN_TAG_MASK; 477 int tag = identifier & ASN_TAG_MASK;
477 478
478 if (tag == ASN_TAG_BER) 479 if (tag == ASN_TAG_BER)
479 tag = get_w (); 480 tag = get_w ();
480 481
481 if (tag == ASN_TAG_BER)
482 tag = get_w ();
483
484 if (constructed) 482 if (constructed)
485 { 483 {
486 UV len = get_length (); 484 UV len = get_length ();
487 UV seqend = (cur - buf) + len; 485 UV seqend = (cur - buf) + len;
488 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 486 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
489 487
490 while (cur < buf + seqend) 488 while (cur < buf + seqend)
491 av_push (av, decode_ber ()); 489 av_push (av, decode_ber ());
492 490
493 if (cur > buf + seqend) 491 if (cur > buf + seqend)
494 croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); 492 croak ("constructed type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend);
495 493
496 res = newRV_inc ((SV *)av); 494 res = newRV_inc ((SV *)av);
497 } 495 }
498 else 496 else
499 switch (profile_lookup (cur_profile, klass, tag)) 497 switch (profile_lookup (cur_profile, klass, tag))
501 case BER_TYPE_NULL: 499 case BER_TYPE_NULL:
502 { 500 {
503 UV len = get_length (); 501 UV len = get_length ();
504 502
505 if (len) 503 if (len)
506 croak ("BER_TYPE_NULL value with non-zero length %d encountered", len); 504 croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len);
507 505
508 res = &PL_sv_undef; 506 res = &PL_sv_undef;
509 } 507 }
510 break; 508 break;
511 509
512 case BER_TYPE_BOOL: 510 case BER_TYPE_BOOL:
513 { 511 {
514 UV len = get_length (); 512 UV len = get_length ();
515 513
516 if (len != 1) 514 if (len != 1)
517 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len); 515 croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len);
518 516
519 res = newSVcacheint (!!get_u8 ()); 517 res = newSVcacheint (!!get_u8 ());
520 } 518 }
521 break; 519 break;
522 520
544 case BER_TYPE_IPADDRESS: 542 case BER_TYPE_IPADDRESS:
545 { 543 {
546 UV len = get_length (); 544 UV len = get_length ();
547 545
548 if (len != 4) 546 if (len != 4)
549 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); 547 croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len);
550 548
551 U8 c1 = get_u8 (); 549 U8 c1 = get_u8 ();
552 U8 c2 = get_u8 (); 550 U8 c2 = get_u8 ();
553 U8 c3 = get_u8 (); 551 U8 c3 = get_u8 ();
554 U8 c4 = get_u8 (); 552 U8 c4 = get_u8 ();
589strlen_sum (STRLEN l1, STRLEN l2) 587strlen_sum (STRLEN l1, STRLEN l2)
590{ 588{
591 size_t sum = l1 + l2; 589 size_t sum = l1 + l2;
592 590
593 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) 591 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
594 croak ("JSON::XS: string size overflow"); 592 croak ("Convert::BER::XS: string size overflow");
595 593
596 return sum; 594 return sum;
597} 595}
598 596
599static void 597static void
1078 const_iv (BER_TYPE_IPADDRESS) 1076 const_iv (BER_TYPE_IPADDRESS)
1079 const_iv (BER_TYPE_CROAK) 1077 const_iv (BER_TYPE_CROAK)
1080 1078
1081 const_iv (SNMP_IPADDRESS) 1079 const_iv (SNMP_IPADDRESS)
1082 const_iv (SNMP_COUNTER32) 1080 const_iv (SNMP_COUNTER32)
1081 const_iv (SNMP_GAUGE32)
1083 const_iv (SNMP_UNSIGNED32) 1082 const_iv (SNMP_UNSIGNED32)
1084 const_iv (SNMP_TIMETICKS) 1083 const_iv (SNMP_TIMETICKS)
1085 const_iv (SNMP_OPAQUE) 1084 const_iv (SNMP_OPAQUE)
1086 const_iv (SNMP_COUNTER64) 1085 const_iv (SNMP_COUNTER64)
1087 }; 1086 };
1088 1087
1089 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 1088 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)); 1089 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1091} 1090}
1092 1091
1093SV * 1092void
1094ber_decode (SV *ber, SV *profile = &PL_sv_undef) 1093ber_decode (SV *ber, SV *profile = &PL_sv_undef)
1094 ALIAS:
1095 ber_decode_prefix = 1
1095 CODE: 1096 PPCODE:
1096{ 1097{
1097 cur_profile = SvPROFILE (profile); 1098 cur_profile = SvPROFILE (profile);
1098 STRLEN len; 1099 STRLEN len;
1099 buf = (U8 *)SvPVbyte (ber, len); 1100 buf = (U8 *)SvPVbyte (ber, len);
1100 cur = buf; 1101 cur = buf;
1101 end = buf + len; 1102 end = buf + len;
1102 1103
1103 RETVAL = decode_ber (); 1104 SV *tuple = decode_ber ();
1105
1106 EXTEND (SP, 2);
1107 PUSHs (sv_2mortal (tuple));
1108
1109 if (ix)
1110 PUSHs (sv_2mortal (newSViv (cur - buf)));
1111 else if (cur != end)
1112 error ("trailing garbage after BER data");
1104} 1113}
1105 OUTPUT: RETVAL
1106 1114
1107void 1115void
1108ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) 1116ber_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: 1117 PPCODE:
1110{ 1118{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines