def __bytes__(self): b = b"" b += bitstruct.pack("u48", self.dst.addr) b += bitstruct.pack("u48", self.src.addr) b += bitstruct.pack("u16", self.streamtype) b += self.nonce return b
def __bytes__(self): b = b"" b += b"M17 " b += bitstruct.pack("u16", self.streamid) b += bytes(self.LICH) b += bitstruct.pack("u16", self.frame_number) b += bytes(self.payload) b += bytes([0] * 2) #crc16, TODO assert self.sz == len(b) return b
def serialize_bitfield(): nonlocal bit_fmt, bit_values if bit_fmt == '': return b'' if not self._mergeBitfield: bits = bitstruct.pack(bit_fmt + '<', *bit_values) else: bits = bitstruct.pack(bit_fmt, *bit_values)[::-1] bit_fmt = '' bit_values = [] return bits
def ori(rt, rs, immediate): instruction = fhex( bitstruct.pack('u16u6u5u5', immediate, 0b010100, rt, rs)) comment = 'ori {}, {}, 0x{:x}'.format(GPR_STRINGS[rt], GPR_STRINGS[rs], immediate) return (instruction, comment)
def create_little(): items = [('>', '')] end = format_length for data in datas[::-1]: if data.byte_order == 'big_endian': continue padding_length = end - (data.start + data.length) if padding_length > 0: items.append(padding_item(padding_length)) items.append(data_item(data)) end = data.start if end > 0: items.append(padding_item(end)) value = padding_mask(items) if format_length > 0: value = bitstruct.pack('u{}'.format(format_length), value) value = int(binascii.hexlify(value[::-1]), 16) return fmt(items), value
def pgn_pack(reserved, data_page, pdu_format, pdu_specific=0): """Pack given values as a parameter group number (PGN) and return it as an integer. """ if pdu_format < 240 and pdu_specific != 0: raise Error( 'Expected PDU specific 0 when PDU format is 0..239, but got {}.'. format(pdu_specific)) try: packed = bitstruct.pack('u1u1u8u8', reserved, data_page, pdu_format, pdu_specific) except bitstruct.Error: if reserved > 1: raise Error('Expected reserved 0..1, but got {}.'.format(reserved)) elif data_page > 1: raise Error( 'Expected data page 0..1, but got {}.'.format(data_page)) elif pdu_format > 255: raise Error( 'Expected PDU format 0..255, but got {}.'.format(pdu_format)) elif pdu_specific > 255: raise Error('Expected PDU specific 0..255, but got {}.'.format( pdu_specific)) else: raise Error('Internal error.') return bitstruct.unpack('u18', packed)[0]
def _encode_data(data, signals, formats, scaling, length): big_unpacked_data = [ _encode_signal(signal, data, scaling) for signal in signals if signal.byte_order == 'big_endian' ] little_unpacked_data = [ _encode_signal(signal, data, scaling) for signal in signals[::-1] if signal.byte_order == 'little_endian' ] big_packed = bitstruct.pack(formats.big_endian, *big_unpacked_data) little_packed = bitstruct.pack(formats.little_endian, *little_unpacked_data)[::-1] packed_union = struct.unpack('>Q', big_packed)[0] packed_union |= struct.unpack('>Q', little_packed)[0] return struct.pack('>Q', packed_union)[:length]
def pgn(self): if self.pdu_format < 240 and self.pdu_specific != 0: raise Error('Expected PDU specific 0 when PDU format is ' '0..239, but got {}.'.format(self.pdu_specific)) try: packed = bitstruct.pack('u1u1u8u8', self.reserved, self.data_page, self.pdu_format, self.pdu_specific) except bitstruct.Error: if self.reserved > 1: raise Error('Expected reserved 0..1, but got {}.'.format( self.reserved)) elif self.data_page > 1: raise Error('Expected data page 0..1, but got {}.'.format( self.data_page)) elif self.pdu_format > 255: raise Error('Expected PDU format 0..255, but got {}.'.format( self.pdu_format)) elif self.pdu_specific > 255: raise Error('Expected PDU specific 0..255, but got {}.'.format( self.pdu_specific)) else: raise Error('Internal error.') return bitstruct.unpack('u18', packed)[0]
def bytes(self, verbose=False): parts = [self.opcode] + list(self.args) bytes_out = [] byte_len = 0 byte_assembly = [] for p in parts: byte_len += p.width byte_assembly.append(p) if byte_len % 8 == 0: byte_assembly = byte_assembly[::-1] fmt = ''.join('>{:s}{:d}'.format(p.signedness, p.width) for p in byte_assembly) + '<' bytes_out.append(bitstruct.pack(fmt, *byte_assembly)) byte_assembly = [] byte_len = 0 if len(byte_assembly): raise RuntimeError("got an incomplete byte") res = b''.join(bytes_out) if verbose: print("assembling {:s} ({:02x}) {} -> {}".format( str(self.__class__.__name__), self.opcode, self.args, res.hex())) return res
def frame_id(self): try: packed = bitstruct.pack('u3u1u1u8u8u8', self.priority, self.reserved, self.data_page, self.pdu_format, self.pdu_specific, self.source_address) except bitstruct.Error: if self.priority > 7: raise Error('Expected priority 0..7, but got {}.'.format( self.priority)) elif self.reserved > 1: raise Error('Expected reserved 0..1, but got {}.'.format( self.reserved)) elif self.data_page > 1: raise Error('Expected data page 0..1, but got {}.'.format( self.data_page)) elif self.pdu_format > 255: raise Error('Expected PDU format 0..255, but got {}.'.format( self.pdu_format)) elif self.pdu_specific > 255: raise Error('Expected PDU specific 0..255, but got {}.'.format( self.pdu_specific)) elif self.source_address > 255: raise Error( 'Expected source address 0..255, but got {}.'.format( self.source_address)) else: raise Error('Internal error.') return bitstruct.unpack('u29', packed)[0]
def pgn_pack(reserved, data_page, pdu_format, pdu_specific=0): """Pack given values as a parameter group number (PGN) and return it as an integer. """ if pdu_format < 240 and pdu_specific != 0: raise Error( 'Expected PDU specific 0 when PDU format is 0..239, but got {}.'.format( pdu_specific)) try: packed = bitstruct.pack('u1u1u8u8', reserved, data_page, pdu_format, pdu_specific) except bitstruct.Error: if reserved > 1: raise Error('Expected reserved 0..1, but got {}.'.format(reserved)) elif data_page > 1: raise Error('Expected data page 0..1, but got {}.'.format( data_page)) elif pdu_format > 255: raise Error('Expected PDU format 0..255, but got {}.'.format( pdu_format)) elif pdu_specific > 255: raise Error('Expected PDU specific 0..255, but got {}.'.format( pdu_specific)) else: raise Error('Internal error.') return bitstruct.unpack('u18', packed)[0]
def create_little(): items = [('>', '')] end = message_length for signal in signals[::-1]: if signal.byte_order == 'big_endian': continue padding_length = end - (signal.start + signal.length) if padding_length > 0: items.append(padding_item(padding_length)) items.append(signal_item(signal)) end = signal.start if end > 0: items.append(padding_item(end)) value = padding_mask(items) if message_length > 0: value = bitstruct.pack('u{}'.format(message_length), value) value = int(binascii.hexlify(value[::-1]), 16) return fmt(items), value
def pre_process_default_value_bit_string(self, member, resolved_member): default = member['default'] if isinstance(default, tuple): # Already pre-processed. return if isinstance(default, list): named_bits = dict(resolved_member['named-bits']) reversed_mask = 0 for name in default: reversed_mask |= (1 << int(named_bits[name])) mask = int(bin(reversed_mask)[2:][::-1], 2) number_of_bits = reversed_mask.bit_length() elif default.startswith('0x'): if len(default) % 2 == 1: default += '0' default = '01' + default[2:] mask = int(default, 16) mask >>= lowest_set_bit(mask) number_of_bits = mask.bit_length() - 1 mask ^= (1 << number_of_bits) else: mask = int(default, 2) mask >>= lowest_set_bit(mask) number_of_bits = len(default) - 2 mask = bitstruct.pack('u{}'.format(number_of_bits), mask) member['default'] = (mask, number_of_bits)
def frame_id_pack(priority, reserved, data_page, pdu_format, pdu_specific, source_address): """Pack given values as a frame id and return it as an integer. """ try: packed = bitstruct.pack('u3u1u1u8u8u8', priority, reserved, data_page, pdu_format, pdu_specific, source_address) except bitstruct.Error: if priority > 7: raise Error('Expected priority 0..7, but got {}.'.format(priority)) elif reserved > 1: raise Error('Expected reserved 0..1, but got {}.'.format(reserved)) elif data_page > 1: raise Error( 'Expected data page 0..1, but got {}.'.format(data_page)) elif pdu_format > 255: raise Error( 'Expected PDU format 0..255, but got {}.'.format(pdu_format)) elif pdu_specific > 255: raise Error('Expected PDU specific 0..255, but got {}.'.format( pdu_specific)) elif source_address > 255: raise Error('Expected source address 0..255, but got {}.'.format( source_address)) else: raise Error('Internal error.') return bitstruct.unpack('u29', packed)[0]
def mac_string(device_id): """ Converts a device id into a mac address hex string :param device_id: The device id :returns: str -- The mac address represented as a string """ return hexlify(byteswap('6', pack('u48', device_id)))
def create_header() -> bytearray: val = ("program: fastqube \n" "version: PROTOTYPE \n" "mode: LOSSLESS\n" "date: {0} \n").format(datetime.datetime.utcnow().isoformat()) padding = (4096 * 8) - (len(val) * 8) fmt = "t{0}p{1}".format(len(val) * 8, padding) return bitstruct.pack(fmt, val)
def ser_6bit(val: str) -> bytearray: result = b'' for chunk in _grouper(4, val.upper(), padvalue=' '): chunk = list(map(lambda x: ord(x) - 0x20, chunk)) chunk.reverse() tmp = bitstruct.pack('u6' * 4, *chunk) result += tmp[::-1] return result
def to_bytes(self) -> bytes: result = bytearray() result += self.header result += bytes([len(self.header) + 1 + len(self.data) + 2]) result += self.data crc = crc_func(result) result += bitstruct.pack(">u16", crc) return result
def test_FLAC_reserved_block_type(): orig = (Path(__file__).parent / 'files' / 'audio' / 'test-flac-vorbis.flac').read_bytes() flac_data = orig[0:4] + bitstruct.pack('b1 u7 u24', False, 10, 0) + orig[4:] flac = FLAC.load(flac_data) assert flac._blocks[0] == FLACMetadataBlock(10, b'')
def from_frame_id(cls, frame_id): try: packed = bitstruct.pack('u29', frame_id) except bitstruct.Error: raise Error('Expected a frame id 0..0x1fffffff, ' 'but got {}.'.format(hex(frame_id))) return cls(*bitstruct.unpack('u3u1u1u8u8u8', packed))
def pack_section(section, *args): """ Packs bytes into a header including the swap to little-endian :param section: The definition of the format and byteswap for this section :param \*args: Values to include in the section in the order they are in the format :returns: bytearray -- The packed bytes of the section """ return byteswap(section['byteswap'], pack(section['format'], *args))
def swm32(reglist, base, offset): instruction = fhex( bitstruct.pack('u4u12u6u5u5', 0b1101, offset, 0b001000, reglist, base)) reglist = ', '.join([GPR_STRINGS[i] for i in REGLIST_GPRS[reglist]]) comment = 'swm32 {}, 0x{:x}({})'.format(reglist, offset, GPR_STRINGS[base]) return (instruction, comment)
def serialize(self): payload = self._payload_prologue() + super().serialize() payload_cksum = (-sum(payload)) & 0xff header = bitstruct.pack(self._multirecord_header_fmt, self._type_id, self.end_of_list, 0, self._format_version, len(payload), payload_cksum) header_cksum = (-sum(header)) & 0xff header += header_cksum.to_bytes(length=1, byteorder='little') return header + payload
def make_frame_address(target, ack_required, res_required, sequence): target = target.split(':') target.reverse() target = ''.join(target) target = int(target, 16) unswapped_header = pack(FRAME_ADDRESS_FORMAT, target, 0, 0, ack_required, res_required, sequence) frame_address = byteswap(FRAME_ADDRESS_BYTESWAP, unswapped_header) return frame_address
def pack(data): assert(type(data) is Calibration) return bitstruct.pack('u8u8u8u8f32f32f32', data.active_segment, data.max_segment, data.polynomial_order, data.max_polynomial, data.inflection_point, data.offset, data.gain)
def from_pgn(cls, pgn): try: packed = bitstruct.pack('u18', pgn) except bitstruct.Error: raise Error('Expected a parameter group number 0..0x3ffff, ' 'but got {}.'.format(hex(pgn))) reserved, data_page, pdu_format, pdu_specific = bitstruct.unpack( 'u1u1u8u8', packed) return cls(0, reserved, data_page, pdu_format, pdu_specific, 0)
def __bytes__(self): return struct.pack(">B", self.led) + bs.byteswap( "3", bs.pack( "u3" * 8, self.color_mode, self.led_mode, *(hex_color_to_vkb_color(self.color2)[::-1]), *(hex_color_to_vkb_color(self.color1)[::-1]), ), )
def gen(self, *argv, **kwargs): pack_data = list() argv_it = iter(argv) for k,v in self.packet_info.items(): if (len(argv) == 0): pack_data.append(kwargs[k]) else: pack_data.append(next(argv_it)) self.message = list(bitstruct.pack(self.pack_fmt, *pack_data)) return self.message
def __bytes__(self): b = b"" if self.LICH: lich_chunk_idx = self.frame_number % 5 assert len(self.LICH_chunks[lich_chunk_idx]) == 48 / 8 b += self.LICH_chunks[lich_chunk_idx] else: b += self.lich_chunk b += bitstruct.pack("u16", self.frame_number) b += bytes(self.payload) b += bytes([0] * 2) #crc16 # b += bitstruct.pack("u4",0) return b
def frame_id_unpack(frame_id): """Unpack given frame id and return a tuple of priority, reserved, data page, PDU format, PDU specific and source address. """ try: packed = bitstruct.pack('u29', frame_id) except bitstruct.Error: raise Error('Expected a frame id 0..0x1fffffff, but got {}.'.format( hex(frame_id))) return FrameId(*bitstruct.unpack('u3u1u1u8u8u8', packed))
def frame_id_unpack(frame_id): """Unpack given frame id and return a tuple of priority, reserved, data page, PDU format, PDU specific and source address. """ try: packed = bitstruct.pack('u29', frame_id) except bitstruct.Error: raise Error( 'Expected a frame id 0..0x1fffffff, but got {}.'.format( hex(frame_id))) return FrameId(*bitstruct.unpack('u3u1u1u8u8u8', packed))
def pgn_unpack(pgn): """Unpack given parameter group number (PGN) and return a tuple of Reserved, Data Page, PDU Format and PDU Specific. """ try: packed = bitstruct.pack('u18', pgn) except bitstruct.Error: raise Error( 'Expected a parameter group number 0..0x3ffff, but got {}.'.format( hex(pgn))) return PGN(*bitstruct.unpack('u1u1u8u8', packed))
def ioctlCommandGenerate(): #ioctlCmd = bitstruct.byteswap("8", bitstruct.pack(">u2u14u8u8", 3, ctypes.sizeof(CSRBprotocolCMDmoduleExecute), 1, 1)) ioctlCmd = bitstruct.pack(">u3u13u8u8", 6, ctypes.sizeof(CSRBprotocolCMDmoduleExecute), 1, 1) #print("ioctlCmd initial assembly: " + ioctlCmd.hex()) ioctlCmd = bitstruct.byteswap("8", ioctlCmd) #print("ioctlCmd byte swapped: " + ioctlCmd.hex()) ioctlCmdInt = int.from_bytes(ioctlCmd, byteorder='little') #print("ioctlCmdInt LE int: " + str(hex(ioctlCmdInt))) return ioctlCmdInt
def build_steamid(steamid_unparsed): if str(steamid_unparsed).isdigit(): universe, steam_type, instance, account_number = bitstruct.unpack( "u8u4u20u32", bitstruct.pack("u64", long(steamid_unparsed)) ) # Bit confusing, but convert from ID64 to STEAM_0:A:B format # See https://developer.valvesoftware.com/wiki/SteamID instance = 0 if account_number % 2 == 1: instance = 1 account_number -= 1 account_number = long(account_number / 2) return SteamID(account_number, instance, steam_type, universe) return SteamID.from_text(steamid_unparsed)
def test_motohawk(self): f = cantools.db.File() filename = os.path.join('tests', 'files', 'motohawk.dbc') with open(filename, 'r') as fin: f.add_dbc(fin) self.assertEqual(len(f.messages), 1) with open(filename, 'r') as fin: self.assertEqual(f.as_dbc(), fin.read()) # decoode a raw message frame_id = 496 temperature = 250.55 average_radius = 3.2 enable = 1 data = bitstruct.pack('p45u12u6u1', int((temperature - 250) / 0.01), int(average_radius / 0.1), enable) decoded = f.decode_message(frame_id, data) self.assertEqual(decoded.Temperature, 250.55) self.assertEqual(decoded.AverageRadius, 3.2) self.assertEqual(decoded.Enable, 1.0)
def frame_id_pack(priority, reserved, data_page, pdu_format, pdu_specific, source_address): """Pack given values as a frame id and return it as an integer. """ try: packed = bitstruct.pack('u3u1u1u8u8u8', priority, reserved, data_page, pdu_format, pdu_specific, source_address) except bitstruct.Error: if priority > 7: raise Error('Expected priority 0..7, but got {}.'.format(priority)) elif reserved > 1: raise Error('Expected reserved 0..1, but got {}.'.format(reserved)) elif data_page > 1: raise Error('Expected data page 0..1, but got {}.'.format(data_page)) elif pdu_format > 255: raise Error('Expected PDU format 0..255, but got {}.'.format( pdu_format)) elif pdu_specific > 255: raise Error('Expected PDU specific 0..255, but got {}.'.format( pdu_specific)) elif source_address > 255: raise Error('Expected source address 0..255, but got {}.'.format( source_address)) else: raise Error('Internal error.') return bitstruct.unpack('u29', packed)[0]
def scan(self, barcode): self.write(pack('u8', 0)) self.write(bytes(barcode, "ascii")) self.write(b'\x0d')