forked from hasse69/rar2fs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkr2i.c
282 lines (254 loc) · 8.72 KB
/
mkr2i.c
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
/*
Copyright (C) 2009 Hans Beckerus (hans.beckerus#AT#gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <platform.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/file.h>
#ifdef HAVE_LOCALE_H
# include <locale.h>
#endif
#include "index.h"
#define MAP_FAILED_ (0x1)
#define STR_DUP(s, path) \
int len = strlen(path);\
(s) = alloca(len + 1); \
strcpy((s), path);
typedef enum {
M_UNKNOWN,
M_RIFF,
M_EBML
} Mode;
/*!
*****************************************************************************
*
****************************************************************************/
static inline int count_c(char c, char *s)
{
int cnt = 0;
while(*s) {
if (*s++ == c)
++cnt;
}
return cnt;
}
/*!
*****************************************************************************
*
****************************************************************************/
static void parse_riff(struct idx_head *h, FILE *fp, off_t sz)
{
char s[256];
char *needle = NULL;
while (!needle && !feof(fp)) {
NO_UNUSED_RESULT fgets(s, sizeof(s), fp);
needle = strstr(s, "idx1");
}
char *tmp = alloca(strlen(needle));
char *tmp2 = tmp;
int cnt = 0;
while(*needle) {
if (*needle != 32 || !cnt) {
*tmp++ = *needle;
++cnt;
}
if (*needle != 32)
cnt = 0;
++needle;
}
*tmp = 0;
tmp = tmp2;
cnt = 0;
while (*tmp) {
if (*tmp++ == 32)
++cnt;
if (cnt == 2) {
needle = tmp;
tmp2 = tmp;
while(*needle) {
if (*needle != ',')
*tmp++ = *needle;
++needle;
}
*tmp = 0;
h->offset = atoll(tmp2)&~4095; /* align offset */
h->size = sz - h->offset;
return;
}
}
}
/*!
*****************************************************************************
*
****************************************************************************/
static void parse_ebml(struct idx_head *h, FILE *fp, off_t sz)
{
char s[256];
char *needle = NULL;
while (!needle && !feof(fp)) {
NO_UNUSED_RESULT fgets(s, sizeof(s), fp);
needle = strstr(s, "Cues");
}
if (needle) {
needle = strstr(needle, "pos.:");
if (needle) {
unsigned int i1;
unsigned int i2;
unsigned int i3;
unsigned int i4;
int n = count_c(',', needle);
switch(n) {
case 0:
i4 = 0;
i3 = 0;
i2 = 0;
sscanf(needle, "pos.: %3u)", &i1);
break;
case 1:
i4 = 0;
i3 = 0;
sscanf(needle, "pos.: %3u,%3u)", &i2,&i1);
break;
case 2:
i4 = 0;
sscanf(needle, "pos.: %3u,%3u,%3u)", &i3,&i2,&i1);
break;
case 3:
sscanf(needle, "pos.: %3u,%3u,%3u,%3u)", &i4,&i3,&i2,&i1);
break;
default:
return; /* not supported */
}
/* XXX add error checking + size check sanity */
h->offset = (i4 * 1000000000ULL) +
(i3 * 1000000) +
(i2 * 1000) + i1;
h->offset = h->offset & ~4095; /* align offset */
h->size = sz - h->offset;
}
}
}
/*!
*****************************************************************************
*
****************************************************************************/
static char *map_file(int fd, size_t size)
{
#ifdef HAVE_LOCKF
if (lockf(fd, F_TLOCK, 0) == -1)
return NULL;
#else
# ifdef HAVE_FLOCK
if (flock(fd, LOCK_EX|LOCK_NB) == -1)
return NULL;
# endif
#endif
/* Prepare the file */
NO_UNUSED_RESULT ftruncate(fd, size);
/* Map the file into address space */
char *addr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED)
return NULL;
return addr;
}
/*!
*****************************************************************************
*
****************************************************************************/
int main(int argn, char *argv[])
{
#ifdef HAVE_SETLOCALE
setlocale(LC_CTYPE, "");
#endif
if (argn != 3) {
printf("Usage: mkr2i <dump file> <source file>\n");
printf(" <dump file> AVI-Mux GUI RIFF/EBML dump file (txt)\n");
printf(" <source file> RIFF(.avi)/EBML(.mkv) source file\n");
exit(0);
}
char *dump = argv[1];
char *src_file = argv[2];
FILE *fd_dump = fopen(dump, "r");
if (!fd_dump) {
printf("Failed to open dump file %s\n", dump);
exit(-1);
}
FILE *fd_src = fopen(src_file, "r");
if (!fd_src) {
printf("Failed to open source file %s\n", src_file);
fclose(fd_dump);
exit(-1);
}
Mode mode;
char dump_magic[10];
NO_UNUSED_RESULT fscanf(fd_dump, "%s", dump_magic);
if (!strcmp(dump_magic, "EBML")) {
mode = M_EBML;
} else if (!strcmp(dump_magic+3, "RIFF")) {
mode = M_RIFF;
} else {
printf("Invalid dump file\n");
exit(-1);
}
/* get size of source file */
struct stat stat_src;
fstat(fileno(fd_src),&stat_src);
/* XXX error check */
struct idx_head head;
head.size = 0;
head.magic = R2I_MAGIC;
head.version = 0;
head.spare = 0;
switch (mode) {
case M_RIFF:
parse_riff(&head, fd_dump, stat_src.st_size);
break;
case M_EBML:
parse_ebml(&head, fd_dump, stat_src.st_size);
break;
default:
break;
}
if (head.size) {
char *dest;
STR_DUP(dest, src_file);
strcpy(&dest[strlen(dest)-4], ".r2i");
fseeko(fd_src, head.offset, SEEK_SET);
int fd = open(dest, O_RDWR|O_CREAT, S_IREAD|S_IWRITE);
if (fd == -1)
return 1;
size_t map_size = (head.size + sizeof(struct idx_head) + 4096) & ~4095;
char *addr = map_file(fd, map_size);
if (!addr) {
printf("Internal error %x\n", MAP_FAILED_);
return 1;
}
char *tmp = addr;
memcpy(tmp, &head, sizeof(struct idx_head));
tmp += sizeof(struct idx_head);
if ((fread(tmp, 1, head.size, fd_src) != head.size) && ferror(fd_src))
return 1;
/* flush to medium */
msync(addr, map_size, MS_SYNC);
munmap(addr, map_size);
close(fd);
}
fclose(fd_src);
fclose(fd_dump);
return 0;
}