# [Coco] Fwd: IP packets on my coco

Dave Philipsen dave at davebiz.com
Wed Jun 8 02:59:58 EDT 2016

```This is a C code snippet for CRC calculation showing a 256-entry table
of unsigned longs (1024 bytes).  I've also read that there are ways to
to it with a 16-entry table of 32-bit words (64 bytes) but it requires a
little more computation that the 256-entry table.

There are also some webpages that have CRC calculators on them so if you
are writing an algorithm you can test it.  So I don't think calculating
the CRC is going to be all that difficult nor should it be overly CPU
intensive.

Dave

/* Table of CRCs of all 8-bit messages. */
unsigned long crc_table[256];

/* Flag: has the table been computed? Initially false. */
int crc_table_computed = 0;

/* Make the table for a fast CRC. */
void make_crc_table(void)
{
unsigned long c;
int n, k;

*for (n = 0; n < 256; n++) { *c = (unsigned long) n;
for (k = 0; k < 8; k++) {
if (c & 1)
c = 0xedb88320L ^ (c >> 1);
else
c = c >> 1;
}
crc_table[n] = c;
}
crc_table_computed = 1;
}

/* Update a running CRC with the bytes buf[0..len-1]--the CRC
should be initialized to all 1's, and the transmitted value
is the 1's complement of the final running CRC (see the
crc() routine below). */

unsigned long update_crc(unsigned long crc, unsigned char *buf,
int len)
{
unsigned long c = crc;
int n;

if (!crc_table_computed)
make_crc_table();
for (n = 0; n < len; n++) {
c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
}
return c;
}

/* Return the CRC of the bytes buf[0..len-1]. */
unsigned long crc(unsigned char *buf, int len)
{
return update_crc(0xffffffffL, buf, len) ^ 0xffffffffL;
}

Dave

On 6/8/2016 12:57 AM, Barry Nelson wrote:
>   The problem with that approach is that an 8 bit CRC requires a 256 byte lookup table or 2^8 bytes. A 32 bit CRC would need a 2^32 byte table or 4294967296 bytes, that is a 4Gb lookup table.
>
>> Dave Philipsen dave at davebiz.com
>> Wed Jun 8 01:27:51 EDT 2016
>> I remember from the CRC calculation in X/Ymodem that there is an easy
>> and faster way to get the CRC using lookup tables.  That's how I did it
>> with Supercomm.  I believe the CRC for the networking packets is a
>> 32-bit CRC so I don't know if lookup tables are still practical or not
>> with that size CRC.  What is amazing is that I was able to find that
>> information without the internet back then.  I just now did a few
>> cursory searches on calculating CRCs and I'm guessing that there is a
>> fairly easy method that doesn't require so much overhead.
>>
>>
>> Dave
>

```