# -*- coding: utf-8 -*-
# Đào dex: logic gate gói dịch vụ + ccpToken (kênh MQTT command) + checkFeatureEnabled
import io, sys, re, os
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
DEXDIR = r'E:\ios\vf_dex'

def scan_ctx(rx, label, b=160, a=280, maxn=8):
    print(f'\n################ {label} ################')
    rx = re.compile(rx)
    total = 0
    for fn in sorted(os.listdir(DEXDIR)):
        if not fn.endswith('.dex'):
            continue
        data = open(os.path.join(DEXDIR, fn), 'rb').read()
        for m in rx.finditer(data):
            total += 1
            if total > maxn:
                return
            i = m.start()
            chunk = data[max(0, i - b):i + a].decode('ascii', 'replace').replace('\x00', '·')
            print(f'--- {fn}@{i}:')
            print(chunk)
            print()
    if total == 0:
        print('(không có match)')

scan_ctx(rb'ccpTokenExpiredTimeMap', 'CCP TOKEN (MQTT command channel)')
scan_ctx(rb'checkFeatureEnabled', 'checkFeatureEnabled')
scan_ctx(rb'REMOTE_[A-Z_]{4,30}', 'FEATURE FLAGS REMOTE_*')
scan_ctx(rb'SOTA_[A-Z0-9_]{3,40}', 'MÃ GÓI SOTA_*')
