-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckSumUtil.cpp
More file actions
executable file
·30 lines (26 loc) · 1021 Bytes
/
checkSumUtil.cpp
File metadata and controls
executable file
·30 lines (26 loc) · 1021 Bytes
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
26
27
28
#include "checkSumUtil.h"
//streamSize is measured in bytes
uint16_t CheckSumUtil::computeSum(void* bytestream, int streamSize) {
uint16_t sum = 0;
for (void* it = bytestream; it < static_cast<uint8_t*>(bytestream) + streamSize; it = static_cast<uint8_t*>(it) + 1) {
// read high byte
uint16_t nextValue = 0x0000;
nextValue |= (*static_cast<uint8_t*>(it) << 8);
// read low byte, if there is one
it = static_cast<uint8_t*>(it) + 1;
if (it < static_cast<uint8_t*>(bytestream) + streamSize) {
nextValue |= (*static_cast<uint8_t*>(it));
}
// Carry the 1 if we overflow the counter
if (static_cast<uint32_t>(nextValue + sum) > 0xFFFF) {
sum += 1;
}
sum += nextValue;
}
return sum;
}
bool CheckSumUtil::checkChecksum(uint16_t checksum, void* bytestream, int streamSize) {
uint16_t sum = computeSum(bytestream, streamSize);
if (sum + checksum == 0xFFFF) return true;
return false;
}