Garmin: Fix checksum of concatenated fit files

This commit is contained in:
José Rebelo
2025-05-21 20:58:05 +01:00
parent ed732493e2
commit d2210265f3
2 changed files with 13 additions and 1 deletions
@@ -31,6 +31,14 @@ public class GarminByteBufferReader {
return byteBuffer.position();
}
public void setPosition(final int position) {
byteBuffer.position(position);
}
public int getLimit() {
return byteBuffer.limit();
}
public int readShort() {
return Short.toUnsignedInt(byteBuffer.getShort());
}
@@ -106,10 +106,14 @@ public class FitFile {
}
garminByteBufferReader.setByteOrder(ByteOrder.LITTLE_ENDIAN);
final int fileCrc = garminByteBufferReader.readShort();
final int actualCrc = ChecksumCalculator.computeCrc(fileContents, 0, fileContents.length - 2);
final int actualCrc = ChecksumCalculator.computeCrc(fileContents, 0, garminByteBufferReader.getPosition() - 2);
if (fileCrc != actualCrc) {
throw new FitParseException("Wrong CRC for FIT file: got " + actualCrc + " expected " + fileCrc);
}
if (garminByteBufferReader.getPosition() < garminByteBufferReader.getLimit()) {
LOG.warn("There are {} bytes after the fit file", garminByteBufferReader.getLimit() - garminByteBufferReader.getPosition());
// TODO a fit file should actually be multiple fit files
}
return new FitFile(header, dataRecords);
}