-
Notifications
You must be signed in to change notification settings - Fork 11
/
explore.py
executable file
·246 lines (214 loc) · 6.99 KB
/
explore.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
#!/usr/bin/env python3
"""
Bit dump binary file to text or image.
Text output is like `xxd` but more adapted to visually exploring bitmaps.
(c) 2019--2024 Rob Hagemans
licence: https://opensource.org/licenses/MIT
"""
import sys
import argparse
import logging
from PIL import Image, ImageDraw, ImageFont
from itertools import zip_longest
from pathlib import Path
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
# parse command line
parser = argparse.ArgumentParser()
parser.add_argument(
'infile', nargs='?',
type=argparse.FileType('rb'), default=sys.stdin.buffer
)
parser.add_argument(
'outfile', nargs='?',
type=str, default=''
)
parser.add_argument(
'-s', '--stride-from', default=1, type=int,
help='lowest number of bytes per scanline'
)
parser.add_argument(
'-t', '--stride-to', default=None, type=int,
help='highest number of bytes per scanline'
)
parser.add_argument(
'-b', '--stride-bits', default=None, type=int,
help='bits per scanline. cannot be used with -s or -t'
)
parser.add_argument(
'-o', '--offset', default=0, type=int,
help='byte offset into binary'
)
parser.add_argument(
'-n', '--bytes', default=-1, type=int,
help='total number of bytes to extract'
)
parser.add_argument(
'--image', default=False, action='store_true',
help='output as image instead of text'
)
# only apply to text
parser.add_argument(
'--ink', '--foreground', '-fg', type=str, default='@',
help='character to use for ink/foreground (default: @)'
)
parser.add_argument(
'--paper', '--background', '-bg', type=str, default='.',
help='character to use for paper/background (default: .)'
)
# only apply to images
parser.add_argument(
'--padding', default=8, type=int,
help='number of vertical pixels between output bitmaps'
)
parser.add_argument(
'--margin', default=10, type=int,
help='number of horizontal pixels left of first bitmap'
)
parser.add_argument(
'--scale', default=1, type=int,
help='number of horizontal and vertical pixels used to represent a single bit'
)
def main():
args = parser.parse_args()
if args.stride_to is None:
args.stride_to = args.stride_from
args.infile.read(args.offset)
data = args.infile.read(args.bytes)
bytesize = len(data)
if args.image or args.outfile and not args.outfile.endswith('.txt'):
if args.stride_bits is not None:
raise ValueError('Bit-aligned strides not supported for images.')
# output filename does not end with .txt
# see if PIL recognises the suffix, otherwise dump as text
try:
return bitdump_image(
args.outfile,
data, bytesize, args.stride_from, args.stride_to,
args.margin, args.padding, args.scale
)
except ValueError as e:
# unknown file extension
if not str(e).startswith('unknown file extension'):
raise
logging.warning(
'Output file extension `%s` not recognised, using text output.',
Path(args.outfile).suffix
)
try:
bitdump_text(
args.outfile,
data, bytesize,
args.stride_from, args.stride_to, args.stride_bits,
args.paper, args.ink, start=args.offset
)
except BrokenPipeError:
pass
def ceildiv(num, den):
"""Integer division, rounding up."""
return -(-num // den)
def showchar(value):
"""Show ascii chars, replace non-ascii with shaded block elementss."""
if value == 0:
return '░'
elif value < 0x20:
return '▒'
elif value == 0xff:
return '█'
elif value >= 0x7f:
return '▓'
return chr(value)
def draw_bits(data, paper, ink):
bits = bin(int.from_bytes(data, 'big'))[2:].zfill(8*len(data))
return bits.replace('0', paper).replace('1', ink)
def bitdump_text(
outfilename,
data, bytesize,
stride_from, stride_to, stride_bits,
paper, ink, start
):
"""Bit dump to text output."""
# width of decimal and hex offset fields
decwidth = len(str(len(data)))
hexwidth = len(hex(len(data))) - 2
if outfilename:
outfile = open(outfilename, 'w')
else:
outfile = sys.stdout
if stride_bits is not None:
# get the bits
drawn = draw_bits(data, paper, ink)
# itertools grouper
args = [iter(drawn)] * stride_bits
grouper = zip_longest(*args, fillvalue='0')
for i, bits in enumerate(grouper):
offset, mod = divmod(i * stride_bits, 8)
if not mod:
outfile.write(f'{offset+start:{decwidth}} {offset+start:0{hexwidth}x} ')
else:
outfile.write(' ' * (decwidth + hexwidth + 3))
outfile.write(''.join(bits))
outfile.write('\n')
return
for stride in range(stride_from, stride_to+1):
width = stride * 8
height = ceildiv(bytesize, stride)
if stride_to > stride_from:
outfile.write('\n')
title = f'stride={stride}'
outfile.write(title + '\n')
outfile.write('-'*len(title) + '\n')
for offset in range(0, bytesize, stride):
values = data[offset:offset+stride]
bits = draw_bits(values, paper, ink)
letters = ''.join(showchar(_v) for _v in values)
outfile.write(f'{offset+start:{decwidth}} {offset+start:0{hexwidth}x} ')
outfile.write(bits)
outfile.write(f' {values.hex(" ")} {letters} ')
outfile.write(' '.join(f'{_v:3d}' for _v in values))
outfile.write('\n')
if outfile != sys.stdout:
outfile.close()
def bitdump_image(
outfilename,
data, bytesize,
stride_from, stride_to,
margin, padding, scale
):
"""Bit dump to image."""
# colours
border = (20, 20, 20)
textcolour = (128, 255, 128)
images = []
# append more than enough zeros to fill any shortfall
data += b'\0' * stride_to
for stride in range(stride_from, stride_to+1):
width = stride * 8
height = ceildiv(bytesize, stride)
img = Image.frombytes('1', (width, height), data)
images.append((stride, img))
font = ImageFont.load_default_imagefont()
max_stride, _ = images[-1]
size = font.font.getsize(str(max_stride))
margin += size[0]
fullimage = Image.new(
'RGB', (
margin + max(_i.width for _, _i in images),
sum(_i.height + padding for _, _i in images)
),
border
)
draw = ImageDraw.Draw(fullimage)
left, top = margin, 0
for stride, img in images:
draw.text((0, top), str(stride), font=font, fill=textcolour)
fullimage.paste(img, (left, top))
top += img.height + padding
fullimage = fullimage.resize((
fullimage.width * scale,
fullimage.height * scale
))
if not outfilename:
fullimage.show()
else:
fullimage.save(outfilename)
main()