logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

BER - Basic Encoding Rules (BER) of Abstract Syntax Notation One (ASN.1)

Authors

       Created by:  Simon Leinen  <simon.leinen@switch.ch>

       Contributions and fixes by:

       Andrzej Tobola <san@iem.pw.edu.pl>:  Added long String decode
       Tobias Oetiker <tobi@oetiker.ch>:  Added 5 Byte Integer decode ...
       Dave Rand <dlr@Bungi.com>:  Added "SysUpTime" decode
       Philippe Simonet <sip00@vg.swissptt.ch>:  Support larger subids
       Yufang HU <yhu@casc.com>:  Support even larger subids
       Mike Mitchell <Mike.Mitchell@sas.com>: New generalized "encode_int()"
       Mike Diehn <mdiehn@mindspring.net>: "encode_ip_address()"
       Rik Hoorelbeke <rik.hoorelbeke@pandora.be>: "encode_oid()" fix
       Brett T Warden <wardenb@eluminant.com>: pretty "UInteger32"
       Bert Driehuis <driehuis@playbeing.org>: Handle SNMPv2 exception codes
       Jakob Ilves (/IlvJa) <jakob.ilves@oracle.com>: PDU decoding
       Jan Kasprzak <kas@informatics.muni.cz>: Fix for PDU syntax check
       Milen Pavlov <milen@batmbg.com>: Recognize variant length for ints

Description

       This is a simple library to encode and decode data using the Basic Encoding Rules (BER) of Abstract
       Syntax Notation One (ASN.1).  It does not claim to be a complete implementation of the standard, but
       implements enough of the BER standard to encode and decode SNMP messages.

Methods

encode_int_0()-encodetheinteger0.
       This is functionally identical to encode_int(0).

   encode_int()-encodeanintegerusingthegeneric"integer"typetag.encode_uinteger32()-encodeanintegerusingtheSNMPUInteger32tag.encode_counter32()-encodeanintegerusingtheSNMPCounter32tag.encode_counter64()-encodeanintegerusingtheSNMPCounter64tag.encode_gauge32()-encodeanintegerusingtheSNMPGauge32tag.encode_oid()-encodeanobjectID,passedasalistofsub-IDs.
           $encoded = encode_oid (1,3,6,1,...);

   encode_null()-encodeanullobject.
       This is used e.g. in binding lists for variables that don't have a value (yet)

   encode_sequence()encode_tagged_sequence()
           $encoded = encode_sequence (encoded1, encoded2, ...);
           $encoded = encode_tagged_sequence (tag, encoded1, encoded2, ...);

       Take already encoded values, and extend them to an encoded sequence.  "encoded_sequence" uses the generic
       sequence tag, while with "encode_tagged_sequence" you can specify your own tag.

   encode_string()-encodeaPerlstringasanOCTETSTRING.encode_ip_address()-encodeanIPv4address.
       This can either be passed as a four-octet sequence in networkbyteorder, or as a text string in dotted-
       quad notation, e.g. "192.0.2.234".

   encode_timeticks()-encodeanintegerasa"TimeTicks"object.
       The integer should count hundredths of a second since the epoch defined by "sysUpTime.0".

   pretty_print()-convertanencodedbytesequenceintohuman-readableform.
       This function can be extended by registering pretty-printing methods for specific type codes.  Most BER
       type codes used in SNMP already have such methods pre-registered by default.  See
       "register_pretty_printer" for how new methods can be added.

   hex_string()-convertOCTETSTRINGtohexadecimalnotation.hex_string_of_type()-convertoctetstringtohex,andchecktypeagainstgiventag.decode_by_template()-decodecomplexobjectaccordingtoatemplate.
           ($var1, ...) = decode_by_template ($pdu, $template, ...);

       The template can contain various %X directives.  Some directives consume additional arguments following
       the template itself.  Most directives will cause values to be returned.  The values are returned as a
       sequence in the order of the directives that generated them.

       %{ - decode sequence.
           This  doesn't  assign  any return value, just checks and skips the tag/length fields of the sequence.
           By default, the tag should be the generic sequence tag, but a  tag  can  also  be  specified  in  the
           directive.  The directive can either specify the tag as a prefix, e.g. "%99{" will require a sequence
           tag of 99, or if the directive is given as "%*{", the tag will be taken from the next argument.

       %s - decode string
       %i - decode integer
       %u - decode unsigned integer
       %O - decode Object ID (OID)
       %A - decode IPv4 address
       %@ - assigns the remaining undecoded part of the PDU to the next return value.

   decode_sequence()-Splitsequenceintocomponents.
           ($first, $rest) = decode_sequence ($pdu);

       Checks  whether the PDU has a sequence type tag and a plausible length field.  Splits the initial element
       off the list, and returns both this and the remainder of the PDU.

   register_pretty_printer()-registerpretty-printingmethodsfortypecodes.
       This function takes a hashref that specifies functions to call when the specified  value  type  is  being
       printed.  It returns the number of functions that were registered.

Name

       BER - Basic Encoding Rules (BER) of Abstract Syntax Notation One (ASN.1)

Synopsis

           use BER;
           $encoded = encode_sequence (encode_int (123), encode_string ("foo"));
           ($i, $s) = decode_by_template ($encoded, "%{%i%s");
           # $i will now be 123, $s the string "foo".

Variables

$pretty_print_timeticks(default:1)
       If non-zero (the default), "pretty_print" will convert TimeTicks to "human readable" strings containing
       days, hours, minutes and seconds.

       If the variable is zero, "pretty_print" will simply return an unsigned integer representing hundredths of
       seconds.  If you prefer this, bind $pretty_print_timeticks to zero.

   $errmsg-errormessagefromlastfailedoperation.
       When they encounter errors, the routines in this module will generally return "undef") and leave an
       informative error message in $errmsg).

See Also