summaryrefslogtreecommitdiff
path: root/plugins/python/cglimpse/entropy.py
blob: 5c4df5a29a0b3999037903425d8fa5ae0ecd9e95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225

import math
from gi.repository import Gdk
from pychrysalide.glibext import ConfigParam

from .method import GlimpseMethod


class ShannonEntropy(GlimpseMethod):


    @staticmethod
    def setup_config(config):
        """Register the configuration parameters for the method."""

        color = Gdk.RGBA()
        color.parse('#3465A4')

        param = ConfigParam('cglimpse.shannon.color', ConfigParam.ConfigParamType.COLOR, color)
        config.add(param)

        param = ConfigParam('cglimpse.shannon.step', ConfigParam.ConfigParamType.ULONG, 0x80)
        config.add(param)


    def __init__(self, builder, config, update_cb):
        """Prepare a Shannon entropy display."""

        super(ShannonEntropy, self).__init__(builder, config, update_cb)

        self._v_legend = 'Entropy'
        self._h_legend = 'Byte offsets'

        self._x_range = [ 0, 1024, 10240 ]
        self._y_range = [ 0.0, 0.25, 1.0 ]

        self._coverage = None
        self._values = []

        button = builder.get_object('shannon_color')
        button.connect('color-set', self._on_color_set)

        param = config.search('cglimpse.shannon.color')
        color = param.value

        self._color = [ color.red, color.green, color.blue, color.alpha ]
        self._shadow_color = [ color.red * 0.5, color.green * 0.5, color.blue * 0.5, color.alpha ]

        button.set_rgba(param.value)

        scale = builder.get_object('shannon_step')
        scale.connect('format-value', self._on_scale_format_value)
        scale.connect('value-changed', self._on_scale_value_changed)

        param = config.search('cglimpse.shannon.step')
        step = param.value

        self._step = step

        scale.set_value(step)


    def _on_color_set(self, button):
        """React on color chosen for the rendering."""

        color = button.get_rgba()

        self._color = [ color.red, color.green, color.blue, color.alpha ]
        self._shadow_color = [ color.red * 0.5, color.green * 0.5, color.blue * 0.5, color.alpha ]

        param = self._config.search('cglimpse.shannon.color')
        param.value = color

        self._update_cb()


    def _on_scale_value_changed(self, scale):
        """React when the range value changes."""

        step = int(scale.get_value())

        self._step = step

        param = self._config.search('cglimpse.shannon.step')
        param.value = step

        self._update_cb()


    def _on_scale_format_value(self, scale, value):
        """Change how the scale value is displayed."""

        return '0x%x' % int(value)


    def format_legend(self, value, vert):
        """Build the label used for a rule."""

        if vert:
            text = str(value)

        else:

            multi = 1

            scale = [ ' kb', ' Mb', ' Gb', ' Tb' ]
            suffix = ''

            remaining = value

            for i in range(len(scale)):

                if remaining < 1024:
                    break

                multi *= 1024

                remaining /= 1024
                suffix = scale[i]

            if value % multi == 0:
                text = '%u%s' % (remaining, suffix)
            else:
                text = '%.1f%s' % (value / multi, suffix)

        return text


    def update(self, data, coverage):
        """Compute internal values for the method."""

        self._coverage = coverage

        size = self._coverage[1] - self._coverage[0]

        step = math.ceil(size / 10)

        self._x_range = [ coverage[0], step, coverage[0] + 10 * step ]

        self._values = []

        for i in range(self._x_range[0], self._x_range[2], self._step):

            counter = [ 0 for i in range(256) ]

            start = i
            end = i + self._step

            if end > self._coverage[1]:
                end = self._coverage[1]

            for b in data[start : end]:
                counter[b] += 1

            ent = 0.0

            for c in counter:
                if c > 0:
                    freq = c / (end - start)
                    ent += freq * math.log(freq, 256)

            self._values.append(-ent)


    def render(self, cr, area):
        """Draw the bytes distribution for the current binary, if any."""

        size = self._coverage[1] - self._coverage[0]

        step = math.ceil(size / 10)

        start = self._coverage[0]
        last_x = area[0]

        last_y = area[1] + area[3] - (area[3] * self._values[0])
        cr.move_to(last_x, last_y + 2)

        for i in range(self._x_range[0], self._x_range[2], self._step):

            end = i + self._step

            if end > self._coverage[1]:
                end = self._coverage[1]

            x = area[0] + ((end - start) * area[2]) / (10 * step)
            y = area[1] + area[3] - (area[3] * self._values[int((i - self._x_range[0]) / self._step)])

            if last_y != y:
                cr.line_to(last_x, y + 2)
                last_y = y

            cr.line_to(x, y + 2)

            last_x = x

        cr.set_source_rgba(*self._shadow_color)
        cr.set_line_width(4)
        cr.stroke()

        last_x = area[0]

        last_y = area[1] + area[3] - (area[3] * self._values[0])
        cr.move_to(last_x, last_y)

        for i in range(self._x_range[0], self._x_range[2], self._step):

            end = i + self._step

            if end > self._coverage[1]:
                end = self._coverage[1]

            x = area[0] + ((end - start) * area[2]) / (10 * step)
            y = area[1] + area[3] - (area[3] * self._values[int((i - self._x_range[0]) / self._step)])

            if last_y != y:
                cr.line_to(last_x, y)
                last_y = y

            cr.line_to(x, y)

            last_x = x

        cr.set_source_rgba(*self._color)
        cr.set_line_width(2)
        cr.stroke()