| // For a fully compiled version look at https://gist.github.com/danlark1/69c9d5e1d356eaa32dbdfccb2e19f401 | |
| #include <stdint.h> | |
| #include <stddef.h> | |
| #include <stdio.h> | |
| #define FNV32INIT 2166136261U | |
| #define FNV32PRIME 16777619U | |
| #define FNV64INIT 14695981039346656037ULL | |
| #define FNV64PRIME 1099511628211ULL | |
| // Any table | |
| extern unsigned long crc64table[256]; | |
| unsigned long fnv64(const void *p, size_t len) { | |
| unsigned long init = FNV64INIT; | |
| const unsigned char *_p = (const unsigned char *)p; | |
| for (size_t i = 0; i < len; ++i) { | |
| init = (init * FNV64PRIME) ^ _p[i]; | |
| } | |
| return init; | |
| } | |
| unsigned long crc64(unsigned long crc, const void *p, size_t len) { | |
| size_t i, t; | |
| const unsigned char *_p = (const unsigned char *)p; | |
| for (i = 0; i < len; i++) { | |
| t = ((crc >> 56) ^ (*_p++)) & 0xFF; | |
| crc = crc64table[t] ^ (crc << 8); | |
| } | |
| return crc; | |
| } | |
| int main() { | |
| const char* data1 = "l.im/8ca"; | |
| const char* data2 = "l.im/8cf"; | |
| size_t size = 8; | |
| unsigned long crc = crc64(fnv64(data1, size), data1, size); | |
| printf("%lu\n", crc); // 6193082687915471267 | |
| crc = crc64(fnv64(data2, size), data2, size); | |
| printf("%lu\n", crc); // 6193082687915471267 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment