# 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.

``````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;
}
``````
LVL 42
###### 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.

Commented:
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
Senior .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
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

Experts Exchange Solution brought to you by