⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tools.c

📁 基于东南大学开发的SEP3203的ARM7中的所有驱动
💻 C
📖 第 1 页 / 共 4 页
字号:
    return (INTSWAP((UINT16)~sum));
}
#endif /* IPCHECK_ASM */

/*************************************************************************
*                                                                      
*   FUNCTION                                                            
*                                                                      
*       TLS_IP_Check_Buffer_Chain                                       
*                                                                      
*   DESCRIPTION                                                         
*                                                                      
*       This function checksums an IP Buffer Chain.                     
*                                                                      
*   INPUTS                                                               
*                                                                      
*       *buf_ptr                                                         
*                                                                      
*   OUTPUTS                                                              
*                                                                      
*       UINT16                  The checksum value
*                                                                      
*************************************************************************/
UINT16 TLS_IP_Check_Buffer_Chain (NET_BUFFER *buf_ptr)
{
    UINT32      sum;
    UINT16      *current_byte = NU_NULL;
    NET_BUFFER  *temp_buf_ptr;
    UINT32      data_len, total_length, length;
    UINT8       odd_length;

    /* Init */
    sum             = 0;
    total_length    = 0;
    odd_length      = NU_FALSE;
    length          = buf_ptr->mem_total_data_len;
    
    /* Check for an odd length. */
    if (length % 2)
    {
        /* For an odd length a byte equal to zero is padded to the end 
           of the buffer for checksum computation. This has to be done
           since the checksum is done on 16 bits at a time. Since the 
           buffers are chained there is no easy way to get to the end
           of the buffer. Therefore we will set a flag so that this special
           case can be handled when the loop below reaches the end of
           the buffer. */
        odd_length = NU_TRUE;

    }

    /* Divide the length by two. */
    length = length >> 1;

    /* Get a pointer to the buffer. */
    temp_buf_ptr = buf_ptr;

    /* Loop through the chain computing the checksum on each byte
       of data. */
    while ((temp_buf_ptr) && (total_length < length))
    {
        /* Reset the data pointer. */
        current_byte    = (UINT16 *)temp_buf_ptr->data_ptr;
        
        for (data_len = (temp_buf_ptr->data_len >> 1); data_len && 
            (total_length < length); total_length++, data_len--)
            sum += *current_byte++;

        /* Point to the next buffer. */
        temp_buf_ptr = temp_buf_ptr->next_buffer;
    }

    /* Do we need to handle padding the zero and finishing the checksum
       computation? */
    if (odd_length)
    {
        /* If the length left is 1 then we need to update the current byte
           pointer. The loop above did not because it stops before the 
           last byte. */

        /* Make sure temp ptr is valid */
        if (temp_buf_ptr)
            /* See if the length is 1 */
            if (temp_buf_ptr->data_len == 1)
                /* Update the current byte ptr to the data in the next buffer. */
                current_byte = (UINT16 *)temp_buf_ptr->data_ptr;
        
        /* Pad the zero */
        ((UINT8 *)current_byte)[1] = 0;

        /* Do the checksum for the last 16 bits. */
        sum += *current_byte;
    }
    
    sum = (sum >> 16) + (sum & 0xffff);
    sum += (sum >> 16);
    return (INTSWAP((UINT16)~sum));
}

