forked from phracker/HopperScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTo String.py
executable file
·33 lines (29 loc) · 1.27 KB
/
To String.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
#
# Turn the current selection into an ASCII string.
#
# Samuel Groß <[email protected]> - github.com/saelo
#
doc = Document.getCurrentDocument()
seg = doc.getCurrentSegment()
start, end = doc.getSelectionAddressRange()
bytes = [seg.readByte(addr) for addr in range(start, end)]
# check if selection is a valid ASCII string
if not bytes[-1] == 0:
doc.log("Selection must include terminating null character")
raise Exception("Invalid selection")
# maybe not the best way but the following should work with python 2.x and 3.x
if not all(0x20 <= byte <= 0x7e or byte == 0x0a or byte == 0x0d for byte in bytes[:-1]):
doc.log("Selection is not a valid ASCII string")
raise Exception("Invalid selection")
string = "".join([chr(b) for b in bytes[:-1]])
# mark bytes as string
seg.setTypeAtAddress(start, end - start, Segment.TYPE_ASCII)
# add comments to the addresses where the string is referenced
xrefs = seg.getReferencesOfAddress(start)
if xrefs:
for xref in xrefs:
xrefSegment = doc.getSegmentAtAddress(xref)
comment = xrefSegment.getInlineCommentAtAddress(xref)
if comment is None or comment.startswith('0x'):
xrefSegment.setInlineCommentAtAddress(xref,
'"%s"%s' % (string[:100], '..' if len(string) > 100 else ''))