"""Find NativeLib / loadLibrary targets in TNG APK."""
import re
import zipfile
from pathlib import Path

apk = Path(r"C:\Users\Administrator\Desktop\notiMessage\reverse\apks\tng\base.apk")
split = Path(r"C:\Users\Administrator\Desktop\notiMessage\reverse\apks\tng\split_config.arm64_v8a.apk")

with zipfile.ZipFile(apk) as z:
    data = b"".join(z.read(n) for n in z.namelist() if n.endswith(".dex"))
    print("base dex files:", [n for n in z.namelist() if n.endswith(".dex")])
    print("base lib entries:", [n for n in z.namelist() if "lib/" in n][:40])

print("\nsplit libs:")
with zipfile.ZipFile(split) as z:
    libs = [n for n in z.namelist() if n.endswith(".so")]
    for n in libs:
        print(" ", n)

# strings related to NativeLib
needles = [
    b"NativeLib",
    b"tngd.networksdk",
    b"RetrieveFromNativeLibs",
    b"getApiSixSecretKeys",
    b"networksdk",
    b"libtng",
    b"loadLibrary",
]
print("\n=== string hits ===")
for n in needles:
    hits = list(re.finditer(n, data))
    print(f"{n!r}: {len(hits)}")
    for h in hits[:5]:
        ctx = data[max(0, h.start()-30):h.end()+80]
        ctx = bytes(c if 32 <= c < 127 else 46 for c in ctx)
        print(" ", ctx)

# library name candidates near NativeLib
print("\n=== lib name-like strings near 'NativeLib' / networksdk ===")
for m in re.finditer(rb"[\x20-\x7e]{4,60}", data):
    s = m.group().decode()
    if "network" in s.lower() or "tngd" in s.lower() or s.startswith("lib") and "tng" in s.lower():
        if len(s) < 80:
            print(" ", s)

# specific: System.loadLibrary argument often stored as short string without lib/ prefix
print("\n=== candidate loadLibrary short names ===")
cands = set(re.findall(rb"[\x00]([A-Za-z0-9_]{3,40})[\x00]", data))
for c in sorted(cands):
    s = c.decode()
    if any(k in s.lower() for k in ("tng", "network", "native", "promon", "shield", "apse")):
        print(" ", s)
