A BER decoder. Contains methods to parse a BER buffer.
| Method from com.sun.jndi.ldap.BerDecoder Detail: |
public int bytesLeft() {
return bufsize - offset;
}
Returns the number of unparsed bytes in this BER buffer. |
public int getParsePosition() {
return offset;
}
Returns the current parse position.
It points to the byte that will be parsed next.
Useful for parsing sequences. |
public boolean parseBoolean() throws DecodeException {
return ((parseIntWithTag(ASN_BOOLEAN) == 0x00) ? false : true);
}
Parses an ASN_BOOLEAN tagged integer from this BER buffer. |
public int parseByte() throws DecodeException {
if (bufsize - offset < 1) {
throw new DecodeException("Insufficient data");
}
return buf[offset++] & 0xff;
}
Parses the next byte in this BER buffer. |
public int parseEnumeration() throws DecodeException {
return parseIntWithTag(ASN_ENUMERATED);
}
Parses an ASN_ENUMERATED tagged integer from this BER buffer. |
public int parseInt() throws DecodeException {
return parseIntWithTag(ASN_INTEGER);
}
Parses an ASN_INTEGER tagged integer from this BER buffer. |
public int parseLength() throws DecodeException {
int lengthbyte = parseByte();
if ((lengthbyte & 0x80) == 0x80) {
lengthbyte &= 0x7f;
if (lengthbyte == 0) {
throw new DecodeException(
"Indefinite length not supported");
}
if (lengthbyte > 4) {
throw new DecodeException("encoding too long");
}
if (bufsize - offset < lengthbyte) {
throw new DecodeException("Insufficient data");
}
int retval = 0;
for( int i = 0; i < lengthbyte; i++) {
retval = (retval < < 8) + (buf[offset++] & 0xff);
}
return retval;
} else {
return lengthbyte;
}
}
Parses a possibly variable length field. |
public byte[] parseOctetString(int tag,
int[] rlen) throws DecodeException {
int origOffset = offset;
int st;
if ((st = parseByte()) != tag) {
throw new DecodeException("Encountered ASN.1 tag " +
Integer.toString(st) +
" (expected tag " + Integer.toString(tag) + ")");
}
int len = parseLength();
if (len > bufsize - offset) {
throw new DecodeException("Insufficient data");
}
byte retarr[] = new byte[len];
if (len > 0) {
System.arraycopy(buf, offset, retarr, 0, len);
offset += len;
}
if (rlen != null) {
rlen[0] = offset - origOffset;
}
return retarr;
}
|
public int parseSeq(int[] rlen) throws DecodeException {
int seq = parseByte();
int len = parseLength();
if (rlen != null) {
rlen[0] = len;
}
return seq;
}
Parses the next sequence in this BER buffer. |
public String parseString(boolean decodeUTF8) throws DecodeException {
return parseStringWithTag(ASN_SIMPLE_STRING, decodeUTF8, null);
}
|
public String parseStringWithTag(int tag,
boolean decodeUTF8,
int[] rlen) throws DecodeException {
int st;
int origOffset = offset;
if ((st = parseByte()) != tag) {
throw new DecodeException("Encountered ASN.1 tag " +
Integer.toString((byte)st) + " (expected tag " + tag + ")");
}
int len = parseLength();
if (len > bufsize - offset) {
throw new DecodeException("Insufficient data");
}
String retstr;
if (len == 0) {
retstr = "";
} else {
byte[] buf2 = new byte[len];
System.arraycopy(buf, offset, buf2, 0, len);
if (decodeUTF8) {
try {
retstr = new String(buf2, "UTF8");
} catch (UnsupportedEncodingException e) {
throw new DecodeException("UTF8 not available on platform");
}
} else {
try {
retstr = new String(buf2, "8859_1");
} catch (UnsupportedEncodingException e) {
throw new DecodeException("8859_1 not available on platform");
}
}
offset += len;
}
if (rlen != null) {
rlen[0] = offset - origOffset;
}
return retstr;
}
|
public int peekByte() throws DecodeException {
if (bufsize - offset < 1) {
throw new DecodeException("Insufficient data");
}
return buf[offset] & 0xff;
}
Returns the next byte in this BER buffer without consuming it. |
public void reset() {
offset = origOffset;
}
Resets this decode to start parsing from the initial offset
(ie., same state as after calling the constructor). |
void seek(int i) throws DecodeException {
if (offset + i > bufsize || offset + i < 0) {
throw new DecodeException("array index out of bounds");
}
offset += i;
}
Used to skip bytes. Usually used when trying to recover from parse error.
Don't need to be public right now? |