Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- // Type definitions
- typedef unsigned short w16; // 16-bit word is a short int
- typedef unsigned long w32; // 32-bit word is a long int
- // Prototypes
- w16 checksum(w16 len, w16 buff[]);
- /*
- *************************************************************************hi
- Function: checksum
- Description: Calculate the 16 bit Internet Checksum.
- ***************************************************************************
- */
- w16 checksum(w16 len, w16 buff[])
- {
- w16 word16;
- w32 sum=0;
- w16 i;
- // make 16 bit words out of every two adjacent 8 bit words in the packet
- // and add them up
- // u16 buff[] is an array containing all the octets in the data.
- for (i=0;i<len;i=i+2){
- word16 =((buff[i]<<8)&0xFF00)+(buff[i+1]&0xFF);
- sum = sum + (w32) word16;
- }
- // take only 16 bits out of the 32 bit sum and add up the carries
- while (sum>>16)
- sum = (sum & 0xFFFF)+(sum >> 16);
- // one's complement the result
- sum = ~sum;
- return ((w16) sum);
- }
- int main(void)
- {
- w16 octets = 4;
- w16 result;
- w16 buffer[]={0xBE,0xBE,0xC0,0xCA};
- result = checksum(octets,(w16 *)buffer);
- // Output the checksum
- printf("checksum = %04X \n", result);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement