def __init__(self, packet, table_map, table_subscribed, ctl_conn): super(TableMapEvent, self).__init__(packet) self._payload = packet[24:] self._ctl_conn = ctl_conn head = self._payload head, self.table_id = utils.read_int(head, 6) # add or modify table map. if self.table_id not in table_map: table_map[self.table_id] = {"schema":None, "table":None, "column_schemas":[]} if self.schema in table_subscribed and self.table in table_subscribed[self.schema]: table_map[self.table_id]["column_schemas"] = \ self.__get_table_informations(self.schema, self.table) head, self.flags = utils.read_int(head, 2) head, schema_name_len = utils.read_int(head, 1) head, self.schema = utils.read_bytes(head, schema_name_len) self.schema = str(self.schema) table_map[self.table_id]["schema"] = self.schema head, _ = utils.read_bytes(head, 1) #filler head, table_name_len = utils.read_int(head, 1) head, self.table = utils.read_bytes(head, table_name_len) self.table = str(self.table) table_map[self.table_id]["table"] = self.table head, _ = utils.read_bytes(head, 1) #filler head, self.columns_cnt = utils.read_lc_int(head) head, column_types = utils.read_bytes(head, self.columns_cnt) for i in range(0, self.columns_cnt): schema = table_map[self.table_id]["column_schemas"][i] t = ord(column_types[i]) schema["TYPE_ID"] = t head, _ = self.__read_metadata(t, schema, head)
def _parse(self): version = '' options = 0 srvstatus = 0 buf = self.data (buf,self.protocol) = utils.read_int(buf,1) (buf,version) = utils.read_string(buf,end='\x00') (buf,thrdid) = utils.read_int(buf,4) (buf,scramble) = utils.read_bytes(buf, 8) buf = buf[1:] # Filler 1 * \x00 (buf,srvcap) = utils.read_int(buf,2) (buf,charset) = utils.read_int(buf,1) (buf,serverstatus) = utils.read_int(buf,2) buf = buf[13:] # Filler 13 * \x00 (buf,scramble_next) = utils.read_bytes(buf,12) scramble += scramble_next self.info = { 'version' : version, 'thrdid' : thrdid, 'seed' : scramble, 'capabilities' : srvcap, 'charset' : charset, 'serverstatus' : serverstatus, }
def decode_text(self): if self.image is None: self.set_output_message("Изображение не было загружено!") return header = len(MainWindow.CryptKey) + MainWindow.Length array = read_bytes(self.image, 0, header) for i in range(0, len(MainWindow.CryptKey)): if bytes((MainWindow.CryptKey[i],)) != array[i]: self.set_output_message("Сообщение не обнаружено.") return size = 0 for i in range(len(MainWindow.CryptKey), len(MainWindow.CryptKey) + MainWindow.Length): size = (size << 8) + int.from_bytes(array[i], "big") array.clear() array = read_bytes(self.image, header, size) text = bytes(array).decode("utf-8") self.ui.text_edit.setPlainText(text) self.set_output_message(f"Обнаружено сообщение длиной {size} байт.")
def decode_text(self): if self.image is None: self.set_output_message("Изображение не загружено в программу!") return header = len(MainWindow.CryptKey) + MainWindow.Length array = read_bytes(self.image, 0, header) for i in range(0, len(MainWindow.CryptKey)): if bytes((MainWindow.CryptKey[i],)) != array[i]: self.set_output_message("Сообщение отсутствует!") return size = 0 for i in range(len(MainWindow.CryptKey), len(MainWindow.CryptKey) + MainWindow.Length): size = (size << 8) + int.from_bytes(array[i], "big") if size > self.data.capacity: self.set_output_message("Ошибка декодирования! Размер заголовка превышает размер сообщения.") return array.clear() array = read_bytes(self.image, header, size) text = bytes(array).decode("utf-8") self.ui.text_edit.setPlainText(text) self.set_output_message(f"Присутствует сообщение длиной {size} байт.")
def _pkt_parse_handshake(self, buf): """Parse a MySQL Handshake-packet""" res = {} (buf,res['protocol']) = utils.read_int(buf,1) (buf,res['server_version_original']) = utils.read_string(buf,end='\x00') (buf,res['server_threadid']) = utils.read_int(buf,4) (buf,res['scramble']) = utils.read_bytes(buf, 8) buf = buf[1:] # Filler 1 * \x00 (buf,res['capabilities']) = utils.read_int(buf,2) (buf,res['charset']) = utils.read_int(buf,1) (buf,res['server_status']) = utils.read_int(buf,2) buf = buf[13:] # Filler 13 * \x00 (buf,scramble_next) = utils.read_bytes(buf,12) res['scramble'] += scramble_next return res
def get_exception(packet): """Returns an exception object based on the MySQL error Returns an exception object based on the MySQL error in the given packet. Returns an Error-Object. """ errno = errmsg = None if packet[4] != '\xff': raise ValueError("Packet is not an error packet") sqlstate = None try: packet = packet[5:] (packet, errno) = utils.read_int(packet, 2) if packet[0] != '\x23': # Error without SQLState errmsg = packet else: (packet, sqlstate) = utils.read_bytes(packet[1:], 5) errmsg = packet except Exception as err: return InterfaceError("Failed getting Error information (%r)" % err) else: return get_mysql_exception(errno, errmsg, sqlstate)
def _path_from_address(cls, address): bs = read_bytes(cls._disk_name, address, cls._header_size) name_length = bs[0] name = bs[1:10][0:name_length].decode('utf-8') name_list = [name] parent_address = int_from_bytes(bs[13:15]) while parent_address != 0: bs = read_bytes(cls._disk_name, parent_address, cls._header_size) name_length = bs[0] name = bs[1:10][0:name_length].decode('utf-8') name_list.append(name) parent_address = int_from_bytes(bs[13:15]) name_list = name_list[0:-1] name_list.reverse() path = '/{}'.format('/'.join(name_list)) return path
def _new_address(cls): for i in range(0, cls._file_limit): address = cls._header_offset + i * cls._header_size header = read_bytes(cls._disk_name, address, cls._header_size) if header[0] == 0: return address return None
def parse_handshake(self, packet): """Parse a MySQL Handshake-packet""" res = {} (packet, res['protocol']) = utils.read_int(packet[4:], 1) (packet, res['server_version_original']) = utils.read_string( packet, end='\x00') (packet, res['server_threadid']) = utils.read_int(packet, 4) (packet, res['scramble']) = utils.read_bytes(packet, 8) packet = packet[1:] # Filler 1 * \x00 (packet, res['capabilities']) = utils.read_int(packet, 2) (packet, res['charset']) = utils.read_int(packet, 1) (packet, res['server_status']) = utils.read_int(packet, 2) packet = packet[13:] # Filler 13 * \x00 (packet, scramble_next) = utils.read_bytes(packet, 12) res['scramble'] += scramble_next return res
def get_exception(packet): """Returns an exception object based on the MySQL error Returns an exception object based on the MySQL error in the given packet. Returns an Error-Object. """ errno = errmsg = None if packet[4] != '\xff': raise ValueError("Packet is not an error packet") sqlstate = None try: packet = packet[5:] (packet, errno) = utils.read_int(packet, 2) if packet[0] != '\x23': # Error without SQLState errmsg = packet else: (packet, sqlstate) = utils.read_bytes(packet[1:], 5) errmsg = packet except Exception, err: return InterfaceError("Failed getting Error information (%r)" % err)
def read(self): if self.type != self.TypeFile: return None if self.content_address == 0: return None bs = read_bytes(self._disk_name, self.content_address, self.size) content = bs.decode('utf-8') return content
def parse_handshake(self, packet): """Parse a MySQL Handshake-packet""" res = {} (packet, res['protocol']) = utils.read_int(packet[4:], 1) (packet, res['server_version_original']) = utils.read_string(packet, end='\x00') (packet, res['server_threadid']) = utils.read_int(packet, 4) (packet, res['scramble']) = utils.read_bytes(packet, 8) packet = packet[1:] # Filler 1 * \x00 (packet, res['capabilities']) = utils.read_int(packet, 2) (packet, res['charset']) = utils.read_int(packet, 1) (packet, res['server_status']) = utils.read_int(packet, 2) packet = packet[13:] # Filler 13 * \x00 (packet, scramble_next) = utils.read_bytes(packet, 12) res['scramble'] += scramble_next return res
def raise_error(cls, buf): """Raise an errors.Error when buffer has a MySQL error""" errno = errmsg = None try: buf = buf[5:] (buf, errno) = utils.read_int(buf, 2) if buf[0] != '\x23': # Error without SQLState errmsg = buf else: (buf, sqlstate) = utils.read_bytes(buf[1:], 5) errmsg = buf except Exception, e: raise errors.InterfaceError("Failed getting Error information (%r)"\ % e)
def _parse(self): buf = self.data if buf[0] != '\xff': raise errors.InterfaceError('Expected an Error Packet.') buf = buf[1:] (buf,self.errno) = utils.read_int(buf, 2) if buf[0] != '\x23': # Error without SQLState self.errmsg = buf else: (buf,self.sqlstate) = utils.read_bytes(buf[1:],5) self.errmsg = buf
def raise_error(buf): """Raise an errors.Error when buffer has a MySQL error""" errno = errmsg = None try: buf = buf[5:] (buf,errno) = utils.read_int(buf, 2) if buf[0] != '\x23': # Error without SQLState errmsg = buf else: (buf,sqlstate) = utils.read_bytes(buf[1:],5) errmsg = buf except Exception, e: raise InterfaceError("Failed getting Error information (%r)"\ % e)
def _file_from_address(cls, address): bs = read_bytes(cls._disk_name, address, cls._header_size) name_length = bs[0] if name_length == 0: return None name = bs[1:10][0:name_length].decode('utf-8') type = bs[10] size = int_from_bytes(bs[11:13]) parent_address = int_from_bytes(bs[13:15]) next_address = int_from_bytes(bs[15:17]) content_address = int_from_bytes(bs[17:19]) path = cls._path_from_address(address) f = cls( path=path, name_length=name_length, name=name, type=type, size=size, address=address, parent_address=parent_address, next_address=next_address, content_address=content_address, ) return f
def load_weights_from_file(self, filename): """ Loads saved weights to model Arguments: --------- model: [tf.keras.Model] Model nb_grids: [int] # of gridcells """ assert (isinstance( filename, Path)), "Weights file must be given as [pathlib.Path]" # TODO: Should implement some sort of assertion to verify the weights file and the # model are compatible (same amount) all_weights = np.fromfile(filename, dtype='float32') offset = 4 nb_convs = len(self.convs) nb_grids = np.prod(self.grid) for layer in self.layers: weights_list = layer.get_weights() if 'conv' in layer.name: if len(weights_list) > 1: # Kernel and bias bias, offset = read_bytes(all_weights, offset, np.prod(weights_list[1].shape)) kernel, offset = read_bytes(all_weights, offset, np.prod(weights_list[0].shape)) kernel = kernel.reshape( list(reversed(weights_list[0].shape))) kernel = kernel.transpose( [2, 3, 1, 0] ) # TODO: Investigate this transpose (why that order of axis, look how kernel are stored in weigths file) layer.set_weights([kernel, bias]) else: # just kernel kernel, offset = read_bytes(all_weights, offset, np.prod(weights_list[0].shape)) kernel = kernel.reshape( list(reversed(weights_list[0].shape))) kernel = kernel.transpose( [2, 3, 1, 0] ) # TODO: Investigate this transpose (why that order of axis, look how kernel are stored in weigths file) layer.set_weights([kernel]) if 'norm' in layer.name: size = np.prod(weights_list[0].shape) beta, offset = read_bytes(all_weights, offset, size) gamma, offset = read_bytes(all_weights, offset, size) mean, offset = read_bytes(all_weights, offset, size) var, offset = read_bytes(all_weights, offset, size) layer.set_weights([gamma, beta, mean, var]) # Rescale last conv kernel to ouput image layer = self.get_layer('conv_{}'.format(nb_convs)) weights_list = layer.get_weights() new_kernel = np.random.normal(size=weights_list[0].shape) / nb_grids new_bias = np.random.normal(size=weights_list[1].shape) / nb_grids layer.set_weights([new_kernel, new_bias])