SHOW:
|
|
- or go back to the newest paste.
1 | ||
2 | def split_as_size(arr,size):return arr[:size],arr[size:] | |
3 | ||
4 | - | def bytes_to_int(bytes): |
4 | + | |
5 | - | #fill this in later. |
5 | + | |
6 | - | # I'm rusty on how python handles bytes tbh |
6 | + | |
7 | - | r,place=0,len(bytes)-1 |
7 | + | |
8 | - | for i in range(place,0,-1): |
8 | + | |
9 | - | r+= (255**place) * int(bytes[i])#I think this works. |
9 | + | |
10 | - | return r |
10 | + | |
11 | def create_from_bytes(cls,byte_array): | |
12 | def extract_chunk(arr): | |
13 | offset,arr=split_as_size(arr,3) | |
14 | size,arr=split_as_size(arr,2) | |
15 | data,arr=split_as_size(arr,int.from_bytes(size)) | |
16 | return Chunk(offset,int.from_bytes(data)),arr | |
17 | def pull_all_chunks(arr): | |
18 | while len(arr)>3: | |
19 | chunk,arr=extract_chunk(arr) | |
20 | yield chunk | |
21 | return cls(list(pull_all_chunks(byte_array))) | |
22 | def __init__(self,chunks):self.chunks = chunks | |
23 | - | data,arr=split_as_size(arr,size) |
23 | + | |
24 | - | return Chunk(offset,data),arr |
24 | + | |
25 | arr[chunk.position:chunk.position+chunk.size]=chunk.data | |
26 | return arr | |
27 | - | chunk,byte_array=extract_chunk(arr) |
27 | + | |
28 | if __name__=="__main__": | |
29 | from sys import argv;argv=argv[1:] | |
30 | from os.path import isfile | |
31 | patches,target=[],None | |
32 | for filename in argv: | |
33 | if not isfile(filename):continue | |
34 | if filename[:filename.rfind(".")].lower()=='ips': | |
35 | patches.append(filename) | |
36 | else:target=filename | |
37 | if target and patches: | |
38 | base_data = open(target,'rb').read()#read in the data to modify. | |
39 | for patch in map(IPS.create_from_stream,map(lambda fn:open(fn,'rb'),patches)): | |
40 | base_data=patch.apply(base_data) | |
41 | with open('patched-{}-{}'.format("-".join(map(patch_name,patches)),target),'wb') as output: | |
42 | output.write(base_data) |