… | |
… | |
108 | BER_CLASS BER_TAG BER_FLAGS BER_DATA |
108 | BER_CLASS BER_TAG BER_FLAGS BER_DATA |
109 | |
109 | |
110 | =item C<:const_asn> |
110 | =item C<:const_asn> |
111 | |
111 | |
112 | ASN class values (these are C<0>, C<1>, C<2> and C<3>, respectively - |
112 | ASN class values (these are C<0>, C<1>, C<2> and C<3>, respectively - |
113 | exactly thw two topmost bits from the identifier octet shifted 6 bits to |
113 | exactly the two topmost bits from the identifier octet shifted 6 bits to |
114 | the right): |
114 | the right): |
115 | |
115 | |
116 | ASN_UNIVERSAL ASN_APPLICATION ASN_CONTEXT ASN_PRIVATE |
116 | ASN_UNIVERSAL ASN_APPLICATION ASN_CONTEXT ASN_PRIVATE |
117 | |
117 | |
118 | ASN tag values (some of which are aliases, such as C<ASN_OID>). Their |
118 | ASN tag values (some of which are aliases, such as C<ASN_OID>). Their |
… | |
… | |
296 | Also, since BER is self-delimited, this can be used to decode multiple BER |
296 | Also, since BER is self-delimited, this can be used to decode multiple BER |
297 | values joined together. |
297 | values joined together. |
298 | |
298 | |
299 | =item $bindata = ber_encode $tuple[, $profile] |
299 | =item $bindata = ber_encode $tuple[, $profile] |
300 | |
300 | |
301 | Encodes the BER tuple into a BER/DER data structure. AS with |
301 | Encodes the BER tuple into a BER/DER data structure. As with |
302 | Cyber_decode>, an optional profile can be given. |
302 | Cyber_decode>, an optional profile can be given. |
303 | |
303 | |
304 | The encoded data should be both BER and DER ("shortest form") compliant |
304 | The encoded data should be both BER and DER ("shortest form") compliant |
305 | unless the input says otherwise (e.g. it uses constructed strings). |
305 | unless the input says otherwise (e.g. it uses constructed strings). |
306 | |
306 | |
… | |
… | |
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.1; |
419 | $VERSION = 1.21; |
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( |
… | |
… | |
468 | |
470 | |
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 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_OPAQUE , BER_TYPE_IPADDRESS); |
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 invalid 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 | } |
474 | |
518 | |
475 | =head2 DEBUGGING |
519 | =head2 DEBUGGING |
476 | |
520 | |
477 | 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" |
478 | representation to STDOUT. |
522 | representation to STDOUT. |
479 | |
523 | |
480 | =over |
524 | =over |
481 | |
525 | |
482 | =item ber_dump $tuple[, $profile[, $prefix]] |
526 | =item ber_dump $tuple[, $profile[, $prefix]] |
483 | |
527 | |
484 | In addition to specifying the BER C<$tuple> to dump, youc an also specify |
528 | In addition to specifying the BER C<$tuple> to dump, you can also specify |
485 | a C<$profile> and a C<$prefix> string that is printed in front of each line. |
529 | a C<$profile> and a C<$prefix> string that is printed in front of each line. |
486 | |
530 | |
487 | If C<$profile> is C<$Convert::BER::XS::SNMP_PROFILE>, then C<ber_dump> |
531 | If C<$profile> is C<$Convert::BER::XS::SNMP_PROFILE>, then C<ber_dump> |
488 | will try to improve its output for SNMP data. |
532 | will try to improve its output for SNMP data. |
489 | |
533 | |
… | |
… | |
733 | |
777 | |
734 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_IPADDRESS , BER_TYPE_IPADDRESS); |
778 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_IPADDRESS , BER_TYPE_IPADDRESS); |
735 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER32 , BER_TYPE_INT); |
779 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER32 , BER_TYPE_INT); |
736 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_UNSIGNED32, BER_TYPE_INT); |
780 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_UNSIGNED32, BER_TYPE_INT); |
737 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_TIMETICKS , BER_TYPE_INT); |
781 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_TIMETICKS , BER_TYPE_INT); |
738 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_OPAQUE , BER_TYPE_IPADDRESS); |
782 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_OPAQUE , BER_TYPE_BYTES); |
739 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER64 , BER_TYPE_INT); |
783 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER64 , BER_TYPE_INT); |
740 | |
784 | |
741 | =head2 LIMITATIONS/NOTES |
785 | =head2 LIMITATIONS/NOTES |
742 | |
786 | |
743 | This module can only en-/decode 64 bit signed and unsigned integers, and |
787 | This module can only en-/decode 64 bit signed and unsigned |
744 | only when your perl supports those. So no UUID OIDs for now (unless you |
788 | integers/tags/lengths, and only when your perl supports those. So no UUID |
745 | map the C<OBJECT IDENTIFIER> tag to something other than C<BER_TYPE_OID>). |
789 | OIDs for now (unless you map the C<OBJECT IDENTIFIER> tag to something |
|
|
790 | other than C<BER_TYPE_OID>). |
746 | |
791 | |
747 | This module does not generally care about ranges, i.e. it will happily |
792 | This module does not generally care about ranges, i.e. it will happily |
748 | de-/encode 64 bit integers into an C<ASN_INTEGER> value, or a negative |
793 | de-/encode 64 bit integers into an C<SNMP_UNSIGNED32> value, or a negative |
749 | number into an C<SNMP_COUNTER64>. |
794 | number into an C<SNMP_COUNTER64>. |
750 | |
795 | |
751 | OBJECT IDENTIFIEERs cannot have unlimited length, although the limit is |
796 | OBJECT IDENTIFIEERs cannot have unlimited length, although the limit is |
752 | much larger than e.g. the one imposed by SNMP or other protocols, and is |
797 | much larger than e.g. the one imposed by SNMP or other protocols, and is |
753 | about 4kB. |
798 | about 4kB. |
754 | |
799 | |
755 | Indefinite length encoding is not supported. |
|
|
756 | |
|
|
757 | 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 |
758 | join them for convenience. |
801 | join them for convenience. |
759 | |
802 | |
760 | REAL values are not supported and will currently croak. |
803 | REAL values will always be encoded in decimal form and ssometimes is |
761 | |
804 | forced into a perl "NV" type, potentially losing precision. |
762 | The encoder and decoder tend to accept more formats than should be |
|
|
763 | strictly supported - security sensitive applications are strongly advised |
|
|
764 | to review the code first. |
|
|
765 | |
|
|
766 | This module has undergone little to no testing so far. |
|
|
767 | |
805 | |
768 | =head2 ITHREADS SUPPORT |
806 | =head2 ITHREADS SUPPORT |
769 | |
807 | |
770 | This module is unlikely to work when the (officially discouraged) ithreads |
808 | This module is unlikely to work in any other than the loading thread when |
771 | are in use. |
809 | the (officially discouraged) ithreads are in use. |
772 | |
810 | |
773 | =head1 AUTHOR |
811 | =head1 AUTHOR |
774 | |
812 | |
775 | Marc Lehmann <schmorp@schmorp.de> |
813 | Marc Lehmann <schmorp@schmorp.de> |
776 | http://software.schmorp.de/pkg/Convert-BER-XS |
814 | http://software.schmorp.de/pkg/Convert-BER-XS |