From: "Björn Kriedemann" Subject: Re: CRC Check Date: 28 Oct 1999 00:00:00 GMT Message-ID: <7va2r3$roj$1@ssauraac-i-1.production.compuserve.com> References: <7v6hup$ka5$1@nnrp1.deja.com> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 X-Complaints-To: newsmaster@compuserve.com X-Trace: ssauraac-i-1.production.compuserve.com 941133475 28435 212.211.64.42 (28 Oct 1999 17:57:55 GMT) Organization: CompuServe Interactive Services NNTP-Posting-Date: 28 Oct 1999 17:57:55 GMT Newsgroups: de.comp.lang.pascal.delphi Hallo aldi666, (also was für Namen die Standesbeamten inzwischen ohne Murren eintragen ...) >vielleicht kann mir jamand mal eine CRC-Routine schicken?? die Routinen bauen bei jedem Aufruf erneut ihre CRC-Tabelle auf. Falls die CRC-Routine in einer Schleife zur Verwendung kommt, sollte man die entsprechende Tabelle als globale Variable deklarieren und im initialization Teil mit BuildCRCnnTable initialisieren. unit CRC; // implementiert 3 CRC-Routinen zur Berechnung der Prüfsumme nach dem // Cyclic Redundancy Check Algorithmus // siehe DDJ April 97 pp103. {$R-} interface function CalculateCRC16(const Data; Length: LongWord): Word; function CalculateXYZModemCRC16(const Data; Length: LongWord): Word; function CalculateCRC32(const Data; Length: LongWord): LongWord; implementation type TCRC16Table = array[0..255] of Word; TCRC32Table = array[0..255] of LongWord; TBytes = array[0..MaxInt - 1] of Byte; procedure BuildCRC16Table(var CRC16Table: TCRC16Table); var i, j: word; CRC: word; begin for i := 0 to 255 do begin CRC := (i shl 8); for j := 0 to 7 do begin if (crc and $8000) = $8000 then crc := (crc shl 1) xor $1021 else crc := (crc shl 1); end; CRC16Table[i]:=crc; end; end; procedure BuildCRC32Table(var CRC32Table: TCRC32Table); var i, j: LongWord; CRC: LongWord; begin for i:=0 to 255 do begin CRC:=(i shl 24); for j:=0 to 7 do begin if (crc and $80000000)=$80000000 then crc:=(crc shl 1) xor $04C11DB7 else crc:=(crc shl 1); end; CRC32Table[i]:=crc; end; end; function CalculateCRC16(const Data; Length: LongWord): Word; var CRC16Table: TCRC16Table; CRC16: word; index: byte; i:LongWord; begin BuildCRC16Table(CRC16Table); CRC16:=0; for i:=0 to Length-1 do begin index := (CRC16 shr 8) and $FF; CRC16 := CRC16Table[Index] xor (CRC16 shl 8) xor TBytes(Data)[i]; end; Result := CRC16; end; function CalculateXYZModemCRC16(const Data; Length: LongWord): Word; var CRC16Table: TCRC16Table; CRC16: word; index: byte; i:LongWord; begin BuildCRC16Table(CRC16Table); CRC16:=$FFFF; for i:=0 to Length-1 do begin index := ((CRC16 shr 8) xor TBytes(Data)[i]) and $FF; CRC16 := CRC16Table[Index] xor (CRC16 shl 8); end; Result := CRC16; end; function CalculateCRC32(const Data; Length: LongWord): LongWord; var CRC32Table: TCRC32Table; CRC32: LongWord; index: byte; i:LongWord; begin BuildCRC32Table(CRC32Table); CRC32:=$FFFFFFFF; for i:=0 to Length-1 do begin index := ((CRC32 shr 24) xor TBytes(Data)[i]) and $FF; CRC32 := CRC32Table[Index] xor (CRC32 shl 8); end; Result := CRC32; end; - björn -