#!/usr/bin/env python3
"""Byte-to-byte map: where do the banned-screen strings/codes live in each layer?"""
import struct, sys
sys.stdout.reconfigure(encoding='utf-8')

FILES = {
    'App.framework': r'patch_src\Payload\Driver.app\Frameworks\App.framework\App',
    'Driver': r'patch_src\Payload\Driver.app\Driver',
    'build-info': r'patch_src\Payload\Driver.app\Frameworks\build-info.framework\build-info',
    'plugin': r'patch_src\Payload\Driver.app\Frameworks\flutter_bshield_callback.framework\flutter_bshield_callback',
}

# stage-1 needles: UI strings + channel names (plain UTF-8)
TEXT = ['vmwieropkg', 'Banned', 'banned', 'header_code', 'repack', 'Repack',
        'developer', 'Developer', 'jailbreak', 'Jailbreak', 'hooking', 'frida',
        'vpn', 'VPN', 'proxy', 'getDetectFlags', 'getShieldCode', 'readData',
        'flutter_bshield', 'bshield', 'BShield', 'protectionStatus', 'ProtectionStatus',
        'Thi\xe1\xba\xbft b\xe1\xbb\x8b', 'v\xe1\xba\xa5n \xc4\x91\xe1\xbb\x81',
        'lockDevice', 'blockDevice', 'block', 'Block', 'device_status']

# stage-2 needles: StandardMessageCodec int32 1 and 11 inside a 2-element list
# list header 0x0D + count, then 0x03 + 4-byte BE int
CODEC1 = bytes([0x03, 0, 0, 0, 0x01])
CODEC11 = bytes([0x03, 0, 0, 0, 0x0B])
# Dart Smi tagged values in AOT object pools (arm64: smi = v<<1)
SMI1 = struct.pack('<Q', 2)
SMI11 = struct.pack('<Q', 22)

for name, path in FILES.items():
    d = open(path, 'rb').read()
    print('== %s (%d bytes)' % (name, len(d)))
    for t in TEXT:
        b = t.encode('utf-8')
        i, hits = 0, []
        while True:
            i = d.find(b, i)
            if i < 0:
                break
            hits.append(i)
            i += 1
        if hits:
            print('   %-24s x%-3d %s' % (t, len(hits), [hex(h) for h in hits[:5]]))
    for label, pat in (('codec int32[1]', CODEC1), ('codec int32[11]', CODEC11),
                       ('smi 1 tagged', SMI1), ('smi 11 tagged', SMI11)):
        i, hits = 0, []
        while True:
            i = d.find(pat, i)
            if i < 0:
                break
            hits.append(i)
            i += 1
        if hits:
            print('   %-24s x%-3d %s' % (label, len(hits), [hex(h) for h in hits[:5]]))
