/// <summary> /// Reads the FIT binary file header and crc to check compatibility and integrity. /// Also checks data reords size. /// Returns true if file is ok (not corrupt). ///</summary> /// <param name="fitStream">Seekable (file)stream to parse.</param> public bool CheckIntegrity(Stream fitStream) { bool isValid; try { // Is there a valid header? Header header = new Header(fitStream); isValid = header.IsValid(); // Are there as many data bytes as the header claims? isValid &= ((header.Size + header.DataSize + 2) == fitStream.Length); // Is the file CRC ok? byte[] data = new byte[fitStream.Length]; fitStream.Position = 0; fitStream.Read(data, 0, data.Length); isValid &= (CRC.Calc16(data, data.Length) == 0x0000); return(isValid); } catch (FitException) { return(false); } }
/// <summary> /// Recompute the header CRC based on the current contents of the header object /// </summary> public void UpdateCRC() { using (MemoryStream ms = new MemoryStream()) { Write(ms); byte[] headerBuffer = ms.ToArray(); Crc = CRC.Calc16(headerBuffer, headerBuffer.Length - 2); } }
/// <summary> /// Updates the data size and CRC in the file header /// Updates file CRC /// </summary> public void Close() { if (open == false) { throw new FitException("Encode:Close - Encode not opened yet, must call Encode:Open()"); } // Rewrites the header now that the datasize is known header.DataSize = (uint)(fitDest.Length - header.Size); header.UpdateCRC(); header.Write(fitDest); // Compute and write the file CRC to the end of the file byte[] data = new byte[fitDest.Length]; fitDest.Position = 0; fitDest.Read(data, 0, data.Length); ushort fileCrc = CRC.Calc16(data, data.Length); byte[] buffer = BitConverter.GetBytes(fileCrc); fitDest.Write(buffer, 0, 2); }
/// <summary> /// Reads a FIT binary file. /// </summary> /// <param name="fitStream">Seekable (file)stream to parse.</param> /// <returns> /// Returns true if reading finishes successfully. /// </returns> public bool Read(Stream fitStream) { bool readOK = true; try { // Attempt to read header fileHeader = new Header(fitStream); readOK &= fileHeader.IsValid(); if (!readOK) { throw new FitException("FIT decode error: File is not FIT format. Check file header data type."); } if ((fileHeader.ProtocolVersion & Fit.ProtocolVersionMajorMask) > (Fit.ProtocolMajorVersion << Fit.ProtocolVersionMajorShift)) { // The decoder does not support decode accross protocol major revisions throw new FitException(String.Format("FIT decode error: Protocol Version {0}.X not supported by SDK Protocol Ver{1}.{2} ", (fileHeader.ProtocolVersion & Fit.ProtocolVersionMajorMask) >> Fit.ProtocolVersionMajorShift, Fit.ProtocolMajorVersion, Fit.ProtocolMinorVersion)); } // Read data messages and definitions while (fitStream.Position < fitStream.Length - 2) { DecodeNextMessage(fitStream); } // Is the file CRC ok? byte[] data = new byte[fitStream.Length]; fitStream.Position = 0; fitStream.Read(data, 0, data.Length); readOK &= (CRC.Calc16(data, data.Length) == 0x0000); } catch (EndOfStreamException e) { readOK = false; Debug.WriteLine("{0} caught and ignored. ", e.GetType().Name); throw new FitException("Decode:Read - Unexpected End of File", e); } return(readOK); }