Search in sources :

Example 6 with BerTag

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

the class BerClassWriter method writeChoiceClass.

private void writeChoiceClass(String className, AsnChoice asn1TypeElement, Tag tag, String isStaticStr, List<String> listOfSubClassNames) throws IOException {
    write("public" + isStaticStr + " class " + className + " implements " + berTypeInterfaceString + "Serializable {\n");
    write("private static final long serialVersionUID = 1L;\n");
    write("public byte[] code = null;");
    if (tag != null) {
        write("public static final BerTag tag = new BerTag(" + getBerTagParametersString(tag) + ");\n");
    }
    List<AsnElementType> componentTypes = asn1TypeElement.componentTypes;
    addAutomaticTagsIfNeeded(componentTypes);
    if (asn1TypeElement.parameters != null) {
        List<AsnParameter> parameters = asn1TypeElement.parameters;
        replaceParamtersByAnyTypes(componentTypes, parameters);
    }
    for (AsnElementType componentType : componentTypes) {
        if (componentType.typeReference != null && (componentType.typeReference instanceof AsnConstructedType)) {
            listOfSubClassNames.add(getClassNameOfComponent(componentType));
        }
    }
    for (AsnElementType componentType : componentTypes) {
        if (isInnerType(componentType)) {
            String subClassName = getClassNameOfComponent(componentType);
            writeConstructedTypeClass(subClassName, componentType.typeReference, null, true, listOfSubClassNames);
        }
    }
    setClassNamesOfComponents(listOfSubClassNames, componentTypes);
    writePublicMembers(componentTypes);
    writeEmptyConstructor(className);
    if (!jaxbMode) {
        writeEncodeConstructor(className, componentTypes);
    }
    if (jaxbMode) {
        writeGetterAndSetter(componentTypes);
    }
    writeChoiceEncodeFunction(componentTypes, tag != null);
    writeChoiceDecodeFunction(componentTypes, tag != null);
    writeEncodeAndSaveFunction(tag == null);
    writeChoiceToStringFunction(componentTypes);
    write("}\n");
}
Also used : AsnConstructedType(org.openmuc.jasn1.compiler.model.AsnConstructedType) AsnParameter(org.openmuc.jasn1.compiler.model.AsnParameter) AsnBitString(org.openmuc.jasn1.compiler.model.AsnBitString) AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnOctetString(org.openmuc.jasn1.compiler.model.AsnOctetString) AsnElementType(org.openmuc.jasn1.compiler.model.AsnElementType)

Example 7 with BerTag

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

the class BerClassWriter method writeSequenceOrSetClass.

