def getpwnam(user): passwd = getpwnam_(user) if not passwd: raise KeyError("getpwnam(): name not found: {}".format(user)) passwd_fmt = "SSIISSS" passwd = uctypes.bytes_at(passwd, ustruct.calcsize(passwd_fmt)) passwd = ustruct.unpack(passwd_fmt, passwd) return struct_passwd(*passwd)
def __init__(self): dict.__init__(self) env = uctypes.struct(_environ_ptr.get(), _ENV_STRUCT) for i in range(4096): if int(env.arr[i]) == 0: break s = uctypes.bytes_at(env.arr[i]).decode() k, v = s.split("=", 1) dict.__setitem__(self, k, v)
def ctime(t=None): # Sun Jun 20 23:21:05 1993 ctime_ = libc.func("P", "ctime", "P") if t is None: t = time() t = int(t) ref = ustruct.pack('l', t) p = ctime_(ref) return uctypes.bytes_at(p, _libc.strlen(p) - 1).decode()
def get_header_value(fld_name): # get a single value, raw, from header; based on field name from sigheader import FLASH_HEADER_BASE, FW_HEADER_SIZE, FWH_PY_FORMAT, FWH_PY_VALUES import ustruct, uctypes idx = FWH_PY_VALUES.split().index(fld_name) hdr = uctypes.bytes_at(FLASH_HEADER_BASE, FW_HEADER_SIZE) return ustruct.unpack_from(FWH_PY_FORMAT, hdr)[idx]
def compress(data, level=-1): dest_buf_sz = compressBound_(len(data)) buf = malloc_(dest_buf_sz) assert buf is not None dest_sz_ref = ffilib.makeref("L", dest_buf_sz) res = compress2_(buf, dest_sz_ref, data, len(data), level) assert res == 0 out = uctypes.bytes_at(buf, dest_sz_ref[0]) free_(buf) return out
def ilistdir(path="."): dir = opendir_(path) if not dir: raise_error() res = [] dirent_fmt = "LLHB256s" while True: dirent = readdir_(dir) if not dirent: break import uctypes dirent = uctypes.bytes_at(dirent, struct.calcsize(dirent_fmt)) dirent = struct.unpack(dirent_fmt, dirent) dirent = (dirent[-1].split(b'\0', 1)[0], dirent[-2], dirent[0]) yield dirent
def get_mpy_version(): # read my own file header # see stm32/bootloader/sigheader.h from sigheader import FLASH_HEADER_BASE, FW_HEADER_SIZE, FWH_PY_FORMAT import ustruct, uctypes try: # located in flash, but could also use RAM version hdr = uctypes.bytes_at(FLASH_HEADER_BASE, FW_HEADER_SIZE) magic_value, timestamp, version_string = ustruct.unpack_from(FWH_PY_FORMAT, hdr)[0:3] parts = ['%02x'%i for i in timestamp] date = '20' + '-'.join(parts[0:3]) vers = bytes(version_string).rstrip(b'\0').decode() return date, vers, ''.join(parts[:-2]) except: # this is early in boot process, so don't fail! return '20YY-MM-DD', '?.??', '180731121314'
try: import uctypes except ImportError: print("SKIP") raise SystemExit data = bytearray(b"01234567") print(uctypes.bytes_at(uctypes.addressof(data), 4)) print(uctypes.bytearray_at(uctypes.addressof(data), 4)) print(uctypes.bytes_at(uctypes.addressof(data) + 2))
async def check_firewall_read(*a): import uctypes ps = uctypes.bytes_at(0x7800, 32) assert False # should not be reached
def get_hardware_rng(): """ Return a 32-bit harware generated random number. According to documentation """ return uctypes.bytes_at(RNG_DATA_REG, RNG_DATA_BYTES)
async def dispatch_DebugLinkMemoryRead(ctx, msg): from trezor.messages.DebugLinkMemory import DebugLinkMemory from uctypes import bytes_at m = DebugLinkMemory() m.memory = bytes_at(msg.address, msg.length) return m
async def dispatch_DebugLinkMemoryRead(ctx, msg): m = DebugLinkMemory() m.memory = bytes_at(msg.address, msg.length) return m
try: import uctypes except ImportError: print("SKIP") raise SystemExit data = bytearray(b'01234567') print(uctypes.bytes_at(uctypes.addressof(data), 4)) print(uctypes.bytearray_at(uctypes.addressof(data), 4))