Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- f = open('feria.lsr', 'rb')
- x = f.read()
- f.close()
- # Position of the strings data segment
- pos = x[0x2c] | x[0x2d]<<8 | x[0x2e]<<16 | x[0x2f]<<24
- # Length of the original segment
- l = x[pos] | x[pos+1]<<8 | x[pos+2]<<16 | x[pos+3]<<24
- # Number of 0-separated strings that should follow this number
- num = x[pos+4] | x[pos+5]<<8 | x[pos+6]<<16 | x[pos+7]<<24
- f = open('feria.txt', 'rb')
- # Strip for newlines being added by text editors
- y = f.read().strip()
- f.close()
- s = y.split(b'\r\n|\r\n')
- if len(s) != num:
- print("Warning: expected %d strings, got %d!" %(num, len(s)))
- # Re-encode all strings
- for i in range(len(s)):
- j = s[i]
- try:
- j = j.decode('utf-8')
- except:
- print("Could not read item %d ('%s') as UTF-8" % (i, s[i]))
- exit()
- try:
- j = j.encode('shift-jis')
- except:
- print("Could not encode item %d ('%s') as Shift-JIS" % (i, s[i]))
- exit()
- s[i] = j + b'\0'
- s = b''.join(s)
- newLen = len(s)
- offset = newLen - l
- # Add the offset to known data pointers that point to data after strings
- # 0x30 up to (and excluding) 0x64 are 32-bit integers pointing to
- # locations after the strings.
- b = list(x[:pos+8])
- for i in range(0x30, 0x64, 4):
- v = b[i] | b[i+1]<<8 | b[i+2]<<16 | b[i+3]<<24
- v += offset
- b[i] = v & 0xff
- b[i+1] = (v & 0xff00) >> 8
- b[i+2] = (v & 0xff0000) >> 16
- b[i+3] = (v & 0xff000000) >> 24
- b[pos] = newLen & 0xff
- b[pos+1] = (newLen & 0xff00) >> 8
- b[pos+2] = (newLen & 0xff0000) >> 16
- b[pos+3] = (newLen & 0xff000000) >> 24
- header = bytes(b)
- footer = x[pos+l+8:]
- f = open('feria_injected.lsr', 'wb')
- f.write(header + s + footer)
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement