Created
April 16, 2023 10:34
-
-
Save v-p-b/cc467a8f83ec1022061f925bc98523d7 to your computer and use it in GitHub Desktop.
Dump substructure offsets from objects parsed by Kaitai Struct
This file contains 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
# This function recurively traverses a KaitaiStruct objects and dumps the offsets of the substructures encountered. | |
# | |
# This is similar to how the WebIDE points you to specific substructers in the parsed tree when you click on some | |
# byte in the hex editor. | |
# Unfortunately I couldn't use any IDE tools for this capability, because [JavaScript sucks](https://github.com/kaitai-io/kaitai_struct/issues/183) | |
# You should generate the Kaitai parser with the `--read-pos` command line option | |
# The behavior is documented with this issue: https://github.com/kaitai-io/kaitai_struct/issues/331 | |
import string | |
l=[] # dummy list, see below | |
def dump(obj, depth=0): | |
if isinstance(obj, list): | |
for o in obj: | |
dump(o, depth+1) | |
return | |
fields = [f for f in dir(obj) if (f[0] in string.ascii_lowercase) or f.startswith("_m_")] | |
for f in fields: | |
l.append(getattr(obj,f)) # We need this so the @property method gets actually executed | |
try: | |
dbg = obj._debug[f] | |
if "start" not in obj._debug[f]: | |
dbg = obj._debug["_m_"+f] | |
print("%s- (%04x - %04x) %s" % (".."*depth, dbg["start"], dbg["end"], repr(obj)+"#"+f)) | |
dump(getattr(obj, f), depth+1) | |
except AttributeError: | |
pass | |
except KeyError: | |
# print("-- Key miss at %s %s --" % (obj, f)) | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment