|
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 3 | +# You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | +# |
| 5 | +# Copyright (c) 2014-2019, Lars Asplund lars.anders.asplund@gmail.com |
| 6 | + |
| 7 | +""" |
| 8 | +Utils for co-execution of a dynamically loadable binaries or shared libraries |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import print_function |
| 12 | +import ctypes |
| 13 | +import _ctypes |
| 14 | +from sys import platform |
| 15 | +from os.path import isfile |
| 16 | + |
| 17 | + |
| 18 | +def dlopen(path): |
| 19 | + """ |
| 20 | + Open/load a PIE binary or a shared library. |
| 21 | + """ |
| 22 | + if not isfile(path): |
| 23 | + print('Executable binary not found: ' + path) |
| 24 | + exit(1) |
| 25 | + try: |
| 26 | + return ctypes.CDLL(path) |
| 27 | + except OSError: |
| 28 | + print('Loading executables dynamically seems not to be supported on this platform') |
| 29 | + exit(1) |
| 30 | + |
| 31 | + |
| 32 | +def dlclose(obj): |
| 33 | + """ |
| 34 | + Close/unload a PIE binary or a shared library. |
| 35 | +
|
| 36 | + :param obj: object returned by ctypes.CDLL when the resource was loaded |
| 37 | + """ |
| 38 | + if platform == 'linux': |
| 39 | + _ctypes.dlclose(obj._handle) # pylint:disable=protected-access |
| 40 | + # else: |
| 41 | + # _ctypes.FreeLibrary(obj._handle) # pylint:disable=protected-access |
| 42 | + |
| 43 | + |
| 44 | +def enc_args(args): |
| 45 | + """ |
| 46 | + Convert args to a suitable format for a foreign C function. |
| 47 | +
|
| 48 | + :param args: list of strings |
| 49 | + """ |
| 50 | + xargs = (ctypes.POINTER(ctypes.c_char) * len(args))() |
| 51 | + for idx, arg in enumerate(args): |
| 52 | + xargs[idx] = ctypes.create_string_buffer(arg.encode('utf-8')) |
| 53 | + return xargs |
| 54 | + |
| 55 | + |
| 56 | +def byte_buf(lst): |
| 57 | + """ |
| 58 | + Convert array to a string buffer (uint8_t* in C). |
| 59 | +
|
| 60 | + :param lst: list of naturals range [0,255] |
| 61 | + """ |
| 62 | + return ctypes.create_string_buffer(bytes(lst), len(lst)) |
| 63 | + |
| 64 | + |
| 65 | +def int_buf(lst, bpw=4, signed=True): |
| 66 | + """ |
| 67 | + Convert array to a string buffer (uint8_t* in C). |
| 68 | +
|
| 69 | + :param lst: list of integers |
| 70 | + :param bpw: number of bytes per word/integer |
| 71 | + :param signed: whether to encode the numbers as signed |
| 72 | + """ |
| 73 | + out = [None] * 4 * len(lst) |
| 74 | + for idx, val in enumerate(lst): |
| 75 | + out[idx * 4:idx * 4 + 4] = (val).to_bytes(bpw, byteorder="little", signed=signed) |
| 76 | + return byte_buf(out) |
| 77 | + |
| 78 | + |
| 79 | +def read_byte_buf(buf): |
| 80 | + """ |
| 81 | + Read byte/string buffer (uint8_t* in C) as a list of numbers. |
| 82 | + """ |
| 83 | + return read_int_buf(buf, bpw=1, signed=False) |
| 84 | + |
| 85 | + |
| 86 | +def read_int_buf(buf, bpw=4, signed=True): |
| 87 | + """ |
| 88 | + Read byte/string buffer as a list of numbers. |
| 89 | +
|
| 90 | + :param buf: byte/string buffer (uint8_t* in C) to read from |
| 91 | + :param bpw: number of bytes per word/integer |
| 92 | + :param signed: whether to decode the numbers as signed |
| 93 | + """ |
| 94 | + out = [None] * int(len(buf) / bpw) |
| 95 | + if bpw == 1: |
| 96 | + for idx, val in enumerate(buf): |
| 97 | + out[idx] = int.from_bytes(val, byteorder="little", signed=signed) |
| 98 | + else: |
| 99 | + for idx, _ in enumerate(out): |
| 100 | + out[idx] = int.from_bytes(buf[idx * bpw:idx * bpw + bpw], byteorder="little", signed=signed) |
| 101 | + return out |
0 commit comments