Class BerEncoder

  • All Implemented Interfaces:
    AsnEncoder

    public class BerEncoder
    extends Object
    implements AsnEncoder
    The BerEncoder class is used to implement the AsnEncoder interface for the Basic Encoding Rules (BER). The encoding rules are used to encode and decode SNMP values using BER.
    Author:
    Brian Weaver
    • Constructor Summary

      Constructors 
      Constructor Description
      BerEncoder()
      Default constructor for the BER Encoder.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int buildHeader​(byte[] buf, int startOffset, byte asnType, int asnLength)
      The buildHeader() method is used to encode an ASN.1 header into the specified byte buffer.
      int buildInteger32​(byte[] buf, int startOffset, byte asnType, int asnInt32)
      The buildInteger32() method is used to encode an ASN.1 32-bit signed integer into the specified byte buffer.
      int buildLength​(byte[] buf, int startOffset, int asnLength)
      The buildLength() method is used to encode an ASN.1 length into the specified byte buffer.
      int buildNull​(byte[] buf, int startOffset, byte asnType)
      The buildNull() method is used to encode an ASN.1 NULL value into the specified byte buffer.
      int buildObjectId​(byte[] buf, int startOffset, byte asnType, int[] oids)
      The buildObjectId() method is used to encode an ASN.1 object id value into the specified byte buffer.
      int buildString​(byte[] buf, int startOffset, byte asnType, byte[] opaque)
      The buildString() method is used to encode an ASN.1 string value into the specified byte buffer.
      int buildUInteger32​(byte[] buf, int startOffset, byte asnType, long asnUInt32)
      The buildUInteger32() method is used to encode an ASN.1 32-bit unsigned integer into the specified byte buffer.
      int buildUInteger64​(byte[] buf, int startOffset, byte asnType, BigInteger asnUInt64)
      The buildUInteger64() method is used to encode an ASN.1 64-bit unsigned integer into the specified byte buffer.
      protected static int byteToInt​(byte b)
      Converts a primitive byte to a primitive long using "unsigned" logic.
      protected static long byteToLong​(byte b)
      Converts a primitive byte to a primitive long.
      protected static void copy​(byte[] src, int srcOff, byte[] dest, int destOff, int bytesToCopy)
      Used to copy data from one buffer to another.
      protected static void copy​(int[] src, int srcOff, int[] dest, int destOff, int intsToCopy)
      Used to copy data from one buffer to another.
      protected static boolean isConstructor​(byte b)
      Used to determine if the ASN.1 type is a constructor.
      protected static boolean isExtensionId​(byte b)
      Used to test if the ASN.1 type is an extension.
      Object[] parseHeader​(byte[] buf, int startOffset)
      The parseHeader() method is used to decode an ASN.1 header from the specified buffer.
      Object[] parseInteger32​(byte[] buf, int startOffset)
      The parseInteger32() method is used to decode an ASN.1 32-bit signed integer from the specified buffer.
      Object[] parseLength​(byte[] buf, int startOffset)
      The parseLength() method is used to decode an ASN.1 length from the specified buffer.
      Object[] parseNull​(byte[] buf, int startOffset)
      The parseNull() method is used to decode an ASN.1 Null value from the specified buffer.
      Object[] parseObjectId​(byte[] buf, int startOffset)
      The parseObjectId() method is used to decode an ASN.1 Object Identifer from the specified buffer.
      Object[] parseString​(byte[] buf, int startOffset)
      The parseString() method is used to decode an ASN.1 opaque string from the specified buffer.
      Object[] parseUInteger32​(byte[] buf, int startOffset)
      The parseUInteger32() method is used to decode an ASN.1 32-bit unsigned integer from the specified buffer.
      Object[] parseUInteger64​(byte[] buf, int startOffset)
      The parseUInteger64() method is used to decode an ASN.1 64-bit unsigned integer from the specified buffer.
      protected static void rotate​(byte[] buf, int begin, int pivot, int end)
      Rotates a give buffer area marked by begin, pivot, and end.
    • Constructor Detail

      • BerEncoder

        public BerEncoder()
        Default constructor for the BER Encoder.
    • Method Detail

      • byteToInt

        protected static int byteToInt​(byte b)
        Converts a primitive byte to a primitive long using "unsigned" logic.
        Parameters:
        b - The 8-bit value to convert
        Returns:
        Returns the 32-bit converted value
      • byteToLong

        protected static long byteToLong​(byte b)
        Converts a primitive byte to a primitive long. The byte is converted using "unsigned" logic
        Parameters:
        b - The 8-bit value to convert
        Returns:
        Returns the 64-bit converted value
      • isConstructor

        protected static boolean isConstructor​(byte b)
        Used to determine if the ASN.1 type is a constructor.
        Parameters:
        b - The ASN.1 type
        Returns:
        True if the ASN.1 type is a constructor, otherwise a false is returned.
      • isExtensionId

        protected static boolean isExtensionId​(byte b)
        Used to test if the ASN.1 type is an extension.
        Parameters:
        b - The ASN.1 type.
        Returns:
        True if the ASN.1 type is an extension. False if the ASN.1 type is not an extension.
      • copy

        protected static void copy​(byte[] src,
                                   int srcOff,
                                   byte[] dest,
                                   int destOff,
                                   int bytesToCopy)
                            throws ArrayIndexOutOfBoundsException
        Used to copy data from one buffer to another. The method has the flexability to allow the caller to specify an offset in each buffer and the total number of bytes to copy
        Parameters:
        src - The source buffer
        srcOff - The offset of the first byte in the source buffer
        dest - The destination buffer
        destOff - The offset of the first byte in the destination buffer
        bytesToCopy - The number of bytes to copy
        Throws:
        ArrayIndexOutOfBoundsException - Thrown if there is insufficent space in either array to copy the data.
      • copy

        protected static void copy​(int[] src,
                                   int srcOff,
                                   int[] dest,
                                   int destOff,
                                   int intsToCopy)
                            throws ArrayIndexOutOfBoundsException
        Used to copy data from one buffer to another. The method has the flexability to allow the caller to specify an offset in each buffer and the total number of integers to copy
        Parameters:
        src - The source buffer
        srcOff - The offset of the first integer in the source buffer
        dest - The destination buffer
        destOff - The offset of the first integer in the destination buffer
        intsToCopy - The number of integer elements to copy
        Throws:
        ArrayIndexOutOfBoundsException - Thrown if there is insufficent space in either array to copy the data.
      • rotate

        protected static void rotate​(byte[] buf,
                                     int begin,
                                     int pivot,
                                     int end)
                              throws ArrayIndexOutOfBoundsException
        Rotates a give buffer area marked by begin, pivot, and end. The pivot marks the point where the bytes between [pivot..end) are moved to the position marked by begin. The bytes between [begin..pivot) are shifted such that begin is at [begin+(end-pivot)].
        Parameters:
        buf - The buffer containing the data to rotate
        begin - The start of the rotation
        pivot - The pivot point for the rotation
        end - The end of the rotational buffer
        Throws:
        ArrayIndexOutOfBoundsException - Thrown if an access exception occurs
      • buildLength

        public int buildLength​(byte[] buf,
                               int startOffset,
                               int asnLength)
                        throws AsnEncodingException
        The buildLength() method is used to encode an ASN.1 length into the specified byte buffer. The method is defined in the AsnEncoder interface.
        Specified by:
        buildLength in interface AsnEncoder
        Parameters:
        buf - The output buffer of encoded bytes.
        startOffset - The offset from the start of the buffer where the method should start writing the encoded data.
        asnLength - The length to be encoded.
        Returns:
        Returns the new offset for the next encoding routine. If the startOffset is subtracted from the return value then the length of the encoded data can be determined.
        Throws:
        AsnEncodingException - Thrown if an error occurs encoding the datatype.
      • parseLength

        public Object[] parseLength​(byte[] buf,
                                    int startOffset)
                             throws AsnDecodingException
        The parseLength() method is used to decode an ASN.1 length from the specified buffer. The method is defined to support the AsnEncoder interface.
        Specified by:
        parseLength in interface AsnEncoder
        Parameters:
        buf - The input buffer
        startOffset - The offset to start decoding in the buffer
        Returns:
        Returns an Object array that contains the new offset and the decoded length. The first object is an Integer object and contains the new offset for the next object in buf. The second object is an Integer and contains the actual decoded length.
        Throws:
        AsnDecodingException - Thrown if an error occurs decoding the buffer.
      • buildHeader

        public int buildHeader​(byte[] buf,
                               int startOffset,
                               byte asnType,
                               int asnLength)
                        throws AsnEncodingException
        The buildHeader() method is used to encode an ASN.1 header into the specified byte buffer. The method is defined to support the AsnEncoder interface. This method is dependant on the buildLength() method.
        Specified by:
        buildHeader in interface AsnEncoder
        Parameters:
        buf - The output buffer of encoded bytes.
        startOffset - The offset from the start of the buffer where the method should start writing the encoded data.
        asnType - The ASN.1 type to place in the buffer
        asnLength - The length to be encoded.
        Returns:
        Returns the new offset for the next encoding routine. If startOffset is subtracted from the return value then the length of the encoded data can be determined.
        Throws:
        AsnEncodingException - Thrown if an error occurs encoding the datatype.
      • parseHeader

        public Object[] parseHeader​(byte[] buf,
                                    int startOffset)
                             throws AsnDecodingException
        The parseHeader() method is used to decode an ASN.1 header from the specified buffer. The method is defined to support the AsnEncoder interface. The method also calls the parseLength() method.
        Specified by:
        parseHeader in interface AsnEncoder
        Parameters:
        buf - The input buffer
        startOffset - The offset to start decoding in the buffer
        Returns:
        Returns an Object array that contains the new offset, ASN.1 type, and decoded length. The first object is an Integer object and contains the new offset for the next object in buf. The second object is a Byte object that represents the decoded ASN.1 Type. The third object is an Integer and contains the actual decoded length.
        Throws:
        AsnDecodingException - Thrown if an error occurs decoding the buffer.
      • buildInteger32

        public int buildInteger32​(byte[] buf,
                                  int startOffset,
                                  byte asnType,
                                  int asnInt32)
                           throws AsnEncodingException
        The buildInteger32() method is used to encode an ASN.1 32-bit signed integer into the specified byte buffer.
        Specified by:
        buildInteger32 in interface AsnEncoder
        Parameters:
        buf - The output buffer of encoded bytes.
        startOffset - The offset from the start of the buffer where the method should start writing the encoded data.
        asnType - The ASN.1 type to place in the buffer
        asnInt32 - The 32-bit signed integer to encode.
        Returns:
        Returns the new offset for the next encoding routine. If startOffset is subtracted from the return value then the length of the encoded data can be determined.
        Throws:
        AsnEncodingException - Thrown if an error occurs encoding the datatype.
      • parseInteger32

        public Object[] parseInteger32​(byte[] buf,
                                       int startOffset)
                                throws AsnDecodingException
        The parseInteger32() method is used to decode an ASN.1 32-bit signed integer from the specified buffer.
        Specified by:
        parseInteger32 in interface AsnEncoder
        Parameters:
        buf - The input buffer
        startOffset - The offset to start decoding in the buffer
        Returns:
        Returns an Object array that contains the new offset, ASN.1 type, and value. The first object is an Integer object and contains the new offset for the next object in buf. The second object is a Byte object that represents the decoded ASN.1 Type. The third object is an Integer and contains the actual decoded value.
        Throws:
        AsnDecodingException - Thrown if an error occurs decoding the buffer.
      • buildUInteger32

        public int buildUInteger32​(byte[] buf,
                                   int startOffset,
                                   byte asnType,
                                   long asnUInt32)
                            throws AsnEncodingException
        The buildUInteger32() method is used to encode an ASN.1 32-bit unsigned integer into the specified byte buffer.
        Specified by:
        buildUInteger32 in interface AsnEncoder
        Parameters:
        buf - The output buffer of encoded bytes.
        startOffset - The offset from the start of the buffer where the method should start writing the encoded data.
        asnType - The ASN.1 type to place in the buffer
        asnUInt32 - The 32-bit unsigned integer to encode.
        Returns:
        Returns the new offset for the next encoding routine. If startOffset is subtracted from the return value then the length of the encoded data can be determined.
        Throws:
        AsnEncodingException - Thrown if an error occurs encoding the datatype.
      • parseUInteger32

        public Object[] parseUInteger32​(byte[] buf,
                                        int startOffset)
                                 throws AsnDecodingException
        The parseUInteger32() method is used to decode an ASN.1 32-bit unsigned integer from the specified buffer.
        Specified by:
        parseUInteger32 in interface AsnEncoder
        Parameters:
        buf - The input buffer
        startOffset - The offset to start decoding in the buffer
        Returns:
        Returns an Object array that contains the new offset, ASN.1 type, and value. The first object is an Integer object and contains the new offset for the next object in buf. The second object is a Byte object that represents the decoded ASN.1 Type. The third object is a Long object and contains the actual decoded value.
        Throws:
        AsnDecodingException - Thrown if an error occurs decoding the buffer.
      • buildUInteger64

        public int buildUInteger64​(byte[] buf,
                                   int startOffset,
                                   byte asnType,
                                   BigInteger asnUInt64)
                            throws AsnEncodingException
        The buildUInteger64() method is used to encode an ASN.1 64-bit unsigned integer into the specified byte buffer.
        Specified by:
        buildUInteger64 in interface AsnEncoder
        Parameters:
        buf - The output buffer of encoded bytes.
        startOffset - The offset from the start of the buffer where the method should start writing the encoded data.
        asnType - The ASN.1 type to place in the buffer
        asnUInt64 - The 64-bit unsigned integer to encode.
        Returns:
        Returns the new offset for the next encoding routine. If startOffset is subtracted from the return value then the length of the encoded data can be determined.
        Throws:
        AsnEncodingException - Thrown if an error occurs encoding the datatype.
      • parseUInteger64

        public Object[] parseUInteger64​(byte[] buf,
                                        int startOffset)
                                 throws AsnDecodingException
        The parseUInteger64() method is used to decode an ASN.1 64-bit unsigned integer from the specified buffer.
        Specified by:
        parseUInteger64 in interface AsnEncoder
        Parameters:
        buf - The input buffer
        startOffset - The offset to start decoding in the buffer
        Returns:
        Returns an Object array that contains the new offset, ASN.1 type, and value. The first object is an Integer object and contains the new offset for the next object in buf. The second object is a Byte object that represents the decoded ASN.1 Type. The third object is a Long object and contains the actual decoded value.
        Throws:
        AsnDecodingException - Thrown if an error occurs decoding the buffer.
      • buildNull

        public int buildNull​(byte[] buf,
                             int startOffset,
                             byte asnType)
                      throws AsnEncodingException
        The buildNull() method is used to encode an ASN.1 NULL value into the specified byte buffer.
        Specified by:
        buildNull in interface AsnEncoder
        Parameters:
        buf - The output buffer of encoded bytes.
        startOffset - The offset from the start of the buffer where the method should start writing the encoded data.
        asnType - The ASN.1 type to place in the buffer
        Returns:
        Returns the new offset for the next encoding routine. If startOffset is subtracted from the return value then the length of the encoded data can be determined.
        Throws:
        AsnEncodingException - Thrown if an error occurs encoding the datatype.
      • parseNull

        public Object[] parseNull​(byte[] buf,
                                  int startOffset)
                           throws AsnDecodingException
        The parseNull() method is used to decode an ASN.1 Null value from the specified buffer. Since there is no "null" value only the new offset and ASN.1 type are returned.
        Specified by:
        parseNull in interface AsnEncoder
        Parameters:
        buf - The input buffer
        startOffset - The offset to start decoding in the buffer
        Returns:
        Returns an Object array that contains the new offset and the ASN.1 type. The first object is an Integer object and contains the new offset for the next object in buf. The second object is a Byte object that represents the decoded ASN.1 Type.
        Throws:
        AsnDecodingException - Thrown if an error occurs decoding the buffer.
      • buildString

        public int buildString​(byte[] buf,
                               int startOffset,
                               byte asnType,
                               byte[] opaque)
                        throws AsnEncodingException
        The buildString() method is used to encode an ASN.1 string value into the specified byte buffer.
        Specified by:
        buildString in interface AsnEncoder
        Parameters:
        buf - The output buffer of encoded bytes.
        startOffset - The offset from the start of the buffer where the method should start writing the encoded data.
        asnType - The ASN.1 type to place in the buffer
        opaque - An array of bytes to encode into the string.
        Returns:
        Returns the new offset for the next encoding routine. If startOffset is subtracted from the return value then the length of the encoded data can be determined.
        Throws:
        AsnEncodingException - Thrown if an error occurs encoding the datatype.
      • parseString

        public Object[] parseString​(byte[] buf,
                                    int startOffset)
                             throws AsnDecodingException
        The parseString() method is used to decode an ASN.1 opaque string from the specified buffer.
        Specified by:
        parseString in interface AsnEncoder
        Parameters:
        buf - The input buffer
        startOffset - The offset to start decoding in the buffer
        Returns:
        Returns an Object array that contains the new offset and ASN.1 type, and byte array. The first object is an Integer object and contains the new offset for the next object in buf. The second object is a Byte object that represents the decoded ASN.1 Type. The third object is an array of primitive bytes.
        Throws:
        AsnDecodingException - Thrown if an error occurs decoding the buffer.
      • buildObjectId

        public int buildObjectId​(byte[] buf,
                                 int startOffset,
                                 byte asnType,
                                 int[] oids)
                          throws AsnEncodingException
        The buildObjectId() method is used to encode an ASN.1 object id value into the specified byte buffer.
        Specified by:
        buildObjectId in interface AsnEncoder
        Parameters:
        buf - The output buffer of encoded bytes.
        startOffset - The offset from the start of the buffer where the method should start writing the encoded data.
        asnType - The ASN.1 type to place in the buffer
        oids - An array of integers to encode.
        Returns:
        Returns the new offset for the next encoding routine. If startOffset is subtracted from the return value then the length of the encoded data can be determined.
        Throws:
        AsnEncodingException - Thrown if an error occurs encoding the datatype.
      • parseObjectId

        public Object[] parseObjectId​(byte[] buf,
                                      int startOffset)
                               throws AsnDecodingException
        The parseObjectId() method is used to decode an ASN.1 Object Identifer from the specified buffer.
        Specified by:
        parseObjectId in interface AsnEncoder
        Parameters:
        buf - The input buffer
        startOffset - The offset to start decoding in the buffer
        Returns:
        Returns an Object array that contains the new offset and ASN.1 type, and ObjectId array. The first object is an Integer object and contains the new offset for the next object in buf. The second object is a Byte object that represents the decoded ASN.1 Type. The third object is an array of primitive integers.
        Throws:
        AsnDecodingException - Thrown if an error occurs decoding the buffer.