#if !(TCPCHECK_ASM)
/*************************************************************************
*                                                                      
*   FUNCTION                                                            
*                                                                      
*       TLS_TCP_Check                                                   
*                                                                      
*   DESCRIPTION                                                         
*                                                                      
*       This function checksums a TCP header.                           
*                                                                      
*   INPUTS                                                              
*                                                                      
*       *pseudoheader           Pointer to header to be checksummed    
*       *buf_ptr                                                         
*                                                                      
*   OUTPUTS                                                             
*                                                                      
*       UINT16                   Checksum value of structure            
*                                                                      
*************************************************************************/
UINT16 TLS_TCP_Check (UINT16 *pseudoheader, NET_BUFFER *buf_ptr)
{
    register UINT32 sum = 0;
    register UINT16 *pshdr = pseudoheader;
    UINT16 HUGE     *current_byte;
    NET_BUFFER      *temp_buf_ptr;
    UINT32          data_len;
    UINT32          remainder;
	UINT32          i;

    /*  This used to be a loop.  The loop was removed to save a few
        cycles.  The header length is always 6 16-bit words.  */
    sum += *pshdr++;
    sum += *pshdr++;
    sum += *pshdr++;
    sum += *pshdr++;
    sum += *pshdr++;
    sum += *pshdr;

    /* Get a pointer to the buffer. */
    temp_buf_ptr = buf_ptr;

    /* Loop through the chain computing the checksum on each byte
       of dat+a. */
    /* Loop through the chain computing the checksum on each byte
       of data. */
    while (temp_buf_ptr)
    {
        /* Reset the data pointer. */
        current_byte    = (UINT16 *)temp_buf_ptr->data_ptr;

        /* The checksum is performed on 16 bits at a time. Half the data 
           length. */
        data_len = temp_buf_ptr->data_len >> 1;

        /* Is there an odd number of bytes? */
        remainder = temp_buf_ptr->data_len & 0x1;

        for ( i = 0; i < data_len; i++)
            sum += current_byte[i];

        if (remainder)
        {
            sum += current_byte[data_len] & INTSWAP(0xFF00);
        }

        /* Point to the next buffer. */
        temp_buf_ptr = temp_buf_ptr->next_buffer;
    }

    sum = (sum >> 16) + (sum & 0xffff);
    sum += (sum >> 16);
    return (INTSWAP((UINT16)~sum));

}

#endif /* !(TCPCHECK_ASM) */

#if !(LONGSWAP_ASM)
/*************************************************************************
*
*   FUNCTION                                                              
*                                                                         
*       TLS_Longswap                                                      
*                                                                         
*   DESCRIPTION                                                           
*                                                                         
*       This function swaps 4 bytes of a long number. Note that this 
*       function will work on both Big Endian and Little Endian 
*       architectures. On Big Endian architectures this function will 
*       not change the byte ordering.  
*                                                                         
*   INPUTS                                                                
*                                                                         
*       number                   32-bit value to be byte swapped          
*                                                                         
*   OUTPUTS                                                               
*                                                                         
*       UINT32                   the input value after being byte swapped 
*                                                                         
*************************************************************************/
UINT32 TLS_Longswap(UINT32 number)
{
    unsigned char *p = (unsigned char *)&number;

    return ((unsigned long)p[0] << 24) + 
           ((unsigned long)p[1] << 16) + 
           ((unsigned long)p[2] << 8) + 
            (unsigned long)p[3];

} /* longswap */

#endif /* !(LONGSWAP_ASM) */

#if !(INTSWAP_ASM)
/*************************************************************************
*
*   FUNCTION                                                              
*                                                                         
*       TLS_Intswap                                                       
*                                                                         
*   DESCRIPTION                                                           
*                                                                         
*       This function swaps 2 bytes of a 16-bit number. Note that this 
*       function will work on both Big Endian and Little Endian 
*       architectures. On Big Endian architectures this function will 
*       not change the byte ordering.  
*                                                                         
*   INPUTS                                                                
*                                                                         
*       number                   32-bit value to be byte swapped          
*                                                                         
*   OUTPUTS                                                               
*                                                                         
*       UINT16                   the input value after being byte swapped 
*                                                                         
*************************************************************************/
UINT16 TLS_Intswap(UINT16 number)
{
unsigned char *p = (unsigned char *)&number;

    return (UINT16)((p[0] << 8) + p[1]);

} /* intswap */

#endif /* !(INTSWAP_ASM) */

#if !(COMPAREN_ASM)
/*************************************************************************
*                                                                      
*   FUNCTION                                                            
*                                                                      
*       TLS_Comparen
*                                                                      
*   DESCRIPTION                                                         
*                                                                      
*       This function compares 2 strings for equality.                  
*                                                                      
*   INPUTS                                                              
*                                                                      
*       s1                       Pointer to the first input string      
*       s2                       Pointer to the second input string     
*       len                      Length of characters to compare        
*                                                                      
*   OUTPUTS                                                             
*                                                                      
*       INT16                    Equality indicator :                   
*                               return 0 if strings 1 and 2 are not equal 
*                               return 1 if strings 1 and 2 are equal     
*                                                                      
*************************************************************************/
INT TLS_Comparen (VOID *s1, VOID *s2, unsigned int len)
{
    return (!memcmp ((void *)s1, (void *)s2, len));
}
#endif /* !(COMPAREN_ASM) */

⌨️ 快捷键说明

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