-
Notifications
You must be signed in to change notification settings - Fork 50
/
hex_inspector.py
276 lines (228 loc) · 9.83 KB
/
hex_inspector.py
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
272
273
274
275
276
"""
Hex Viewer.
Licensed under MIT
Copyright (c) 2011-2020 Isaac Muse <[email protected]>
"""
import sublime
import sublime_plugin
import math
import datetime
from struct import unpack
from . import hex_common as common
from binascii import unhexlify
hv_endianness = None
class HexShowInspectorCommand(sublime_plugin.WindowCommand):
"""Show the hex inspector panel."""
def is_enabled(self):
"""Check if command is enabled."""
return bool(common.is_enabled() and common.hv_settings("inspector", False))
def run(self):
"""Run the command."""
# Setup inspector window
view = self.window.get_output_panel('hex_viewer_inspector')
view.set_syntax_file("Packages/HexViewer/HexInspect.%s" % common.ST_SYNTAX)
view.settings().set("draw_white_space", "none")
view.settings().set("draw_indent_guides", False)
view.settings().set("gutter", False)
view.settings().set("line_numbers", False)
# Show
self.window.run_command("show_panel", {"panel": "output.hex_viewer_inspector"})
self.window.run_command("hex_inspector", {"reset": True})
class HexHideInspectorCommand(sublime_plugin.WindowCommand):
"""Hide the hex inspector panel."""
def is_enabled(self):
"""Check if command is enabled."""
return bool(common.is_enabled() and common.hv_settings("inspector", False))
def run(self):
"""Run the command."""
self.window.run_command("hide_panel", {"panel": "output.hex_viewer_inspector"})
class HexToggleInspectorEndiannessCommand(sublime_plugin.WindowCommand):
"""Toggle hex inspector's endianness."""
def is_enabled(self):
"""Check if command is enabled."""
return bool(common.is_enabled() and common.hv_settings("inspector", False))
def run(self):
"""Run the command."""
global hv_endianness
hv_endianness = "big" if hv_endianness == "little" else "little"
self.window.run_command('hex_highlighter')
class HexInspectGlobal(object):
"""Global hex inspector data."""
bfr = None
region = None
@classmethod
def clear(cls):
"""Clear."""
cls.bfr = None
cls.region = None
class HexInspectorApplyCommand(sublime_plugin.TextCommand):
"""Apply text to the hex inspector panel."""
def run(self, edit):
"""Run the command."""
self.view.replace(edit, HexInspectGlobal.region, HexInspectGlobal.bfr)
class HexInspectorListenerCommand(sublime_plugin.EventListener):
"""Hex Inspector listener command."""
def on_pre_close(self, view):
"""On close."""
if common.is_enabled() and view is not None and not view.settings().get("hex_viewer_fake", False):
win = view.window()
panel_view = win.get_output_panel('hex_viewer_inspector')
parent_win = panel_view.window()
if parent_win:
parent_win.run_command('hide_panel', {'cancel': True})
class HexInspectorCommand(sublime_plugin.WindowCommand):
"""Hex inspector command."""
def get_bytes(self, start, bytes_wide):
"""Get the bytes at the cursor."""
byte_str = self.view.substr(sublime.Region(start, start + 2))
byte64 = None
byte32 = None
byte16 = None
byte8 = None
start += 2
size = self.view.size()
count = 1
group_divide = 1
address = 12
ascii_divide = group_divide + bytes_wide + address + 1
target_bytes = 8
# Look for 64 bit worth of bytes
while start < size and count < target_bytes:
# Check if sitting on first nibble
if self.view.score_selector(start, 'raw.nibble.upper'):
byte_str += self.view.substr(sublime.Region(start, start + 2))
count += 1
start += 2
else:
# Must be at byte group falling edge; try and step over divider
start += group_divide
if start < size and self.view.score_selector(start, 'raw.nibble.upper'):
byte_str += self.view.substr(sublime.Region(start, start + 2))
count += 1
start += 2
# Must be at line end; try and step to next line
else:
start += ascii_divide
if start < size and self.view.score_selector(start, 'raw.nibble.upper'):
byte_str += self.view.substr(sublime.Region(start, start + 2))
count += 1
start += 2
else:
# No more bytes to check
break
byte8 = byte_str[0:2]
if count > 1:
byte16 = byte_str[0:4]
if count > 3:
byte32 = byte_str[0:8]
if count > 7:
byte64 = byte_str[0:16]
return byte8, byte16, byte32, byte64
def display(self, view, byte8, bytes16, bytes32, bytes64):
"""Display hex inspector data."""
item_dec = common.hv_settings("inspector_integer_format", "%-12s: %-14d")
item_str = common.hv_settings("inspector_missing/bad_format", "%-12s: %-14s")
item_float = common.hv_settings("inspector_float_format", "%-12s: %-14e")
item_double = common.hv_settings("inspector_double_format", "%-12s: %-14e")
item_bin = common.hv_settings("inspector_binary_format", "%-12s: %-14s")
item_timestamp, item_time = common.hv_settings("inspector_timestamp_format", ("%-12s: %-14s", "%c"))
nl = "\n"
endian = ">" if self.endian == "big" else "<"
i_buffer = "%28s:%-28s" % ("Hex Inspector ", (" Big Endian" if self.endian == "big" else " Little Endian")) + nl
if byte8 is not None:
i_buffer += item_dec * 2 % (
"byte", unpack(endian + "B", unhexlify(byte8))[0],
"short", unpack(endian + "b", unhexlify(byte8))[0]
) + nl
else:
i_buffer += item_str * 2 % (
"byte", "--",
"short", "--"
) + nl
if bytes16 is not None:
i_buffer += item_dec * 2 % (
"word", unpack(endian + "H", unhexlify(bytes16))[0],
"int", unpack(endian + "h", unhexlify(bytes16))[0]
) + nl
else:
i_buffer += item_str * 2 % (
"word", "--",
"int", "--"
) + nl
if bytes32 is not None:
i_buffer += item_dec * 2 % (
"dword", unpack(endian + "I", unhexlify(bytes32))[0],
"longint", unpack(endian + "i", unhexlify(bytes32))[0]
) + nl
else:
i_buffer += item_str * 2 % (
"dword", "--",
"longint", "--"
) + nl
if bytes64 is not None:
i_buffer += item_dec * 2 % (
"qword", unpack(endian + "Q", unhexlify(bytes64))[0],
"longlongint", unpack(endian + "q", unhexlify(bytes64))[0]
) + nl
else:
i_buffer += item_str * 2 % (
"qword", "--",
"longlongint", "--"
) + nl
if bytes32 is not None:
s_float = unpack(endian + "f", unhexlify(bytes32))[0]
if math.isnan(s_float):
i_buffer += item_str % ("float", "NaN")
else:
i_buffer += item_float % (
"float", s_float
)
else:
i_buffer += item_str % ("float", "--")
if bytes64 is not None:
d_float = unpack(endian + "d", unhexlify(bytes64))[0]
if math.isnan(d_float):
i_buffer += item_str % ("double", "NaN") + nl
else:
i_buffer += item_double % (
"double", d_float
) + nl
else:
i_buffer += item_str % ("double", "--") + nl
if byte8 is not None:
i_buffer += item_bin % ("binary", '{0:08b}'.format(unpack(endian + "B", unhexlify(byte8))[0])) + nl
else:
i_buffer += item_str % ("binary", "--") + nl
if bytes64 is not None:
try:
t = datetime.datetime.fromtimestamp(unpack(endian + "Q", unhexlify(bytes64))[0]).strftime(item_time)
i_buffer += item_timestamp % (
"timestamp", t
) + nl
except Exception:
i_buffer += item_str % ("timestamp", "--") + nl
else:
i_buffer += item_str % ("timestamp", "--") + nl
# Update content
view.set_read_only(False)
HexInspectGlobal.bfr = i_buffer
HexInspectGlobal.region = sublime.Region(0, view.size())
view.run_command("hex_inspector_apply")
HexInspectGlobal.clear()
view.set_read_only(True)
view.sel().clear()
def is_enabled(self):
"""Check if the command is enabled."""
return common.is_enabled()
def run(self, first_byte=None, bytes_wide=None, reset=False):
"""Run the command."""
self.view = self.window.active_view()
self.endian = hv_endianness
byte8, bytes16, bytes32, bytes64 = None, None, None, None
if not reset and first_byte is not None and bytes_wide is not None:
byte8, bytes16, bytes32, bytes64 = self.get_bytes(int(first_byte), int(bytes_wide))
self.display(self.window.get_output_panel('hex_viewer_inspector'), byte8, bytes16, bytes32, bytes64)
def plugin_loaded():
"""Setup plugin."""
global hv_endianness
hv_endianness = common.hv_settings("inspector_endian", "little")