Skip to content

Instantly share code, notes, and snippets.

@asahui
Last active March 9, 2022 01:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asahui/a6af64606a9476a40442274335f5feaf to your computer and use it in GitHub Desktop.
Save asahui/a6af64606a9476a40442274335f5feaf to your computer and use it in GitHub Desktop.
PS2 Game ELF CRC
#!/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