Descriptor of a structure in a memory.
| 81 | |
| 82 | |
| 83 | class Descriptor(object): |
| 84 | """Descriptor of a structure in a memory.""" |
| 85 | |
| 86 | def __init__(self, fields): |
| 87 | self.fields = fields |
| 88 | self.is_flexible = False |
| 89 | for _, type_or_func in fields: |
| 90 | if isinstance(type_or_func, types.FunctionType): |
| 91 | self.is_flexible = True |
| 92 | break |
| 93 | if not self.is_flexible: |
| 94 | self.ctype = Descriptor._GetCtype(fields) |
| 95 | self.size = ctypes.sizeof(self.ctype) |
| 96 | |
| 97 | def Read(self, memory, offset): |
| 98 | if self.is_flexible: |
| 99 | fields_copy = self.fields[:] |
| 100 | last = 0 |
| 101 | for name, type_or_func in fields_copy: |
| 102 | if isinstance(type_or_func, types.FunctionType): |
| 103 | partial_ctype = Descriptor._GetCtype(fields_copy[:last]) |
| 104 | partial_object = partial_ctype.from_buffer(memory, offset) |
| 105 | type = type_or_func(partial_object) |
| 106 | if type is not None: |
| 107 | fields_copy[last] = (name, type) |
| 108 | last += 1 |
| 109 | else: |
| 110 | last += 1 |
| 111 | complete_ctype = Descriptor._GetCtype(fields_copy[:last]) |
| 112 | else: |
| 113 | complete_ctype = self.ctype |
| 114 | return complete_ctype.from_buffer(memory, offset) |
| 115 | |
| 116 | @staticmethod |
| 117 | def _GetCtype(fields): |
| 118 | class Raw(ctypes.Structure): |
| 119 | _fields_ = fields |
| 120 | _pack_ = 1 |
| 121 | |
| 122 | def __str__(self): |
| 123 | return "{" + ", ".join("%s: %s" % (field, self.__getattribute__(field)) |
| 124 | for field, _ in Raw._fields_) + "}" |
| 125 | return Raw |
| 126 | |
| 127 | |
| 128 | def FullDump(reader, heap): |
no outgoing calls
no test coverage detected
searching dependent graphs…