Changes in uspace/srv/net/checksum.c [aadf01e:918e9910] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/checksum.c
raadf01e r918e9910 47 47 #define CRC_DIVIDER_LE 0xEDB88320 48 48 49 uint32_t compute_crc32_le( uint32_t seed, uint8_t * data, size_t length){50 size_t 49 uint32_t compute_crc32_le( uint32_t seed, uint8_t * data, size_t length ){ 50 size_t index; 51 51 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 ); 57 57 }else{ 58 58 seed >>= 1; … … 62 62 length -= 8; 63 63 } 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 ); 69 69 }else{ 70 70 seed >>= 1; … … 76 76 } 77 77 78 uint32_t compute_crc32_be( uint32_t seed, uint8_t * data, size_t length){79 size_t 78 uint32_t compute_crc32_be( uint32_t seed, uint8_t * data, size_t length ){ 79 size_t index; 80 80 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 ); 86 86 }else{ 87 87 seed <<= 1; … … 91 91 length -= 8; 92 92 } 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 ); 98 98 }else{ 99 99 seed <<= 1; … … 105 105 } 106 106 107 uint32_t compute_checksum( uint32_t seed, uint8_t * data, size_t length){108 size_t 107 uint32_t compute_checksum( uint32_t seed, uint8_t * data, size_t length ){ 108 size_t index; 109 109 110 110 // 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 ]; 113 113 } 114 114 115 115 // 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; 118 118 } 119 119 … … 121 121 } 122 122 123 uint16_t compact_checksum( uint32_t sum){123 uint16_t compact_checksum( uint32_t sum ){ 124 124 // shorten to the 16 bits 125 while(sum >> 16){ 126 sum = (sum &0xFFFF) + (sum >> 16); 127 } 125 while( sum >> 16 ) sum = ( sum & 0xFFFF ) + ( sum >> 16 ); 128 126 129 return ( uint16_t) sum;127 return ( uint16_t ) sum; 130 128 } 131 129 132 uint16_t flip_checksum( uint16_t checksum){130 uint16_t flip_checksum( uint16_t checksum ){ 133 131 // flip, zero is returned as 0xFFFF (not flipped) 134 132 checksum = ~ checksum; … … 136 134 } 137 135 138 uint16_t ip_checksum( uint8_t * data, size_t length){139 return flip_checksum( compact_checksum(compute_checksum(0, data, length)));136 uint16_t ip_checksum( uint8_t * data, size_t length ){ 137 return flip_checksum( compact_checksum( compute_checksum( 0, data, length ))); 140 138 } 141 139
Note:
See TracChangeset
for help on using the changeset viewer.