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