2016-05-16 00:08:24 +00:00
|
|
|
/* Copyright (C) radare2 - 2007-2016 - pancake */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2016-05-09 06:56:48 +00:00
|
|
|
#include <r_types.h>
|
|
|
|
#include <r_util.h>
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2016-05-16 00:08:24 +00:00
|
|
|
static bool crc_table_is_init = false;
|
2009-07-08 11:49:55 +00:00
|
|
|
static ut32 crc_table[256];
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2012-11-29 03:07:59 +00:00
|
|
|
R_API ut32 r_hash_crc32(const ut8 *buf, ut64 len) {
|
2016-05-09 06:56:48 +00:00
|
|
|
ut32 crc = 0;
|
|
|
|
ut8 tmp[sizeof (ut32)];
|
2009-02-05 21:08:46 +00:00
|
|
|
if (!crc_table_is_init) {
|
2009-07-08 11:49:55 +00:00
|
|
|
ut32 i, j, h = 1;
|
2016-05-16 00:08:24 +00:00
|
|
|
crc_table_is_init = true;
|
2009-02-05 21:08:46 +00:00
|
|
|
crc_table[0] = 0;
|
|
|
|
for (i = 128; i; i >>= 1) {
|
|
|
|
h = (h >> 1) ^ ((h & 1) ? (int)0xedb88320 : 0);
|
|
|
|
for (j = 0; j < 256; j += 2*i)
|
|
|
|
crc_table[i+j] = crc_table[j] ^ h;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 00:08:24 +00:00
|
|
|
crc ^= UT32_MAX;
|
|
|
|
while (len--) {
|
2009-02-05 21:08:46 +00:00
|
|
|
crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
|
2016-05-16 00:08:24 +00:00
|
|
|
}
|
|
|
|
crc ^= UT32_MAX;
|
2016-05-09 06:56:48 +00:00
|
|
|
|
|
|
|
// unswap endian
|
|
|
|
r_write_le32 (tmp, crc);
|
2016-05-16 00:08:24 +00:00
|
|
|
return r_read_le32 (tmp);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|