BIOSUtilities/biosutilities/common/structs.py
Plato Mavropoulos 40553b3244 BIOSUtilities v25.02.23
Fixed AMI PFAT Extract file naming of "ALL" and "OOB"
Upgraded dependency "dissect.util" from 3.18 to 3.19
2025-02-24 00:42:26 +02:00

38 lines
942 B
Python

#!/usr/bin/env python3 -B
# coding=utf-8
"""
Copyright (C) 2022-2025 Plato Mavropoulos
"""
import ctypes
from typing import Any, Final
CHAR: Final[Any] = ctypes.c_char
UINT8: Final[Any] = ctypes.c_ubyte
UINT16: Final[Any] = ctypes.c_ushort
UINT32: Final[Any] = ctypes.c_uint
UINT64: Final[Any] = ctypes.c_uint64
def ctypes_struct(buffer: bytes | bytearray, start_offset: int, class_object: Any,
param_list: list | None = None) -> Any:
"""
https://github.com/skochinsky/me-tools/blob/master/me_unpack.py by Igor Skochinsky
"""
if not param_list:
param_list = []
structure: Any = class_object(*param_list)
struct_len: int = ctypes.sizeof(structure)
struct_data: bytes | bytearray = buffer[start_offset:start_offset + struct_len]
least_len: int = min(len(struct_data), struct_len)
ctypes.memmove(ctypes.addressof(structure), struct_data, least_len)
return structure