def funcall(self, oid, *args): """ Low-level call to PostgreSQL function, you must supply the oid of the function, and have the args supplied as ints or strings. """ self.__ready = 0 self.__send(_pack('!2sIi', 'F\0', oid, len(args))) for arg in args: atype = type(arg) if (atype == int) and (arg >= 0): # Make sure positive longs, such as OIDs, get # sent back as unsigned ints self.__send(_pack('!iI', 4, arg)) elif (atype == int) or (atype == int): self.__send(_pack('!ii', 4, arg)) else: self.__send(_pack('!i', len(arg))) self.__send(arg) while not self.__ready: self.__read_response() result, self.__func_result = self.__func_result, None return result
def write(handle, devnumber, data): """Writes some data to the receiver, addressed to a certain device. :param handle: an open UR handle. :param devnumber: attached device number. :param data: data to send, up to 5 bytes. The first two (required) bytes of data must be the SubId and address. :raises NoReceiver: if the receiver is no longer available, i.e. has been physically removed from the machine, or the kernel driver has been unloaded. The handle will be closed automatically. """ # the data is padded to either 5 or 18 bytes if len(data) > _SHORT_MESSAGE_SIZE - 2 or data[:1] == b'\x82': wdata = _pack(b'!BB18s', 0x11, devnumber, data) else: wdata = _pack(b'!BB5s', 0x10, devnumber, data) if _log.isEnabledFor(_DEBUG): _log.debug("(%s) <= w[%02X %02X %s %s]", handle, ord(wdata[:1]), devnumber, _strhex(wdata[2:4]), _strhex(wdata[4:])) try: _hid.write(int(handle), wdata) except Exception as reason: _log.error("write failed, assuming handle %r no longer available", handle) close(handle) raise NoReceiver(reason=reason)
def _varintEncode(n): if n < 0xfd: return _pack('<B', n) # NOTE: Technically, there are more encodings for numbers bigger than # 16-bit, but transaction counts can't be that high with version 2 Bitcoin # blocks return b'\xfd' + _pack('<H', n)
def _pkt_R(self): # # Startup Response # code = _unpack('!i', self.__read_bytes(4))[0] if code == 0: self.__authenticated = 1 #print 'Authenticated!' elif code == 1: raise InterfaceError('Kerberos V4 authentication is required by server, but not supported by this client') elif code == 2: raise InterfaceError('Kerberos V5 authentication is required by server, but not supported by this client') elif code == 3: self.__send(_pack('!i', len(self.__passwd)+5) + self.__passwd + '\0') elif code == 4: salt = self.__read_bytes(2) try: import crypt except: raise InterfaceError('Encrypted authentication is required by server, but Python crypt module not available') cpwd = crypt.crypt(self.__passwd, salt) self.__send(_pack('!i', len(cpwd)+5) + cpwd + '\0') elif code == 5: import md5 m = md5.new(self.__passwd + self.__userid).hexdigest() m = md5.new(m + self.__read_bytes(4)).hexdigest() m = 'md5' + m + '\0' self.__send(_pack('!i', len(m)+4) + m) else: raise InterfaceError('Unknown startup response code: R%d (unknown password encryption?)' % code)
def funcall(self, oid, *args): """ Low-level call to PostgreSQL function, you must supply the oid of the function, and have the args supplied as ints or strings. """ if DEBUG: funcname = self.__lo_funcnames.get(oid, str(oid)) print "funcall", funcname, args self.__ready = 0 self.__send(_pack("!2sIi", "F\0", oid, len(args))) for arg in args: atype = type(arg) if (atype == types.LongType) and (arg >= 0): # Make sure positive longs, such as OIDs, get sent back as unsigned ints self.__send(_pack("!iI", 4, arg)) elif (atype == types.IntType) or (atype == types.LongType): self.__send(_pack("!ii", 4, arg)) else: self.__send(_pack("!i", len(arg))) self.__send(arg) while not self.__ready: self.__read_response() result, self.__func_result = self.__func_result, None return result
def libuuid_generate(): """Generate a UUID with libuuid using the best available method. This will raise an exception if libuuid is not available. """ buf = _pack(">16s","") out = _pack(">37s","") _libuuid.call("uuid_generate",buf) _libuuid.call("uuid_unparse",buf,out) return _unpack(">36sB",out)[0]
def libuuid_generate_random(): """Generate a UUID with libuuid using a high-quality source of randomness. This will raise an exception if libuuid is not available. """ buf = _pack(">16s","") out = _pack(">37s","") _libuuid.call("uuid_generate_random",buf) _libuuid.call("uuid_unparse",buf,out) return _unpack(">36sB",out)[0]
def libuuid_generate_time(): """Generate a UUID with libuuid by mixing time and MAC address. This will raise an exception if libuuid is not available. """ buf = _pack(">16s","") out = _pack(">37s","") _libuuid.call("uuid_generate_time",buf) _libuuid.call("uuid_unparse",buf,out) return _unpack(">36sB",out)[0]
def a2s_rules(server_addr, timeout=2, challenge=0): """Get rules from server :param server_addr: (ip, port) for the server :type server_addr: tuple :param timeout: (optional) timeout in seconds :type timeout: float :param challenge: (optional) challenge number :type challenge: int :raises: :class:`RuntimeError`, :class:`socket.timeout` :returns: a list of players :rtype: :class:`list` """ ss = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) ss.connect(server_addr) ss.settimeout(timeout) # request challenge number if challenge in (-1, 0): ss.send(_pack('<lci', -1, b'V', challenge)) try: _, header, challenge = _unpack_from('<lcl', ss.recv(512)) except: ss.close() raise if header != b'A': raise RuntimeError("Unexpected challenge response") # request player info ss.send(_pack('<lci', -1, b'V', challenge)) try: data = StructReader(_handle_a2s_response(ss)) finally: ss.close() header, num_rules = data.unpack('<4xcH') if header != b'E': raise RuntimeError("Invalid reponse header - %s" % repr(header)) rules = {} while len(rules) != num_rules: name = data.read_cstring() value = data.read_cstring() if _re_match(r'^\-?[0-9]+$', value): value = int(value) elif _re_match(r'^\-?[0-9]+\.[0-9]+$', value): value = float(value) rules[name] = value return rules
def _makeapev2tag(apeitems): '''Construct an APE tag string from a dict of ApeItems''' apeentries = [item.maketag() for item in apeitems.itervalues()] apeentries.sort(_sortapeitems) apesize = _pack("<i",reduce(_apelengthreduce, apeentries, 32)) numitems = _pack("<i",len(apeentries)) headerfooter = _apepreamble + apesize + numitems apeentries.insert(0, headerfooter + '\0' + _apeheaderflags + "\x00" * 8) apeentries.append(headerfooter + '\0' + _apefooterflags + "\x00" * 8) return "".join(apeentries)
def code(v, k): """ TEA coder encrypt 64 bits value, by 128 bits key, QQ do 16 round TEA. To see: http://www.ftp.cl.cam.ac.uk/ftp/papers/djw-rmn/djw-rmn-tea.html . TEA 加密, 64比特明码, 128比特密钥, qq的TEA算法使用16轮迭代 具体参看 http://www.ftp.cl.cam.ac.uk/ftp/papers/djw-rmn/djw-rmn-tea.html >>> c = code('abcdefgh', 'aaaabbbbccccdddd') >>> b2a_hex(c) 'a557272c538d3e96' """ n=16 #qq use 16 delta = 0x9e3779b9 k = _unpack('>LLLL', k[0:16]) y, z = _unpack('>LL', v[0:8]) s = 0 for i in range(n): s += delta y += (op &(z<<4))+ k[0] ^ z+ s ^ (op&(z>>5)) + k[1] ; y &= op z += (op &(y<<4))+ k[2] ^ y+ s ^ (op&(y>>5)) + k[3] ; z &= op r = _pack('>LL',y,z) return r
def get_feature_report(device_handle, bytes_count, report_number=None): out_buffer = _C.create_string_buffer('\x00' * (bytes_count + 2)) if report_number is not None: out_buffer[0] = _pack(b'!B', report_number) bytes_read = _native.hid_get_feature_report(device_handle, out_buffer, bytes_count) if bytes_read > -1: return out_buffer[:bytes_read]
def inet_ntop(af, packed_ip): """Convert an packed IP address of the given family to string format.""" if af == AF_INET: # IPv4. return inet_ntoa(packed_ip) elif af == AF_INET6: # IPv6. if len(packed_ip) != 16 or not hasattr(packed_ip, 'split'): raise ValueError('invalid length of packed IP address string') tokens = ['%x' % i for i in _unpack('>8H', packed_ip)] # Convert packed address to an integer value. words = list(_unpack('>8H', packed_ip)) int_val = 0 for i, num in enumerate(reversed(words)): word = num word = word << 16 * i int_val = int_val | word if 0xffff < int_val <= 0xffffffff or int_val >> 32 == 0xffff: # IPv4 compatible / mapped IPv6. packed_ipv4 = _pack('>2H', *[int(i, 16) for i in tokens[-2:]]) ipv4_str = inet_ntoa(packed_ipv4) tokens = tokens[0:-2] + [ipv4_str] return ':'.join(_compact_ipv6_tokens(tokens)) else: raise ValueError('unknown address family %d' % af)
def _inet_pton_af_inet(ip_string): """ Convert an IP address in string format (123.45.67.89) to the 32-bit packed binary format used in low-level network functions. Differs from inet_aton by only support decimal octets. Using octal or hexadecimal values will raise a ValueError exception. """ #TODO: optimise this ... use inet_aton with mods if available ... if hasattr(ip_string, 'split'): invalid_addr = ValueError('illegal IP address string %r' % ip_string) # Support for hexadecimal and octal octets. tokens = ip_string.split('.') # Pack octets. if len(tokens) == 4: words = [] for token in tokens: if token.startswith('0x') or \ (token.startswith('0') and len(token) > 1): raise invalid_addr try: octet = int(token) except ValueError: raise invalid_addr if (octet >> 8) != 0: raise invalid_addr words.append(_pack('B', octet)) return _bytes_join(words) else: raise invalid_addr raise ValueError('argument should be a string, not %s' % type(ip_string))
def _extranonce(tmpl, workid): coinbase = tmpl.cbtxn.data if not workid: return coinbase extradata = _pack('<Q', workid) coinbase = _append_cb(tmpl, extradata) return coinbase
def decipher(v, k): """ TEA decipher, decrypt 64bits value with 128 bits key. TEA 解密程序, 用128比特密钥, 解密64比特值 it's the inverse function of TEA encrypt. 他是TEA加密函数的反函数. >>> c = code('abcdefgh', 'aaaabbbbccccdddd') >>> decipher( c, 'aaaabbbbccccdddd') 'abcdefgh' """ n = 16 y, z = _unpack('>LL', v[0:8]) a, b, c, d = _unpack('>LLLL', k[0:16]) delta = 0x9E3779B9L; s = (delta << 4)&op for i in xrange(n): z -= ((y<<4)+c) ^ (y+s) ^ ((y>>5) + d) z &= op y -= ((z<<4)+a) ^ (z+s) ^ ((z>>5) + b) y &= op s -= delta s &= op return _pack('>LL', y, z)
def _get_random_bytes(n): reader = _get_random_reader() buf = '' if reader: loose_counter = 0 while len(buf) != n: buf += reader(n) if loose_counter > 10: break loose_counter += 1 d = n-len(buf) if d>0: buf += '\0'*d if n==16: fmt = _fmt_16 elif n==6: fmt = _fmt_6 elif n==2: fmt = _fmt_2 else: fmt = ">%sB" % n return _pack(fmt,*tuple(map(_randomize_byte,_unpack(fmt,buf))))
def get_mdata(tmpl, usetime = None, out_expire = None, extranoncesz = sizeof_workid, can_roll_ntime = True): if usetime is None: usetime = _time() if not (True and time_left(tmpl, usetime) and (not tmpl.cbtxn is None) and _build_merkle_branches(tmpl) ): return None if extranoncesz == sizeof_workid: # Avoid overlapping with blkmk_get_data use extranoncesz += 1 cbuf = _pack('<I', tmpl.version) cbuf += tmpl.prevblk dummy = b'\0' * extranoncesz cbextranonceoffset = [None] cbtxn = _append_cb(tmpl, dummy, cbextranonceoffset) if cbtxn is None: return None cbuf += b'\0' * 0x20 cbuf += _set_times(tmpl, usetime, out_expire, can_roll_ntime) cbuf += tmpl.diffbits return (cbuf, cbtxn, cbextranonceoffset[0], tmpl._mrklbranch)
def a2s_ping(server_addr, timeout=2): """Ping a server .. warning:: This method for pinging is considered deprecated and may not work on certian servers. Use :func:`.a2s_info` instead. :param server_addr: (ip, port) for the server :type server_addr: tuple :param timeout: (optional) timeout in seconds :type timeout: float :raises: :class:`RuntimeError`, :class:`socket.timeout` :returns: ping response in milliseconds or `None` for timeout :rtype: :class:`float` """ ss = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) ss.connect(server_addr) ss.settimeout(timeout) ss.send(_pack('<lc', -1, b'i')) start = _time() try: data = _handle_a2s_response(ss) finally: ss.close() ping = max(0.0, _time() - start) * 1000 if data[4:5] == b'j': return ping
def _sample_data(tmpl, dataid): cbuf = _pack('<I', tmpl.version) cbuf += tmpl.prevblk cbtxndata = _extranonce(tmpl, dataid) if not cbtxndata: return None merkleroot = _build_merkle_root(tmpl, cbtxndata) if not merkleroot: return None cbuf += merkleroot cbuf += _pack('<I', tmpl.curtime) cbuf += tmpl.diffbits return cbuf
def writeZRDFile(rayArray, file_name, file_type): """write an array of `ZemaxRay` objects to a zrd file. The uncompressed mode can only be used if all required data is available. Therefore this function can be used to convert an uncompressed zrd file to a compressed file but not vice versa. Usage: ``writeZRD(rayArray, file_name, file_type)`` Parameters ---------- rayArray : list list of `ZemaxRay` objects to be saved to zrd file file_name: string name of the zrd file (provide full path) file_type: string type of the zrd file ('uncompressed' or 'compressed'). For compressed type, currently the function only writes as compressed full data (CFD) format Returns ------- n/a Examples -------- >>> writeZRD(rayArray, 'rays.zrd','uncompressed') """ if file_type is not 'uncompressed': # Temporary .... to remove after complete implementation raise NotImplementedError('Function cannot write to compressed file format') comp_type = 'uncompressed_zrd' if (file_type == 'uncompressed') else 'compressed_zrd' zrd_type = 0 if (file_type == 'uncompressed') else 20000 c_int, c_uint = _ctypes.c_int, _ctypes.c_uint c_double, c_float = _ctypes.c_double, _ctypes.c_float format_dict = {c_int:'i', c_uint:'I', c_double:'d', c_float:'f'} file_handle = open(file_name, "wb") file_handle.write(_pack('i', rayArray[0].zrd_version+zrd_type)) file_handle.write(_pack('i', rayArray[0].n_segments)) # number of rays for ray in rayArray: file_handle.write(_pack('i', len(ray.status))) # number of segments in the ray fields = getattr(ray, comp_type) for ss in range(len(ray.status)): for field, field_format in fields: format_char = format_dict[field_format] file_handle.write(_pack(format_char, getattr(ray, field)[ss])) file_handle.close()
def _assemble_submission2(tmpl, data, extranonce, dataid, nonce, foreign): if dataid: if extranonce: raise RuntimeError('Cannot specify both extranonce and dataid') extranonce = _pack('<Q', workid) elif extranonce and len(extranonce) == sizeof_workid: # Avoid overlapping with blkmk_get_data use extranonce += b'\0' return _assemble_submission2_internal(tmpl, data, extranonce, nonce, foreign)
def __del__(self): if self._ses is None: return try: ioctl(_cryptodev, CIOCFSESSION, _pack('I', self._ses)) except TypeError: pass self._ses = None
def ping(handle, devnumber): """Check if a device is connected to the UR. :returns: The HID protocol supported by the device, as a floating point number, if the device is active. """ if _log.isEnabledFor(_DEBUG): _log.debug("(%s) pinging device %d", handle, devnumber) # import inspect as _inspect # print ('\n '.join(str(s) for s in _inspect.stack())) # randomize the SoftwareId and mark byte to be able to identify the ping # reply, and set most significant (0x8) bit in SoftwareId so that the reply # is always distinguishable from notifications request_id = 0x0018 | _random_bits(3) request_data = _pack(b'!HBBB', request_id, 0, 0, _random_bits(8)) ihandle = int(handle) notifications_hook = getattr(handle, 'notifications_hook', None) _skip_incoming(handle, ihandle, notifications_hook) write(ihandle, devnumber, request_data) while True: now = _timestamp() reply = _read(handle, _PING_TIMEOUT) delta = _timestamp() - now if reply: report_id, reply_devnumber, reply_data = reply if reply_devnumber == devnumber: if reply_data[:2] == request_data[:2] and reply_data[4:5] == request_data[-1:]: # HID++ 2.0+ device, currently connected return ord(reply_data[2:3]) + ord(reply_data[3:4]) / 10.0 if report_id == 0x10 and reply_data[:1] == b'\x8F' and reply_data[1:3] == request_data[:2]: assert reply_data[-1:] == b'\x00' error = ord(reply_data[3:4]) if error == _hidpp10.ERROR.invalid_SubID__command: # a valid reply from a HID++ 1.0 device return 1.0 if error == _hidpp10.ERROR.resource_error: # device unreachable # raise DeviceUnreachable(number=devnumber, request=request_id) break if error == _hidpp10.ERROR.unknown_device: # no paired device with that number _log.error("(%s) device %d error on ping request: unknown device", handle, devnumber) raise NoSuchDevice(number=devnumber, request=request_id) if notifications_hook: n = make_notification(reply_devnumber, reply_data) if n: notifications_hook(n) if delta >= _PING_TIMEOUT: _log.warn("(%s) timeout on device %d ping", handle, devnumber)
def encode(file, features): ''' Encode a list of (WKB, property dict) features into an MVT stream. Geometries in the features list are assumed to be in spherical mercator. Floating point precision in the output is approximated to 26 bits. ''' parts = [] for feature in features: wkb = approximate_wkb(feature[0]) prop = json.dumps(feature[1]) parts.extend([_pack('>I', len(wkb)), wkb, _pack('>I', len(prop)), prop]) body = _compress(_pack('>I', len(features)) + b''.join(parts)) file.write(b'\x89MVT') file.write(_pack('>I', len(body))) file.write(body)
def maketag(self): '''Return on disk representation of tag item self.parsetag(self.maketag(), 0) should result in no change to self ''' if self.type == 'utf8' or self.type == 'external': values = '\x00'.join([value.encode('utf8') for value in self]) else: values = '\x00'.join(self) size = _pack("<i",len(values)) flags = chr(int(self.readonly) + 2 * (_apeitemtypes.index(self.type))) return '%s\x00\x00\x00%s%s\x00%s' % (size, flags, self.key, values)
def _set_common_suffix(): global _static_time_buf if _common_suffix_method == USE_RANDOM: _static_time_buf = _get_random_bytes(6) # I don't know how to set the multicast bit # so I am leaving that out else: suf = _get_mac_address() if _common_suffix_method == USE_SHA: suf = _get_6bytes(suf) if suf: _static_time_buf = _pack(_fmt_6,*suf) else: _static_time_buf = _get_random_bytes(6)
def code(v, k): n = 16 # qq use 16 delta = 0x9e3779b9L k = _unpack('>LLLL', k[0:16]) y, z = _unpack('>LL', v[0:8]) s = 0 for i in xrange(n): s += delta y += (op & (z << 4)) + k[0] ^ z + s ^ (op & (z >> 5)) + k[1]; y &= op z += (op & (y << 4)) + k[2] ^ y + s ^ (op & (y >> 5)) + k[3]; z &= op r = _pack('>LL', y, z) return r
def _set_times(tmpl, usetime = None, out_expire = None, can_roll_ntime = False): time_passed = int(usetime - tmpl._time_rcvd) timehdr = tmpl.curtime + time_passed if (timehdr > tmpl.maxtime): timehdr = tmpl.maxtime return _pack('<I', timehdr) if not out_expire is None: out_expire[0] = tmpl.expires - time_passed - 1 if can_roll_ntime: # If the caller can roll the time header, we need to expire before reaching the maxtime maxtime_expire_limit = (tmpl.maxtime - timehdr) + 1 if out_expire[0] > maxtime_expire_limit: out_expire[0] = maxtime_expire_limit
def init_generation3(tmpl, script, override_cb=False): if (not tmpl.cbtxn is None) and not (override_cb and ('generate' in tmpl.mutations)): return (0, False) if len(script) >= 0xfd: return (0, True) sh = b'' h = tmpl.height while h > 127: sh += _pack('<B', h & 0xff) h >>= 8 sh += _pack('<B', h) sh = _pack('<B', len(sh)) + sh if getattr(tmpl, 'auxs', None): auxcat = b'' for aux in tmpl.auxs.values(): auxcat += aux if len(auxcat): sh += _pack('<B', len(auxcat)) + auxcat if len(sh) > coinbase_size_limit: return (0, True) data = b'' data += b"\x01\0\0\0" # txn ver data += b"\x01" # input count data += b"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" # prevout data += b"\xff\xff\xff\xff" # index (-1) data += _pack('<B', len(sh)) # scriptSig length data += sh data += b"\xff\xff\xff\xff" # sequence data += b"\x01" # output count data += _pack('<Q', tmpl.cbvalue) data += _pack('<B', len(script)) data += script data += b'\0\0\0\0' # lock time if tmpl.txns_datasz + len(data) > tmpl.sizelimit: return (0, True) txn = _Transaction(None) txn.data = data tmpl.cbtxn = txn tmpl.mutations.add('coinbase/append') tmpl.mutations.add('coinbase') tmpl.mutations.add('generate') return (tmpl.cbvalue, True)
def pack_uint64_be(n: int): return _pack(">Q", n) def pack_int8_be(n: int): return _pack(">b", n)
def pack_int16_be(n: int): return _pack(">h", n) def pack_int32_be(n: int): return _pack(">i", n)
def pack_int8_be(n: int): return _pack(">b", n) def pack_int16_be(n: int): return _pack(">h", n)
def pack_int32_be(n: int): return _pack(">i", n) def pack_int64_be(n: int): return _pack(">q", n)
def writeBeamFile(beamfilename, version, n, ispol, units, d, zposition, rayleigh, waist, lamda, index, receiver_eff, system_eff, efield): """Write a Zemax Beam file Parameters ---------- beamfilename : string the filename of the beam file to read version : integer the file format version number n : 2-tuple, (nx, ny) the number of samples in the x and y directions ispol : boolean is the beam polarized? units : integer the units of the beam, 0 = mm, 1 = cm, 2 = in, 3 = m d : 2-tuple, (dx, dy) the x and y grid spacing zposition : 2-tuple, (zpositionx, zpositiony) the x and y z position of the beam rayleigh : 2-tuple, (rayleighx, rayleighy) the x and y rayleigh ranges of the beam waist : 2-tuple, (waistx, waisty) the x and y waists of the beam lamda : double the wavelength of the beam index : double the index of refraction in the current medium receiver_eff : double the receiver efficiency. Zero if fiber coupling is not computed system_eff : double the system efficiency. Zero if fiber coupling is not computed. efield : 4-tuple of 2D lists, (Ex_real, Ex_imag, Ey_real, Ey_imag) a tuple containing two dimensional lists with the real and imaginary parts of the x and y polarizations of the beam Returns ------- status : integer 0 = success; -997 = file write failure; -996 = couldn't convert data to integer, -995 = unexpected error. """ try: f = open(beamfilename, "wb") # zemax version number f.write(_pack('i', version)) f.write(_pack('i', n[0])) f.write(_pack('i', n[1])) f.write(_pack('i', ispol)) f.write(_pack('i', units)) # write 16 zeroes to pad out file f.write(_pack('4i', 4, 5, 6, 7)) f.write(_pack('d', d[0])) f.write(_pack('d', d[1])) if version == 0: f.write(_pack('d', zposition[0])) f.write(_pack('d', rayleigh[0])) f.write(_pack('d', lamda)) f.write(_pack('d', zposition[1])) f.write(_pack('d', rayleigh[1])) f.write(_pack('d', waist[0])) f.write(_pack('d', waist[1])) f.write(_pack('d', index)) if version == 1: f.write(_pack('d', zposition[0])) f.write(_pack('d', rayleigh[0])) f.write(_pack('d', waist[0])) f.write(_pack('d', zposition[1])) f.write(_pack('d', rayleigh[1])) f.write(_pack('d', waist[1])) f.write(_pack('d', lamda)) f.write(_pack('d', index)) f.write(_pack('d', receiver_eff)) f.write(_pack('d', system_eff)) f.write(_pack('8d', 1, 2, 3, 4, 5, 6, 7, 8)) (Ex_real, Ex_imag, Ey_real, Ey_imag) = efield for i in range(n[0]): for j in range(n[1]): f.write(_pack('d', Ex_real[i][j])) f.write(_pack('d', Ex_imag[i][j])) if ispol: for i in range(n[0]): for j in range(n[1]): f.write(_pack('d', Ey_real[i][j])) f.write(_pack('d', Ey_imag[i][j])) f.close() return 0 except IOError as e: print("I/O error({0}): {1}".format(e.errno, e.strerror)) return -997 except ValueError: print("Could not convert data to an integer.") return -996 except: print("Unexpected error:", _sys.exc_info()[0]) return -995
def inet_pton(af, ip_string): """ Convert an IP address from string format to a packed string suitable for use with low-level network functions. """ if af == AF_INET: # IPv4. return _inet_pton_af_inet(ip_string) elif af == AF_INET6: invalid_addr = ValueError('illegal IP address string %r' % ip_string) # IPv6. values = [] if not hasattr(ip_string, 'split'): raise invalid_addr if 'x' in ip_string: # Don't accept hextets with the 0x prefix. raise invalid_addr if '::' in ip_string: if ip_string == '::': # Unspecified address. return '\x00'.encode() * 16 # IPv6 compact mode. try: prefix, suffix = ip_string.split('::') except ValueError: raise invalid_addr l_prefix = [] l_suffix = [] if prefix != '': l_prefix = prefix.split(':') if suffix != '': l_suffix = suffix.split(':') # IPv6 compact IPv4 compatibility mode. if len(l_suffix) and '.' in l_suffix[-1]: ipv4_str = _inet_pton_af_inet(l_suffix.pop()) l_suffix.append('%x' % _unpack('>H', ipv4_str[0:2])[0]) l_suffix.append('%x' % _unpack('>H', ipv4_str[2:4])[0]) token_count = len(l_prefix) + len(l_suffix) if not 0 <= token_count <= 8 - 1: raise invalid_addr gap_size = 8 - (len(l_prefix) + len(l_suffix)) values = [_pack('>H', int(i, 16)) for i in l_prefix] \ + ['\x00\x00'.encode() for i in range(gap_size)] \ + [_pack('>H', int(i, 16)) for i in l_suffix] try: for token in l_prefix + l_suffix: word = int(token, 16) if not 0 <= word <= 0xffff: raise invalid_addr except ValueError: raise invalid_addr else: # IPv6 verbose mode. if ':' in ip_string: tokens = ip_string.split(':') if '.' in ip_string: ipv6_prefix = tokens[:-1] if ipv6_prefix[:-1] != ['0', '0', '0', '0', '0']: raise invalid_addr if ipv6_prefix[-1].lower() not in ('0', 'ffff'): raise invalid_addr # IPv6 verbose IPv4 compatibility mode. if len(tokens) != 7: raise invalid_addr ipv4_str = _inet_pton_af_inet(tokens.pop()) tokens.append('%x' % _unpack('>H', ipv4_str[0:2])[0]) tokens.append('%x' % _unpack('>H', ipv4_str[2:4])[0]) values = [_pack('>H', int(i, 16)) for i in tokens] else: # IPv6 verbose mode. if len(tokens) != 8: raise invalid_addr try: tokens = [int(token, 16) for token in tokens] for token in tokens: if not 0 <= token <= 0xffff: raise invalid_addr except ValueError: raise invalid_addr values = [_pack('>H', i) for i in tokens] else: raise invalid_addr return _bytes_join(values) else: raise ValueError('Unknown address family %d' % af)
def _entry2bstr(d, entry): name, vals = entry return _pack("<HB", d[name], len(vals)) + b"".join( (_val2bstr(d, v) for v in vals))
def request(handle, devnumber, request_id, *params, no_reply=False, return_error=False, long_message=False, protocol=1.0): """Makes a feature call to a device and waits for a matching reply. :param handle: an open UR handle. :param devnumber: attached device number. :param request_id: a 16-bit integer. :param params: parameters for the feature call, 3 to 16 bytes. :returns: the reply data, or ``None`` if some error occurred. or no reply expected """ # import inspect as _inspect # print ('\n '.join(str(s) for s in _inspect.stack())) with acquire_timeout(handle_lock(handle), handle, 10.): assert isinstance(request_id, int) if (devnumber != 0xFF or protocol >= 2.0) and request_id < 0x8000: # For HID++ 2.0 feature requests, randomize the SoftwareId to make it # easier to recognize the reply for this request. also, always set the # most significant bit (8) in SoftwareId, to make notifications easier # to distinguish from request replies. # This only applies to peripheral requests, ofc. request_id = (request_id & 0xFFF0) | 0x08 | _random_bits(3) timeout = _RECEIVER_REQUEST_TIMEOUT if devnumber == 0xFF else _DEVICE_REQUEST_TIMEOUT # be extra patient on long register read if request_id & 0xFF00 == 0x8300: timeout *= 2 if params: params = b''.join( _pack('B', p) if isinstance(p, int) else p for p in params) else: params = b'' # if _log.isEnabledFor(_DEBUG): # _log.debug("(%s) device %d request_id {%04X} params [%s]", handle, devnumber, request_id, _strhex(params)) request_data = _pack('!H', request_id) + params ihandle = int(handle) notifications_hook = getattr(handle, 'notifications_hook', None) try: _skip_incoming(handle, ihandle, notifications_hook) except NoReceiver: _log.warn('device or receiver disconnected') return None write(ihandle, devnumber, request_data, long_message) if no_reply: return None # we consider timeout from this point request_started = _timestamp() delta = 0 while delta < timeout: reply = _read(handle, timeout) if reply: report_id, reply_devnumber, reply_data = reply if reply_devnumber == devnumber: if report_id == HIDPP_SHORT_MESSAGE_ID and reply_data[:1] == b'\x8F' and reply_data[ 1:3] == request_data[:2]: error = ord(reply_data[3:4]) if _log.isEnabledFor(_DEBUG): _log.debug( '(%s) device 0x%02X error on request {%04X}: %d = %s', handle, devnumber, request_id, error, _hidpp10.ERROR[error]) return _hidpp10.ERROR[error] if return_error else None if reply_data[:1] == b'\xFF' and reply_data[ 1:3] == request_data[:2]: # a HID++ 2.0 feature call returned with an error error = ord(reply_data[3:4]) _log.error( '(%s) device %d error on feature request {%04X}: %d = %s', handle, devnumber, request_id, error, _hidpp20.ERROR[error]) raise _hidpp20.FeatureCallError(number=devnumber, request=request_id, error=error, params=params) if reply_data[:2] == request_data[:2]: if devnumber == 0xFF: if request_id == 0x83B5 or request_id == 0x81F1: # these replies have to match the first parameter as well if reply_data[2:3] == params[:1]: return reply_data[2:] else: # hm, not matching my request, and certainly not a notification continue else: return reply_data[2:] else: return reply_data[2:] else: # a reply was received, but did not match our request in any way # reset the timeout starting point request_started = _timestamp() if notifications_hook: n = make_notification(report_id, reply_devnumber, reply_data) if n: notifications_hook(n) # elif _log.isEnabledFor(_DEBUG): # _log.debug("(%s) ignoring reply %02X [%s]", handle, reply_devnumber, _strhex(reply_data)) # elif _log.isEnabledFor(_DEBUG): # _log.debug("(%s) ignoring reply %02X [%s]", handle, reply_devnumber, _strhex(reply_data)) delta = _timestamp() - request_started # if _log.isEnabledFor(_DEBUG): # _log.debug("(%s) still waiting for reply, delta %f", handle, delta) _log.warn( 'timeout (%0.2f/%0.2f) on device %d request {%04X} params [%s]', delta, timeout, devnumber, request_id, _strhex(params))
def pack_uint32_be(n: int): return _pack(">I", n) def pack_uint64_be(n: int): return _pack(">Q", n)
def ping(handle, devnumber, long_message=False): """Check if a device is connected to the receiver. :returns: The HID protocol supported by the device, as a floating point number, if the device is active. """ if _log.isEnabledFor(_DEBUG): _log.debug('(%s) pinging device %d', handle, devnumber) # import inspect as _inspect # print ('\n '.join(str(s) for s in _inspect.stack())) with acquire_timeout(handle_lock(handle), handle, 10.): # randomize the SoftwareId and mark byte to be able to identify the ping # reply, and set most significant (0x8) bit in SoftwareId so that the reply # is always distinguishable from notifications request_id = 0x0018 | _random_bits(3) request_data = _pack('!HBBB', request_id, 0, 0, _random_bits(8)) ihandle = int(handle) notifications_hook = getattr(handle, 'notifications_hook', None) try: _skip_incoming(handle, ihandle, notifications_hook) except NoReceiver: _log.warn('device or receiver disconnected') return write(ihandle, devnumber, request_data, long_message) # we consider timeout from this point request_started = _timestamp() delta = 0 while delta < _PING_TIMEOUT: reply = _read(handle, _PING_TIMEOUT) if reply: report_id, reply_devnumber, reply_data = reply if reply_devnumber == devnumber: if reply_data[:2] == request_data[:2] and reply_data[ 4:5] == request_data[-1:]: # HID++ 2.0+ device, currently connected return ord( reply_data[2:3]) + ord(reply_data[3:4]) / 10.0 if report_id == HIDPP_SHORT_MESSAGE_ID and reply_data[:1] == b'\x8F' and reply_data[ 1:3] == request_data[:2]: assert reply_data[-1:] == b'\x00' error = ord(reply_data[3:4]) if error == _hidpp10.ERROR.invalid_SubID__command: # a valid reply from a HID++ 1.0 device return 1.0 if error == _hidpp10.ERROR.resource_error: # device unreachable return if error == _hidpp10.ERROR.unknown_device: # no paired device with that number _log.error( '(%s) device %d error on ping request: unknown device', handle, devnumber) raise NoSuchDevice(number=devnumber, request=request_id) if notifications_hook: n = make_notification(report_id, reply_devnumber, reply_data) if n: notifications_hook(n) # elif _log.isEnabledFor(_DEBUG): # _log.debug("(%s) ignoring reply %02X [%s]", handle, reply_devnumber, _strhex(reply_data)) delta = _timestamp() - request_started _log.warn('(%s) timeout (%0.2f/%0.2f) on device %d ping', handle, delta, _PING_TIMEOUT, devnumber)
def pack_uint16_be(n: int): return _pack(">H", n) def pack_uint32_be(n: int): return _pack(">I", n)
assert enc == ct print('etg:', binascii.hexlify(enctag)) print('tag:', binascii.hexlify(tag)) assert enctag == tag elif False: for i in range(100000): c = Crypto( CRYPTO_AES_XTS, binascii.unhexlify( '1bbfeadf539daedcae33ced497343f3ca1f2474ad932b903997d44707db41382' )) data = binascii.unhexlify('52a42bca4e9425a25bbc8c8bf6129dec') ct = binascii.unhexlify('517e602becd066b65fa4f4f56ddfe240') iv = _pack('QQ', 71, 0) enc = c.encrypt(data, iv) assert enc == ct elif True: c = Crypto( CRYPTO_AES_XTS, binascii.unhexlify( '1bbfeadf539daedcae33ced497343f3ca1f2474ad932b903997d44707db41382' )) data = binascii.unhexlify('52a42bca4e9425a25bbc8c8bf6129dec') ct = binascii.unhexlify('517e602becd066b65fa4f4f56ddfe240') iv = _pack('QQ', 71, 0) enc = c.encrypt(data, iv) assert enc == ct
def pack(format, *args): try: return _pack(format, *args) except error as e: e.args = e.args[0]+" "+repr(args)+" format: "+format, raise e
def __init__(self, dsn=None, username='', password='', host=None, dbname='', port='', opt=''): self.__backend_pid = None self.__backend_key = None self.__socket = None self.__input_buffer = '' self.__authenticated = 0 self.__ready = 0 self.__result = None self.__current_result = None self.__notify_queue = [] self.__func_result = None self.__lo_funcs = {} self.__lo_funcnames = {} self._pg_types = {} self._oid_map = {} self._python_converters = [] # # Come up with a reasonable default host for # win32 and presumably Unix platforms # if host == None: if sys.platform == 'win32': host = '127.0.0.1' else: host = '/tmp/.s.PGSQL.5432' args = _parseDSN(dsn) if 'host' not in args: args['host'] = host if 'port' not in args: args['port'] = port or 5432 if 'dbname' not in args: args['dbname'] = dbname if 'user' not in args: args['user'] = username if 'password' not in args: args['password'] = password if 'options' not in args: args['options'] = opt if args['host'].startswith('/'): s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.connect(args['host']) else: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((args['host'], int(args['port']))) if not args['user']: # # If no userid specified in the args, try to use the userid # this process is running under, if we can figure that out. # try: import os, pwd args['user'] = pwd.getpwuid(os.getuid())[0] except: pass self.__socket = s self.__passwd = args['password'] self.__userid = args['user'] # # Send startup packet specifying protocol version 2.0 # (works with PostgreSQL 6.3 or higher?) # self.__send( _pack('!ihh64s32s64s64s64s', 296, 2, 0, args['dbname'], args['user'], args['options'], '', '')) while not self.__ready: self.__read_response() # # Get type info from the backend to help put together some dictionaries # to help in converting Pgsql types to Python types. # self._initialize_types() self.__initialize_type_map()
def _section2bstr(d, section): secname, entries = section return _pack("<HH", d[secname], len(entries)) + b"".join( (_entry2bstr(d, e) for e in entries))
def send_feature_report(device_handle, data, report_number=None): if report_number is not None: data = _pack(b'!B', report_number) + data bytes_written = _native.hid_send_feature_report(device_handle, _C.c_char_p(data), len(data)) return bytes_written > -1
def pack(fmt, *values): return bytes(_pack(fmt, *values))
def pack_int64_le(n: int): return _pack("<q", n) def pack_float_le(n: float): return _pack("<f", n)
def pack_double_be(n: float): return _pack(">d", n) def unpack_uint8(packed_bytes: bytes, offset = 0): return _unpack_from("=B", packed_bytes, offset)[0]
def _uuid_pack(low, mid, hi, seq, node): return _pack(">IHHH6s", low, mid, hi, seq, node)
def pack_uint8_be(n: int): return _pack(">B", n) def pack_uint16_be(n: int): return _pack(">H", n)
def pack_float_be(n: float): return _pack(">f", n) def pack_double_be(n: float): return _pack(">d", n)
def pack_double_le(n: float): return _pack("<d", n) def pack_uint8_be(n: int): return _pack(">B", n)
def pack_int64_be(n: int): return _pack(">q", n) def pack_float_be(n: float): return _pack(">f", n)
def pack_float_le(n: float): return _pack("<f", n) def pack_double_le(n: float): return _pack("<d", n)
def xor(a, b): a1, a2 = _unpack('>LL', a[0:8]) b1, b2 = _unpack('>LL', b[0:8]) r = _pack('>LL', (a1 ^ b1) & op, (a2 ^ b2) & op) return r
def to_mrc(fid, volume, labels=[], fmt=None): if fmt is None: fmt = os.path.splitext(fid)[-1][1:] if fmt not in ('ccp4', 'mrc', 'map'): raise ValueError('Format is not recognized. Use ccp4, mrc, or map.') dtype = volume.array.dtype.name if dtype == 'int8': mode = 0 elif dtype in ('int16', 'int32'): mode = 1 elif dtype in ('float32', 'float64'): mode = 2 else: raise TypeError("Data type ({:})is not supported.".format(dtype)) if fmt == 'ccp4': nxstart, nystart, nzstart = volume.offset origin = [0, 0, 0] uc = volume.unit_cell xl, yl, zl = uc.a, uc.b, uc.c alpha, beta, gamma = uc.alpha, uc.beta, uc.gamma ispg = uc.space_group.number ns, nr, nc = volume.unit_cell_shape[::-1] elif fmt in ('mrc', 'map'): nxstart, nystart, nzstart = [0, 0, 0] origin = volume.origin xl, yl, zl = [ vs * n for vs, n in zip(volume.voxelspacing, reversed(volume.shape)) ] alpha = beta = gamma = 90 ispg = 1 ns, nr, nc = volume.shape voxelspacing = volume.voxelspacing nz, ny, nx = volume.shape mapc, mapr, maps = [1, 2, 3] nsymbt = 0 lskflg = 0 skwmat = [0.0] * 9 skwtrn = [0.0] * 3 fut_use = [0.0] * 12 str_map = list('MAP ') str_map = 'MAP ' #TODO machst are similar for little and big endian if _BYTEORDER == 'little': machst = list('\x44\x41\x00\x00') elif _BYTEORDER == 'big': machst = list('\x44\x41\x00\x00') else: raise ValueError("Byteorder {:} is not recognized".format(byteorder)) labels = [' '] * 800 nlabels = 0 min_density = volume.array.min() max_density = volume.array.max() mean_density = volume.array.mean() std_density = volume.array.std() with open(fid, 'wb') as out: out.write(_pack('i', nx)) out.write(_pack('i', ny)) out.write(_pack('i', nz)) out.write(_pack('i', mode)) out.write(_pack('i', nxstart)) out.write(_pack('i', nystart)) out.write(_pack('i', nzstart)) out.write(_pack('i', nc)) out.write(_pack('i', nr)) out.write(_pack('i', ns)) out.write(_pack('f', xl)) out.write(_pack('f', yl)) out.write(_pack('f', zl)) out.write(_pack('f', alpha)) out.write(_pack('f', beta)) out.write(_pack('f', gamma)) out.write(_pack('i', mapc)) out.write(_pack('i', mapr)) out.write(_pack('i', maps)) out.write(_pack('f', min_density)) out.write(_pack('f', max_density)) out.write(_pack('f', mean_density)) out.write(_pack('i', ispg)) out.write(_pack('i', nsymbt)) out.write(_pack('i', lskflg)) for f in skwmat: out.write(_pack('f', f)) for f in skwtrn: out.write(_pack('f', f)) for f in fut_use: out.write(_pack('f', f)) for f in origin: out.write(_pack('f', f)) for c in str_map: out.write(_pack('c', c.encode('ascii'))) for c in machst: out.write(_pack('c', c.encode('ascii'))) out.write(_pack('f', std_density)) # max 10 labels # nlabels = min(len(labels), 10) # TODO labels not handled correctly #for label in labels: # list_label = [c for c in label] # llabel = len(list_label) # if llabel < 80: # # # max 80 characters # label = min(len(label), 80) out.write(_pack('i', nlabels)) for c in labels: out.write(_pack('c', c.encode('ascii'))) # write density modes = [np.int8, np.int16, np.float32] volume.array.astype(modes[mode]).tofile(out)
def _val2bstr(d, v): t = type(v) if t == int: return _pack("<Bi", 1, v) elif t == float: return _pack("<Bf", 2, v) elif t == str: return _pack("<Bi", 3, d[v])
def to_mrc(fid, volume, labels=[], fmt=None): if fmt is None: fmt = os.path.splitext(fid)[-1][1:] if fmt not in ('ccp4', 'mrc', 'map'): raise ValueError('Format is not recognized. Use ccp4, mrc, or map.') voxelspacing = volume.voxelspacing nz, ny, nx = volume.shape dtype = volume.array.dtype.name if dtype == 'int8': mode = 0 elif dtype in ('int16', 'int32'): mode = 1 elif dtype in ('float32', 'float64'): mode = 2 else: raise TypeError("Data type ({:})is not supported.".format(dtype)) if fmt in ('ccp4', 'map'): nxstart, nystart, nzstart = [int(round(x)) for x in volume.start] else: nxstart, nystart, nzstart = [0, 0, 0] xl, yl, zl = volume.dimensions alpha = beta = gamma = 90.0 mapc, mapr, maps = [1, 2, 3] ispg = 1 nsymbt = 0 lskflg = 0 skwmat = [0.0] * 9 skwtrn = [0.0] * 3 fut_use = [0.0] * 12 if fmt == 'mrc': origin = volume.origin else: origin = [0, 0, 0] str_map = list('MAP ') if _BYTEORDER == 'little': machst = list('\x44\x41\x00\x00') elif _BYTEORDER == 'big': machst = list('\x44\x41\x00\x00') else: raise ValueError("Byteorder {:} is not recognized".format(byteorder)) labels = [' '] * 800 nlabels = 0 min_density = volume.array.min() max_density = volume.array.max() mean_density = volume.array.mean() std_density = volume.array.std() with open(fid, 'wb') as out: out.write(_pack('i', nx)) out.write(_pack('i', ny)) out.write(_pack('i', nz)) out.write(_pack('i', mode)) out.write(_pack('i', nxstart)) out.write(_pack('i', nystart)) out.write(_pack('i', nzstart)) out.write(_pack('i', nx)) out.write(_pack('i', ny)) out.write(_pack('i', nz)) out.write(_pack('f', xl)) out.write(_pack('f', yl)) out.write(_pack('f', zl)) out.write(_pack('f', alpha)) out.write(_pack('f', beta)) out.write(_pack('f', gamma)) out.write(_pack('i', mapc)) out.write(_pack('i', mapr)) out.write(_pack('i', maps)) out.write(_pack('f', min_density)) out.write(_pack('f', max_density)) out.write(_pack('f', mean_density)) out.write(_pack('i', ispg)) out.write(_pack('i', nsymbt)) out.write(_pack('i', lskflg)) for f in skwmat: out.write(_pack('f', f)) for f in skwtrn: out.write(_pack('f', f)) for f in fut_use: out.write(_pack('f', f)) for f in origin: out.write(_pack('f', f)) for c in str_map: out.write(_pack('c', c)) for c in machst: out.write(_pack('c', c)) out.write(_pack('f', std_density)) # max 10 labels # nlabels = min(len(labels), 10) # TODO labels not handled correctly #for label in labels: # list_label = [c for c in label] # llabel = len(list_label) # if llabel < 80: # # # max 80 characters # label = min(len(label), 80) out.write(_pack('i', nlabels)) for c in labels: out.write(_pack('c', c)) # write density modes = [np.int8, np.int16, np.float32] volume.array.astype(modes[mode]).tofile(out)
def pack_int32_le(n: int): return _pack("<i", n) def pack_int64_le(n: int): return _pack("<q", n)