cake
/
libp7
Archived
1
0
Fork 1
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libp7/src/utils/checksum.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);
}