Пример #1
0
def unpack(ctypes_obj, string, bytes=None):
    """Unpack a python string into a ctypes structure

    Args:
        ctypes_obj (ctypes.Structure):  ctypes structure to pack into

        string (str):  String to copy over the ctypes_obj memory space

    Kwargs:
        bytes: Number of bytes to copy

    Raises:
        ValueError, MemoryError

    If the length of the string is not the correct size for the memory
    footprint of the ctypes structure then the bytes keyword argument must
    be used
    """
    if bytes is None:
        if len(string) != ctypes.sizeof(ctypes_obj):
            raise ValueError("Attempt to unpack a string of size %d into a \
                struct of size %d" % (len(string), ctypes.sizeof(ctypes_obj)))
        bytes = len(string)

    if bytes > ctypes.sizeof(ctypes_obj):
        raise MemoryError("Attempt to unpack %d bytes over an object \
                        of size %d" % (bytes, ctypes.sizeof(ctypes_obj)))

    ctypes.memmove(ctypes.addressof(ctypes_obj), string, bytes)
Пример #2
0
    def load_static_sym(self):
        # http://wiki.osdev.org/COFF
        # http://www.delorie.com/djgpp/doc/coff/symtab.html

        sym_table_off = self.pe.FILE_HEADER.PointerToSymbolTable
        n_sym = self.pe.FILE_HEADER.NumberOfSymbols
        string_table_off = sym_table_off + sizeof(SymbolEntry) * n_sym
        base = self.pe.OPTIONAL_HEADER.ImageBase + \
               self.pe.OPTIONAL_HEADER.SectionAlignment

        off = sym_table_off
        i = 0

        while i < n_sym:
            sym = self.pe.get_sym_at_offset(off)

            if sym.sclass == 2:  # static symbol
                name = \
                    sym.sym.name.decode() if sym.sym.addr.zeroes != 0 else \
                    self.pe.get_string_at_offset(string_table_off + \
                                                 sym.sym.addr.offset)

                # print("%d   %s" % (sym.scnum, name))

                self.classbinary.reverse_symbols[sym.value + base] = name
                self.classbinary.symbols[name] = sym.value + base
                
            if sym.numaux != 0:
                off += sym.numaux * sizeof(SymbolEntry)
                i += sym.numaux

            off += sizeof(SymbolEntry)
            i += 1
Пример #3
0
    def win_pick(window, starting_color):
        paste = None
        start_color = None
        if starting_color is not None:
            start_color = hexstr_to_bgr(starting_color[1:])
        s = sublime.load_settings("ColorPicker.sublime-settings")
        custom_colors = s.get("custom_colors", ['0'] * 16)

        if len(custom_colors) < 16:
            custom_colors = ['0'] * 16
            s.set('custom_colors', custom_colors)

        cc = CHOOSECOLOR()
        ctypes.memset(ctypes.byref(cc), 0, ctypes.sizeof(cc))
        cc.lStructSize = ctypes.sizeof(cc)

        if sublime_version == 2:
            cc.hwndOwner = window.hwnd()
        else:
            # Temporary fix for Sublime Text 3 - For some reason the hwnd crashes it
            # Of course, clicking out of the colour picker and into Sublime will make
            # Sublime not respond, but as soon as you exit the colour picker it's ok
            cc.hwndOwner = None

        cc.Flags = CC_SOLIDCOLOR | CC_FULLOPEN | CC_RGBINIT
        cc.rgbResult = c_uint32(start_color) if not paste and start_color else get_pixel()
        cc.lpCustColors = to_custom_color_array(custom_colors)

        if ChooseColorW(ctypes.byref(cc)):
            color = bgr_to_hexstr(cc.rgbResult)
        else:
            color = None

        return color
def _task_list():
    psapi = ctypes.windll.psapi
    kernel = ctypes.windll.kernel32

    hModule = ctypes.c_ulong()
    count = ctypes.c_ulong()
    modname = ctypes.c_buffer(30)
    PROCESS_QUERY_INFORMATION = 0x0400
    PROCESS_VM_READ = 0x0010

    pid_list = win32process.EnumProcesses()
    info_list = []
    
    for pid in pid_list:
        
        hProcess = kernel.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, False, pid)
        if hProcess:
            psapi.EnumProcessModules(hProcess, ctypes.byref(hModule), ctypes.sizeof(hModule), ctypes.byref(count))
            psapi.GetModuleBaseNameA(hProcess, hModule.value, modname, ctypes.sizeof(modname))
            pname = ctypes.string_at(modname)

            procmeminfo = win32process.GetProcessMemoryInfo(hProcess)
            procmemusage = (procmeminfo["WorkingSetSize"]/1024)
            info_list.append((pid, pname, procmemusage))
                        
            kernel.CloseHandle(hProcess)

    return info_list
Пример #5
0
def get_pids(process_name):
    BIG_ARRAY = DWORD * 4096
    processes = BIG_ARRAY()
    needed = DWORD()

    pids = []
    result = windll.psapi.EnumProcesses(processes, sizeof(processes), addressof(needed))
    if not result:
        return pids

    num_results = needed.value / sizeof(DWORD)

    for i in range(num_results):
        pid = processes[i]
        process = windll.kernel32.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, pid)
        if process:
            module = HANDLE()
            result = windll.psapi.EnumProcessModules(process, addressof(module), sizeof(module), addressof(needed))
            if result:
                name = create_unicode_buffer(1024)
                result = windll.psapi.GetModuleBaseNameW(process, module, name, len(name))
                # TODO: This might not be the best way to
                # match a process name; maybe use a regexp instead.
                if name.value.startswith(process_name):
                    pids.append(pid)
                windll.kernel32.CloseHandle(module)
            windll.kernel32.CloseHandle(process)

    return pids
Пример #6
0
def hash_data(buffer, alg, block_size=16*1024):
    if alg not in _calg_map:
        raise InvalidHashAlgorithm()
    
    block_size = int(block_size)
    if block_size <= 0:
        raise ValueError("'block_size' should be a positive integer")

    with _crypt_context() as hCryptProv:
        with _crypt_hash(hCryptProv, _calg_map[alg]) as hCryptHash:
            buffer_len = len(buffer)
            if block_size > buffer_len:
                block_size = buffer_len

            i = 0
            while i < buffer_len:
                count = block_size if (i + block_size <= buffer_len) else (buffer_len - i)
                if not CryptHashData(hCryptHash, _cast(buffer[i:i+count], PBYTE), DWORD(count), DWORD(0)):
                    raise WinError()
                i += count

            dwHashLen = DWORD(sizeof(DWORD))
            dwHashSize = DWORD()
            if not CryptGetHashParam(hCryptHash, HP_HASHSIZE, _cast(pointer(dwHashSize), PBYTE), pointer(dwHashLen), DWORD(0)):
                raise WinError()

            pbHashVal = (BYTE * dwHashSize.value)()
            dwHashLen.value = sizeof(pbHashVal)
            if not CryptGetHashParam(hCryptHash, HP_HASHVAL, pbHashVal, pointer(dwHashLen), DWORD(0)):
                raise WinError()

            return ''.join(map(lambda b: format(b, '02x'), bytes(pbHashVal)))
Пример #7
0
    def read_fts_container(self, filepath):
        f = open(filepath, "rb")
        data = f.read()
        f.close()

        self.log.debug("Loaded %i bytes from file %s" % (len(data), filepath))
        
        pos = 0
        
        primaryHeader = UNIQUE_HEADER.from_buffer_copy(data, pos)
        pos += sizeof(UNIQUE_HEADER)
        self.log.debug("Header path: %s" % primaryHeader.path.decode('iso-8859-1'))
        self.log.debug("Header count: %i" % primaryHeader.count)
        self.log.debug("Header version: %f" % primaryHeader.version)
        self.log.debug("Header uncompressedsize: %i" % primaryHeader.uncompressedsize)
            
        secondaryHeadersType = UNIQUE_HEADER3 * primaryHeader.count
        secondaryHeaders = secondaryHeadersType.from_buffer_copy(data, pos)
        pos += sizeof(secondaryHeadersType)
        
        for h in secondaryHeaders:
            self.log.debug("Header2 path: %s" % h.path.decode('iso-8859-1'))
        
        uncompressed = self.ioLib.unpack(data[pos:])
        
        if primaryHeader.uncompressedsize != len(uncompressed):
            self.log.warn("Uncompressed size mismatch, expected %i actual %i" % (primaryHeader.uncompressedsize, len(uncompressed)))
        
        return self.read_fts(uncompressed)
    def _readSMART(self, name):
        serial = 'NO SERIAL NUMBER'
        handle = ctypes.windll.kernel32.CreateFileW(
            name,
            win32file.GENERIC_READ | win32file.GENERIC_WRITE,
            win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE,
            None, win32file.OPEN_EXISTING, 0, 0)
        if handle == -1:
            logging.warning("Failed to open device '%s' for querying the "
                            "serial number. Error code: %d", name,
                            win32api.GetLastError())
            return serial
        q = StoragePropertyQuery()
        r = StorageDeviceDescriptor()
        read_count = ctypes.wintypes.ULONG()
        ret = ctypes.windll.kernel32.DeviceIoControl(
            handle, 0x002D1400, ctypes.addressof(q), ctypes.sizeof(q),
            ctypes.addressof(r), ctypes.sizeof(r),
            ctypes.addressof(read_count), 0)

        if ret:
            serial = buffer(r)[r.SerialNumberOffset:r.SerialNumberOffset + 20]
        else:
            logging.warning("DeviceIoControl for device %s failed with"
                            "eror code: %d. Could not look up serial number",
                            name, win32api.GetLastError())
        ctypes.windll.kernel32.CloseHandle(handle)
        return serial
