File: crc16.cc

package info (click to toggle)
gr-dab 0.5-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,280 kB
  • sloc: python: 14,976; cpp: 6,738; ansic: 547; makefile: 17; sh: 11
file content (21 lines) | stat: -rw-r--r-- 520 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <inttypes.h>
#include <stdio.h>
#include <iostream>

uint16_t crc16(const char *bytestream, int length, uint16_t generator, uint16_t initial_state) {
  uint16_t state;
  char byte;
  
  state = bytestream[0]*256+bytestream[1];
  state = ~state;
  for (int i=2; i<length; i++) {
    byte = bytestream[i];
    for (int j=7; j>=0; j--) {
      if (((state>>15)&1) == 1)
        state = (((state<<1) + ((byte>>j)&1)) ^ generator);
      else
        state = (state<<1) + ((byte>>j)&1);
    }
  }
  return ~state;
}