import itertools D='files/universal_translator/' S=[open(D+f'samples/sample_0{i}.bin','rb').read() for i in (3,4)] EXP=[bytes.fromhex('1837393923'),bytes.fromhex('3B3311083E100140080008')] M=(1<<64)-1 def sg(x): return x-(1<<64) if x>>63 else x STK={0:'ROT',1:'TUCK',2:'OVER',3:'DUP',4:'EMIT',5:'NIP',6:'SWAP',7:'DROP'} fixed={0x19:'+',0x17:'-',0x13:'*',0x14:'AND',0x10:'RSHIFT',0x12:'LSHIFT'} rest=['XOR','OR','MAX','MIN']; free=[0x11,0x15,0x16,0x18] def run(prog,tab,signed,shmod): s=[];out=[] for b in prog: w=tab.get(b) if w is None: s.append(b);continue if w=='DUP': s.append(s[-1]) elif w=='DROP': s.pop() elif w=='SWAP': s[-1],s[-2]=s[-2],s[-1] elif w=='OVER': s.append(s[-2]) elif w=='ROT': s.append(s.pop(-3)) elif w=='NIP': del s[-2] elif w=='TUCK': s.insert(-2,s[-1]) elif w=='EMIT': out.append(s.pop()&0xff) else: c=s.pop();a=s.pop() f=sg if signed else (lambda x:x) if w=='+':r=a+c elif w=='-':r=a-c elif w=='*':r=a*c elif w=='XOR':r=a^c elif w=='AND':r=a&c elif w=='OR':r=a|c elif w=='MAX':r=a if f(a)>f(c) else c elif w=='MIN':r=a if f(a)>(c&63)) if shmod else (a>>c if c<64 else 0) s.append(r&M) return bytes(out) for signed in (0,1): for shmod in (0,1): for p in itertools.permutations(rest): tab=dict(STK);tab.update(fixed);tab.update(zip(free,p)) o=[run(x,tab,signed,shmod) for x in S] print(signed,shmod,p,[x.hex() for x in o], o==EXP) tab=dict(STK);tab.update(fixed);tab.update({0x11:'XOR',0x15:'MIN',0x16:'MAX',0x18:'OR'}) T=open(D+'transmission.bin','rb').read() for sh in (0,1): try: print(run(T,tab,1,sh)) except Exception as e: print('err',e)