date and time created 91/04/04 12:24:19 by bostic
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Fri, 5 Apr 1991 03:24:19 +0000 (19:24 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Fri, 5 Apr 1991 03:24:19 +0000 (19:24 -0800)
SCCS-vsn: usr.bin/cksum/sum1.c 5.1

usr/src/usr.bin/cksum/sum1.c [new file with mode: 0644]

diff --git a/usr/src/usr.bin/cksum/sum1.c b/usr/src/usr.bin/cksum/sum1.c
new file mode 100644 (file)
index 0000000..51f4a22
--- /dev/null
@@ -0,0 +1,42 @@
+/*-
+ * Copyright (c) 1991 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)sum1.c     5.1 (Berkeley) %G%";
+#endif /* not lint */
+
+#include <sys/types.h>
+#include <unistd.h>
+
+csum1(fd, cval, clen)
+       register int fd;
+       u_long *cval, *clen;
+{
+       register u_long total;
+       register int nr;
+       register u_int crc;
+       register u_char *p;
+       u_char buf[8192];
+
+       /*
+        * 16-bit checksum, rotating right before each addition;
+        * overflow is discarded.
+        */
+       crc = total = 0;
+       while ((nr = read(fd, buf, sizeof(buf))) > 0)
+               for (total += nr, p = buf; nr--; ++p) {
+                       if (crc & 1)
+                               crc |= 0x10000;
+                       crc = ((crc >> 1) + *p) & 0xffff;
+               }
+       if (nr < 0)
+               return(1);
+
+       *cval = crc;
+       *clen = total;
+       return(0);
+}