Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import os
- class Data(object):
- @staticmethod
- def hex(data):
- return " ".join("%02X" % (ord(c)) for c in data)
- class Rom(object):
- HEADER_SIZE = 0x200
- class Error(Exception):
- pass
- class Block(object):
- __slots__ = ("start", "end")
- def __init__(self, start, end):
- self.start = start
- self.end = end
- @property
- def size(self):
- return self.end - self.start
- __slots__ = ("_file", "_header", "data")
- def __init__(self, file):
- self._file = file
- self.__check()
- with open(file, "rb") as rom:
- if self._header:
- rom.seek(Rom.HEADER_SIZE)
- self.data = rom.read()
- def __check(self):
- name = self._file.lower()
- if name.endswith(".sfc"):
- self._header = False
- elif name.endswith(".smc"):
- self._header = True
- else:
- raise Rom.Error("Invalid ROM: %s" % (self._file))
- def compare(self, rom):
- start = None
- for i in xrange(len(self.data)):
- try:
- if self.data[i] == rom.data[i]:
- if start is not None:
- yield Rom.Block(start, i)
- start = None
- else:
- if start is None:
- start = i
- except IndexError:
- break
- if start is not None:
- yield Rom.Block(start, i)
- def main(argc, argv):
- if argc != 3:
- sys.exit(1)
- rom1 = Rom(argv[1].strip())
- rom2 = Rom(argv[2].strip())
- total = 0
- size = 0
- biggest = 0
- smallest = os.path.getsize(argv[1].strip())
- os.system("cls")
- for block in rom1.compare(rom2):
- total += 1
- size += block.size
- biggest = max(biggest, block.size)
- smallest = min(smallest, block.size)
- start = block.start
- end = min(start+8, block.end)
- data1 = rom1.data[start:end]
- data2 = rom2.data[start:end]
- print("[BLOCK] 0x%06X -> 0x%06X | %s -> %s" % (
- block.start, block.end,
- Data.hex(data1), Data.hex(data2)
- ))
- if total > 0:
- print("")
- print("------------------------------")
- print("[TOTAL] %i block(s)" % (total))
- print("[SIZE] %i byte(s)" % (size))
- print("------------------------------")
- if total > 1:
- print("")
- print("------------------------------")
- print("[BIGGEST] %i byte(s)" % (biggest))
- print("[SMALLEST] %i byte(s)" % (smallest))
- print("------------------------------")
- else:
- print("[*] ROMs match!")
- print("")
- os.system("pause")
- sys.exit(0)
- if __name__ == "__main__":
- main(len(sys.argv), sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement