-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhex_render.py
More file actions
182 lines (154 loc) · 6.2 KB
/
Copy pathhex_render.py
File metadata and controls
182 lines (154 loc) · 6.2 KB
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
import cv2
import numpy as np
_F = cv2.FONT_HERSHEY_PLAIN
FS = 1.0
FT = 1
CW = 9 # character width (px)
ROW_H = 18
HDR_H = 24
STAT_H = 54
BPR = 16 # bytes per row
X_OFF = 8
X_HEX = X_OFF + 9 * CW # after "OFFSET " label
# last col absolute x: X_HEX + 15*3*CW + 1*CW (group gap) = X_HEX + 46*CW
# end of last byte: X_HEX + 46*CW + 2*CW = X_HEX + 48*CW
X_ASCII = X_HEX + 48 * CW + 2 * CW # 2*CW gap after hex
MIN_W = X_ASCII + 19 * CW + 20 # "|" + 16 + "|" + margin
C = {
"bg": (14, 14, 18),
"hdr_bg": (26, 26, 34),
"stat_bg": (18, 18, 28),
"sep": (46, 46, 62),
"off": (88, 128, 255),
"hdr": (88, 88, 118),
"null": (42, 42, 52),
"print": (68, 208, 88),
"nonpr": (188, 68, 68),
"asc_p": (108, 228, 118),
"asc_d": (52, 52, 62),
"asc_sep": (46, 46, 62),
"hl": (228, 228, 0),
"hl_bg": (62, 58, 0),
"sh": (88, 198, 255),
"sh_bg": (0, 48, 108),
"str": (168, 68, 255),
"str_bg": (52, 0, 72),
"stat_t": (148, 148, 168),
"srch_c": (78, 198, 255),
"sep_ln": (38, 38, 52),
}
def _t(canvas, s, x, y, color):
cv2.putText(canvas, s, (x, y), _F, FS, color, FT, cv2.LINE_AA)
def _rf(canvas, x, y, w, h, color):
if w > 0 and h > 0:
cv2.rectangle(canvas, (x, y), (x + w, y + h), color, -1)
def hex_x(col):
group = col // 8
return X_HEX + col * 3 * CW + group * CW
def col_from_x(x):
if x < X_HEX:
return -1
for col in range(BPR):
bx = hex_x(col)
if bx <= x < bx + 2 * CW:
return col
return -1
def render(data, state, ww, wh):
canvas = np.zeros((wh, ww, 3), dtype=np.uint8)
canvas[:] = C["bg"]
scroll = state.get("scroll_row", 0)
hl = state.get("highlight_offset", -1)
sh_set = state.get("search_result_set", set())
str_set = state.get("str_byte_set", set())
total = len(data)
total_rows = max(1, (total + BPR - 1) // BPR)
content_h = wh - HDR_H - STAT_H
vis = max(1, content_h // ROW_H)
# Header bar
cv2.rectangle(canvas, (0, 0), (ww, HDR_H), C["hdr_bg"], -1)
_t(canvas, "OFFSET", X_OFF, 16, C["hdr"])
for col in range(BPR):
_t(canvas, f"{col:02X}", hex_x(col), 16, C["hdr"])
_t(canvas, "|", X_ASCII, 16, C["asc_sep"])
for i, ch in enumerate("0123456789ABCDEF"):
_t(canvas, ch, X_ASCII + (i + 1) * CW, 16, C["hdr"])
_t(canvas, "|", X_ASCII + 17 * CW, 16, C["asc_sep"])
cv2.line(canvas, (0, HDR_H), (ww, HDR_H), C["sep_ln"], 1)
# Vertical separators
sv1 = X_HEX - CW
sv2 = X_ASCII - CW
cv2.line(canvas, (sv1, HDR_H), (sv1, wh - STAT_H), C["sep_ln"], 1)
cv2.line(canvas, (sv2, HDR_H), (sv2, wh - STAT_H), C["sep_ln"], 1)
# Group separator in hex area (between col 7 and 8)
sg = hex_x(8) - CW // 2
cv2.line(canvas, (sg, HDR_H), (sg, wh - STAT_H), C["sep_ln"], 1)
# Rows
for r in range(vis):
row = scroll + r
if row >= total_rows:
break
bo = row * BPR
row_data = data[bo: bo + BPR]
y = HDR_H + r * ROW_H + ROW_H - 4
_t(canvas, f"{bo:08X}", X_OFF, y, C["off"])
printable = []
for col, b in enumerate(row_data):
ao = bo + col
hx = hex_x(col)
is_hl = (ao == hl)
is_sh = (ao in sh_set)
is_str = (ao in str_set)
if is_hl: _rf(canvas, hx - 1, y - ROW_H + 5, 2 * CW + 1, ROW_H - 2, C["hl_bg"])
elif is_sh: _rf(canvas, hx - 1, y - ROW_H + 5, 2 * CW + 1, ROW_H - 2, C["sh_bg"])
elif is_str: _rf(canvas, hx - 1, y - ROW_H + 5, 2 * CW + 1, ROW_H - 2, C["str_bg"])
if is_hl: hc = C["hl"]
elif is_sh: hc = C["sh"]
elif is_str: hc = C["str"]
elif b == 0: hc = C["null"]
elif 0x20 <= b <= 0x7E: hc = C["print"]
else: hc = C["nonpr"]
_t(canvas, f"{b:02X}", hx, y, hc)
printable.append((b, 0x20 <= b <= 0x7E))
# ASCII column
_t(canvas, "|", X_ASCII, y, C["asc_sep"])
for i, (b, is_p) in enumerate(printable):
ao = bo + i
ch = chr(b) if is_p else "."
if is_p and ao == hl: ac = C["hl"]
elif is_p and ao in sh_set: ac = C["sh"]
elif is_p and ao in str_set: ac = C["str"]
elif is_p: ac = C["asc_p"]
else: ac = C["asc_d"]
_t(canvas, ch, X_ASCII + (i + 1) * CW, y, ac)
_t(canvas, "|", X_ASCII + 17 * CW, y, C["asc_sep"])
return canvas, vis, total_rows
def render_status(canvas, state, wh, ww):
sy = wh - STAT_H
cv2.rectangle(canvas, (0, sy), (ww, wh), C["stat_bg"], -1)
cv2.line(canvas, (0, sy), (ww, sy), C["sep_ln"], 1)
sm = state.get("search_mode", False)
stxt = state.get("search_text", "")
msg = state.get("status_msg", "")
sr = state.get("search_results", [])
si = state.get("search_result_idx", -1)
stm = state.get("string_mode", False)
sl = state.get("string_list", [])
sidx = state.get("string_idx", 0)
sinp = state.get("string_input", "")
y1 = sy + 17
y2 = sy + 38
if stm and not sl:
_t(canvas, f"Min string length: {sinp}_", 8, y1, C["srch_c"])
_t(canvas, "[0-9] type length [Enter] search [Esc] cancel", 8, y2, C["stat_t"])
elif stm and sl:
off, s = sl[min(sidx, len(sl) - 1)]
preview = (s[:70] + "...") if len(s) > 70 else s
_t(canvas, f"[{sidx + 1}/{len(sl)}] @{off:08X} {preview}", 8, y1, C["str"])
_t(canvas, "[N] next [P] prev [Esc] exit strings", 8, y2, C["stat_t"])
elif sm:
_t(canvas, f"Search: {stxt}_", 8, y1, C["srch_c"])
_t(canvas, "Hex bytes: FF A0 2E or ASCII text [Enter] find [Esc] cancel", 8, y2, C["stat_t"])
else:
rinfo = f" result {si + 1}/{len(sr)}" if (sr and si >= 0) else ""
_t(canvas, f"{msg}{rinfo}", 8, y1, C["stat_t"])
_t(canvas, "[/] search [N/P] next/prev [S] strings [j/k][↑↓][PgUp/Dn] scroll [Q] close", 8, y2, C["stat_t"])