Python ctypes.memset() Examples

The following are 30 code examples of ctypes.memset(). 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: crypto.py    From privacyidea with GNU Affero General Public License v3.0 7 votes vote down vote up
def zerome(bufferObject):
    '''
    clear a string value from memory

    :param bufferObject: the string variable, which should be cleared
    :type  bufferObject: string or key buffer

    :return:    - nothing -
    '''
    data = ctypes.POINTER(ctypes.c_char)()
    size = ctypes.c_int()  # Note, int only valid for python 2.5
    ctypes.pythonapi.PyObject_AsCharBuffer(ctypes.py_object(bufferObject),
                                           ctypes.pointer(data),
                                           ctypes.pointer(size))
    ctypes.memset(data, 0, size.value)

    return 
Example #2
Source File: device.py    From py-uio with MIT License 6 votes vote down vote up
def fill( rgn, length=None, offset=0, value=0 ):
        if value not in range(256):
            raise ValueError( "invalid fill value" )
        if length is None:
            length = rgn.mappable
        # map ctypes instance (does all necessary error checking)
        mem = (ubyte * length).from_buffer( rgn._mmap, offset )
        ctypes.memset( mem, value, length )

    # write data into region at given offset 
Example #3
Source File: c.py    From kernel_tuner with Apache License 2.0 6 votes vote down vote up
def memset(self, allocation, value, size):
        """set the memory in allocation to the value in value

        :param allocation: An Argument for some memory allocation unit
        :type allocation: Argument

        :param value: The value to set the memory to
        :type value: a single 8-bit unsigned int

        :param size: The size of to the allocation unit in bytes
        :type size: int
        """
        C.memset(allocation.ctypes, value, size) 
Example #4
Source File: render.py    From kitty with GNU General Public License v3.0 6 votes vote down vote up
def add_dline(buf: ctypes.Array, cell_width: int, position: int, thickness: int, cell_height: int) -> None:
    a = min(position - thickness, cell_height - 1)
    b = min(position, cell_height - 1)
    top, bottom = min(a, b), max(a, b)
    deficit = 2 - (bottom - top)
    if deficit > 0:
        if bottom + deficit < cell_height:
            bottom += deficit
        elif bottom < cell_height - 1:
            bottom += 1
            if deficit > 1:
                top -= deficit - 1
        else:
            top -= deficit
    top = max(0, min(top, cell_height - 1))
    bottom = max(0, min(bottom, cell_height - 1))
    for y in {top, bottom}:
        ctypes.memset(ctypes.addressof(buf) + (cell_width * y), 255, cell_width) 
Example #5
Source File: sharedctypes.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def RawValue(typecode_or_type, *args):
    '''
    Returns a ctypes object allocated from shared memory
    '''
    type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
    obj = _new_value(type_)
    ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
    obj.__init__(*args)
    return obj 
Example #6
Source File: sharedctypes.py    From unity-python with MIT License 5 votes vote down vote up
def RawArray(typecode_or_type, size_or_initializer):
    '''
    Returns a ctypes array allocated from shared memory
    '''
    type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
    if isinstance(size_or_initializer, (int, long)):
        type_ = type_ * size_or_initializer
        obj = _new_value(type_)
        ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
        return obj
    else:
        type_ = type_ * len(size_or_initializer)
        result = _new_value(type_)
        result.__init__(*size_or_initializer)
        return result 
Example #7
Source File: sharedctypes.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def RawArray(typecode_or_type, size_or_initializer):
    '''
    Returns a ctypes array allocated from shared memory
    '''
    type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
    if isinstance(size_or_initializer, int):
        type_ = type_ * size_or_initializer
        obj = _new_value(type_)
        ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
        return obj
    else:
        type_ = type_ * len(size_or_initializer)
        result = _new_value(type_)
        result.__init__(*size_or_initializer)
        return result 
