0N/A/*
2362N/A * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/Apackage com.sun.jmx.snmp;
0N/A
0N/A
0N/A
0N/A
0N/A/**
0N/A * The <CODE>BerDecoder</CODE> class is used for decoding
0N/A * BER-encoded data.
0N/A *
0N/A * A <CODE>BerDecoder</CODE> needs to be set up with the byte string containing
0N/A * the encoding. It maintains a current position in the byte string.
0N/A *
0N/A * Methods allows to fetch integer, string, OID, etc., from the current
0N/A * position. After a fetch the current position is moved forward.
0N/A *
0N/A * A fetch throws a <CODE>BerException</CODE> if the encoding is not of the
0N/A * expected type.
0N/A *
0N/A * <p><b>This API is a Sun Microsystems internal API and is subject
0N/A * to change without notice.</b></p>
0N/A *
0N/A * @since 1.5
0N/A */
0N/A
0N/Apublic class BerDecoder {
0N/A
0N/A /**
0N/A * Constructs a new decoder and attaches it to the specified byte string.
0N/A *
0N/A * @param b The byte string containing the encoded data.
0N/A */
0N/A
0N/A public BerDecoder(byte b[]) {
0N/A bytes = b ;
0N/A reset() ;
0N/A }
0N/A
0N/A public void reset() {
0N/A next = 0 ;
0N/A stackTop = 0 ;
0N/A }
0N/A
0N/A /**
0N/A * Fetch an integer.
0N/A *
0N/A * @return The decoded integer.
0N/A *
0N/A * @exception BerException Current position does not point to an integer.
0N/A */
0N/A
0N/A public int fetchInteger() throws BerException {
0N/A return fetchInteger(IntegerTag) ;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Fetch an integer with the specified tag.
0N/A *
0N/A * @param tag The expected tag.
0N/A *
0N/A * @return The decoded integer.
0N/A *
0N/A * @exception BerException Current position does not point to an integer
0N/A * or the tag is not the expected one.
0N/A */
0N/A
0N/A public int fetchInteger(int tag) throws BerException {
0N/A int result = 0 ;
0N/A final int backup = next ;
0N/A try {
0N/A if (fetchTag() != tag) {
0N/A throw new BerException() ;
0N/A }
0N/A result = fetchIntegerValue() ;
0N/A }
0N/A catch(BerException e) {
0N/A next = backup ;
0N/A throw e ;
0N/A }
0N/A
0N/A return result ;
0N/A }
0N/A
0N/A
0N/A
0N/A /**
0N/A * Fetch an integer and return a long value.
0N/A *
0N/A * @return The decoded integer.
0N/A *
0N/A * @exception BerException Current position does not point to an integer.
0N/A */
0N/A
0N/A public long fetchIntegerAsLong() throws BerException {
0N/A return fetchIntegerAsLong(IntegerTag) ;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Fetch an integer with the specified tag and return a long value.
0N/A *
0N/A * @param tag The expected tag.
0N/A *
0N/A * @return The decoded integer.
0N/A *
0N/A * @exception BerException Current position does not point to an integer
0N/A * or the tag is not the expected one.
0N/A */
0N/A
0N/A public long fetchIntegerAsLong(int tag) throws BerException {
0N/A long result = 0 ;
0N/A final int backup = next ;
0N/A try {
0N/A if (fetchTag() != tag) {
0N/A throw new BerException() ;
0N/A }
0N/A result = fetchIntegerValueAsLong() ;
0N/A }
0N/A catch(BerException e) {
0N/A next = backup ;
0N/A throw e ;
0N/A }
0N/A
0N/A return result ;
0N/A }
0N/A
0N/A
0N/A
0N/A /**
0N/A * Fetch an octet string.
0N/A *
0N/A * @return The decoded string.
0N/A *
0N/A * @exception BerException Current position does not point to an octet string.
0N/A */
0N/A
0N/A public byte[] fetchOctetString() throws BerException {
0N/A return fetchOctetString(OctetStringTag) ;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Fetch an octet string with a specified tag.
0N/A *
0N/A * @param tag The expected tag.
0N/A *
0N/A * @return The decoded string.
0N/A *
0N/A * @exception BerException Current position does not point to an octet string
0N/A * or the tag is not the expected one.
0N/A */
0N/A
0N/A public byte[] fetchOctetString(int tag) throws BerException {
0N/A byte[] result = null ;
0N/A final int backup = next ;
0N/A try {
0N/A if (fetchTag() != tag) {
0N/A throw new BerException() ;
0N/A }
0N/A result = fetchStringValue() ;
0N/A }
0N/A catch(BerException e) {
0N/A next = backup ;
0N/A throw e ;
0N/A }
0N/A
0N/A return result ;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Fetch an object identifier.
0N/A *
0N/A * @return The decoded object identifier as an array of long.
0N/A */
0N/A
0N/A public long[] fetchOid() throws BerException {
0N/A return fetchOid(OidTag) ;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Fetch an object identifier with a specified tag.
0N/A *
0N/A * @param tag The expected tag.
0N/A *
0N/A * @return The decoded object identifier as an array of long.
0N/A *
0N/A * @exception BerException Current position does not point to an oid
0N/A * or the tag is not the expected one.
0N/A */
0N/A
0N/A public long[] fetchOid(int tag) throws BerException {
0N/A long[] result = null ;
0N/A final int backup = next ;
0N/A try {
0N/A if (fetchTag() != tag) {
0N/A throw new BerException() ;
0N/A }
0N/A result = fetchOidValue() ;
0N/A }
0N/A catch(BerException e) {
0N/A next = backup ;
0N/A throw e ;
0N/A }
0N/A
0N/A return result ;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Fetch a <CODE>NULL</CODE> value.
0N/A *
0N/A * @exception BerException Current position does not point to <CODE>NULL</CODE> value.
0N/A */
0N/A
0N/A public void fetchNull() throws BerException {
0N/A fetchNull(NullTag) ;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Fetch a <CODE>NULL</CODE> value with a specified tag.
0N/A *
0N/A * @param tag The expected tag.
0N/A *
0N/A * @exception BerException Current position does not point to
0N/A * <CODE>NULL</CODE> value or the tag is not the expected one.
0N/A */
0N/A
0N/A public void fetchNull(int tag) throws BerException {
0N/A final int backup = next ;
0N/A try {
0N/A if (fetchTag() != tag) {
0N/A throw new BerException() ;
0N/A }
0N/A final int length = fetchLength();
0N/A if (length != 0) throw new BerException();
0N/A }
0N/A catch(BerException e) {
0N/A next = backup ;
0N/A throw e ;
0N/A }
0N/A }
0N/A
0N/A
0N/A
0N/A /**
0N/A * Fetch an <CODE>ANY</CODE> value. In fact, this method does not decode anything
0N/A * it simply returns the next TLV as an array of bytes.
0N/A *
0N/A * @return The TLV as a byte array.
0N/A *
0N/A * @exception BerException The next TLV is really badly encoded...
0N/A */
0N/A
0N/A public byte[] fetchAny() throws BerException {
0N/A byte[] result = null ;
0N/A final int backup = next ;
0N/A try {
0N/A final int tag = fetchTag() ;
0N/A final int contentLength = fetchLength() ;
0N/A if (contentLength < 0) throw new BerException() ;
0N/A final int tlvLength = next + contentLength - backup ;
0N/A if (contentLength > (bytes.length - next))
0N/A throw new IndexOutOfBoundsException("Decoded length exceeds buffer");
0N/A final byte[] data = new byte[tlvLength] ;
0N/A java.lang.System.arraycopy(bytes,backup,data,0,tlvLength);
0N/A // for (int i = 0 ; i < tlvLength ; i++) {
0N/A // data[i] = bytes[backup + i] ;
0N/A // }
0N/A next = next + contentLength ;
0N/A result = data;
0N/A }
0N/A catch(IndexOutOfBoundsException e) {
0N/A next = backup ;
0N/A throw new BerException() ;
0N/A }
0N/A // catch(Error e) {
0N/A // debug("fetchAny: Error decoding BER: " + e);
0N/A // throw e;
0N/A // }
0N/A
0N/A return result ;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Fetch an <CODE>ANY</CODE> value with a specific tag.
0N/A *
0N/A * @param tag The expected tag.
0N/A *
0N/A * @return The TLV as a byte array.
0N/A *
0N/A * @exception BerException The next TLV is really badly encoded...
0N/A */
0N/A
0N/A public byte[] fetchAny(int tag) throws BerException {
0N/A if (getTag() != tag) {
0N/A throw new BerException() ;
0N/A }
0N/A return fetchAny() ;
0N/A }
0N/A
0N/A
0N/A
0N/A /**
0N/A * Fetch a sequence header.
0N/A * The decoder computes the end position of the sequence and push it
0N/A * on its stack.
0N/A *
0N/A * @exception BerException Current position does not point to a sequence header.
0N/A */
0N/A
0N/A public void openSequence() throws BerException {
0N/A openSequence(SequenceTag) ;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Fetch a sequence header with a specific tag.
0N/A *
0N/A * @param tag The expected tag.
0N/A *
0N/A * @exception BerException Current position does not point to a sequence header
0N/A * or the tag is not the expected one.
0N/A */
0N/A
0N/A public void openSequence(int tag) throws BerException {
0N/A final int backup = next ;
0N/A try {
0N/A if (fetchTag() != tag) {
0N/A throw new BerException() ;
0N/A }
0N/A final int l = fetchLength() ;
0N/A if (l < 0) throw new BerException();
0N/A if (l > (bytes.length - next)) throw new BerException();
0N/A stackBuf[stackTop++] = next + l ;
0N/A }
0N/A catch(BerException e) {
0N/A next = backup ;
0N/A throw e ;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Close a sequence.
0N/A * The decode pull the stack and verifies that the current position
0N/A * matches with the calculated end of the sequence. If not it throws
0N/A * an exception.
0N/A *
0N/A * @exception BerException The sequence is not expected to finish here.
0N/A */
0N/A
0N/A public void closeSequence() throws BerException {
0N/A if (stackBuf[stackTop - 1] == next) {
0N/A stackTop-- ;
0N/A }
0N/A else {
0N/A throw new BerException() ;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Return <CODE>true</CODE> if the end of the current sequence is not reached.
0N/A * When this method returns <CODE>false</CODE>, <CODE>closeSequence</CODE> can (and must) be
0N/A * invoked.
0N/A *
0N/A * @return <CODE>true</CODE> if there is still some data in the sequence.
0N/A */
0N/A
0N/A public boolean cannotCloseSequence() {
0N/A return (next < stackBuf[stackTop - 1]) ;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Get the tag of the data at the current position.
0N/A * Current position is unchanged.
0N/A *
0N/A * @return The next tag.
0N/A */
0N/A
0N/A public int getTag() throws BerException {
0N/A int result = 0 ;
0N/A final int backup = next ;
0N/A try {
0N/A result = fetchTag() ;
0N/A }
0N/A finally {
0N/A next = backup ;
0N/A }
0N/A
0N/A return result ;
0N/A }
0N/A
0N/A
0N/A
0N/A public String toString() {
0N/A final StringBuffer result = new StringBuffer(bytes.length * 2) ;
0N/A for (int i = 0 ; i < bytes.length ; i++) {
0N/A final int b = (bytes[i] > 0) ? bytes[i] : bytes[i] + 256 ;
0N/A if (i == next) {
0N/A result.append("(") ;
0N/A }
0N/A result.append(Character.forDigit(b / 16, 16)) ;
0N/A result.append(Character.forDigit(b % 16, 16)) ;
0N/A if (i == next) {
0N/A result.append(")") ;
0N/A }
0N/A }
0N/A if (bytes.length == next) {
0N/A result.append("()") ;
0N/A }
0N/A
0N/A return new String(result) ;
0N/A }
0N/A
0N/A
0N/A //
0N/A // Some standard tags
0N/A //
0N/A public final static int BooleanTag = 1 ;
0N/A public final static int IntegerTag = 2 ;
0N/A public final static int OctetStringTag = 4 ;
0N/A public final static int NullTag = 5 ;
0N/A public final static int OidTag = 6 ;
0N/A public final static int SequenceTag = 0x30 ;
0N/A
0N/A
0N/A
0N/A
0N/A ////////////////////////// PRIVATE ///////////////////////////////
0N/A
0N/A
0N/A
0N/A /**
0N/A * Fetch a tag and move the current position forward.
0N/A *
0N/A * @return The tag
0N/A */
0N/A
0N/A private final int fetchTag() throws BerException {
0N/A int result = 0 ;
0N/A final int backup = next ;
0N/A
0N/A try {
0N/A final byte b0 = bytes[next++] ;
0N/A result = (b0 >= 0) ? b0 : b0 + 256 ;
0N/A if ((result & 31) == 31) {
0N/A while ((bytes[next] & 128) != 0) {
0N/A result = result << 7 ;
0N/A result = result | (bytes[next++] & 127);
0N/A }
0N/A }
0N/A }
0N/A catch(IndexOutOfBoundsException e) {
0N/A next = backup ;
0N/A throw new BerException() ;
0N/A }
0N/A
0N/A return result ;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Fetch a length and move the current position forward.
0N/A *
0N/A * @return The length
0N/A */
0N/A
0N/A private final int fetchLength() throws BerException {
0N/A int result = 0 ;
0N/A final int backup = next ;
0N/A
0N/A try {
0N/A final byte b0 = bytes[next++] ;
0N/A if (b0 >= 0) {
0N/A result = b0 ;
0N/A }
0N/A else {
0N/A for (int c = 128 + b0 ; c > 0 ; c--) {
0N/A final byte bX = bytes[next++] ;
0N/A result = result << 8 ;
0N/A result = result | ((bX >= 0) ? bX : bX+256) ;
0N/A }
0N/A }
0N/A }
0N/A catch(IndexOutOfBoundsException e) {
0N/A next = backup ;
0N/A throw new BerException() ;
0N/A }
0N/A
0N/A return result ;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Fetch an integer value and move the current position forward.
0N/A *
0N/A * @return The integer
0N/A */
0N/A
0N/A private int fetchIntegerValue() throws BerException {
0N/A int result = 0 ;
0N/A final int backup = next ;
0N/A
0N/A try {
0N/A final int length = fetchLength() ;
0N/A if (length <= 0) throw new BerException() ;
0N/A if (length > (bytes.length - next)) throw
0N/A new IndexOutOfBoundsException("Decoded length exceeds buffer");
0N/A final int end = next + length ;
0N/A result = bytes[next++] ;
0N/A while (next < end) {
0N/A final byte b = bytes[next++] ;
0N/A if (b < 0) {
0N/A result = (result << 8) | (256 + b) ;
0N/A }
0N/A else {
0N/A result = (result << 8) | b ;
0N/A }
0N/A }
0N/A }
0N/A catch(BerException e) {
0N/A next = backup ;
0N/A throw e ;
0N/A }
0N/A catch(IndexOutOfBoundsException e) {
0N/A next = backup ;
0N/A throw new BerException() ;
0N/A }
0N/A catch(ArithmeticException e) {
0N/A next = backup ;
0N/A throw new BerException() ;
0N/A }
0N/A return result ;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Fetch an integer value and return a long value.
0N/A * FIX ME: someday we could have only on fetchIntegerValue() which always
0N/A * returns a long value.
0N/A *
0N/A * @return The integer
0N/A */
0N/A
0N/A private final long fetchIntegerValueAsLong() throws BerException {
0N/A long result = 0 ;
0N/A final int backup = next ;
0N/A
0N/A try {
0N/A final int length = fetchLength() ;
0N/A if (length <= 0) throw new BerException() ;
0N/A if (length > (bytes.length - next)) throw
0N/A new IndexOutOfBoundsException("Decoded length exceeds buffer");
0N/A
0N/A final int end = next + length ;
0N/A result = bytes[next++] ;
0N/A while (next < end) {
0N/A final byte b = bytes[next++] ;
0N/A if (b < 0) {
0N/A result = (result << 8) | (256 + b) ;
0N/A }
0N/A else {
0N/A result = (result << 8) | b ;
0N/A }
0N/A }
0N/A }
0N/A catch(BerException e) {
0N/A next = backup ;
0N/A throw e ;
0N/A }
0N/A catch(IndexOutOfBoundsException e) {
0N/A next = backup ;
0N/A throw new BerException() ;
0N/A }
0N/A catch(ArithmeticException e) {
0N/A next = backup ;
0N/A throw new BerException() ;
0N/A }
0N/A return result ;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Fetch a byte string and move the current position forward.
0N/A *
0N/A * @return The byte string
0N/A */
0N/A
0N/A private byte[] fetchStringValue() throws BerException {
0N/A byte[] result = null ;
0N/A final int backup = next ;
0N/A
0N/A try {
0N/A final int length = fetchLength() ;
0N/A if (length < 0) throw new BerException() ;
0N/A if (length > (bytes.length - next))
0N/A throw new IndexOutOfBoundsException("Decoded length exceeds buffer");
0N/A final byte data[] = new byte[length] ;
0N/A java.lang.System.arraycopy(bytes,next,data,0,length);
0N/A next += length;
0N/A // int i = 0 ;
0N/A // while (i < length) {
0N/A // result[i++] = bytes[next++] ;
0N/A // }
0N/A result = data;
0N/A }
0N/A catch(BerException e) {
0N/A next = backup ;
0N/A throw e ;
0N/A }
0N/A catch(IndexOutOfBoundsException e) {
0N/A next = backup ;
0N/A throw new BerException() ;
0N/A }
0N/A catch(ArithmeticException e) {
0N/A next = backup ;
0N/A throw new BerException() ;
0N/A }
0N/A // catch(Error e) {
0N/A // debug("fetchStringValue: Error decoding BER: " + e);
0N/A // throw e;
0N/A // }
0N/A
0N/A return result ;
0N/A }
0N/A
0N/A
0N/A
0N/A /**
0N/A * Fetch an oid and move the current position forward.
0N/A *
0N/A * @return The oid
0N/A */
0N/A
0N/A private final long[] fetchOidValue() throws BerException {
0N/A long[] result = null ;
0N/A final int backup = next ;
0N/A
0N/A try {
0N/A final int length = fetchLength() ;
0N/A if (length <= 0) throw new BerException() ;
0N/A if (length > (bytes.length - next))
0N/A throw new IndexOutOfBoundsException("Decoded length exceeds buffer");
0N/A // Count how many bytes have their 8th bit to 0
0N/A // -> this gives the number of components in the oid
0N/A int subidCount = 2 ;
0N/A for (int i = 1 ; i < length ; i++) {
0N/A if ((bytes[next + i] & 0x80) == 0) {
0N/A subidCount++ ;
0N/A }
0N/A }
0N/A final int datalen = subidCount;
0N/A final long[] data = new long[datalen];
0N/A final byte b0 = bytes[next++] ;
0N/A
0N/A // bugId 4641746
0N/A // The 8th bit of the first byte should always be set to 0
0N/A if (b0 < 0) throw new BerException();
0N/A
0N/A // bugId 4641746
0N/A // The first sub Id cannot be greater than 2
0N/A final long lb0 = b0 / 40 ;
0N/A if (lb0 > 2) throw new BerException();
0N/A
0N/A final long lb1 = b0 % 40;
0N/A data[0] = lb0 ;
0N/A data[1] = lb1 ;
0N/A int i = 2 ;
0N/A while (i < datalen) {
0N/A long subid = 0 ;
0N/A byte b = bytes[next++] ;
0N/A while ((b & 0x80) != 0) {
0N/A subid = (subid << 7) | (b & 0x7f) ;
0N/A // bugId 4654674
0N/A if (subid < 0) throw new BerException();
0N/A b = bytes[next++] ;
0N/A }
0N/A subid = (subid << 7) | b ;
0N/A // bugId 4654674
0N/A if (subid < 0) throw new BerException();
0N/A data[i++] = subid ;
0N/A }
0N/A result = data;
0N/A }
0N/A catch(BerException e) {
0N/A next = backup ;
0N/A throw e ;
0N/A }
0N/A catch(IndexOutOfBoundsException e) {
0N/A next = backup ;
0N/A throw new BerException() ;
0N/A }
0N/A // catch(Error e) {
0N/A // debug("fetchOidValue: Error decoding BER: " + e);
0N/A // throw e;
0N/A // }
0N/A
0N/A return result ;
0N/A }
0N/A
0N/A // private static final void debug(String str) {
0N/A // System.out.println(str);
0N/A // }
0N/A
0N/A //
0N/A // This is the byte array containing the encoding.
0N/A //
0N/A private final byte bytes[];
0N/A
0N/A //
0N/A // This is the current location. It is the next byte
0N/A // to be decoded. It's an index in bytes[].
0N/A //
0N/A private int next = 0 ;
0N/A
0N/A //
0N/A // This is the stack where end of sequences are kept.
0N/A // A value is computed and pushed in it each time openSequence()
0N/A // is invoked.
0N/A // A value is pulled and checked each time closeSequence() is called.
0N/A //
0N/A private final int stackBuf[] = new int[200] ;
0N/A private int stackTop = 0 ;
0N/A
0N/A}