|
Fletcher's checksum is one of several types of checksum algorithms, which are relatively simple processes used by computers to check the integrity of data. The implementation of the Fletcher-32 is very similar to the Adler-32 algorithm but several differences should be noted. Fletcher wraps around at modulo 65535 while Adler wraps at the prime 65521. Fletcher adds upper bits (16-31) into its sum while Adler drops these bits. Fletcher-32 works on 16 bit data while Adler works on 8 bit data. It is designed to overcome some of the inadequacies of simply summing all the bytes as in the original checksum. Fletcher's checksum, unlike the original checksum, can detect the inserting/deleting of zero value bytes, the reordering of bytes, and the incrementing and decrementing of bytes in opposite directions. Fletcher's checksum is described in RFC 1146. You can also find information about generating (as well as verifying) such a checksum in Annex B of RFC 905. An optimized implementation in the C programming language operates as follows: uint32_t fletcher( uint16_t *data, size_t len ) { uint32_t sum1 = 0xffff, sum2 = 0xffff; while (len) { unsigned tlen = len > 360 ? 360 : len; len -= tlen; do { sum1 += *data++; sum2 += sum1; } while (--tlen); sum1 = (sum1 & 0xffff) + (sum1 >> 16); sum2 = (sum2 & 0xffff) + (sum2 >> 16); } /* Second reduction step to reduce sums to 16 bits */ sum1 = (sum1 & 0xffff) + (sum1 >> 16); sum2 = (sum2 & 0xffff) + (sum2 >> 16); return sum2 << 16 | sum1; } A few tricks, well-known to implementors of the IP checksum, are used here for efficiency:
void fletcher( uint8_t *checkA, uint8_t *checkB, uint8_t *data, size_t len ) { uint16_t sum1 = 0xff, sum2 = 0xff; while (len) { size_t tlen = len > 21 ? 21 : len; len -= tlen; do { sum1 += *data++; sum2 += sum1; } while (--tlen); sum1 = (sum1 & 0xff) + (sum1 >> 8); sum2 = (sum2 & 0xff) + (sum2 >> 8); } /* Second reduction step to reduce sums to 8 bits */ sum1 = (sum1 & 0xff) + (sum1 >> 8); sum2 = (sum2 & 0xff) + (sum2 >> 8); *checkA = (uint8_t)sum1; *checkB = (uint8_t)sum2; return; } References
External links
|
This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License.
Mercedes Car
This site monitored by SitePinger.net