Example #8
Source File: Memory.py    From PyVM with MIT License 5 votes vote down vote up
def memset(self, offset: int, value: int, size: int) -> int:
        # self.asan_raw(offset, size) -> pasted here for speed
        if offset > self.__size or offset + size > self.__size:
            raise MemoryError(
                f'Not enough memory (tried to write to address range '
                f'0x{offset:08x}-0x{offset + size:08x} ({size} bytes), maximum address: 0x{self.size:08x} bytes)'
            )

        assert offset >= 0, f'Invalid memory address: {hex(offset)}'

        return memset(self.base + offset, value, size) - self.base

    # def set_addr(self, offset: int, size: int, addr: int) -> None:
    #     memmove(self.base + offset, addr, size) 
Example #9
Source File: render.py    From kitty with GNU General Public License v3.0 5 votes vote down vote up
def add_line(buf: ctypes.Array, cell_width: int, position: int, thickness: int, cell_height: int) -> None:
    y = position - thickness // 2
    while thickness > 0 and -1 < y < cell_height:
        thickness -= 1
        ctypes.memset(ctypes.addressof(buf) + (cell_width * y), 255, cell_width)
        y += 1 
Example #10
Source File: test_directsound.py    From pyglet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def filled_buffer(audio_format, buffer_):
    pointer = buffer_.lock(0, 1024)
    if audio_format.sample_size == 8:
        c = 0x80
    else:
        c = 0x00
    ctypes.memset(pointer.audio_ptr_1, c, pointer.audio_length_1.value)
    if pointer.audio_length_2.value > 0:
        ctypes.memset(pointer.audio_ptr_2, c, pointer.audio_length_2.value)

    return buffer_ 
Example #11
Source File: adaptation.py    From pyglet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def write(self, audio_data, length):
        # Pass audio_data=None to write silence
        if length == 0:
            return 0

        write_ptr = self._ds_buffer.lock(self._write_cursor_ring, length)
        assert 0 < length <= self._buffer_size
        assert length == write_ptr.audio_length_1.value + write_ptr.audio_length_2.value

        if audio_data:
            ctypes.memmove(write_ptr.audio_ptr_1, audio_data.data, write_ptr.audio_length_1.value)
            audio_data.consume(write_ptr.audio_length_1.value, self.source.audio_format)
            if write_ptr.audio_length_2.value > 0:
                ctypes.memmove(write_ptr.audio_ptr_2, audio_data.data, write_ptr.audio_length_2.value)
                audio_data.consume(write_ptr.audio_length_2.value, self.source.audio_format)
        else:
            if self.source.audio_format.sample_size == 8:
                c = 0x80
            else:
                c = 0
            ctypes.memset(write_ptr.audio_ptr_1, c, write_ptr.audio_length_1.value)
            if write_ptr.audio_length_2.value > 0:
                ctypes.memset(write_ptr.audio_ptr_2, c, write_ptr.audio_length_2.value)
        self._ds_buffer.unlock(write_ptr)

        self._write_cursor += length
        self._write_cursor_ring += length
        self._write_cursor_ring %= self._buffer_size 
Example #12
Source File: terminal.py    From collection with MIT License 5 votes vote down vote up
def _win32_load_kernel (self):
		if self.unix:
			return False
		import ctypes
		if not self.kernel32:
			self.kernel32 = ctypes.windll.LoadLibrary("kernel32.dll")
		if not self.textdata:
			self.textdata = ctypes.create_string_buffer('0' * 2048)
		ctypes.memset(self.textdata, 0, 2048)
		return True 
Example #13
Source File: fuse.py    From ff4d with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def fgetattr(self, path, buf, fip):
        ctypes.memset(buf, 0, ctypes.sizeof(c_stat))

        st = buf.contents
        if not fip:
            fh = fip
        elif self.raw_fi:
            fh = fip.contents
        else:
            fh = fip.contents.fh

        attrs = self.operations('getattr', self._decode_optional_path(path), fh)
        set_st_attrs(st, attrs, use_ns=self.use_ns)
        return 0 
