ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Convert-BER-XS/XS.pm
(Generate patch)

Comparing Convert-BER-XS/XS.pm (file contents):
Revision 1.54 by root, Tue Apr 23 21:03:12 2019 UTC vs.
Revision 1.59 by root, Fri May 3 08:49:01 2019 UTC

409use common::sense; 409use common::sense;
410 410
411use XSLoader (); 411use XSLoader ();
412use Exporter qw(import); 412use Exporter qw(import);
413 413
414use Carp ();
415
414our $VERSION; 416our $VERSION;
415 417
416BEGIN { 418BEGIN {
417 $VERSION = 1.11; 419 $VERSION = 1.2;
418 XSLoader::load __PACKAGE__, $VERSION; 420 XSLoader::load __PACKAGE__, $VERSION;
419} 421}
420 422
421our %EXPORT_TAGS = ( 423our %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.
479sub _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
501sub _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
476To aid debugging, you cna call the C<ber_dump> function to print a "nice" 521To aid debugging, you can call the C<ber_dump> function to print a "nice"
477representation to STDOUT. 522representation to STDOUT.
478 523
479=over 524=over
480 525
481=item ber_dump $tuple[, $profile[, $prefix]] 526=item ber_dump $tuple[, $profile[, $prefix]]
737 $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_OPAQUE , BER_TYPE_BYTES); 782 $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_OPAQUE , BER_TYPE_BYTES);
738 $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER64 , BER_TYPE_INT); 783 $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER64 , BER_TYPE_INT);
739 784
740=head2 LIMITATIONS/NOTES 785=head2 LIMITATIONS/NOTES
741 786
742This module can only en-/decode 64 bit signed and unsigned integers, and 787This module can only en-/decode 64 bit signed and unsigned
743only when your perl supports those. So no UUID OIDs for now (unless you 788integers/tags/lengths, and only when your perl supports those. So no UUID
744map the C<OBJECT IDENTIFIER> tag to something other than C<BER_TYPE_OID>). 789OIDs for now (unless you map the C<OBJECT IDENTIFIER> tag to something
790other than C<BER_TYPE_OID>).
745 791
746This module does not generally care about ranges, i.e. it will happily 792This module does not generally care about ranges, i.e. it will happily
747de-/encode 64 bit integers into an C<ASN_INTEGER> value, or a negative 793de-/encode 64 bit integers into an C<SNMP_UNSIGNED32> value, or a negative
748number into an C<SNMP_COUNTER64>. 794number into an C<SNMP_COUNTER64>.
749 795
750OBJECT IDENTIFIEERs cannot have unlimited length, although the limit is 796OBJECT IDENTIFIEERs cannot have unlimited length, although the limit is
751much larger than e.g. the one imposed by SNMP or other protocols, and is 797much larger than e.g. the one imposed by SNMP or other protocols, and is
752about 4kB. 798about 4kB.
753 799
754Indefinite length encoding is not supported.
755
756Constructed strings are decoded just fine, but there should be a way to 800Constructed strings are decoded just fine, but there should be a way to
757join them for convenience. 801join them for convenience.
758 802
759REAL values are not supported and will currently croak. 803REAL values will always be encoded in decimal form and ssometimes is
760 804forced into a perl "NV" type, potentially losing precision.
761The encoder and decoder tend to accept more formats than should be
762strictly supported - security sensitive applications are strongly advised
763to review the code first.
764
765This module has undergone little to no testing so far.
766 805
767=head2 ITHREADS SUPPORT 806=head2 ITHREADS SUPPORT
768 807
769This module is unlikely to work when the (officially discouraged) ithreads 808This module is unlikely to work in any other than the loading thread when
770are in use. 809the (officially discouraged) ithreads are in use.
771 810
772=head1 AUTHOR 811=head1 AUTHOR
773 812
774 Marc Lehmann <schmorp@schmorp.de> 813 Marc Lehmann <schmorp@schmorp.de>
775 http://software.schmorp.de/pkg/Convert-BER-XS 814 http://software.schmorp.de/pkg/Convert-BER-XS

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines