blob: a0f0c469c1c44354b8bd684508c94dd6e5d6b5bb (
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
/* Author: Jan Sucan */
#include "crc32q.h"
#include <stdbool.h>
#define CRC32_TABLE_SIZE 256
#define CRC32_GENERATING_POLYNOME 0x814141ab
static void crc32q_create_table(uint32_t * const tab, uint32_t gen_polynome);
void
crc32q_create_table(uint32_t * const tab, uint32_t gen_polynome)
{
int i, j;
uint32_t c;
uint32_t msbMask = 1U << ((sizeof(c) * 8) - 1);
for (i = 0; i < CRC32_TABLE_SIZE; i++) {
c = i << 24;
for (j = 0; j < 8; j++) {
if (c & msbMask) {
c = (c << 1) ^ gen_polynome;
} else {
c = c << 1;
}
}
tab[i] = c;
}
}
static uint32_t crc32_tab[CRC32_TABLE_SIZE];
static bool crc32q_table_created = false;
void
crc32q_init(void)
{
crc32q_create_table(crc32_tab, CRC32_GENERATING_POLYNOME);
crc32q_table_created = true;
}
uint32_t
crc32q(const void * const buf, size_t size)
{
// Vytvorenie tabulky pred prvym pouzitim
if (!crc32q_table_created) {
crc32q_create_table(crc32_tab, CRC32_GENERATING_POLYNOME);
crc32q_table_created = true;
}
// Vypocet CRC-32
const uint8_t *p;
p = buf;
uint32_t crc = 0U; // Inicializacna CRC hodnota
while (size--) {
crc = (crc << 8) ^ crc32_tab[(crc >> 24) ^ *p++];
}
return crc ^ 0U;
}
|