Пример #9
0
    def copy_windows(text):
        # This function is heavily based on
        # http://msdn.com/ms649016#_win32_Copying_Information_to_the_Clipboard
        with window() as hwnd:
            # http://msdn.com/ms649048
            # If an application calls OpenClipboard with hwnd set to NULL,
            # EmptyClipboard sets the clipboard owner to NULL;
            # this causes SetClipboardData to fail.
            # => We need a valid hwnd to copy something.
            with clipboard(hwnd):
                safeEmptyClipboard()

                if text:
                    # http://msdn.com/ms649051
                    # If the hMem parameter identifies a memory object,
                    # the object must have been allocated using the
                    # function with the GMEM_MOVEABLE flag.
                    count = wcslen(text) + 1
                    handle = safeGlobalAlloc(GMEM_MOVEABLE,
                                             count * sizeof(c_wchar))
                    locked_handle = safeGlobalLock(handle)

                    ctypes.memmove(c_wchar_p(locked_handle), c_wchar_p(text),
                                   count * sizeof(c_wchar))

                    safeGlobalUnlock(handle)
                    safeSetClipboardData(CF_UNICODETEXT, handle)
Пример #10
0
	def _gen_ehdr(self, pid, phdrs):
		"""
		Generate elf header for process pid with program headers phdrs.
		"""
		ehdr = elf.Elf64_Ehdr()

		ctypes.memset(ctypes.addressof(ehdr), 0, ctypes.sizeof(ehdr))
		ehdr.e_ident[elf.EI_MAG0]	= elf.ELFMAG0
		ehdr.e_ident[elf.EI_MAG1]	= elf.ELFMAG1
		ehdr.e_ident[elf.EI_MAG2]	= elf.ELFMAG2
		ehdr.e_ident[elf.EI_MAG3]	= elf.ELFMAG3
		ehdr.e_ident[elf.EI_CLASS]	= elf.ELFCLASS64
		ehdr.e_ident[elf.EI_DATA]	= elf.ELFDATA2LSB
		ehdr.e_ident[elf.EI_VERSION]	= elf.EV_CURRENT

		ehdr.e_type		= elf.ET_CORE
		ehdr.e_machine		= elf.EM_X86_64
		ehdr.e_version		= elf.EV_CURRENT
		ehdr.e_phoff		= ctypes.sizeof(elf.Elf64_Ehdr())
		ehdr.e_ehsize		= ctypes.sizeof(elf.Elf64_Ehdr())
		ehdr.e_phentsize	= ctypes.sizeof(elf.Elf64_Phdr())
		#FIXME Case len(phdrs) > PN_XNUM should be handled properly.
		# See fs/binfmt_elf.c from linux kernel.
		ehdr.e_phnum	= len(phdrs)

		return ehdr
Пример #11
0
	def _gen_fpregset(self, pid, tid):
		"""
		Generate NT_FPREGSET note for thread tid of process pid.
		"""
		core	= self.cores[tid]
		regs	= core["thread_info"]["fpregs"]

		fpregset = elf.elf_fpregset_t()
		ctypes.memset(ctypes.addressof(fpregset), 0, ctypes.sizeof(fpregset))

		fpregset.cwd		= regs["cwd"]
		fpregset.swd		= regs["swd"]
		fpregset.ftw		= regs["twd"]
		fpregset.fop		= regs["fop"]
		fpregset.rip		= regs["rip"]
		fpregset.rdp		= regs["rdp"]
		fpregset.mxcsr		= regs["mxcsr"]
		fpregset.mxcr_mask	= regs["mxcsr_mask"]
		fpregset.st_space	= (ctypes.c_uint * len(regs["st_space"]))(*regs["st_space"])
		fpregset.xmm_space	= (ctypes.c_uint * len(regs["xmm_space"]))(*regs["xmm_space"])
		#fpregset.padding	= regs["padding"] unused

		nhdr = elf.Elf64_Nhdr()
		nhdr.n_namesz	= 5
		nhdr.n_descsz	= ctypes.sizeof(elf.elf_fpregset_t())
		nhdr.n_type	= elf.NT_FPREGSET

		note = elf_note()
		note.data	= fpregset
		note.owner	= "CORE"
		note.nhdr 	= nhdr

		return note
Пример #12
0
    def get_cdrom_drive_mount_point(self):

        mount_point = None

        buf = ctypes.create_unicode_buffer(2048)
        buf_len = kernel32.GetLogicalDriveStringsW(ctypes.sizeof(buf) / ctypes.sizeof(wintypes.WCHAR), buf)
        if not buf_len:
            raise Exception("Cannot enumerate logical devices")

        cdrom_dev = self.get_physical_path().rsplit("\\")[-1].upper()

        i = 0
        while not mount_point and i < buf_len:
            curr_drive = ctypes.wstring_at(ctypes.addressof(buf) + i * ctypes.sizeof(wintypes.WCHAR))[:-1]

            dev = ctypes.create_unicode_buffer(2048)
            ret_val = kernel32.QueryDosDeviceW(curr_drive, dev, ctypes.sizeof(dev) / ctypes.sizeof(wintypes.WCHAR))
            if not ret_val:
                raise Exception("Cannot query NT device")

            if dev.value.rsplit("\\")[-1].upper() == cdrom_dev:
                mount_point = curr_drive
            else:
                i += len(curr_drive) + 2

        return mount_point
Пример #13
0
def _get_properties(properties, length): 
    """
    Convenience Function to get the material properties as a dict
    and values in a python format.
    """
    result = {}
    #read all properties
    for p in [properties[i] for i in range(length)]:
        #the name
        p = p.contents
        key = (str(p.mKey.data.decode("utf-8")).split('.')[1], p.mSemantic)

        #the data
        from ctypes import POINTER, cast, c_int, c_float, sizeof
        if p.mType == 1:
            arr = cast(p.mData, POINTER(c_float * int(p.mDataLength/sizeof(c_float)) )).contents
            value = [x for x in arr]
        elif p.mType == 3: #string can't be an array
            value = cast(p.mData, POINTER(structs.MaterialPropertyString)).contents.data.decode("utf-8")
        elif p.mType == 4:
            arr = cast(p.mData, POINTER(c_int * int(p.mDataLength/sizeof(c_int)) )).contents
            value = [x for x in arr]
        else:
            value = p.mData[:p.mDataLength]

        if len(value) == 1:
            [value] = value

        result[key] = value

    return PropertyGetter(result)
Пример #14
0
def getBGR32(dc, bitmap):
	"""
	Returns a (raw BGR str, (width, height)) for C{dc}, C{bitmap}.
	Guaranteed to be 32-bit.  Note that the origin of the returned image is
	in the bottom-left corner, and the image has 32-bit line padding.
	"""
	bmpInfo = bitmap.GetInfo()
	width, height = bmpInfo['bmWidth'], bmpInfo['bmHeight']

	bmi = BITMAPINFO()
	ctypes.memset(ctypes.byref(bmi), 0x00, ctypes.sizeof(bmi))
	bmi.bmiHeader.biSize = ctypes.sizeof(BITMAPINFOHEADER)
	bmi.bmiHeader.biWidth = width
	bmi.bmiHeader.biHeight = height
	bmi.bmiHeader.biBitCount = 24
	bmi.bmiHeader.biPlanes = 1

	bufferLen = height * ((width * 3 + 3) & -4)
	pbBits = ctypes.create_string_buffer(bufferLen)

	ret = ctypes.windll.gdi32.GetDIBits(
		dc.GetHandleAttrib(),
		bitmap.GetHandle(),
		0,
		height,
		ctypes.byref(pbBits),
		ctypes.pointer(bmi),
		win32con.DIB_RGB_COLORS)
	if ret == 0:
		raise DIBFailed("Return code 0 from GetDIBits")

	assert len(pbBits.raw) == bufferLen, len(pbBits.raw)

	return pbBits.raw, (width, height)
