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 * 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 * 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 * 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. 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 0N/A * The <CODE>BerDecoder</CODE> class is used for decoding 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 * 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 * A fetch throws a <CODE>BerException</CODE> if the encoding is not of the 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 * Constructs a new decoder and attaches it to the specified byte string. 0N/A * @param b The byte string containing the encoded data. 0N/A * @return The decoded integer. 0N/A * @exception BerException Current position does not point to an integer. 0N/A * Fetch an integer with the specified tag. 0N/A * @param tag The expected tag. 0N/A * @return The decoded integer. 0N/A * @exception BerException Current position does not point to an integer 0N/A * or the tag is not the expected one. 0N/A * Fetch an integer and return a long value. 0N/A * @return The decoded integer. 0N/A * @exception BerException Current position does not point to an integer. 0N/A * Fetch an integer with the specified tag and return a long value. 0N/A * @param tag The expected tag. 0N/A * @return The decoded integer. 0N/A * @exception BerException Current position does not point to an integer 0N/A * or the tag is not the expected one. 0N/A * Fetch an octet string. 0N/A * @return The decoded string. 0N/A * @exception BerException Current position does not point to an octet string. 0N/A * Fetch an octet string with a specified tag. 0N/A * @param tag The expected tag. 0N/A * @return The decoded string. 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 * Fetch an object identifier. 0N/A * @return The decoded object identifier as an array of long. 0N/A * Fetch an object identifier with a specified tag. 0N/A * @param tag The expected tag. 0N/A * @return The decoded object identifier as an array of long. 0N/A * @exception BerException Current position does not point to an oid 0N/A * or the tag is not the expected one. 0N/A * Fetch a <CODE>NULL</CODE> value. 0N/A * @exception BerException Current position does not point to <CODE>NULL</CODE> value. 0N/A * Fetch a <CODE>NULL</CODE> value with a specified tag. 0N/A * @param tag The expected tag. 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 * 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 * @return The TLV as a byte array. 0N/A * @exception BerException The next TLV is really badly encoded... 0N/A // for (int i = 0 ; i < tlvLength ; i++) { 0N/A // data[i] = bytes[backup + i] ; 0N/A // debug("fetchAny: Error decoding BER: " + e); 0N/A * Fetch an <CODE>ANY</CODE> value with a specific tag. 0N/A * @param tag The expected tag. 0N/A * @return The TLV as a byte array. 0N/A * @exception BerException The next TLV is really badly encoded... 0N/A * Fetch a sequence header. 0N/A * The decoder computes the end position of the sequence and push it 0N/A * @exception BerException Current position does not point to a sequence header. 0N/A * Fetch a sequence header with a specific tag. 0N/A * @param tag The expected tag. 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 * 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 * @exception BerException The sequence is not expected to finish here. 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 * @return <CODE>true</CODE> if there is still some data in the sequence. 0N/A * Get the tag of the data at the current position. 0N/A * Current position is unchanged. 0N/A * @return The next tag. 0N/A // Some standard tags 0N/A ////////////////////////// PRIVATE /////////////////////////////// 0N/A * Fetch a tag and move the current position forward. 0N/A * Fetch a length and move the current position forward. 0N/A * @return The length 0N/A for (
int c =
128 +
b0 ; c >
0 ; c--) {
0N/A * Fetch an integer value and move the current position forward. 0N/A * @return The integer 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 * @return The integer 0N/A * Fetch a byte string and move the current position forward. 0N/A * @return The byte string 0N/A // while (i < length) { 0N/A // result[i++] = bytes[next++] ; 0N/A // debug("fetchStringValue: Error decoding BER: " + e); 0N/A * Fetch an oid and move the current position forward. 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 // The 8th bit of the first byte should always be set to 0 0N/A // The first sub Id cannot be greater than 2 0N/A while ((b &
0x80) !=
0) {
0N/A // debug("fetchOidValue: Error decoding BER: " + e); 0N/A // private static final void debug(String str) { 0N/A // System.out.println(str); 0N/A // This is the byte array containing the encoding. 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 // 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 // A value is pulled and checked each time closeSequence() is called.