Example #14
Source File: sharedctypes.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def RawValue(typecode_or_type, *args):
    '''
    Returns a ctypes object allocated from shared memory
    '''
    type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
    obj = _new_value(type_)
    ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
    obj.__init__(*args)
    return obj 
Example #15
Source File: sharedctypes.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def RawArray(typecode_or_type, size_or_initializer):
    '''
    Returns a ctypes array allocated from shared memory
    '''
    type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
    if isinstance(size_or_initializer, (int, long)):
        type_ = type_ * size_or_initializer
        obj = _new_value(type_)
        ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
        return obj
    else:
        type_ = type_ * len(size_or_initializer)
        result = _new_value(type_)
        result.__init__(*size_or_initializer)
        return result 
Example #16
Source File: sharedctypes.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def RawValue(typecode_or_type, *args):
    '''
    Returns a ctypes object allocated from shared memory
    '''
    type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
    obj = _new_value(type_)
    ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
    obj.__init__(*args)
    return obj 
Example #17
Source File: sharedctypes.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def RawArray(typecode_or_type, size_or_initializer):
    '''
    Returns a ctypes array allocated from shared memory
    '''
    type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
    if isinstance(size_or_initializer, int):
        type_ = type_ * size_or_initializer
        obj = _new_value(type_)
        ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
        return obj
    else:
        type_ = type_ * len(size_or_initializer)
        result = _new_value(type_)
        result.__init__(*size_or_initializer)
        return result 
Example #18
Source File: event_trace.py    From PythonForWindows with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def create(cls):
        buff = windows.utils.BUFFER(cls)(size=cls.FULL_SIZE)
        # ctypes.memset(buff, "\x00", cls.FULL_SIZE)
        self = buff[0]
        self.Wnode.BufferSize = cls.FULL_SIZE
        self.LoggerNameOffset = ctypes.sizeof(cls)
        self.LogFileNameOffset = ctypes.sizeof(cls) + MAX_SESSION_NAME_LEN
        return self 
Example #19
Source File: sharedctypes.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def RawValue(typecode_or_type, *args):
    '''
    Returns a ctypes object allocated from shared memory
    '''
    type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
    obj = _new_value(type_)
    ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
    obj.__init__(*args)
    return obj 
Example #20
Source File: sharedctypes.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def RawArray(typecode_or_type, size_or_initializer):
    '''
    Returns a ctypes array allocated from shared memory
    '''
    type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
    if isinstance(size_or_initializer, (int, long)):
        type_ = type_ * size_or_initializer
        obj = _new_value(type_)
        ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
        return obj
    else:
        type_ = type_ * len(size_or_initializer)
        result = _new_value(type_)
        result.__init__(*size_or_initializer)
        return result 
Example #21
Source File: sharedctypes.py    From unity-python with MIT License 5 votes vote down vote up
def RawValue(typecode_or_type, *args):
    '''
    Returns a ctypes object allocated from shared memory
    '''
    type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
    obj = _new_value(type_)
    ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
    obj.__init__(*args)
    return obj 
Example #22
Source File: fusee-launcher.py    From fusee-launcher with GNU General Public License v2.0 5 votes vote down vote up
def ioctl(self, driver_handle: ctypes.c_void_p, ioctl_code: ctypes.c_ulong, input_bytes: ctypes.c_void_p, input_bytes_count: ctypes.c_size_t, output_bytes: ctypes.c_void_p, output_bytes_count: ctypes.c_size_t):
        """ Wrapper for DeviceIoControl """
        overlapped = self.libk.OVERLAPPED()
        ctypes.memset(ctypes.addressof(overlapped), 0, ctypes.sizeof(overlapped))

        ret = ctypes.windll.kernel32.DeviceIoControl(driver_handle, ioctl_code, input_bytes, input_bytes_count, output_bytes, output_bytes_count, None, ctypes.byref(overlapped))

        # We expect this to error, which matches the others ^_^
        if ret == False:
            raise ctypes.WinError() 