Пример #15
0
Файл: pmda.py Проект: tongfw/pcp
 def run(self):
     """
     All the real work happens herein; we can be called in one of three
     situations, determined by environment variables.  First couple are
     during the agent Install process, where the domain.h and namespace
     files need to be created.  The third case is the real mccoy, where
     an agent is actually being started by pmcd/dbpmda and makes use of
     libpcp_pmda to talk PCP protocol.
     """
     if ('PCP_PYTHON_DOMAIN' in os.environ):
         self.domain_write()
     elif ('PCP_PYTHON_PMNS' in os.environ):
         self.pmns_write(os.environ['PCP_PYTHON_PMNS'])
     else:
         self.pmns_refresh()
         cpmda.pmid_oneline_refresh(self._metric_oneline)
         cpmda.pmid_longtext_refresh(self._metric_helptext)
         cpmda.indom_oneline_refresh(self._indom_oneline)
         cpmda.indom_longtext_refresh(self._indom_helptext)
         numindoms = len(self._indomtable)
         ibuf = create_string_buffer(numindoms * sizeof(pmdaIndom))
         indoms = cast(ibuf, POINTER(pmdaIndom))
         for i in xrange(numindoms):
             indoms[i] = self._indomtable[i]
         nummetrics = len(self._metrictable)
         mbuf = create_string_buffer(nummetrics * sizeof(pmdaMetric))
         metrics = cast(mbuf, POINTER(pmdaMetric))
         for i in xrange(nummetrics):
             metrics[i] = self._metrictable[i]
         cpmda.pmda_dispatch(ibuf.raw, numindoms, mbuf.raw, nummetrics)
Пример #16
0
    def _set_format(self):
        if not self.controls:
            return

        object_formats = (dinput.DIOBJECTDATAFORMAT * len(self.controls))()
        offset = 0
        for object_format, control in zip(object_formats, self.controls):
            object_format.dwOfs = offset
            object_format.dwType = control._type
            offset += 4
             
        format = dinput.DIDATAFORMAT()
        format.dwSize = ctypes.sizeof(format)
        format.dwObjSize = ctypes.sizeof(dinput.DIOBJECTDATAFORMAT)
        format.dwFlags = 0
        format.dwDataSize = offset
        format.dwNumObjs = len(object_formats)
        format.rgodf = ctypes.cast(ctypes.pointer(object_formats),
                                   dinput.LPDIOBJECTDATAFORMAT)
        self._device.SetDataFormat(format)

        prop = dinput.DIPROPDWORD()
        prop.diph.dwSize = ctypes.sizeof(prop)
        prop.diph.dwHeaderSize = ctypes.sizeof(prop.diph)
        prop.diph.dwObj = 0
        prop.diph.dwHow = dinput.DIPH_DEVICE
        prop.dwData = 64 * ctypes.sizeof(dinput.DIDATAFORMAT)
        self._device.SetProperty(dinput.DIPROP_BUFFERSIZE, 
                                 ctypes.byref(prop.diph))
Пример #17
0
    def __init__(self, buffer):
        super().__init__()

        self.checksum = compute_checksum(memoryview(buffer)[:-8])

        self.add_option(Metadata, buffer, 0)

        offset = sizeof(Metadata)
        while offset < len(buffer):
            header = OptionHeader.from_buffer_copy(buffer, offset)
            if header.size:
                option_class = index[header.tag]
                if header.size != sizeof(option_class):
                    raise InvalidSize(
                        'Option: {!r}, calculated: {}, reported: {}'.format(
                            option_class, sizeof(option_class), header.size
                        )
                    )
                self.add_option(option_class, buffer, offset)
                offset += sizeof(option_class)

        if not hasattr(self, 'cks'):
            raise ChecksumNotPresent
        if self.checksum != self.cks.value:
            raise IncorrectChecksum('calculated: {}, reported: {}'.format(
                self.checksum,
                self.cks.value
            ))
Пример #18
0
    def fromdict(cls, credential, flags=0):
        unsupported = set(credential.keys()) - SUPPORTED_CREDKEYS
        if len(unsupported):
            raise ValueError("Unsupported keys: {0}".format(unsupported))
        if flags != 0:
            raise ValueError("flag != 0 not yet supported")

        c_creds = cls()
        c_pcreds = PCREDENTIAL(c_creds)

        # zero-out memory
        ctypes.memset(c_pcreds, 0, ctypes.sizeof(c_creds))

        for key in SUPPORTED_CREDKEYS:
            if key in credential:
                if key != 'CredentialBlob':
                    setattr(c_creds, key, credential[key])
                else:
                    blob = make_unicode(credential['CredentialBlob'])
                    blob_data = ctypes.create_unicode_buffer(blob)
                    # Create_unicode_buffer adds a NULL at the end of the
                    # string we do not want that.
                    c_creds.CredentialBlobSize = \
                        ctypes.sizeof(blob_data) - \
                        ctypes.sizeof(ctypes.c_wchar)
                    c_creds.CredentialBlob = ctypes.cast(blob_data, LPBYTE)
        return c_creds
Пример #19
0
def determine_64_bit_int():
    """
    The only configuration parameter needed at compile-time is how to
    specify a 64-bit signed integer.  Python's ctypes module can get us
    that information, but it is only available in Python 2.5 or later.
    If we can't be absolutely certain, we default to "long long int",
    which is correct on most platforms (x86, x86_64).  If we find
    platforms where this heuristic doesn't work, we may need to
    hardcode for them.
    """
    try:
        try:
            import ctypes
        except ImportError:
            raise ValueError()

        if ctypes.sizeof(ctypes.c_longlong) == 8:
            return "long long int"
        elif ctypes.sizeof(ctypes.c_long) == 8:
            return "long int"
        elif ctypes.sizeof(ctypes.c_int) == 8:
            return "int"
        else:
            raise ValueError()

    except ValueError:
        return "long long int"
Пример #20
0
    def parse(cls, client: 'Client'):
        tc_type = client.recv(ctypes.sizeof(ctypes.c_byte))
        # Decimal or Null
        if tc_type == TC_NULL:
            return Null.build_c_type(), tc_type

        header_class = cls.build_c_header()
        buffer = tc_type + client.recv(
            ctypes.sizeof(header_class)
            - len(tc_type)
        )
        header = header_class.from_buffer_copy(buffer)
        data_type = type(
            cls.__name__,
            (header_class,),
            {
                '_pack_': 1,
                '_fields_': [
                    ('data', ctypes.c_ubyte * header.length),
                ],
            }
        )
        buffer += client.recv(
            ctypes.sizeof(data_type)
            - ctypes.sizeof(header_class)
        )
        return data_type, buffer
Пример #21
0
    def transaction(self, bytes_to_send):
        """Sends bytes via the SPI bus.

        :param bytes_to_send: The bytes to send on the SPI device.
        :type bytes_to_send: bytes
        :returns: bytes -- returned bytes from SPI device
        :raises: InitError
        """
        bytes_to_send = _pybytes(bytes_to_send)

        # make some buffer space to store reading/writing
        wbuffer = ctypes.create_string_buffer(bytes_to_send,
                                              len(bytes_to_send))
        rbuffer = ctypes.create_string_buffer(len(bytes_to_send))

        # create the spi transfer struct
        transfer = spi_ioc_transfer(
            tx_buf=ctypes.addressof(wbuffer),
            rx_buf=ctypes.addressof(rbuffer),
            len=ctypes.sizeof(wbuffer))

        if self.spi_transaction_callback is not None:
            self.spi_transaction_callback(bytes_to_send)

        # send the spi command
        ioctl(self.fd, SPI_IOC_MESSAGE(1), transfer)
        return _pyord(ctypes.string_at(rbuffer, ctypes.sizeof(rbuffer)))
    def __write_header(self, wave_format):
        AutoLock(self.lock)
        assert(self.hmmio)
        hmmio = self.hmmio
        ck_root = self.ck_root
        ck_child = self.ck_child
        mmio_info = self.mmio_info
        
        #Wave chunk
        ck_root.fccType = 'WAVE'
        ret = winmm.mmioCreateChunk(hmmio, ck_root.ptr(), MMIO_CREATERIFF)
        assert(ret == 0)

        #Format chunk
        ck_child.ckid = 'fmt '
        ck_child.cksize = ctypes.sizeof(WaveFormatEx)-ctypes.sizeof(ctypes.c_short) # sizeof(PCMWAVEFORMAT)
        ret = winmm.mmioCreateChunk(hmmio, ck_child.ptr(), 0)
        assert(ret == 0)

        #Write format data
        sz = ctypes.sizeof(wave_format)
        if (wave_format.wFormatTag != WAVE_FORMAT_PCM):
            sz = sz + wave_format.cbSize
        ret = winmm.mmioWrite(hmmio, wave_format.ptr(), sz)
        assert(ret == sz)

        #Back to root
        ret = winmm.mmioAscend(hmmio, ck_child.ptr(), 0)
        assert(ret == 0)
        
        #Data chunk
        ck_child.ckid = "data"
        ck_child.cksize = 0
        ret = winmm.mmioCreateChunk(self.hmmio, ck_child.ptr(), 0)
        assert(ret == 0)
