Created
November 13, 2020 01:31
-
-
Save MonoidMusician/9a51d6ddadeb747431978716f5921d37 to your computer and use it in GitHub Desktop.
Tool to recover RIFF formats (e.g. .wav files) from raw binary dd files. Requires gdd on mac from brew coreutils.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import os | |
import mmap | |
filename = sys.argv[1] | |
tag = b"RIFF" | |
def doit(f, s): | |
#sz = os.stat(f.fileno()).st_size | |
c = 0 | |
p = 0 | |
while True: | |
p = f.find(s) | |
yield p | |
f.seek(p+len(s)) | |
sd = open(filename, 'rb') | |
sz = os.stat(sd.fileno()).st_size | |
sd.seek(0) | |
mm = mmap.mmap(sd.fileno(), 0, access=mmap.ACCESS_READ) | |
mm.seek(sd.tell()) | |
print("#!/usr/bin/env bash") | |
print(""" | |
if command -v gdd &> /dev/null | |
then | |
DD=gdd | |
fi | |
set -e | |
set -m | |
trap 'killall ${DD-dd}' EXIT | |
set -v | |
""") | |
for i,p in enumerate(doit(mm, tag)): | |
with open(sd.fileno(), 'rb', closefd=False) as line: | |
try: | |
line.seek(p) | |
#print(line.peek(200).replace(b'\x00', b'')) | |
assert(line.read(4) == b"RIFF") | |
field = line.read(4) | |
sz = 0 | |
adj = 1 | |
for n in field: | |
sz += adj*n | |
adj *= 0x100 | |
sz += 8 | |
print("${{DD-dd}} bs=1M iflag=skip_bytes,count_bytes skip={0} count={1} if={filename} of=recovery/{2}.wav &".format(p, sz, i, filename=filename)) | |
except OSError as e: | |
#print(e) | |
break | |
print("wait && trap - EXIT") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment