Skip to content

Instantly share code, notes, and snippets.

@ztane
Created December 16, 2021 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ztane/4a398ece622066bee09ee01619b7f586 to your computer and use it in GitHub Desktop.
Save ztane/4a398ece622066bee09ee01619b7f586 to your computer and use it in GitHub Desktop.
Try to fix a 0cc/FTM file by writing copies with certain chunks missing
#!/usr/bin/python3
import sys
def read_block():
block_id = f.read(16)
version = f.read(4)
size = f.read(4)
contents = f.read(int.from_bytes(size, "little"))
return block_id, version, size, contents
def read_contents(write_func=lambda *x: None, ignore=-1):
header = f.read(18)
version = f.read(4)
write_func(header, version)
c = 1
while True:
try:
block_id, version, size, contents = read_block()
if block_id == b'':
return c
if c != ignore:
write_func(block_id, version, size, contents)
print(block_id)
else:
print('ignoring', block_id)
c += 1
except Exception as e:
print(e, repr(e))
with open(sys.argv[1], 'rb') as f:
total_blocks = read_contents()
for i in range(1, total_blocks + 1):
fn = f'{sys.argv[1]}.removed_{i}.0cc'
with open(fn, 'wb') as outf:
f.seek(0)
def write_func(*x):
for i in x:
outf.write(i)
total_blocks = read_contents(write_func, i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment