📄 chksumtest.c
字号:
#include "typedef.h"
#include "utilities.h"
#include "ip.h"
//#define disno
u16_t ncs(u16_t *buf, u16_t len,u32_t insum)
{
#ifdef disno
//for()
u16_t *p=buf,i=0,l=len;
u8_t *b;
#endif
u32_t sum = insum;
u16_t *wp = (u16_t *) buf;
#ifdef disno
Uart_Printf("\nIP CHECK SUM::\n");
for(b=(BYTE*)buf,i=0;i<len;++i,++b){
if (0==i%10)Uart_Printf("\n");
Uart_Printf(" 0x%x",*(u8_t*)b);
}
#endif
/* Sum up 16 bit values. */
while (len > 1) {sum += *wp++;len -= 2;}
if (len) {
#ifdef debug_checksum
Uart_Printf("\nTCPCHECKSUM: there is an odd data...\n");
#endif
sum += *(u8_t*) wp;}
while (sum >> 16) {sum = (u16_t)sum + (sum >> 16);}
return ((u16_t)(~sum&0xffff));
}
__packed struct plmm{
u32_t a;//2
u32_t b;//2
u8_t c;//
u8_t d;//1
u16_t e;//1
}pt;
WORD TCPCheckSum(struct SIPHead*pIPHead,WORD TCPSize)
{
//<<<<<<<<<<<<<<<<<
u16_t i=0,lend,len=TCPSize ;
u8_t*b ;
//>>>>>>>>>>>>>>>>>>
// #ifdef disno
//<<<<<<<<<<<<<<<<<<
u32_t acc=0 ;
u16_t*p,*wp ;
//>>>>>>>>>>>>>>>>>>
// #endif
pt.a = pIPHead->IPScr ;
pt.b = pIPHead->IPDest ;
pt.c = 0 ;
pt.d = pIPHead->Protocol ;
pt.e = htons (TCPSize);
lend = len;
p=(u16_t*)&pt ;
acc=0 ;
for(i=0;i<6;++i)
acc+=*(u16_t*)p++;
wp=(u16_t*)((u8_t*)pIPHead+IP_HEAD_MIN_LEN);
while(len>1)
{
acc+=*wp++;
len-=2;
}
if(len)
{
#ifdef debu_checksum
Uart_Printf("\nETHNUTCS: there is an odd data...\n");
#endif
acc+=*(u8_t*)wp ;
}
while(acc>>16)
{
acc=(u16_t)acc+(acc>>16);
}
#ifdef disno
////<<<<<<<<<<<<<<<<<<<
Uart_Printf("\nfirst::: TCPCheckSum::\n");
for(b=(BYTE*)((BYTE*)pIPHead+IP_HEAD_MIN_LEN),i=0;i<lend;++i,++b)
{
if(0==i%10)Uart_Printf("\n");
Uart_Printf(" 0x%x",*(u8_t*)b);
}
#endif
////>>>>>>>>>>>>>>>>>>>.
return htons((u16_t)(~acc&0xffff));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -