use of org.openmuc.jasn1.ber.BerTag in project jasn1 by openmuc.
the class BerAny method decode.
public int decode(InputStream is, BerTag tag) throws IOException {
int decodedLength = 0;
int tagLength;
if (tag == null) {
tag = new BerTag();
tagLength = tag.decode(is);
decodedLength += tagLength;
} else {
tagLength = tag.encode(new ReverseByteArrayOutputStream(10));
}
BerLength lengthField = new BerLength();
int lengthLength = lengthField.decode(is);
decodedLength += lengthLength + lengthField.val;
value = new byte[tagLength + lengthLength + lengthField.val];
Util.readFully(is, value, tagLength + lengthLength, lengthField.val);
ReverseByteArrayOutputStream os = new ReverseByteArrayOutputStream(value, tagLength + lengthLength - 1);
BerLength.encodeLength(os, lengthField.val);
tag.encode(os);
return decodedLength;
}
use of org.openmuc.jasn1.ber.BerTag in project jasn1 by openmuc.
the class AuthenticateResponseOk 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(EuiccSigned1.tag)) {
euiccSigned1 = new EuiccSigned1();
subCodeLength += euiccSigned1.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)) {
euiccSignature1 = new BerOctetString();
subCodeLength += euiccSignature1.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(EuiccSigned1.tag)) {
euiccSigned1 = new EuiccSigned1();
subCodeLength += euiccSigned1.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)) {
euiccSignature1 = new BerOctetString();
subCodeLength += euiccSignature1.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);
}
use of org.openmuc.jasn1.ber.BerTag in project jasn1 by openmuc.
the class LoadCRLRequest 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(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 0)) {
crl = new CertificateList();
subCodeLength += crl.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(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 0)) {
crl = new CertificateList();
subCodeLength += crl.decode(is, false);
if (subCodeLength == totalLength) {
return codeLength;
}
}
throw new IOException("Unexpected end of sequence, length tag: " + totalLength + ", actual sequence length: " + subCodeLength);
}
use of org.openmuc.jasn1.ber.BerTag in project jasn1 by openmuc.
the class BerClassWriter method writeSequenceOrSetOfDecodeFunction.
private void writeSequenceOrSetOfDecodeFunction(AsnElementType componentType, String classNameOfSequenceOfElement, boolean hasExplicitTag, boolean isSequence) throws IOException {
Tag componentTag = getTag(componentType);
write("public int decode(InputStream is, boolean withTag) throws IOException {");
write("int codeLength = 0;");
write("int subCodeLength = 0;");
if (componentTag != null || supportIndefiniteLength) {
write("BerTag berTag = new BerTag();");
}
write("if (withTag) {");
write("codeLength += tag.decodeAndCheck(is);");
write("}\n");
write("BerLength length = new BerLength();");
write("codeLength += length.decode(is);");
write("int totalLength = length.val;\n");
if (supportIndefiniteLength) {
writeSequenceOfDecodeIndefiniteLenghtPart(componentType, classNameOfSequenceOfElement, hasExplicitTag);
}
if (hasExplicitTag) {
write("int nextByte = is.read();");
write("if (nextByte == -1) {");
write("throw new EOFException(\"Unexpected end of input stream.\");");
write("}");
if (isSequence) {
write("if (nextByte != (0x30)) {");
} else {
write("if (nextByte != (0x31)) {");
}
write("throw new IOException(\"Tag does not match!\");");
write("}");
write("length.decode(is);");
write("totalLength = length.val;\n");
}
write("while (subCodeLength < totalLength) {");
write(classNameOfSequenceOfElement + " element = new " + classNameOfSequenceOfElement + "();");
String explicitEncoding = getExplicitDecodingParameter(componentType);
if (componentTag != null) {
if (componentTag.type == TagType.EXPLICIT) {
write("subCodeLength += berTag.decode(is);");
write("subCodeLength += length.decode(is);");
explicitEncoding = ", true";
} else {
write("subCodeLength += berTag.decode(is);");
if (isDirectAnyOrChoice(componentType)) {
explicitEncoding = ", berTag";
} else {
explicitEncoding = ", false";
}
}
write("subCodeLength += element.decode(is" + explicitEncoding + ");");
} else {
if (isDirectAnyOrChoice(componentType)) {
write("subCodeLength += element.decode(is, null);");
} else {
write("subCodeLength += element.decode(is, true);");
}
}
write("seqOf.add(element);");
write("}");
write("if (subCodeLength != totalLength) {");
write("throw new IOException(\"Decoded SequenceOf or SetOf has wrong length. Expected \" + totalLength + \" but has \" + subCodeLength);\n");
write("}");
write("codeLength += subCodeLength;\n");
write("return codeLength;");
write("}\n");
}
use of org.openmuc.jasn1.ber.BerTag in project jasn1 by openmuc.
the class BerClassWriter method writeSequenceDecodeIndefiniteLenghtPart.
private void writeSequenceDecodeIndefiniteLenghtPart(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");
}
Aggregations