2024-04-23 18:22:53 -04:00
|
|
|
#!/usr/bin/env python3 -B
|
|
|
|
# coding=utf-8
|
2022-04-01 10:43:22 -04:00
|
|
|
|
2022-04-17 13:48:43 -04:00
|
|
|
"""
|
2024-04-23 18:22:53 -04:00
|
|
|
Copyright (C) 2022-2024 Plato Mavropoulos
|
2022-04-17 13:48:43 -04:00
|
|
|
"""
|
2022-04-01 10:43:22 -04:00
|
|
|
|
|
|
|
import ctypes
|
|
|
|
|
2024-04-23 18:22:53 -04:00
|
|
|
Char: type[ctypes.c_char] | int = ctypes.c_char
|
|
|
|
UInt8: type[ctypes.c_ubyte] | int = ctypes.c_ubyte
|
|
|
|
UInt16: type[ctypes.c_ushort] | int = ctypes.c_ushort
|
|
|
|
UInt32: type[ctypes.c_uint] | int = ctypes.c_uint
|
|
|
|
UInt64: type[ctypes.c_uint64] | int = ctypes.c_uint64
|
|
|
|
|
2022-04-01 10:43:22 -04:00
|
|
|
|
|
|
|
def get_struct(buffer, start_offset, class_name, param_list=None):
|
2024-04-23 18:22:53 -04:00
|
|
|
"""
|
|
|
|
https://github.com/skochinsky/me-tools/blob/master/me_unpack.py by Igor Skochinsky
|
|
|
|
"""
|
|
|
|
|
|
|
|
parameters = [] if param_list is None else param_list
|
|
|
|
|
|
|
|
structure = class_name(*parameters) # Unpack parameter list
|
2022-04-01 10:43:22 -04:00
|
|
|
|
|
|
|
struct_len = ctypes.sizeof(structure)
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-04-01 10:43:22 -04:00
|
|
|
struct_data = buffer[start_offset:start_offset + struct_len]
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-04-01 10:43:22 -04:00
|
|
|
fit_len = min(len(struct_data), struct_len)
|
|
|
|
|
|
|
|
ctypes.memmove(ctypes.addressof(structure), struct_data, fit_len)
|
|
|
|
|
2022-04-15 11:17:58 -04:00
|
|
|
return structure
|