CRC TCP/IP Lean

Hi All,

Can someone explain what the following code is doing?  I'm assuming it has something to do with CRC and overflow.  

The full function and some hints can be found here but don't have the book as of yet.

http://books.google.com/books?id=NOLVo6KjmVIC&pg=PA310&lpg=PA310&dq=void+check_byte(BYTE+b)&source=bl&ots=jMj-Xp3B0R&sig=loy2s4WwaqiJl3WT7i350g3xqUc&hl=en&ei=tAm6S5C_KYv88wSJutDqAw&sa=X&oi=book_result&ct=result&resnum=4&ved=0CBEQ6AEwAw#v=onepage&q=void%20check_byte(BYTE%20b)&f=false


void check_byte(BYTE b)
{
    if (checkflag)
    {
        if ((checklo = b+checklo) < b)
        {
            if (++checkhi == 0)
                checklo++;
        }
    }
    else
    {
        if ((checkhi = b+checkhi) < b)
        {
            if (++checklo == 0)
                checkhi++;
        }
    }
    checkflag = !checkflag;
}

Open in new window

LVL 41
Kyle AbrahamsSenior .Net DeveloperAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Infinity08Connect With a Mentor Commented:
>> The code passes in IP's (LWORD) for the b parameter.

It can only pass one byte at a time, so the function is called repeatedly for every byte of the input data.


>> I get the checkflag is alternating between low and high but assuming checklo is 0.

Assume that checklo and checkhi start out as 0. They are presumably reset to 0 every time a new checksum needs to be calculated.


>> checklo = b+ checklo < b will always evaluate to false.

Unless there has been an overflow, in which case there is a carry. And that's what the if block is for - to add the carry.


>> what's an LWORD and how is it cast to a byte again?

I don't know how it's defined in your code, but it's likely a 32bit integer value.
You don't cast it to a byte however - you pass each of its 4 bytes to the check_byte function, one after the other. And when that's finished, the checksum can be retrieved from checklo and checkhi.
0
 
Infinity08Commented:
It calculates a 16bit checksum.

This function is called for every byte of the input data. The resulting checksum will be in the chackhi and checklo BYTEs. The checkflag is just used to alternate between the checkhi and checklo BYTEs.
0
 
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
Still not seeing how this is working:

The code passes in IP's (LWORD) for the b parameter.

I get the checkflag is alternating between low and high but assuming checklo is 0.

checklo = b+ checklo < b will always evaluate to false.

It's been a while since I've been this low (mainly .Net now). . . what's an LWORD and how is it cast to a byte again?

0
 
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
Thanks.
0
All Courses

From novice to tech pro — start learning today.