def _read(self): # magic if not self._check_magic(): raise Exception('invalid .tmod file') # tModLoader version self.tml_vs = utils.read_string(self.file) # hash self.hash = self._read_bytes(20) # signature self.signature = self._read_bytes(256) # payload length self.pload_len = utils.read_int32(self.file) # payload pload = self._read_bytes(self.pload_len) # check data intergrity if self.hash != hashlib.sha1(pload).digest(): raise Exception('data corruption') pload_stream = BytesIO(pload) # mod name self.name = utils.read_string(pload_stream) # mod version self.vs = utils.read_string(pload_stream) # mod file count self.file_cnt = utils.read_int32(pload_stream) # compressed data self.cprsd_data = pload_stream.read(-1) self.file.close()
def test_read_string(): a = b" test1 test2" with BytesIO(a) as sp: print("first time", utils.read_string(sp)) print("second time", utils.read_string(sp)) print("third time", utils.read_string(sp))
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 _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 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 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 _flate(self): self.files = [] stream = BytesIO(self.cprsd_data) for i in range(self.file_cnt): f_name = utils.read_string(stream) if f_name.endswith('.rawimg'): f_name = '.png'.join(f_name.rsplit('.rawimg')) f_length = utils.read_int32(stream) f_cprsd_length = utils.read_int32(stream) self.files.append(FileEntry(f_name, f_length, f_cprsd_length)) for fe in self.files: print('Flate: ' + fe.name, end='') if fe.length > fe.cprsd_length: fe.data = zlib.decompress(stream.read(fe.cprsd_length), -zlib.MAX_WBITS) else: fe.data = stream.read(fe.length) print(" => OK")
import utils as ut import numpy as np if __name__ == '__main__': lines = ut.read_string("inputs/day5_1.txt") digits = list(map(int, lines.replace("\n", " ").split())) n = len(digits) i = 0 steps = 0 while True: tmp = i i += digits[i] digits[tmp] += 1 steps += 1 if i >= n: break print("steps: %d" % steps)
@numba.jit(nopython=True) def redistribute(blocks): max, ind = argmax(blocks) n = len(blocks) blocks[ind] = 0 for i in range(ind+1, ind+1+max): j = i % n blocks[j] += 1 return blocks if __name__ == '__main__': blocks = ut.read_string("inputs/day6_1.txt") blocks = list(map(int, blocks.replace("\n"," ").split())) #blocks = [0, 2, 7, 0] #{{hashlib.md5(b'Hello World') history = {} steps = 0 while True : code = str(blocks) if code in history: print("steps: %d - loop: %d" % (steps, steps - history[code])) break else: history[code] = steps blocks = redistribute(blocks)
i = 0 steps = 0 while True: tmp = i i += digits[i] if digits[tmp] >= 3: digits[tmp] -= 1 else: digits[tmp] += 1 steps += 1 if i >= n or i < 0: break return steps if __name__ == '__main__': digits = ut.read_string("inputs/day5_2.txt") #digits = "0 3 0 1 -3" digits = list(map(int, digits.replace("\n"," ").split())) import time s = time.time() steps = fast(digits) t = time.time() - s print("steps: %d - time: %.3f" % (steps, t))