Search in sources :

Example 16 with BerLength

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

Example 17 with BerLength

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

Example 18 with BerLength

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

the class PrepareDownloadRequest 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(SmdpSigned2.tag)) {
            smdpSigned2 = new SmdpSigned2();
            subCodeLength += smdpSigned2.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)) {
            smdpSignature2 = new BerOctetString();
            subCodeLength += smdpSignature2.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(Octet32.tag)) {
            hashCc = new Octet32();
            subCodeLength += hashCc.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)) {
            smdpCertificate = new Certificate();
            subCodeLength += smdpCertificate.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(SmdpSigned2.tag)) {
        smdpSigned2 = new SmdpSigned2();
        subCodeLength += smdpSigned2.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)) {
        smdpSignature2 = new BerOctetString();
        subCodeLength += smdpSignature2.decode(is, false);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(Octet32.tag)) {
        hashCc = new Octet32();
        subCodeLength += hashCc.decode(is, false);
        subCodeLength += berTag.decode(is);
    }
    if (berTag.equals(Certificate.tag)) {
        smdpCertificate = new Certificate();
        subCodeLength += smdpCertificate.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)

Example 19 with BerLength

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

the class BerClassWriter method writeRetaggingTypeClass.

private void writeRetaggingTypeClass(String typeName, String assignedTypeName, AsnType typeDefinition, Tag tag) throws IOException {
    write("public class " + typeName + " extends " + cleanUpName(assignedTypeName) + " {\n");
    write("private static final long serialVersionUID = 1L;\n");
    if (tag != null) {
        write("public static final BerTag tag = new BerTag(" + getBerTagParametersString(tag) + ");\n");
        if (tag.type == TagType.EXPLICIT) {
            write("public byte[] code = null;\n");
        }
    }
    write("public " + typeName + "() {");
    write("}\n");
    String[] constructorParameters = getConstructorParameters(getUniversalType(typeDefinition));
    if (constructorParameters.length != 2 || constructorParameters[0] != "byte[]") {
        write("public " + typeName + "(byte[] code) {");
        write("super(code);");
        write("}\n");
    }
    if ((!jaxbMode || isPrimitiveOrRetaggedPrimitive(typeDefinition)) && (constructorParameters.length != 0)) {
        String constructorParameterString = "";
        String superCallParameterString = "";
        for (int i = 0; i < constructorParameters.length; i += 2) {
            if (i > 0) {
                constructorParameterString += ", ";
                superCallParameterString += ", ";
            }
            constructorParameterString += constructorParameters[i] + " " + constructorParameters[i + 1];
            superCallParameterString += constructorParameters[i + 1];
        }
        write("public " + typeName + "(" + constructorParameterString + ") {");
        write("super(" + superCallParameterString + ");");
        write("}\n");
        if (constructorParameters[0].equals("BigInteger")) {
            write("public " + typeName + "(long value) {");
            write("super(value);");
            write("}\n");
        } else if (constructorParameters.length == 4 && constructorParameters[3].equals("numBits")) {
            write("public " + typeName + "(boolean[] value) {");
            write("super(value);");
            write("}\n");
        }
    }
    if (tag != null) {
        if (isDirectAnyOrChoice((AsnTaggedType) typeDefinition)) {
            writeSimpleEncodeFunction();
        }
        write("public int encode(OutputStream reverseOS, boolean withTag) throws IOException {\n");
        if (constructorParameters.length != 2 || constructorParameters[0] != "byte[]") {
            write("if (code != null) {");
            write("for (int i = code.length - 1; i >= 0; i--) {");
            write("reverseOS.write(code[i]);");
            write("}");
            write("if (withTag) {");
            write("return tag.encode(reverseOS) + code.length;");
            write("}");
            write("return code.length;");
            write("}\n");
        }
        write("int codeLength;\n");
        if (tag.type == TagType.EXPLICIT) {
            if (isDirectAnyOrChoice((AsnTaggedType) typeDefinition)) {
                write("codeLength = super.encode(reverseOS);");
            } else {
                write("codeLength = super.encode(reverseOS, true);");
            }
            write("codeLength += BerLength.encodeLength(reverseOS, codeLength);");
        } else {
            write("codeLength = super.encode(reverseOS, false);");
        }
        write("if (withTag) {");
        write("codeLength += tag.encode(reverseOS);");
        write("}\n");
        write("return codeLength;");
        write("}\n");
        if (isDirectAnyOrChoice((AsnTaggedType) typeDefinition)) {
            writeSimpleDecodeFunction("true");
        }
        write("public int decode(InputStream is, boolean withTag) throws IOException {\n");
        write("int codeLength = 0;\n");
        write("if (withTag) {");
        write("codeLength += tag.decodeAndCheck(is);");
        write("}\n");
        if (tag.type == TagType.EXPLICIT) {
            write("BerLength length = new BerLength();");
            write("codeLength += length.decode(is);\n");
            if (isDirectAnyOrChoice((AsnTaggedType) typeDefinition)) {
                write("codeLength += super.decode(is, null);\n");
            } else {
                write("codeLength += super.decode(is, true);\n");
            }
        } else {
            write("codeLength += super.decode(is, false);\n");
        }
        write("return codeLength;");
        write("}\n");
    }
    write("}");
}
Also used : AsnBitString(org.openmuc.jasn1.compiler.model.AsnBitString) AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnOctetString(org.openmuc.jasn1.compiler.model.AsnOctetString)

