Xref: utzoo alt.bbs:216 comp.sys.mac.programmer:3069 comp.sys.mac:22577 comp.sources.wanted:5477 Path: utzoo!hoptoad!daisy!wyse!uunet!labrea!agate!helios.ee.lbl.gov!ux1.lbl.gov!beard From: beard@ux1.lbl.gov (Patrick C Beard) Newsgroups: alt.bbs,comp.sys.mac.programmer,comp.sys.mac,comp.sources.wanted Subject: Re: Wanted: CRC-16 Routines in Pascal (C table driven version) Keywords: crc xmodem bbs help wanted pascal checksum table-driven Message-ID: <1235@helios.ee.lbl.gov> Date: 8 Nov 88 07:36:25 GMT References: <1971@uokmax.UUCP> <6672@venera.isi.edu> Sender: usenet@helios.ee.lbl.gov Reply-To: beard@ux1.lbl.gov (Patrick C Beard) Distribution: usa Organization: Lawrence Berkeley Laboratory, Berkeley Lines: 81 In article <6672@venera.isi.edu> dick@venera.isi.edu.UUCP (Richard Gillmann) writes: > >Here is a version of the CRC calculation as used by XMODEM, YMODEM, etc. >It is written in Microsoft Pascal. The umulok routine is a library call >that does an unsigned multiply and allows overflow. It's used to >simulate a shift and check of the carry bit. Of course, this could be >much speeded up by writing in assembler or using tables. > >{Computes a 16-bit circular redundancy check. Initialize your variable for > crc_value to 0, then call this routine for each byte.} >procedure crc_16(c : char; var crc_value : word); >begin > crc_value := crc_value xor (wrd(c)*256); > for var i := 1 to 8 do > if not umulok(crc_value,2,crc_value) then {hi bit was a 1} > crc_value := crc_value xor 16#1021; {X^15+X^12+X^5+1} >end {crc_16}; Here is a table driven version (in C, sorry). crc should be set to zero before using COMPUTE_CRC. /* crc.c -- routine for calculating a CRC-16. Put together by Patrick Beard, from Xmodem.3.6 distribution, by */ /* CRC-16 constants. From Usenet contribution by Mark G. Mendel, Network Systems Corp. (ihnp4!umn-cs!hyper!mark) */ /* the CRC polynomial. */ #define P 0x1021 /* number of bits in CRC */ #define W 16 /* the number of bits per char */ #define B 8 /* CRC-16 constant array... from Usenet contribution by Mark G. Mendel, Network Systems Corp. (ihnp4!umn-cs!hyper!mark) */ /* crctab as calculated by initcrctab() */ unsigned short crctab[1<>(W-B)) ^ byte];