This repository was archived by the owner on Mar 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (44 loc) · 1.33 KB
/
main.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
import argparse
def hexdump(in_path: str, /, line_count=20, offset=0):
with open(in_path, "rb") as in_stream:
__hexdump(in_stream, line_count, offset)
def __hexdump(inp, line_count, offset):
inp.read(offset)
adr = offset
line_acc = 0
while (bs := inp.read(16)) \
and (True if line_count is None else line_acc < line_count):
__print_line(adr, bs)
adr += len(bs)
line_acc += 1
def __print_line(adr, bs):
print(f"{adr:8X} ", end="")
for b in bs:
print(f"{b:02X} ", end="")
for i in range(len(bs), 16):
print(" ", end="")
print(" ", end="")
for b in bs:
print(__convert_char(b), end="")
for i in range(len(bs), 16):
print(" ", end="")
print()
def __convert_char(c):
if c <= 32 or c >= 127:
return '.'
return chr(c)
def main() -> int:
parser = argparse.ArgumentParser(
prog='hexdump',
description="prints the hexdump output to stdout",
)
parser.add_argument('filename')
parser.add_argument('-l', '--line-count', type=int)
parser.add_argument('-o', '--offset', type=int, default=0)
args = parser.parse_args()
hexdump(args.filename,
line_count=args.line_count,
offset=args.offset)
return 0
if __name__ == "__main__":
exit(main())