mirror of
https://github.com/radareorg/radare2.git
synced 2025-02-14 02:51:12 +00:00
![pancake](/assets/img/avatar_default.png)
$ git grep ' $' | grep -v ^test/ | cut -d : -f 1 | sort -u > /tmp/trailing $ for a in `cat /tmp/trailing` ; do sed -i -e 's/ *$//' $a ; done
33 lines
692 B
C
33 lines
692 B
C
/*
|
|
* This code was done
|
|
* by an anonymous gnome
|
|
* ------------------------
|
|
* That's pure mathematics, so no sense to adding license shit here.
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include <math.h>
|
|
#include "r_types.h"
|
|
|
|
R_API double r_hash_entropy(const ut8 *data, ut64 size) {
|
|
if (!data || !size) {
|
|
return 0;
|
|
}
|
|
ut64 i, count[256] = {0};
|
|
double h = 0;
|
|
for (i = 0; i < size; i++) {
|
|
count[data[i]]++;
|
|
}
|
|
for (i = 0; i < 256; i++) {
|
|
if (count[i]) {
|
|
double p = (double) count[i] / size;
|
|
h -= p * log2 (p);
|
|
}
|
|
}
|
|
return h;
|
|
}
|
|
R_API double r_hash_entropy_fraction(const ut8 *data, ut64 size) {
|
|
return size ? r_hash_entropy (data, size) / \
|
|
log2 ((double) R_MIN (size, 256)) : 0;
|
|
}
|