From b701626cfcb642991f9850e295c4e8d09e9b1ef6 Mon Sep 17 00:00:00 2001 From: pancake Date: Tue, 10 Nov 2015 12:18:27 +0100 Subject: [PATCH] Fix some clang-analyzer issues --- libr/bin/dwarf.c | 45 ++- libr/bin/format/omf/omf.c | 32 +- libr/core/anal.c | 1 - libr/core/core.c | 8 - libr/hash/xxhash.c | 657 ++++++++++++++++++-------------------- libr/hash/xxhash.h | 256 +++++++-------- libr/include/r_core.h | 1 - 7 files changed, 476 insertions(+), 524 deletions(-) diff --git a/libr/bin/dwarf.c b/libr/bin/dwarf.c index 381360b0e4..151a9cbba9 100644 --- a/libr/bin/dwarf.c +++ b/libr/bin/dwarf.c @@ -246,14 +246,18 @@ static const ut8 *r_bin_dwarf_parse_lnp_header ( fprintf(f, " opcode_base: %d\n", hdr->opcode_base); } - hdr->std_opcode_lengths = calloc(sizeof(ut8), hdr->opcode_base); + if (hdr->opcode_base>0) { + hdr->std_opcode_lengths = calloc(sizeof(ut8), hdr->opcode_base); - for (i = 1; i <= hdr->opcode_base - 1; i++) { - if (buf+2>buf_end) break; - hdr->std_opcode_lengths[i] = READ (buf, ut8); - if (f) { - fprintf(f, " op %d %d\n", i, hdr->std_opcode_lengths[i]); + for (i = 1; i <= hdr->opcode_base - 1; i++) { + if (buf+2>buf_end) break; + hdr->std_opcode_lengths[i] = READ (buf, ut8); + if (f) { + fprintf(f, " op %d %d\n", i, hdr->std_opcode_lengths[i]); + } } + } else { + hdr->std_opcode_lengths = NULL; } i = 0; @@ -1146,41 +1150,37 @@ static const ut8 *r_bin_dwarf_parse_attr_value (const ut8 *obuf, int obuf_len, case DW_FORM_block2: value->encoding.block.length = READ (buf, ut16); - value->encoding.block.data = calloc(sizeof(ut8), - value->encoding.block.length); - - for (j = 0; j < value->encoding.block.length; j++) { - value->encoding.block.data[j] = READ (buf, ut8); + if (value->encoding.block.length>0) { + value->encoding.block.data = calloc(sizeof(ut8), + value->encoding.block.length); + for (j = 0; j < value->encoding.block.length; j++) { + value->encoding.block.data[j] = READ (buf, ut8); + } } break; - case DW_FORM_block4: value->encoding.block.length = READ (buf, ut32); - value->encoding.block.data = calloc(sizeof(ut8), - value->encoding.block.length); - - for (j = 0; j < value->encoding.block.length; j++) { - value->encoding.block.data[j] = READ (buf, ut8); + if (value->encoding.block.length>0) { + value->encoding.block.data = calloc(sizeof(ut8), + value->encoding.block.length); + for (j = 0; j < value->encoding.block.length; j++) { + value->encoding.block.data[j] = READ (buf, ut8); + } } break; - case DW_FORM_data2: value->encoding.data = READ (buf, ut16); break; - case DW_FORM_data4: value->encoding.data = READ (buf, ut32); break; - case DW_FORM_data8: value->encoding.data = READ (buf, ut64); break; - case DW_FORM_string: value->encoding.str_struct.string = strdup((const char*)buf); buf += (strlen((const char*)buf) + 1); break; - case DW_FORM_block: buf = r_uleb128 (buf, ST32_MAX, &value->encoding.block.length); @@ -1191,7 +1191,6 @@ static const ut8 *r_bin_dwarf_parse_attr_value (const ut8 *obuf, int obuf_len, value->encoding.block.data[j] = READ (buf, ut8); } break; - case DW_FORM_block1: value->encoding.block.length = READ (buf, ut8); diff --git a/libr/bin/format/omf/omf.c b/libr/bin/format/omf/omf.c index fb70f1ad7b..423044d098 100644 --- a/libr/bin/format/omf/omf.c +++ b/libr/bin/format/omf/omf.c @@ -520,27 +520,29 @@ static int get_omf_data_info(r_bin_omf_obj *obj) { static int get_omf_infos(r_bin_omf_obj *obj) { // get all name defined in lnames records obj->nb_name = count_omf_multi_record_type (obj, OMF_LNAMES); - if (!(obj->names = R_NEWS0 (char *, obj->nb_name))) - return false; - if (!cpy_omf_names (obj)) - return false; - + if (obj->nb_name>0) { + if (!(obj->names = R_NEWS0 (char *, obj->nb_name))) + return false; + if (!cpy_omf_names (obj)) + return false; + } // get all sections (segdef record) obj->nb_section = count_omf_record_type (obj, OMF_SEGDEF); - if (!(obj->sections = R_NEWS0 (OMF_segment *, obj->nb_section))) - return false; - get_omf_section_info (obj); - + if (obj->nb_section>0) { + if (!(obj->sections = R_NEWS0 (OMF_segment *, obj->nb_section))) + return false; + get_omf_section_info (obj); + } // get all data (ledata record) get_omf_data_info (obj); - // get all symbols (pubdef + lpubdef) obj->nb_symbol = count_omf_multi_record_type (obj, OMF_PUBDEF); - if (!(obj->symbols = R_NEWS0 (OMF_symbol *, obj->nb_symbol))) - return false; - if (!get_omf_symbol_info (obj)) - return false; - + if (obj->nb_symbol>0) { + if (!(obj->symbols = R_NEWS0 (OMF_symbol *, obj->nb_symbol))) + return false; + if (!get_omf_symbol_info (obj)) + return false; + } return true; } diff --git a/libr/core/anal.c b/libr/core/anal.c index f34da504f0..380497bc99 100644 --- a/libr/core/anal.c +++ b/libr/core/anal.c @@ -1774,7 +1774,6 @@ R_API int r_core_anal_data (RCore *core, ut64 addr, int count, int depth) { memset (buf, 0xff, len); r_io_read_at (core->io, addr, buf, len); buf[len-1] = 0; - //} for (i = j = 0; j=len) { diff --git a/libr/core/core.c b/libr/core/core.c index d7d60ee70b..9e2bf87177 100644 --- a/libr/core/core.c +++ b/libr/core/core.c @@ -160,13 +160,6 @@ R_API RCore *r_core_cast(void *p) { return (RCore*)p; } -/* -R_API void r_core_cmd_flush (RCore *core) { - // alias - r_cons_flush (); -} -*/ - static int core_cmd_callback (void *user, const char *cmd) { RCore *core = (RCore *)user; return r_core_cmd0 (core, cmd); @@ -684,7 +677,6 @@ openfile: if ((!strncmp (line->buffer.data, "s ", 2)) || (!strncmp (line->buffer.data, "ad ", 3)) || (!strncmp (line->buffer.data, "bf ", 3)) || - (!strncmp (line->buffer.data, "dcu ", 4)) || (!strncmp (line->buffer.data, "ag ", 3)) || (!strncmp (line->buffer.data, "afi ", 4)) || (!strncmp (line->buffer.data, "afb ", 4)) || diff --git a/libr/hash/xxhash.c b/libr/hash/xxhash.c index 293de40f44..379eba83f4 100644 --- a/libr/hash/xxhash.c +++ b/libr/hash/xxhash.c @@ -1,348 +1,309 @@ -/* - xxHash - Fast Hash algorithm - Copyright (C) 2012, Yann Collet. - BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following disclaimer - in the documentation and/or other materials provided with the - distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - You can contact the author at : - - 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 -//************************************** -#include // for malloc(), free() -#include // for memcpy() -#include "xxhash.h" -#include - -R_API ut32 r_hash_xxhash(const ut8 *buf, ut64 len) { - void *s = XXH32_init (0); - XXH32_feed (s, buf, (int)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 -# 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 -//************************************** -#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) - -// Note : under GCC, it may sometimes be faster to enable the (2nd) macro definition, instead of using win32 intrinsic -#if defined(_WIN32) -# define XXH_rotl32(x,r) _rotl(x,r) -#else -# 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 -//************************************** -#define PRIME32_1 2654435761U -#define PRIME32_2 2246822519U -#define PRIME32_3 3266489917U -#define PRIME32_4 668265263U -#define PRIME32_5 374761393U - - - -//************************************** -// Macros -//************************************** -#define XXH_LE32(p) (XXH_BIG_ENDIAN ? XXH_swap32(*(unsigned int*)(p)) : *(unsigned int*)(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; - - if (len>=16) - { - const unsigned char* const limit = bEnd - 16; - unsigned int v1 = seed + PRIME32_1 + PRIME32_2; - unsigned int v2 = seed + PRIME32_2; - unsigned int v3 = seed + 0; - unsigned int v4 = seed - PRIME32_1; - - do - { - v1 += XXH_LE32(p) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4; - v2 += XXH_LE32(p) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4; - v3 += XXH_LE32(p) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4; - v4 += XXH_LE32(p) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4; - } while (p<=limit) ; - - h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18); - } - else - { - h32 = seed + PRIME32_5; - } - - h32 += (unsigned int) len; - - while (p<=bEnd-4) - { - h32 += XXH_LE32(p) * PRIME32_3; - h32 = XXH_rotl32(h32, 17) * PRIME32_4 ; - p+=4; - } - - while (p> 15; - h32 *= PRIME32_2; - h32 ^= h32 >> 13; - h32 *= PRIME32_3; - h32 ^= h32 >> 16; - - return h32; - -#endif -} - - -//**************************** -// Advanced Hash Functions -//**************************** - -struct XXH_state32_t -{ - unsigned int seed; - unsigned int v1; - unsigned int v2; - unsigned int v3; - unsigned int v4; - unsigned long long total_len; - char memory[16]; - int memsize; -}; - - -void* XXH32_init (unsigned int seed) -{ - struct XXH_state32_t * state = (struct XXH_state32_t *) malloc ( sizeof(struct XXH_state32_t)); - state->seed = seed; - state->v1 = seed + PRIME32_1 + PRIME32_2; - state->v2 = seed + PRIME32_2; - state->v3 = seed + 0; - state->v4 = seed - PRIME32_1; - state->total_len = 0; - state->memsize = 0; - - return (void*)state; -} - - -int XXH32_feed (void* state_in, const void* input, int len) -{ - struct XXH_state32_t * state = state_in; - const unsigned char* p = (const unsigned char*)input; - const unsigned char* const bEnd = p + len; - - state->total_len += len; - - if (state->memsize + len < 16) // fill in tmp buffer - { - memcpy(state->memory + state->memsize, input, len); - state->memsize += len; - return 0; - } - - if (state->memsize) // some data left from previous feed - { - memcpy(state->memory + state->memsize, input, 16-state->memsize); - { - const unsigned int* p32 = (const unsigned int*)state->memory; - state->v1 += XXH_LE32(p32) * PRIME32_2; state->v1 = XXH_rotl32(state->v1, 13); state->v1 *= PRIME32_1; p32++; - state->v2 += XXH_LE32(p32) * PRIME32_2; state->v2 = XXH_rotl32(state->v2, 13); state->v2 *= PRIME32_1; p32++; - state->v3 += XXH_LE32(p32) * PRIME32_2; state->v3 = XXH_rotl32(state->v3, 13); state->v3 *= PRIME32_1; p32++; - state->v4 += XXH_LE32(p32) * PRIME32_2; state->v4 = XXH_rotl32(state->v4, 13); state->v4 *= PRIME32_1; p32++; - } - p += 16-state->memsize; - state->memsize = 0; - } - - { - const unsigned char* const limit = bEnd - 16; - unsigned int v1 = state->v1; - unsigned int v2 = state->v2; - unsigned int v3 = state->v3; - unsigned int v4 = state->v4; - - while (p<=limit) - { - v1 += XXH_LE32(p) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4; - v2 += XXH_LE32(p) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4; - v3 += XXH_LE32(p) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4; - v4 += XXH_LE32(p) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4; - } - - state->v1 = v1; - state->v2 = v2; - state->v3 = v3; - state->v4 = v4; - } - - if (p < bEnd) - { - memcpy(state->memory, p, bEnd-p); - state->memsize = bEnd-p; - } - - return 0; -} - - -unsigned int XXH32_getIntermediateResult (void* state_in) -{ - struct XXH_state32_t * state = state_in; - unsigned char * p = (unsigned char*)state->memory; - unsigned char* bEnd = (unsigned char*)state->memory + state->memsize; - unsigned int h32; - - - if (state->total_len >= 16) - { - h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18); - } - else - { - h32 = state->seed + PRIME32_5; - } - - h32 += (unsigned int) state->total_len; - - while (p<=bEnd-4) - { - h32 += XXH_LE32(p) * PRIME32_3; - h32 = XXH_rotl32(h32, 17) * PRIME32_4 ; - p+=4; - } - - while (p> 15; - h32 *= PRIME32_2; - h32 ^= h32 >> 13; - h32 *= PRIME32_3; - h32 ^= h32 >> 16; - - return h32; -} - - -unsigned int XXH32_result (void* state_in) -{ - unsigned int h32 = XXH32_getIntermediateResult(state_in); - - free(state_in); - - return h32; -} +/* + xxHash - Fast Hash algorithm + Copyright (C) 2012, Yann Collet. + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - 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 +//************************************** +#include // for malloc(), free() +#include // for memcpy() +#include "xxhash.h" +#include + +R_API ut32 r_hash_xxhash(const ut8 *buf, ut64 len) { + void *s = XXH32_init (0); + XXH32_feed (s, buf, (int)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 +# 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 +//************************************** +#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) + +// Note : under GCC, it may sometimes be faster to enable the (2nd) macro definition, instead of using win32 intrinsic +#if defined(_WIN32) +# define XXH_rotl32(x,r) _rotl(x,r) +#else +# 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 +//************************************** +#define PRIME32_1 2654435761U +#define PRIME32_2 2246822519U +#define PRIME32_3 3266489917U +#define PRIME32_4 668265263U +#define PRIME32_5 374761393U + +//************************************** +// Macros +//************************************** +#define XXH_LE32(p) (XXH_BIG_ENDIAN ? XXH_swap32(*(unsigned int*)(p)) : *(unsigned int*)(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; + + if (len>=16) { + const unsigned char* const limit = bEnd - 16; + unsigned int v1 = seed + PRIME32_1 + PRIME32_2; + unsigned int v2 = seed + PRIME32_2; + unsigned int v3 = seed + 0; + unsigned int v4 = seed - PRIME32_1; + + do { + v1 += XXH_LE32(p) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4; + v2 += XXH_LE32(p) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4; + v3 += XXH_LE32(p) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4; + v4 += XXH_LE32(p) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4; + } while (p<=limit) ; + + h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18); + } else { + h32 = seed + PRIME32_5; + } + + h32 += (unsigned int) len; + + while (p<=bEnd-4) { + h32 += XXH_LE32(p) * PRIME32_3; + h32 = XXH_rotl32(h32, 17) * PRIME32_4 ; + p+=4; + } + + while (p> 15; + h32 *= PRIME32_2; + h32 ^= h32 >> 13; + h32 *= PRIME32_3; + h32 ^= h32 >> 16; + + return h32; +#endif +} + +//**************************** +// Advanced Hash Functions +//**************************** + +struct XXH_state32_t { + unsigned int seed; + unsigned int v1; + unsigned int v2; + unsigned int v3; + unsigned int v4; + unsigned long long total_len; + char memory[16]; + int memsize; +}; + +void* XXH32_init (unsigned int seed) { + struct XXH_state32_t * state = (struct XXH_state32_t *) malloc ( sizeof(struct XXH_state32_t)); + state->seed = seed; + state->v1 = seed + PRIME32_1 + PRIME32_2; + state->v2 = seed + PRIME32_2; + state->v3 = seed + 0; + state->v4 = seed - PRIME32_1; + state->total_len = 0; + state->memsize = 0; + return (void*)state; +} + +int XXH32_feed (void* state_in, const void* input, int len) { + struct XXH_state32_t * state = state_in; + const unsigned char* p = (const unsigned char*)input; + const unsigned char* const bEnd = p + len; + + state->total_len += len; + // fill in tmp buffer + if (state->memsize + len < 16) { + memcpy(state->memory + state->memsize, input, len); + state->memsize += len; + return 0; + } + + // some data left from previous feed + if (state->memsize) { + memcpy(state->memory + state->memsize, input, 16-state->memsize); + { + const unsigned int* p32 = (const unsigned int*)state->memory; + state->v1 += XXH_LE32(p32) * PRIME32_2; state->v1 = XXH_rotl32(state->v1, 13); state->v1 *= PRIME32_1; p32++; + state->v2 += XXH_LE32(p32) * PRIME32_2; state->v2 = XXH_rotl32(state->v2, 13); state->v2 *= PRIME32_1; p32++; + state->v3 += XXH_LE32(p32) * PRIME32_2; state->v3 = XXH_rotl32(state->v3, 13); state->v3 *= PRIME32_1; p32++; + state->v4 += XXH_LE32(p32) * PRIME32_2; state->v4 = XXH_rotl32(state->v4, 13); state->v4 *= PRIME32_1; p32++; + } + p += 16-state->memsize; + state->memsize = 0; + } + + { + const unsigned char* const limit = bEnd - 16; + unsigned int v1 = state->v1; + unsigned int v2 = state->v2; + unsigned int v3 = state->v3; + unsigned int v4 = state->v4; + + while (p<=limit) { + v1 += XXH_LE32(p) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4; + v2 += XXH_LE32(p) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4; + v3 += XXH_LE32(p) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4; + v4 += XXH_LE32(p) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4; + } + state->v1 = v1; + state->v2 = v2; + state->v3 = v3; + state->v4 = v4; + } + + if (p < bEnd) { + state->memsize = R_MIN (bEnd - p, sizeof (state->memory)); + memcpy (state->memory, p, state->memsize); + } + return 0; +} + +unsigned int XXH32_getIntermediateResult (void* state_in) { + struct XXH_state32_t * state = state_in; + unsigned char * p = (unsigned char*)state->memory; + unsigned char* bEnd = (unsigned char*)state->memory + state->memsize; + unsigned int h32; + + + if (state->total_len >= 16) { + h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18); + } else { + h32 = state->seed + PRIME32_5; + } + + h32 += (unsigned int) state->total_len; + + while (p<=bEnd-4) { + h32 += XXH_LE32(p) * PRIME32_3; + h32 = XXH_rotl32(h32, 17) * PRIME32_4 ; + p += 4; + } + + while (p> 15; + h32 *= PRIME32_2; + h32 ^= h32 >> 13; + h32 *= PRIME32_3; + h32 ^= h32 >> 16; + return h32; +} + +unsigned int XXH32_result (void* state_in) { + unsigned int h32 = XXH32_getIntermediateResult(state_in); + free (state_in); + return h32; +} diff --git a/libr/hash/xxhash.h b/libr/hash/xxhash.h index d253fa0c43..93f40a5e4d 100644 --- a/libr/hash/xxhash.h +++ b/libr/hash/xxhash.h @@ -1,128 +1,128 @@ -/* - xxHash - Fast Hash algorithm - Header File - Copyright (C) 2012, Yann Collet. - BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following disclaimer - in the documentation and/or other materials provided with the - distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - You can contact the author at : - - xxHash source repository : http://code.google.com/p/xxhash/ -*/ - -/* Notice extracted from xxHash homepage : - -xxHash is an extremely fast Hash algorithm, running at RAM speed limits. -It also successfully passes all tests from the SMHasher suite. - -Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz) - -Name Speed Q.Score Author -xxHash 5.4 GB/s 10 -CrapWow 3.2 GB/s 2 Andrew -MumurHash 3a 2.7 GB/s 10 Austin Appleby -SpookyHash 2.0 GB/s 10 Bob Jenkins -SBox 1.4 GB/s 9 Bret Mulvey -Lookup3 1.2 GB/s 9 Bob Jenkins -SuperFastHash 1.2 GB/s 1 Paul Hsieh -CityHash64 1.05 GB/s 10 Pike & Alakuijala -FNV 0.55 GB/s 5 Fowler, Noll, Vo -CRC32 0.43 GB/s 9 -MD5-32 0.33 GB/s 10 Ronald L. Rivest -SHA1-32 0.28 GB/s 10 - -Q.Score is a measure of quality of the hash function. -It depends on successfully passing SMHasher test set. -10 is a perfect score. -*/ - -#pragma once - -#if defined (__cplusplus) -extern "C" { -#endif - - -//**************************** -// Simple Hash Functions -//**************************** - -unsigned int XXH32 (const void* input, int len, unsigned int seed); - -/* -XXH32() : - Calculate the 32-bits hash of "input", of length "len" - "seed" can be used to alter the result - This function successfully passes all SMHasher tests. - Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s - Note that "len" is type "int", which means it is limited to 2^31-1. - If your data is larger, use the advanced functions below. -*/ - - - -//**************************** -// Advanced Hash Functions -//**************************** - -void* XXH32_init (unsigned int seed); -int XXH32_feed (void* state, const void* input, int len); -unsigned int XXH32_result (void* state); - -/* -These functions calculate the xxhash of an input provided in several small packets, -as opposed to an input provided as a single block. - -You must start with : -void* XXH32_init() -The function returns a pointer which holds the state of calculation. - -This pointer must be provided as "void* state" parameter for XXH32_feed(). -XXH32_feed() can be called as many times as necessary. -The function returns an error code, with 0 meaning OK, and all other values meaning there is an error. -Note that "len" is type "int", which means it is limited to 2^31-1. -If your data is larger, it is recommended -to chunk your data into blocks of size 2^30 (1GB) to avoid any "int" overflow issue. - -Finally, you can end the calculation anytime, by using XXH32_result(). -This function returns the final 32-bits hash. -You must provide the same "void* state" parameter created by XXH32_init(). - -Memory will be freed by XXH32_result(). -*/ - - -unsigned int XXH32_getIntermediateResult (void* state); -/* -This function does the same as XXH32_result(), generating a 32-bit hash, -but preserve memory context. -This way, it becomes possible to generate intermediate hashes, and then continue feeding data with XXH32_feed(). -To free memory context, use XXH32_result(). -*/ - - - -#if defined (__cplusplus) -} -#endif +/* + xxHash - Fast Hash algorithm + Header File + Copyright (C) 2012, Yann Collet. + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - xxHash source repository : http://code.google.com/p/xxhash/ +*/ + +/* Notice extracted from xxHash homepage : + +xxHash is an extremely fast Hash algorithm, running at RAM speed limits. +It also successfully passes all tests from the SMHasher suite. + +Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz) + +Name Speed Q.Score Author +xxHash 5.4 GB/s 10 +CrapWow 3.2 GB/s 2 Andrew +MumurHash 3a 2.7 GB/s 10 Austin Appleby +SpookyHash 2.0 GB/s 10 Bob Jenkins +SBox 1.4 GB/s 9 Bret Mulvey +Lookup3 1.2 GB/s 9 Bob Jenkins +SuperFastHash 1.2 GB/s 1 Paul Hsieh +CityHash64 1.05 GB/s 10 Pike & Alakuijala +FNV 0.55 GB/s 5 Fowler, Noll, Vo +CRC32 0.43 GB/s 9 +MD5-32 0.33 GB/s 10 Ronald L. Rivest +SHA1-32 0.28 GB/s 10 + +Q.Score is a measure of quality of the hash function. +It depends on successfully passing SMHasher test set. +10 is a perfect score. +*/ + +#pragma once + +#if defined (__cplusplus) +extern "C" { +#endif + + +//**************************** +// Simple Hash Functions +//**************************** + +unsigned int XXH32 (const void* input, int len, unsigned int seed); + +/* +XXH32() : + Calculate the 32-bits hash of "input", of length "len" + "seed" can be used to alter the result + This function successfully passes all SMHasher tests. + Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s + Note that "len" is type "int", which means it is limited to 2^31-1. + If your data is larger, use the advanced functions below. +*/ + + + +//**************************** +// Advanced Hash Functions +//**************************** + +void* XXH32_init (unsigned int seed); +int XXH32_feed (void* state, const void* input, int len); +unsigned int XXH32_result (void* state); + +/* +These functions calculate the xxhash of an input provided in several small packets, +as opposed to an input provided as a single block. + +You must start with : +void* XXH32_init() +The function returns a pointer which holds the state of calculation. + +This pointer must be provided as "void* state" parameter for XXH32_feed(). +XXH32_feed() can be called as many times as necessary. +The function returns an error code, with 0 meaning OK, and all other values meaning there is an error. +Note that "len" is type "int", which means it is limited to 2^31-1. +If your data is larger, it is recommended +to chunk your data into blocks of size 2^30 (1GB) to avoid any "int" overflow issue. + +Finally, you can end the calculation anytime, by using XXH32_result(). +This function returns the final 32-bits hash. +You must provide the same "void* state" parameter created by XXH32_init(). + +Memory will be freed by XXH32_result(). +*/ + + +unsigned int XXH32_getIntermediateResult (void* state); +/* +This function does the same as XXH32_result(), generating a 32-bit hash, +but preserve memory context. +This way, it becomes possible to generate intermediate hashes, and then continue feeding data with XXH32_feed(). +To free memory context, use XXH32_result(). +*/ + + + +#if defined (__cplusplus) +} +#endif diff --git a/libr/include/r_core.h b/libr/include/r_core.h index 314c63d3d4..e2e823c365 100644 --- a/libr/include/r_core.h +++ b/libr/include/r_core.h @@ -196,7 +196,6 @@ R_API int r_core_fgets(char *buf, int len); R_API int r_core_cmdf(void *user, const char *fmt, ...); R_API int r_core_flush(void *user, const char *cmd); R_API int r_core_cmd0(void *user, const char *cmd); -R_API void r_core_cmd_flush (RCore *core); R_API void r_core_cmd_init(RCore *core); R_API int r_core_cmd_pipe(RCore *core, char *radare_cmd, char *shell_cmd); R_API char *r_core_cmd_str(RCore *core, const char *cmd);