Re: Interesting code I need help with

From: Ojala Pasi 'Albert' (albert_at_cs.tut.fi)
Date: 2004-04-13 09:09:24

> Well, interesting to me.  I'm not a whiz at ML, and I found the 
> following code
> 
> It's from the QLink disk (yes, that disk...)  I know it is a checksum, 
> but I can;t figure out what math it is trying to do.
> 
> The sequence 7f,7f,23,05,09 generates checksum with bcff=82,bd00=3e 
> 
> I was wondering if someone could help me with this.  The end result will 
> be in C, but I can do that myself if I can just figure out the algorithm.

Seems to be 16-bit CRC. Something like:

unsigned short CRC = 0;
unsigned short CalcCRC(unsigned char byte) {
    int i;
    for (i=0;i<8;i++) {
	unsigned short tmp = (byte&1)^CRC;
	byte >>= 1;
	if (tmp & 1) {
	    tmp >>= 1;
	    tmp ^= 0xa001;
	} else {
	    tmp >>= 1;
	}
	CRC = tmp;
    }
    return CRC;
}

The value 0xa001 controls the CRC polynomial used.
At least your test case generates the same CRC.

-Pasi
-- 
"Back in the bad old days as a warning to criminals judges would order
 malcontents to be hung by their ankles: heels over head. The phrase came
 to mean a state of helplessness, confusion. Now that I think of it, that
 *is* being in love."		-- Hill in "Oz", season 3

       Message was sent through the cbm-hackers mailing list

Archive generated by hypermail pre-2.1.8.