Last active
March 9, 2022 01:32
-
-
Save asahui/a6af64606a9476a40442274335f5feaf to your computer and use it in GitHub Desktop.
PS2 Game ELF CRC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
from struct import unpack | |
# unpack way | |
def getCRC(filename): | |
with open(filename, "rb") as f: | |
crc = 0 | |
data =f.read(4) | |
while data != b"": | |
word, = unpack("I", data) | |
crc ^= word | |
data = f.read(4) | |
print 'crc: {0:X}'.format(crc) | |
# array way | |
def getCRC1(filename): | |
import os | |
from array import array | |
with open(filename, "rb") as f: | |
data = array('I') | |
data.fromfile(f, os.stat(filename).st_size / 4) | |
crc = reduce(lambda x,y: x^y, data.tolist(), 0) | |
print 'crc: {0:X}'.format(crc) | |
if __name__ == '__main__': | |
if not len(sys.argv) == 2: | |
print "Usage: python crc.py ELF_FILE" | |
else: | |
getCRC1(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment