… | |
… | |
69 | |
69 | |
70 | my $buf = ber_encode $ber, $Convert::BER::XS::SNMP_PROFILE; |
70 | my $buf = ber_encode $ber, $Convert::BER::XS::SNMP_PROFILE; |
71 | |
71 | |
72 | =head1 DESCRIPTION |
72 | =head1 DESCRIPTION |
73 | |
73 | |
74 | WARNING: Before release 1.0, the API is not considered stable in any way. |
|
|
75 | |
|
|
76 | This module implements a I<very> low level BER/DER en-/decoder. |
74 | This module implements a I<very> low level BER/DER en-/decoder. |
77 | |
75 | |
78 | It is tuned for low memory and high speed, while still maintaining some |
76 | It is tuned for low memory and high speed, while still maintaining some |
79 | level of user-friendlyness. |
77 | level of user-friendlyness. |
80 | |
78 | |
… | |
… | |
409 | use common::sense; |
407 | use common::sense; |
410 | |
408 | |
411 | use XSLoader (); |
409 | use XSLoader (); |
412 | use Exporter qw(import); |
410 | use Exporter qw(import); |
413 | |
411 | |
|
|
412 | use Carp (); |
|
|
413 | |
414 | our $VERSION; |
414 | our $VERSION; |
415 | |
415 | |
416 | BEGIN { |
416 | BEGIN { |
417 | $VERSION = 1.11; |
417 | $VERSION = 1.21; |
418 | XSLoader::load __PACKAGE__, $VERSION; |
418 | XSLoader::load __PACKAGE__, $VERSION; |
419 | } |
419 | } |
420 | |
420 | |
421 | our %EXPORT_TAGS = ( |
421 | our %EXPORT_TAGS = ( |
422 | const_index => [qw( |
422 | const_index => [qw( |
… | |
… | |
469 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_IPADDRESS , BER_TYPE_IPADDRESS); |
469 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_IPADDRESS , BER_TYPE_IPADDRESS); |
470 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER32 , BER_TYPE_INT); |
470 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER32 , BER_TYPE_INT); |
471 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_UNSIGNED32, BER_TYPE_INT); |
471 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_UNSIGNED32, BER_TYPE_INT); |
472 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_TIMETICKS , BER_TYPE_INT); |
472 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_TIMETICKS , BER_TYPE_INT); |
473 | |
473 | |
|
|
474 | # decodes REAL values according to ECMA-63 |
|
|
475 | # this is pretty strict, except it doesn't catch -0. |
|
|
476 | # I don't have access to ISO 6093 (or BS 6727, or ANSI X.3-42)), so this is all guesswork. |
|
|
477 | sub _decode_real_decimal { |
|
|
478 | my ($format, $val) = @_; |
|
|
479 | |
|
|
480 | $val =~ y/,/./; # probably not in ISO-6093 |
|
|
481 | |
|
|
482 | if ($format == 1) { |
|
|
483 | $val =~ /^ \ * [+-]? [0-9]+ \z/x |
|
|
484 | or Carp::croak "BER_TYPE_REAL NR1 value not in NR1 format ($val) (X.690 8.5.8)"; |
|
|
485 | } elsif ($format == 2) { |
|
|
486 | $val =~ /^ \ * [+-]? (?: [0-9]+\.[0-9]* | [0-9]*\.[0-9]+ ) \z/x |
|
|
487 | or Carp::croak "BER_TYPE_REAL NR2 value not in NR2 format ($val) (X.690 8.5.8)"; |
|
|
488 | } elsif ($format == 3) { |
|
|
489 | $val =~ /^ \ * [+-] (?: [0-9]+\.[0-9]* | [0-9]*\.[0-9]+ ) [eE] [+-]? [0-9]+ \z/x |
|
|
490 | or Carp::croak "BER_TYPE_REAL NR3 value not in NR3 format ($val) (X.690 8.5.8)"; |
|
|
491 | } else { |
|
|
492 | Carp::croak "BER_TYPE_REAL invalid decimal numerical representation format $format"; |
|
|
493 | } |
|
|
494 | |
|
|
495 | $val |
|
|
496 | } |
|
|
497 | |
|
|
498 | # this is a mess, but perl's support for floating point formatting is nearly nonexistant |
|
|
499 | sub _encode_real_decimal { |
|
|
500 | my ($val, $nvdig) = @_; |
|
|
501 | |
|
|
502 | $val = sprintf "%.*G", $nvdig + 1, $val; |
|
|
503 | |
|
|
504 | if ($val =~ /E/) { |
|
|
505 | $val =~ s/E(?=[^+-])/E+/; |
|
|
506 | $val =~ s/E/.E/ if $val !~ /\./; |
|
|
507 | $val =~ s/^/+/ unless $val =~ /^-/; |
|
|
508 | |
|
|
509 | return "\x03$val" # NR3 |
|
|
510 | } |
|
|
511 | |
|
|
512 | $val =~ /\./ |
|
|
513 | ? "\x02$val" # NR2 |
|
|
514 | : "\x01$val" # NR1 |
|
|
515 | } |
|
|
516 | |
474 | =head2 DEBUGGING |
517 | =head2 DEBUGGING |
475 | |
518 | |
476 | To aid debugging, you cna call the C<ber_dump> function to print a "nice" |
519 | To aid debugging, you can call the C<ber_dump> function to print a "nice" |
477 | representation to STDOUT. |
520 | representation to STDOUT. |
478 | |
521 | |
479 | =over |
522 | =over |
480 | |
523 | |
481 | =item ber_dump $tuple[, $profile[, $prefix]] |
524 | =item ber_dump $tuple[, $profile[, $prefix]] |
… | |
… | |
684 | dot, e.g. C<1.3.6.1.213>. |
727 | dot, e.g. C<1.3.6.1.213>. |
685 | |
728 | |
686 | =item C<BER_TYPE_RELOID> |
729 | =item C<BER_TYPE_RELOID> |
687 | |
730 | |
688 | Same as C<BER_TYPE_OID> but uses relative object identifier |
731 | Same as C<BER_TYPE_OID> but uses relative object identifier |
689 | encoding: ASN.1 has this hack of encoding the first two OID components |
732 | encoding: ASN.1 uses some hack encoding of the first two OID components |
690 | into a single integer in a weird attempt to save an insignificant amount |
733 | into a single integer in a weird attempt to save an insignificant amount |
691 | of space in an otherwise wasteful encoding, and relative OIDs are |
734 | of space in an otherwise wasteful encoding, and relative OIDs are |
692 | basically OIDs without this hack. The practical difference is that the |
735 | basically OIDs without this hack. The practical difference is that the |
693 | second component of an OID can only have the values 1..40, while relative |
736 | second component of an OID can only have the values 1..40, while relative |
694 | OIDs do not have this restriction. |
737 | OIDs do not have this restriction. |
… | |
… | |
753 | about 4kB. |
796 | about 4kB. |
754 | |
797 | |
755 | Constructed strings are decoded just fine, but there should be a way to |
798 | Constructed strings are decoded just fine, but there should be a way to |
756 | join them for convenience. |
799 | join them for convenience. |
757 | |
800 | |
758 | REAL values are not supported and will currently croak. |
801 | REAL values will always be encoded in decimal form and ssometimes is |
|
|
802 | forced into a perl "NV" type, potentially losing precision. |
759 | |
803 | |
760 | =head2 ITHREADS SUPPORT |
804 | =head2 ITHREADS SUPPORT |
761 | |
805 | |
762 | This module is unlikely to work in any other than the loading thread when |
806 | This module is unlikely to work in any other than the loading thread when |
763 | the (officially discouraged) ithreads are in use. |
807 | the (officially discouraged) ithreads are in use. |