private void writeSequenceOrSetClass(String className, AsnSequenceSet asnSequenceSet, Tag tag, String isStaticStr, List<String> listOfSubClassNames) throws IOException {
    write("public" + isStaticStr + " class " + className + " implements " + berTypeInterfaceString + "Serializable {\n");
    write("private static final long serialVersionUID = 1L;\n");
    List<AsnElementType> componentTypes = asnSequenceSet.componentTypes;
    addAutomaticTagsIfNeeded(componentTypes);
    if (asnSequenceSet.parameters != null) {
        List<AsnParameter> parameters = asnSequenceSet.parameters;
        replaceParamtersByAnyTypes(componentTypes, parameters);
    }
    for (AsnElementType componentType : componentTypes) {
        if (componentType.typeReference != null && (componentType.typeReference instanceof AsnConstructedType)) {
            listOfSubClassNames.add(getClassNameOfComponent(componentType));
        }
    }
    for (AsnElementType componentType : componentTypes) {
        if (isInnerType(componentType)) {
            String subClassName = getClassNameOfComponent(componentType);
            writeConstructedTypeClass(subClassName, componentType.typeReference, null, true, listOfSubClassNames);
        }
    }
    Tag mainTag;
    if (tag == null) {
        if (asnSequenceSet.isSequence) {
            mainTag = stdSeqTag;
        } else {
            mainTag = stdSetTag;
        }
    } else {
        mainTag = tag;
    }
    write("public static final BerTag tag = new BerTag(" + getBerTagParametersString(mainTag) + ");\n");
    write("public byte[] code = null;");
    setClassNamesOfComponents(listOfSubClassNames, componentTypes);
    writePublicMembers(componentTypes);
    writeEmptyConstructor(className);
    if (!jaxbMode) {
        writeEncodeConstructor(className, componentTypes);
    }
    if (jaxbMode) {
        writeGetterAndSetter(componentTypes);
    }
    boolean hasExplicitTag = (tag != null) && (tag.type == TagType.EXPLICIT);
    writeSimpleEncodeFunction();
    writeSequenceOrSetEncodeFunction(componentTypes, hasExplicitTag, asnSequenceSet.isSequence);
    writeSimpleDecodeFunction("true");
    if (asnSequenceSet.isSequence) {
        writeSequenceDecodeFunction(componentTypes, hasExplicitTag);
    } else {
        writeSetDecodeFunction(componentTypes);
    }
    writeEncodeAndSaveFunction();
    writeSequenceOrSetToStringFunction(componentTypes);
    write("}\n");
}
Also used : AsnConstructedType(org.openmuc.jasn1.compiler.model.AsnConstructedType) AsnParameter(org.openmuc.jasn1.compiler.model.AsnParameter) AsnBitString(org.openmuc.jasn1.compiler.model.AsnBitString) AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnOctetString(org.openmuc.jasn1.compiler.model.AsnOctetString) BerTag(org.openmuc.jasn1.ber.BerTag) AsnTag(org.openmuc.jasn1.compiler.model.AsnTag) AsnElementType(org.openmuc.jasn1.compiler.model.AsnElementType)

Example 8 with BerTag

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

the class BerClassWriter method writeSequenceDecodeFunction.

