Prepare structure object to extract members from data. Format is a list containing definitions for the elements of the structure.
| 942 | |
| 943 | |
| 944 | class Structure: |
| 945 | """Prepare structure object to extract members from data. |
| 946 | |
| 947 | Format is a list containing definitions for the elements |
| 948 | of the structure. |
| 949 | """ |
| 950 | |
| 951 | def __init__(self, format, name=None, file_offset=None): |
| 952 | # Format is forced little endian, for big endian non Intel platforms |
| 953 | self.__format__ = "<" |
| 954 | self.__keys__ = [] |
| 955 | self.__format_length__ = 0 |
| 956 | self.__field_offsets__ = {} |
| 957 | self.__unpacked_data_elms__ = [] |
| 958 | |
| 959 | d = format[1] |
| 960 | # need a tuple to be hashable in set_format using lru cache |
| 961 | if not isinstance(d, tuple): |
| 962 | d = tuple(d) |
| 963 | |
| 964 | ( |
| 965 | self.__format__, |
| 966 | self.__unpacked_data_elms__, |
| 967 | self.__field_offsets__, |
| 968 | self.__keys__, |
| 969 | self.__format_length__, |
| 970 | ) = set_format(d) |
| 971 | |
| 972 | self.__all_zeroes__ = False |
| 973 | self.__file_offset__ = file_offset |
| 974 | if name: |
| 975 | self.name = name |
| 976 | else: |
| 977 | self.name = format[0] |
| 978 | |
| 979 | def __get_format__(self): |
| 980 | return self.__format__ |
| 981 | |
| 982 | def get_field_absolute_offset(self, field_name): |
| 983 | """Return the offset within the field for the requested field in the structure.""" |
| 984 | return self.__file_offset__ + self.__field_offsets__[field_name] |
| 985 | |
| 986 | def get_field_relative_offset(self, field_name): |
| 987 | """Return the offset within the structure for the requested field.""" |
| 988 | return self.__field_offsets__[field_name] |
| 989 | |
| 990 | def get_file_offset(self): |
| 991 | return self.__file_offset__ |
| 992 | |
| 993 | def set_file_offset(self, offset): |
| 994 | self.__file_offset__ = offset |
| 995 | |
| 996 | def all_zeroes(self): |
| 997 | """Returns true is the unpacked data is all zeros.""" |
| 998 | |
| 999 | return self.__all_zeroes__ |
| 1000 | |
| 1001 | def sizeof(self): |
no outgoing calls
no test coverage detected