/
video_restore.rb
executable file
·279 lines (227 loc) · 7.03 KB
/
video_restore.rb
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
require 'optparse'
require 'bcrypt'
class OptparseExample
Version = '0.0.1'.freeze
attr_reader :parser, :options
class ScriptOptions
attr_accessor :input, :output, :start, :extention, :part_size, :threads
def initialize
self.input = nil
self.output = './output/'
self.start = "\x00\x00\x00 ftypavc1"
self.extention = 'mp4'
self.part_size = 50 # bites
self.threads = 1
end
end
def initialize
@options = ScriptOptions.new
end
def self.parse(args)
my_parser = new
my_parser.option_parser.parse!(args)
my_parser.check_options
my_parser.options
end
def option_parser
@parser ||= OptionParser.new do |parser|
parser.banner = 'Usage: example.rb [options]'
parser.separator ''
parser.separator 'Specific options:'
parser.on('-i', '--input [FILE]', String,
'Input file (REQUIRED)',
'Disc image by DD') do |e|
options.input = e
end
parser.separator ''
parser.on('-o', '--output [DIRECTORY]', String,
'Output directory',
'Directory to store finded files',
'Default: "./output/"') do |e|
options.output = e || options.output
end
parser.separator ''
parser.on('-s', '--start [HEX CHARACTERS]', String,
'Characters from which files starts',
'Default: "0x000000206674797061766331"') do |e|
options.start = e.hex || options.start
end
parser.on('-p', '--part_size [INTEGER]', Integer,
'Part size in bites',
'Default: 50') do |e|
options.part_size = e || options.part_size
end
parser.on('-t', '--threads [INTEGER]', Integer,
'Count threads',
'Default: 1') do |e|
options.threads = e || options.threads
end
parser.separator ''
parser.separator 'Common options:'
parser.on_tail('-h', '--help', 'Show this message') do
p parser
exit
end
end
end
def check_options
fail 'Missing input file' if options.input.nil?
end
end
class Buffer
FILE_NAME = 'export_%05i'.freeze
attr_reader :path, :file, :length
attr_writer :start
@@index = -1
@@thread = 0
def initialize(options, thread=nil)
@@thread = thread || @@thread
@path = "#{options.output}#{@@thread}_#{FILE_NAME % get_index}.#{options.extention}"
fd = IO.sysopen(@path, 'wb')
@file = IO.new(fd, 'wb')
@empty = true
@length = 0
end
def get_index
@@index += 1
end
def add(data)
@file.write(data)
@empty = false
@length += data.length
end
def close
unless empty?
@file.close
else
File.delete(@path)
end
end
def empty?
@empty
end
def started?
!@start.nil?
end
end
def waste
10.times { BCrypt::Password.create('secret') }
end
class Restore
def self.start
new.main
end
def main
begin
options = OptparseExample.parse(ARGV)
rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
p e
rescue Exception => e
p e
exit
end
unless File.exist? options.input
exit
end
p 'Start process...'
process_file(options)
p 'End!'
end
def getTail(options, part)
start_length = options.start.length
len = part.length
now = len - start_length
itStartPart = -> (_part) { _part == options.start[0..._part.length] }
tail = ''
while now < len
poz = part.index(options.start[0], now)
unless poz.nil?
founded_part = part[poz, part.length - poz]
if itStartPart[founded_part]
tail = founded_part
break
end
end
now += 1
end
tail
end
def chomp_tail(part, tail)
part = part[0, part.length - tail.length]
end
def process_part(options, part, buf, tail, after_limit = false)
last = -1
part = tail + part
tail = ''
len = part.length
max = -> (a, b) { a >= b ? a : b }
get_count = -> (a, b) { b - a }
while last < len
poz = part.index(options.start, last + 1)
if poz.nil?
tail = getTail(options, part)
part = chomp_tail(part, tail)
if buf.started?
last = max[last, 0]
count = get_count[last, len + 1]
added_part = part[last, count]
buf.add(added_part)
end
break
else
if buf.started?
last = max[last, 0]
count = get_count[last, poz]
if count > 0
tmp = part[last, count]
buf.add(tmp)
end
buf.close
last += count
buf = Buffer.new(options)
buf.start = poz
else
buf.start = poz
last = poz
end
return [buf, true, tail] if after_limit
end
end
[buf, false, tail]
end
def read_part(options, offset)
IO.binread(options.input, options.part_size, offset)
end
def process_file(options)
p "Threads: #{options.threads}"
max_offset = File.size(options.input)
part_size = (max_offset / options.threads).ceil
last = -1
threads = []
options.threads.times do |i|
threads << Thread.new { process_thread(options, i, last += 1, [last += (part_size - 1), max_offset].min) }
end
threads.each { |thr| thr.join }
p Process.waitall
end
def process_thread(options, thread=0, start_offset=0, end_offset=nil)
p "","Start process. thread: #{thread} start_offset: #{start_offset} end_offset: #{end_offset}"
offset = start_offset
end_offset ||= File.size(options.input)
buf = Buffer.new(options, thread)
tail = ''
loop do
# a = Time.now
part = read_part(options, offset)
break if part.nil?
buf, need_break, tail = process_part(options, part, buf, tail, offset >= end_offset)
break if need_break
offset += options.part_size
# p Time.now - a
waste
# p Time.now - a
end
buf.close
end
end
Restore.start if __FILE__ == $PROGRAM_NAME