mirror of
https://github.com/radareorg/radare2.git
synced 2024-12-04 11:43:39 +00:00
Cleanup endianness in crc32 and xxhash
This commit is contained in:
parent
5e1ad580db
commit
9cb03bab32
@ -38,17 +38,7 @@ R_API int r_hash_calculate(RHash *ctx, ut64 algobit, const ut8 *buf, int len) {
|
||||
if (algobit & R_HASH_CRC32) {
|
||||
ut8 *pres;
|
||||
ut32 res = r_hash_crc32 (buf, len);
|
||||
#if CPU_ENDIAN
|
||||
/* big endian here */
|
||||
memcpy (ctx->digest, &res, R_HASH_SIZE_CRC32);
|
||||
#else
|
||||
/* little endian here */
|
||||
pres = (ut8 *) &res;
|
||||
ctx->digest[0] = pres[3];
|
||||
ctx->digest[1] = pres[2];
|
||||
ctx->digest[2] = pres[1];
|
||||
ctx->digest[3] = pres[0];
|
||||
#endif
|
||||
return R_HASH_SIZE_CRC32;
|
||||
}
|
||||
if (algobit & R_HASH_XXHASH) {
|
||||
|
@ -1,13 +1,14 @@
|
||||
/* Copyright (C) radare2 - 2007-2012 - pancake */
|
||||
|
||||
#include "r_types.h"
|
||||
#include <r_types.h>
|
||||
#include <r_util.h>
|
||||
|
||||
static char crc_table_is_init = 0;
|
||||
static ut32 crc_table[256];
|
||||
|
||||
// result is endian swap
|
||||
R_API ut32 r_hash_crc32(const ut8 *buf, ut64 len) {
|
||||
unsigned int crc = 0;
|
||||
ut32 crc = 0;
|
||||
ut8 tmp[sizeof (ut32)];
|
||||
if (!crc_table_is_init) {
|
||||
ut32 i, j, h = 1;
|
||||
crc_table_is_init = 1;
|
||||
@ -21,5 +22,11 @@ R_API ut32 r_hash_crc32(const ut8 *buf, ut64 len) {
|
||||
crc ^= 0xffffffff;
|
||||
while (len--)
|
||||
crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
|
||||
return crc ^ 0xffffffff;
|
||||
|
||||
crc ^= 0xffffffff;
|
||||
|
||||
// unswap endian
|
||||
r_write_le32 (tmp, crc);
|
||||
crc = r_read_be32 (tmp);
|
||||
return crc;
|
||||
}
|
||||
|
@ -30,22 +30,6 @@
|
||||
- xxHash source repository : http://code.google.com/p/xxhash/
|
||||
*/
|
||||
|
||||
|
||||
|
||||
//**************************************
|
||||
// Tuning parameters
|
||||
//**************************************
|
||||
// FORCE_NATIVE_FORMAT :
|
||||
// By default, xxHash library provides endian-independent Hash values.
|
||||
// Results are therefore identical for big-endian and little-endian CPU.
|
||||
// This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format.
|
||||
// Should endian-independance be of no importance to your application, you may uncomment the #define below
|
||||
// It will improve speed for Big-endian CPU.
|
||||
// This option has no impact on Little_Endian CPU.
|
||||
//#define FORCE_NATIVE_FORMAT 1
|
||||
|
||||
|
||||
|
||||
//**************************************
|
||||
// Includes
|
||||
//**************************************
|
||||
@ -60,36 +44,6 @@ R_API ut32 r_hash_xxhash(const ut8 *buf, ut64 len) {
|
||||
return XXH32_result (s);
|
||||
}
|
||||
|
||||
|
||||
//**************************************
|
||||
// CPU Feature Detection
|
||||
//**************************************
|
||||
// Little Endian or Big Endian ?
|
||||
// You can overwrite the #define below if you know your architecture endianess
|
||||
#if defined(FORCE_NATIVE_FORMAT) && (FORCE_NATIVE_FORMAT==1)
|
||||
// Force native format. The result will be endian dependant.
|
||||
# define XXH_BIG_ENDIAN 0
|
||||
#elif defined (__GLIBC__)
|
||||
# include <endian.h>
|
||||
# if (__BYTE_ORDER == __BIG_ENDIAN)
|
||||
# define XXH_BIG_ENDIAN 1
|
||||
# endif
|
||||
#elif (defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN)) && !(defined(__LITTLE_ENDIAN__) || defined(__LITTLE_ENDIAN) || defined(_LITTLE_ENDIAN))
|
||||
# define XXH_BIG_ENDIAN 1
|
||||
#elif defined(__sparc) || defined(__sparc__) \
|
||||
|| defined(__ppc__) || defined(_POWER) || defined(__POWERPC__) || defined(_ARCH_PPC) || defined(__PPC__) || defined(__PPC) || defined(PPC) || defined(__powerpc__) || defined(__powerpc) || defined(powerpc) \
|
||||
|| defined(__hpux) || defined(__hppa) \
|
||||
|| defined(_MIPSEB) || defined(__s390__)
|
||||
# define XXH_BIG_ENDIAN 1
|
||||
#endif
|
||||
|
||||
#if !defined(XXH_BIG_ENDIAN)
|
||||
// Little Endian assumed. PDP Endian and other very rare endian format are unsupported.
|
||||
# define XXH_BIG_ENDIAN 0
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
//**************************************
|
||||
// Compiler-specific Options & Functions
|
||||
//**************************************
|
||||
@ -102,19 +56,6 @@ R_API ut32 r_hash_xxhash(const ut8 *buf, ut64 len) {
|
||||
# define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r)))
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) // Visual Studio
|
||||
# define XXH_swap32 _byteswap_ulong
|
||||
#elif GCC_VERSION >= 403
|
||||
# define XXH_swap32 __builtin_bswap32
|
||||
#else
|
||||
static inline unsigned int XXH_swap32 (unsigned int x) {
|
||||
return ((x << 24) & 0xff000000 ) |
|
||||
((x << 8) & 0x00ff0000 ) |
|
||||
((x >> 8) & 0x0000ff00 ) |
|
||||
((x >> 24) & 0x000000ff );
|
||||
}
|
||||
#endif
|
||||
|
||||
//**************************************
|
||||
// Constants
|
||||
//**************************************
|
||||
@ -127,20 +68,13 @@ static inline unsigned int XXH_swap32 (unsigned int x) {
|
||||
//**************************************
|
||||
// Macros
|
||||
//**************************************
|
||||
#define XXH_LE32(p) (XXH_BIG_ENDIAN ? XXH_swap32(*(unsigned int*)(p)) : *(unsigned int*)(p))
|
||||
#define XXH_LE32(p) r_read_le32(p)
|
||||
|
||||
//****************************
|
||||
// Simple Hash Functions
|
||||
//****************************
|
||||
|
||||
unsigned int XXH32(const void* input, int len, unsigned int seed) {
|
||||
#if 0
|
||||
// Simple version, good for code maintenance, but unfortunately slow for small inputs
|
||||
void* state = XXH32_init(seed);
|
||||
XXH32_feed(state, input, len);
|
||||
return XXH32_result(state);
|
||||
#else
|
||||
|
||||
const unsigned char* p = (const unsigned char*)input;
|
||||
const unsigned char* const bEnd = p + len;
|
||||
unsigned int h32;
|
||||
@ -185,7 +119,6 @@ unsigned int XXH32(const void* input, int len, unsigned int seed) {
|
||||
h32 ^= h32 >> 16;
|
||||
|
||||
return h32;
|
||||
#endif
|
||||
}
|
||||
|
||||
//****************************
|
||||
|
Loading…
Reference in New Issue
Block a user