我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用ctypes.c_size_t()。
def send(self, data): processed_bytes = ctypes.c_size_t(0) with self._raise_on_error(): result = Security.SSLWrite( self.context, data, len(data), ctypes.byref(processed_bytes) ) if result == SecurityConst.errSSLWouldBlock and processed_bytes.value == 0: # Timed out raise socket.timeout("send timed out") else: _assert_no_error(result) # We sent, and probably succeeded. Tell them how much we sent. return processed_bytes.value
def decrypt_ige(cipher_text, key, iv): """ Decrypts the given text in 16-bytes blocks by using the given key and 32-bytes initialization vector. """ aeskey = AES_KEY() ckey = (ctypes.c_ubyte * len(key))(*key) cklen = ctypes.c_int(len(key)*8) cin = (ctypes.c_ubyte * len(cipher_text))(*cipher_text) ctlen = ctypes.c_size_t(len(cipher_text)) cout = (ctypes.c_ubyte * len(cipher_text))() civ = (ctypes.c_ubyte * len(iv))(*iv) _libssl.AES_set_decrypt_key(ckey, cklen, ctypes.byref(aeskey)) _libssl.AES_ige_encrypt( ctypes.byref(cin), ctypes.byref(cout), ctlen, ctypes.byref(aeskey), ctypes.byref(civ), AES_DECRYPT ) return bytes(cout)
def add(self, ref, pred): if not isinstance(ref, torch.IntTensor): raise TypeError('ref must be a torch.IntTensor (got {})' .format(type(ref))) if not isinstance(pred, torch.IntTensor): raise TypeError('pred must be a torch.IntTensor(got {})' .format(type(pred))) assert self.unk > 0, 'unknown token index must be >0' rref = ref.clone() rref.apply_(lambda x: x if x != self.unk else -x) rref = rref.contiguous().view(-1) pred = pred.contiguous().view(-1) C.bleu_add( ctypes.byref(self.stat), ctypes.c_size_t(rref.size(0)), ctypes.c_void_p(rref.data_ptr()), ctypes.c_size_t(pred.size(0)), ctypes.c_void_p(pred.data_ptr()), ctypes.c_int(self.pad), ctypes.c_int(self.eos))
def all_reduce(input, output=None, op=SUM, stream=None): comm = communicator() if output is None: output = input if stream is not None: stream = stream.cuda_stream data_type = nccl_types[input.type()] check_error(lib.ncclAllReduce( ctypes.c_void_p(input.data_ptr()), ctypes.c_void_p(output.data_ptr()), ctypes.c_size_t(input.numel()), data_type, op, comm, ctypes.c_void_p(stream))) return output
def _set(self, dropout, seed): if self.state is None and dropout > 0: dropout_states_size = ctypes.c_long() check_error(lib.cudnnDropoutGetStatesSize( self.handle, ctypes.byref(dropout_states_size))) self.state = torch.cuda.ByteTensor(dropout_states_size.value) state_ptr = self.state.data_ptr() state_size = self.state.size(0) else: state_ptr = None state_size = 0 check_error(lib.cudnnSetDropoutDescriptor( self, self.handle, ctypes.c_float(dropout), ctypes.c_void_p(state_ptr), ctypes.c_size_t(state_size), ctypes.c_ulonglong(seed), )) self.dropout = dropout
def configure(self, requested_depth): """ Specifies the depth of the host memory part of the DMA FIFO. Args: requested_depth (int): The depth of the host memory part of the DMA FIFO in number of elements. Returns: actual_depth (int): The actual number of elements in the host memory part of the DMA FIFO, which may be more than the requested number. """ actual_depth = ctypes.c_size_t() self._nifpga.ConfigureFifo2(self._session, self._number, requested_depth, actual_depth) return actual_depth.value
def __init__(self): v_posix.PosixMixin.__init__(self) v_posix.PtraceMixin.__init__(self) self.libc = ctypes.CDLL(c_util.find_library('c')) self.myport = self.libc.mach_task_self() self.libc.mach_port_allocate.argtypes = [ipc_space_t, mach_port_right_t, ctypes.POINTER(mach_port_name_t)] self.libc.mach_port_allocate.restype = kern_return_t self.libc.mach_vm_read.argtypes = [ mach_port_t, size_t, size_t, ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_uint32)] self.libc.mach_vm_read.restype = kern_return_t self.libc.ptrace.restype = ctypes.c_int self.libc.ptrace.argtypes = [ctypes.c_int, ctypes.c_uint32, ctypes.c_size_t, ctypes.c_int] machhelp_path = os.path.join(darwindir, 'machhelper.dylib') self.machhelper = ctypes.CDLL(machhelp_path) self.machhelper.platformPs.restype = ctypes.POINTER(ProcessListEntry) self.useptrace = False self.portset = self.newMachPort(MACH_PORT_RIGHT_PORT_SET) self.excport = self.newMachRWPort() self.addPortToSet(self.excport)
def nprocessors(): try: try: # Mac OS libc=ctypes.cdll.LoadLibrary(ctypes.util.find_library('libc')) v=ctypes.c_int(0) size=ctypes.c_size_t(ctypes.sizeof(v)) libc.sysctlbyname('hw.ncpu', ctypes.c_voidp(ctypes.addressof(v)), ctypes.addressof(size), None, 0) return v.value except: # Cygwin (Windows) and Linuxes # Could try sysconf(_SC_NPROCESSORS_ONLN) (LSB) next. Instead, count processors in cpuinfo. s = open('/proc/cpuinfo', 'r').read() return s.replace(' ', '').replace('\t', '').count('processor:') except: return 1
def generate_signature(signature_plain, signature_lib): iv = os.urandom(32) output_size = ctypes.c_size_t() signature_lib.encrypt(signature_plain, len(signature_plain), iv, 32, None, ctypes.byref(output_size)) output = (ctypes.c_ubyte * output_size.value)() signature_lib.encrypt(signature_plain, len(signature_plain), iv, 32, ctypes.byref(output), ctypes.byref(output_size)) signature = b''.join(list(map(lambda x: six.int2byte(x), output))) return signature
def recv_into(self, buffer, nbytes=None): # Read short on EOF. if self._closed: return 0 if nbytes is None: nbytes = len(buffer) buffer = (ctypes.c_char * nbytes).from_buffer(buffer) processed_bytes = ctypes.c_size_t(0) with self._raise_on_error(): result = Security.SSLRead( self.context, buffer, nbytes, ctypes.byref(processed_bytes) ) # There are some result codes that we want to treat as "not always # errors". Specifically, those are errSSLWouldBlock, # errSSLClosedGraceful, and errSSLClosedNoNotify. if (result == SecurityConst.errSSLWouldBlock): # If we didn't process any bytes, then this was just a time out. # However, we can get errSSLWouldBlock in situations when we *did* # read some data, and in those cases we should just read "short" # and return. if processed_bytes.value == 0: # Timed out, no data read. raise socket.timeout("recv timed out") elif result in (SecurityConst.errSSLClosedGraceful, SecurityConst.errSSLClosedNoNotify): # The remote peer has closed this connection. We should do so as # well. Note that we don't actually return here because in # principle this could actually be fired along with return data. # It's unlikely though. self.close() else: _assert_no_error(result) # Ok, we read and probably succeeded. We should return whatever data # was actually read. return processed_bytes.value
def _generate_signature(self, signature_plain, iv, lib_path="encrypt.so"): if self._signature_lib is None: self.activate_signature(lib_path) self._signature_lib.argtypes = [ctypes.c_char_p, ctypes.c_size_t, ctypes.c_char_p, ctypes.POINTER(ctypes.POINTER(ctypes.c_ubyte))] self._signature_lib.restype = ctypes.c_int rounded_size = len(signature_plain) + (256 - (len(signature_plain) % 256)); total_size = rounded_size + 5; output = ctypes.POINTER(ctypes.c_ubyte * total_size)() output_size = self._signature_lib.encrypt(signature_plain, len(signature_plain), iv, ctypes.byref(output)) signature = b''.join(list(map(lambda x: six.int2byte(x), output.contents))) return signature
def hotpatch(source, destination): source = cast(source, c_void_p).value destination = cast(destination, c_void_p).value old = DWORD() if windll.kernel32.VirtualProtect(source - 5, 8, PAGE_EXECUTE_READWRITE, byref(old)): try: written = c_size_t() jmp_code = struct.pack('<BI', 0xE9, (destination - source) & 0xFFFFFFFF) windll.kernel32.WriteProcessMemory(-1, source - 5, cast(jmp_code, c_char_p), len(jmp_code), byref(written)) windll.kernel32.WriteProcessMemory(-1, source, cast(struct.pack('<H', 0xF9EB), c_char_p), 2, byref(written)) finally: windll.kernel32.VirtualProtect(source - 5, 8, old, byref(old)) return source + 2
def unhotpatch(source): source = cast(source, c_void_p).value old = DWORD() if windll.kernel32.VirtualProtect(source, 2, PAGE_EXECUTE_READWRITE, byref(old)): try: written = c_size_t() windll.kernel32.WriteProcessMemory(-1, source, cast(b'\x8B\xFF', c_char_p), 2, byref(written)) finally: windll.kernel32.VirtualProtect(source, 2, old, byref(old))
def _import_cast(self): """Use ctypes to access the C function. Raise any sort of error: we just support this where ctypes works as expected. """ import ctypes lib = ctypes.pydll.LoadLibrary(psycopg2._psycopg.__file__) cast = lib.typecast_BINARY_cast cast.argtypes = [ctypes.c_char_p, ctypes.c_size_t, ctypes.py_object] cast.restype = ctypes.py_object return cast
def write(self, geom): "Returns the WKB representation of the given geometry." return six.memoryview(wkb_writer_write(self.ptr, geom.ptr, byref(c_size_t())))
def write_hex(self, geom): "Returns the HEXEWKB representation of the given geometry." return wkb_writer_write_hex(self.ptr, geom.ptr, byref(c_size_t())) # ### WKBWriter Properties ### # Property for getting/setting the byteorder.
def load_inotify(): ''' Initialize the inotify library ''' global _inotify if _inotify is None: if hasattr(sys, 'getwindowsversion'): # On windows abort before loading the C library. Windows has # multiple, incompatible C runtimes, and we have no way of knowing # if the one chosen by ctypes is compatible with the currently # loaded one. raise INotifyError('INotify not available on windows') if sys.platform == 'darwin': raise INotifyError('INotify not available on OS X') if not hasattr(ctypes, 'c_ssize_t'): raise INotifyError('You need python >= 2.7 to use inotify') name = find_library('c') if not name: raise INotifyError('Cannot find C library') libc = ctypes.CDLL(name, use_errno=True) for function in ('inotify_add_watch', 'inotify_init1', 'inotify_rm_watch'): if not hasattr(libc, function): raise INotifyError('libc is too old') # inotify_init1() prototype = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int, use_errno=True) init1 = prototype(('inotify_init1', libc), ((1, 'flags', 0),)) # inotify_add_watch() prototype = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_char_p, ctypes.c_uint32, use_errno=True) add_watch = prototype(('inotify_add_watch', libc), ( (1, 'fd'), (1, 'pathname'), (1, 'mask'))) # inotify_rm_watch() prototype = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_int, use_errno=True) rm_watch = prototype(('inotify_rm_watch', libc), ( (1, 'fd'), (1, 'wd'))) # read() prototype = ctypes.CFUNCTYPE(ctypes.c_ssize_t, ctypes.c_int, ctypes.c_void_p, ctypes.c_size_t, use_errno=True) read = prototype(('read', libc), ( (1, 'fd'), (1, 'buf'), (1, 'count'))) _inotify = (init1, add_watch, rm_watch, read) return _inotify
def isSysWow64(): k32 = ctypes.windll.kernel32 if not hasattr(k32, 'IsWow64Process'): return False ret = ctypes.c_ulong(0) myproc = ctypes.c_size_t(-1) if not k32.IsWow64Process(myproc, ctypes.addressof(ret)): return False return bool(ret.value)
def encrypt_ige(plain_text, key, iv): """ Encrypts the given text in 16-bytes blocks by using the given key and 32-bytes initialization vector. """ # Add random padding iff it's not evenly divisible by 16 already if len(plain_text) % 16 != 0: padding_count = 16 - len(plain_text) % 16 plain_text += os.urandom(padding_count) aeskey = AES_KEY() ckey = (ctypes.c_ubyte * len(key))(*key) cklen = ctypes.c_int(len(key)*8) cin = (ctypes.c_ubyte * len(plain_text))(*plain_text) ctlen = ctypes.c_size_t(len(plain_text)) cout = (ctypes.c_ubyte * len(plain_text))() civ = (ctypes.c_ubyte * len(iv))(*iv) _libssl.AES_set_encrypt_key(ckey, cklen, ctypes.byref(aeskey)) _libssl.AES_ige_encrypt( ctypes.byref(cin), ctypes.byref(cout), ctlen, ctypes.byref(aeskey), ctypes.byref(civ), AES_ENCRYPT ) return bytes(cout)