Changes in uspace/lib/net/generic/net_checksum.c [8d601db:ccca251] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/net/generic/net_checksum.c
r8d601db rccca251 48 48 * 49 49 * @param[in] sum Computed checksum. 50 * @return sCompacted computed checksum to the 16 bits.50 * @return Compacted computed checksum to the 16 bits. 51 51 */ 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); … … 66 66 * @param[in] data Pointer to the beginning of data to process. 67 67 * @param[in] length Length of the data in bytes. 68 * @return sThe computed checksum of the length bytes of the data.68 * @return The computed checksum of the length bytes of the data. 69 69 */ 70 70 uint32_t compute_checksum(uint32_t seed, uint8_t *data, size_t length) … … 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; … … 88 88 * @param[in] data Pointer to the beginning of data to process. 89 89 * @param[in] length Length of the data in bits. 90 * @return sThe computed CRC32 of the length bits of the data.90 * @return The computed CRC32 of the length bits of the data. 91 91 */ 92 92 uint32_t compute_crc32_be(uint32_t seed, uint8_t * data, size_t length) … … 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 } … … 142 142 * @param[in] data Pointer to the beginning of data to process. 143 143 * @param[in] length Length of the data in bits. 144 * @return sThe computed CRC32 of the length bits of the data.144 * @return The computed CRC32 of the length bits of the data. 145 145 */ 146 146 uint32_t compute_crc32_le(uint32_t seed, uint8_t * data, size_t length) … … 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 } … … 193 193 * 194 194 * @param[in] checksum The computed checksum. 195 * @return sThe internet protocol header checksum.196 * @return s0xFFFF if the computed checksum is zero.195 * @return The internet protocol header checksum. 196 * @return 0xFFFF if the computed checksum is zero. 197 197 */ 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; … … 211 211 * @param[in] data The header data. 212 212 * @param[in] length The header length in bytes. 213 * @return sThe internet protocol header checksum.214 * @return s0xFFFF if the computed checksum is zero.213 * @return The internet protocol header checksum. 214 * @return 0xFFFF if the computed checksum is zero. 215 215 */ 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.