Guest User

inject.py

a guest
Jul 8th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. f = open('feria.lsr', 'rb')
  2. x = f.read()
  3. f.close()
  4. f = open('feria.txt', 'rb')
  5. y = f.read()
  6. f.close()
  7.  
  8. s = x.split(b'\0')
  9. t = y.split(b'\r\n|\r\n')
  10.  
  11. r = []
  12. c = 0
  13. offset = 0
  14. for l in s:
  15.   try:
  16.     # Decoding successful and result != '':
  17.     # Encode new string instead
  18.     decoded = l.decode('shift-jis')
  19.     if len(decoded) < 1:
  20.       raise None
  21.     try:
  22.       encoded = t[c].decode('utf-8').encode('shift-jis')
  23.       offset += len(encoded)-len(l)
  24.       r.append(encoded)
  25.     except:
  26.       print("Could not encode item %d (%s) as shift-jis" % (c, t[c]))
  27.     c += 1
  28.   except:
  29.     r.append(l)
  30.  
  31. print('Offset: %d' % offset)
  32.  
  33. # Add the offset to known data pointers that point to data after strings
  34. # 0x30 up to (and excluding) 0x64 are 32-bit integers pointing to
  35. # locations after the strings.
  36. # 0x271 is a 32-bit integer containing the length of the strings.
  37. b = list(b'\0'.join(r))
  38. for i in list(range(0x30, 0x64, 4)) + [0x271]:
  39.   v = b[i] | b[i+1]<<8 | b[i+2]<<16 | b[i+3]<<24
  40.   v += offset
  41.   b[i] = v & 0xff
  42.   b[i+1] = (v & 0xff00) >> 8
  43.   b[i+2] = (v & 0xff0000) >> 16
  44.   b[i+3] = (v & 0xff000000) >> 24
  45.  
  46. r = bytes(b)
  47.  
  48. f = open('feria_injected.lsr', 'wb')
  49. f.write(r)
  50. f.close()
Advertisement
Add Comment
Please, Sign In to add comment