Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import shutil
- import subprocess
- import tempfile
- import zlib
- replace_list = [('006cc000', 0x6CC000, 25553321, 0x1FAA800,),
- ('09880800', 0x9880800, 13533, 0x09884000),
- ('0c780800', 0xC780800, 15855, 0x0C784800),
- ('093ae800', 0x93AE800, 37749, 0x093B8000)]
- def unzip(f, offset, size):
- saved = f.tell()
- f.seek(offset)
- with tempfile.NamedTemporaryFile(delete = False) as g:
- filename = g.name
- g.write(zlib.decompress(f.read(size)))
- f.seek(saved)
- return filename
- #Make a copy of AC.BIN
- if os.path.isfile('AC.orig'):
- pass
- elif os.path.isfile('AC.BIN'):
- shutil.copy('AC.BIN', 'AC.orig')
- else:
- print('File AC.BIN not found.')
- quit()
- #Extract file and decompress
- with open('AC.orig', 'rb') as f:
- with open('AC.BIN', 'wb') as g:
- #Sort by sub-file address
- for name, offset, size, next_offset in sorted(replace_list, key = lambda x: x[1]):
- g.write(f.read(offset - g.tell())) #Copy up to offset
- tempfilename = unzip(f, offset, size) #Unzip file
- patched = '{}.BIN'.format(name)
- #Apply patch using xdelta
- subprocess.run(['xdelta', '-d', '-s', tempfilename, '{}.xdelta'.format(name), patched])
- os.remove(tempfilename) #Delete temporary file
- with open(patched, 'rb') as h: #Compress and write our patched file
- g.write(zlib.compress(h.read()))
- os.remove(patched) #Delete temporary file
- g.write(b'\x00' * (next_offset - g.tell())) #Write zeros up to start of next file
- f.seek(next_offset) #Align file pointer
- g.write(f.read()) #Copy the rest of the file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement