Changeset e50cd7f in mainline for uspace/lib/net/generic/net_checksum.c
- Timestamp:
- 2011-04-17T19:17:55Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 63517c2, cfbbe1d3
- Parents:
- ef354b6 (diff), 8595577b (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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/net/generic/net_checksum.c
ref354b6 re50cd7f 52 52 uint16_t compact_checksum(uint32_t sum) 53 53 { 54 / / shorten to the 16 bits54 /* Shorten to the 16 bits */ 55 55 while (sum >> 16) 56 56 sum = (sum & 0xffff) + (sum >> 16); … … 72 72 size_t index; 73 73 74 / / sum all the 16 bit fields74 /* Sum all the 16 bit fields */ 75 75 for (index = 0; index + 1 < length; index += 2) 76 76 seed += (data[index] << 8) + data[index + 1]; 77 77 78 / / last odd byte with zero padding78 /* Last odd byte with zero padding */ 79 79 if (index + 1 == length) 80 80 seed += data[index] << 8; … … 94 94 size_t index; 95 95 96 / / process full bytes96 /* Process full bytes */ 97 97 while (length >= 8) { 98 / / add the data98 /* Add the data */ 99 99 seed ^= (*data) << 24; 100 100 101 / / for each added bit101 /* For each added bit */ 102 102 for (index = 0; index < 8; ++index) { 103 / / if the first bit is set103 /* If the first bit is set */ 104 104 if (seed & 0x80000000) { 105 / / shift and divide the checksum105 /* Shift and divide the checksum */ 106 106 seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE); 107 107 } else { 108 / / shift otherwise108 /* Shift otherwise */ 109 109 seed <<= 1; 110 110 } 111 111 } 112 112 113 / / move to the next byte113 /* Move to the next byte */ 114 114 ++data; 115 115 length -= 8; 116 116 } 117 117 118 / / process the odd bits118 /* Process the odd bits */ 119 119 if (length > 0) { 120 / / add the data with zero padding120 /* Add the data with zero padding */ 121 121 seed ^= ((*data) & (0xff << (8 - length))) << 24; 122 122 123 / / for each added bit123 /* For each added bit */ 124 124 for (index = 0; index < length; ++index) { 125 / / if the first bit is set125 /* If the first bit is set */ 126 126 if (seed & 0x80000000) { 127 / / shift and divide the checksum127 /* Shift and divide the checksum */ 128 128 seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE); 129 129 } else { 130 / / shift otherwise130 /* Shift otherwise */ 131 131 seed <<= 1; 132 132 } … … 148 148 size_t index; 149 149 150 / / process full bytes150 /* Process full bytes */ 151 151 while (length >= 8) { 152 / / add the data152 /* Add the data */ 153 153 seed ^= (*data); 154 154 155 / / for each added bit155 /* For each added bit */ 156 156 for (index = 0; index < 8; ++index) { 157 / / if the last bit is set157 /* If the last bit is set */ 158 158 if (seed & 1) { 159 / / shift and divide the checksum159 /* Shift and divide the checksum */ 160 160 seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE); 161 161 } else { 162 / / shift otherwise162 /* Shift otherwise */ 163 163 seed >>= 1; 164 164 } 165 165 } 166 166 167 / / move to the next byte167 /* Move to the next byte */ 168 168 ++data; 169 169 length -= 8; 170 170 } 171 171 172 / / process the odd bits172 /* Process the odd bits */ 173 173 if (length > 0) { 174 / / add the data with zero padding174 /* Add the data with zero padding */ 175 175 seed ^= (*data) >> (8 - length); 176 176 177 177 for (index = 0; index < length; ++index) { 178 / / if the last bit is set178 /* If the last bit is set */ 179 179 if (seed & 1) { 180 / / shift and divide the checksum180 /* Shift and divide the checksum */ 181 181 seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE); 182 182 } else { 183 / / shift otherwise183 /* Shift otherwise */ 184 184 seed >>= 1; 185 185 } … … 198 198 uint16_t flip_checksum(uint16_t checksum) 199 199 { 200 / / flip, zero is returned as 0xFFFF (not flipped)200 /* Flip, zero is returned as 0xFFFF (not flipped) */ 201 201 checksum = ~checksum; 202 202 return checksum ? checksum : IP_CHECKSUM_ZERO; … … 216 216 uint16_t ip_checksum(uint8_t *data, size_t length) 217 217 { 218 / / compute, compact and flip the data checksum218 /* Compute, compact and flip the data checksum */ 219 219 return flip_checksum(compact_checksum(compute_checksum(0, data, 220 220 length)));
Note:
See TracChangeset
for help on using the changeset viewer.