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);
}
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;
}
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;
}
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;
}
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);
}
Aggregations