def _ffi( module_name=MODULE_NAME, headers=HEADER_FILES, sources=SOURCE_FILES, libraries=LIBRARIES): """ Returns an instance of :class:`FFI` without compiling the module. This function is used internally but also as an entrypoint in the setup.py for `cffi_modules`. :keyword str module_name: Optional module name to use when setting the source. :keyword tuple headers: Optional path(s) to the header files. :keyword tuple sources: Optional path(s) to the source files. """ logger.debug( "_ffi(module_name=%r, headers=%r, sources=%r)", module_name, headers, sources) header = _read(*headers) source = _read(*sources) ffi = FFI() ffi.set_unicode(True) ffi.set_source(module_name, source, libraries=libraries) # Windows uses SAL annotations which can provide some helpful information # about the inputs and outputs to a function. Rather than require these # to be stripped out manually we should strip them out programmatically. ffi.cdef(REGEX_SAL_ANNOTATION.sub(" ", header)) return ffi
def setUpClass(cls): # Reset everything back to the default values first. cls.ffi = None cls.kernel32 = None cls.ws2_32 = None cls.HAS_INTERNET = None # First run and this test case requires internet access. Determine # if we have access to the internet then cache the value. if cls.REQUIRES_INTERNET and SharedState.HAS_INTERNET is None: original_timeout = socket.getdefaulttimeout() socket.setdefaulttimeout(1) try: for hostname in cls.INTERNET_HOSTS: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect((hostname, cls.INTERNET_PORT)) SharedState.HAS_INTERNET = True break # pylint: disable=broad-except except Exception: # pragma: no cover pass finally: sock.close() else: # pragma: no cover SharedState.HAS_INTERNET = False finally: socket.setdefaulttimeout(original_timeout) if os.name == "nt" and SharedState.ffi is None: try: ffi = FFI() ffi.set_unicode(True) ffi.cdef(dedent(""" // kernel32 functions DWORD GetLastError(void); void SetLastError(DWORD); // ws2_32 functions void WSASetLastError(int); int WSAGetLastError(void); """)) SharedState.ffi = ffi SharedState.kernel32 = ffi.dlopen("kernel32") SharedState.ws2_32 = ffi.dlopen("ws2_32") # pylint: disable=broad-except except Exception as error: # pragma: no cover if os.name == "nt": # pylint: disable=redefined-variable-type SharedState.ffi = error cls.HAS_INTERNET = SharedState.HAS_INTERNET cls.ffi = SharedState.ffi cls.kernel32 = SharedState.kernel32 cls.ws2_32 = SharedState.ws2_32
# # (C) Copyright 2015 Enthought, Inc., Austin, TX # All right reserved. # # This file is open source software distributed according to the terms in # LICENSE.txt # """ Utility functions to help with cffi wrapping. """ from __future__ import absolute_import from cffi import FFI ffi = FFI() ffi.set_unicode(True) def HMODULE(cdata): return int(ffi.cast("intptr_t", cdata)) def PVOID(x): return ffi.cast("void *", x) def IS_INTRESOURCE(x): """ Check if x is an index into the id list. """ return int(ffi.cast("uintptr_t", x)) >> 16 == 0
class Sentech(object): def __init__(self): self.ffi = FFI() self.ffi.set_unicode(True) self.library_handler = None self.camera_handle = None self.is_transferring_image = False self.is_inside_callback_function = False self.init_api() def init_api(self): include_file_path = self.get_include_file_path() with open(include_file_path, 'r') as include_file: logging.info("Reading the include file") self.ffi.cdef(include_file.read()) dll_file_path = self.get_dll_file_path() self.library_handler = self.ffi.dlopen(dll_file_path) def get_dll_file_path(self): if platform.architecture()[0] == '64bit': dll_file_path = get_current_module_path( __file__, "../../../lib/camera/x64/StTrgApi.dll") logging.debug("64 bit platform found") elif platform.architecture()[0] == '32bit': dll_file_path = get_current_module_path( __file__, "../../../lib/camera/x86/StTrgApi.dll") logging.debug("32 bit platform found") else: message = "Cannot determine platform architecture: 32-bit or 64-bit." logging.error(message) raise RuntimeError(message) logging.debug("dll_file_path: {:s}".format(dll_file_path)) return dll_file_path def get_include_file_path(self): include_file_path = get_current_module_path( __file__, "../../../include/camera/StTrgApi_stripped.h") logging.info("include_file_path: {:s}".format(include_file_path)) return include_file_path def get_api_version(self): if self.library_handler is None: logging.info("No library handle found, try to initialize the api") self.init_api() file_version_ms = self.ffi.new("PDWORD", 0) file_version_ls = self.ffi.new("PDWORD", 0) _product_version_ms = self.ffi.new("PDWORD", 0) _product_version_ls = self.ffi.new("PDWORD", 0) status = self.library_handler.StTrg_GetDllVersion( file_version_ms, file_version_ls, _product_version_ms, _product_version_ls) if not status: logging.error("Cannot get the dll version") s = Struct("4B") logging.debug(s.unpack(self.ffi.buffer(file_version_ms, 4))) minor, _, major, _ = s.unpack(self.ffi.buffer(file_version_ms, 4)) build = file_version_ls[0] logging.debug(file_version_ls[0]) api_version = (major, minor, 0, build) return api_version def get_api_file_version(self): if self.library_handler is None: logging.info("No library handle found, try to initialize the api") self.init_api() _file_version_ms = self.ffi.new("PDWORD", 0) _file_version_ls = self.ffi.new("PDWORD", 0) product_version_ms = self.ffi.new("PDWORD", 0) product_version_ls = self.ffi.new("PDWORD", 0) status = self.library_handler.StTrg_GetDllVersion( _file_version_ms, _file_version_ls, product_version_ms, product_version_ls) if not status: logging.error("Cannot get the ddl version") s = Struct("4B") logging.debug(s.unpack(self.ffi.buffer(product_version_ms, 4))) minor, _, major, _ = s.unpack(self.ffi.buffer(product_version_ms, 4)) build = product_version_ls[0] logging.debug(product_version_ls[0]) api_file_version = (major, minor, 0, build) return api_file_version def get_camera_version(self): self._find_camera() usb_vendor_id = self.ffi.new("PWORD", 0) usb_product_id = self.ffi.new("PWORD", 0) fpga_version = self.ffi.new("PWORD", 0) firmwre_version = self.ffi.new("PWORD", 0) status = self.library_handler.StTrg_GetCameraVersion( self.camera_handle, usb_vendor_id, usb_product_id, fpga_version, firmwre_version) if not status: logging.error("Cannot get the ddl version") logging.info(usb_vendor_id[0]) logging.info(usb_product_id[0]) logging.info(fpga_version[0]) logging.info(firmwre_version[0]) return usb_vendor_id[0], usb_product_id[0], fpga_version[ 0], firmwre_version[0] def open_camera(self): if self.library_handler is None: logging.info("No library handle found, try to initialize the api") self.init_api() self.camera_handle = self.library_handler.StTrg_Open() if self.camera_handle_value == -1: logging.error("Cannot open the camera") logging.info(self.camera_handle) def close_camera(self): if self.library_handler is None: logging.info("No library handle found, the camera is not open") return if self.camera_handle is None or self.camera_handle_value == -1: logging.info("No camera handle found, the camera is not open") self.camera_handle = None return if self.is_inside_callback_function: logging.warning( "Inside the callback function, cannot close the camera.") return self.library_handler.StTrg_Close(self.camera_handle) self.camera_handle = None def get_product_name(self): self._find_camera() product_name = "" # product_name_buffer = self.ffi.new("PSTR[]", 256) # buffer_size = len(product_name_buffer) # status = self.library_handler.StTrg_GetProductNameA(self.camera_handle, product_name_buffer, buffer_size) # if not status: # logging.error("Cannot get the GetProductNameA") # logging.info(product_name_buffer[0]) # logging.info(product_name_buffer) # #logging.info(self.ffi.string(product_name_buffer)) # logging.info(buffer_size) product_name_buffer = self.ffi.new("PWSTR") buffer_size = 256 status = self.library_handler.StTrg_GetProductNameW( self.camera_handle, product_name_buffer, buffer_size) if not status: logging.error("Cannot get the GetProductNameW") logging.info(product_name_buffer[0]) logging.info(self.ffi.string(product_name_buffer)) logging.info(buffer_size) product_name = self.ffi.string(product_name_buffer) return product_name def has_function(self, camera_function_id): self._find_camera() function_availability = self.ffi.new("BOOL*", False) status = self.library_handler.StTrg_HasFunction( self.camera_handle, camera_function_id, function_availability) if not status: logging.error("Cannot get the GetProductNameW") logging.info(camera_function_id) logging.info(function_availability[0]) return bool(function_availability[0]) def _find_camera(self): if self.library_handler is None: logging.info("No library handle found, try to initialize the api") self.init_api() if self.library_handler is not None and ( self.camera_handle is None or self.camera_handle_value == -1): logging.info("No camera handle found, try to open the camera") self.open_camera() def check_all_functions(self): camera_function_ids = [ 0, 1, 2, 4, 5, 7, 8, 15, 17, 18, 21, 22, 23, 24, 25, 26, 27, 28, 29, 55, 56, 57, 58, 59, 60, 61, 62, 63, 68, 69, 72, 131, 256, 257, 258, 259, 260, 261, 0x00050006, 0x00090004, 0x00090005, 0x00090009, 0x0009000A, 0x0009000C, 0x0009000F, 0x000A000C ] function_availabilities = {} for camera_function_id in camera_function_ids: function_availability = self.has_function(camera_function_id) function_availabilities[camera_function_id] = function_availability return function_availabilities def print_available_functions(self): function_availabilities = self.check_all_functions() for key, value in function_availabilities.items(): if value: print("{:d} -> {:s}".format(key, str(value))) def get_color_array(self): self._find_camera() color_array = self.ffi.new("PWORD", 0) status = self.library_handler.StTrg_GetColorArray( self.camera_handle, color_array) if not status: logging.error("Cannot get the color array") logging.info(color_array[0]) return ColorArray(color_array[0]) def get_camera_user_id(self): self._find_camera() camera_id = self.ffi.new("PDWORD", 0) if six.PY3: camera_name_buffer = self.ffi.new("PWSTR") buffer_size = 250 status = self.library_handler.StTrg_ReadCameraUserIDW( self.camera_handle, camera_id, camera_name_buffer, buffer_size) elif six.PY2: #camera_name_buffer = self.ffi.new("PSTR") camera_name_buffer = self.ffi.new("PWSTR") buffer_size = 250 #status = self.library_handler.StTrg_ReadCameraUserIDA(self.camera_handle, camera_id, camera_name_buffer, buffer_size) status = self.library_handler.StTrg_ReadCameraUserIDW( self.camera_handle, camera_id, camera_name_buffer, buffer_size) if not status: logging.error("Cannot get the camera user ID") logging.info(camera_id[0]) logging.info(camera_name_buffer[0]) logging.info(self.ffi.string(camera_name_buffer)) logging.info(buffer_size) camera_name = self.ffi.string(camera_name_buffer) return camera_id[0], camera_name def is_prohibited_call_timing(self): prohibited = self.is_transferring_image or self.is_inside_callback_function return prohibited def read_setting_file(self, setting_file_path): if self.is_prohibited_call_timing(): logging.warning( "Prohibited function call timing, read_setting_file.") return self._find_camera() if six.PY3: setting_file_path_buffer = self.ffi.new("PCWSTR", setting_file_path[0]) status = self.library_handler.StTrg_ReadSettingFileW( self.camera_handle, setting_file_path_buffer) elif six.PY2: setting_file_path_buffer = self.ffi.new("PCSTR", setting_file_path[0]) status = self.library_handler.StTrg_ReadSettingFileA( self.camera_handle, setting_file_path_buffer) if not status: logging.error("Cannot get the read setting file: {:s}".format( setting_file_path)) logging.info(setting_file_path_buffer[0]) logging.info(self.ffi.string(setting_file_path_buffer)) def write_setting_file(self, setting_file_path): if self.is_prohibited_call_timing(): logging.warning( "Prohibited function call timing, write_setting_file.") return self._find_camera() if six.PY3: setting_file_path_buffer = self.ffi.new("PCWSTR", setting_file_path[0]) status = self.library_handler.StTrg_WriteSettingFileW( self.camera_handle, setting_file_path_buffer) elif six.PY2: setting_file_path_buffer = self.ffi.new("PCSTR", setting_file_path[0]) status = self.library_handler.StTrg_WriteSettingFileA( self.camera_handle, setting_file_path_buffer) if not status: logging.error("Cannot get the write setting file: {:s}".format( setting_file_path)) logging.info(setting_file_path_buffer[0]) logging.info(self.ffi.string(setting_file_path_buffer)) def get_available_scan_mode(self): self._find_camera() enable_scan_mode = self.ffi.new("PWORD", 0) status = self.library_handler.StTrg_GetEnableScanMode( self.camera_handle, enable_scan_mode) if not status: logging.error("Cannot get the available scan mode") logging.info(enable_scan_mode[0]) return hex(enable_scan_mode[0]) def get_scan_mode(self): self._find_camera() scan_mode = self.ffi.new("PWORD", 0) offset_x = self.ffi.new("PDWORD", 0) offset_y = self.ffi.new("PDWORD", 0) width = self.ffi.new("PDWORD", 0) height = self.ffi.new("PDWORD", 0) status = self.library_handler.StTrg_GetScanMode( self.camera_handle, scan_mode, offset_x, offset_y, width, height) if not status: logging.error("Cannot get the scan mode") logging.info(scan_mode[0]) logging.info(offset_x[0]) logging.info(offset_y[0]) logging.info(width[0]) logging.info(height[0]) return ScanMode( scan_mode[0]), offset_x[0], offset_y[0], width[0], height[0] def set_scan_mode(self, scan_mode, offset_x, offset_y, width, height): if self.is_prohibited_call_timing(): logging.warning("Prohibited function call timing, set_scan_mode.") return self._find_camera() status = self.library_handler.StTrg_SetScanMode( self.camera_handle, scan_mode.value, offset_x, offset_y, width, height) if not status: logging.error("Cannot set the scan mode") @property def camera_handle_value(self): if self.camera_handle is None: return None else: camera_handle_value = int(self.ffi.cast('int', self.camera_handle)) return camera_handle_value
class ATCore(object): _ERRORS = { 0: 'AT_SUCCESS', 1: 'AT_ERR_NOTINITIALISED', 2: 'AT_ERR_NOTIMPLEMENTED', 3: 'AT_ERR_READONLY', 4: 'AT_ERR_NOTREADABLE', 5: 'AT_ERR_NOTWRITABLE', 6: 'AT_ERR_OUTOFRANGE', 7: 'AT_ERR_INDEXNOTAVAILABLE', 8: 'AT_ERR_INDEXNOTIMPLEMENTED', 9: 'AT_ERR_EXCEEDEDMAXSTRINGLENGTH', 10: 'AT_ERR_CONNECTION', 11: 'AT_ERR_NODATA', 12: 'AT_ERR_INVALIDHANDLE', 13: 'AT_ERR_TIMEDOUT', 14: 'AT_ERR_BUFFERFULL', 15: 'AT_ERR_INVALIDSIZE', 16: 'AT_ERR_INVALIDALIGNMENT', 17: 'AT_ERR_COMM', 18: 'AT_ERR_STRINGNOTAVAILABLE', 19: 'AT_ERR_STRINGNOTIMPLEMENTED', 20: 'AT_ERR_NULL_FEATURE', 21: 'AT_ERR_NULL_HANDLE', 22: 'AT_ERR_NULL_IMPLEMENTED_VAR', 23: 'AT_ERR_NULL_READABLE_VAR', 24: 'AT_ERR_NULL_READONLY_VAR', 25: 'AT_ERR_NULL_WRITABLE_VAR', 26: 'AT_ERR_NULL_MINVALUE', 27: 'AT_ERR_NULL_MAXVALUE', 28: 'AT_ERR_NULL_VALUE', 29: 'AT_ERR_NULL_STRING', 30: 'AT_ERR_NULL_COUNT_VAR', 31: 'AT_ERR_NULL_ISAVAILABLE_VAR', 32: 'AT_ERR_NULL_MAXSTRINGLENGTH', 33: 'AT_ERR_NULL_EVCALLBACK', 34: 'AT_ERR_NULL_QUEUE_PTR', 35: 'AT_ERR_NULL_WAIT_PTR', 36: 'AT_ERR_NULL_PTRSIZE', 37: 'AT_ERR_NOMEMORY', 38: 'AT_AT_ERR_DEVICEINUSE', 100: 'AT_ERR_HARDWARE_OVERFLOW', } __version__ = '0.1' LIBRARY_NAME = 'atcore' AT_INFINITE=0xFFFFFFFF AT_CALLBACK_SUCCESS = 0 AT_TRUE = 1 AT_FALSE = 0 AT_SUCCESS = 0 AT_ERR_NOTINITIALISED = 1 AT_ERR_NOTIMPLEMENTED = 2 AT_ERR_READONLY = 3 AT_ERR_NOTREADABLE = 4 AT_ERR_NOTWRITABLE = 5 AT_ERR_OUTOFRANGE= 6 AT_ERR_INDEXNOTAVAILABLE= 7 AT_ERR_INDEXNOTIMPLEMENTED= 8 AT_ERR_EXCEEDEDMAXSTRINGLENGTH= 9 AT_ERR_CONNECTION= 10 AT_ERR_NODATA= 11 AT_ERR_INVALIDHANDLE= 12 AT_ERR_TIMEDOUT= 13 AT_ERR_BUFFERFULL= 14 AT_ERR_INVALIDSIZE= 15 AT_ERR_INVALIDALIGNMENT= 16 AT_ERR_COMM= 17 AT_ERR_STRINGNOTAVAILABLE= 18 AT_ERR_STRINGNOTIMPLEMENTED= 19 AT_ERR_NULL_FEATURE= 20 AT_ERR_NULL_HANDLE= 21 AT_ERR_NULL_IMPLEMENTED_VAR= 22 AT_ERR_NULL_READABLE_VAR= 23 AT_ERR_NULL_READONLY_VAR= 24 AT_ERR_NULL_WRITABLE_VAR= 25 AT_ERR_NULL_MINVALUE= 26 AT_ERR_NULL_MAXVALUE= 27 AT_ERR_NULL_VALUE= 28 AT_ERR_NULL_STRING= 29 AT_ERR_NULL_COUNT_VAR= 30 AT_ERR_NULL_ISAVAILABLE_VAR= 31 AT_ERR_NULL_MAXSTRINGLENGTH= 32 AT_ERR_NULL_EVCALLBACK= 33 AT_ERR_NULL_QUEUE_PTR= 34 AT_ERR_NULL_WAIT_PTR= 35 AT_ERR_NULL_PTRSIZE= 36 AT_ERR_NOMEMORY= 37 AT_ERR_DEVICEINUSE= 38 AT_ERR_DEVICENOTFOUND= 39 AT_ERR_HARDWARE_OVERFLOW= 100 AT_HANDLE_UNINITIALISED= -1 AT_HNDL_SYSTEM = 1 def __init__(self): from cffi import FFI self.ffi = FFI() self.ffi.set_unicode(True) self.C = self.ffi.cdef(""" typedef int AT_H; typedef int AT_BOOL; typedef long long AT_64; typedef unsigned char AT_U8; typedef wchar_t AT_WC; int AT_InitialiseLibrary(); int AT_FinaliseLibrary(); int AT_Open(int CameraIndex, AT_H *Hndl); int AT_Close(AT_H Hndl); int AT_IsImplemented(AT_H Hndl, const AT_WC* Feature, AT_BOOL* Implemented); int AT_IsReadable(AT_H Hndl, const AT_WC* Feature, AT_BOOL* Readable); int AT_IsWritable(AT_H Hndl, const AT_WC* Feature, AT_BOOL* Writable); int AT_IsReadOnly(AT_H Hndl, const AT_WC* Feature, AT_BOOL* ReadOnly); int AT_SetInt(AT_H Hndl, const AT_WC* Feature, AT_64 Value); int AT_GetInt(AT_H Hndl, const AT_WC* Feature, AT_64* Value); int AT_GetIntMax(AT_H Hndl, const AT_WC* Feature, AT_64* MaxValue); int AT_GetIntMin(AT_H Hndl, const AT_WC* Feature, AT_64* MinValue); int AT_SetFloat(AT_H Hndl, const AT_WC* Feature, double Value); int AT_GetFloat(AT_H Hndl, const AT_WC* Feature, double* Value); int AT_GetFloatMax(AT_H Hndl, const AT_WC* Feature, double* MaxValue); int AT_GetFloatMin(AT_H Hndl, const AT_WC* Feature, double* MinValue); int AT_SetBool(AT_H Hndl, const AT_WC* Feature, AT_BOOL Value); int AT_GetBool(AT_H Hndl, const AT_WC* Feature, AT_BOOL* Value); /* int AT_SetEnumerated(AT_H Hndl, const AT_WC* Feature, int Value); int AT_SetEnumeratedString(AT_H Hndl, const AT_WC* Feature, const AT_WC* String); int AT_GetEnumerated(AT_H Hndl, const AT_WC* Feature, int* Value); int AT_GetEnumeratedCount(AT_H Hndl,const AT_WC* Feature, int* Count); int AT_IsEnumeratedIndexAvailable(AT_H Hndl, const AT_WC* Feature, int Index, AT_BOOL* Available); int AT_IsEnumeratedIndexImplemented(AT_H Hndl, const AT_WC* Feature, int Index, AT_BOOL* Implemented); int AT_GetEnumeratedString(AT_H Hndl, const AT_WC* Feature, int Index, AT_WC* String, int StringLength); */ int AT_SetEnumIndex(AT_H Hndl, const AT_WC* Feature, int Value); int AT_SetEnumString(AT_H Hndl, const AT_WC* Feature, const AT_WC* String); int AT_GetEnumIndex(AT_H Hndl, const AT_WC* Feature, int* Value); int AT_GetEnumCount(AT_H Hndl,const AT_WC* Feature, int* Count); int AT_IsEnumIndexAvailable(AT_H Hndl, const AT_WC* Feature, int Index, AT_BOOL* Available); int AT_IsEnumIndexImplemented(AT_H Hndl, const AT_WC* Feature, int Index, AT_BOOL* Implemented); int AT_GetEnumStringByIndex(AT_H Hndl, const AT_WC* Feature, int Index, AT_WC* String, int StringLength); int AT_Command(AT_H Hndl, const AT_WC* Feature); int AT_SetString(AT_H Hndl, const AT_WC* Feature, const AT_WC* String); int AT_GetString(AT_H Hndl, const AT_WC* Feature, AT_WC* String, int StringLength); int AT_GetStringMaxLength(AT_H Hndl, const AT_WC* Feature, int* MaxStringLength); int AT_QueueBuffer(AT_H Hndl, AT_U8* Ptr, int PtrSize); int AT_WaitBuffer(AT_H Hndl, AT_U8** Ptr, int* PtrSize, unsigned int Timeout); int AT_Flush(AT_H Hndl); """) #self.lib = self.ffi.verify('#include "atcore.h"', include_dirs=["."], libraries=["atcore"]) self.lib = self.ffi.dlopen('atcore') self.handle_return(self.lib.AT_InitialiseLibrary()) def __del__(self): self.handle_return(self.lib.AT_FinaliseLibrary()) def handle_return(self,ret_value): if ret_value != 0: raise ATCoreException('{} ({})'.format(ret_value, self._ERRORS[ret_value])) return ret_value def get_version(self): return self.__version__ def open(self, index): """Open camera AT_H. """ result = self.ffi.new("AT_H *") self.handle_return(self.lib.AT_Open(index, result)) return result[0] def close(self, AT_H): """Close camera AT_H. """ self.handle_return(self.lib.AT_Close(AT_H)) def is_implemented(self, AT_H, command): """Checks if command is implemented. """ result = self.ffi.new("AT_BOOL *") self.handle_return(self.lib.AT_IsImplemented(AT_H, u(command), result)) return result[0] def is_readable(self, AT_H, command): """Checks if command is readable. """ result = self.ffi.new("AT_BOOL *") self.handle_return(self.lib.AT_IsReadable(AT_H, u(command), result)) return result[0] def is_writable(self, AT_H, command): """Checks if command is writable. """ result = self.ffi.new("AT_BOOL *") self.handle_return(self.lib.AT_IsWritable(AT_H, u(command), result)) return result[0] def is_readonly(self, AT_H, command): """Checks if command is read only. """ result = self.ffi.new("AT_BOOL *") self.handle_return(self.lib.AT_IsReadOnly(AT_H, u(command), result)) return result[0] def set_int(self, AT_H, command, value): """SetInt function. """ self.handle_return(self.lib.AT_SetInt(AT_H, u(command), value)) def get_int(self, AT_H, command): """Run command and get Int return value. """ result = self.ffi.new("AT_64 *") self.handle_return(self.lib.AT_GetInt(AT_H, u(command), result)) return result[0] def get_int_max(self, AT_H, command): """Run command and get maximum Int return value. """ result = self.ffi.new("AT_64 *") self.handle_return(self.lib.AT_GetIntMax(AT_H, u(command), result)) return result[0] def get_int_min(self, AT_H, command): """Run command and get minimum Int return value. """ result = self.ffi.new("AT_64 *") self.handle_return(self.lib.AT_GetIntMin(AT_H, u(command), result)) return result[0] def set_float(self, AT_H, command, value): """Set command with Float value parameter. """ self.handle_return(self.lib.AT_SetFloat(AT_H, u(command), value)) def get_float(self, AT_H, command): """Run command and get float return value. """ result = self.ffi.new("double *") self.handle_return(self.lib.AT_GetFloat(AT_H, u(command), result)) return result[0] def get_float_max(self, AT_H, command): """Run command and get maximum float return value. """ result = self.ffi.new("double *") self.handle_return(self.lib.AT_GetFloatMax(AT_H, u(command), result)) return result[0] def get_float_min(self, AT_H, command): """Run command and get minimum float return value. """ result = self.ffi.new("double *") self.handle_return(self.lib.AT_GetFloatMin(AT_H, u(command), result)) return result[0] def get_bool(self, AT_H, command): """Run command and get Bool return value. """ result = self.ffi.new("AT_BOOL *") self.handle_return(self.lib.AT_GetBool(AT_H, u(command), result)) return result[0] def set_bool(self, AT_H, command, value): """Set command with Bool value parameter. """ self.handle_return(self.lib.AT_SetBool(AT_H, u(command), value)) def set_enum_index(self, AT_H, command, value): """Set command with Enumerated value parameter. """ self.handle_return(self.lib.AT_SetEnumIndex(AT_H, u(command), value)) def set_enum_string(self, AT_H, command, item): """Set command with EnumeratedString value parameter. """ self.handle_return(self.lib.AT_SetEnumString(AT_H, u(command), u(item))) def get_enum_index(self, AT_H, command): """Run command and set Enumerated return value. """ result = self.ffi.new("int *") self.handle_return(self.lib.AT_GetEnumIndex(AT_H, u(command), result)) return result[0] def get_enum_string_options(self, AT_H, command) : """Get list of option strings """ count = self.get_enum_count(AT_H, command) strings = [] for i in range(0, count): strings.append(self.get_enum_string_by_index(AT_H, u(command),i)) return strings def get_enum_string(self, AT_H, command, result_length=128): """Run command and set Enumerated return value. """ ret = self.get_enum_index(AT_H, command) return self.get_enum_string_by_index(AT_H, u(command), ret) def get_enum_count(self, AT_H, command): """Run command and set Enumerated return value. """ result = self.ffi.new("int *") self.handle_return(self.lib.AT_GetEnumCount(AT_H, u(command), result)) return result[0] def is_enum_index_available(self, AT_H, command, index): """Check if enumerated index is available """ result = self.ffi.new("AT_BOOL *") self.handle_return(self.lib.AT_IsEnumIndexAvailable(AT_H, u(command), index, result)) return result[0] def is_enum_index_implemented(self, AT_H, command, index): """Check if enumerated index is implemented """ result = self.ffi.new("AT_BOOL *") self.handle_return(self.lib.AT_IsEnumIndexImplemented(AT_H, u(command), index, result)) return result[0] def get_enum_string_by_index(self, AT_H, command, index, result_length=128): """Get command with EnumeratedString value parameter. """ result = self.ffi.new("AT_WC [%s]" % result_length) self.handle_return(self.lib.AT_GetEnumStringByIndex(AT_H, u(command), index, result, result_length)) return self.ffi.string(result) def command(self, AT_H, command): """Run command. """ self.handle_return(self.lib.AT_Command(AT_H, u(command))) def set_string(self, AT_H, command, strvalue): """SetString function. """ self.handle_return(self.lib.AT_SetString(AT_H, u(command), u(strvalue))) def get_string(self, AT_H, command, result_length=128): """Run command and get string return value. """ result = self.ffi.new("AT_WC [%s]" % result_length) self.handle_return(self.lib.AT_GetString(AT_H, u(command), result, result_length)) return self.ffi.string(result, result_length) def get_string_max_length(self, AT_H, command): """Run command and get maximum Int return value. """ result = self.ffi.new("int *") self.handle_return(self.lib.AT_GetStringMaxLength(AT_H, u(command), result)) return result[0] def queue_buffer(self, AT_H, buf_ptr, buffer_size): """Put buffer in queue. """ self.handle_return(self.lib.AT_QueueBuffer(AT_H, self.ffi.cast("AT_U8 *", buf_ptr), buffer_size)) def wait_buffer(self, AT_H, timeout=20000): """Wait for next buffer to fill. """ buf_ptr = self.ffi.new("AT_U8 **") buffer_size = self.ffi.new("int *") self.handle_return(self.lib.AT_WaitBuffer(AT_H, buf_ptr, buffer_size, int(timeout))) return (buf_ptr, buffer_size[0]) def flush(self, AT_H): self.handle_return(self.lib.AT_Flush(AT_H))
# # (C) Copyright 2015 Enthought, Inc., Austin, TX # All right reserved. # # This file is open source software distributed according to the terms in # LICENSE.txt # """ Utility functions to help with cffi wrapping. """ from __future__ import absolute_import from cffi import FFI from win32ctypes.core.compat import is_bytes, is_integer, text_type ffi = FFI() ffi.set_unicode(True) def HMODULE(cdata): return int(ffi.cast("intptr_t", cdata)) def PVOID(x): return ffi.cast("void *", x) def IS_INTRESOURCE(x): """ Check if x is an index into the id list. """ return int(ffi.cast("uintptr_t", x)) >> 16 == 0
def strip_file(file_name): ffi = FFI() ffi.set_unicode(True) kernel32 = ffi.dlopen("kernel32.dll") imagehlp = ffi.dlopen("imagehlp.dll") ffi.cdef(""" typedef struct _SECURITY_ATTRIBUTES { DWORD nLength; LPVOID lpSecurityDescriptor; BOOL bInheritHandle; } SECURITY_ATTRIBUTES, *PSECURITY_ATTRIBUTES, *LPSECURITY_ATTRIBUTES; HANDLE CreateFileW( LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile ); BOOL ImageEnumerateCertificates( HANDLE FileHandle, WORD TypeFilter, PDWORD CertificateCount, PDWORD Indices, DWORD IndexCount ); BOOL ImageRemoveCertificate( HANDLE FileHandle, DWORD Index ); BOOL WINAPI CloseHandle( HANDLE hObject ); """) GENERIC_READ = 0x80000000 GENERIC_WRITE = 0x40000000 FILE_SHARE_READ = 1 FILE_SHARE_DELETE = 4 OPEN_EXISTING = 3 CERT_SECTION_TYPE_ANY = 255 INVALID_HANDLE_VALUE = ffi.cast("HANDLE", -1) desired_access = GENERIC_READ | GENERIC_WRITE share_mode = FILE_SHARE_READ | FILE_SHARE_DELETE print("[+] Opening file [{}]".format(file_name)) handle = kernel32.CreateFileW(file_name, desired_access, share_mode, ffi.NULL, OPEN_EXISTING, 0, ffi.NULL) if (handle == INVALID_HANDLE_VALUE): print("[-] File not found") sys.exit(1) count = ffi.new("PDWORD") print("[+] Enumerating certificates") if (imagehlp.ImageEnumerateCertificates(handle, CERT_SECTION_TYPE_ANY, count, ffi.NULL, 0)): print("[-] Found {} certificate(s)".format(count[0])) for x in range(count[0]): print("[+] Removing certificate [{}]".format(x + 1)) if (imagehlp.ImageRemoveCertificate(handle, x)): print("[-] Removed Certificate!") else: print("[-] Image is not a PE") kernel32.CloseHandle(handle)
from os.path import dirname, join, realpath from cffi import FFI ffibuilder = FFI() ffibuilder.set_unicode(False) ffibuilder.cdef(r""" int read_bed_chunk(char*, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t*, uint64_t*); """) dirname(realpath(__file__)) ffibuilder.set_source("pandas_plink.bed_reader", "", sources=[join('pandas_plink', '_bed_reader.c')]) if __name__ == "__main__": ffibuilder.compile(verbose=True)