Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1702 | - | 1 | #ifdef __cplusplus |
2 | extern "C" { |
||
3 | #endif |
||
4 | |||
5 | #ifndef _CHECKSUM_H_ |
||
6 | #define _CHECKSUM_H_ |
||
7 | |||
8 | |||
9 | /** |
||
10 | * |
||
11 | * CALCULATE THE CHECKSUM |
||
12 | * |
||
13 | */ |
||
14 | |||
15 | #define X25_INIT_CRC 0xffff |
||
16 | #define X25_VALIDATE_CRC 0xf0b8 |
||
17 | |||
18 | /** |
||
19 | * @brief Accumulate the X.25 CRC by adding one char at a time. |
||
20 | * |
||
21 | * The checksum function adds the hash of one char at a time to the |
||
22 | * 16 bit checksum (uint16_t). |
||
23 | * |
||
24 | * @param data new char to hash |
||
25 | * @param crcAccum the already accumulated checksum |
||
26 | **/ |
||
27 | static inline void crc_accumulate(uint8_t data, uint16_t *crcAccum) |
||
28 | { |
||
29 | /*Accumulate one byte of data into the CRC*/ |
||
30 | uint8_t tmp; |
||
31 | |||
32 | tmp = data ^ (uint8_t)(*crcAccum &0xff); |
||
33 | tmp ^= (tmp<<4); |
||
34 | *crcAccum = (*crcAccum>>8) ^ (tmp<<8) ^ (tmp <<3) ^ (tmp>>4); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @brief Initiliaze the buffer for the X.25 CRC |
||
39 | * |
||
40 | * @param crcAccum the 16 bit X.25 CRC |
||
41 | */ |
||
42 | static inline void crc_init(uint16_t* crcAccum) |
||
43 | { |
||
44 | *crcAccum = X25_INIT_CRC; |
||
45 | } |
||
46 | |||
47 | |||
48 | /** |
||
49 | * @brief Calculates the X.25 checksum on a byte buffer |
||
50 | * |
||
51 | * @param pBuffer buffer containing the byte array to hash |
||
52 | * @param length length of the byte array |
||
53 | * @return the checksum over the buffer bytes |
||
54 | **/ |
||
55 | static inline uint16_t crc_calculate(const uint8_t* pBuffer, uint16_t length) |
||
56 | { |
||
57 | uint16_t crcTmp; |
||
58 | crc_init(&crcTmp); |
||
59 | while (length--) { |
||
60 | crc_accumulate(*pBuffer++, &crcTmp); |
||
61 | } |
||
62 | return crcTmp; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @brief Accumulate the X.25 CRC by adding an array of bytes |
||
67 | * |
||
68 | * The checksum function adds the hash of one char at a time to the |
||
69 | * 16 bit checksum (uint16_t). |
||
70 | * |
||
71 | * @param data new bytes to hash |
||
72 | * @param crcAccum the already accumulated checksum |
||
73 | **/ |
||
74 | static inline void crc_accumulate_buffer(uint16_t *crcAccum, const char *pBuffer, uint8_t length) |
||
75 | { |
||
76 | const uint8_t *p = (const uint8_t *)pBuffer; |
||
77 | while (length--) { |
||
78 | crc_accumulate(*p++, crcAccum); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | |||
83 | |||
84 | |||
85 | #endif /* _CHECKSUM_H_ */ |
||
86 | |||
87 | #ifdef __cplusplus |
||
88 | } |
||
89 | #endif |