TURAG-Feldbus
xor_checksum.h
Go to the documentation of this file.
1
9#ifndef TINA_UTIL_XOR_CHECKSUM_H_
10#define TINA_UTIL_XOR_CHECKSUM_H_
11
12
13#ifdef __cplusplus
14# include <cstdint>
15# include <cstdbool>
16#else
17# include <stdint.h>
18# include <stdbool.h>
19#endif
20
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26
40static inline uint8_t xor_checksum_calculate(const void* data, unsigned size) {
41 uint8_t checksum = 0;
42
43 uint8_t i;
44 for (i=0; i < size; ++i) checksum ^= ((const uint8_t*)data)[i];
45
46 return checksum;
47}
48
57static inline bool xor_checksum_check(const void* data, unsigned size, uint8_t chksum) {
58 if (chksum == xor_checksum_calculate(data, size)) {
59 return true;
60 } else {
61 return false;
62 }
63}
64
70#ifdef __cplusplus
71}
72#endif
73
74
75#endif /* TINA_UTIL_XOR_CHECKSUM_H_ */