ncsi: make ncsi_calculate_checksum work with unaligned data
Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
diff --git a/src/ncsi.c b/src/ncsi.c
index 75dcc08..f3427bd 100644
--- a/src/ncsi.c
+++ b/src/ncsi.c
@@ -38,7 +38,7 @@
#include "ncsi-pkt.h"
-static uint32_t ncsi_calculate_checksum(uint16_t *data, int len)
+static uint32_t ncsi_calculate_checksum(uint8_t *data, int len)
{
uint32_t checksum = 0;
int i;
@@ -47,8 +47,8 @@
* 32-bit unsigned sum of the NC-SI packet header and NC-SI packet
* payload interpreted as a series of 16-bit unsigned integer values.
*/
- for (i = 0; i < len / 2; i++) {
- checksum += htons(data[i]);
+ for (i = 0; i < len; i += 2) {
+ checksum += (((uint16_t) data[i]) << 8) + data[i+1];
}
checksum = (~checksum + 1);
@@ -188,7 +188,7 @@
}
/* Add the optional checksum at the end of the frame. */
- checksum = ncsi_calculate_checksum((uint16_t *)rnh, ncsi_rsp_len);
+ checksum = ncsi_calculate_checksum((uint8_t *)rnh, ncsi_rsp_len);
pchecksum = (uint32_t *)((void *)rnh + ncsi_rsp_len);
*pchecksum = htonl(checksum);
ncsi_rsp_len += 4;