Skip to content

Commit

Permalink
Regressed entropy updates due to TypeError's
Browse files Browse the repository at this point in the history
  • Loading branch information
devttys0 committed Oct 27, 2020
1 parent 83f19df commit 5b6c4ce
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/binwalk/modules/entropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
from binwalk.core.compat import *
from binwalk.core.module import Module, Option, Kwarg

try:
import numpy as np
except ImportError:
pass
#try:
# import numpy as np
#except ImportError:
# pass
try:
from numba import njit
except ImportError:
Expand Down Expand Up @@ -238,22 +238,22 @@ def shannon(self, data):
entropy = 0

if data:
data = data.encode('latin-1')
length = len(data)
seen = [0] * 256

seen = dict(((chr(x), 0) for x in range(0, 256)))
for byte in data:
seen[byte] += 1

for x in seen:
p_x = float(x) / length
for x in range(0, 256):
p_x = float(seen[chr(x)]) / length
if p_x > 0:
entropy -= p_x * math.log(p_x, 2)

return (entropy / 8)

def shannon_numpy(self, data):
if data:
return self._shannon_numpy(data.encode('latin-1'))
return self._shannon_numpy(bytes2str(data))
else:
return 0

Expand Down

0 comments on commit 5b6c4ce

Please sign in to comment.