Пример #23
0
def read_record(buf, offset, tag2struct=defs.tag2struct):
    tag = ord(buf[offset])
    if tag in tag2struct and tag!=32 and offset+1+ctypes.sizeof(tag2struct[tag])<=len(buf):
        struct = tag2struct[tag]
        assert struct != defs.FILE_HEADER
        record = struct.from_buffer_copy(buf, offset+1)
        offset += 1 + ctypes.sizeof(struct)
        return tag, record, offset
    else:
        junkstart, junkend = offset, None
        while offset<len(buf):
            tag1, tag2 = ord(buf[offset]), None
            if tag1 in tag2struct and tag1!=32 and offset+1+ctypes.sizeof(tag2struct[tag1])<=len(buf):
                #print>>sys.stderr, "tag1 0x%02x at offset 0x%04x" % (tag1, offset)
                struct1 = tag2struct[tag1]
                tag2 = ord(buf[offset+1+ctypes.sizeof(struct1)])
                if tag2 in tag2struct and tag2!=32:
                    junkend = offset
            if junkend is not None:
                tag, struct = tag1, struct1
                #print>>sys.stderr, "Tag 0x%02x at offset 0x%04x, preceded by junk: %s" % (tag, offset, hexlify(buf[junkstart:junkend]))
                #print "Tag 0x%02x at offset 0x%04x, preceded by junk: %s" % (tag, offset, hexlify(buf[junkstart:junkend]))
                break
            offset += 1
        else:
            junkend = len(buf)
            #print>>sys.stderr, "File ends with junk: %s" % (hexlify(buf[junkstart:junkend]))

        struct = ReprBytes("SUSPECTED_JUNK", junkend-junkstart)
        record = struct.from_buffer_copy(buf, junkstart)
        return None, record, offset
Пример #24
0
def _setup_ctypes_cache():
    from pypy.rpython.lltypesystem import rffi
    _ctypes_cache.update({
        lltype.Signed:   ctypes.c_long,
        lltype.Unsigned: ctypes.c_ulong,
        lltype.Char:     ctypes.c_ubyte,
        rffi.DOUBLE:     ctypes.c_double,
        rffi.FLOAT:      ctypes.c_float,
        rffi.LONGDOUBLE: ctypes.c_longdouble,
        rffi.SIGNEDCHAR: ctypes.c_byte,
        rffi.UCHAR:      ctypes.c_ubyte,
        rffi.SHORT:      ctypes.c_short,
        rffi.USHORT:     ctypes.c_ushort,
        rffi.INT:        ctypes.c_int,
        rffi.INT_real:   ctypes.c_int,
        rffi.UINT:       ctypes.c_uint,
        rffi.LONG:       ctypes.c_long,
        rffi.ULONG:      ctypes.c_ulong,
        rffi.LONGLONG:   ctypes.c_longlong,
        rffi.ULONGLONG:  ctypes.c_ulonglong,
        rffi.SIZE_T:     ctypes.c_size_t,
        lltype.Bool:     ctypes.c_long, # XXX
        llmemory.Address:  ctypes.c_void_p,
        llmemory.GCREF:    ctypes.c_void_p,
        llmemory.WeakRef:  ctypes.c_void_p, # XXX
        })

    # for unicode strings, do not use ctypes.c_wchar because ctypes
    # automatically converts arrays into unicode strings.
    # Pick the unsigned int that has the same size.
    if ctypes.sizeof(ctypes.c_wchar) == ctypes.sizeof(ctypes.c_uint16):
        _ctypes_cache[lltype.UniChar] = ctypes.c_uint16
    else:
        _ctypes_cache[lltype.UniChar] = ctypes.c_uint32
Пример #25
0
def test_dirent():
    dirent = configure.getstruct("struct dirent",
                                       """
           struct dirent  /* for this example only, not the exact dirent */
           {
               long d_ino;
               int d_off;
               unsigned short d_reclen;
               char d_name[32];
           };
                                       """,
                                       [("d_reclen", ctypes.c_ushort)])
    assert issubclass(dirent, ctypes.Structure)
    ssize = (ctypes.sizeof(ctypes.c_long) +
             ctypes.sizeof(ctypes.c_int) +
             ctypes.sizeof(ctypes.c_ushort) +
             32)
    extra_padding = (-ssize) % ctypes.alignment(ctypes.c_long)

    assert dirent._fields_ == [('_alignment', ctypes.c_long),
                               ('_pad0', ctypes.c_char),
                               ('_pad1', ctypes.c_char),
                               ('_pad2', ctypes.c_char),
                               ('_pad3', ctypes.c_char),
                               ('d_reclen', ctypes.c_ushort),
                               ] + [
                               ('_pad%d' % n, ctypes.c_char)
                                    for n in range(4, 4+32+extra_padding)]
    assert ctypes.sizeof(dirent) == ssize + extra_padding
    assert ctypes.alignment(dirent) == ctypes.alignment(ctypes.c_long)
Пример #26
0
def DerefPointer(pointer, processID):
#	print 'POINTER!!!!! %s, pointer points to address (/has value): %s (%s)' % (type(pointer.contents), ctypes.addressof(pointer.contents), hex(ctypes.addressof(pointer.contents)))
#	res = []
#	res.append('%s = %r' % (fieldName, pointer))
#	res.append('\n')

	try:
#		ctypes.addressof(value.contents)
		if ctypes.addressof(pointer.contents) != 0:
#		if bool(pointer.contents):
	#		contents = pointer.contents
			pointedAtType = type(pointer.contents)
			typeInstanceInLocalMem = pointedAtType()
			contents = ReadMem(ctypes.addressof(pointer.contents), ctypes.sizeof(pointer.contents), None, processID)

			fit = min(len(contents), ctypes.sizeof(typeInstanceInLocalMem))

			# Copy 
			ctypes.memmove(ctypes.addressof(typeInstanceInLocalMem), contents, fit)

			return typeInstanceInLocalMem

			return contents
		else:
			return None
	except:
		pass
	return None
Пример #27
0
    def __init__(self, buffer_size=1024):
        """Builds the tracing session properties.

        Parameters
        ---------

        buffer_size: int
            the amount of memory allocated for each trace buffer
        """

        # allocate buffer for the trace
        self.max_string_len = 1024
        self.buff_size = sizeof(EVENT_TRACE_PROPERTIES) + 2 * sizeof(c_wchar) * self.max_string_len

        self._buff = (c_char * self.buff_size)()
        self._props = cast(pointer(self._buff), POINTER(EVENT_TRACE_PROPERTIES))

        # set trace properties
        self._props.contents.wnode.buffer_size = self.buff_size
        self._props.contents.wnode.guid = KERNEL_TRACE_CONTROL_GUID
        self._props.contents.wnode.flags = WNODE_FLAG_TRACED_GUID
        self._props.contents.logger_name_offset = sizeof(EVENT_TRACE_PROPERTIES)
        self._props.contents.log_file_name_offset = 0
        self._props.contents.log_file_mode = PROCESS_TRACE_MODE_REAL_TIME
        self._props.contents.buffer_size = buffer_size
Пример #28
0
        def _get_taskinfo(self):
            integer_t = ctypes.c_int
            natural_t = ctypes.c_uint
            vm_size_t = ctypes.c_ulong

            class time_value_t(ctypes.Structure):
                _fields_ = [("seconds", integer_t),
                            ("microseconds", integer_t)]

                def __repr__(self):
                    return "%s.%s" % (self.seconds, self.microseconds)

            policy_t = ctypes.c_int

            class task_basic_info(ctypes.Structure):
                _pack_ = 4
                _fields_ = [("suspend_count", integer_t),
                            ("virtual_size", vm_size_t),
                            ("resident_size", vm_size_t),
                            ("user_time", time_value_t),
                            ("system_time", time_value_t),
                            ("policy", policy_t)]

                def __repr__(self):
                    return repr(dict((key, getattr(self, key))
                                     for key in dir(self)
                                     if not key.startswith("_")))

            kern_return_t = ctypes.c_int
            mach_port_t = natural_t
            task_name_t = ctypes.c_uint
            task_flavor_t = ctypes.c_uint
            task_info_t = ctypes.POINTER(ctypes.c_int)
            mach_msg_type_number_t = natural_t
            TASK_BASIC_INFO_COUNT = ctypes.sizeof(
                task_basic_info) / ctypes.sizeof(natural_t)
            TASK_BASIC_INFO = 5
            KERN_SUCCESS = 0

            libkern = ctypes.CDLL("/usr/lib/system/libsystem_kernel.dylib")
            task_info = libkern.task_info
            task_info.restype = kern_return_t
            task_info.argtypes = [task_name_t,
                                  task_flavor_t,
                                  ctypes.POINTER(task_basic_info),
                                  ctypes.POINTER(mach_msg_type_number_t)]

            mach_task_self = libkern.mach_task_self
            mach_task_self.restype = mach_port_t
            mach_task_self.argtypes = []

            ti = task_basic_info()

            count = mach_msg_type_number_t(TASK_BASIC_INFO_COUNT)
            kr = task_info(mach_task_self(), TASK_BASIC_INFO,
                           ctypes.byref(ti),
                           ctypes.byref(count))
            if kr != KERN_SUCCESS:
                return None
            return ti
Пример #29
0
def ReadStructureFromOtherProcessMemory(address, structureType, processID):
	# Buffer to store the read bytes.
	outBufferAddr = ctypes.create_string_buffer(ctypes.sizeof(structureType))
