From fa080de7afc95aa1c19a6e6fc0e0708ced2eadc4 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 8 Jul 2015 20:46:52 -0400 Subject: Initial commit --- sys/osb/strsum.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 sys/osb/strsum.c (limited to 'sys/osb/strsum.c') diff --git a/sys/osb/strsum.c b/sys/osb/strsum.c new file mode 100644 index 00000000..71655b3f --- /dev/null +++ b/sys/osb/strsum.c @@ -0,0 +1,100 @@ +#define import_spp +#define import_knames +#include + + +#ifdef INT32_SUM + +/** + * STRSUM -- Compute the 32-bit checksum of an SPP string. + */ + +int +STRSUM (XCHAR *array, XINT *length, XINT *maxch) +{ + int i, len, carry=0, newcarry=0; + unsigned int *iarray, sum = 0; + char pkstr[*maxch]; + + register int n = *maxch; + register XCHAR *ip = array; + register char *op = (char *) pkstr; + + + /* Convert the input string to a packed char array. + */ + while ((*op++ = *ip++) != XEOS && --n >= 0) + ; + *--op = EOS; + + /* Compute the checksum. + */ + iarray = (unsigned int *) pkstr; + len = *length / 4; + + for (i=0; i ~ sum) + carry++; + + sum += iarray[i]; + } + + while (carry) { + if (carry > ~ sum) + newcarry++; + sum += carry; + carry = newcarry; + newcarry = 0; + } + + return (sum); +} + +#else + +/** + * STRSUM -- Compute the 32-bit checksum of an SPP string. + */ + +int +STRSUM (XCHAR *array, XINT *length, XINT *maxch) +{ + int i, len, carry=0, newcarry=0; + unsigned int *iarray, sum = 0; + unsigned long lsum = 0; + char pkstr[*maxch]; + + register int n = *maxch; + register XCHAR *ip = array; + register char *op = (char *) pkstr; + + + /* Convert the input string to a packed char array. + */ + while ((*op++ = *ip++) != XEOS && --n >= 0) + ; + *--op = EOS; + + /* Compute the checksum. + */ + iarray = (unsigned int *) pkstr; + len = *length / 4; + + for (i=0; i ~ lsum) + carry++; + lsum += iarray[i]; + } + + while (carry) { + if (carry > ~ lsum) + newcarry++; + lsum += carry; + carry = newcarry; + newcarry = 0; + } + + return (abs(sum = lsum)); +} + +#endif -- cgit