use of org.codice.ddf.libs.klv.KlvDecodingException in project alliance by codice.
the class AbstractMetadataPacket method validateChecksum.
private boolean validateChecksum(final KlvContext klvContext, final byte[] klvBytes) throws KlvDecodingException {
if (!klvContext.hasDataElement(Stanag4609TransportStreamParser.UAS_DATALINK_LOCAL_SET)) {
throw new KlvDecodingException("KLV did not contain the UAS Datalink Local Set");
}
final KlvContext localSetContext = ((KlvLocalSet) klvContext.getDataElementByName(Stanag4609TransportStreamParser.UAS_DATALINK_LOCAL_SET)).getValue();
if (localSetContext.hasDataElement(Stanag4609TransportStreamParser.CHECKSUM)) {
final int packetChecksum = ((KlvUnsignedShort) localSetContext.getDataElementByName(Stanag4609TransportStreamParser.CHECKSUM)).getValue();
short calculatedChecksum = 0;
// checksum length (the checksum value is 2 bytes, which is why we subtract 2).
for (int i = 0; i < klvBytes.length - 2; ++i) {
calculatedChecksum += (klvBytes[i] & 0xFF) << (8 * ((i + 1) % 2));
}
return (calculatedChecksum & 0xFFFF) == packetChecksum;
}
throw new KlvDecodingException("Decoded KLV packet didn't contain checksum (which is required).");
}
Aggregations