… | |
… | |
409 | use common::sense; |
409 | use common::sense; |
410 | |
410 | |
411 | use XSLoader (); |
411 | use XSLoader (); |
412 | use Exporter qw(import); |
412 | use Exporter qw(import); |
413 | |
413 | |
|
|
414 | use Carp (); |
|
|
415 | |
414 | our $VERSION; |
416 | our $VERSION; |
415 | |
417 | |
416 | BEGIN { |
418 | BEGIN { |
417 | $VERSION = 1.11; |
419 | $VERSION = 1.2; |
418 | XSLoader::load __PACKAGE__, $VERSION; |
420 | XSLoader::load __PACKAGE__, $VERSION; |
419 | } |
421 | } |
420 | |
422 | |
421 | our %EXPORT_TAGS = ( |
423 | our %EXPORT_TAGS = ( |
422 | const_index => [qw( |
424 | const_index => [qw( |
… | |
… | |
469 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_IPADDRESS , BER_TYPE_IPADDRESS); |
471 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_IPADDRESS , BER_TYPE_IPADDRESS); |
470 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER32 , BER_TYPE_INT); |
472 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER32 , BER_TYPE_INT); |
471 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_UNSIGNED32, BER_TYPE_INT); |
473 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_UNSIGNED32, BER_TYPE_INT); |
472 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_TIMETICKS , BER_TYPE_INT); |
474 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_TIMETICKS , BER_TYPE_INT); |
473 | |
475 | |
|
|
476 | # decodes REAL values according to ECMA-63 |
|
|
477 | # this is pretty strict, except it doesn't catch -0. |
|
|
478 | # I don't have access to ISO 6093 (or BS 6727, or ANSI X.3-42)), so this is all guesswork. |
|
|
479 | sub _decode_real_decimal { |
|
|
480 | my ($format, $val) = @_; |
|
|
481 | |
|
|
482 | $val =~ y/,/./; # probably not in ISO-6093 |
|
|
483 | |
|
|
484 | if ($format == 1) { |
|
|
485 | $val =~ /^ \ * [+-]? [0-9]+ \z/x |
|
|
486 | or Carp::croak "BER_TYPE_REAL NR1 value not in NR1 format ($val) (X.690 8.5.8)"; |
|
|
487 | } elsif ($format == 2) { |
|
|
488 | $val =~ /^ \ * [+-]? (?: [0-9]+\.[0-9]* | [0-9]*\.[0-9]+ ) \z/x |
|
|
489 | or Carp::croak "BER_TYPE_REAL NR2 value not in NR2 format ($val) (X.690 8.5.8)"; |
|
|
490 | } elsif ($format == 3) { |
|
|
491 | $val =~ /^ \ * [+-] (?: [0-9]+\.[0-9]* | [0-9]*\.[0-9]+ ) [eE] [+-]? [0-9]+ \z/x |
|
|
492 | or Carp::croak "BER_TYPE_REAL NR3 value not in NR3 format ($val) (X.690 8.5.8)"; |
|
|
493 | } else { |
|
|
494 | Carp::croak "BER_TYPE_REAL illegal decimal numerical representation format $format"; |
|
|
495 | } |
|
|
496 | |
|
|
497 | $val |
|
|
498 | } |
|
|
499 | |
|
|
500 | # this is a mess, but perl's support for floating point formatting is nearly nonexistant |
|
|
501 | sub _encode_real_decimal { |
|
|
502 | my ($val, $nvdig) = @_; |
|
|
503 | |
|
|
504 | $val = sprintf "%.*G", $nvdig + 1, $val; |
|
|
505 | |
|
|
506 | if ($val =~ /E/) { |
|
|
507 | $val =~ s/E(?=[^+-])/E+/; |
|
|
508 | $val =~ s/E/.E/ if $val !~ /\./; |
|
|
509 | $val =~ s/^/+/ unless $val =~ /^-/; |
|
|
510 | |
|
|
511 | return "\x03$val" # NR3 |
|
|
512 | } |
|
|
513 | |
|
|
514 | $val =~ /\./ |
|
|
515 | ? "\x02$val" # NR2 |
|
|
516 | : "\x01$val" # NR1 |
|
|
517 | } |
|
|
518 | |
474 | =head2 DEBUGGING |
519 | =head2 DEBUGGING |
475 | |
520 | |
476 | To aid debugging, you cna call the C<ber_dump> function to print a "nice" |
521 | To aid debugging, you can call the C<ber_dump> function to print a "nice" |
477 | representation to STDOUT. |
522 | representation to STDOUT. |
478 | |
523 | |
479 | =over |
524 | =over |
480 | |
525 | |
481 | =item ber_dump $tuple[, $profile[, $prefix]] |
526 | =item ber_dump $tuple[, $profile[, $prefix]] |
… | |
… | |
753 | about 4kB. |
798 | about 4kB. |
754 | |
799 | |
755 | Constructed strings are decoded just fine, but there should be a way to |
800 | Constructed strings are decoded just fine, but there should be a way to |
756 | join them for convenience. |
801 | join them for convenience. |
757 | |
802 | |
758 | REAL values are not supported and will currently croak. |
803 | REAL values will always be encoded in decimal form and ssometimes is |
|
|
804 | forced into a perl "NV" type, potentially losing precision. |
759 | |
805 | |
760 | =head2 ITHREADS SUPPORT |
806 | =head2 ITHREADS SUPPORT |
761 | |
807 | |
762 | This module is unlikely to work in any other than the loading thread when |
808 | This module is unlikely to work in any other than the loading thread when |
763 | the (officially discouraged) ithreads are in use. |
809 | the (officially discouraged) ithreads are in use. |