-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
45 lines (42 loc) · 980 Bytes
/
hash.c
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
// Hashing function
// (C) 2016-2017 Ronsor.
#include <inttypes.h>
uint32_t rc_crc32(uint32_t crc, unsigned char *buf, size_t len)
{
static uint32_t table[256];
static int have_table = 0;
uint32_t rem;
uint8_t octet;
int i, j;
const char *p, *q;
/* This check is not thread safe; there is no mutex. */
if (have_table == 0) {
/* Calculate CRC table. */
for (i = 0; i < 256; i++) {
rem = i; /* remainder from polynomial division */
for (j = 0; j < 8; j++) {
if (rem & 1) {
rem >>= 1;
rem ^= 0xedb88320;
} else
rem >>= 1;
}
table[i] = rem;
}
have_table = 1;
}
crc = ~crc;
q = buf + len;
for (p = buf; p < q; p++) {
octet = *p; /* Cast to unsigned octet. */
crc = (crc >> 8) ^ table[(crc & 0xff) ^ octet];
}
return ~crc;
}
char* str_crc32(unsigned char* data, int length){
uint32_t ret = rc_crc32(0, data, length);
char *res = malloc(32);
//printf("%" PRIX32, ret);
sprintf(res, "%" PRIX32, ret);
return res;
}