2015-03-28 14:01:31 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
2015-07-01 15:40:56 +02:00
|
|
|
#include <inttypes.h>
|
2015-03-28 14:01:31 +01:00
|
|
|
#include <stdio.h>
|
2015-07-01 15:40:56 +02:00
|
|
|
#include "od.h"
|
2015-08-07 14:56:36 +02:00
|
|
|
#include "net/inet_csum.h"
|
2015-03-28 14:01:31 +01:00
|
|
|
|
2015-07-01 15:40:56 +02:00
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
#include "debug.h"
|
|
|
|
|
2015-11-26 05:33:28 +01:00
|
|
|
uint16_t inet_csum_slice(uint16_t sum, const uint8_t *buf, uint16_t len, size_t accum_len)
|
2015-03-28 14:01:31 +01:00
|
|
|
{
|
|
|
|
uint32_t csum = sum;
|
|
|
|
|
2015-07-01 15:40:56 +02:00
|
|
|
DEBUG("inet_sum: sum = 0x%04" PRIx16 ", len = %" PRIu16, sum, len);
|
|
|
|
#if ENABLE_DEBUG
|
|
|
|
#ifdef MODULE_OD
|
|
|
|
DEBUG(", buf:\n");
|
|
|
|
od_hex_dump(buf, len, OD_WIDTH_DEFAULT);
|
|
|
|
#else
|
|
|
|
DEBUG(", buf output only with od module\n");
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2015-11-26 05:33:28 +01:00
|
|
|
if (len == 0)
|
|
|
|
return csum;
|
|
|
|
|
|
|
|
if (accum_len & 1) { /* if accumulated length is odd */
|
|
|
|
csum += *buf; /* add first byte as bottom half of 16-byte word */
|
|
|
|
buf++;
|
|
|
|
len--;
|
|
|
|
accum_len++;
|
|
|
|
}
|
|
|
|
|
make: fix sign-compare errors
cpu, nrf5x_common: fix sign-compare in periph/flashpage
drivers, periph_common: fix sign-compare in flashpage
cpu, sam0_common: fix sign-compare error in periph/gpio
cpu, cc2538: fix sign-compare in periph/timer
cpu, sam3: fix sign-compare in periph/gpio
cpu, stm32_common: fix sign-compare in periph/pwm
cpu, stm32_common: fix sign-compare in periph/timer
cpu, stm32_common: fix sign-compare in periph/flashpage
cpu, nrf5x_common: fix sign-compare in radio/nrfmin
cpu, samd21: fix sign-compare in periph/pwm
cpu, ezr32wg: fix sign-compare in periph/gpio
cpu, ezr32wg: fix sign-compare in periph/timer
drivers, ethos: fix sign-compare
sys, net: fix sign-compare
cpu, atmega_common: fix sign-compare error
cpu, msp430fxyz: fix sign-compare in periph/gpio
boards, msb-430-common: fix sign-compare in board_init
driver, cc2420: fix sign-compared
sys/net: fix sign-compare in gnrc_tftp
driver, pcd8544: fix sign-compare
driver, pn532: fix sign-compare
driver, sdcard_spi: fix sign-compare
tests: fix sign_compare
sys/net, lwmac: fix sign_compare
pkg, lwip: fix sign-compare
boards, waspmote: make CORECLOCK unsigned long to fix sign_compare error
tests, sock_ip: fix sign compare
tests, msg_avail: fix sign compare
tests, sock_udp: fix sign compare
boards: fix sign-compare for calliope and microbit matrix
2017-10-31 11:57:40 +01:00
|
|
|
for (unsigned i = 0; i < (len >> 1); buf += 2, i++) {
|
2016-03-29 23:31:10 +02:00
|
|
|
csum += (uint16_t)(*buf << 8) + *(buf + 1); /* group bytes by 16-byte words */
|
|
|
|
/* and add them */
|
2015-03-28 14:01:31 +01:00
|
|
|
}
|
|
|
|
|
2016-03-29 23:31:10 +02:00
|
|
|
if ((accum_len + len) & 1) /* if accumulated length is odd */
|
|
|
|
csum += (uint16_t)(*buf << 8); /* add last byte as top half of 16-byte word */
|
2015-03-28 14:01:31 +01:00
|
|
|
|
2015-07-01 18:19:22 +02:00
|
|
|
while (csum >> 16) {
|
|
|
|
uint16_t carry = csum >> 16;
|
|
|
|
csum = (csum & 0xffff) + carry;
|
|
|
|
}
|
2015-03-28 14:01:31 +01:00
|
|
|
|
2015-07-01 18:19:22 +02:00
|
|
|
DEBUG("inet_sum: new sum = 0x%04" PRIx32 "\n", csum);
|
2015-07-01 15:40:56 +02:00
|
|
|
|
2015-07-01 18:19:22 +02:00
|
|
|
return csum;
|
2015-03-28 14:01:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|