hbc/pywii/pywii-tools/extract.py

60 lines
1.4 KiB
Python
Raw Permalink Normal View History

2021-03-17 15:53:31 +01:00
#!/usr/bin/env python3
2016-11-23 06:35:12 +01:00
import sys, os, os.path
import pywii as wii
wii.loadkeys()
def parseint(d):
if len(d) > 2 and d[0:2].lower()=='0x':
return int(d,16)
return int(d)
if len(sys.argv) < 4 or len(sys.argv) > 7:
2021-03-17 15:53:31 +01:00
print("Usage:")
print(" python %s <encrypted ISO> <partition number> <file to extract to> [Partition offset] [length]"%sys.argv[0])
2016-11-23 06:35:12 +01:00
sys.exit(1)
iso_name, partno, data_name = sys.argv[1:4]
partno = int(partno)
part_offset = 0
data_offset = 0
copy_length = None
if len(sys.argv) >= 5:
part_offset = parseint(sys.argv[4])
if len(sys.argv) == 6:
copy_length = parseint(sys.argv[5])
if copy_length is not None and copy_length < 0:
2021-03-17 15:53:31 +01:00
print("Error: negative copy length")
2016-11-23 06:35:12 +01:00
sys.exit(1)
disc = wii.WiiDisc(iso_name)
disc.showinfo()
part = wii.WiiCachedPartition(disc, partno, cachesize=32, debug=False, checkhash=False)
if part_offset >= part.data_bytes:
2021-03-17 15:53:31 +01:00
print("Error: Offset past end of partition")
2016-11-23 06:35:12 +01:00
sys.exit(1)
if copy_length is None:
copy_length = part.data_bytes - part_offset
if copy_length > (part.data_bytes - part_offset):
2021-03-17 15:53:31 +01:00
print("Error: Length too large")
2016-11-23 06:35:12 +01:00
sys.exit(1)
dataf = open(data_name, "wb")
left = copy_length
offset = part_offset
while left > 0:
blocklen = min(left, 4*1024*1024)
d = part.read(offset, blocklen)
if len(d) != blocklen:
2021-03-17 15:53:31 +01:00
print("Part EOF reached!")
2016-11-23 06:35:12 +01:00
sys.exit(1)
dataf.write(d)
offset += blocklen
left -= blocklen
dataf.close()