1
0

Crc.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #region Copyright
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // The following FIT Protocol software provided may be used with FIT protocol
  4. // devices only and remains the copyrighted property of Dynastream Innovations Inc.
  5. // The software is being provided on an "as-is" basis and as an accommodation,
  6. // and therefore all warranties, representations, or guarantees of any kind
  7. // (whether express, implied or statutory) including, without limitation,
  8. // warranties of merchantability, non-infringement, or fitness for a particular
  9. // purpose, are specifically disclaimed.
  10. //
  11. // Copyright 2016 Dynastream Innovations Inc.
  12. ////////////////////////////////////////////////////////////////////////////////
  13. // ****WARNING**** This file is auto-generated! Do NOT edit this file.
  14. // Profile Version = 16.60Release
  15. // Tag = production-akw-16.60.00-0-g5d3d436
  16. ////////////////////////////////////////////////////////////////////////////////
  17. #endregion
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Diagnostics;
  21. using System.Text;
  22. using System.IO;
  23. namespace Dynastream.Fit
  24. {
  25. /// <summary>
  26. /// Implements Dynastream CRC16 function
  27. /// </summary>
  28. public static class CRC
  29. {
  30. private static ushort[] crcTable = new ushort[]
  31. {
  32. 0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
  33. 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
  34. };
  35. #region Methods
  36. public static ushort Get16(ushort crc, byte data)
  37. {
  38. ushort tmp;
  39. // compute checksum of lower four bits of byte
  40. tmp = crcTable[crc & 0xF];
  41. crc = (ushort)((crc >> 4) & 0x0FFF);
  42. crc = (ushort)(crc ^ tmp ^ crcTable[data & 0xF]);
  43. // compute checksum of upper four bits of byte
  44. tmp = crcTable[crc & 0xF];
  45. crc = (ushort)((crc >> 4) & 0x0FFF);
  46. crc = (ushort)(crc ^ tmp ^ crcTable[(data >> 4) & 0xF]);
  47. return crc;
  48. }
  49. public static ushort Calc16(byte[] dataBlock, int size)
  50. {
  51. ushort crc = 0;
  52. for (int i = 0; i < size; i++)
  53. {
  54. crc = CRC.Get16(crc, dataBlock[i]);
  55. }
  56. return crc;
  57. }
  58. #endregion // Methods
  59. }
  60. } // namespace