This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

ips.py

By: a guest on Dec 11th, 2013  |  syntax: Python  |  size: 1.40 KB  |  views: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. def split_as_size(arr,size):return arr[:size],arr[size:]
  3.  
  4. class IPS:
  5.         MAGIC = b'\x50\x41\x54\x43\x48'
  6.         END = b'\x45\x4f\x46'
  7.         @classmethod
  8.         def create_from_stream(cls,stream):
  9.                 return cls.create_from_bytes(stream.read())
  10.         @classmethod
  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.         def apply(self,arr):
  24.                 for chunk in self.chunks:
  25.                         arr[chunk.position:chunk.position+chunk.size]=chunk.data
  26.                 return 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)
clone this paste RAW Paste Data