radare2/libr/hash/hamdist.c
pancake f2563a7509 * Export 'srwx' perms of sections in rabin2 -rS
- Handled by 'S' command
* Added dummy 'z' command to handle zignaturez
  - Added more dummy 'az' commands
  - RCore now depends on RSign
* Some refactoring and speedup in _update method of RSearch
  - Added support for distance search (maybe buggy and incomplete atm)
  - Fix binary mask for keywords after previous commit
* Added 'r_str_rwx*' helper functions in r_util
2010-04-08 12:29:47 +02:00

53 lines
1.4 KiB
C

/*
* Copyright (C) 2007-2010
* pancake <youterm.com>
*
* radare is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* radare is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with radare; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#if 0
From Wikipedia, the free encyclopedia
In information theory, the Hamming distance between two strings of equal
length is the number of positions for which the corresponding symbols
are different. Put another way, it measures the minimum number of
substitutions required to change one into the other, or the number of
errors that transformed one string into the other.
#endif
#include "r_types.h"
static int hamdist(int x, int y) {
int dist = 0, val = x^y;
while (val) {
++dist;
val &= val - 1;
}
return dist;
}
R_API ut8 r_hash_hamdist(const ut8 *buf, int len) {
int i, x, y;
x = y = i = 0;
for (i=0; i<len; i++) {
y = buf[i];
x = hamdist (x, y);
}
return x;
}