Changeset 18188 for trunk/FACT++


Ignore:
Timestamp:
06/01/15 11:52:48 (9 years ago)
Author:
tbretz
Message:
Added Fletcher16 checksum calculation.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/tools.h

    r15167 r18188  
    1818    std::vector<std::string> Split(const std::string &, const std::string &);
    1919    std::string Uncomment(const std::string &opt);
     20
     21    template<typename T>
     22        uint16_t Fletcher16(const T *t, size_t cnt)
     23    {
     24        const uint8_t *data = reinterpret_cast<const uint8_t*>(t);
     25
     26        size_t bytes = cnt*sizeof(T);
     27
     28        uint16_t sum1 = 0xff;
     29        uint16_t sum2 = 0xff;
     30
     31        while (bytes)
     32        {
     33            size_t tlen = bytes > 20 ? 20 : bytes;
     34            bytes -= tlen;
     35
     36            do {
     37                sum2 += sum1 += *data++;
     38            } while (--tlen);
     39
     40            sum1 = (sum1 & 0xff) + (sum1 >> 8);
     41            sum2 = (sum2 & 0xff) + (sum2 >> 8);
     42        }
     43
     44        // Second reduction step to reduce sums to 8 bits
     45        sum1 = (sum1 & 0xff) + (sum1 >> 8);
     46        sum2 = (sum2 & 0xff) + (sum2 >> 8);
     47
     48        return sum2 << 8 | sum1;
     49    }
     50
    2051}
    2152
Note: See TracChangeset for help on using the changeset viewer.