Search in sources :

Example 1 with BerVisibleString

use of org.openmuc.jasn1.ber.types.string.BerVisibleString in project jasn1 by openmuc.

the class BerClassWriter method getBerType.

private String getBerType(AsnType asnType) {
    String fullClassName = asnType.getClass().getName();
    String className = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
    if (className.equals("AsnCharacterString")) {
        AsnCharacterString asnCharacterString = (AsnCharacterString) asnType;
        if (asnCharacterString.stringtype.equals("ISO646String")) {
            return "BerVisibleString";
        } else if (asnCharacterString.stringtype.equals("T61String")) {
            return "BerTeletexString";
        }
        return "Ber" + ((AsnCharacterString) asnType).stringtype;
    }
    return "Ber" + className.substring(3);
}
Also used : AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnBitString(org.openmuc.jasn1.compiler.model.AsnBitString) AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnOctetString(org.openmuc.jasn1.compiler.model.AsnOctetString)

Example 2 with BerVisibleString

use of org.openmuc.jasn1.ber.types.string.BerVisibleString in project jasn1 by openmuc.

the class PersonnelRecord 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(Name.tag)) {
            name = new Name();
            subCodeLength += name.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.CONTEXT_CLASS, BerTag.PRIMITIVE, 0)) {
            title = new BerVisibleString();
            subCodeLength += title.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(org.openmuc.jasn1.compiler.modules.module2.EmployeeNumberZ.tag)) {
            number = new org.openmuc.jasn1.compiler.modules.module2.EmployeeNumberZ();
            subCodeLength += number.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.CONTEXT_CLASS, BerTag.PRIMITIVE, 1)) {
            dateOfHire = new Date();
            subCodeLength += dateOfHire.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.CONTEXT_CLASS, BerTag.CONSTRUCTED, 2)) {
            nameOfSpouse = new Name();
            subCodeLength += nameOfSpouse.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.CONTEXT_CLASS, BerTag.CONSTRUCTED, 3)) {
            children = new Children();
            subCodeLength += children.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.CONTEXT_CLASS, BerTag.PRIMITIVE, 4)) {
            testBitString = new MyBitString();
            subCodeLength += testBitString.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.CONTEXT_CLASS, BerTag.PRIMITIVE, 6)) {
            test = new MyInt();
            subCodeLength += test.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;
        }
        test2 = new TestChoice();
        int choiceDecodeLength = test2.decode(is, berTag);
        if (choiceDecodeLength != 0) {
            subCodeLength += choiceDecodeLength;
            subCodeLength += berTag.decode(is);
        } else {
            test2 = null;
        }
        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;
        }
        test3 = new TestChoice();
        choiceDecodeLength = test3.decode(is, berTag);
        if (choiceDecodeLength != 0) {
            subCodeLength += choiceDecodeLength;
            subCodeLength += berTag.decode(is);
        } else {
            test3 = null;
        }
        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.CONTEXT_CLASS, BerTag.CONSTRUCTED, 8)) {
            subCodeLength += length.decode(is);
            test4 = new TestChoice();
            choiceDecodeLength = test4.decode(is, null);
            if (choiceDecodeLength != 0) {
                subCodeLength += choiceDecodeLength;
                subCodeLength += berTag.decode(is);
            } else {
                test4 = null;
            }
        }
        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.CONTEXT_CLASS, BerTag.CONSTRUCTED, 9)) {
            subCodeLength += length.decode(is);
            test5 = new TestChoice();
            choiceDecodeLength = test5.decode(is, null);
            if (choiceDecodeLength != 0) {
                subCodeLength += choiceDecodeLength;
                subCodeLength += berTag.decode(is);
            } else {
                test5 = null;
            }
        }
        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.CONTEXT_CLASS, BerTag.CONSTRUCTED, 10)) {
            subCodeLength += length.decode(is);
            test6 = new TestChoice();
            choiceDecodeLength = test6.decode(is, null);
            if (choiceDecodeLength != 0) {
                subCodeLength += choiceDecodeLength;
                subCodeLength += berTag.decode(is);
            } else {
                test6 = null;
            }
        }
        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;
        }
        employeeNumberZ = new EmployeeNumberZ();
        choiceDecodeLength = employeeNumberZ.decode(is, berTag);
        if (choiceDecodeLength != 0) {
            subCodeLength += choiceDecodeLength;
            subCodeLength += berTag.decode(is);
        } else {
            employeeNumberZ = null;
        }
        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(BerVisibleString.tag)) {
            code_ = new BerVisibleString();
            subCodeLength += code_.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(TestSequenceOf.tag)) {
            testSequenceOf = new TestSequenceOf();
            subCodeLength += testSequenceOf.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(TestSequenceOf2.tag)) {
            testSequenceOf2 = new TestSequenceOf2();
            subCodeLength += testSequenceOf2.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(BerEmbeddedPdv.tag)) {
            embeddedPdv = new BerEmbeddedPdv();
            subCodeLength += embeddedPdv.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(Name.tag)) {
        name = new Name();
        subCodeLength += name.decode(is, false);
        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, 0)) {
        title = new BerVisibleString();
        subCodeLength += title.decode(is, false);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(org.openmuc.jasn1.compiler.modules.module2.EmployeeNumberZ.tag)) {
        number = new org.openmuc.jasn1.compiler.modules.module2.EmployeeNumberZ();
        subCodeLength += number.decode(is, false);
        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)) {
        dateOfHire = new Date();
        subCodeLength += dateOfHire.decode(is, false);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 2)) {
        nameOfSpouse = new Name();
        subCodeLength += nameOfSpouse.decode(is, false);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 3)) {
        children = new Children();
        subCodeLength += children.decode(is, false);
        subCodeLength += berTag.decode(is);
    }
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 4)) {
        testBitString = new MyBitString();
        subCodeLength += testBitString.decode(is, false);
        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, 6)) {
        test = new MyInt();
        subCodeLength += test.decode(is, false);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    test2 = new TestChoice();
    int choiceDecodeLength = test2.decode(is, berTag);
    if (choiceDecodeLength != 0) {
        subCodeLength += choiceDecodeLength;
        subCodeLength += berTag.decode(is);
    } else {
        test2 = null;
    }
    test3 = new TestChoice();
    subCodeLength += test3.decode(is, berTag);
    subCodeLength += berTag.decode(is);
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 8)) {
        subCodeLength += length.decode(is);
        test4 = new TestChoice();
        subCodeLength += test4.decode(is, null);
        subCodeLength += berTag.decode(is);
    }
    if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 9)) {
        subCodeLength += length.decode(is);
        test5 = new TestChoice();
        subCodeLength += test5.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.CONSTRUCTED, 10)) {
        subCodeLength += length.decode(is);
        test6 = new TestChoice();
        subCodeLength += test6.decode(is, null);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    employeeNumberZ = new EmployeeNumberZ();
    choiceDecodeLength = employeeNumberZ.decode(is, berTag);
    if (choiceDecodeLength != 0) {
        subCodeLength += choiceDecodeLength;
        subCodeLength += berTag.decode(is);
    } else {
        employeeNumberZ = null;
    }
    if (berTag.equals(BerVisibleString.tag)) {
        code_ = new BerVisibleString();
        subCodeLength += code_.decode(is, false);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(TestSequenceOf.tag)) {
        testSequenceOf = new TestSequenceOf();
        subCodeLength += testSequenceOf.decode(is, false);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(TestSequenceOf2.tag)) {
        testSequenceOf2 = new TestSequenceOf2();
        subCodeLength += testSequenceOf2.decode(is, false);
        subCodeLength += berTag.decode(is);
    } else {
        throw new IOException("Tag does not match the mandatory sequence element tag.");
    }
    if (berTag.equals(BerEmbeddedPdv.tag)) {
        embeddedPdv = new BerEmbeddedPdv();
        subCodeLength += embeddedPdv.decode(is, false);
        if (subCodeLength == totalLength) {
            return codeLength;
        }
    }
    throw new IOException("Unexpected end of sequence, length tag: " + totalLength + ", actual sequence length: " + subCodeLength);
}
Also used : IOException(java.io.IOException) EmployeeNumberZ(org.openmuc.jasn1.compiler.modules.module2.EmployeeNumberZ) EmployeeNumberZ(org.openmuc.jasn1.compiler.modules.module2.EmployeeNumberZ) EOFException(java.io.EOFException)

