Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!ames!necntc!adelie!infinet!rhorn From: rhorn@infinet.UUCP (Rob Horn) Newsgroups: comp.misc Subject: Re: Anybody have a checksum algorithm that detects byte-swap? Message-ID: <1138@infinet.UUCP> Date: 29 Jun 88 01:32:14 GMT References: <735@vsi.UUCP> Reply-To: rhorn@infinet.UUCP (Rob Horn) Distribution: comp Organization: Infinet, Inc. North Andover, MA Lines: 29 In article <735@vsi.UUCP> friedl@vsi.UUCP (Stephen J. Friedl) writes: > [wants a checksum-like algorithm that detects transpositions] > > I have a naive algorithm of multiplying the byte just read >with the byte number: > while (c = getchar(), c != EOF) > sum += (c * ++count); Use CRC's. They are faster. A table-driven CRC uses only seven simple instructions per byte, versus three for the basic checksum. For a few more instructions you can avoid the table. The core loop looks like while (...) { crcptr = CRC_TABLE( c ^ crc[low_byte]); crc[low_byte] = crcptr[low_byte] ^ crc[high_byte] crc[high] = crcptr[high_byte] } which can of course be optimized further depending on how the rest of the program is structured. Building CRC_TABLE is more work. For a table free but slightly slower version, see Frank DaCruz's book on Kermit. It will compute CRC's about as fast as the above multiply code. -- Rob Horn UUCP: ...harvard!adelie!infinet!rhorn ...ulowell!infinet!rhorn, ..decvax!infinet!rhorn Snail: Infinet, 40 High St., North Andover, MA