1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
#include <stdlib.h>
#define import_spp
#define import_knames
#include <iraf.h>
#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<len; i++) {
if (iarray[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<len; i++) {
if (iarray[i] > ~ lsum)
carry++;
lsum += iarray[i];
}
while (carry) {
if (carry > ~ lsum)
newcarry++;
lsum += carry;
carry = newcarry;
newcarry = 0;
}
return (abs(sum = lsum));
}
#endif
|