Example 3 with BerVisibleString

use of org.openmuc.jasn1.ber.types.string.BerVisibleString in project jasn1 by openmuc.

the class X690BerExampleTest method encodingDecoding.

@Test
public void encodingDecoding() throws IOException {
    ReverseByteArrayOutputStream berOS = new ReverseByteArrayOutputStream(1000);
    // Name name = new Name(new BerVisibleString("John"), new
    // BerVisibleString("P"), new BerVisibleString("Smith"));
    Name name = new Name();
    name.code = new byte[] { (byte) 0x10, (byte) 0x1A, (byte) 0x04, (byte) 0x4a, (byte) 0x6f, (byte) 0x68, (byte) 0x6e, (byte) 0x1A, (byte) 0x01, (byte) 0x50, (byte) 0x1A, (byte) 0x05, (byte) 0x53, (byte) 0x6d, (byte) 0x69, (byte) 0x74, (byte) 0x68 };
    BerVisibleString title = new BerVisibleString("Director".getBytes("US-ASCII"));
    // EmployeeNumber number = new EmployeeNumber(51);
    EmployeeNumber number = new EmployeeNumber();
    number.code = new byte[] { 0x01, 0x33 };
    Date dateOfHire = new Date("19710917".getBytes());
    Name nameOfSpouse = new Name();
    nameOfSpouse.setGivenName(new BerVisibleString("Mary"));
    nameOfSpouse.setInitial(new BerVisibleString("T"));
    nameOfSpouse.setFamilyName(new BerVisibleString("Smith"));
    Name child1Name = new Name();
    child1Name.setGivenName(new BerVisibleString("Ralph"));
    child1Name.setInitial(new BerVisibleString("T"));
    child1Name.setFamilyName(new BerVisibleString("Smith"));
    ChildInformation child1 = new ChildInformation();
    child1.setName(child1Name);
    child1.setDateOfBirth(new Date("19571111".getBytes()));
    System.out.println("192: " + HexConverter.toShortHexString("19571111".getBytes()));
    child1.encodeAndSave(80);
    System.out.println("geneCode: " + HexConverter.toShortHexString(child1.code));
    System.out.println("realCode: " + HexConverter.toShortHexString(new byte[] { (byte) 0x1f, (byte) 0x61, (byte) 0x11, (byte) 0x1A, (byte) 0x05, (byte) 0x52, (byte) 0x61, (byte) 0x6c, (byte) 0x70, (byte) 0x68, (byte) 0x1A, (byte) 0x01, (byte) 0x54, (byte) 0x1A, (byte) 0x05, (byte) 0x53, (byte) 0x6d, (byte) 0x69, (byte) 0x74, (byte) 0x68, (byte) 0xa0, (byte) 0x0a, (byte) 0x43, (byte) 0x08, (byte) 0x31, (byte) 0x39, (byte) 0x35, (byte) 0x37, (byte) 0x31, (byte) 0x31, (byte) 0x31, (byte) 0x31 }));
    Assert.assertArrayEquals(new byte[] { (byte) 0x1f, (byte) 0x61, (byte) 0x11, (byte) 0x1A, (byte) 0x05, (byte) 0x52, (byte) 0x61, (byte) 0x6c, (byte) 0x70, (byte) 0x68, (byte) 0x1A, (byte) 0x01, (byte) 0x54, (byte) 0x1A, (byte) 0x05, (byte) 0x53, (byte) 0x6d, (byte) 0x69, (byte) 0x74, (byte) 0x68, (byte) 0xa0, (byte) 0x0a, (byte) 0x43, (byte) 0x08, (byte) 0x31, (byte) 0x39, (byte) 0x35, (byte) 0x37, (byte) 0x31, (byte) 0x31, (byte) 0x31, (byte) 0x31 }, child1.code);
    Name child2Name = new Name();
    child2Name.setGivenName(new BerVisibleString("Susan"));
    child2Name.setInitial(new BerVisibleString("B"));
    child2Name.setFamilyName(new BerVisibleString("Jones"));
    ChildInformation child2 = new ChildInformation();
    child2.setName(child2Name);
    child2.setDateOfBirth(new Date("19590717".getBytes()));
    PersonnelRecord.Children childrenSeq = new PersonnelRecord.Children();
    List<ChildInformation> childList = childrenSeq.getChildInformation();
    childList.add(child1);
    childList.add(child2);
    PersonnelRecord personnelRecord = new PersonnelRecord();
    personnelRecord.setName(name);
    personnelRecord.setTitle(title);
    personnelRecord.setNumber(number);
    personnelRecord.setDateOfHire(dateOfHire);
    personnelRecord.setNameOfSpouse(nameOfSpouse);
    personnelRecord.setChildren(childrenSeq);
    personnelRecord.encode(berOS, true);
    byte[] expectedBytes = new byte[] { (byte) 0x60, (byte) 0x81, (byte) 0x85, (byte) 0x61, (byte) 0x10, (byte) 0x1A, (byte) 0x04, (byte) 0x4a, (byte) 0x6f, (byte) 0x68, (byte) 0x6e, (byte) 0x1A, (byte) 0x01, (byte) 0x50, (byte) 0x1A, (byte) 0x05, (byte) 0x53, (byte) 0x6d, (byte) 0x69, (byte) 0x74, (byte) 0x68, (byte) 0xa0, (byte) 0x0a, (byte) 0x1A, (byte) 0x08, (byte) 0x44, (byte) 0x69, (byte) 0x72, (byte) 0x65, (byte) 0x63, (byte) 0x74, (byte) 0x6f, (byte) 0x72, (byte) 0x42, (byte) 0x01, (byte) 0x33, (byte) 0xa1, (byte) 0x0a, (byte) 0x43, (byte) 0x08, (byte) 0x31, (byte) 0x39, (byte) 0x37, (byte) 0x31, (byte) 0x30, (byte) 0x39, (byte) 0x31, (byte) 0x37, (byte) 0xa2, (byte) 0x12, (byte) 0x61, (byte) 0x10, (byte) 0x1A, (byte) 0x04, (byte) 0x4d, (byte) 0x61, (byte) 0x72, (byte) 0x79, (byte) 0x1A, (byte) 0x01, (byte) 0x54, (byte) 0x1A, (byte) 0x05, (byte) 0x53, (byte) 0x6d, (byte) 0x69, (byte) 0x74, (byte) 0x68, (byte) 0xa3, (byte) 0x42, (byte) 0x31, (byte) 0x1f, (byte) 0x61, (byte) 0x11, (byte) 0x1A, (byte) 0x05, (byte) 0x52, (byte) 0x61, (byte) 0x6c, (byte) 0x70, (byte) 0x68, (byte) 0x1A, (byte) 0x01, (byte) 0x54, (byte) 0x1A, (byte) 0x05, (byte) 0x53, (byte) 0x6d, (byte) 0x69, (byte) 0x74, (byte) 0x68, (byte) 0xa0, (byte) 0x0a, (byte) 0x43, (byte) 0x08, (byte) 0x31, (byte) 0x39, (byte) 0x35, (byte) 0x37, (byte) 0x31, (byte) 0x31, (byte) 0x31, (byte) 0x31, (byte) 0x31, (byte) 0x1f, (byte) 0x61, (byte) 0x11, (byte) 0x1A, (byte) 0x05, (byte) 0x53, (byte) 0x75, (byte) 0x73, (byte) 0x61, (byte) 0x6e, (byte) 0x1A, (byte) 0x01, (byte) 0x42, (byte) 0x1A, (byte) 0x05, (byte) 0x4a, (byte) 0x6f, (byte) 0x6e, (byte) 0x65, (byte) 0x73, (byte) 0xa0, (byte) 0x0a, (byte) 0x43, (byte) 0x08, (byte) 0x31, (byte) 0x39, (byte) 0x35, (byte) 0x39, (byte) 0x30, (byte) 0x37, (byte) 0x31, (byte) 0x37 };
    System.out.println("encoded structure:");
    System.out.println(getByteArrayString(berOS.getArray()));
    Assert.assertArrayEquals(expectedBytes, berOS.getArray());
    ByteBuffer byteBuffer = berOS.getByteBuffer();
    Assert.assertEquals((byte) 0x60, byteBuffer.get());
    Assert.assertEquals((byte) 0x37, byteBuffer.get(byteBuffer.limit() - 1));
    ByteArrayInputStream bais = new ByteArrayInputStream(berOS.getArray());
    PersonnelRecord personnelRecord_decoded = new PersonnelRecord();
    personnelRecord_decoded.decode(bais, true);
    Assert.assertEquals("John", new String(personnelRecord_decoded.getName().getGivenName().value));
// System.out
// .println("presentation_context_identifier= "
// +
// cpType_decoded.normal_mode_parameters.presentation_context_definition_list.seqOf.get(0).abstract_syntax_name);
}
Also used : ChildInformation(org.openmuc.jasn1.compiler.x690_ber_example.ChildInformation) EmployeeNumber(org.openmuc.jasn1.compiler.x690_ber_example.EmployeeNumber) PersonnelRecord(org.openmuc.jasn1.compiler.x690_ber_example.PersonnelRecord) BerVisibleString(org.openmuc.jasn1.ber.types.string.BerVisibleString) ByteBuffer(java.nio.ByteBuffer) Date(org.openmuc.jasn1.compiler.x690_ber_example.Date) ReverseByteArrayOutputStream(org.openmuc.jasn1.ber.ReverseByteArrayOutputStream) Name(org.openmuc.jasn1.compiler.x690_ber_example.Name) ByteArrayInputStream(java.io.ByteArrayInputStream) BerVisibleString(org.openmuc.jasn1.ber.types.string.BerVisibleString) Test(org.junit.Test)

