Changes in uspace/lib/net/generic/net_checksum.c [ccca251:1bfd3d3] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/net/generic/net_checksum.c
rccca251 r1bfd3d3 52 52 uint16_t compact_checksum(uint32_t sum) 53 53 { 54 / * Shorten to the 16 bits */54 // 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 fields */74 // 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 padding */78 // 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 bytes */96 // process full bytes 97 97 while (length >= 8) { 98 / * Add the data */98 // add the data 99 99 seed ^= (*data) << 24; 100 100 101 / * For each added bit */101 // for each added bit 102 102 for (index = 0; index < 8; ++index) { 103 / * If the first bit is set */103 // if the first bit is set 104 104 if (seed & 0x80000000) { 105 / * Shift and divide the checksum */105 // shift and divide the checksum 106 106 seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE); 107 107 } else { 108 / * Shift otherwise */108 // shift otherwise 109 109 seed <<= 1; 110 110 } 111 111 } 112 112 113 / * Move to the next byte */113 // move to the next byte 114 114 ++data; 115 115 length -= 8; 116 116 } 117 117 118 / * Process the odd bits */118 // process the odd bits 119 119 if (length > 0) { 120 / * Add the data with zero padding */120 // add the data with zero padding 121 121 seed ^= ((*data) & (0xff << (8 - length))) << 24; 122 122 123 / * For each added bit */123 // for each added bit 124 124 for (index = 0; index < length; ++index) { 125 / * If the first bit is set */125 // if the first bit is set 126 126 if (seed & 0x80000000) { 127 / * Shift and divide the checksum */127 // shift and divide the checksum 128 128 seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE); 129 129 } else { 130 / * Shift otherwise */130 // shift otherwise 131 131 seed <<= 1; 132 132 } … … 148 148 size_t index; 149 149 150 / * Process full bytes */150 // process full bytes 151 151 while (length >= 8) { 152 / * Add the data */152 // add the data 153 153 seed ^= (*data); 154 154 155 / * For each added bit */155 // for each added bit 156 156 for (index = 0; index < 8; ++index) { 157 / * If the last bit is set */157 // if the last bit is set 158 158 if (seed & 1) { 159 / * Shift and divide the checksum */159 // shift and divide the checksum 160 160 seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE); 161 161 } else { 162 / * Shift otherwise */162 // shift otherwise 163 163 seed >>= 1; 164 164 } 165 165 } 166 166 167 / * Move to the next byte */167 // move to the next byte 168 168 ++data; 169 169 length -= 8; 170 170 } 171 171 172 / * Process the odd bits */172 // process the odd bits 173 173 if (length > 0) { 174 / * Add the data with zero padding */174 // 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 set */178 // if the last bit is set 179 179 if (seed & 1) { 180 / * Shift and divide the checksum */180 // shift and divide the checksum 181 181 seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE); 182 182 } else { 183 / * Shift otherwise */183 // 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 checksum */218 // 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.