2003-11-28 18:07:29 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2008-04-07 22:48:11 +04:00
|
|
|
// $Id: crc.cc,v 1.2 2008-04-07 18:48:11 sshwarts Exp $
|
2003-11-28 18:07:29 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// I grabbed these CRC routines from the following source:
|
|
|
|
// http://www.landfield.com/faqs/compression-faq/part1/section-25.html
|
|
|
|
//
|
|
|
|
// These routines are very useful, so I'm including them in bochs.
|
|
|
|
// They are not covered by the license, as they are not my doing.
|
|
|
|
// My gratitude to the author for offering them on the 'net.
|
|
|
|
//
|
2006-01-24 22:03:55 +03:00
|
|
|
// I only changed the u_long to Bit32u, and u_char to Bit8u, and gave
|
|
|
|
// the functions prototypes.
|
2003-11-28 18:07:29 +03:00
|
|
|
//
|
|
|
|
// -Kevin
|
|
|
|
//
|
|
|
|
// **************************************************************************
|
|
|
|
// The following C code (by Rob Warnock <rpw3@sgi.com>) does CRC-32 in
|
|
|
|
// BigEndian/BigEndian byte/bit order. That is, the data is sent most
|
|
|
|
// significant byte first, and each of the bits within a byte is sent most
|
|
|
|
// significant bit first, as in FDDI. You will need to twiddle with it to do
|
|
|
|
// Ethernet CRC, i.e., BigEndian/LittleEndian byte/bit order. [Left as an
|
|
|
|
// exercise for the reader.]
|
|
|
|
//
|
|
|
|
// The CRCs this code generates agree with the vendor-supplied Verilog models
|
|
|
|
// of several of the popular FDDI "MAC" chips.
|
|
|
|
// **************************************************************************
|
|
|
|
|
2006-01-24 22:03:55 +03:00
|
|
|
#include "config.h"
|
2003-11-28 18:07:29 +03:00
|
|
|
|
|
|
|
/* Initialized first time "crc32()" is called. If you prefer, you can
|
|
|
|
* statically initialize it at compile time. [Another exercise.]
|
|
|
|
*/
|
2006-01-24 22:03:55 +03:00
|
|
|
static Bit32u crc32_table[256];
|
2003-11-28 18:07:29 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Build auxiliary table for parallel byte-at-a-time CRC-32.
|
|
|
|
*/
|
|
|
|
#define CRC32_POLY 0x04c11db7 /* AUTODIN II, Ethernet, & FDDI */
|
|
|
|
|
2006-01-24 22:03:55 +03:00
|
|
|
static void init_crc32(void)
|
2003-11-28 18:07:29 +03:00
|
|
|
{
|
2008-04-07 22:48:11 +04:00
|
|
|
int i, j;
|
|
|
|
Bit32u c;
|
2003-11-28 18:07:29 +03:00
|
|
|
|
2008-04-07 22:48:11 +04:00
|
|
|
for (i = 0; i < 256; ++i) {
|
|
|
|
for (c = i << 24, j = 8; j > 0; --j)
|
|
|
|
c = c & 0x80000000 ? (c << 1) ^ CRC32_POLY : (c << 1);
|
|
|
|
crc32_table[i] = c;
|
|
|
|
}
|
2003-11-28 18:07:29 +03:00
|
|
|
}
|
2006-01-24 22:03:55 +03:00
|
|
|
|
|
|
|
Bit32u crc32(const Bit8u *buf, int len)
|
|
|
|
{
|
2008-04-07 22:48:11 +04:00
|
|
|
const Bit8u *p;
|
|
|
|
Bit32u crc;
|
2006-01-24 22:03:55 +03:00
|
|
|
|
2008-04-07 22:48:11 +04:00
|
|
|
if (!crc32_table[1]) /* if not already done, */
|
|
|
|
init_crc32(); /* build table */
|
2006-01-24 22:03:55 +03:00
|
|
|
|
2008-04-07 22:48:11 +04:00
|
|
|
crc = 0xffffffff; /* preload shift register, per CRC-32 spec */
|
|
|
|
for (p = buf; len > 0; ++p, --len)
|
|
|
|
crc = (crc << 8) ^ crc32_table[(crc >> 24) ^ *p];
|
|
|
|
return ~crc; /* transmit complement, per CRC-32 spec */
|
2006-01-24 22:03:55 +03:00
|
|
|
}
|