xor wrap around loses no info To: hackers@_ERASE_freebsd.org Subject: cksum patches A friend of mine (Terry Carroll, no email address available) hacked cksum sources on my FreeBSD system to make them `better/faster?' There's no commit comment/ justification with them 'cos he left in a rush, & said the comments document it. He mentioned something about crc calculation being wasted processor time, as as we don't want to do recovery, something simpler will suffice. He also mentioned the `discard' as being deprecable (see code) Don't ask me more, I'm not sure ;-) I'm posting it so's his work is not lost. I've not installed or tested it. Perhaps someone who is interested in cksum would like to check this ?. The patches are ours to use how we will. The patches are below. (PS 1.1.5 & 2.0 src/usr.bin/cksum/* are same, so apply patches to either) `Caveat Emptor' (=Buyer Beware) Julian Stacey --- 1,6 ---- # @_ERASE_(#)Makefile 8.1 (Berkeley) 6/6/93 PROG= cksum ! SRCS= cksum.c crc.c print.c sum1.c sum2.c sum3.c .include *** old/usr/src/usr.bin/cksum/cksum.c Wed Aug 18 06:16:39 1993 --- new/usr/src/usr.bin/cksum/cksum.c Mon Nov 7 11:03:04 1994 *************** *** 79,84 **** --- 79,87 ---- } else if (*optarg == '2') { cfncn = csum2; pfncn = psum2; + } else if (*optarg == '3') { + cfncn = csum3; + pfncn = pcrc; } else { (void)fprintf(stderr, "cksum: illegal argument to -o option\n"); *************** *** 119,124 **** void usage() { ! (void)fprintf(stderr, "usage: cksum [-o 1 | 2] [file ...]\n"); exit(1); } --- 122,127 ---- void usage() { ! (void)fprintf(stderr, "usage: cksum [-o 1 | 2 | 3] [file ...]\n"); exit(1); } *** old/usr/src/usr.bin/cksum/extern.h Wed Aug 18 06:16:41 1993 --- new/usr/src/usr.bin/cksum/extern.h Mon Nov 7 11:01:22 1994 *************** *** 42,45 **** --- 42,46 ---- void psum2 __P((char *, unsigned long, unsigned long)); int csum1 __P((int, unsigned long *, unsigned long *)); int csum2 __P((int, unsigned long *, unsigned long *)); + int csum3 __P((int, unsigned long *, unsigned long *)); __END_DECLS *** /dev/null Wed Nov 9 10:17:20 1994 --- new/usr/src/usr.bin/cksum/sum3.c Mon Nov 7 11:20:48 1994 *************** *** 0 **** --- 1,98 ---- + /*- + * Copyright (c) 1994 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + /* Contributed by Terence David Carroll + ** Kirchenstrasse 28, D-81675 Munich, GERMANY + */ + + /* XOR-wrap-SUM is fast, at least when implemented in assembler. + ** It is almost as good as CRC, no information is discarded because + ** of the wrap-around adding and the separate XOR and SUM complement + ** each other in providing a good hash value for the file. + ** Using any sum with discard is brain damaged thinking. + ** CRC is good but has supplementary data requirements. + ** The 16-bits are always processed little-endian style for + ** portability of results for character data across platforms. + */ + #ifndef lint + static char sccsid[] = "@_ERASE_(#)sum3.c 8.1 (Berkeley) 11/7/94"; + #endif /* not lint */ + + #include + #include + + int + csum3(fd, cval, clen) + register int fd; + u_long *cval, *clen; + { + u_char buf[8192]; + register u_char *p; + register u_int val; + register u_int xor; + register u_long sum; + register int nr; + register int nc; + register u_long total; + /* 16-bit XOR and 16-bit wraparound-SUM */ + /* With right rotation before op. */ + nc = xor = sum = total = 0; + while ((nr = read(fd, buf, sizeof(buf))) > 0) { + total += nr; + p = buf; + if ( nc != 0 ) { + val |= *p++ << 8; + xor = ((xor >> 1)|(xor & 1 ? 0x8000 : 0))^val; + sum = ((sum >> 1)|(sum & 1 ? 0x8000 : 0))+val; + if ( sum & 0x10000 ) sum = (sum+1)&0xFFFF; + } + nc = nr & 1; + nr >>= 1; + while ( nr-- != 0 ) { + val = *p++; + val |= *p++ << 8; + xor = ((xor >> 1)|(xor & 1 ? 0x8000 : 0))^val; + sum = ((sum >> 1)|(sum & 1 ? 0x8000 : 0))+val; + if ( sum & 0x10000 ) sum = (sum+1)&0xFFFF; + } + if ( nc != 0 ) val = *p++; + } + if ( nc != 0 ) { + xor = ((xor >> 1)|(xor & 1 ? 0x8000 : 0))^val; + sum = ((sum >> 1)|(sum & 1 ? 0x8000 : 0))+val; + if ( sum & 0x10000 ) sum = (sum+1)&0xFFFF; + } + if (nr < 0) return(1); + *cval = ((u_long)xor << 16)|sum; + *clen = total; + return(0); + }