private void writeSequenceDecodeFunction(List<AsnElementType> componentTypes, boolean hasExplicitTag) 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) {
        writeSequenceDecodeIndefiniteLenghtPart(componentTypes);
    }
    write("codeLength += totalLength;\n");
    if (hasExplicitTag) {
        write("int nextByte = is.read();");
        write("if (nextByte == -1) {");
        write("throw new EOFException(\"Unexpected end of input stream.\");");
        write("}");
        write("if (nextByte != (0x30)) {");
        write("throw new IOException(\"Tag does not match!\");");
        write("}");
        write("length.decode(is);");
        write("totalLength = length.val;\n");
    }
    int lastNoneOptionalFieldIndex = -1;
    for (int j = 0; j < componentTypes.size(); j++) {
        AsnElementType componentType = componentTypes.get(componentTypes.size() - 1 - j);
        if (!isOptional(componentType)) {
            lastNoneOptionalFieldIndex = componentTypes.size() - 1 - j;
            break;
        }
    }
    if (lastNoneOptionalFieldIndex == -1) {
        write("if (totalLength == 0) {");
        write("return codeLength;");
        write("}");
    }
    write("subCodeLength += berTag.decode(is);");
    String initChoiceDecodeLength = "int ";
    for (int j = 0; j < componentTypes.size(); j++) {
        AsnElementType componentType = componentTypes.get(j);
        String explicitEncoding = getExplicitDecodingParameter(componentType);
        Tag componentTag = getTag(componentType);
        if (componentTag != null) {
            write("if (berTag.equals(" + getBerTagParametersString(componentTag) + ")) {");
            if (isExplicit(componentTag)) {
                write("subCodeLength += length.decode(is);");
            }
            write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
            write("subCodeLength += " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
            if (lastNoneOptionalFieldIndex <= j) {
                write("if (subCodeLength == totalLength) {");
                write("return codeLength;");
                write("}");
            }
            if (j != (componentTypes.size() - 1)) {
                write("subCodeLength += berTag.decode(is);");
            }
            write("}");
            if (j == (componentTypes.size() - 1)) {
                write("throw new IOException(\"Unexpected end of sequence, length tag: \" + totalLength + \", actual sequence length: \" + subCodeLength);\n");
            } else if (!isOptional(componentType)) {
                write("else {");
                write("throw new IOException(\"Tag does not match the mandatory sequence element tag.\");");
                write("}");
            }
        } else {
            if (isDirectAnyOrChoice(componentType)) {
                write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
                if (isOptional(componentType)) {
                    write(initChoiceDecodeLength + "choiceDecodeLength = " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
                    initChoiceDecodeLength = "";
                    if (j != (componentTypes.size() - 1)) {
                        write("if (choiceDecodeLength != 0) {");
                        write("subCodeLength += choiceDecodeLength;");
                        if (lastNoneOptionalFieldIndex <= j) {
                            write("if (subCodeLength == totalLength) {");
                            write("return codeLength;");
                            write("}");
                        }
                        write("subCodeLength += berTag.decode(is);");
                        write("}");
                        write("else {");
                        write(getName(componentType) + " = null;");
                        write("}");
                    } else {
                        // if last sequence element
                        write("subCodeLength += choiceDecodeLength;");
                        write("if (subCodeLength == totalLength) {");
                        write("return codeLength;");
                        write("}");
                    }
                } else {
                    write("subCodeLength += " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
                    if (j != (componentTypes.size() - 1)) {
                        if (lastNoneOptionalFieldIndex <= j) {
                            write("if (subCodeLength == totalLength) {");
                            write("return codeLength;");
                            write("}");
                        }
                        write("subCodeLength += berTag.decode(is);");
                    } else {
                        write("if (subCodeLength == totalLength) {");
                        write("return codeLength;");
                        write("}");
                    }
                }
                if (j == (componentTypes.size() - 1)) {
                    write("throw new IOException(\"Unexpected end of sequence, length tag: \" + totalLength + \", actual sequence length: \" + subCodeLength);\n");
                }
            } else {
                write("if (berTag.equals(" + getClassNameOfComponent(componentType) + ".tag)) {");
                write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
                write("subCodeLength += " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
                if (lastNoneOptionalFieldIndex <= j) {
                    write("if (subCodeLength == totalLength) {");
                    write("return codeLength;");
                    write("}");
                }
                if (j != (componentTypes.size() - 1)) {
                    write("subCodeLength += berTag.decode(is);");
                }
                write("}");
                if (j == (componentTypes.size() - 1)) {
                    write("throw new IOException(\"Unexpected end of sequence, length tag: \" + totalLength + \", actual sequence length: \" + subCodeLength);\n");
                } else if (!isOptional(componentType)) {
                    write("else {");
                    write("throw new IOException(\"Tag does not match the mandatory sequence element tag.\");");
                    write("}");
                }
            }
        }
        write("");
    }
    if (componentTypes.isEmpty()) {
        write("return subCodeLength;");
    }
    write("}\n");
}
Also used : AsnBitString(org.openmuc.jasn1.compiler.model.AsnBitString) AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnOctetString(org.openmuc.jasn1.compiler.model.AsnOctetString) BerTag(org.openmuc.jasn1.ber.BerTag) AsnTag(org.openmuc.jasn1.compiler.model.AsnTag) AsnElementType(org.openmuc.jasn1.compiler.model.AsnElementType)

Example 9 with BerTag

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

the class BerClassWriter method getExplicitDecodingParameter.

private String getExplicitDecodingParameter(AsnElementType componentType) throws IOException {
    Tag tag = getTag(componentType);
    String explicitEncoding;
    if (tag != null && tag.type == TagType.EXPLICIT) {
        if (isDirectAnyOrChoice(componentType)) {
            explicitEncoding = ", null";
        } else {
            explicitEncoding = ", true";
        }
    } else {
        if (isDirectAnyOrChoice(componentType)) {
            explicitEncoding = ", berTag";
        } else {
            explicitEncoding = ", false";
        }
    }
    return explicitEncoding;
}
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)

Example 10 with BerTag

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

the class BerClassWriter method writeSetDecodeIndefiniteLenghtPart.

private void writeSetDecodeIndefiniteLenghtPart(List<AsnElementType> componentTypes) throws IOException {
    write("if (totalLength == -1) {");
    write("subCodeLength += berTag.decode(is);\n");
    String initChoiceDecodeLength = "int ";
    for (AsnElementType componentType : componentTypes) {
        Tag componentTag = getTag(componentType);
        write("if (berTag.tagNumber == 0 && berTag.tagClass == 0 && berTag.primitive == 0) {");
        write("int nextByte = is.read();");
        write("if (nextByte != 0) {");
        write("if (nextByte == -1) {");
        write("throw new EOFException(\"Unexpected end of input stream.\");");
        write("}");
        write("throw new IOException(\"Decoded sequence has wrong end of contents octets\");");
        write("}");
        write("codeLength += subCodeLength + 1;");
        write("return codeLength;");
        write("}");
        String explicitEncoding;
        if (isDirectAnyOrChoice(componentType)) {
            if (isExplicit(componentTag)) {
                write("if (berTag.equals(" + getBerTagParametersString(componentTag) + ")) {");
                write("subCodeLength += length.decode(is);");
                explicitEncoding = "null";
            } else {
                explicitEncoding = "berTag";
            }
            write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
            write(initChoiceDecodeLength + "choiceDecodeLength = " + getName(componentType) + ".decode(is, " + explicitEncoding + ");");
            if (!isExplicit(componentTag)) {
                initChoiceDecodeLength = "";
            }
            write("if (choiceDecodeLength != 0) {");
            write("subCodeLength += choiceDecodeLength;");
            write("subCodeLength += berTag.decode(is);");
            write("}");
            write("else {");
            write(getName(componentType) + " = null;");
            write("}\n");
            if (isExplicit(componentTag)) {
                write("}");
            }
        } else {
            explicitEncoding = ", false";
            if (componentTag != null) {
                write("if (berTag.equals(" + getBerTagParametersString(componentTag) + ")) {");
                if (isExplicit(componentTag)) {
                    write("codeLength += length.decode(is);");
                    explicitEncoding = ", true";
                }
            } else {
                write("if (berTag.equals(" + getClassNameOfComponent(componentType) + ".tag)) {");
            }
            write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
            write("subCodeLength += " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
            write("subCodeLength += berTag.decode(is);");
            write("}");
        }
    }
    write("int nextByte = is.read();");
    write("if (berTag.tagNumber != 0 || berTag.tagClass != 0 || berTag.primitive != 0");
    write("|| nextByte != 0) {");
    write("if (nextByte == -1) {");
    write("throw new EOFException(\"Unexpected end of input stream.\");");
    write("}");
    write("throw new IOException(\"Decoded sequence has wrong end of contents octets\");");
    write("}");
    write("codeLength += subCodeLength + 1;");
    write("return codeLength;");
    write("}\n");
}
Also used : AsnBitString(org.openmuc.jasn1.compiler.model.AsnBitString) AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnOctetString(org.openmuc.jasn1.compiler.model.AsnOctetString) BerTag(org.openmuc.jasn1.ber.BerTag) AsnTag(org.openmuc.jasn1.compiler.model.AsnTag) AsnElementType(org.openmuc.jasn1.compiler.model.AsnElementType)

Aggregations

IOException (java.io.IOException)14 BerTag (org.openmuc.jasn1.ber.BerTag)12 AsnBitString (org.openmuc.jasn1.compiler.model.AsnBitString)11 AsnCharacterString (org.openmuc.jasn1.compiler.model.AsnCharacterString)11 AsnOctetString (org.openmuc.jasn1.compiler.model.AsnOctetString)11 EOFException (java.io.EOFException)10 AsnTag (org.openmuc.jasn1.compiler.model.AsnTag)9 AsnElementType (org.openmuc.jasn1.compiler.model.AsnElementType)8 Certificate (org.openmuc.jasn1.compiler.pkix1explicit88.Certificate)5 BerLength (org.openmuc.jasn1.ber.BerLength)2 AsnConstructedType (org.openmuc.jasn1.compiler.model.AsnConstructedType)2 AsnParameter (org.openmuc.jasn1.compiler.model.AsnParameter)2 RelativeDistinguishedName (org.openmuc.jasn1.compiler.pkix1explicit88.RelativeDistinguishedName)2 SubjectKeyIdentifier (org.openmuc.jasn1.compiler.pkix1implicit88.SubjectKeyIdentifier)2 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