def create_msg(self, node, subnode, cmd, data, size): """Creates a command message following the MCB protocol. Args: node (int): Reserved bits used to identify the destination device. subnode (int): Subsystem bits used to identify the destination device. cmd (int): Command to lead the message. data (bytes): Data to be added to the message. size (int): Size of data. Returns: bin: MCB command message. """ node_head = (node << 4) | (subnode & 0xf) node_head = struct.pack('<H', node_head) if size > self.EXTENDED_MESSAGE_SIZE: cmd = cmd + 1 head = struct.pack('<H', cmd) head_size = struct.pack('<H', size) head_size = head_size + bytes( [0] * (self.EXTENDED_MESSAGE_SIZE - len(head_size))) ret = node_head + head + head_size + struct.pack( '<H', binascii.crc_hqx(node_head + head + head_size, 0)) + data else: head = struct.pack('<H', cmd) ret = node_head + head + data + struct.pack( '<H', binascii.crc_hqx(node_head + head + data, 0)) return ret
def raw_msg(node, subnode, cmd, data, size): """Creates a raw message with the proper format. Args: node (int): Node ID. subnode (int): Subnode to be targeted. cmd (int): Command of the message. data (bytes): Data of the message. size (int): Size of the message. Returns: bytes: Message frame. """ node_head = (node << 4) | (subnode & 0xf) node_head = struct.pack('<H', node_head) if size > 8: cmd = cmd + 1 head = struct.pack('<H', cmd) head_size = struct.pack('<H', size) head_size = head_size + bytes([0] * (8 - len(head_size))) return node_head + head + head_size + struct.pack( '<H', binascii.crc_hqx(node_head + head + head_size, 0)) + data else: head = struct.pack('<H', cmd) return node_head + head + data + struct.pack( '<H', binascii.crc_hqx(node_head + head + data, 0))
def crcSignFullPacketComKey(pData, pKey): crc = ba.hexlify(struct.pack('>I', ba.crc_hqx(ba.unhexlify(pData), 0x1021))) pData = pData + crc[6:8] + crc[4:6] crc = crc[6:8] + crc[4:6] + ba.hexlify( pKey ) crc = ba.hexlify(struct.pack('>I', ba.crc_hqx(ba.unhexlify(crc), 0x1021))) pData = pData + crc[6:8] + crc[4:6] return pData
def _crc_sign_full_packet_com_key(data: str) -> str: """Calculate the crc for packets before send. Args: data: packet data to sign. Return: The calculated and signed packet data. Raises: aioswitcher.erros.EncodingError: when failed to sign the packet. Note: This is a private function containing blocking code. Please consider using ``crc_sign_full_packet_com_key`` (without the `_`), to schedule as a task in the event loop. """ try: crc = hexlify(pack(">I", crc_hqx(unhexlify(data), 0x1021))).decode(ENCODING_CODEC) data = data + crc[6:8] + crc[4:6] crc = (crc[6:8] + crc[4:6] + (hexlify(REMOTE_KEY)).decode(ENCODING_CODEC)) crc = hexlify(pack(">I", crc_hqx(unhexlify(crc), 0x1021))).decode(ENCODING_CODEC) data = data + crc[6:8] + crc[4:6] return data except HANDLED_EXCEPTIONS as ex: raise EncodingError("failed to sign crc") from ex
def test_crc_hqx(self): crc = binascii.crc_hqx(self.type2test(b"Test the CRC-32 of"), 0) crc = binascii.crc_hqx(self.type2test(b" this string."), crc) self.assertEqual(crc, 14290) self.assertRaises(TypeError, binascii.crc_hqx) self.assertRaises(TypeError, binascii.crc_hqx, self.type2test(b''))
def skontrolujFeedbackSubor(clientSocket, clientAddressPort, bufferOfData, aktualnyPacket): feedbackBajty = b'' clientSocket.settimeout(0.1) try: feedbackBajty = clientSocket.recvfrom(HLAVICKA_FEEDBACK) except: print("Neposlal sa posledny packet z patice") resendPoslednyPacket5checksum = binascii.crc_hqx(b'4' + b'0' + bytes(aktualnyPacket) + bufferOfData[9], 0) # menil som tu veci hlavickaResendPosledny5 = struct.pack("cHi", b'4', resendPoslednyPacket5checksum, int(aktualnyPacket)) clientSocket.sendto(hlavickaResendPosledny5 + bufferOfData[9], clientAddressPort) clientSocket.settimeout(None) if (feedbackBajty == b''): feedbackBajty = clientSocket.recvfrom(HLAVICKA_FEEDBACK) (dataType, checksum, check, firstBadPacket) = struct.unpack("cHci", feedbackBajty[0][0:HLAVICKA_FEEDBACK]) if int(dataType) == 2 and int(check) == 1 and checksum == binascii.crc_hqx(b'2' + b'0' + b'1' + bytes(firstBadPacket), 0): print("Vsetkych packety prisli v pohode.") else: print("Niektory packet neprisiel alebo nastali problemy") prvyZly = int(firstBadPacket) while prvyZly <= aktualnyPacket: resendSuborChecksum = binascii.crc_hqx(b'4' + b'0' + bytes(prvyZly) + bufferOfData[prvyZly % 10 - 1], 0) resendSuborHlavicka = struct.pack("cHi", b'4', resendSuborChecksum, int(prvyZly)) clientSocket.sendto(resendSuborHlavicka + bufferOfData[prvyZly % 10 - 1], clientAddressPort) prvyZly += 1 return
def accept(self, char): if char == b'~' and not self.started: self.started = True self.finished = False return if char != b'~': self.buf += char return if char == b'~' and not self.finished: self.finished = True self.started = False if int.from_bytes(self.buf[-2:], "big") != binascii.crc_hqx( self.buf[:-2], 0xFFFF): print('CRC16 of received frame is incorect') print(f'FRAME: {self.buf}') print("CRC: 0x{:02x} (recv) != 0x{:02x}".format( int.from_bytes(self.buf[-2:], "big"), binascii.crc_hqx(self.buf[:-2], 0xFFFF))) self.buf = b'' return self.last_cmd = self.buf[:-2].decode() self.buf = b'' return self.last_cmd
def __calc_crc(data, key ="00000000000000000000000000000000"): crc = bytearray(struct.pack('>I', binascii.crc_hqx(data, 4129))) data = data + crc[3:4] + crc[2:3] crc = crc[3:4] + crc[2:3] + bytearray(key, 'utf8') crc = bytearray(struct.pack('>I', binascii.crc_hqx(crc, 4129))) data = data + crc[3:4] + crc[2:3] return bytearray(data)
def crcSignFullPacketComKey(pData, pKey): crc = ba.hexlify(struct.pack('>I', ba.crc_hqx(ba.unhexlify(pData), 0x1021))) pData = pData + crc[6:8].decode("utf-8") + crc[4:6].decode("utf-8") crc = crc[6:8] + crc[4:6] + ba.hexlify(bytes(pKey.encode('UTF-8'))) crc = ba.hexlify(struct.pack('>I', ba.crc_hqx(ba.unhexlify(crc), 0x1021))) pData = pData + crc[6:8].decode("utf-8") + crc[4:6].decode("utf-8") return pData
def calc_crc(data, crc=0): for char in data: crc = ((crc << 8) | char) ^ binascii.crc_hqx( bytes([(crc >> 8) & 0xFF]), 0) for i in range(0, 2): crc = ( (crc << 8) | 0) ^ binascii.crc_hqx(bytes([(crc >> 8) & 0xFF]), 0) return crc & 0xFFFF
def test_empty_string(self): # A test for SF bug #1022953. Make sure SystemError is not raised. for n in ['b2a_qp', 'a2b_hex', 'b2a_base64', 'a2b_uu', 'a2b_qp', 'b2a_hex', 'unhexlify', 'hexlify', 'crc32', 'b2a_hqx', 'a2b_hqx', 'a2b_base64', 'rlecode_hqx', 'b2a_uu', 'rledecode_hqx']: f = getattr(binascii, n) f('') binascii.crc_hqx('', 0)
def test_crc_hqx(self): crc = binascii.crc_hqx(self.type2test(b'Test the CRC-32 of'), 0) crc = binascii.crc_hqx(self.type2test(b' this string.'), crc) self.assertEqual(crc, 14290) self.assertRaises(TypeError, binascii.crc_hqx) self.assertRaises(TypeError, binascii.crc_hqx, self.type2test(b'')) for crc in (0, 1, 4660, 74565, 305419896, -1): self.assertEqual(binascii.crc_hqx(self.type2test(b''), crc), crc & 65535)
def vytvorSpojenie(clientSocket, clientAddressPort): checksumSpojenie = binascii.crc_hqx((b'1' + b'1'), 0) hlavicka = struct.pack("ccH", b'1', b'1', checksumSpojenie) clientSocket.sendto(hlavicka, clientAddressPort) odozvaSpojenie = clientSocket.recvfrom(hlavickaPreSpojenie) (dataType, conn, checksum) = struct.unpack("ccH", odozvaSpojenie[0][0:hlavickaPreSpojenie]) if int(dataType) == 1 and int(conn) == 2 and checksum == binascii.crc_hqx(b'1' + b'2', 0): return "ok" else: return "deny"
def test_crc_hqx(self): crc = binascii.crc_hqx(self.type2test(b"Test the CRC-32 of"), 0) crc = binascii.crc_hqx(self.type2test(b" this string."), crc) self.assertEqual(crc, 14290) self.assertRaises(TypeError, binascii.crc_hqx) self.assertRaises(TypeError, binascii.crc_hqx, self.type2test(b"")) for crc in 0, 1, 0x1234, 0x12345, 0x12345678, -1: self.assertEqual(binascii.crc_hqx(self.type2test(b""), crc), crc & 0xFFFF)
def skontrolujPacketSpravy(serverSocket ,currentPacket, ocakavanyPacket, checksum, prijateBajty): if int(currentPacket) == int(ocakavanyPacket) and checksum == binascii.crc_hqx(prijateBajty[0][hlavickaPreSpravu:], 0): checksumFeedBack = binascii.crc_hqx(b'2' + b'1', 0) serverSocket.sendto(struct.pack("ccH", b'2', b'1', checksumFeedBack), prijateBajty[1]) return prijateBajty else: print("Treba znova poslat packet") checksumFeedBack = binascii.crc_hqx(b'2' + b'0', 0) serverSocket.sendto(struct.pack("ccH", b'2', b'0', checksumFeedBack), prijateBajty[1]) noveBajty = serverSocket.recvfrom(1500) return noveBajty
def test_crc_hqx(self): crc = binascii.crc_hqx(self.type2test(b"Test the CRC-32 of"), 0) crc = binascii.crc_hqx(self.type2test(b" this string."), crc) self.assertEqual(crc, 14290) self.assertRaises(TypeError, binascii.crc_hqx) self.assertRaises(TypeError, binascii.crc_hqx, self.type2test(b'')) for crc in 0, 1, 0x1234, 0x12345, 0x12345678, -1: self.assertEqual(binascii.crc_hqx(self.type2test(b''), crc), crc & 0xffff)
def test_empty_string(self): empty = self.type2test(b'') for func in all_functions: if func == 'crc_hqx': binascii.crc_hqx(empty, 0) continue f = getattr(binascii, func) try: f(empty) except Exception as err: self.fail('{}({!r}) raises {!r}'.format(func, empty, err))
def posliPrvyPacketSubor(clientSocket, clientAddressPort, nazovSuboru, aktualnyPacket): checksumSubor = binascii.crc_hqx(nazovSuboru.encode(), 0) hlavicka = struct.pack("ccHi", b'4', b'0', checksumSubor, int(aktualnyPacket)) clientSocket.sendto(hlavicka + nazovSuboru.encode(), clientAddressPort) odozvaNazovSuboru = clientSocket.recvfrom(hlavickaPreFeedback) (dataType, check, checksum) = struct.unpack("ccH", odozvaNazovSuboru[0][0:hlavickaPreFeedback]) if int(dataType) == 2 and int(check) == 1 and checksum == binascii.crc_hqx(dataType + check, 0): print("Nazov suboru prisiel v poriadku") else: print("Nazov suboru neprisiel") clientSocket.sendto(hlavicka + nazovSuboru.encode(), odozvaNazovSuboru[1])
def test_empty_string(self): # A test for SF bug #1022953. Make sure SystemError is not raised. for n in ['b2a_qp', 'a2b_hex', 'b2a_base64', 'a2b_uu', 'a2b_qp', 'b2a_hex', 'unhexlify', 'hexlify', 'crc32', 'b2a_hqx', 'a2b_hqx', 'a2b_base64', 'rlecode_hqx', 'b2a_uu', 'rledecode_hqx']: f = getattr(binascii, n) try: f(b'') except SystemError as err: self.fail("%s(b'') raises SystemError: %s" % (n, err)) binascii.crc_hqx(b'', 0)
def test_empty_string(self): # A test for SF bug #1022953. Make sure SystemError is not raised. empty = self.type2test(b'') for func in all_functions: if func == 'crc_hqx': # crc_hqx needs 2 arguments binascii.crc_hqx(empty, 0) continue f = getattr(binascii, func) try: f(empty) except Exception as err: self.fail("{}({!r}) raises {!r}".format(func, empty, err))
def crc_sign_full_packet_com_key(pData): """CRC calculation""" try: crc = (ba.hexlify(pack('>I', ba.crc_hqx(ba.unhexlify(pData), 0x1021)))).decode('utf-8') pData = pData + crc[6:8] + crc[4:6] crc = crc[6:8] + crc[4:6] + (ba.hexlify(REMOTE_KEY)).decode('utf-8') crc = (ba.hexlify(pack('>I', ba.crc_hqx(ba.unhexlify(crc), 0x1021)))).decode('utf-8') pData = pData + crc[6:8] + crc[4:6] return pData except: _LOGGER.exception('failed to sign crc ' + traceback.format_exc()) return None
def run(self): global bolPoslanySubor while True: if bolPoslanySubor == "ano": keepAliveChecksum = binascii.crc_hqx(b'5' + b'0', 0) keepAliveHlavicka = struct.pack("cH", b'5', keepAliveChecksum) self.clientSocket.sendto(keepAliveHlavicka, self.clientAddressPort) odozvaKeepAlive = self.clientSocket.recvfrom(HLAVICKA_KEEPALIVE) (dataType, checksum) = struct.unpack("cH", odozvaKeepAlive[0][0:HLAVICKA_KEEPALIVE]) if int(dataType) == 5 and checksum == binascii.crc_hqx(b'5' + b'0', 0): print("Spojenie stale funguje") else: print("Spatny feedback na keepalive") bolPoslanySubor = "nie" time.sleep(20)
def _crc_sign_full_packet_com_key(data: str) -> str: """Crc calculation.""" try: crc = hexlify(pack('>I', crc_hqx(unhexlify(data), 0x1021))).decode(ENCODING_CODEC) data = data + crc[6:8] + crc[4:6] crc = crc[6:8] + crc[4:6] + ( hexlify(REMOTE_KEY)).decode(ENCODING_CODEC) crc = hexlify(pack('>I', crc_hqx(unhexlify(crc), 0x1021))).decode(ENCODING_CODEC) data = data + crc[6:8] + crc[4:6] return data except HANDLED_EXCEPTIONS as ex: raise EncodingError("failed to sign crc") from ex
def _layout_id(self, layout) -> str: return str( -min( max(binascii.crc_hqx(layout.internal_name.encode("utf-8"), 0) // 2, 1), 32768, ) )
def send(self, b, end=False): """Send up to 7 bytes of data. :param bytes b: 0 - 7 bytes of data to transmit. :param bool end: If this is the last data. """ assert len(b) <= 7, "Max 7 bytes can be sent" if not end: assert len(b) == 7, "Less than 7 bytes only allowed if last data" self._seqno += 1 command = self._seqno if end: command |= NO_MORE_BLOCKS self._done = True # Change expected ACK:ed sequence self._blksize = self._seqno # Save how many bytes this message contains since this is the last self._last_bytes_sent = len(b) request = bytearray(8) request[0] = command request[1:len(b) + 1] = b self.sdo_client.send_request(request) self.pos += len(b) if self.crc_supported: # Calculate CRC self._crc = binascii.crc_hqx(b, self._crc) if self._seqno >= self._blksize: # End of this block, wait for ACK self._block_ack()
def generate_crc(msg: bytes, crc_init: int) -> bytes: if len(msg) % 2 != 0: msg += b'\x00' crc = binascii.crc_hqx(msg, crc_init) crc_bin = struct.pack('<H', crc) return crc_bin
def calc_hashslot(key): s = key.find(b"{") if s != -1: e = key.find(b"}") if e > s + 1: key = key[s + 1:e] return crc_hqx(key, 0) % 16384
def __get_crc(self, cmd_no, data=None): crc_data = cmd_no if data is not None: crc_data += binascii.unhexlify(data) ret_int = binascii.crc_hqx(crc_data, 0) ret = ret_int.to_bytes(2, 'big') return ret
def encode(self): if self.blocknum == 0: self.data = b"" if "filename" in self.metadata: bb = self.metadata["filename"].encode() self.data += b"FNM" + bytes([len(bb)]) + bb if "sbxname" in self.metadata: bb = self.metadata["sbxname"].encode() self.data += b"SNM" + bytes([len(bb)]) + bb if "filesize" in self.metadata: bb = self.metadata["filesize"].to_bytes(8, byteorder='big') self.data += b"FSZ" + bytes([len(bb)]) + bb if "filedatetime" in self.metadata: bb = self.metadata["filedatetime"].to_bytes(8, byteorder='big') self.data += b"FDT" + bytes([len(bb)]) + bb if "sbxdatetime" in self.metadata: bb = self.metadata["sbxdatetime"].to_bytes(8, byteorder='big') self.data += b"SDT" + bytes([len(bb)]) + bb if "hash" in self.metadata: bb = self.metadata["hash"] self.data += b"HSH" + bytes([len(bb)]) + bb data = self.data + b'\x1A' * (self.datasize - len(self.data)) buffer = (self.uid + self.blocknum.to_bytes(4, byteorder='big') + data) crc = binascii.crc_hqx(buffer, self.ver).to_bytes(2, byteorder='big') block = self.magic + crc + buffer if self.encdec: block = self.encdec.xor(block) return block
def modbus_calc_properties(config): byte_offset = 0 reg_count = 0 if CONF_OFFSET in config: byte_offset = config[CONF_OFFSET] # A CONF_BYTE_OFFSET setting overrides CONF_OFFSET if CONF_BYTE_OFFSET in config: byte_offset = config[CONF_BYTE_OFFSET] if CONF_REGISTER_COUNT in config: reg_count = config[CONF_REGISTER_COUNT] if CONF_VALUE_TYPE in config: value_type = config[CONF_VALUE_TYPE] if reg_count == 0: reg_count = TYPE_REGISTER_MAP[value_type] if CONF_CUSTOM_COMMAND in config: if CONF_ADDRESS not in config: # generate a unique modbus address using the hash of the name # CONF_NAME set even if only CONF_ID is used. # a modbus register address is required to add the item to sensormap value = config[CONF_NAME] if isinstance(value, str): value = value.encode() config[CONF_ADDRESS] = binascii.crc_hqx(value, 0) config[CONF_REGISTER_TYPE] = ModbusRegisterType.CUSTOM config[CONF_FORCE_NEW_RANGE] = True return byte_offset, reg_count
def _create_crc(self, msg: bytes) -> bytes: if len(msg) % 2 != 0: msg += b'\x00' crc = binascii.crc_hqx(msg, self._crc_init) crc_bin = struct.pack('<H', crc) return crc_bin
def get_checksums(_data: object) -> dict: """Generate a dictionary containing various checksum values for the given data >>> get_checksums(br'This is a test.') {'CRC32': 94035228, 'CRC16': 50568} """ _bytedata: bytes = bytes(str(_data), encoding=r'utf-8') return {r'CRC32': crc32(_bytedata), r'CRC16': crc_hqx(_bytedata, 0)}
def test_empty_string(self): if test_support.due_to_ironpython_bug( "http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=317834" ): return # A test for SF bug #1022953. Make sure SystemError is not raised. empty = self.type2test("") for func in all_functions: if func == "crc_hqx": # crc_hqx needs 2 arguments binascii.crc_hqx(empty, 0) continue f = getattr(binascii, func) try: f(empty) except Exception, err: self.fail("{}({!r}) raises {!r}".format(func, empty, err))
def test_not_implemented(self): test_cases = [ lambda: binascii.a2b_qp(None), lambda: binascii.a2b_qp(None, None), lambda: binascii.a2b_hqx(None), lambda: binascii.rledecode_hqx(None), lambda: binascii.rlecode_hqx(None), lambda: binascii.b2a_hqx(None), lambda: binascii.crc_hqx(None, None), ] for temp_func in test_cases: self.assertRaises(NotImplementedError, temp_func)
def test_empty_string(self): # A test for SF bug #1022953. Make sure SystemError is not raised. for n in [ "b2a_qp", "a2b_hex", "b2a_base64", "a2b_uu", "a2b_qp", "b2a_hex", "unhexlify", "hexlify", "crc32", "b2a_hqx", "a2b_hqx", "a2b_base64", "rlecode_hqx", "b2a_uu", "rledecode_hqx", ]: f = getattr(binascii, n) f("") binascii.crc_hqx("", 0)
def crccalc(command): """returns crc as integer from binary string command""" crc=binascii.crc_hqx(command,0) crchi=crc>>8 crclo=crc&255 if crchi == 0x28 or crchi==0x0d or crchi==0x0a: crc+=256 if crclo == 0x28 or crclo==0x0d or crclo==0x0a: crc+=1 return crc
def GetCacheFileName(key, machoVersion=99999): """Returns filename for specified object name.""" if machoVersion >= 213: return "%x.cache" % binascii.crc_hqx(cPickle.dumps(key), 0) # Legacy stuff. Deprecated. Here for historical reasons :) #elif machoVersion >= 151: # return "%x.cache" % binascii.crc_hqx((blue.marshal.Save(key, machoVersion=machoVersion)), 0) #elif machoVersion >= 136: # return blue.marshal.Save(key).encode("base64").replace('=', '').replace('/', '-').replace('\n', '') + ".cache" #else: # return blue.marshal.Save(key).encode("hex") + ".cache" else: raise RuntimeError("machoNet version 213 or higher required")
def test_returned_value(self): # Limit to the minimum of all limits (b2a_uu) MAX_ALL = 45 raw = self.rawdata[:MAX_ALL] for fa, fb in zip(a2b_functions, b2a_functions): a2b = getattr(binascii, fa) b2a = getattr(binascii, fb) try: a = b2a(self.type2test(raw)) res = a2b(self.type2test(a)) except Exception as err: self.fail("{}/{} conversion raises {!r}".format(fb, fa, err)) if fb == "b2a_hqx": # b2a_hqx returns a tuple res, _ = res self.assertEqual(res, raw, "{}/{} conversion: " "{!r} != {!r}".format(fb, fa, res, raw)) self.assertIsInstance(res, bytes) self.assertIsInstance(a, bytes) self.assertLess(max(a), 128) self.assertIsInstance(binascii.crc_hqx(raw, 0), int) self.assertIsInstance(binascii.crc32(raw), int)
def parse_setup(report_file): setup_data = None crc = 0xffff version = None for line in report_file.readlines(): m = re.match("Generated with uBlue setup DLL version: 1.0.0.(\d+)$", line.strip()) if m: version = int(m.group(1)) if line.strip() == "[Setup Data]": setup_data = {} if not line.strip() or setup_data is None: continue if not re.match("^[0-9A-F-]+$", line.strip()): continue data = line.strip().split("-") assert int(data[0], 16) == len(data[1:]) # Opcode: Setup (0x06) assert data[1] == "06" target = int(data[2], 16) if not setup_data.get(target): setup_data[target] = "" # Offset into target assert int(data[3], 16) == len(setup_data[target].decode("hex")) setup_data[target] += "".join(data[4:]) if target == 0xf0: # Remove existing CRC when calculating new CRC data = data[:-2] crc = crc_hqx("".join(data).decode("hex"), crc) # Check CRC expected_crc = struct.unpack(">H", "".join(setup_data[0xf0].decode("hex")[-2:]))[0] assert crc == expected_crc return (version, setup_data)
def GetCacheFileName(key): """Returns filename for specified object name.""" # BEGIN UGLY HACK ---------------------------------------------------- # CCP is relying on pickle to produce consistent output, which it does # not because pickle's output depends on the refcount of objects; an # object referenced only once will not get memoized. Cache keys are # seemingly always created by EVE with refcounts >1. When Reverence # decodes them, they only have a refcount of 1. This code ensures that # those refcounts are increased so cPickle produces the correct output # when you feed a decoded object's key to this function. # (and no, copy.deepcopy doesn't work as expected on tuples) memoize = [].append def increase_refcounts(k): if type(k) is tuple: for element in k: increase_refcounts(element) memoize(k) increase_refcounts(key) # END UGLY HACK ------------------------------------------------------ return "%x.cache" % binascii.crc_hqx(cPickle.dumps(key), 0)
def __init__(self, shared, objectID, cachedObject, objectVersion=None): self.__shared__ = shared self.__objectID__ = objectID self.__nodeID__ = sm.GetService("machoNet").GetNodeID() self.__cachedObject__ = cachedObject self.__compressed__ = 0 self.__thePickle__ = None self.__objectVersion__ = (blue.os.GetWallclockTimeNow(), objectVersion) if (self.__shared__ or objectVersion is None) and macho.mode != "client": self.__thePickle__ = blue.marshal.Save(cachedObject) if len(self.__thePickle__) > 170: try: t = ClockThis("machoNet::util.CachedObject::compress", zlib.compress, self.__thePickle__, 1) except zlib.error as e: raise RuntimeError("Compression Failure: " + strx(e)) if len(t) < len(self.__thePickle__): self.__thePickle__ = t self.__compressed__ = 1 if objectVersion is None: self.__objectVersion__ = ( self.__objectVersion__[0], binascii.crc_hqx(self.__thePickle__, macho.version + 170472), )
def format_field(self, value, format_spec): """Override original method to include modifier extensions""" if value is None: # No value is given return "" # Process format spec match = re.search(r"([#@]?)(\d*)(.*)", format_spec) xmod, num, fmt = match.groups() if isinstance(value, int) and type(value) != int: # This is an object derived from int, convert it to string value = str(value) if isinstance(value, str): if fmt == "x": # Display string in hex xprefix = "" if xmod == "#": xprefix = "0x" return xprefix + value.encode("hex") elif fmt == "crc32": # CRC32 format return "{0:#010x}".format(crc32(value) & 0xffffffff) elif fmt == "crc16": # CRC16 format return "{0:#06x}".format(crc_hqx(value, 0xa5a5) & 0xffff) elif xmod == "@": # Format {0:@starindex[,endindex]} is like value[starindex:endindex] # {0:@3} is like value[3:] # {0:@3,5} is like value[3:5] # {0:.5} is like value[:5] end = 0 if len(fmt) > 2 and fmt[0] == ",": end = int(fmt[1:]) return value[int(num):end] else: return value[int(num):] elif isinstance(value, list): # Format: {0[[:listfmt]:itemfmt]} if len(format_spec): fmts = format_spec.split(":", 1) ifmt = "{0:" + fmts[-1] + "}" vlist = [self.format(ifmt, x) for x in value] if len(fmts) == 2: # Two format specs, use the first one for the list itself # and the second spec is for each item in the list return fmts[0].join(vlist) # Only one format spec is given, display list with format spec # applied to each item in the list return "[" + ", ".join(vlist) + "]" elif isinstance(value, int) or isinstance(value, long) or isinstance(value, float): if _max_map.get(fmt): # Format: {0:max32|umax32|max64|umax64} # Output: if value matches the largest number in format given, # the max name is displayed, else the value is displayed # {0:max32}: value:0x7fffffff then "max32" is displayed # {0:max32}: value:35 then 35 is displayed return _max_map[fmt].get(value, str(value)) elif fmt[:4] == "date": # Format: {0:date[:datefmt]} # Output: display value as a date # value: 1416846041.521868 # display: 'Mon Nov 24 09:20:41 2014' dfmt = "%c" # Default date spec when datefmt is not given fmts = fmt.split(":", 1) if len(fmts) == 2: dfmt = fmts[1] if dfmt.find("%q"): # Replace all instances of %q with the microseconds usec = "%06d" % (1000000 * (value - int(value))) dfmt = dfmt.replace("%q", usec) return time.strftime(dfmt, time.localtime(value)) return format(value, format_spec)
def GetCacheFileName(key, machoVersion=99999): """Returns filename for specified object name.""" if machoVersion >= 213: return "%x.cache" % binascii.crc_hqx(cPickle.dumps(key), 0) else: raise RuntimeError("machoNet version 213 or higher required")
def _read(self, len): data = self.ifp.read(len) self.crc = binascii.crc_hqx(data, self.crc) return data
def _write(self, data): self.crc = binascii.crc_hqx(data, self.crc) self.ofp.write(data)
def Placebo_CryptoHash(*args): timer = Enter('machoNet::PlaceboCrypto::CryptoHash') try: return str(binascii.crc_hqx(blue.marshal.Save(args), 0)) finally: Leave(timer)
def KeyToFileName(self, key): pickle = cPickle.dumps(key) checksum = binascii.crc_hqx(pickle, 0) return '%x' % checksum
def crc(stream): return crc_hqx(stream, 0)
def crc16(value): """Convert string to its crc16 representation""" return binascii.crc_hqx(value, 0xa5a5) & 0xffff
def crypto_hash(*args): return str(binascii.crc_hqx(blue.marshal.Save(args), 0))
"""Macintosh binhex compression/decompression.