38 lines
1.3 KiB
C
38 lines
1.3 KiB
C
/* ************************************************************************** */
|
|
/* _____ _ */
|
|
/* utils/checksum.c |_ _|__ _ _| |__ ___ _ _ */
|
|
/* | Project: libp7 | |/ _ \| | | | '_ \ / _ \ | | | */
|
|
/* | | (_) | |_| | | | | __/ |_| | */
|
|
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
|
|
/* Last updated: 2016/10/13 07:33:17 |___/ */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#include <libp7/internals.h>
|
|
|
|
/**
|
|
* p7_checksum:
|
|
* Returns the checksum of a packet
|
|
*
|
|
* The checksum is an unsigned integer that can fit in 8 bits.
|
|
*
|
|
* @arg packet pointer to the packet
|
|
* @arg size the packet size (in bytes)
|
|
* @return the checksum
|
|
*/
|
|
|
|
unsigned int p7_checksum(unsigned char *packet, p7ushort_t size)
|
|
{
|
|
unsigned int sum = 0;
|
|
packet++; size -= 3; /* remove type and checksum fields */
|
|
|
|
/* add everything (mod 256) */
|
|
while (size--)
|
|
sum += *packet++;
|
|
|
|
/* NOT + 1 as defined in fxReverse documentation, and be sure it's
|
|
* under 256!
|
|
*/
|
|
sum = ~sum + 1; sum %= 256;
|
|
return (sum);
|
|
}
|