I wrote:
> I think the feature can be added in libr_hash instead, which already
> uses <math.h>/libm, so no extra dependencies are needed. Patch coming
> when I have time to try out a few different methods and look at code
> duplication vs performance.
Here it is: Just a wrapper function that calculates the maximum and
divides by it. Could also be useful for eg. plotting a graph of entropy
vs. file offset.
I tried a few ways of avoiding the repeated calculation of log(2), but
there was < 1% performance gain, and it resulted in either less
portability or somehow setting it up at compile time/startup.
Glyn
diff -r c9dfcd92e312 libr/core/cmd.c
--- a/libr/core/cmd.c Wed Mar 02 14:11:34 2011 +0100
+++ b/libr/core/cmd.c Wed Mar 02 20:21:43 2011 +0000
@@ -48,7 +48,7 @@
ret++;
break;
case 'e': // entropy
- ret = (ut8) ((r_hash_entropy (bufz, size)*255)/8);
+ ret = (ut8) (r_hash_entropy_fraction (bufz, size)*255);
break;
case 'h': //head
default:
diff -r c9dfcd92e312 libr/hash/entropy.c
--- a/libr/hash/entropy.c Wed Mar 02 14:11:34 2011 +0100
+++ b/libr/hash/entropy.c Wed Mar 02 20:21:43 2011 +0000
@@ -29,3 +29,13 @@
}
return h;
}
+
+R_API double r_hash_entropy_fraction(const ut8 *data, ut64 size) {
+ double h = r_hash_entropy(data,size);
+
+ if (size>255) return h/8;
+ else {
+ double log2 = log((double)2);
+ return h*log2/log(size);
+ }
+}
diff -r c9dfcd92e312 libr/include/r_hash.h
--- a/libr/include/r_hash.h Wed Mar 02 14:11:34 2011 +0100
+++ b/libr/include/r_hash.h Wed Mar 02 20:21:43 2011 +0000
@@ -100,6 +100,7 @@
/* analysis */
R_API ut8 r_hash_hamdist(const ut8 *buf, int len);
R_API double r_hash_entropy(const ut8 *data, ut64 len);
+R_API double r_hash_entropy_fraction(const ut8 *data, ut64 len);
R_API int r_hash_pcprint(const ut8 *buffer, ut64 len);
#endif
_______________________________________________
radare mailing list
[email protected]
http://lists.nopcode.org/listinfo.cgi/radare-nopcode.org