I'm trying to make a file carver for .jpeg images in Python but unfortunately i'm finding it a lot harder than i thought it would be.
As far as i can tell, the problem i'm facing is caused by multiple SOI and EOI markers in an image for the thumbnails.
I need a way to separate the thumbnails and actual images but considering the EOI is just FFD9, i'm finding that quite hard.
My code:
with open(r'\\.\X:', 'rb') as f:
startfile = 0
size = 0
start = '\xFF\xD8\xFF\xE0'
end = '\xFF\xD9'
chunksize = 512
chunk = f.read(chunksize)
while chunk:
s = chunk.find(start)
e = chunk.find(end)
if s >= 0: startfile = f.tell() - chunksize + s
if e >= 0: size = f.tell() - chunksize + e + 2
if startfile and size:
eof = size-startfile
images.append((startfile, eof))
startfile = size = 0
chunk = f.read(chunksize)
for pos, item in enumerate(images):
with open(str(pos)+'.jpg', 'wb') as o:
f.seek(item[0])
o.write(f.read(item[1])