java.lang.Object
org.snmp4j.asn1.BER
The BER class provides utility methods for the BER encoding and decoding.
- Version:
- 2.5.8
- Author:
- Frank Fock, Jochen Katz
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
TheMutableByte
class serves for exchanging type information from the various decode* methods. -
Field Summary
Modifier and TypeFieldDescriptionstatic final byte
static final byte
static final byte
static final byte
static final byte
static final byte
static final byte
static final byte
static final byte
static final byte
static final byte
static final byte
static final byte
static final byte
static final byte
static final byte
static final byte
static final byte
static final byte
static final byte
static final byte
static final int
static final byte
static final byte
static final byte
static final byte
static final byte
static final int
static final int
static final int
static final byte
static final byte
static final byte
static final byte
static final byte
static final byte
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
checkSequenceLength
(int expectedLength, int actualLength, BERSerializable sequence) Checks the length of a sequence, by computing the expected payload length and comparing it with the actual length as given.static void
checkSequenceLength
(int expectedLength, BERSerializable sequence) Deprecated.static BigInteger
decodeBigInteger
(BERInputStream is, BER.MutableByte type) static int
decodeHeader
(BERInputStream is, BER.MutableByte type) Decodes an ASN.1 header for an object with the ID and length specified.static int
decodeHeader
(BERInputStream is, BER.MutableByte type, boolean checkLength) Decodes an ASN.1 header for an object with the ID and length specified.static int
decodeInteger
(BERInputStream is, BER.MutableByte type) static int
Decodes a ASN.1 length.static int
decodeLength
(BERInputStream is, boolean checkLength) Decodes a ASN.1 length.static void
decodeNull
(BERInputStream is, BER.MutableByte type) static int[]
decodeOID
(BERInputStream is, BER.MutableByte type) static byte[]
decodeString
(BERInputStream is, BER.MutableByte type) static long
decodeUnsignedInt64
(BERInputStream is, BER.MutableByte type) static long
static void
encodeBigInteger
(OutputStream os, byte type, BigInteger value) Encode a signed integer.static void
encodeHeader
(OutputStream os, int type, int length) Encodes an ASN.1 header for an object with the ID and length specified.static void
encodeHeader
(OutputStream os, int type, int length, int numBytesLength) Encodes an ASN.1 header for an object with the ID and length specified with a fixed length of the encoded length as supplied.static void
encodeInteger
(OutputStream os, byte type, int value) Encode a signed integer.static void
encodeLength
(OutputStream os, int length) Encodes the length of an ASN.1 object.static void
encodeLength
(OutputStream os, int length, int numLengthBytes) Encodes the length of an ASN.1 object.static void
encodeOID
(OutputStream os, byte type, int[] oid) Encode an ASN.1 oid filled with the supplied oid value.static void
encodeSequence
(OutputStream os, byte type, int length) Encode an ASN.1 header for a sequence with the ID and length specified.static void
encodeString
(OutputStream os, byte type, byte[] string) Encode an ASN.1 octet string filled with the supplied input string.static void
encodeSubID
(OutputStream os, int subID) static void
encodeUnsignedInt64
(OutputStream os, byte type, long value) static void
encodeUnsignedInteger
(OutputStream os, byte type, long value) Encode an unsigned integer.static int
getBERLengthOfLength
(int length) Compute the space needed to encode the length.static int
getBigIntegerBERLength
(BigInteger value) Get the BER encoded length of a BigInteger value.static int
getOIDLength
(int[] value) Gets the payload length in bytes of the BER encoded OID value.static String
static int
getSubIDLength
(int subID) static boolean
static boolean
Gets the SEQUENCE length checking mode.boolean
static void
setCheckFirstSubID012
(boolean checkFirstSubID012) static void
setCheckSequenceLength
(boolean checkSequenceLen) Sets the application wide SEQUENCE length checking mode.void
setCheckValueLength
(boolean checkValueLength)
-
Field Details
-
ASN_BOOLEAN
public static final byte ASN_BOOLEAN- See Also:
-
ASN_INTEGER
public static final byte ASN_INTEGER- See Also:
-
ASN_BIT_STR
public static final byte ASN_BIT_STR- See Also:
-
ASN_OCTET_STR
public static final byte ASN_OCTET_STR- See Also:
-
ASN_NULL
public static final byte ASN_NULL- See Also:
-
ASN_OBJECT_ID
public static final byte ASN_OBJECT_ID- See Also:
-
ASN_SEQUENCE
public static final byte ASN_SEQUENCE- See Also:
-
ASN_SET
public static final byte ASN_SET- See Also:
-
ASN_UNIVERSAL
public static final byte ASN_UNIVERSAL- See Also:
-
ASN_APPLICATION
public static final byte ASN_APPLICATION- See Also:
-
ASN_CONTEXT
public static final byte ASN_CONTEXT- See Also:
-
ASN_PRIVATE
public static final byte ASN_PRIVATE- See Also:
-
ASN_PRIMITIVE
public static final byte ASN_PRIMITIVE- See Also:
-
ASN_CONSTRUCTOR
public static final byte ASN_CONSTRUCTOR- See Also:
-
ASN_LONG_LEN
public static final byte ASN_LONG_LEN- See Also:
-
ASN_EXTENSION_ID
public static final byte ASN_EXTENSION_ID- See Also:
-
ASN_BIT8
public static final byte ASN_BIT8- See Also:
-
INTEGER
public static final byte INTEGER- See Also:
-
INTEGER32
public static final byte INTEGER32- See Also:
-
BITSTRING
public static final byte BITSTRING- See Also:
-
OCTETSTRING
public static final byte OCTETSTRING- See Also:
-
NULL
public static final byte NULL- See Also:
-
OID
public static final byte OID- See Also:
-
SEQUENCE
public static final byte SEQUENCE- See Also:
-
IPADDRESS
public static final byte IPADDRESS- See Also:
-
COUNTER
public static final byte COUNTER- See Also:
-
COUNTER32
public static final byte COUNTER32- See Also:
-
GAUGE
public static final byte GAUGE- See Also:
-
GAUGE32
public static final byte GAUGE32- See Also:
-
TIMETICKS
public static final byte TIMETICKS- See Also:
-
OPAQUE
public static final byte OPAQUE- See Also:
-
COUNTER64
public static final byte COUNTER64- See Also:
-
NOSUCHOBJECT
public static final int NOSUCHOBJECT- See Also:
-
NOSUCHINSTANCE
public static final int NOSUCHINSTANCE- See Also:
-
ENDOFMIBVIEW
public static final int ENDOFMIBVIEW- See Also:
-
MAX_OID_LENGTH
public static final int MAX_OID_LENGTH- See Also:
-
-
Constructor Details
-
BER
public BER()
-
-
Method Details
-
encodeHeader
Encodes an ASN.1 header for an object with the ID and length specified.- Parameters:
os
- anOutputStream
to which the header is encoded.type
- the type of the ASN.1 object. Must be < 30, i.e. no extension octets.length
- the length of the object. The maximum length is 0xFFFFFFFF;- Throws:
IOException
- if the output stream fails to store the encoded header.
-
encodeHeader
public static void encodeHeader(OutputStream os, int type, int length, int numBytesLength) throws IOException Encodes an ASN.1 header for an object with the ID and length specified with a fixed length of the encoded length as supplied.- Parameters:
os
- anOutputStream
to which the header is encoded.type
- the type of the ASN.1 object. Must be < 30, i.e. no extension octets.length
- the length of the object. The maximum length is 0xFFFFFFFF;numBytesLength
- the number of bytes used to encode the length of the length.- Throws:
IOException
- if the output stream fails to store the encoded header.
-
getBERLengthOfLength
public static int getBERLengthOfLength(int length) Compute the space needed to encode the length.- Parameters:
length
- Length to encode- Returns:
- the count of bytes needed to encode the value
length
-
encodeLength
Encodes the length of an ASN.1 object.- Parameters:
os
- anOutputStream
to which the length is encoded.length
- the length of the object. The maximum length is 0xFFFFFFFF;- Throws:
IOException
- if the output stream fails to store the encoded length.
-
encodeLength
Encodes the length of an ASN.1 object.- Parameters:
os
- anOutputStream
to which the length is encoded.length
- the length of the object. The maximum length is 0xFFFFFFFF;numLengthBytes
- the number of bytes to be used to encode the length using the long form.- Throws:
IOException
- if the output stream fails to store the encoded length.
-
encodeInteger
Encode a signed integer.- Parameters:
os
- anOutputStream
to which the length is encoded.type
- the tag type for the integer (typically 0x02)value
- the integer value to encode.- Throws:
IOException
- if the output stream fails to store the encoded integer.
-
encodeBigInteger
public static void encodeBigInteger(OutputStream os, byte type, BigInteger value) throws IOException Encode a signed integer.- Parameters:
os
- anOutputStream
to which the length is encoded.type
- the tag type for the integer (typically 0x02)value
- the integer value to encode.- Throws:
IOException
- if the output stream fails to store the encoded integer.
-
getBigIntegerBERLength
Get the BER encoded length of a BigInteger value.- Parameters:
value
- a BigInteger value with a length that is less 2^31.- Returns:
- the length of the BER encoding of the supplied BigInteger as INTEGER value.
-
encodeUnsignedInteger
Encode an unsigned integer. ASN.1 integer ::= 0x02 asnlength byte {byte}*- Parameters:
os
- anOutputStream
to which the length is encoded.type
- the tag type for the integer (typically 0x02)value
- the integer value to encode.- Throws:
IOException
- if the output stream fails to store the encoded value.
-
encodeString
Encode an ASN.1 octet string filled with the supplied input string.- Parameters:
os
- anOutputStream
to which the length is encoded.type
- the tag type for the integer (typically 0x02)string
- thebyte
array containing the octet string value.- Throws:
IOException
- if the output stream fails to store the encoded value.
-
encodeSequence
Encode an ASN.1 header for a sequence with the ID and length specified. This only works on data types < 30, i.e. no extension octets. The maximum length is 0xFFFF;- Parameters:
os
- anOutputStream
to which the length is encoded.type
- the tag type for the integer (typically 0x02)length
- the length of the sequence to encode.- Throws:
IOException
- if the output stream fails to store the encoded value.
-
getOIDLength
public static int getOIDLength(int[] value) Gets the payload length in bytes of the BER encoded OID value.- Parameters:
value
- an array of unsigned integer values representing an object identifier.- Returns:
- the BER encoded length of the OID without header and length.
-
getSubIDLength
public static int getSubIDLength(int subID) -
encodeOID
Encode an ASN.1 oid filled with the supplied oid value.- Parameters:
os
- anOutputStream
to which the length is encoded.type
- the tag type for the integer (typically 0x06)oid
- theint
array containing the OID value.- Throws:
IOException
- if the output stream fails to store the encoded value.
-
encodeSubID
- Throws:
IOException
-
encodeUnsignedInt64
- Throws:
IOException
-
decodeLength
Decodes a ASN.1 length.- Parameters:
is
- anInputStream
- Returns:
- the decoded length.
- Throws:
IOException
- if the input stream contains an invalid BER encoding or an IO exception occurred while reading from the stream.
-
decodeLength
Decodes a ASN.1 length.- Parameters:
is
- anInputStream
checkLength
- iffalse
length check is always suppressed.- Returns:
- the decoded length.
- Throws:
IOException
- if the input stream contains an invalid BER encoding or an IO exception occurred while reading from the stream.
-
decodeHeader
public static int decodeHeader(BERInputStream is, BER.MutableByte type, boolean checkLength) throws IOException Decodes an ASN.1 header for an object with the ID and length specified. On entry, datalength is input as the number of valid bytes following "data". On exit, it is returned as the number of valid bytes in this object following the id and length. This only works on data types < 30, i.e. no extension octets. The maximum length is 0xFFFF;- Parameters:
is
- the BERInputStream to decode.type
- returns the type of the object at the current position in the input stream.checkLength
- iffalse
length check is always suppressed.- Returns:
- the decoded length of the object.
- Throws:
IOException
- if the input stream contains an invalid BER encoding or an IO exception occurred while reading from the stream.
-
decodeHeader
Decodes an ASN.1 header for an object with the ID and length specified. On entry, datalength is input as the number of valid bytes following "data". On exit, it is returned as the number of valid bytes in this object following the id and length. This only works on data types < 30, i.e. no extension octets. The maximum length is 0xFFFF;- Parameters:
is
- the BERInputStream to decode.type
- returns the type of the object at the current position in the input stream.- Returns:
- the decoded length of the object.
- Throws:
IOException
- if the input stream contains an invalid BER encoding or an IO exception occurred while reading from the stream.
-
decodeInteger
- Throws:
IOException
-
decodeBigInteger
public static BigInteger decodeBigInteger(BERInputStream is, BER.MutableByte type) throws IOException - Throws:
IOException
-
getPositionMessage
-
decodeUnsignedInteger
public static long decodeUnsignedInteger(BERInputStream is, BER.MutableByte type) throws IOException - Throws:
IOException
-
decodeString
- Throws:
IOException
-
decodeOID
- Throws:
IOException
-
decodeNull
- Throws:
IOException
-
decodeUnsignedInt64
- Throws:
IOException
-
isCheckSequenceLength
public static boolean isCheckSequenceLength()Gets the SEQUENCE length checking mode.- Returns:
true
if the length of a parsed SEQUENCE should be checked against the real length of the objects parsed.
-
setCheckSequenceLength
public static void setCheckSequenceLength(boolean checkSequenceLen) Sets the application wide SEQUENCE length checking mode.- Parameters:
checkSequenceLen
- specifies whether he length of a parsed SEQUENCE should be checked against the real length of the objects parsed.
-
checkSequenceLength
@Deprecated public static void checkSequenceLength(int expectedLength, BERSerializable sequence) throws IOException Deprecated.UsecheckSequenceLength(int, int, BERSerializable)
because it is immune against longer than minimum numeric value encodings.Checks the length of a sequence, by computing the expected payload length and comparing it with the expected length as given in the header of the BER sequence. IfisCheckSequenceLength()
and the length differ anIOException
is thrown.- Parameters:
expectedLength
- the expected length as encoded in the header of the BER sequence to check.sequence
- the BER sequence to validate.- Throws:
IOException
- is thrown ifisCheckSequenceLength()
and the length differ.
-
checkSequenceLength
public static void checkSequenceLength(int expectedLength, int actualLength, BERSerializable sequence) throws IOException Checks the length of a sequence, by computing the expected payload length and comparing it with the actual length as given. IfisCheckSequenceLength()
and the length differ anIOException
is thrown.- Parameters:
expectedLength
- the expected length as encoded in the header of the BER sequence to check.actualLength
- the number of bytes actually decoded from the BER stream for this sequence.sequence
- the BER sequence to validate.- Throws:
IOException
- is thrown ifisCheckSequenceLength()
and the length differ.
-
isCheckValueLength
public boolean isCheckValueLength() -
setCheckValueLength
public void setCheckValueLength(boolean checkValueLength) -
isCheckFirstSubID012
public static boolean isCheckFirstSubID012() -
setCheckFirstSubID012
public static void setCheckFirstSubID012(boolean checkFirstSubID012)
-
checkSequenceLength(int, int, BERSerializable)
because it is immune against longer than minimum numeric value encodings.