Changeset 71b00dcc in mainline for uspace/srv/net/checksum.c


Ignore:
Timestamp:
2010-03-07T22:51:38Z (14 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
60ab6c3
Parents:
b5cbff4 (diff), 31c80a5 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge from lp:~lukasmejdrech/helenos/network.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/checksum.c

    rb5cbff4 r71b00dcc  
    4747#define CRC_DIVIDER_LE  0xEDB88320
    4848
    49 uint32_t compute_crc32_le( uint32_t seed, uint8_t * data, size_t length ){
    50         size_t  index;
     49uint32_t compute_crc32_le(uint32_t seed, uint8_t * data, size_t length){
     50        size_t index;
    5151
    52         while( length >= 8 ){
    53                 seed ^= ( * data );
    54                 for( index = 0; index < 8; ++ index ){
    55                         if( seed & 1 ){
    56                                 seed = ( seed >> 1 ) ^ (( uint32_t ) CRC_DIVIDER_LE );
     52        while(length >= 8){
     53                seed ^= (*data);
     54                for(index = 0; index < 8; ++ index){
     55                        if(seed &1){
     56                                seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE);
    5757                        }else{
    5858                                seed >>= 1;
     
    6262                length -= 8;
    6363        }
    64         if( length > 0 ){
    65                 seed ^= ( * data ) >> ( 8 - length );
    66                 for( index = 0; index < length; ++ index ){
    67                         if( seed & 1 ){
    68                                 seed = ( seed >> 1 ) ^ (( uint32_t ) CRC_DIVIDER_LE );
     64        if(length > 0){
     65                seed ^= (*data) >> (8 - length);
     66                for(index = 0; index < length; ++ index){
     67                        if(seed &1){
     68                                seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE);
    6969                        }else{
    7070                                seed >>= 1;
     
    7676}
    7777
    78 uint32_t compute_crc32_be( uint32_t seed, uint8_t * data, size_t length ){
    79         size_t  index;
     78uint32_t compute_crc32_be(uint32_t seed, uint8_t * data, size_t length){
     79        size_t index;
    8080
    81         while( length >= 8 ){
    82                 seed ^= ( * data ) << 24;
    83                 for( index = 0; index < 8; ++ index ){
    84                         if( seed & 0x80000000 ){
    85                                 seed = ( seed << 1 ) ^ (( uint32_t ) CRC_DIVIDER_BE );
     81        while(length >= 8){
     82                seed ^= (*data) << 24;
     83                for(index = 0; index < 8; ++ index){
     84                        if(seed &0x80000000){
     85                                seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE);
    8686                        }else{
    8787                                seed <<= 1;
     
    9191                length -= 8;
    9292        }
    93         if( length > 0 ){
    94                 seed ^= (( * data ) & ( 0xFF << ( 8 - length ))) << 24;
    95                 for( index = 0; index < length; ++ index ){
    96                         if( seed & 0x80000000 ){
    97                                 seed = ( seed << 1 ) ^ (( uint32_t ) CRC_DIVIDER_BE );
     93        if(length > 0){
     94                seed ^= ((*data) &(0xFF << (8 - length))) << 24;
     95                for(index = 0; index < length; ++ index){
     96                        if(seed &0x80000000){
     97                                seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE);
    9898                        }else{
    9999                                seed <<= 1;
     
    105105}
    106106
    107 uint32_t compute_checksum( uint32_t seed, uint8_t * data, size_t length ){
    108         size_t  index;
     107uint32_t compute_checksum(uint32_t seed, uint8_t * data, size_t length){
     108        size_t index;
    109109
    110110        // sum all the 16 bit fields
    111         for( index = 0; index + 1 < length; index += 2 ){
    112                 seed += ( data[ index ] << 8 ) + data[ index + 1 ];
     111        for(index = 0; index + 1 < length; index += 2){
     112                seed += (data[index] << 8) + data[index + 1];
    113113        }
    114114
    115115        // last odd byte with zero padding
    116         if( index + 1 == length ){
    117                 seed += data[ index ] << 8;
     116        if(index + 1 == length){
     117                seed += data[index] << 8;
    118118        }
    119119
     
    121121}
    122122
    123 uint16_t compact_checksum( uint32_t sum ){
     123uint16_t compact_checksum(uint32_t sum){
    124124        // shorten to the 16 bits
    125         while( sum >> 16 ) sum = ( sum & 0xFFFF ) + ( sum >> 16 );
     125        while(sum >> 16){
     126                sum = (sum &0xFFFF) + (sum >> 16);
     127        }
    126128
    127         return ( uint16_t ) sum;
     129        return (uint16_t) sum;
    128130}
    129131
    130 uint16_t flip_checksum( uint16_t checksum ){
     132uint16_t flip_checksum(uint16_t checksum){
    131133        // flip, zero is returned as 0xFFFF (not flipped)
    132134        checksum = ~ checksum;
     
    134136}
    135137
    136 uint16_t ip_checksum( uint8_t * data, size_t length ){
    137         return flip_checksum( compact_checksum( compute_checksum( 0, data, length )));
     138uint16_t ip_checksum(uint8_t * data, size_t length){
     139        return flip_checksum(compact_checksum(compute_checksum(0, data, length)));
    138140}
    139141
Note: See TracChangeset for help on using the changeset viewer.