Código: Seleccionar todo
import struct
import sys
blocksize = 0x2000
errstart = 0 # <strong class="highlight">File</strong> offset of start of error span
errend = 0 # <strong class="highlight">File</strong> offset just past end of error span
diffct = 0 # Difference region count
chunk_char = {8:'Q'}
def endSpan(loc):
global diffct,errstart,errend,inSpan
errend = loc
elength = loc-errstart
print "Span of %10X bytes bad from %12X to %12X\n" % (elength, errstart, errend-1)
diffct += 1
return
def unpack(buffer, chunk):
rv = []
for x in range(0, len(buffer)/chunk):
rv.append('%X' % struct.unpack(chunk_char[chunk],buffer[chunk*x:chunk*(x+1)]))
return rv
def usage():
print "Usage: %s file1 file2" % sys.argv[0]
return
def main():
global errstart, inSpan
argc = len(sys.argv)
if argc < 3:
usage()
return 1
f1name = sys.argv[1]
f1d = open(f1name, 'rb')
if f1d==None:
print "Error al abrir %s para lectura", f1name
f2name = sys.argv[2]
f2d = open(f2name, 'rb')
if f2d==None:
print "Error al abrir %s para lectura", f2name
if f1d==None or f2d==None: return 1 # Exit if can't open <strong class="highlight">file</strong>
print "Comparando %s y %s" % (f1name, f2name)
diffs = 0 # Count of difference regions
f1off = 0 # Offset of read
f2off = 0 # ...
stride = 8 # Our unit of work
maxlpe = 30 # Max lines to print per error
inSpan = False # Not in a span of errors
s1 = s2 = "Something not null" # Just not None, dammit!
while True:
if s1 != None:
s1 = f1d.read(blocksize)
if s2 != None:
s2 = f2d.read(blocksize)
if s1=="" or s2=="":
if inSpan:
endSpan(f1off)
break
if s1!=s2:
lst1 = unpack(s1,stride)
lst2 = unpack(s2,stride)
lst = [(long(lst1[x],16),long(lst2[x],16),f1off+stride*x) for x in range(len(lst1))]
for e in lst:
if e[0]==e[1]: # Equal?
if inSpan:
endSpan(e[2])
inSpan = False
else: # Not equal
if not inSpan:
inSpan = True
errstart = e[2]
curlpe = 0 # Lines printed for this error
print " OffsetArchivo Contenido1ro Contenido2do XOR"
if curlpe < maxlpe: # OK to print this error line?
print "%12X %16X %16X %16X" % (e[2], e[0], e[1], e[0]^e[1])
curlpe += 1
elif curlpe == maxlpe:
print " ... and so on ..."
curlpe += 1
# Here when done with these bits
f1off += blocksize
f2off += blocksize
f1d.close()
f2d.close()
print "%u difference regions found" % diffct
main()
python nombredearchivo.py a.exe b.exe
salida:
- Comparando a.exe y b.exe
OffsetArchivo Contenido1ro Contenido2do XOR
F8 0 100000000000 100000000000
Span of 8 bytes bad from F8 to FF
1 difference regions found