This repository has been archived by the owner on Jan 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
fResync.cc
334 lines (277 loc) · 6.2 KB
/
fResync.cc
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* fResync - MPEG frame-resync utility
* Copyright(c) 2004 of wave++ (Yuri D'Elia) <[email protected]>
* Distributed under GNU LGPL without ANY warranty.
*/
// local headers
#include "fIcy.hh"
#include "msg.hh"
#include "mpeg.hh"
#include "copy.hh"
// system headers
#include <fstream>
using std::ifstream;
using std::ofstream;
#include <iostream>
using std::cout;
using std::cerr;
#include <memory>
using std::auto_ptr;
// c system headers
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
/*
* Parameters handling
*/
struct params_t
{
const char* file;
size_t framelen;
size_t maxframes;
bool buffered;
bool simulate;
bool help;
};
bool
parse_params(params_t& buf, int argc, char* const argv[])
{
// real values
int arg;
while((arg = getopt(argc, argv, "bn:m:svh")) != -1)
switch(arg)
{
case 'b':
buf.buffered = true;
break;
case 'n':
buf.maxframes = strtoul(optarg, NULL, 0);
break;
case 'm':
buf.framelen = strtoul(optarg, NULL, 0);
break;
case 's':
buf.simulate = true;
break;
case 'h':
buf.help = true;
break;
case 'v':
verbose = true;
break;
}
argc -= optind;
buf.file = argv[optind];
return (!buf.file);
}
void
show_help()
{
cout << prg << fIcy::fResyncHelp << prg << " v" << fIcy::version <<
" is\n" << fIcy::copyright;
}
bool
init(params_t& buf, int argc, char* argv[])
{
// defaults
prg = argv[0];
buf.buffered = false;
buf.framelen = fIcy::frameLen;
buf.maxframes = fIcy::maxFrames;
verbose = buf.simulate = buf.help = false;
// arguments
bool ret(parse_params(buf, argc, argv));
return (ret && !buf.help);
}
/*
* File truncation utilities
*/
struct region_t
{
size_t start;
size_t size;
};
bool
truncate(const char* file, const region_t& reg)
{
// check whether truncate can be used
if(reg.start == 0)
return truncate(file, reg.size);
// re-open the input file and output file
ifstream in(file);
if(!in || unlink(file))
return true;
ofstream out(file);
if(!out)
return true;
// skip garbage and copy the stream
in.seekg(reg.start);
return copy(out, in, reg.size);
}
/*
* Implementation
*/
bool
search_sync(ifstream& fd, const params_t& params, region_t& reg)
{
// create the buffer as the double of the maximal space possibly needed
size_t mlen(params.framelen * params.maxframes * 2);
auto_ptr<char> buf(new char[mlen]);
// beginning
fd.seekg(0);
fd.read(buf.get(), mlen);
if(!fd)
return true;
reg.start = mpeg::sync_forward(buf.get(), mlen, params.maxframes);
if(reg.start == mlen)
return true;
// end
fd.seekg(-static_cast<ifstream::off_type>(mlen), std::ios_base::end);
size_t pos(fd.tellg());
fd.read(buf.get(), mlen);
if(!fd)
return true;
size_t end(mpeg::sync_reverse(buf.get(), mlen, params.maxframes));
if(!end || (pos + end) <= reg.start)
return true;
reg.size = (pos + end) - reg.start;
return false;
}
bool
search_sync(const char* buf, size_t len, const params_t& params, region_t& reg)
{
// create the buffer as the double of the maximal space possibly needed
size_t mlen(params.framelen * params.maxframes * 2);
if(len < mlen)
mlen = len;
// beginning
reg.start = mpeg::sync_forward(buf, mlen, params.maxframes);
if(reg.start == mlen)
return true;
// end
size_t pos(len - mlen);
size_t end(mpeg::sync_reverse(buf + pos, mlen, params.maxframes));
if(!end || (pos + end) <= reg.start)
return true;
reg.size = (pos + end) - reg.start;
return false;
}
int
resync_buf(const params_t& params)
{
// open the file
ifstream fd(params.file, (params.simulate?
std::ios_base::in: (std::ios_base::in | std::ios_base::out)));
if(!fd)
{
err("cannot open %s for read/write", params.file);
return Exit::fail;
}
// search the offsets
region_t reg;
if(search_sync(fd, params, reg))
{
err("cannot resync %s, try increasing frame size", params.file);
return Exit::fail;
}
msg("sync found at %lu for %lu bytes", reg.start, reg.size);
// save file size
fd.seekg(0, std::ios_base::end);
size_t size(fd.tellg());
fd.close();
// sync the file
if(params.simulate)
{
// offsets only
cout << reg.start << " " << reg.size << std::endl;
}
else if(reg.start != 0 || reg.size != size)
{
// the file needs to be modified
if(truncate(params.file, reg))
{
err("cannot truncate %s", params.file);
return Exit::fail;
}
}
else
msg("no resync needed");
return Exit::success;
}
int
resync_mmap(const params_t& params)
{
// open the file
int fd = open(params.file, O_RDWR);
if(fd == -1)
{
err("cannot open %s for read/write", params.file);
return Exit::fail;
}
// file size
struct stat st;
if(fstat(fd, &st))
{
err("cannot stat %s", params.file);
return Exit::fail;
}
// mmap the file
char* addr = reinterpret_cast<char*>(mmap(NULL, st.st_size,
(PROT_READ | PROT_WRITE), MAP_SHARED, fd, 0));
if(!addr)
{
close(fd);
err("cannot mmap %s", params.file);
return Exit::fail;
}
// search the offsets
bool fail;
region_t reg;
if((fail = search_sync(addr, st.st_size, params, reg)))
err("cannot resync %s, try increasing frame size", params.file);
else
{
msg("sync found at %lu for %lu bytes", reg.start, reg.size);
if(reg.start != 0 || reg.size != static_cast<size_t>(st.st_size))
{
// non-zero offsets
if(reg.start != 0)
{
memmove(addr, addr + reg.start, reg.size);
reg.start = 0;
}
// final truncation
if((fail = ftruncate(fd, reg.size)))
err("cannot truncate %s", params.file);
}
else
msg("no resync needed");
}
// free resources
munmap(addr, st.st_size);
close(fd);
return (fail? Exit::fail: Exit::success);
}
int
main(int argc, char* argv[])
{
// initialize
params_t params;
if(init(params, argc, argv))
{
err("bad parameters; see %s -h", prg);
return Exit::args;
}
if(params.help)
{
show_help();
return Exit::args;
}
return (params.buffered || params.simulate?
resync_buf(params):
resync_mmap(params));
}