use of org.openmuc.jasn1.ber.BerLength 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.BerLength in project jasn1 by openmuc.
the class BerBitString method decode.
public int decode(InputStream is, boolean withTag) throws IOException {
// could be encoded in primitiv and constructed mode
// only primitiv mode is implemented
int codeLength = 0;
if (withTag) {
codeLength += tag.decodeAndCheck(is);
}
BerLength length = new BerLength();
codeLength += length.decode(is);
value = new byte[length.val - 1];
int unusedBits = is.read();
if (unusedBits == -1) {
throw new EOFException("Unexpected end of input stream.");
}
if (unusedBits > 7) {
throw new IOException("Number of unused bits in bit string expected to be less than 8 but is: " + unusedBits);
}
numBits = (value.length * 8) - unusedBits;
if (value.length > 0) {
Util.readFully(is, value);
}
codeLength += value.length + 1;
return codeLength;
}
use of org.openmuc.jasn1.ber.BerLength in project jasn1 by openmuc.
the class BerBoolean 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 != 1) {
throw new IOException("Decoded length of BerBoolean is not correct");
}
int nextByte = is.read();
if (nextByte == -1) {
throw new EOFException("Unexpected end of input stream.");
}
codeLength++;
if (nextByte == 0) {
value = false;
} else {
value = true;
}
return codeLength;
}
use of org.openmuc.jasn1.ber.BerLength in project jasn1 by openmuc.
the class BerInteger 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 < 1) {
throw new IOException("Decoded length of BerInteger is not correct");
}
byte[] byteCode = new byte[length.val];
Util.readFully(is, byteCode);
codeLength += length.val;
value = new BigInteger(byteCode);
return codeLength;
}
use of org.openmuc.jasn1.ber.BerLength 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);
}
Aggregations