#	bytesRead = ctypes.c_size_t()
	c_structure = structureType()	# Allocate a structure in the *current* (!) process to mirror the structure from the *other* process.
	ReadMem(address, ctypes.sizeof(structureType), ctypes.byref(c_structure), processID)
	return c_structure
Пример #30
0
def _get_properties(properties, length): 
    """
    Convenience Function to get the material properties as a dict
    and values in a python format.
    """
    result = {}
    #read all properties
    for p in [properties[i] for i in range(length)]:
        #the name
        p = p.contents
        key = str(p.mKey.data)

        #the data
        from ctypes import POINTER, cast, c_int, c_float, sizeof
        if p.mType == 1:
            arr = cast(p.mData, POINTER(c_float * int(p.mDataLength/sizeof(c_float)) )).contents
            value = numpy.array([x for x in arr])
        elif p.mType == 3: #string can't be an array
            value = cast(p.mData, POINTER(structs.String)).contents.data
        elif p.mType == 4:
            arr = cast(p.mData, POINTER(c_int * int(p.mDataLength/sizeof(c_int)) )).contents
            value = numpy.array([x for x in arr])
        else:
            value = p.mData[:p.mDataLength]

        result[key] = value

    return result
Пример #31
0
    def decode(self, file, filename):
        if not file:
            file = open(filename, 'rb')
        bytes = file.read()
        buffer = ctypes.c_buffer(bytes)

        if bytes[:2] != 'BM':
            raise ImageDecodeException('Not a Windows bitmap file: %r' %
                                       (filename or file))

        file_header = to_ctypes(buffer, 0, BITMAPFILEHEADER)
        bits_offset = file_header.bfOffBits
        info_header_offset = ctypes.sizeof(BITMAPFILEHEADER)
        info_header = to_ctypes(buffer, info_header_offset, BITMAPINFOHEADER)
        palette_offset = info_header_offset + info_header.biSize

        if info_header.biSize < ctypes.sizeof(BITMAPINFOHEADER):
            raise ImageDecodeException('Unsupported BMP type: %r' %
                                       (filename or file))

        width = info_header.biWidth
        height = info_header.biHeight
        if width <= 0 or info_header.biPlanes != 1:
            raise ImageDecodeException('BMP file has corrupt parameters: %r' %
                                       (filename or file))
        pitch_sign = height < 0 and -1 or 1
        height = abs(height)

        compression = info_header.biCompression
        if compression not in (BI_RGB, BI_BITFIELDS):
            raise ImageDecodeException('Unsupported compression: %r' %
                                       (filename or file))

        clr_used = 0
        bitcount = info_header.biBitCount
        if bitcount == 1:
            pitch = (width + 7) // 8
            bits_type = ctypes.c_ubyte
            decoder = decode_1bit
        elif bitcount == 4:
            pitch = (width + 1) // 2
            bits_type = ctypes.c_ubyte
            decoder = decode_4bit
        elif bitcount == 8:
            bits_type = ctypes.c_ubyte
            pitch = width
            decoder = decode_8bit
        elif bitcount == 16:
            pitch = width * 2
            bits_type = ctypes.c_uint16
            decoder = decode_bitfields
        elif bitcount == 24:
            pitch = width * 3
            bits_type = ctypes.c_ubyte
            decoder = decode_24bit
        elif bitcount == 32:
            pitch = width * 4
            if compression == BI_RGB:
                decoder = decode_32bit_rgb
                bits_type = ctypes.c_ubyte
            elif compression == BI_BITFIELDS:
                decoder = decode_bitfields
                bits_type = ctypes.c_uint32
            else:
                raise ImageDecodeException('Unsupported compression: %r' %
                                           (filename or file))
        else:
            raise ImageDecodeException('Unsupported bit count %d: %r' %
                                       (bitcount, filename or file))

        pitch = (pitch + 3) & ~3
        packed_width = pitch // ctypes.sizeof(bits_type)

        if bitcount < 16 and compression == BI_RGB:
            clr_used = info_header.biClrUsed or (1 << bitcount)
            palette = to_ctypes(buffer, palette_offset, RGBQUAD * clr_used)
            bits = to_ctypes(buffer, bits_offset,
                             bits_type * packed_width * height)
            return decoder(bits, palette, width, height, pitch, pitch_sign)
        elif bitcount >= 16 and compression == BI_RGB:
            bits = to_ctypes(buffer, bits_offset,
                             bits_type * (packed_width * height))
            return decoder(bits, None, width, height, pitch, pitch_sign)
        elif compression == BI_BITFIELDS:
            if info_header.biSize >= ctypes.sizeof(BITMAPV4HEADER):
                info_header = to_ctypes(buffer, info_header_offset,
                                        BITMAPV4HEADER)
                r_mask = info_header.bV4RedMask
                g_mask = info_header.bV4GreenMask
                b_mask = info_header.bV4BlueMask
            else:
                fields_offset = info_header_offset + \
                    ctypes.sizeof(BITMAPINFOHEADER)
                fields = to_ctypes(buffer, fields_offset, RGBFields)
                r_mask = fields.red
                g_mask = fields.green
                b_mask = fields.blue

            class _BitsArray(ctypes.LittleEndianStructure):
                _pack_ = 1
                _fields_ = [
                    ('data', bits_type * packed_width * height),
                ]

            bits = to_ctypes(buffer, bits_offset, _BitsArray).data
            return decoder(bits, r_mask, g_mask, b_mask, width, height, pitch,
                           pitch_sign)
Пример #32
0
 def ensure_size(self, size):
     while ctypes.sizeof(self.buf) - self.wpos < size:
         ctypes.resize(self.buf, ctypes.sizeof(self.buf) * 2)
def test_dns_record_flags_size():
    assert ctypes.sizeof(gdef.DNS_RECORD_FLAGS) == 4
Пример #34
0
  class CTypesEncoder(json.JSONEncoder):
    
    def default(self, obj):
      
      if isinstance(obj, ctypes.Structure):

        jsonDict = {}
        for name in obj.__dict__:
          value = getattr(obj, name)
          jsonDict[name] = value

        for field in obj._fields_:
          name, type = field
          value = getattr(obj, name)
          jsonDict[name] = value

        return jsonDict

      elif isinstance(obj, ctypes.Array):
        
        return [ item for item in obj ]
      
      print obj.__class__
      return json.JSONEncoder.default(self, obj)


  header.data_offset = ctypes.sizeof(HEADER) + ctypes.sizeof(attrib_desc_t)
  print CTypesEncoder().encode( [ header, attrib_desc ] )
  print ""
Пример #35
0
def xtf_read_gen(
    path: str,
    types: List[XTFHeaderType] = None
) -> Generator[Union[XTFFileHeader, XTFPacket], None, None]:
    """
    Generator object which iterates over the XTF file, return first the file header and then subsequent packets
    :param path: The path to the XTF file
    :param types: Optional list of XTFHeaderTypes to keep. Default (None) returns all types. Can improve performance
    :return: None
    """
    # Read index file if it exists
    (path_root, _) = splitext(path)
    path_idx = path_root + '.pyxtf_idx'
    has_idx = isfile(path_idx)
    if has_idx:
        with open(path_idx, 'rb') as f_idx:
            xtf_idx = pickle.load(
                f_idx)  # type: Dict[XTFHeaderType, List[int]]
    else:
        xtf_idx = {}

    # Read XTF file
    with open(path, 'rb') as f:
        # Read initial file header
        file_header = XTFFileHeader.create_from_buffer(buffer=f)

        n_channels = file_header.channel_count()
        if n_channels > 6:
            raise NotImplementedError(
                "Support for more than 6 channels not implemented.")

        # Return the file header before starting packet iteration
        yield file_header

        # Loop through XTF packets and handle according to type
        if has_idx:
            # Only return packets that matches types arg (if None, return all)
            for packet_start_loc, p_headertype in xtf_idx_pos_iter(
                    xtf_idx, types):
                f.seek(packet_start_loc)

                # Get the class associated with this header type (if any)
                # How to read and construct each type is implemented in the class (default impl. in XTFBase.__new__)
                p_class = XTFPacketClasses.get(p_headertype, None)
                if p_class:
                    p_header = p_class.create_from_buffer(
                        buffer=f, file_header=file_header)
                    yield p_header
                else:
                    warning_str = 'Unsupported packet type \'{}\' encountered'.format(
                        str(p_headertype))
                    warn(warning_str)
        else:
            # Preallocate, as it is assigned to at every iteration
            p_start = XTFPacketStart()

            while True:
                # Test for file end without advancing the file position
                if not f.peek(1):
                    break

                # Save packet start location
                packet_start_loc = f.tell()

                # Read the first few shared packet bytes without advancing file pointer
                bytes_read = f.readinto(p_start)
                if bytes_read < ctypes.sizeof(XTFPacketStart):
                    raise RuntimeError(
                        'XTF file shorter than expected while reading packet.')

                # Only return packets that matches types arg (if None, return all)
                p_headertype = XTFHeaderType(p_start.HeaderType)
                if not types or p_headertype in types:
                    f.seek(packet_start_loc)

                    # Get the class associated with this header type (if any)
                    # How to read and construct each type is implemented in the class (default impl. in XTFBase.__new__)
                    p_class = XTFPacketClasses.get(p_headertype, None)
                    if p_class:
                        p_header = p_class.create_from_buffer(
                            buffer=f, file_header=file_header)
                        yield p_header
                    else:
                        warning_str = 'Unsupported packet type \'{}\' encountered'.format(
                            str(p_headertype))
                        warn(warning_str)

                # Skip over any data padding before next iteration
                f.seek(packet_start_loc + p_start.NumBytesThisRecord)

                # Store index file information
                try:
                    xtf_idx[p_headertype].append(packet_start_loc)
                except KeyError:
                    xtf_idx[p_headertype] = [packet_start_loc]

            # Pickle index file
            with open(path_idx, mode='wb') as f_idx:
                pickle.dump(xtf_idx, f_idx)

        return
