#!/usr/bin/env python3
"""Compare the Driver binary inside the delivered build 54 IPA vs build 55's."""
import os, sys, zipfile

sys.stdout.reconfigure(encoding='utf-8')

IPA54 = r'E:\ios\GSMDriver-3.19.0-patched54-unsigned.ipa'
NEW55 = r'E:\ios\work319\patch_src\Payload\Driver.app\Driver'

zf = zipfile.ZipFile(IPA54)
name = [n for n in zf.namelist() if n.endswith('Driver.app/Driver')][0]
old = zf.read(name)
new = open(NEW55, 'rb').read()
print('build54 Driver: %d bytes   build55 Driver: %d bytes' % (len(old), len(new)))
if len(old) != len(new):
    sys.exit('size differs')

diffs = [i for i in range(len(old)) if old[i] != new[i]]
print('differing bytes: %d' % len(diffs))
runs = []
for i in diffs:
    if runs and i <= runs[-1][1] + 4096:
        runs[-1][1] = i
    else:
        runs.append([i, i])
print('runs (coalesced within 4K): %d' % len(runs))
for a, b in runs[:30]:
    print('  0x%09x-0x%09x  (%d bytes)' % (a, b, b - a + 1))

# also list the frameworks in 54 vs 55 ipa count
names54 = [n for n in zf.namelist() if n.endswith('.framework/') or '/Frameworks/' in n]
print('\nbuild54 ipa entries: %d' % len(zf.namelist()))
