summaryrefslogtreecommitdiff
path: root/iupe-hexview-toolbox.lua
blob: fdb2638da677b5979a922d5747adb7258afe6709 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
loadmodule "luaiup"
loadmodule "luahandle"
loadmodule "lualibs"

if not iupe then iupe = {} end
if not iupep then iupep = {} end

iupep.hexview_toolbox = {
    clipboard = iup.clipboard{},
    
    getcursor = function (self, hexview)
        local rcursor = self.cursorlist.value - 3
        local r
        if rcursor == -2 then
            r = hexview.kcursor
        elseif rcursor == -1 then
            r = hexview.mcursor
        else
            r = hexview.markers[rcursor]
        end
        
        return r + 0, markercolors[rcursor]
    end,
    
    hexview_cb = function (self, hexview, reset)
        local filecursor = hexview.filecursor
        local filesize = hexview.filesize
        local fcl = self.cursors.file
        fcl.dec.title = filecursor
        fcl.hex.title = hex(filecursor, "0x%08x")
        local rend = filesize - filecursor
        fcl.rend_dec.title = rend
        fcl.rend_hex.title = hex(rend, "0x%08x")

        local cursor, bgcolor = self:getcursor(hexview)
        local oldcursor = self.oldcursor + 0
        if not reset and (oldcursor == cursor or cursor == -1) then return iup.DEFAULT end
        self.oldcursor = cursor
        local cl = self.cursors.normal
        cl.dec.title = cursor
        cl.hex.title = hex(cursor, "0x%08x")
        rend = filesize - cursor
        cl.rend_dec.title = rend
        cl.rend_hex.title = hex(rend, "0x%08x")
        
        cl.dec.bgcolor = bgcolor
        cl.hex.bgcolor = bgcolor
        cl.rend_dec.bgcolor = bgcolor
        cl.rend_hex.bgcolor = bgcolor
        
        local endianess = self.endianess.value + 0
        local nbbytes = math.min(4, filesize - cursor)
        
        local handle = hexview.handle
        local v = self.values
        if not handle or nbbytes == 0 then
            for _, w in ipairs(v) do w.title = "n/a" end
            return iup.DEFAULT
        end

        local buf = Buffer(true)
        handle:seek(cursor)
        buf:copyfrom(handle, nbbytes)
        
        local u8 = buf:readU8()
        buf:seek(0)
        v.u8.title = u8
        v.i8.title = u8 > 127 and u8 - 256 or u8
        
        if nbbytes >= 2 then
            local u16
            if endianess == 1 then
                u16 = buf:readU16()
            else
                local u1, u2
                u1 = buf:readU8()
                u2 = buf:readU8()
                u16 = u2 + u1 * 256
            end
            buf:seek(0)
            v.u16.title = u16
            v.i16.title = u16 > 32767 and u16 - 65536 or u16
        else
            v.u16.title = "n/a"
            v.i16.title = "n/a"
        end
        
        if nbbytes >= 4 then
            local u32
            if endianess == 1 then
                u32 = buf:readU32()
            else
                local u1, u2, u3, u4
                u1 = buf:readU8()
                u2 = buf:readU8()
                u3 = buf:readU8()
                u4 = buf:readU8()
                u32 = u4 + u3 * 256 + u2 * 65536 + u1 * 16777216
            end
            buf:seek(0)
            if endianess == 2 then
                local u1, u2, u3, u4
                u1 = buf:readU8()
                u2 = buf:readU8()
                u3 = buf:readU8()
                u4 = buf:readU8()
                buf:reset()
                buf:writeU8(u4)
                buf:writeU8(u3)
                buf:writeU8(u2)
                buf:writeU8(u1)
            end
            local flt = buf:readFloat()
            v.u32.title = u32
            v.i32.title = u32 > 2147483647 and u32 - 4294967296 or u32
            v.flt.title = flt
        else
            v.u16.title = "n/a"
            v.i16.title = "n/a"
            v.flt.title = "n/a"
        end
        
        buf:destroy()
        
        return iup.DEFAULT
    end,
    
    cursor_change = function (self, hexview, toolbox, rend)
        local ok, value = iup.GetParam("Cursor change", nil, "%s\n", self.title)
        local isint
        isint, value = pcall(function() return value + 0 end)
        if not isint then return iup.DEFAULT end
        local newvalue = rend and (hexview.filesize - value) or value
        hexview:setcursor(toolbox.cursorlist.value - 3, newvalue)
        return iup.DEFAULT
    end,
    
    filecursor_change = function (self, hexview, toolbox, rend)
        local ok, value = iup.GetParam("File position change", nil, "%s\n", self.title)
        local isint
        isint, value = pcall(function() return value + 0 end)
        if not isint then return iup.DEFAULT end
        hexview.filecursor = math.min(math.max(rend and (hexview.filesize - value) or value, 0), hexview.filesize)
        toolbox:hexview_cb(hexview)
        hexview:updatescrollbar()
        hexview:draw()
        return iup.DEFAULT
    end,
    
    value_enquery = function (self)
        -- iup.GetParam("Value copy/paste", nil, "%s\n", self.title)
        iupep.hexview_toolbox.clipboard.text = self.title
    end,
    
    create = function (tab)
        local hexview = tab.hexview
        tab.hexview = nil
        local cursorlabel_dec = iup.button{ tip = "Position of active cursor.\nClick to select/change.", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" }
        local cursorlabel_hex = iup.button{ tip = "Position of active cursor.\nClick to select/change.", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" }
        local cursorlabel_rend_dec = iup.button{ tip = "Position of active cursor,\nrelative to the end.\nClick to select/change.", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" }
        local cursorlabel_rend_hex = iup.button{ tip = "Position of active cursor,\nrelative to the end.\nClick to select/change.", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" }
        local filecursorlabel_dec = iup.button{ tip = "Position of window.\nClick to select/change.", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" }
        local filecursorlabel_hex = iup.button{ tip = "Position of window.\nClick to select/change.", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" }
        local filecursorlabel_rend_dec = iup.button{ tip = "Position of window,\nrelative to the end.\nClick to select/change.", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" }
        local filecursorlabel_rend_hex = iup.button{ tip = "Position of window,\nrelative to the end.\nClick to select/change.", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" }
        local cursorlist = iup.list{ tip = "Active cursor.", dropdown = "Yes", value = "1", "Keyboard", "Mouse", "Cursor 0", "Cursor 1", "Cursor 2",  "Cursor 3",  "Cursor 4",  "Cursor 5",  "Cursor 6",  "Cursor 7",  "Cursor 8",  "Cursor 9",  "Cursor 10" }
        local endianess = iup.list{ tip = "Display value endianess.", dropdown = "Yes", value = "1", "Little Endian", "Big Endian" }
        local cvlabel_u8 = iup.button{ tip = "Type: uint8.\nClick to copy value in clipboard", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" }
        local cvlabel_i8 = iup.button{ tip = "Type: int8.\nClick to copy value in clipboard", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" }
        local cvlabel_u16 = iup.button{ tip = "Type: uint16.\nClick to copy value in clipboard", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" }
        local cvlabel_i16 = iup.button{ tip = "Type: int16.\nClick to copy value in clipboard", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" }
        local cvlabel_u32 = iup.button{ tip = "Type: uint32.\nClick to copy value in clipboard", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" }
        local cvlabel_i32 = iup.button{ tip = "Type: int32.\nClick to copy value in clipboard", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" }
        local cvlabel_flt = iup.button{ tip = "Type: float.\nClick to copy value in clipboard", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" }
        local r = iup.frame {
            iup.vbox {
                iup.label{ title = "Cursor", font = "Helvetica, Bold 10" },
                cursorlist,
                iup.label{ separator = "Horizontal" },
                iup.label{ title = "Cursor position", font = "Helvetica, Bold 10" },
                cursorlabel_dec,
                cursorlabel_rend_dec,
                cursorlabel_hex,
                cursorlabel_rend_hex,
                iup.label{ separator = "Horizontal" },
                iup.label{ title = "Cursor value", font = "Helvetica, Bold 10" },
                endianess,
                cvlabel_i8,
                cvlabel_u8,
                cvlabel_i16,
                cvlabel_u16,
                cvlabel_i32,
                cvlabel_u32,
                cvlabel_flt,
                iup.label{ separator = "Horizontal" },
                iup.label{ title = "File position", font = "Helvetica, Bold 10" },
                filecursorlabel_dec,
                filecursorlabel_rend_dec,
                filecursorlabel_hex,
                filecursorlabel_rend_hex,
                iup.fill{},
            },
            
            unpack(tab),
        }
        
        r.cursors = {
            normal = {
                dec = cursorlabel_dec,
                hex = cursorlabel_hex,
                rend_dec = cursorlabel_rend_dec,
                rend_hex = cursorlabel_rend_hex,
            },
            file = {
                dec = filecursorlabel_dec,
                hex = filecursorlabel_hex,
                rend_dec = filecursorlabel_rend_dec,
                rend_hex = filecursorlabel_rend_hex,
            },
        }
        
        r.values = {
            u8 = cvlabel_u8,
            i8 = cvlabel_i8,
            u16 = cvlabel_u16,
            i16 = cvlabel_i16,
            u32 = cvlabel_u32,
            i32 = cvlabel_i32,
            flt = cvlabel_flt,
        }
        
        r.cursorlist = cursorlist
        r.endianess = endianess
        r.getcursor = iupep.hexview_toolbox.getcursor
        r.hexview_cb = iupep.hexview_toolbox.hexview_cb
        r.oldcursor = -1
        
        hexview:registercb(r.hexview_cb, r)
        
        function cursorlist:action(text, pos, state)
            r:hexview_cb(hexview)
        end
        
        function endianess:action(text, pos, state)
            r:hexview_cb(hexview, true)
        end
        
        cursorlabel_dec.action = function (self) return iupep.hexview_toolbox.cursor_change(self, hexview, r, false) end
        cursorlabel_hex.action = function (self) return iupep.hexview_toolbox.cursor_change(self, hexview, r, false) end
        cursorlabel_rend_dec.action = function (self) return iupep.hexview_toolbox.cursor_change(self, hexview, r, true) end
        cursorlabel_rend_hex.action = function (self) return iupep.hexview_toolbox.cursor_change(self, hexview, r, true) end
        
        filecursorlabel_dec.action = function (self) return iupep.hexview_toolbox.filecursor_change(self, hexview, r, false) end
        filecursorlabel_hex.action = function (self) return iupep.hexview_toolbox.filecursor_change(self, hexview, r, false) end
        filecursorlabel_rend_dec.action = function (self) return iupep.hexview_toolbox.filecursor_change(self, hexview, r, true) end
        filecursorlabel_rend_hex.action = function (self) return iupep.hexview_toolbox.filecursor_change(self, hexview, r, true) end
        
        cvlabel_u8.action = iupep.hexview_toolbox.value_enquery
        cvlabel_i8.action = iupep.hexview_toolbox.value_enquery
        cvlabel_u16.action = iupep.hexview_toolbox.value_enquery
        cvlabel_i16.action = iupep.hexview_toolbox.value_enquery
        cvlabel_u32.action = iupep.hexview_toolbox.value_enquery
        cvlabel_i32.action = iupep.hexview_toolbox.value_enquery
        cvlabel_flt.action = iupep.hexview_toolbox.value_enquery
        
        r:hexview_cb(hexview)
        return r
    end,
}

iupe.hexview_toolbox = iupep.hexview_toolbox.create