Created
November 5, 2014 22:40
-
-
Save BlockoS/8e49df68d1c2f39ba4f0 to your computer and use it in GitHub Desktop.
Creates ips patch from .json
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/python | |
# -*- coding: utf-8 -*- | |
import os | |
import sys | |
import json | |
class IPSRecord: | |
def __init__(self): | |
self.offset = 0 | |
self.data = None | |
self.repeat = 0 | |
class IPSPatch: | |
def __init__(self): | |
self.records = [] | |
def add(self, record): | |
self.records.append(record) | |
def write(self, filename): | |
header = b'PATCH' | |
footer = b'EOF' | |
recordHeader = bytearray(5) | |
rleData = bytearray(3) | |
output = open(filename, 'wb') | |
output.write(header) | |
for item in self.records: | |
recordData = None | |
# serialize offset | |
recordHeader[0] = (item.offset >> 16) & 0xff | |
recordHeader[1] = (item.offset >> 8) & 0xff | |
recordHeader[2] = (item.offset ) & 0xff | |
if None == item.repeat: | |
# serialize size | |
size = len(item.data) | |
recordHeader[3] = (size >> 8) & 0xff; | |
recordHeader[4] = (size ) & 0xff; | |
# item data | |
recordData = item.data | |
else: | |
# empty size | |
recordHeader[3] = 0 | |
recordHeader[4] = 0 | |
# record data is 3 bytes long. | |
# -- 1st and 2nd bytes are repeat count | |
rleData[0] = (item.repeat >> 8) & 0xff | |
rleData[1] = (item.repeat ) & 0xff | |
# -- 3rd byte is the repeated data | |
rleData[2] = item.data & 0xff | |
recordData = rleData | |
# output record header | |
output.write(recordHeader) | |
# output record data | |
output.write(recordData) | |
output.write(footer) | |
output.close() | |
def read(self, filename): | |
inputFile = open(filename) | |
data = json.load(inputFile) | |
inputFile.close() | |
for entry in data: | |
item = None | |
if 'filename' in entry: | |
item = IPSRecord | |
fh = open(entry['filename']) | |
item.data = bytearray(fh.read()) | |
fh.close() | |
elif 'data' in entry: | |
item = IPSRecord | |
item.data = bytearray.fromhex(entry['data']) | |
elif 'repeat' in entry: | |
item = IPSRecord | |
item.data = int(entry['repeat'], 16) | |
item.repeat = entry['count'] | |
if None == item: | |
raise Exception("Missing filename, data or repeat option.") | |
item.offset = entry['offset'] | |
self.add(item) | |
if __name__ == "__main__": | |
ips = IPSPatch() | |
ips.read("patch.json") | |
ips.write("bla.ips") |
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
[ | |
{ | |
"offset": 222, | |
"filename": "bla.bin" | |
}, | |
{ | |
"offset": 200, | |
"data": "29ff01ef" | |
}, | |
{ | |
"offset": 155, | |
"repeat": "ff", | |
"count": 16 | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment