Versione migliorata, che prova a riconoscere automaticamente dove iniziano i blocchi, così non bisogna passargli gli offset manualmente. Gli argomenti sono soltanto il file .tap che si vuole ripulire e quello dove salvare i dati ripuliti
#!/usr/bin/python3
import io
import sys
def statist(infile,outfile):
inputs = io.open(infile,'rb')
outputs = io.open(outfile,'wb')
pulisci = False
stat = [{},{},{},{},{},{},{},{}]
beginstat = [{},{}]
lunghpilot = 0
blocchi = 0
while 1:
p =inputs.tell()
thisbyte=inputs.read(1)
if len(thisbyte)==0:
break
if not pulisci:
if thisbyte[0]>=0x0f and thisbyte[0]<0x20:
lunghpilot = lunghpilot + 1
#clean pilot
#thisbyte=bytes([0x1b])
elif thisbyte[0]>=0x40 and thisbyte[0]<0xc0 and lunghpilot > 450:
pulisci = True
inizioblocco = p
print("iniz blocco "+str(blocchi)+" a "+hex(thisbyte[0])+" at "+str(p))
elif thisbyte[0]==0x00:
lunghpilot = 0
thisbyte=thisbyte+inputs.read(3)
else:
lunghpilot = 0
if pulisci:
if ((p - inizioblocco) % 40) == 0:
if(thisbyte[0]<0x40):
print("fineblocco "+hex(thisbyte[0])+" at "+str(p))
lunghpilot = 0
pulisci = False
blocchi = blocchi + 1
thisbyte2=inputs.read(1)
#clean end of block
thisbyte=bytes([0x35,0x35])
else:
thisbyte=thisbyte+inputs.read(1)
for i in range(2):
if not thisbyte[i] in beginstat[i]:
beginstat[i][thisbyte[i]]=0
beginstat[i][thisbyte[i]]=beginstat[i][thisbyte[i]]+1
toolittle=thisbyte[0]-0x5a
if toolittle<0:
#print("accorc "+hex(thisbyte[0])+" "+hex(thisbyte[1])+" at "+str(p))
thisbyte=bytes([0x5a,thisbyte[1]+toolittle])
#clean start of block
#thisbyte=bytes([0x6c,0x6c])
elif ((p - inizioblocco) % 40) == 1:
print("What?")
elif ((p - inizioblocco) % 40) == 2:
#clean right after start of block
pass
thisbyte=bytes([0x35])
elif ((p - inizioblocco) % 40) == 3:
#clean right after start of block
pass
thisbyte=bytes([0x35])
else:
thisbyte=thisbyte+inputs.read(3)
statbase = 0
resbytes=thisbyte
if (thisbyte[0]+thisbyte[1]<thisbyte[2]+thisbyte[3]):
if(thisbyte[0]>0x28):
resbytes=bytes([0x25])+resbytes[1:4]
if(thisbyte[2]<0x2f):
resbytes=resbytes[0:2]+bytes([0x2f])+resbytes[3:4]
#clean zero bit
#resbytes=bytes([0x17,0x17,0x35,0x35])
else:
statbase = 4
if(thisbyte[0]<0x2f):
resbytes=bytes([0x2f])+resbytes[1:4]
if(thisbyte[2]>0x28):
resbytes=resbytes[0:2]+bytes([0x25])+resbytes[3:4]
#clean one bit
#resbytes=bytes([0x35,0x35,0x17,0x17])
for i in range(4):
if not thisbyte[i] in stat[i+statbase]:
stat[i+statbase][thisbyte[i]]=0
stat[i+statbase][thisbyte[i]]=stat[i+statbase][thisbyte[i]]+1
thisbyte=resbytes
outputs.write(thisbyte)
bytesin=inputs.read()
outputs.write(bytesin)
statbase = 0
for i in stat:
print("stat for byte "+str(int(statbase/4))+" pos "+str(statbase%4))
statbase=statbase+1
for k in sorted(i.keys()):
print("byte "+hex(k)+" occurs "+str(i[k])+" times")
statbase = 0
for i in beginstat:
print("stat for begin "+str(statbase))
statbase=statbase+1
for k in sorted(i.keys()):
print("byte "+hex(k)+" occurs "+str(i[k])+" times")
if len(sys.argv)<3:
print("not enough args")
sys.exit
statist(sys.argv[1],sys.argv[2])