|
|
| version 1.1, 2012/02/21 17:26:12 | version 1.1.1.2, 2013/07/21 23:54:39 |
|---|---|
| Line 51 in_cksum(void *parg, int nbytes) | Line 51 in_cksum(void *parg, int nbytes) |
| /* To be consistent, offset is 0-based index, rather than the 1-based | /* To be consistent, offset is 0-based index, rather than the 1-based |
| index required in the specification ISO 8473, Annex C.1 */ | index required in the specification ISO 8473, Annex C.1 */ |
| /* calling with offset == FLETCHER_CHECKSUM_VALIDATE will validate the checksum | |
| without modifying the buffer; a valid checksum returns 0 */ | |
| u_int16_t | u_int16_t |
| fletcher_checksum(u_char * buffer, const size_t len, const uint16_t offset) | fletcher_checksum(u_char * buffer, const size_t len, const uint16_t offset) |
| { | { |
| Line 62 fletcher_checksum(u_char * buffer, const size_t len, c | Line 64 fletcher_checksum(u_char * buffer, const size_t len, c |
| checksum = 0; | checksum = 0; |
| assert (offset < len); | |
| /* | if (offset != FLETCHER_CHECKSUM_VALIDATE) |
| * Zero the csum in the packet. | /* Zero the csum in the packet. */ |
| */ | { |
| csum = (u_int16_t *) (buffer + offset); | assert (offset < (len - 1)); /* account for two bytes of checksum */ |
| *(csum) = 0; | csum = (u_int16_t *) (buffer + offset); |
| *(csum) = 0; | |
| } | |
| p = buffer; | p = buffer; |
| c0 = 0; | c0 = 0; |
| Line 89 fletcher_checksum(u_char * buffer, const size_t len, c | Line 92 fletcher_checksum(u_char * buffer, const size_t len, c |
| left -= partial_len; | left -= partial_len; |
| } | } |
| /* The cast is important, to ensure the mod is taken as a signed value. */ | /* The cast is important, to ensure the mod is taken as a signed value. */ |
| x = (int)((len - offset - 1) * c0 - c1) % 255; | x = (int)((len - offset - 1) * c0 - c1) % 255; |
| Line 98 fletcher_checksum(u_char * buffer, const size_t len, c | Line 101 fletcher_checksum(u_char * buffer, const size_t len, c |
| y = 510 - c0 - x; | y = 510 - c0 - x; |
| if (y > 255) | if (y > 255) |
| y -= 255; | y -= 255; |
| /* | |
| * Now we write this to the packet. | |
| * We could skip this step too, since the checksum returned would | |
| * be stored into the checksum field by the caller. | |
| */ | |
| buffer[offset] = x; | |
| buffer[offset + 1] = y; | |
| /* Take care of the endian issue */ | if (offset == FLETCHER_CHECKSUM_VALIDATE) |
| checksum = htons((x << 8) | (y & 0xFF)); | { |
| checksum = (c1 << 8) + c0; | |
| } | |
| else | |
| { | |
| /* | |
| * Now we write this to the packet. | |
| * We could skip this step too, since the checksum returned would | |
| * be stored into the checksum field by the caller. | |
| */ | |
| buffer[offset] = x; | |
| buffer[offset + 1] = y; | |
| /* Take care of the endian issue */ | |
| checksum = htons((x << 8) | (y & 0xFF)); | |
| } | |
| return checksum; | return checksum; |
| } | } |