Search in sources :

Example 11 with BerLength

use of org.openmuc.jasn1.ber.BerLength in project jasn1 by openmuc.

the class BerEmbeddedPdv method decode.

public int decode(InputStream is, boolean withTag) throws IOException {
    int codeLength = 0;
    int subCodeLength = 0;
    BerTag berTag = new BerTag();
    if (withTag) {
        codeLength += tag.decodeAndCheck(is);
    }
    BerLength length = new BerLength();
    codeLength += length.decode(is);
    int totalLength = length.val;
    codeLength += totalLength;
    subCodeLength += berTag.decode(is);
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 0)) {
        subCodeLength += length.decode(is);
        identification = new Identification();
        subCodeLength += identification.decode(is, null);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 1)) {
        dataValueDescriptor = new BerObjectDescriptor();
        subCodeLength += dataValueDescriptor.decode(is, false);
        subCodeLength += berTag.decode(is);
    }
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 2)) {
        dataValue = new BerOctetString();
        subCodeLength += dataValue.decode(is, false);
        if (subCodeLength == totalLength) {
            return codeLength;
        }
    }
    throw new IOException("Unexpected end of sequence, length tag: " + totalLength + ", actual sequence length: " + subCodeLength);
}
Also used : BerLength(org.openmuc.jasn1.ber.BerLength) BerObjectDescriptor(org.openmuc.jasn1.ber.types.string.BerObjectDescriptor) IOException(java.io.IOException) BerTag(org.openmuc.jasn1.ber.BerTag)

Example 12 with BerLength

use of org.openmuc.jasn1.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(org.openmuc.jasn1.ber.BerLength) IOException(java.io.IOException)

Example 13 with BerLength

use of org.openmuc.jasn1.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(org.openmuc.jasn1.ber.BerLength) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 14 with BerLength

use of org.openmuc.jasn1.ber.BerLength in project jasn1 by openmuc.

the class BerOctetString 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(org.openmuc.jasn1.ber.BerLength)

Example 15 with BerLength

use of org.openmuc.jasn1.ber.BerLength in project jasn1 by openmuc.

the class OtherSignedNotification method decode.