Пример #36
0
def _get_total_physmem():
    retval = None

    try:
        if IS_WIN:
            import ctypes

            kernel32 = ctypes.windll.kernel32
            c_ulong = ctypes.c_ulong

            class MEMORYSTATUS(ctypes.Structure):
                _fields_ = [('dwLength', c_ulong), ('dwMemoryLoad', c_ulong),
                            ('dwTotalPhys', c_ulong), ('dwAvailPhys', c_ulong),
                            ('dwTotalPageFile', c_ulong),
                            ('dwAvailPageFile', c_ulong),
                            ('dwTotalVirtual', c_ulong),
                            ('dwAvailVirtual', c_ulong)]

            memory_status = MEMORYSTATUS()
            memory_status.dwLength = ctypes.sizeof(MEMORYSTATUS)
            kernel32.GlobalMemoryStatus(ctypes.byref(memory_status))

            retval = memory_status.dwTotalPhys
        else:
            retval = 1024 * int(
                re.search(r"(?i)MemTotal:\s+(\d+)\skB",
                          open("/proc/meminfo").read()).group(1))
    except:
        pass

    if not retval:
        try:
            import psutil
            retval = psutil.virtual_memory().total
        except:
            pass

    if not retval:
        try:
            retval = int(
                re.search(r"real mem(ory)?\s*=\s*(\d+) ",
                          open("/var/run/dmesg.boot").read()).group(2))
        except:
            pass

    if not retval:
        try:
            retval = int(
                re.search(
                    r"hw\.(physmem|memsize):\s*(\d+)",
                    subprocess.check_output(
                        "sysctl hw", shell=True,
                        stderr=subprocess.STDOUT)).group(2))
        except:
            pass

    if not retval:
        try:
            retval = 1024 * int(
                re.search(
                    r"\s+(\d+) K total memory",
                    subprocess.check_output(
                        "vmstat -s", shell=True,
                        stderr=subprocess.STDOUT)).group(1))
        except:
            pass

    if not retval:
        try:
            retval = int(
                re.search(
                    r"Mem:\s+(\d+)",
                    subprocess.check_output(
                        "free -b", shell=True,
                        stderr=subprocess.STDOUT)).group(1))
        except:
            pass

    if not retval:
        try:
            retval = 1024 * int(
                re.search(
                    r"KiB Mem:\s*\x1b[^\s]+\s*(\d+)",
                    subprocess.check_output(
                        "top -n 1", shell=True,
                        stderr=subprocess.STDOUT)).group(1))
        except:
            pass

    return retval
Пример #37
0
def evaluate(text):
    b = text if isinstance(text, bytes) else bytes(str(text), 'utf-8')
    pt = ctypes.create_string_buffer(b)
    return przetak.evaluate(GoString(pt.raw, ctypes.sizeof(pt)))
Пример #38
0
OnigCodePoint = ctypes.c_uint


class OnigRegexType(ctypes.Structure):
    _fields_ = []


regex_t = OnigRegexType
OnigRegex = ctypes.POINTER(OnigRegexType)

try:
    # Python 2.7
    _c_ssize_t = ctypes.c_ssize_t
except AttributeError:
    # Python 2.6
    if ctypes.sizeof(ctypes.c_int) == ctypes.sizeof(ctypes.c_void_p):
        _c_ssize_t = ctypes.c_int
    elif ctypes.sizeof(ctypes.c_long) == ctypes.sizeof(ctypes.c_void_p):
        _c_ssize_t = ctypes.c_long
    elif ctypes.sizeof(ctypes.c_longlong) == ctypes.sizeof(ctypes.c_void_p):
        _c_ssize_t = ctypes.c_longlong


class OnigRegion(ctypes.Structure):
    _fields_ = [
        ("allocated", ctypes.c_int),
        ("num_regs", ctypes.c_int),
        ("beg", ctypes.POINTER(_c_ssize_t)),
        ("end", ctypes.POINTER(_c_ssize_t)),
        ("history_root", ctypes.c_void_p),
    ]
Пример #39
0
WEIGHT_TYPE = np.float64
INVTT_TYPE = np.float64
TIMESTAMP_TYPE = np.float64
PSD_TYPE = np.float64

try:
    _mappraiser = ct.CDLL("libmappraiser.so")
except OSError:
    path = ctu.find_library("mappraiser")
    if path is not None:
        _mappraiser = ct.CDLL(path)

available = _mappraiser is not None

try:
    if MPI._sizeof(MPI.Comm) == ct.sizeof(ct.c_int):
        MPI_Comm = ct.c_int
    else:
        MPI_Comm = ct.c_void_p
except Exception as e:
    raise Exception(
        'Failed to set the portable MPI communicator datatype: "{}". '
        "MPI4py is probably too old. ".format(e))


def encode_comm(comm):
    comm_ptr = MPI._addressof(comm)
    return MPI_Comm.from_address(comm_ptr)


_mappraiser.MLmap.restype = None
Пример #40
0
def to_ctypes(buffer, offset, type):
    if offset + ctypes.sizeof(type) > len(buffer):
        raise ImageDecodeException('BMP file is truncated')
    ptr = ptr_add(ctypes.pointer(buffer), offset)
    return ctypes.cast(ptr, ctypes.POINTER(type)).contents
Пример #41
0
def SendInput(*inputs):
    nInputs = len(inputs)
    LPINPUT = INPUT * nInputs
    pInputs = LPINPUT(*inputs)
    cbSize = ctypes.c_int(ctypes.sizeof(INPUT))
    return ctypes.windll.user32.SendInput(nInputs, pInputs, cbSize)
Пример #42
0
    def __init__(self, options, minidump_name):
        self.minidump_name = minidump_name
        self.minidump_file = open(minidump_name, "r")
        self.minidump = mmap.mmap(self.minidump_file.fileno(), 0,
                                  mmap.MAP_PRIVATE)
        self.header = MINIDUMP_HEADER.Read(self.minidump, 0)
        if self.header.signature != MinidumpReader._HEADER_MAGIC:
            print >> sys.stderr, "Warning: unsupported minidump header magic"
        DebugPrint(self.header)
        directories = []
        offset = self.header.stream_directories_rva
        for _ in xrange(self.header.stream_count):
            directories.append(MINIDUMP_DIRECTORY.Read(self.minidump, offset))
            offset += MINIDUMP_DIRECTORY.size
        self.arch = None
        self.exception = None
        self.exception_context = None
        self.memory_list = None
        self.memory_list64 = None
        self.thread_map = {}

        # Find MDRawSystemInfo stream and determine arch.
        for d in directories:
            if d.stream_type == MD_SYSTEM_INFO_STREAM:
                system_info = MINIDUMP_RAW_SYSTEM_INFO.Read(
                    self.minidump, d.location.rva)
                self.arch = system_info.processor_architecture
                assert self.arch in [
                    MD_CPU_ARCHITECTURE_AMD64, MD_CPU_ARCHITECTURE_X86
                ]
        assert not self.arch is None

        for d in directories:
            DebugPrint(d)
            if d.stream_type == MD_EXCEPTION_STREAM:
                self.exception = MINIDUMP_EXCEPTION_STREAM.Read(
                    self.minidump, d.location.rva)
                DebugPrint(self.exception)
                if self.arch == MD_CPU_ARCHITECTURE_X86:
                    self.exception_context = MINIDUMP_CONTEXT_X86.Read(
                        self.minidump, self.exception.thread_context.rva)
                elif self.arch == MD_CPU_ARCHITECTURE_AMD64:
                    self.exception_context = MINIDUMP_CONTEXT_AMD64.Read(
                        self.minidump, self.exception.thread_context.rva)
                DebugPrint(self.exception_context)
            elif d.stream_type == MD_THREAD_LIST_STREAM:
                thread_list = MINIDUMP_THREAD_LIST.Read(
                    self.minidump, d.location.rva)
                assert ctypes.sizeof(thread_list) == d.location.data_size
                DebugPrint(thread_list)
                for thread in thread_list.threads:
                    DebugPrint(thread)
                    self.thread_map[thread.id] = thread
            elif d.stream_type == MD_MEMORY_LIST_STREAM:
                print >> sys.stderr, "Warning: not a full minidump"
                assert self.memory_list is None
                self.memory_list = MINIDUMP_MEMORY_LIST.Read(
                    self.minidump, d.location.rva)
                assert ctypes.sizeof(self.memory_list) == d.location.data_size
                DebugPrint(self.memory_list)
            elif d.stream_type == MD_MEMORY_64_LIST_STREAM:
                assert self.memory_list64 is None
                self.memory_list64 = MINIDUMP_MEMORY_LIST64.Read(
                    self.minidump, d.location.rva)
                assert ctypes.sizeof(
                    self.memory_list64) == d.location.data_size
                DebugPrint(self.memory_list64)
