This repository has been archived on 2021-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
kaneton/kaneton/library/sum2.c
Mercier Pierre-Olivier fee4dd4e6d Initial snapshot
2013-02-11 22:04:30 +01:00

47 lines
987 B
C

/*
* licence kaneton licence
*
* project kaneton
*
* file /home/buckman/kaneton/libs/klibrary/libdata/sum2.c
*
* created julien quintard [fri feb 11 02:56:44 2005]
* updated matthieu bucchianeri [wed jul 26 19:18:50 2006]
*/
/*
* ---------- includes --------------------------------------------------------
*/
#include <library/library.h>
/*
* ---------- functions -------------------------------------------------------
*/
u_int32_t sum2(char *buf,
int size)
{
register u_int32_t thecrc;
register u_int32_t total;
register u_char *p;
/*
* Draft 8 POSIX 1003.2:
*
* s = sum of all bytes
* r = s % 2^16 + (s % 2^32) / 2^16
* thecrc = (r % 2^16) + r / 2^16
*/
thecrc = total = 0;
for (total += (u_int32_t)size, p = (u_char*)buf; size--; ++p)
thecrc += *p;
thecrc = (thecrc & 0xffff) + (thecrc >> 16);
thecrc = (thecrc & 0xffff) + (thecrc >> 16);
return (thecrc);
}