X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Falgo%2Frotating_hash.h;fp=bertos%2Falgo%2Frotating_hash.h;h=c154af1d33a8b3f28d57acb7ca28d7b1d934e610;hb=791e167e053bdd9250d34a9a5ccae6ccde4d6679;hp=0000000000000000000000000000000000000000;hpb=faf2f6bfd5933ff75e6cc01e3d48f9277f731d8f;p=bertos.git diff --git a/bertos/algo/rotating_hash.h b/bertos/algo/rotating_hash.h new file mode 100644 index 00000000..c154af1d --- /dev/null +++ b/bertos/algo/rotating_hash.h @@ -0,0 +1,82 @@ +/** + * \file + * + * + * \brief Rotating Hash algorithm (interface). + * + * This is a simple yet powerfull checksum algorithm. + * Instead of just xor-ing the data, rotating hash + * circular shift the checksum 4 place left before xoring. + * This is a bit more stronger than simply sum the data. + * + * \version $Id$ + * + * \author Francesco Sacchi + */ + + +#ifndef ALGO_ROTATING_H +#define ALGO_ROTATING_H + +#include + +typedef uint16_t rotating_t; + + +/** + * Init rotating checksum. + */ +INLINE void rotating_init(rotating_t *rot) +{ + *rot = 0; +} + +/** + * Update checksum pointed by \c rot with \c c data. + */ +INLINE void rotating_update1(uint8_t c, rotating_t *rot) +{ + *rot = (*rot << 4) ^ (*rot >> 12) ^ c; +} + +/** + * Update checksum pointed by \c rot with data supplied in \c buf. + */ +INLINE void rotating_update(const void *_buf, size_t len, rotating_t *rot) +{ + const uint8_t *buf = (const uint8_t *)_buf; + + while (len--) + rotating_update1(*buf++, rot); +} + + +#endif // ALGO_ROTATING_H