Search in sources :

Example 6 with BerLength

use of com.beanit.asn1bean.ber.BerLength in project jasn1 by openmuc.

the class BerObjectIdentifier method decode.

public int decode(InputStream is, boolean withTag) throws IOException {
    int codeLength = 0;
    if (withTag) {
        codeLength += tag.decodeAndCheck(is);
    }
    BerLength length = new BerLength();
    codeLength += length.decode(is);
    if (length.val == 0) {
        value = new int[0];
        return codeLength;
    }
    byte[] byteCode = new byte[length.val];
    Util.readFully(is, byteCode);
    codeLength += length.val;
    List<Integer> objectIdentifierComponentsList = new ArrayList<>();
    int subIDEndIndex = 0;
    while ((byteCode[subIDEndIndex] & 0x80) == 0x80) {
        if (subIDEndIndex >= (length.val - 1)) {
            throw new IOException("Invalid Object Identifier");
        }
        subIDEndIndex++;
    }
    int subidentifier = 0;
    for (int i = 0; i <= subIDEndIndex; i++) {
        subidentifier |= ((byteCode[i] & 0x7f) << ((subIDEndIndex - i) * 7));
    }
    if (subidentifier < 40) {
        objectIdentifierComponentsList.add(0);
        objectIdentifierComponentsList.add(subidentifier);
    } else if (subidentifier < 80) {
        objectIdentifierComponentsList.add(1);
        objectIdentifierComponentsList.add(subidentifier - 40);
    } else {
        objectIdentifierComponentsList.add(2);
        objectIdentifierComponentsList.add(subidentifier - 80);
    }
    subIDEndIndex++;
    while (subIDEndIndex < length.val) {
        int subIDStartIndex = subIDEndIndex;
        while ((byteCode[subIDEndIndex] & 0x80) == 0x80) {
            if (subIDEndIndex == (length.val - 1)) {
                throw new IOException("Invalid Object Identifier");
            }
            subIDEndIndex++;
        }
        subidentifier = 0;
        for (int j = subIDStartIndex; j <= subIDEndIndex; j++) {
            subidentifier |= ((byteCode[j] & 0x7f) << ((subIDEndIndex - j) * 7));
        }
        objectIdentifierComponentsList.add(subidentifier);
        subIDEndIndex++;
    }
    value = new int[objectIdentifierComponentsList.size()];
    for (int i = 0; i < objectIdentifierComponentsList.size(); i++) {
        value[i] = objectIdentifierComponentsList.get(i);
    }
    return codeLength;
}
Also used : BerLength(com.beanit.asn1bean.ber.BerLength) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 7 with BerLength

use of com.beanit.asn1bean.ber.BerLength in project jasn1 by openmuc.

the class BerVisibleString method decode.

public int decode(InputStream is, boolean withTag) throws IOException {
    int codeLength = 0;
    if (withTag) {
        codeLength += tag.decodeAndCheck(is);
    }
    BerLength length = new BerLength();
    codeLength += length.decode(is);
    value = new byte[length.val];
    if (length.val != 0) {
        Util.readFully(is, value);
        codeLength += length.val;
    }
    return codeLength;
}
Also used : BerLength(com.beanit.asn1bean.ber.BerLength)

Example 8 with BerLength

use of com.beanit.asn1bean.ber.BerLength in project jasn1 by openmuc.

the class BerBoolean method decode.

public int decode(InputStream is, boolean withTag) throws IOException {
    int codeLength = 0;
    if (withTag) {
        codeLength += tag.decodeAndCheck(is);
    }
    BerLength length = new BerLength();
    codeLength += length.decode(is);
    if (length.val != 1) {
        throw new IOException("Decoded length of BerBoolean is not correct");
    }
    int nextByte = is.read();
    if (nextByte == -1) {
        throw new EOFException("Unexpected end of input stream.");
    }
    codeLength++;
    value = nextByte != 0;
    return codeLength;
}
Also used : BerLength(com.beanit.asn1bean.ber.BerLength) EOFException(java.io.EOFException) IOException(java.io.IOException)

Example 9 with BerLength

use of com.beanit.asn1bean.ber.BerLength in project jasn1 by openmuc.

the class BerNull method decode.

public int decode(InputStream is, boolean withTag) throws IOException {
    int codeLength = 0;
    if (withTag) {
        codeLength += tag.decodeAndCheck(is);
    }
    BerLength length = new BerLength();
    codeLength += length.decode(is);
    if (length.val != 0) {
        throw new IOException("Decoded length of BerNull is not correct");
    }
    return codeLength;
}
Also used : BerLength(com.beanit.asn1bean.ber.BerLength) IOException(java.io.IOException)

Example 10 with BerLength

use of com.beanit.asn1bean.ber.BerLength in project jasn1 by openmuc.

the class BerOctetString method decodePrimitiveOctetString.

private int decodePrimitiveOctetString(InputStream is) throws IOException {
    int codeLength = 0;
    BerLength length = new BerLength();
    codeLength += length.decode(is);
    value = new byte[length.val];
    if (length.val != 0) {
        Util.readFully(is, value);
        codeLength += length.val;
    }
    return codeLength;
}
Also used : BerLength(com.beanit.asn1bean.ber.BerLength)

Aggregations

BerLength (com.beanit.asn1bean.ber.BerLength)14 IOException (java.io.IOException)11 BerTag (com.beanit.asn1bean.ber.BerTag)6 EOFException (java.io.EOFException)3 BerInteger (com.beanit.asn1bean.ber.types.BerInteger)2 BerOctetString (com.beanit.asn1bean.ber.types.BerOctetString)2 BerObjectDescriptor (com.beanit.asn1bean.ber.types.string.BerObjectDescriptor)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1