aboutsummaryrefslogtreecommitdiff
path: root/src/inc/stdalg.c
blob: 99558a95b0f8a0272ce5bd729b9d34e3b41e659e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "stdalg.h"

#ifdef STDALGO_CRC7
  unsigned char crc7(unsigned char* bytes, unsigned char size)
  {
    register unsigned char val = 0; // last cycles value
    register unsigned char cb;      // current byte.

    unsigned char i;
    for(i=0; i<=size; i++)
    {
      cb = bytes[i];

      int q = (i==size) ? 7 : 8; // counter
      do
      {
        val <<= 1;
        if(cb & 0x80)         // check MSB of current byte
          ++val;              // overflow, add 1

        if(val & 0x80)        // check MSB of (last) seed
          val ^= 0x9;         // polynome
        cb     <<= 1;
      } while(--q);
      val &= 0x7f;            // lower 7 bits
    }
    return ((val<<1) + 1);
  }
#endif
..