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 42
Kyle AbrahamsSenior .Net DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Infinity08Commented:
>> 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.