Search in sources :

Example 11 with BerLength

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

the class BerOctetString method decodeConstructedOctetString.

private int decodeConstructedOctetString(InputStream is) throws IOException {
    BerLength length = new BerLength();
    int lengthLength = length.decode(is);
    value = new byte[0];
    int vLength = 0;
    if (length.val < 0) {
        BerTag berTag = new BerTag();
        vLength += berTag.decode(is);
        while (!berTag.equals(0, 0, 0)) {
            BerOctetString subOctetString = new BerOctetString();
            vLength += subOctetString.decode(is, false);
            value = concatenate(value, subOctetString.value);
            vLength += berTag.decode(is);
        }
        vLength += BerLength.readEocByte(is);
    } else {
        while (vLength < length.val) {
            BerOctetString subOctetString = new BerOctetString();
            vLength += subOctetString.decode(is);
            value = concatenate(value, subOctetString.value);
        }
    }
    return lengthLength + vLength;
}
Also used : BerLength(com.beanit.asn1bean.ber.BerLength) BerTag(com.beanit.asn1bean.ber.BerTag)

Example 12 with BerLength

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

the class BerBitString method decode.

public int decode(InputStream is, boolean withTag) throws IOException {
    // could be encoded in primitiv and constructed mode
    // only primitiv mode is implemented
    int codeLength = 0;
    if (withTag) {
        codeLength += tag.decodeAndCheck(is);
    }
    BerLength length = new BerLength();
    codeLength += length.decode(is);
    value = new byte[length.val - 1];
    int unusedBits = is.read();
    if (unusedBits == -1) {
        throw new EOFException("Unexpected end of input stream.");
    }
    if (unusedBits > 7) {
        throw new IOException("Number of unused bits in bit string expected to be less than 8 but is: " + unusedBits);
    }
    numBits = (value.length * 8) - unusedBits;
    if (value.length > 0) {
        Util.readFully(is, value);
    }
    codeLength += value.length + 1;
    return codeLength;
}
Also used : BerLength(com.beanit.asn1bean.ber.BerLength) EOFException(java.io.EOFException) IOException(java.io.IOException)

Example 13 with BerLength

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

the class BerReal 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 = 0;
        return codeLength;
    }
    if (length.val == 1) {
        int nextByte = is.read();
        if (nextByte == -1) {
            throw new EOFException("Unexpected end of input stream.");
        }
        if (nextByte == 0x40) {
            value = Double.POSITIVE_INFINITY;
        } else if (nextByte == 0x41) {
            value = Double.NEGATIVE_INFINITY;
        } else {
            throw new IOException("invalid real encoding");
        }
        return codeLength + 1;
    }
    byte[] byteCode = new byte[length.val];
    Util.readFully(is, byteCode);
    if ((byteCode[0] & 0x80) != 0x80) {
        throw new IOException("Only binary REAL encoding is supported");
    }
    codeLength += length.val;
    int tempLength = 1;
    int sign = 1;
    if ((byteCode[0] & 0x40) == 0x40) {
        sign = -1;
    }
    int exponentLength = (byteCode[0] & 0x03) + 1;
    if (exponentLength == 4) {
        exponentLength = byteCode[1];
        tempLength++;
    }
    tempLength += exponentLength;
    int exponent = 0;
    for (int i = 0; i < exponentLength; i++) {
        exponent |= byteCode[1 + i] << (8 * (exponentLength - i - 1));
    }
    long mantissa = 0;
    for (int i = 0; i < length.val - tempLength; i++) {
        mantissa |= (byteCode[i + tempLength] & 0xffL) << (8 * (length.val - tempLength - i - 1));
    }
    value = sign * mantissa * Math.pow(2, exponent);
    return codeLength;
}
Also used : BerLength(com.beanit.asn1bean.ber.BerLength) EOFException(java.io.EOFException) IOException(java.io.IOException)

Example 14 with BerLength

use of com.beanit.asn1bean.ber.BerLength in project sandbox-java by stIncMale.

the class ReceiptAttribute method decode.

public int decode(InputStream is, boolean withTag) throws IOException {
    int tlByteCount = 0;
    int vByteCount = 0;
    BerTag berTag = new BerTag();
    if (withTag) {
        tlByteCount += tag.decodeAndCheck(is);
    }
    BerLength length = new BerLength();
    tlByteCount += length.decode(is);
    int lengthVal = length.val;
    vByteCount += berTag.decode(is);
    if (berTag.equals(BerInteger.tag)) {
        type = new BerInteger();
        vByteCount += type.decode(is, false);
        vByteCount += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match mandatory sequence component.");
    }
    if (berTag.equals(BerInteger.tag)) {
        version = new BerInteger();
        vByteCount += version.decode(is, false);
        vByteCount += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match mandatory sequence component.");
    }
    if (berTag.equals(BerOctetString.tag)) {
        value = new BerOctetString();
        vByteCount += value.decode(is, false);
        if (lengthVal >= 0 && vByteCount == lengthVal) {
            return tlByteCount + vByteCount;
        }
        vByteCount += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match mandatory sequence component.");
    }
    if (lengthVal < 0) {
        if (!berTag.equals(0, 0, 0)) {
            throw new IOException("Decoded sequence has wrong end of contents octets");
        }
        vByteCount += BerLength.readEocByte(is);
        return tlByteCount + vByteCount;
    }
    throw new IOException("Unexpected end of sequence, length tag: " + lengthVal + ", bytes decoded: " + vByteCount);
}
Also used : BerLength(com.beanit.asn1bean.ber.BerLength) BerOctetString(com.beanit.asn1bean.ber.types.BerOctetString) IOException(java.io.IOException) BerInteger(com.beanit.asn1bean.ber.types.BerInteger) BerTag(com.beanit.asn1bean.ber.BerTag)

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