Search in sources :

Example 1 with KlvDecodingException

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).");
}
Also used : KlvLocalSet(org.codice.ddf.libs.klv.data.set.KlvLocalSet) KlvUnsignedShort(org.codice.ddf.libs.klv.data.numerical.KlvUnsignedShort) KlvContext(org.codice.ddf.libs.klv.KlvContext) KlvDecodingException(org.codice.ddf.libs.klv.KlvDecodingException)

Aggregations

KlvContext (org.codice.ddf.libs.klv.KlvContext)1 KlvDecodingException (org.codice.ddf.libs.klv.KlvDecodingException)1 KlvUnsignedShort (org.codice.ddf.libs.klv.data.numerical.KlvUnsignedShort)1 KlvLocalSet (org.codice.ddf.libs.klv.data.set.KlvLocalSet)1