Created
November 12, 2017 21:06
-
-
Save tiagocoutinho/cbb2181305e30579f4d5e56710c85aa5 to your computer and use it in GitHub Desktop.
hexdump with multiple memory zones
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hexdump | |
def binaries_gen(binaries): | |
""" | |
Generates a sequence of bytes from a seq<(offset, bytes)> | |
If there are gaps between the bytes the generator yields N None where N is | |
the length of the gap | |
Example:: | |
>>> gen = binaries_gen(((0, 'foo'), (5, 'bar'))) | |
>>> list(gen) | |
['f', 'o', 'o', None, None, 'b', 'a', 'r'] | |
""" | |
next_addr = 0 | |
for offset, binary in binaries: | |
# if there is a gap fill it with 'empty' | |
for i in range(offset - next_addr): | |
yield | |
for byte in binary: | |
yield byte | |
next_addr = offset + len(binary) | |
def dumps(binaries, *args, **kwargs): | |
""" | |
Hex dump a sequence of seq<(offset, bytes)> | |
(needs hexdump with PR https://bitbucket.org/techtonik/hexdump/pull-requests/4) | |
Example:: | |
>>> dumps(((0, 'foo'), (5, 'bar'))) | |
'66 6F 6F -- -- 62 61 72' | |
""" | |
return hexdump.dump(tuple(binaries_gen(binaries), *args, **kwargs)) | |
def hexdumps(binaries, *args, **kwargs): | |
""" | |
Hex dump a sequence of seq<(offset, bytes)> | |
(needs hexdump with PR https://bitbucket.org/techtonik/hexdump/pull-requests/4) | |
Example:: | |
>>> data = (0, 'foo bar is going to be'), (25, 'cut') | |
>>> hexdumps(data) | |
00000000: 66 6F 6F 20 62 61 72 20 69 73 20 67 6F 69 6E 67 foo bar is going | |
00000010: 20 74 6F 20 62 65 -- -- -- 63 75 74 to be...cut | |
""" | |
return hexdump.hexdump(tuple(binaries_gen(binaries), *args, **kwargs)) | |
def main(): | |
data = (0, 'foo bar is going to be'), (25, 'cut') | |
hexdumps(data) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment