# -*- coding: utf-8 -*-
import re
import zipfile
from pathlib import Path

APK = Path(__file__).resolve().parent.parent / "apks" / "maribank_sg_base.apk"
needle = b"dfp is empty"
with zipfile.ZipFile(str(APK)) as zf:
    data = b"".join(zf.read(n) for n in zf.namelist() if n.endswith(".dex"))

idx = 0
while True:
    idx = data.find(needle, idx)
    if idx < 0:
        break
    ctx = data[max(0, idx - 400) : idx + 400]
    classes = sorted(set(m.group().decode()[1:-1].replace("/", ".")
                         for m in re.finditer(rb"Lcom/[^;\x00]{5,160};", ctx)))
    print("\n--- hit at", idx, "---")
    for c in classes:
        if any(k in c.lower() for k in ("user", "register", "dfp", "fingerprint", "viewmodel", "rn", "helper")):
            print(" ", c)
    # printable strings nearby
    for m in re.finditer(rb"[\x20-\x7e]{4,80}", ctx):
        s = m.group().decode()
        if any(k in s.lower() for k in ("dfp", "register", "empty", "error", "unavailable", "fingerprint")):
            print("  str:", s)
    idx += len(needle)
