cksum.c

来自「开发工具用EMBEST IDE进行开发,该程序是一个S3C44B0直接驱动网卡的」· C语言 代码 · 共 45 行

C
45
字号
/*
;-------------------------------------------------------------------------
; CKSUM.A51
;
; This computes the internet checksum on an 8051
;
; Call from C program: UINT cksum(UCHAR xdata * buf, UINT length);
; Where * buf is pointer to first char (must be in xdata space)
; and "length" is number of bytes (not words) to sum over
;-------------------------------------------------------------------------
*/
#include "44b.h"
#include "44blib.h"
#include "def.h"
#include "option.h"
#include "ethernet.h"
#include "arp.h"
#include "string.h"
#include "lib.h"
unsigned short cksum(unsigned char  *check, unsigned short length) 
{
	unsigned long sum=0;
	unsigned short i;
	unsigned short *ptr; 
    ptr=(unsigned short *)check;
	for (i=0;i<(length)/2;i++)
	{
		sum+=ntohs(*ptr++);

	//	Uart_Printf("\n IP:  sum  %x   %d \n",sum,i);
	}
	if (length&0x01)
	{
		sum=sum+((ntohs(*ptr))&0xff00);
	}
	sum=(sum&0xffff)+((sum>>16)&0xffff);
	if(sum&0xffff0000)
	{
		sum++;
	}
	return ( (unsigned short)((sum)&0xffff));
//	return ( (UINT)(~((sum)&0xffff)));
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?