Example #23
Source File: sharedctypes.py    From oss-ftp with MIT License 5 votes vote down vote up
def RawArray(typecode_or_type, size_or_initializer):
    '''
    Returns a ctypes array allocated from shared memory
    '''
    type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
    if isinstance(size_or_initializer, (int, long)):
        type_ = type_ * size_or_initializer
        obj = _new_value(type_)
        ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
        return obj
    else:
        type_ = type_ * len(size_or_initializer)
        result = _new_value(type_)
        result.__init__(*size_or_initializer)
        return result 
Example #24
Source File: system.py    From stem with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _set_argv(process_name: str) -> None:
  """
  Overwrites our argv in a similar fashion to how it's done in C with:
  strcpy(argv[0], 'new_name');
  """

  if Py_GetArgcArgv is None:
    return

  global _PROCESS_NAME

  # both gets the current process name and initializes _MAX_NAME_LENGTH

  current_name = get_process_name()

  argv, argc = ctypes.c_int(0), argc_t()
  Py_GetArgcArgv(argv, ctypes.pointer(argc))

  if len(process_name) > _MAX_NAME_LENGTH:
    raise IOError("Can't rename process to something longer than our initial name (this would overwrite memory used for the env)")

  # space we need to clear
  zero_size = max(len(current_name), len(process_name))

  ctypes.memset(argc.contents, 0, zero_size + 1)  # null terminate the string's end
  process_name_encoded = process_name.encode('utf8')
  ctypes.memmove(argc.contents, process_name_encoded, len(process_name))
  _PROCESS_NAME = process_name 
Example #25
Source File: sharedctypes.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def RawValue(typecode_or_type, *args):
    '''
    Returns a ctypes object allocated from shared memory
    '''
    type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
    obj = _new_value(type_)
    ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
    obj.__init__(*args)
    return obj 
Example #26
Source File: sharedctypes.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def RawArray(typecode_or_type, size_or_initializer):
    '''
    Returns a ctypes array allocated from shared memory
    '''
    type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
    if isinstance(size_or_initializer, (int, long)):
        type_ = type_ * size_or_initializer
        obj = _new_value(type_)
        ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
        return obj
    else:
        type_ = type_ * len(size_or_initializer)
        result = _new_value(type_)
        result.__init__(*size_or_initializer)
        return result 
Example #27
Source File: terminal.py    From terminal with MIT License 5 votes vote down vote up
def _win32_load_kernel (self):
		if self.unix:
			return False
		import ctypes
		if not self.kernel32:
			self.kernel32 = ctypes.windll.LoadLibrary("kernel32.dll")
		if not self.textdata:
			self.textdata = ctypes.create_string_buffer('0' * 2048)
		ctypes.memset(self.textdata, 0, 2048)
		return True 
Example #28
Source File: sharedctypes.py    From BinderFilter with MIT License 5 votes vote down vote up
def RawValue(typecode_or_type, *args):
    '''
    Returns a ctypes object allocated from shared memory
    '''
    type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
    obj = _new_value(type_)
    ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
    obj.__init__(*args)
    return obj 
Example #29
Source File: sharedctypes.py    From BinderFilter with MIT License 5 votes vote down vote up
def RawArray(typecode_or_type, size_or_initializer):
    '''
    Returns a ctypes array allocated from shared memory
    '''
    type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
    if isinstance(size_or_initializer, (int, long)):
        type_ = type_ * size_or_initializer
        obj = _new_value(type_)
        ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
        return obj
    else:
        type_ = type_ * len(size_or_initializer)
        result = _new_value(type_)
        result.__init__(*size_or_initializer)
        return result 
Example #30
Source File: clienttest.py    From Computable with MIT License 5 votes vote down vote up
def segfault():
    """this will segfault"""
    import ctypes
    ctypes.memset(-1,0,1)