Пример #43
0
def Keyboard(code, flags=0):
    input = Input(KeybdInput(code, flags))
    ctypes.windll.user32.SendInput(1, ctypes.pointer(input), ctypes.sizeof(input))
    return input
Пример #44
0
    HANDLED_TYPES = (_bytes.unicode, )

    @classmethod
    def from_param(cls, value, typeCode=None):
        # TODO: raise CopyError if the flag is set!
        converted = _bytes.as_8_bit(value)
        result = StringHandler.from_param(converted)
        if converted is not value:
            if ERROR_ON_COPY:
                raise error.CopyError(
                    """Unicode string passed, cannot copy with ERROR_ON_COPY set, please use 8-bit strings"""
                )
            result._temporary_array_ = converted
        return result

    def asArray(self, value, typeCode=None):
        value = _bytes.as_8_bit(value)
        return StringHandler.asArray(self, value, typeCode=typeCode)


BYTE_SIZES = {
    GL_1_1.GL_DOUBLE: ctypes.sizeof(_types.GLdouble),
    GL_1_1.GL_FLOAT: ctypes.sizeof(_types.GLfloat),
    GL_1_1.GL_INT: ctypes.sizeof(_types.GLint),
    GL_1_1.GL_SHORT: ctypes.sizeof(_types.GLshort),
    GL_1_1.GL_UNSIGNED_BYTE: ctypes.sizeof(_types.GLubyte),
    GL_1_1.GL_UNSIGNED_SHORT: ctypes.sizeof(_types.GLshort),
    GL_1_1.GL_BYTE: ctypes.sizeof(_types.GLbyte),
    GL_1_1.GL_UNSIGNED_INT: ctypes.sizeof(_types.GLuint),
}
ERROR_IO_PENDING = 997
ERROR_INVALID_USER_BUFFER = 1784

READ_CONTROL = 0x00020000
STANDARD_RIGHTS_READ = READ_CONTROL

KEY_QUERY_VALUE = 0x0001
HKEY_LOCAL_MACHINE = 0x80000002
SYNCHRONIZE = 0x00100000
KEY_ENUMERATE_SUB_KEYS = 0x0008
KEY_NOTIFY = 0x0010
KEY_READ = ((STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS
             | KEY_NOTIFY) & (~SYNCHRONIZE))
KEY_EXECUTE = KEY_READ & ~SYNCHRONIZE

if ctypes.sizeof(ctypes.c_void_p) == 8:
    ULONG_PTR = ctypes.c_int64
else:
    ULONG_PTR = ctypes.c_ulong


#Structyres.
class DUMMYSTRUCTNAME(ctypes.Structure):
    # pylint: disable=too-few-public-methods
    _fields_ = [('Offset', ctypes.wintypes.DWORD),\
    ('OffsetHigh', ctypes.wintypes.DWORD),]


