def _parse_packet(self, packet_bytes): ''' Parse a raw udp packet and return a list of parsed messages. ''' byte_buffer = ByteBuffer(packet_bytes) parsed_messages = [] while not byte_buffer.is_empty(): message_id, sequence_number, message_flags = \ byte_buffer.read_struct(self.MESSAGE_TRANSPORT_HEADER) message_type_id = messages.BaseMessage.read_header_from_byte_buffer( byte_buffer)[0] message = self.message_factory.get_by_id(message_type_id)() message.read_from_byte_buffer(byte_buffer) # - These flags are for consumption by .update() message_flags_bf = bitfield(message_flags) message.is_reliable = message_flags_bf[1] message.is_ordered = message_flags_bf[0] message.sequence_number = sequence_number message.message_id = message_id parsed_messages.append(message) return parsed_messages
def to_binary(self): self.encoding = BIN_ENCODING header = ByteBuffer() header.append_u8(SIGNATURE) header.append_u8(SIG_COMPRESSED) header.append_u8(encoding_vals[self.encoding]) # Python's ints are big, so can't just bitwise invert header.append_u8(0xFF ^ encoding_vals[self.encoding]) self.nodeBuf = ByteBuffer() self.dataBuf = ByteBuffer() self.dataByteBuf = ByteBuffer(self.dataBuf.data) self.dataWordBuf = ByteBuffer(self.dataBuf.data) for child in self.xml_doc.childNodes: self._node_to_binary(child) # always has the isArray bit set self.nodeBuf.append_u8(xml_types['endSection'] | 64) self.nodeBuf.realign_writes() header.append_u32(len(self.nodeBuf)) self.nodeBuf.append_u32(len(self.dataBuf)) return bytes(header.data + self.nodeBuf.data + self.dataBuf.data)
def __init__(self, path): with open(path, 'rb') as f: self.contents = ByteBuffer(f.read(), endian='<') self.name = self.contents.get('16s') self.header_size = self.contents.get_u32() self.filecount = self.contents.get_u32() self.contents.offset += 48 # random padding/flags/title bytes offsets = [] for _ in range(self.filecount): offsets.append(self.contents.get_u32()) self.tracks = [] for off in offsets: self.contents.offset = off self.tracks.append(self.Track(self.contents))
def tradingviewx(transport): ##{ buffer = ByteBuffer() xhr = next(transport) while True: try: data = xhr.raw.read(XHR_READ_SIZE) except Exception: xhr = next(transport) continue buffer.write(data) line = buffer.read_until(b"\r\n", consume=True) if not line: continue if line == ": -1": continue if line.startswith("data: "): line = line[6:] try: pkg = jsload(line) except Exception: continue channel = pkg.get("text", {}).get("channel") if channel != "chat_bitcoin": continue content = pkg.get("text").get("content") login = content.get('username') msg = content.get("text", "").encode("utf-8", errors="replace") meta = content.get("meta", {}) url = meta.get("url", "").encode("utf-8", errors="replace") if url: msg = "{}\n{:<19}{}{}{}".format(msg, "", COLOR_10, url, COLOR_0) if not msg: continue yield (login, msg)
def is_binary_xml(input): nodeBuf = ByteBuffer(input) return (nodeBuf.get_u8() == SIGNATURE and nodeBuf.get_u8() in (SIG_COMPRESSED, SIG_UNCOMPRESSED))
def from_binary(self, input): self.xml_doc = minidom.Document() node = self.xml_doc self.nodeBuf = ByteBuffer(input) assert self.nodeBuf.get_u8() == SIGNATURE compress = self.nodeBuf.get_u8() assert compress in (SIG_COMPRESSED, SIG_UNCOMPRESSED) self.compressed = compress == SIG_COMPRESSED encoding_key = self.nodeBuf.get_u8() assert self.nodeBuf.get_u8() == 0xFF ^ encoding_key self.encoding = encoding_strings[encoding_key] nodeEnd = self.nodeBuf.get_u32() + 8 self.nodeBuf.end = nodeEnd self.dataBuf = ByteBuffer(input, nodeEnd) dataSize = self.dataBuf.get_u32() # This is all no fun self.dataByteBuf = ByteBuffer(input, nodeEnd) self.dataWordBuf = ByteBuffer(input, nodeEnd) nodesLeft = True while nodesLeft and self.nodeBuf.hasData(): while self.nodeBuf.peek_u8() == 0: debug_print("Skipping 0 node ID") self.nodeBuf.get_u8() nodeType = self.nodeBuf.get_u8() isArray = nodeType & 64 nodeType &= ~64 nodeFormat = xml_formats.get(nodeType, {'name':'Unknown'}) debug_print('Node type is {} ({})'.format(nodeFormat['name'], nodeType)) # node or attribute name name = '' if nodeType != xml_types['nodeEnd'] and nodeType != xml_types['endSection']: if self.compressed: name = unpack_sixbit(self.nodeBuf) else: length = self.nodeBuf.get_u8() name = self.nodeBuf.get('s', length) debug_print(name) skip = True if nodeType == xml_types['attr']: value = self.data_grab_string() node.setAttribute(name, value) elif nodeType == xml_types['nodeEnd']: if node.parentNode: node = node.parentNode elif nodeType == xml_types['endSection']: nodesLeft = False elif nodeType not in xml_formats: raise NotImplementedError('Implement node {}'.format(nodeType)) else: # inner value to process skip = False if skip: continue child = self.xml_doc.createElement(name) node.appendChild(child) node = child if nodeType == xml_types['nodeStart']: continue node.setAttribute('__type', nodeFormat['name']) varCount = nodeFormat['count'] arrayCount = 1 if varCount == -1: # the 2 cannot be combined varCount = self.dataBuf.get_u32() isArray = True elif isArray: arrayCount = self.dataBuf.get_u32() // (calcsize(nodeFormat['type'] * varCount)) node.setAttribute('__count', str(arrayCount)) totalCount = arrayCount * varCount if isArray: data = self.dataBuf.get(nodeFormat['type'], totalCount) self.dataBuf.realign_reads() else: data = self.data_grab_aligned(nodeFormat['type'], totalCount) if nodeType == xml_types['binary']: node.setAttribute('__size', str(totalCount)) string = ''.join(('{0:02x}'.format(x) for x in data)) elif nodeType == xml_types['string']: string = bytes(data[:-1]).decode(self.encoding) else: string = ' '.join(map(nodeFormat.get('toStr', str), data)) node.appendChild(self.xml_doc.createTextNode(string))