Python _ctypes.Array() Examples

The following are 18 code examples of _ctypes.Array(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module _ctypes , or try the search function .
Example #1
Source File: remotectypes.py    From LKD with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def create_remote_array(subtype, len):

    class RemoteArray(_ctypes.Array):
        _length_ = len
        _type_ = subtype

        def __init__(self, addr, target):
            self._base_addr = addr
            self.target = target

        def __getitem__(self, slice):
            if not isinstance(slice, (int, long)):
                raise NotImplementedError("RemoteArray slice __getitem__")
            if slice >= len:
                raise IndexError("Access to {0} for a RemoteArray of size {1}".format(slice, len))
            item_addr = self._base_addr + (ctypes.sizeof(subtype) * slice)

            # TODO: do better ?
            class TST(ctypes.Structure):
                _fields_ = [("TST", subtype)]
            return RemoteStructure.from_structure(TST)(item_addr, target=self.target).TST
    return RemoteArray


# 64bits pointers 
Example #2
Source File: remotectypes.py    From LKD with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _handle_field_getattr(self, ftype, fosset, fsize):
        s = self._target.read_memory(self._base_addr + fosset, fsize)
        if ftype in self._field_type_to_remote_type:
            return self._field_type_to_remote_type[ftype].from_buffer_with_target(bytearray(s), target=self._target).value
        if issubclass(ftype, _ctypes._Pointer):  # Pointer
            return RemoteStructurePointer.from_buffer_with_target_and_ptr_type(bytearray(s), target=self._target, ptr_type=ftype)
        if issubclass(ftype, RemotePtr64):  # Pointer to remote64 bits process
            return RemoteStructurePointer64.from_buffer_with_target_and_ptr_type(bytearray(s), target=self._target, ptr_type=ftype)
        if issubclass(ftype, RemoteStructureUnion):  # Structure|Union already transfomed in remote
            return ftype(self._base_addr + fosset, self._target)
        if issubclass(ftype, ctypes.Structure):  # Structure that must be transfomed
            return RemoteStructure.from_structure(ftype)(self._base_addr + fosset, self._target)
        if issubclass(ftype, ctypes.Union):  # Union that must be transfomed
            return RemoteUnion.from_structure(ftype)(self._base_addr + fosset, self._target)
        if issubclass(ftype, _ctypes.Array):  # Arrays
            return create_remote_array(ftype._type_, ftype._length_)(self._base_addr + fosset, self._target)
        # Normal types
        # Follow the ctypes usage: if it's not directly inherited from _SimpleCData
        # We do not apply the .value
        # Seems weird but it's mandatory AND useful :D (in pe_parse)
        if _SimpleCData not in ftype.__bases__:
            return ftype.from_buffer(bytearray(s))
        return ftype.from_buffer(bytearray(s)).value 
Example #3
Source File: _dtype_ctypes.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def dtype_from_ctypes_type(t):
    """
    Construct a dtype object from a ctypes type
    """
    if issubclass(t, _ctypes.Array):
        return _from_ctypes_array(t)
    elif issubclass(t, _ctypes._Pointer):
        raise TypeError("ctypes pointers have no dtype equivalent")
    elif issubclass(t, _ctypes.Structure):
        return _from_ctypes_structure(t)
    elif issubclass(t, _ctypes.Union):
        return _from_ctypes_union(t)
    elif isinstance(getattr(t, '_type_', None), str):
        return _from_ctypes_scalar(t)
    else:
        raise NotImplementedError(
            "Unknown ctypes type {}".format(t.__name__)) 
Example #4
Source File: _dtype_ctypes.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def dtype_from_ctypes_type(t):
    """
    Construct a dtype object from a ctypes type
    """
    if issubclass(t, _ctypes.Array):
        return _from_ctypes_array(t)
    elif issubclass(t, _ctypes._Pointer):
        raise TypeError("ctypes pointers have no dtype equivalent")
    elif issubclass(t, _ctypes.Structure):
        return _from_ctypes_structure(t)
    elif issubclass(t, _ctypes.Union):
        return _from_ctypes_union(t)
    elif isinstance(getattr(t, '_type_', None), str):
        return _from_ctypes_scalar(t)
    else:
        raise NotImplementedError(
            "Unknown ctypes type {}".format(t.__name__)) 
Example #5
Source File: _dtype_ctypes.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def dtype_from_ctypes_type(t):
    """
    Construct a dtype object from a ctypes type
    """
    if issubclass(t, _ctypes.Array):
        return _from_ctypes_array(t)
    elif issubclass(t, _ctypes._Pointer):
        raise TypeError("ctypes pointers have no dtype equivalent")
    elif issubclass(t, _ctypes.Structure):
        return _from_ctypes_structure(t)
    elif issubclass(t, _ctypes.Union):
        return _from_ctypes_union(t)
    elif isinstance(getattr(t, '_type_', None), str):
        return _from_ctypes_scalar(t)
    else:
        raise NotImplementedError(
            "Unknown ctypes type {}".format(t.__name__)) 
Example #6
Source File: _dtype_ctypes.py    From recruit with Apache License 2.0 6 votes vote down vote up
def dtype_from_ctypes_type(t):
    """
    Construct a dtype object from a ctypes type
    """
    if issubclass(t, _ctypes.Array):
        return _from_ctypes_array(t)
    elif issubclass(t, _ctypes._Pointer):
        raise TypeError("ctypes pointers have no dtype equivalent")
    elif issubclass(t, _ctypes.Structure):
        return _from_ctypes_structure(t)
    elif issubclass(t, _ctypes.Union):
        return _from_ctypes_union(t)
    elif isinstance(getattr(t, '_type_', None), str):
        return _from_ctypes_scalar(t)
    else:
        raise NotImplementedError(
            "Unknown ctypes type {}".format(t.__name__)) 
Example #7
Source File: _dtype_ctypes.py    From coffeegrindsize with MIT License 6 votes vote down vote up
def dtype_from_ctypes_type(t):
    """
    Construct a dtype object from a ctypes type
    """
    if issubclass(t, _ctypes.Array):
        return _from_ctypes_array(t)
    elif issubclass(t, _ctypes._Pointer):
        raise TypeError("ctypes pointers have no dtype equivalent")
    elif issubclass(t, _ctypes.Structure):
        return _from_ctypes_structure(t)
    elif issubclass(t, _ctypes.Union):
        return _from_ctypes_union(t)
    elif isinstance(getattr(t, '_type_', None), str):
        return _from_ctypes_scalar(t)
    else:
        raise NotImplementedError(
            "Unknown ctypes type {}".format(t.__name__)) 
Example #8
Source File: remotectypes.py    From PythonForWindows with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def is_array(x):
    return isinstance(x, _ctypes.Array) 
Example #9
Source File: _raw_api.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def expect_byte_string(data):
    if not byte_string(data) and not isinstance(data, Array):
        raise TypeError("Only byte strings can be passed to C code") 
Example #10
Source File: _raw_api.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def expect_byte_string(data):
    if not byte_string(data) and not isinstance(data, Array):
        raise TypeError("Only byte strings can be passed to C code") 
Example #11
Source File: _raw_api.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def expect_byte_string(data):
    if not byte_string(data) and not isinstance(data, Array):
        raise TypeError("Only byte strings can be passed to C code") 
Example #12
Source File: remotectypes.py    From PythonForWindows with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _handle_field_getattr(self, ftype, fosset, fsize):
        s = self._target.read_memory(self._base_addr + fosset, fsize)
        if ftype in self._field_type_to_remote_type:
            return self._field_type_to_remote_type[ftype].from_buffer_with_target(bytearray(s), target=self._target).value
        if issubclass(ftype, _ctypes._Pointer):  # Pointer
            return RemoteStructurePointer.from_buffer_with_target_and_ptr_type(bytearray(s), target=self._target, ptr_type=ftype)
        if issubclass(ftype, RemotePtr64):  # Pointer to remote64 bits process
            return RemoteStructurePointer64.from_buffer_with_target_and_ptr_type(bytearray(s), target=self._target, ptr_type=ftype)
        if issubclass(ftype, RemotePtr32):  # Pointer to remote32 bits process
            return RemoteStructurePointer32.from_buffer_with_target_and_ptr_type(bytearray(s), target=self._target, ptr_type=ftype)
        if issubclass(ftype, RemoteStructureUnion):  # Structure|Union already transfomed in remote
            return ftype(self._base_addr + fosset, self._target)
        if issubclass(ftype, ctypes.Structure):  # Structure that must be transfomed
            return RemoteStructure.from_structure(ftype)(self._base_addr + fosset, self._target)
        if issubclass(ftype, ctypes.Union):  # Union that must be transfomed
            return RemoteUnion.from_structure(ftype)(self._base_addr + fosset, self._target)
        if issubclass(ftype, _ctypes.Array):  # Arrays
            # if this is a string: just cast the read value to string
            if ftype._type_ == ctypes.c_char: # Use issubclass instead ?
                return s.split("\x00", 1)[0]
            elif ftype._type_ == ctypes.c_wchar: # Use issubclass instead ?
                # Decode from utf16 -> size /=2 | put it in a wchar array | split at the first "\x00"
                return (ftype._type_ * (fsize / 2)).from_buffer_copy(s.decode('utf16'))[:].split("\x00", 1)[0] # Sorry..
            # I am pretty sur something smarter is possible..
            return create_remote_array(ftype._type_, ftype._length_)(self._base_addr + fosset, self._target)
        # Normal types
        # Follow the ctypes usage: if it's not directly inherited from _SimpleCData
        # We do not apply the .value
        # Seems weird but it's mandatory AND useful :D (in pe_parse)
        if _SimpleCData not in ftype.__bases__:
            return ftype.from_buffer(bytearray(s))
        return ftype.from_buffer(bytearray(s)).value 
Example #13
Source File: remotectypes.py    From PythonForWindows with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def create_remote_array(subtype, len):

    class RemoteArray(_ctypes.Array):
        _length_ = len
        _type_ = subtype

        def __init__(self, addr, target):
            self._base_addr = addr
            self.target = target

        def __getitem__(self, slice):
            # import pdb;pdb.set_trace()
            if not isinstance(slice, int_types):
                raise NotImplementedError("RemoteArray slice __getitem__")
            if slice >= len:
                raise IndexError("Access to {0} for a RemoteArray of size {1}".format(slice, len))
            item_addr = self._base_addr + (ctypes.sizeof(subtype) * slice)

            # TODO: do better ?
            class TST(ctypes.Structure):
                _fields_ = [("TST", subtype)]
            return RemoteStructure.from_structure(TST)(item_addr, target=self.target).TST

        def __getslice__(self, start, stop): # Still used even for python 2.7 wtf :F
            stop = min(stop, len)
            start = max(start, 0)
            # dummy implementation
            return [self[i] for i in range(start, stop)]

    return RemoteArray


# 64bits pointers 
Example #14
Source File: remotectypes.py    From PythonForWindows with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def is_array_type(x):
    return issubclass(x, _ctypes.Array) 
Example #15
Source File: _raw_api.py    From SupergirlOnCrypt with Do What The F*ck You Want To Public License 5 votes vote down vote up
def c_uint8_ptr(data):
        if byte_string(data) or isinstance(data, Array):
            return data
        elif isinstance(data, bytearray):
            local_type = c_ubyte * len(data)
            return local_type.from_buffer(data)
        else:
            raise TypeError("Object type %s cannot be passed to C code" % type(data)) 
Example #16
Source File: _raw_api.py    From SupergirlOnCrypt with Do What The F*ck You Want To Public License 5 votes vote down vote up
def c_uint8_ptr(data):
        if isinstance(data, bytearray):
            # This only works for cffi >= 1.7
            return ffi.cast(uint8_t_type, ffi.from_buffer(data))
        elif byte_string(data) or isinstance(data, Array):
            return data
        else:
            raise TypeError("Object type %s cannot be passed to C code" % type(data)) 
Example #17
Source File: remotectypes.py    From LKD with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def is_array_type(x):
    return issubclass(x, _ctypes.Array) 
Example #18
Source File: remotectypes.py    From LKD with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def is_array(x):
    return isinstance(x, _ctypes.Array)