| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#if defined(_MSC_VER) && (_MSC_VER < 1600) |
| 26 |
typedef unsigned __int64 uint64_t; |
| 27 |
typedef __int64 int64_t; |
| 28 |
typedef unsigned __int32 uint32_t; |
| 29 |
typedef __int32 int32_t; |
| 30 |
typedef unsigned __int16 uint16_t; |
| 31 |
typedef __int16 int16_t; |
| 32 |
typedef unsigned __int8 uint8_t; |
| 33 |
typedef __int8 int8_t; |
| 34 |
|
| 35 |
#else |
| 36 |
|
| 37 |
#include <inttypes.h> |
| 38 |
|
| 39 |
#endif |
| 40 |
|
| 41 |
|
| 42 |
#ifdef __cplusplus |
| 43 |
extern "C" { |
| 44 |
#endif |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
uint16_t CRCEvaluate(uint8_t* datablock, uint32_t datalength) { |
| 58 |
uint32_t i; |
| 59 |
uint16_t crc_tot; |
| 60 |
uint8_t crchi, crclo; |
| 61 |
uint8_t a, b; |
| 62 |
uint8_t tmp1, tmp2; |
| 63 |
|
| 64 |
crchi = 0xFF; |
| 65 |
crclo = 0xFF; |
| 66 |
|
| 67 |
for (i = 0; i < datalength; i++) { |
| 68 |
a = datablock[i]; |
| 69 |
a ^= crchi; |
| 70 |
crchi = a; |
| 71 |
a >>= 4; |
| 72 |
a &= 0x0F; |
| 73 |
a ^= crchi; |
| 74 |
crchi = crclo; |
| 75 |
crclo = a; |
| 76 |
tmp1 = ((a & 0x0F) << 4) & 0xF0; |
| 77 |
tmp2 = ((a & 0xF0) >> 4) & 0x0F; |
| 78 |
a = tmp1 | tmp2; |
| 79 |
b = a; |
| 80 |
tmp1 = ((a & 0x7F) << 1) & 0xFE; |
| 81 |
tmp2 = ((a & 0x80) >> 7) & 0x01; |
| 82 |
a = tmp1 | tmp2; |
| 83 |
a &= 0x1F; |
| 84 |
crchi ^= a; |
| 85 |
a = b & 0xF0; |
| 86 |
crchi ^= a; |
| 87 |
tmp1 = ((b & 0x7F) << 1) & 0xFE; |
| 88 |
tmp2 = ((b & 0x80) >> 7) & 0x01; |
| 89 |
b = tmp1 | tmp2; |
| 90 |
b &= 0xE0; |
| 91 |
crclo ^= b; |
| 92 |
} |
| 93 |
|
| 94 |
crc_tot = ((0x00FF & (uint16_t) crchi) << 8) & 0xFF00; |
| 95 |
crc_tot |= (0x00FF & (uint16_t) crclo); |
| 96 |
|
| 97 |
return (crc_tot); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
int16_t CRCCheck(uint8_t* datablock, uint32_t datalength) |
| 114 |
{ |
| 115 |
uint16_t crc; |
| 116 |
|
| 117 |
crc = 0; |
| 118 |
|
| 119 |
if (datalength <= 2) |
| 120 |
return (-1); |
| 121 |
|
| 122 |
|
| 123 |
crc = CRCEvaluate((uint8_t*) (datablock + 2), (uint32_t) (datalength - 2)); |
| 124 |
if (((uint8_t) ((crc & 0xFF00) >> 8) != (uint8_t) datablock[1]) || |
| 125 |
((uint8_t) (crc & 0x00FF) != (uint8_t) datablock[0])) |
| 126 |
return (0); |
| 127 |
else |
| 128 |
return (1); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
#ifdef __cplusplus |
| 133 |
} |
| 134 |
#endif |
| 135 |
|