Example 20 with BerLength

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

the class BerClassWriter method writeSetDecodeFunction.

private void writeSetDecodeFunction(List<AsnElementType> componentTypes) throws IOException {
    write("public int decode(InputStream is, boolean withTag) throws IOException {");
    write("int codeLength = 0;");
    write("int subCodeLength = 0;");
    write("BerTag berTag = new BerTag();\n");
    write("if (withTag) {");
    write("codeLength += tag.decodeAndCheck(is);");
    write("}\n");
    write("BerLength length = new BerLength();");
    write("codeLength += length.decode(is);\n");
    write("int totalLength = length.val;");
    if (supportIndefiniteLength == true) {
        writeSetDecodeIndefiniteLenghtPart(componentTypes);
    }
    write("while (subCodeLength < totalLength) {");
    write("subCodeLength += berTag.decode(is);");
    for (int j = 0; j < componentTypes.size(); j++) {
        AsnElementType componentType = componentTypes.get(j);
        Tag componentTag = getTag(componentType);
        String explicitEncoding = ", false";
        String elseString = "";
        if (j != 0) {
            elseString = "else ";
        }
        if (isDirectAnyOrChoice(componentType)) {
            if (!isExplicit(componentTag)) {
                throw new IOException("choice or ANY within set has no explicit tag.");
            }
            write(elseString + "if (berTag.equals(" + getBerTagParametersString(componentTag) + ")) {");
            write("subCodeLength += new BerLength().decode(is);");
            explicitEncoding = ", null";
            write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
            write("subCodeLength += " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
            write("}");
        } else {
            if (componentTag != null) {
                if (isExplicit(componentTag)) {
                    write(elseString + "if (berTag.equals(" + getBerTagParametersString(componentTag) + ")) {");
                } else {
                    write(elseString + "if (berTag.equals(" + getBerTagParametersString(componentTag) + ")) {");
                }
                if (isExplicit(componentTag)) {
                    write("subCodeLength += new BerLength().decode(is);");
                    explicitEncoding = ", true";
                }
            } else {
                write(elseString + "if (berTag.equals(" + getClassNameOfComponent(componentType) + ".tag)) {");
            }
            write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
            if (", null".equals(explicitEncoding)) {
                write("BerLength length2 = new BerLength();");
                write("subCodeLength += length2.decode(is);");
            }
            write("subCodeLength += " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
            write("}");
        }
    }
    write("}");
    write("if (subCodeLength != totalLength) {");
    write("throw new IOException(\"Length of set does not match length tag, length tag: \" + totalLength + \", actual set length: \" + subCodeLength);\n");
    write("}");
    write("codeLength += subCodeLength;\n");
    write("return codeLength;");
    write("}\n");
}
Also used : BerTag(org.openmuc.jasn1.ber.BerTag) AsnTag(org.openmuc.jasn1.compiler.model.AsnTag) AsnBitString(org.openmuc.jasn1.compiler.model.AsnBitString) AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnOctetString(org.openmuc.jasn1.compiler.model.AsnOctetString) IOException(java.io.IOException) AsnElementType(org.openmuc.jasn1.compiler.model.AsnElementType)

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