xref: /original-bsd/usr.bin/uucp/uucico/chksum.c (revision ec35a16d)
1 /*-
2  * Copyright (c) 1985, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)chksum.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #ifndef pdp11
13 chksum (s, n)
14 register char *s;
15 register n;
16 {
17 	register long sum, x, t;
18 
19 	sum = 0xffff;
20 	x = 0;
21 	do {
22 		/* Rotate left, copying bit 15 to bit 0 */
23 		sum <<= 1;
24 		if (sum & 0x10000)
25 			sum ^= 0x10001;
26 		t = sum;
27 		sum = (sum + (*s++ & 0377)) & 0xffff;
28 		x += sum ^ n;
29 		if (sum <= t)
30 			sum = (sum ^ x) & 0xffff;
31 	} while (--n > 0);
32 
33 	return (long) (short) sum;
34 }
35 #else pdp11
36 chksum(s,n)
37 register char *s;
38 register n;
39 {
40 	register unsigned sum, t;
41 	register x;
42 
43 	sum = -1;
44 	x = 0;
45 
46 	do {
47 		if (sum&0x8000) {
48 			sum <<= 1;
49 			sum++;
50 		} else
51 			sum <<= 1;
52 		t = sum;
53 		sum += (unsigned)*s++ & 0377;
54 		x += sum^n;
55 		if ((sum&0xffff) <= (t&0xffff)) {
56 			sum ^= x;
57 		}
58 	} while (--n > 0);
59 
60 	return sum & 0xffff;
61 }
62 
63 #endif pdp11
64