This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recoverRsrcData.py
90 lines (84 loc) · 2.78 KB
/
recoverRsrcData.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import zlib
import os
import logging
import SocketServer
import sys
import glob
BLOCK_SIZE = 1024
def findStart(file):
import zlib
f = open(file+'/..namedfork/rsrc','rb')
BLOCK_SIZE = 128000
d = f.read(BLOCK_SIZE)
cont = True
s = 0
while cont:
try:
decompressed = zlib.decompress(d[s:])
cont = False
except:
s = s+1
if s> BLOCK_SIZE:
s = -1
cont = False
return s
f.close()
def getRsrc(filename):
output = open(filename+'.rsrc','wb')
with open(filename+'/..namedfork/rsrc','rb') as input:
fsize = os.fstat(input.fileno()).st_size
while True:
to_go = fsize - input.tell()
block = input.read(to_go > BLOCK_SIZE and BLOCK_SIZE or to_go)
if len(block):
output.write(block)
def readFile(filename):
seek = findStart(filename)
if seek == -1:
print "%s does not have recoverable data in resource fork" % filename
return
with open(filename+'/..namedfork/rsrc','rb') as input:
fsize = os.fstat(input.fileno()).st_size-50
input.seek(seek)
while True:
to_go = fsize - input.tell()
block = input.read(to_go > BLOCK_SIZE and BLOCK_SIZE or to_go)
if len(block):
yield block
else:
break
def recoverFile(filename,output_file):
output = open(output_file,'wb')
decompressor = zlib.decompressobj()
unused = ''
for response in readFile(filename):
if not response:
break
to_decompress = decompressor.unconsumed_tail + unused + response
unused = ''
while to_decompress:
try:
decompressed = decompressor.decompress(to_decompress)
except:
print "%s couldn't be decompressed" % filename
return
if decompressed:
output.write(decompressed)
to_decompress = decompressor.unconsumed_tail
if decompressor.unused_data:
unused = decompressor.unused_data
remainder = decompressor.flush()
output.write(remainder)
decompressor = zlib.decompressobj()
else:
to_decompress = None
remainder = decompressor.flush()
if remainder:
output.write(remainder)
""" Recover data from resource fork """
""" usage: python rescueRsrcData.py filename """
""" Grabs resource fork of HFS, runs contents through zlib inflate """
if __name__ == '__main__':
requested_file = sys.argv[1]
output_file = os.path.expanduser('~')+'/Desktop/'+os.path.basename(requested_file)
recoverFile(requested_file,output_file)