Example 4 with BerVisibleString

use of org.openmuc.jasn1.ber.types.string.BerVisibleString in project jasn1 by openmuc.

the class ModulesTest method encodingDecoding.

@Test
public void encodingDecoding() throws IOException {
    ReverseByteArrayOutputStream berOS = new ReverseByteArrayOutputStream(1000);
    MyDate1 dateOfHire = new MyDate1();
    // MyDate1 dateOfHire = new MyDate1("19710917");
    dateOfHire.value = new String("19710917").getBytes();
    dateOfHire.encode(berOS, true);
    MyInt2 myInt2Encode = new MyInt2(2);
    berOS.reset();
    myInt2Encode.encode(berOS, true);
    MyInt2 myInt2Decode = new MyInt2();
    byte[] code = HexConverter.fromShortHexString("a303020102");
    InputStream is = new ByteArrayInputStream(code);
    myInt2Decode.decode(is, true);
    Assert.assertEquals(myInt2Decode.value.intValue(), 2);
    PersonnelRecord pr = new PersonnelRecord();
    Name name = new Name();
    name.setGivenName(new BerVisibleString("givenName".getBytes()));
    name.setFamilyName(new BerVisibleString("familyName".getBytes()));
    name.setInitial(new BerVisibleString("initial".getBytes()));
    pr.setName(name);
    pr.setTitle(new BerVisibleString("title".getBytes()));
    pr.setNumber(new EmployeeNumberZ(1));
    pr.setDateOfHire(new Date("23121981".getBytes()));
    pr.setNameOfSpouse(name);
    ChildInformation child = new ChildInformation();
    child.setName(new Name("child name".getBytes()));
    child.setDateOfBirth(new Date("12121912".getBytes()));
    PersonnelRecord.Children children = new PersonnelRecord.Children();
    List<ChildInformation> childInformation = children.getChildInformation();
    childInformation.add(child);
    childInformation.add(child);
    pr.setTestBitString(new MyBitString(new byte[] { (byte) 0x80, (byte) 0xff }, 10));
    pr.setTest(new MyInt(3));
    TestChoice testChoice = new TestChoice();
    testChoice.setChoiceElement1(child);
    pr.setTest2(testChoice);
    pr.setTest3(testChoice);
    pr.setTest4(testChoice);
    pr.setTest5(testChoice);
    pr.setTest6(testChoice);
    TestSequenceOf testSequenceOf = new TestSequenceOf();
    List<BerInteger> berIntegers = testSequenceOf.getBerInteger();
    for (int i = 0; i < 10; i++) {
        berIntegers.add(new BerInteger(i));
    }
    pr.setTestSequenceOf(testSequenceOf);
    TestSequenceOf2 testSequenceOf2 = new TestSequenceOf2();
    List<SEQUENCE> sequences = testSequenceOf2.getSEQUENCE();
    for (int i = 0; i < 10; i++) {
        SEQUENCE sequence = new SEQUENCE();
        sequence.setTest1(new BerInteger(i++));
        sequence.setTest2(new BerInteger(i));
        sequences.add(sequence);
    }
    pr.setTestSequenceOf2(testSequenceOf2);
    BerEmbeddedPdv berEmbeddedPdv = new BerEmbeddedPdv();
    pr.setEmbeddedPdv(berEmbeddedPdv);
    System.out.println("PersonnelRecord.toString():\n" + pr);
}
Also used : BerVisibleString(org.openmuc.jasn1.ber.types.string.BerVisibleString) MyBitString(org.openmuc.jasn1.compiler.modules.module1.MyBitString) PersonnelRecord(org.openmuc.jasn1.compiler.modules.module1.PersonnelRecord) EmployeeNumberZ(org.openmuc.jasn1.compiler.modules.module2.EmployeeNumberZ) ReverseByteArrayOutputStream(org.openmuc.jasn1.ber.ReverseByteArrayOutputStream) Name(org.openmuc.jasn1.compiler.modules.module1.Name) BerEmbeddedPdv(org.openmuc.jasn1.ber.types.BerEmbeddedPdv) SEQUENCE(org.openmuc.jasn1.compiler.modules.module1.PersonnelRecord.TestSequenceOf2.SEQUENCE) BerVisibleString(org.openmuc.jasn1.ber.types.string.BerVisibleString) MyBitString(org.openmuc.jasn1.compiler.modules.module1.MyBitString) ChildInformation(org.openmuc.jasn1.compiler.modules.module1.ChildInformation) TestSequenceOf(org.openmuc.jasn1.compiler.modules.module1.TestSequenceOf) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TestChoice(org.openmuc.jasn1.compiler.modules.module1.TestChoice) BerInteger(org.openmuc.jasn1.ber.types.BerInteger) TestSequenceOf2(org.openmuc.jasn1.compiler.modules.module1.PersonnelRecord.TestSequenceOf2) Date(org.openmuc.jasn1.compiler.modules.module1.Date) MyDate1(org.openmuc.jasn1.compiler.modules.module1.MyDate1) ByteArrayInputStream(java.io.ByteArrayInputStream) MyInt(org.openmuc.jasn1.compiler.modules.module1.MyInt) MyInt2(org.openmuc.jasn1.compiler.modules.module1.MyInt2) Test(org.junit.Test)

