import io,sys
data = open(r"C:/Users/vikto/Documents/Claude/rt-4d/radio-spi-dump.bin","rb").read()
out=io.open(r"C:/Users/vikto/Documents/Claude/rt-4d/analyze/out7.txt","w",encoding="utf-8")

seg=data[0x198000:0x19b800]
txt=seg.decode('gb2312', errors='replace')
out.write("BLOB1 GB2312 first 200 chars:\n")
out.write(txt[:200]+"\n\n")
out.write("BLOB1 GB2312 chars 3000..3080:\n")
out.write(txt[3000:3080]+"\n\n")

# BLOB3 filter FFFF and 0
seg3=data[0x3f0000:0x400000]
cps=[]
for i in range(0,len(seg3)-1,2):
    v=(seg3[i]<<8)|seg3[i+1]
    cps.append(v)
real=[v for v in cps if v not in (0,0xffff)]
out.write("BLOB3 real cps count: %d\n"%len(real))
out.write("BLOB3 first 40 chars: "+''.join(chr(v) for v in real[:40])+"\n")
out.write("BLOB3 range: %04x..%04x\n"%(min(real),max(real)))
asc=all(real[i]<=real[i+1] for i in range(len(real)-1))
out.write("BLOB3 ascending(ignoring pad): %s\n\n"%asc)

# Does BLOB1's GB2312 chars correspond to BLOB3 unicode set? Convert BLOB1 chars to codepoints
b1cps=[ord(c) for c in txt if ord(c)>0x2000]
out.write("BLOB1 first 40 unicode cps: "+' '.join('%04x'%c for c in b1cps[:40])+"\n")
out.write("BLOB1 unique cps: %d, BLOB3 unique: %d\n"%(len(set(b1cps)),len(set(real))))
inter=set(b1cps)&set(real)
out.write("intersection size: %d\n"%len(inter))
out.close()
print("done")
