SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
crc.h
Go to the documentation of this file.
1 // This file is part of LeanSDR Copyright (C) 2016-2018 <pabr@pabr.org>.
2 // See the toplevel README for more information.
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef LEANSDR_CRC_H
18 #define LEANSDR_CRC_H
19 
20 #include <stdint.h>
21 
22 #include "leansdr/discrmath.h"
23 
24 namespace leansdr
25 {
26 
27 // EN 302 307-1 section 5.1.4 CRC-8 encoder
28 
30 {
32  {
33  // Precompute
34  // EN 302 307-1 5.1.4 Figure 2
36  for (int u = 0; u < 256; ++u)
37  {
38  uint8_t u8 = u;
39  bitvect<uint8_t, 8> c = shiftdivmod(&u8, 1, g);
40  table[u] = c.v[0];
41  }
42  }
43  uint8_t compute(const uint8_t *buf, int len)
44  {
45  uint8_t c = 0;
46  for (; len--; ++buf)
47  c = table[c ^ *buf];
48  return c;
49  }
50 
51  private:
52  static const uint8_t POLY_DVBS2_CRC8 = 0xd5; // (1)11010101
53  uint8_t table[256];
54 };
55 
56 // CRC-32 ITU V.42 for FINGERPRINT
57 
58 uint32_t crc32(const uint8_t *buf, int len)
59 {
60  static const uint32_t poly = 0xedb88320;
61  uint32_t c = 0xffffffff;
62  for (int i = 0; i < len; ++i)
63  {
64  c ^= buf[i];
65  for (int bit = 8; bit--;)
66  c = (c & 1) ? (c >> 1) ^ poly : (c >> 1);
67  }
68  return c ^ 0xffffffff;
69 }
70 
71 } // namespace leansdr
72 
73 #endif // LEANSDR_CRC_H
unsigned char u8
Definition: framework.h:453
unsigned int uint32_t
Definition: rtptypes_win.h:46
unsigned char uint8_t
Definition: rtptypes_win.h:42
int32_t i
Definition: decimators.h:244
uint8_t table[256]
Definition: crc.h:53
uint32_t crc32(const uint8_t *buf, int len)
Definition: crc.h:58
uint8_t compute(const uint8_t *buf, int len)
Definition: crc.h:43
static const uint8_t POLY_DVBS2_CRC8
Definition: crc.h:52
bitvect< T, N > shiftdivmod(const Tm *m, size_t nm, const bitvect< T, N > &p, T init=0)
Definition: discrmath.h:138