MAV'RIC
checksum.h
1 #ifdef __cplusplus
2 extern "C" {
3 #endif
4 
5 #ifndef _CHECKSUM_H_
6 #define _CHECKSUM_H_
7 
8 
15 #define X25_INIT_CRC 0xffff
16 #define X25_VALIDATE_CRC 0xf0b8
17 
18 #ifndef HAVE_CRC_ACCUMULATE
19 
28 static inline void crc_accumulate(uint8_t data, uint16_t *crcAccum)
29 {
30  /*Accumulate one byte of data into the CRC*/
31  uint8_t tmp;
32 
33  tmp = data ^ (uint8_t)(*crcAccum &0xff);
34  tmp ^= (tmp<<4);
35  *crcAccum = (*crcAccum>>8) ^ (tmp<<8) ^ (tmp <<3) ^ (tmp>>4);
36 }
37 #endif
38 
39 
45 static inline void crc_init(uint16_t* crcAccum)
46 {
47  *crcAccum = X25_INIT_CRC;
48 }
49 
50 
58 static inline uint16_t crc_calculate(const uint8_t* pBuffer, uint16_t length)
59 {
60  uint16_t crcTmp;
61  crc_init(&crcTmp);
62  while (length--) {
63  crc_accumulate(*pBuffer++, &crcTmp);
64  }
65  return crcTmp;
66 }
67 
68 
78 static inline void crc_accumulate_buffer(uint16_t *crcAccum, const char *pBuffer, uint16_t length)
79 {
80  const uint8_t *p = (const uint8_t *)pBuffer;
81  while (length--) {
82  crc_accumulate(*p++, crcAccum);
83  }
84 }
85 
86 #endif /* _CHECKSUM_H_ */
87 
88 #ifdef __cplusplus
89 }
90 #endif