# Matches libzhl signatures against the exe. Usage: zhl_match.py exe zhl_dir out.tsv # exact unique match -> "exact"; else longest unique prefix (>=20 bytes) -> "prefixN" import re, sys, struct, glob, os exe, zdir, out = sys.argv[1:4] data = open(exe, 'rb').read() pe = struct.unpack_from('= limit: break return r sigP = re.compile(r'^\s*"([0-9a-fA-F?]+)"\s*:\s*$') structP = re.compile(r'^\s*struct\s+(\w+)') nameP = re.compile(r'([A-Za-z_][\w:~]*)\s*\(') stats = {} rows = [] if True: for f in sorted(glob.glob(os.path.join(zdir, '*.zhl'))): lines = open(f, errors='replace').read().split('\n') struct_ = None for i, l in enumerate(lines): m = structP.match(l) if m: struct_ = m.group(1) m = sigP.match(l) if not m: continue decl = '' j = i + 1 while j < len(lines) and ';' not in decl: decl += lines[j].strip() + ' '; j += 1 nm = nameP.search(re.sub(r'<[^>]*>', '', decl)) if not nm: continue name = nm.group(1) if '::' not in name and struct_: name = struct_ + '::' + name sig = m.group(1) h = hits(sig, len(sig)) q = 'exact' if len(h) == 1 else ('multi' if h else None) if not h: lo, hi = 40, len(sig) # binary search longest prefix with >=1 hit if len(sig) > lo and hits(sig, lo): while hi - lo > 2: mid = (lo + hi) // 4 * 2 if hits(sig, mid): lo = mid else: hi = mid h = hits(sig, lo) q = 'prefix%d/%d' % (lo // 2, len(sig) // 2) if len(h) == 1 else 'multi' stats[(q or 'miss').split('/')[0][:6]] = stats.get((q or 'miss').split('/')[0][:6], 0) + 1 if q and q != 'multi': rows.append((to_va(h[0]), name, q, decl.strip())) from collections import Counter dup = Counter(r[0] for r in rows) with open(out, 'w') as o: for r in rows: if dup[r[0]] > 1 and r[2] != 'exact': continue # prefix match collided with another name o.write('%08x\t%s\t%s\t%s\n' % r) print(stats) # vtables: class \t slot \t name (slot names come from decls or "skip; // Name" comments) with open(os.path.join(os.path.dirname(out), 'zhl_vtables.tsv'), 'w') as o: for f in sorted(glob.glob(os.path.join(zdir, '*.zhl'))): txt = open(f, errors='replace').read() for m in re.finditer(r'struct\s+(\w+)[^{]*\{(.*?)__vtable\s*\{(.*?)\n\s*\};', txt, re.S): cls, body = m.group(1), m.group(3) body = re.sub(r'"[^"]*"\s*:', '', body) entries = re.findall(r'\s*(skip\s*;[^\n]*|[^;]*?;)', body, re.S) slot = 0 for e in entries: e = e.strip() if not e: continue if e.startswith('skip'): nm = re.search(r'//\s*(\w+)', e) name = nm.group(1) if nm else None else: nm = nameP.search(re.sub(r'<[^>]*>', '', e)) name = nm.group(1) if nm else None if name and name != 'skip': o.write('%s\t%d\t%s\n' % (cls, slot, name)) slot += 1 # struct fields from zhl: struct \t offset \t type name with open(os.path.join(os.path.dirname(out), 'zhl_fields.tsv'), 'w') as o: for f in sorted(glob.glob(os.path.join(zdir, '*.zhl'))): struct_ = None for l in open(f, errors='replace'): m = structP.match(l) if m: struct_ = m.group(1) b = re.search(r':\s*public\s+(\w+)', l) if b: o.write('%s\tbase\t%s\n' % (struct_, b.group(1))) m = re.match(r'^\s*([\w:<>,\s\*&]+?)\s*:\s*(0x[0-9a-fA-F]+)\s*;', l) if not (m and struct_): continue parts = re.findall(r'([^,:]+?)\s*:\s*(0x[0-9a-fA-F]+)', l.split(';')[0]) # "int _a : 0x0, _b : 0x4;" is two fields ftype = parts[0][0].split()[:-1] for decl, off in parts: words = decl.split() o.write('%s\t%s\t%s\n' % (struct_, off.lower(), ' '.join((words if len(words) > 1 else ftype + words))))