"""Optional host-only reference runner for the supplied TAK decoder 2.3 DLL. Install pefile and unicorn with pip. The Android app never uses this script, the Windows DLL, or emulation. Only a small Win32 IO adapter is provided here; this is not a general Windows runtime. """ import sys,struct from pathlib import Path import pefile from unicorn import Uc,UC_ARCH_X86,UC_MODE_32,UC_HOOK_CODE,UC_HOOK_MEM_INVALID from unicorn.x86_const import * class Oracle: def __init__(self,path): self.pe=pefile.PE(path); self.base=self.pe.OPTIONAL_HEADER.ImageBase self.uc=Uc(UC_ARCH_X86,UC_MODE_32); u=self.uc u.mem_map(0,0x10000); u.mem_write(0,struct.pack('=0x14000000:raise ValueError('heap exhausted') return a def cstr(self,a): if not a:return '' b=bytearray() while (x:=self.read(a+len(b),1))!=b'\0':b+=x return b.decode('latin1') def ret(self,val,n): sp=self.uc.reg_read(UC_X86_REG_ESP);ra=self.dword(sp) self.uc.reg_write(UC_X86_REG_EAX,val&0xffffffff);self.uc.reg_write(UC_X86_REG_ESP,sp+4+n*4);self.uc.reg_write(UC_X86_REG_EIP,ra) def hook(self,u,a,size,data): self.last=(self.last+[a])[-15:] if a==self.stop:u.emu_stop();return if a not in self.funcs:return name=self.funcs[a];sp=u.reg_read(UC_X86_REG_ESP);args=[self.dword(sp+4+i*4) for i in range(10)] if self.trace:print(name,[hex(x) for x in args[:4]],flush=True) if name in ('InitializeCriticalSection','EnterCriticalSection','LeaveCriticalSection','DeleteCriticalSection'):self.ret(1,1) elif name=='VirtualAlloc':self.ret(self.alloc(args[1]),4) elif name=='VirtualFree':self.ret(1,3) elif name=='LocalAlloc':self.ret(self.alloc(args[1]),2) elif name in ('LocalFree','CloseHandle','TlsFree','FreeLibrary'):self.ret(0 if name=='LocalFree' else 1,1) elif name=='TlsAlloc':self.ret(1,0) elif name=='TlsGetValue':self.ret(self.tls.get(args[0],0),1) elif name=='TlsSetValue':self.tls[args[0]]=args[1];self.ret(1,2) elif name=='GetVersionExA':u.mem_write(args[0]+4,struct.pack('>32) self.ret(n,2) elif name=='SetFilePointer': f=self.files[args[0]];off=args[1] if args[1]<0x80000000 else args[1]-0x100000000 if args[2]:off+=(self.dword(args[2]) if self.dword(args[2])<0x80000000 else self.dword(args[2])-0x100000000)<<32 n=f.seek(off,args[3]);self.ret(n,4) elif name=='ReadFile': f=self.files[args[0]];b=f.read(args[2]);u.mem_write(args[1],b) if args[3]:self.write32(args[3],len(b)) self.ret(1,5) else:raise RuntimeError(f'Unhandled import {name}: {[hex(x) for x in args[:6]]}') def invalid(self,u,access,a,size,value,data): raise RuntimeError(f'DLL accessed unmapped memory at {a:#x}') def call(self,fn,*args): a=self.exports[fn] if isinstance(fn,str) else fn u=self.uc;u.reg_write(UC_X86_REG_ESP,self.stack-4*(len(args)+1));u.mem_write(self.stack-4*(len(args)+1),struct.pack('<'+'I'*(len(args)+1),self.stop,*args)) u.reg_write(UC_X86_REG_EBP,0);u.reg_write(UC_X86_REG_EAX,0);u.reg_write(UC_X86_REG_EDX,0);u.reg_write(UC_X86_REG_ECX,0) u.emu_start(a,self.stop,count=100000000) if u.reg_read(UC_X86_REG_EIP)!=self.stop:raise RuntimeError('Instruction budget exceeded') return u.reg_read(UC_X86_REG_EAX) def open_reference(dll, source): oracle = Oracle(str(dll)) oracle.call(oracle.base+oracle.pe.OPTIONAL_HEADER.AddressOfEntryPoint, oracle.base, 1, 0) oracle.input_path = str(source) filename = oracle.alloc(64) oracle.uc.mem_write(filename, b"reference.tak\0") options = oracle.alloc(64) handle = oracle.call("tak_SSD_Create_FromFile", filename, options, 0, 0) if not oracle.call("tak_SSD_Valid", handle): raise RuntimeError(f"Reference DLL could not open the file; state {oracle.call('tak_SSD_State', handle):#x}") return oracle, handle def compare(dll, source, native, packets=None): import subprocess, tempfile, hashlib with tempfile.TemporaryDirectory(prefix="loopdeck-tak1-") as temp: output = Path(temp)/"native.pcm" command = [str(native), str(source), str(output)] if packets is not None: command.append(str(packets)) subprocess.run(command, check=True) actual = output.read_bytes() oracle, handle = open_reference(dll, source) info = oracle.alloc(128) if oracle.call("tak_SSD_GetStreamInfo", handle, info): raise RuntimeError("Cannot read reference stream information") block = oracle.dword(info+40) if not block or len(actual)%block: raise RuntimeError("Native output is not aligned to reference PCM frames") destination, got = oracle.alloc(16384*block), oracle.alloc(4) compared = 0 try: while compared < len(actual): frames = min(16384, (len(actual)-compared)//block) status = oracle.call("tak_SSD_ReadAudio", handle, destination, frames, got) if status or oracle.dword(got) != frames: raise RuntimeError(f"Reference failed after {compared} bytes; status {status:#x}") reference = oracle.read(destination, frames*block) chunk = actual[compared:compared+len(reference)] if chunk != reference: offset = next(i for i,(a,b) in enumerate(zip(chunk,reference)) if a != b) raise RuntimeError(f"PCM mismatch at byte {compared+offset}") compared += len(reference) finally: oracle.call("tak_SSD_Destroy", handle) for file in oracle.files.values(): file.close() print(f"Exact PCM match: {compared} bytes; sha256 {hashlib.sha256(actual).hexdigest()}") if __name__ == "__main__": import argparse parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--dll", type=Path, required=True) parser.add_argument("--input", type=Path, required=True) parser.add_argument("--native", type=Path, required=True) parser.add_argument("--packets", type=int, help="Limit both sides to complete packets in a truncated sample") args = parser.parse_args() compare(args.dll.resolve(), args.input.resolve(), args.native.resolve(), args.packets)