Example 5 with BerVisibleString

use of org.openmuc.jasn1.ber.types.string.BerVisibleString in project jasn1 by openmuc.

the class Jasn1Sample method main.

public static void main(String[] args) throws IOException {
    ReverseByteArrayOutputStream os = new ReverseByteArrayOutputStream(1000);
    // Name name = new Name(new BerVisibleString("John"), new
    // BerVisibleString("P"), new BerVisibleString("Smith"));
    // instead of creating the Name object as in previous statement you can
    // assign the byte code directly as in the following statement. The
    // encode function of the name object will then simply insert this byte
    // array in the BerOutputStream. This can speed up things if the code
    // for certain structures is known and does not change.
    Name name = new Name();
    name.code = new byte[] { (byte) 0x10, (byte) 0x1A, (byte) 0x04, (byte) 0x4a, (byte) 0x6f, (byte) 0x68, (byte) 0x6e, (byte) 0x1A, (byte) 0x01, (byte) 0x50, (byte) 0x1A, (byte) 0x05, (byte) 0x53, (byte) 0x6d, (byte) 0x69, (byte) 0x74, (byte) 0x68 };
    BerVisibleString title = new BerVisibleString("Director".getBytes());
    EmployeeNumber number = new EmployeeNumber(51);
    Date dateOfHire = new Date("19710917".getBytes());
    Name nameOfSpouse = new Name();
    nameOfSpouse.setGivenName(new BerVisibleString("Mary"));
    nameOfSpouse.setInitial(new BerVisibleString("T"));
    nameOfSpouse.setFamilyName(new BerVisibleString("Smith"));
    Name child1Name = new Name();
    child1Name.setGivenName(new BerVisibleString("Ralph"));
    child1Name.setInitial(new BerVisibleString("T"));
    child1Name.setFamilyName(new BerVisibleString("Smith"));
    ChildInformation child1 = new ChildInformation();
    child1.setName(child1Name);
    child1.setDateOfBirth(new Date("19571111".getBytes()));
    // encodeAndSave will start the encoding and save the result in
    // child1.code. This is useful if the same structure will have to be
    // encoded several times as part of different structures. Using this
    // function will make sure that the real encoding is only done once.
    child1.encodeAndSave(80);
    Name child2Name = new Name();
    child2Name.setGivenName(new BerVisibleString("Susan"));
    child2Name.setInitial(new BerVisibleString("B"));
    child2Name.setFamilyName(new BerVisibleString("Jones"));
    ChildInformation child2 = new ChildInformation();
    child2.setName(child2Name);
    child2.setDateOfBirth(new Date("19590717".getBytes()));
    PersonnelRecord.Children childrenSeq = new PersonnelRecord.Children();
    List<ChildInformation> childList = childrenSeq.getChildInformation();
    childList.add(child1);
    childList.add(child2);
    PersonnelRecord personnelRecord = new PersonnelRecord();
    personnelRecord.setName(name);
    personnelRecord.setTitle(title);
    personnelRecord.setNumber(number);
    personnelRecord.setDateOfHire(dateOfHire);
    personnelRecord.setNameOfSpouse(nameOfSpouse);
    personnelRecord.setChildren(childrenSeq);
    personnelRecord.encode(os);
    System.out.println("Encoded bytes:");
    System.out.println(DatatypeConverter.printHexBinary(os.getArray()));
    InputStream is = new ByteArrayInputStream(os.getArray());
    PersonnelRecord personnelRecord_decoded = new PersonnelRecord();
    personnelRecord_decoded.decode(is);
    System.out.println("\nDecoded structure:");
    System.out.println(personnelRecord_decoded);
    System.out.println("Given name = " + personnelRecord_decoded.getName().getGivenName());
}
Also used : ChildInformation(org.openmuc.jasn1.compiler.x690_ber_example.ChildInformation) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EmployeeNumber(org.openmuc.jasn1.compiler.x690_ber_example.EmployeeNumber) BerVisibleString(org.openmuc.jasn1.ber.types.string.BerVisibleString) PersonnelRecord(org.openmuc.jasn1.compiler.x690_ber_example.PersonnelRecord) Date(org.openmuc.jasn1.compiler.x690_ber_example.Date) ReverseByteArrayOutputStream(org.openmuc.jasn1.ber.ReverseByteArrayOutputStream) Name(org.openmuc.jasn1.compiler.x690_ber_example.Name)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)3 ReverseByteArrayOutputStream (org.openmuc.jasn1.ber.ReverseByteArrayOutputStream)3 BerVisibleString (org.openmuc.jasn1.ber.types.string.BerVisibleString)3 InputStream (java.io.InputStream)2 Test (org.junit.Test)2 EmployeeNumberZ (org.openmuc.jasn1.compiler.modules.module2.EmployeeNumberZ)2 ChildInformation (org.openmuc.jasn1.compiler.x690_ber_example.ChildInformation)2 Date (org.openmuc.jasn1.compiler.x690_ber_example.Date)2 EmployeeNumber (org.openmuc.jasn1.compiler.x690_ber_example.EmployeeNumber)2 Name (org.openmuc.jasn1.compiler.x690_ber_example.Name)2 PersonnelRecord (org.openmuc.jasn1.compiler.x690_ber_example.PersonnelRecord)2 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 BerEmbeddedPdv (org.openmuc.jasn1.ber.types.BerEmbeddedPdv)1 BerInteger (org.openmuc.jasn1.ber.types.BerInteger)1 AsnBitString (org.openmuc.jasn1.compiler.model.AsnBitString)1 AsnCharacterString (org.openmuc.jasn1.compiler.model.AsnCharacterString)1 AsnOctetString (org.openmuc.jasn1.compiler.model.AsnOctetString)1 ChildInformation (org.openmuc.jasn1.compiler.modules.module1.ChildInformation)1