2013-08-15 10:17:15 +02:00
|
|
|
/*
|
2014-01-31 15:06:01 +01:00
|
|
|
* Copyright (C) 2013 Freie Universität Berlin.
|
2013-08-15 10:17:15 +02:00
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* 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.
|
2014-01-31 15:06:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @{
|
|
|
|
* @file net_help.c
|
|
|
|
* @brief Providing implementation for prototypes defined in net_help.h.
|
|
|
|
* @author Oliver Gesch <oliver.gesch@googlemail.com>
|
2013-08-15 10:17:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2013-12-16 17:54:58 +01:00
|
|
|
#include "thread.h"
|
2013-08-15 10:17:15 +02:00
|
|
|
|
|
|
|
#include "net_help.h"
|
|
|
|
|
2014-11-17 09:38:06 +01:00
|
|
|
uint16_t net_help_csum(uint16_t sum, uint8_t *buf, uint16_t len)
|
2013-08-15 10:17:15 +02:00
|
|
|
{
|
2014-08-11 19:51:23 +02:00
|
|
|
int count = len >> 1;
|
2013-08-15 10:17:15 +02:00
|
|
|
|
|
|
|
if (count) {
|
2014-08-11 19:51:23 +02:00
|
|
|
uint16_t carry = 0;
|
2013-08-15 10:17:15 +02:00
|
|
|
|
2014-08-11 19:51:23 +02:00
|
|
|
do {
|
|
|
|
uint16_t t = (*buf << 8) + *(buf + 1);
|
|
|
|
count--;
|
|
|
|
buf += 2;
|
2013-08-15 10:17:15 +02:00
|
|
|
sum += carry;
|
2014-08-11 19:51:23 +02:00
|
|
|
sum += t;
|
|
|
|
carry = (t > sum);
|
|
|
|
} while (count);
|
|
|
|
|
|
|
|
sum += carry;
|
2013-08-15 10:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len & 1) {
|
|
|
|
uint16_t u = (*buf << 8);
|
|
|
|
sum += (*buf << 8);
|
|
|
|
|
|
|
|
if (sum < u) {
|
|
|
|
sum++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
}
|
2014-01-31 15:06:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|