-
Notifications
You must be signed in to change notification settings - Fork 2
/
pointer_peek.py
102 lines (86 loc) · 2.83 KB
/
pointer_peek.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
"""
Takes a file and an offset of a pointer and returns what that pointer points to, location and snippet.
"""
import sys
import os
from utils import SRC_PATH, DEST_PATH, unpack, ascii_to_hex_string
from rominfo import POINTER_CONSTANT
def ensure_src_file_path(str):
try:
f = open(str, 'r')
f.close()
except IOError:
str = os.path.join(SRC_PATH, str)
f = open(str, 'r')
f.close()
return str
def ensure_dest_file_path(str):
try:
f = open(str, 'r')
f.close()
except IOError:
str = os.path.join(DEST_PATH, str)
f = open(str, 'r')
f.close()
return str
def word_at_offset(filename, offset):
#filename = ensure_src_file_path(filename)
with open(filename, 'rb') as f:
result = ""
f.seek(offset)
data = f.read(2)
for b in data:
result += "%02x" % ord(b)
return unpack(result[0:2], result[2:])
def text_at_offset(filename, offset, go_until_wait=False):
try:
f = open(filename, 'rb')
except TypeError:
filename = os.path.join(DEST_PATH, filename.filename)
f = open(filename, 'rb')
f.seek(offset)
result = ""
data = f.read(1)
if go_until_wait:
control_code = 0x13
else:
control_code = 00
while ord(data) != control_code: # END control code
result += data
data = f.read(1)
f.close()
return result
def text_with_pointer(filename, pointer_offset):
pointer_offset = int(pointer_offset, 16)
constant = POINTER_CONSTANT[filename]
try:
f = open(filename, 'rb')
f.close()
except IOError:
filename = os.path.join(DEST_PATH, filename)
destination = word_at_offset(filename, pointer_offset) + constant
return text_at_offset(filename, destination)
def bytestring_at_offset(filename, offset):
return ascii_to_hex_string(text_at_offset(filename, offset))
if __name__ == '__main__':
filename = sys.argv[1]
constant = POINTER_CONSTANT[filename]
offset = int(sys.argv[2], 16)
if len(sys.argv) > 3:
original = sys.argv[3] == '--original'
else:
original = False
if original:
filepath = os.path.join(SRC_PATH, filename)
else:
filepath = os.path.join(DEST_PATH, filename)
pointer_value = word_at_offset(filepath, offset)
print "value:", hex(pointer_value)
if pointer_value == 0xb81e:
# Lots of things listed in the pointer spreadhseet are 2 too high, and point to the "ptr begin" ctrl code.
print "I think you meant %s" % offset
pointer_value = word_at_offset(filepath, offset+2)
pointed_location = pointer_value + constant
print "points to:", hex(pointed_location)
print text_at_offset(filepath, pointed_location)
print bytestring_at_offset(filepath, pointed_location)