class DUMMYUNIONNAME(ctypes.Union):
    # pylint: disable=too-few-public-methods
    _fields_ = [('_0', DUMMYSTRUCTNAME),\
Пример #46
0
def Hardware(message, parameter=0):
    input = Input(HardwareInput(message, parameter))
    ctypes.windll.user32.SendInput(1, ctypes.pointer(input), ctypes.sizeof(input))
    return input
Пример #47
0
    the spaces replaced by underscores.
    """
    return p_name.lower().replace(" ", "_")


class DCAMException(halExceptions.HardwareException):
    pass


#
# Initialization
#
dcam = ctypes.windll.dcamapi

paraminit = DCAMAPI_INIT(0, 0, 0, 0, None, None)
paraminit.size = ctypes.sizeof(paraminit)
error_code = dcam.dcamapi_init(ctypes.byref(paraminit))
if (error_code != DCAMERR_NOERROR):
    raise DCAMException("DCAM initialization failed with error code " +
                        str(error_code))

n_cameras = paraminit.iDeviceCount


class HCamData(object):
    """
    Hamamatsu camera data object.

    Initially I tried to use create_string_buffer() to allocate storage for the 
    data from the camera but this turned out to be too slow. The software
    kept falling behind the camera and create_string_buffer() seemed to be the
Пример #48
0
def Mouse(flags, x=0, y=0, data=0):
    input = Input(MouseInput(flags, x, y, data))
    ctypes.windll.user32.SendInput(1, ctypes.pointer(input), ctypes.sizeof(input))
    return input
Пример #49
0
    def _shutdown_with_windows_restart_manager(self, pid):
        """ Shut down a process using the Windows Restart Manager.

        When Windows shuts down, it uses a protocol including the
        WM_QUERYENDSESSION and WM_ENDSESSION messages to give
        applications a chance to shut down safely. The best way to
        simulate this is via the Restart Manager, which allows a process
        (such as an installer) to use the same mechanism to shut down
        any other processes which are using registered resources.

        This function starts a Restart Manager session, registers the
        process as a resource, and shuts down the process.

        :param pid: The process id (int) of the process to shutdown

        :raises: WindowsError: if a Windows API call fails
        """

        import ctypes
        from ctypes import Structure, POINTER, WINFUNCTYPE, windll, pointer, WinError
        from ctypes.wintypes import HANDLE, DWORD, BOOL, WCHAR, UINT, ULONG, LPCWSTR

        # set up Windows SDK types
        OpenProcess = windll.kernel32.OpenProcess
        OpenProcess.restype = HANDLE
        OpenProcess.argtypes = [
            DWORD,  # dwDesiredAccess
            BOOL,  # bInheritHandle
            DWORD
        ]  # dwProcessId
        PROCESS_QUERY_INFORMATION = 0x0400

        class FILETIME(Structure):
            _fields_ = [('dwLowDateTime', DWORD), ('dwHighDateTime', DWORD)]

        LPFILETIME = POINTER(FILETIME)

        GetProcessTimes = windll.kernel32.GetProcessTimes
        GetProcessTimes.restype = BOOL
        GetProcessTimes.argtypes = [
            HANDLE,  # hProcess
            LPFILETIME,  # lpCreationTime
            LPFILETIME,  # lpExitTime
            LPFILETIME,  # lpKernelTime
            LPFILETIME
        ]  # lpUserTime

        ERROR_SUCCESS = 0

        class RM_UNIQUE_PROCESS(Structure):
            _fields_ = [('dwProcessId', DWORD), ('ProcessStartTime', FILETIME)]

        RmStartSession = windll.rstrtmgr.RmStartSession
        RmStartSession.restype = DWORD
        RmStartSession.argtypes = [
            POINTER(DWORD),  # pSessionHandle
            DWORD,  # dwSessionFlags
            POINTER(WCHAR)
        ]  # strSessionKey

        class GUID(ctypes.Structure):
            _fields_ = [('Data1', ctypes.c_ulong), ('Data2', ctypes.c_ushort),
                        ('Data3', ctypes.c_ushort),
                        ('Data4', ctypes.c_ubyte * 8)]

        CCH_RM_SESSION_KEY = ctypes.sizeof(GUID) * 2

        RmRegisterResources = windll.rstrtmgr.RmRegisterResources
        RmRegisterResources.restype = DWORD
        RmRegisterResources.argtypes = [
            DWORD,  # dwSessionHandle
            UINT,  # nFiles
            POINTER(LPCWSTR),  # rgsFilenames
            UINT,  # nApplications
            POINTER(RM_UNIQUE_PROCESS),  # rgApplications
            UINT,  # nServices
            POINTER(LPCWSTR)
        ]  # rgsServiceNames

        RM_WRITE_STATUS_CALLBACK = WINFUNCTYPE(None, UINT)
        RmShutdown = windll.rstrtmgr.RmShutdown
        RmShutdown.restype = DWORD
        RmShutdown.argtypes = [
            DWORD,  # dwSessionHandle
            ULONG,  # lActionFlags
            RM_WRITE_STATUS_CALLBACK
        ]  # fnStatus

        RmEndSession = windll.rstrtmgr.RmEndSession
        RmEndSession.restype = DWORD
        RmEndSession.argtypes = [DWORD]  # dwSessionHandle

        # Get the info needed to uniquely identify the process
        hProc = OpenProcess(PROCESS_QUERY_INFORMATION, False, pid)
        if not hProc:
            raise WinError()

        creationTime = FILETIME()
        exitTime = FILETIME()
        kernelTime = FILETIME()
        userTime = FILETIME()
        if not GetProcessTimes(hProc, pointer(creationTime), pointer(exitTime),
                               pointer(kernelTime), pointer(userTime)):
            raise WinError()

        # Start the Restart Manager Session
        dwSessionHandle = DWORD()
        sessionKeyType = WCHAR * (CCH_RM_SESSION_KEY + 1)
        sessionKey = sessionKeyType()
        if RmStartSession(pointer(dwSessionHandle), 0,
                          sessionKey) != ERROR_SUCCESS:
            raise WinError()

        try:
            UProcs_count = 1
            UProcsArrayType = RM_UNIQUE_PROCESS * UProcs_count
            UProcs = UProcsArrayType(RM_UNIQUE_PROCESS(pid, creationTime))

            # Register the process as a resource
            if RmRegisterResources(dwSessionHandle, 0, None, UProcs_count,
                                   UProcs, 0, None) != ERROR_SUCCESS:
                raise WinError()

            # Shut down all processes using registered resources
            if RmShutdown(
                    dwSessionHandle, 0,
                    ctypes.cast(None,
                                RM_WRITE_STATUS_CALLBACK)) != ERROR_SUCCESS:
                raise WinError()

        finally:
            RmEndSession(dwSessionHandle)
Пример #50
0
    def newFrames(self):
        """
        Return a list of the ids of all the new frames since the last check.
        Returns an empty list if the camera has already stopped and no frames
        are available.
    
        This will block waiting for at least one new frame.
        """

        captureStatus = ctypes.c_int32(0)
        self.checkStatus(
            dcam.dcamcap_status(self.camera_handle,
                                ctypes.byref(captureStatus)))

        # Wait for a new frame if the camera is acquiring.
        if captureStatus.value == DCAMCAP_STATUS_BUSY:
            paramstart = DCAMWAIT_START(
                0, 0, DCAMWAIT_CAPEVENT_FRAMEREADY | DCAMWAIT_CAPEVENT_STOPPED,
                100)
            paramstart.size = ctypes.sizeof(paramstart)
            self.checkStatus(
                dcam.dcamwait_start(self.wait_handle,
                                    ctypes.byref(paramstart)),
                "dcamwait_start")

        # Check how many new frames there are.
        paramtransfer = DCAMCAP_TRANSFERINFO(0, DCAMCAP_TRANSFERKIND_FRAME, 0,
                                             0)
        paramtransfer.size = ctypes.sizeof(paramtransfer)
        self.checkStatus(
            dcam.dcamcap_transferinfo(self.camera_handle,
                                      ctypes.byref(paramtransfer)),
            "dcamcap_transferinfo")
        cur_buffer_index = paramtransfer.nNewestFrameIndex
        cur_frame_number = paramtransfer.nFrameCount

        # Check that we have not acquired more frames than we can store in our buffer.
        # Keep track of the maximum backlog.
        backlog = cur_frame_number - self.last_frame_number
        if (backlog > self.number_image_buffers):
            print(
                ">> Warning! hamamatsu camera frame buffer overrun detected!")
        if (backlog > self.max_backlog):
            self.max_backlog = backlog
        self.last_frame_number = cur_frame_number

        # Create a list of the new frames.
        new_frames = []
        if (cur_buffer_index < self.buffer_index):
            for i in range(self.buffer_index + 1, self.number_image_buffers):
                new_frames.append(i)
            for i in range(cur_buffer_index + 1):
                new_frames.append(i)
        else:
            for i in range(self.buffer_index, cur_buffer_index):
                new_frames.append(i + 1)
        self.buffer_index = cur_buffer_index

        if self.debug:
            print(new_frames)

        return new_frames
Пример #51
0
def CMSG_SPACE(length):  # bits/socket.h
    ret = CMSG_ALIGN(length).value + CMSG_ALIGN(ctypes.sizeof(cmsghdr)).value
    return ctypes.c_size_t(ret)
Пример #52
0
import struct


class InotifyEvent(ctypes.Structure):
    _fields_ = [
        ("wd", c_int),
        ("mask", c_uint32),
        ("cookie", c_uint32),
        ("len", c_uint32),
        ("name", c_char_p),
    ]


s_size = 16
# default 2048 events
INOTIFY_EVENT_BUFFER_SIZE = 2048 * (ctypes.sizeof(InotifyEvent) + s_size)

event_types = {
    "IN_CLOSE_WRITE": 0x00000008,
    "IN_CREATE": 0x00000100,
    "IN_MOVED_FROM": 0x00000040,
    "IN_MOVED_TO": 0x00000080,
    "IN_DELETE": 0x00000200,
    "IN_DELETE_SELF": 0x00000400,
    "IN_IGNORED": 0x8000
}

IN_NONBLOCK = 0x00004000


def parse_inotify_buffer(event_buffer):
Пример #53
0
def CMSG_LEN(length):
    sizeof_cmshdr = ctypes.sizeof(cmsghdr)
    return ctypes.c_size_t(CMSG_ALIGN(sizeof_cmshdr).value + length)
Пример #54
0
    def __init__(self):
        Structure.__init__(self)

        self.cb = sizeof(self)
Пример #55
0
class MPIMsg(CompositeObject):

    _C_field_bufs = 'bufs'
    _C_field_bufg = 'bufg'
    _C_field_sizes = 'sizes'
    _C_field_rrecv = 'rrecv'
    _C_field_rsend = 'rsend'

    if MPI._sizeof(MPI.Request) == sizeof(c_int):
        c_mpirequest_p = type('MPI_Request', (c_int,), {})
    else:
        c_mpirequest_p = type('MPI_Request', (c_void_p,), {})

    fields = [
        (_C_field_bufs, c_void_p),
        (_C_field_bufg, c_void_p),
        (_C_field_sizes, POINTER(c_int)),
        (_C_field_rrecv, c_mpirequest_p),
        (_C_field_rsend, c_mpirequest_p),
    ]

    def __init__(self, name, target, halos, language=None):
        self._target = target
        self._halos = halos
        self._language = language

        super().__init__(name, 'msg', self.fields)

        # Required for buffer allocation/deallocation before/after jumping/returning
        # to/from C-land
        self._allocator = default_allocator(language)
        self._memfree_args = []

    def __del__(self):
        self._C_memfree()

    def _C_memfree(self):
        # Deallocate the MPI buffers
        for i in self._memfree_args:
            self._allocator.free(*i)
        self._memfree_args[:] = []

    def __value_setup__(self, dtype, value):
        # We eventually produce an array of `struct msg` that is as big as
        # the number of peers we have to communicate with
        return (dtype._type_*self.npeers)()

    @property
    def target(self):
        return self._target

    @property
    def halos(self):
        return self._halos

    @property
    def language(self):
        return self._language

    @property
    def npeers(self):
        return len(self._halos)

    def _arg_defaults(self, alias=None):
        target = alias or self.target
        for i, halo in enumerate(self.halos):
            entry = self.value[i]
            # Buffer size for this peer
            shape = []
            for dim, side in zip(*halo):
                try:
                    shape.append(getattr(target._size_owned[dim], side.name))
                except AttributeError:
                    assert side is CENTER
                    shape.append(target._size_domain[dim])
            entry.sizes = (c_int*len(shape))(*shape)
            # Allocate the send/recv buffers
            size = reduce(mul, shape)
            ctype = dtype_to_ctype(target.dtype)
            entry.bufg, bufg_memfree_args = self._allocator._alloc_C_libcall(size, ctype)
            entry.bufs, bufs_memfree_args = self._allocator._alloc_C_libcall(size, ctype)
            # The `memfree_args` will be used to deallocate the buffer upon returning
            # from C-land
            self._memfree_args.extend([bufg_memfree_args, bufs_memfree_args])

        return {self.name: self.value}

    def _arg_values(self, args=None, **kwargs):
        return self._arg_defaults(alias=kwargs.get(self.target.name, self.target))

    def _arg_apply(self, *args, **kwargs):
        self._C_memfree()

    # Pickling support
    _pickle_args = ['name', 'target', 'halos']
    _pickle_kwargs = CompositeObject._pickle_kwargs + ['language']
Пример #56
0
def CMSG_ALIGN(length):  # bits/socket.h
    ret = (length + ctypes.sizeof(ctypes.c_size_t) - 1
           & ~(ctypes.sizeof(ctypes.c_size_t) - 1))
    return ctypes.c_size_t(ret)
Пример #57
0
 def CMSG_SPACE(sz):
     return CMSG_ALIGN(sz) + CMSG_ALIGN(ct.sizeof(cmsghdr))
Пример #58
0
# Copyright (C) 2017-present Pinterest Inc.
#
# This module is part of ptracer and is released under
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0

import ctypes
import sys

PLATFORM = None
WORD_SIZE = ctypes.sizeof(ctypes.c_void_p)
BITS = WORD_SIZE * 8

if sys.platform.startswith('linux'):
    PLATFORM = 'linux'

if PLATFORM is None or BITS != 64:
    raise RuntimeError('unsupported platform: {} ({} bit)'.format(
        sys.platform, BITS))
Пример #59
0
 def CMSG_ALIGN(sz):
     i = ct.sizeof(ct.c_size_t)
     return ((sz + i - 1) // i) * i
Пример #60
0
 def CMSG_LEN(sz):
     return CMSG_ALIGN(ct.sizeof(cmsghdr)) + sz