public int decode(InputStream is, boolean withTag) throws IOException {
    int codeLength = 0;
    int subCodeLength = 0;
    BerTag berTag = new BerTag();
    if (withTag) {
        codeLength += tag.decodeAndCheck(is);
    }
    BerLength length = new BerLength();
    codeLength += length.decode(is);
    int totalLength = length.val;
    if (totalLength == -1) {
        subCodeLength += berTag.decode(is);
        if (berTag.tagNumber == 0 && berTag.tagClass == 0 && berTag.primitive == 0) {
            int nextByte = is.read();
            if (nextByte != 0) {
                if (nextByte == -1) {
                    throw new EOFException("Unexpected end of input stream.");
                }
                throw new IOException("Decoded sequence has wrong end of contents octets");
            }
            codeLength += subCodeLength + 1;
            return codeLength;
        }
        if (berTag.equals(NotificationMetadata.tag)) {
            tbsOtherNotification = new NotificationMetadata();
            subCodeLength += tbsOtherNotification.decode(is, false);
            subCodeLength += berTag.decode(is);
        }
        if (berTag.tagNumber == 0 && berTag.tagClass == 0 && berTag.primitive == 0) {
            int nextByte = is.read();
            if (nextByte != 0) {
                if (nextByte == -1) {
                    throw new EOFException("Unexpected end of input stream.");
                }
                throw new IOException("Decoded sequence has wrong end of contents octets");
            }
            codeLength += subCodeLength + 1;
            return codeLength;
        }
        if (berTag.equals(BerTag.APPLICATION_CLASS, BerTag.PRIMITIVE, 55)) {
            euiccNotificationSignature = new BerOctetString();
            subCodeLength += euiccNotificationSignature.decode(is, false);
            subCodeLength += berTag.decode(is);
        }
        if (berTag.tagNumber == 0 && berTag.tagClass == 0 && berTag.primitive == 0) {
            int nextByte = is.read();
            if (nextByte != 0) {
                if (nextByte == -1) {
                    throw new EOFException("Unexpected end of input stream.");
                }
                throw new IOException("Decoded sequence has wrong end of contents octets");
            }
            codeLength += subCodeLength + 1;
            return codeLength;
        }
        if (berTag.equals(Certificate.tag)) {
            euiccCertificate = new Certificate();
            subCodeLength += euiccCertificate.decode(is, false);
            subCodeLength += berTag.decode(is);
        }
        if (berTag.tagNumber == 0 && berTag.tagClass == 0 && berTag.primitive == 0) {
            int nextByte = is.read();
            if (nextByte != 0) {
                if (nextByte == -1) {
                    throw new EOFException("Unexpected end of input stream.");
                }
                throw new IOException("Decoded sequence has wrong end of contents octets");
            }
            codeLength += subCodeLength + 1;
            return codeLength;
        }
        if (berTag.equals(Certificate.tag)) {
            eumCertificate = new Certificate();
            subCodeLength += eumCertificate.decode(is, false);
            subCodeLength += berTag.decode(is);
        }
        int nextByte = is.read();
        if (berTag.tagNumber != 0 || berTag.tagClass != 0 || berTag.primitive != 0 || nextByte != 0) {
            if (nextByte == -1) {
                throw new EOFException("Unexpected end of input stream.");
            }
            throw new IOException("Decoded sequence has wrong end of contents octets");
        }
        codeLength += subCodeLength + 1;
        return codeLength;
    }
    codeLength += totalLength;
    subCodeLength += berTag.decode(is);
    if (berTag.equals(NotificationMetadata.tag)) {
        tbsOtherNotification = new NotificationMetadata();
        subCodeLength += tbsOtherNotification.decode(is, false);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(BerTag.APPLICATION_CLASS, BerTag.PRIMITIVE, 55)) {
        euiccNotificationSignature = new BerOctetString();
        subCodeLength += euiccNotificationSignature.decode(is, false);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(Certificate.tag)) {
        euiccCertificate = new Certificate();
        subCodeLength += euiccCertificate.decode(is, false);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(Certificate.tag)) {
        eumCertificate = new Certificate();
        subCodeLength += eumCertificate.decode(is, false);
        if (subCodeLength == totalLength) {
            return codeLength;
        }
    }
    throw new IOException("Unexpected end of sequence, length tag: " + totalLength + ", actual sequence length: " + subCodeLength);
}
Also used : EOFException(java.io.EOFException) IOException(java.io.IOException) Certificate(org.openmuc.jasn1.compiler.pkix1explicit88.Certificate)

Aggregations

IOException (java.io.IOException)18 EOFException (java.io.EOFException)13 BerLength (org.openmuc.jasn1.ber.BerLength)10 BerTag (org.openmuc.jasn1.ber.BerTag)6 AsnBitString (org.openmuc.jasn1.compiler.model.AsnBitString)5 AsnCharacterString (org.openmuc.jasn1.compiler.model.AsnCharacterString)5 AsnOctetString (org.openmuc.jasn1.compiler.model.AsnOctetString)5 Certificate (org.openmuc.jasn1.compiler.pkix1explicit88.Certificate)5 AsnTag (org.openmuc.jasn1.compiler.model.AsnTag)4 AsnElementType (org.openmuc.jasn1.compiler.model.AsnElementType)3 SubjectKeyIdentifier (org.openmuc.jasn1.compiler.pkix1implicit88.SubjectKeyIdentifier)2 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 ReverseByteArrayOutputStream (org.openmuc.jasn1.ber.ReverseByteArrayOutputStream)1 BerObjectDescriptor (org.openmuc.jasn1.ber.types.string.BerObjectDescriptor)1 EmployeeNumberZ (org.openmuc.jasn1.compiler.modules.module2.EmployeeNumberZ)1 Attribute (org.openmuc.jasn1.compiler.pkix1explicit88.Attribute)1 CertificateList (org.openmuc.jasn1.compiler.pkix1explicit88.CertificateList)1 CertificateSerialNumber (org.openmuc.jasn1.compiler.pkix1explicit88.CertificateSerialNumber)1 DirectoryString (org.openmuc.jasn1.compiler.pkix1explicit88.DirectoryString)1