-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriffx.c
387 lines (350 loc) · 10.9 KB
/
riffx.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright (c) 2019 volpol, Urban Wallasch <irrwahn35@freenet.de>
*
* Licensed under the terms of the 0BSD ("Zero-clause BSD") license.
* See LICENSE file for details.
*
* Changes 2019 by irrwahn:
* - renamed wsp.c to riffx.c
* - replaced mkdir_p with mkdirp
* - refactoring, reformatting
* - some minor changes and optimizations
*
* Traverse input file(s) and dump anything that looks remotely like a
* RIFF data stream into separate output files, named using extracted
* labels, if applicable. Useful e.g. for extracting audio streams from
* game files like Borderlands2 *.pck.
*
* NOTE: The extracted raw RIFF streams most likely will need some form
* of post-processing to be useful. E.g. for the Audiokinetic Wwise
* RIFF/RIFX sound format you should:
*
* 1. Run each dumped file through the ww2ogg converter.
* [ See https://github.com/hcs64/ww2ogg ]
*
* 2. Fix up the resulting Ogg vorbis file with revorb.
* [ See https://github.com/jonboydell/revorb-nix ]
*
* Porting:
*
* - Replace open/mmap/creat/write with functions from stdio (fopen,
* fread, fwrite) while taking extra care of splitting on buffer
* boundaries.
*
* - Port mkdirp.
*
*/
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#define LOG(...) fprintf(stderr, __VA_ARGS__)
/* mkdirp
* Create a directory and missing parents.
*/
static inline int mkdirp(const char *pathname, mode_t mode) {
if (!pathname || !*pathname) {
errno = ENOENT;
return -1;
}
int err = 0;
char *p;
char path[strlen(pathname) + 1];
struct stat sb;
if (stat(pathname, &sb) == 0 && S_ISDIR(sb.st_mode))
return 0;
mode |= S_IRWXU;
strcpy(path, pathname);
p = path + 1;
do {
p = strchr(p, '/');
if (p)
*p = '\0';
if (stat(path, &sb) != 0 || !S_ISDIR(sb.st_mode))
err = mkdir(path, mode);
if (p)
*p++ = '/';
} while (!err && p && *p);
return err;
}
/* mem_mem
* Locate needle of length nlen in haystack of length hlen.
* Returns a pointer to the first occurrence of needle in haystack, or
* haystack for a needle of zero length, or NULL if needle was not found
* in haystack.
*
* Uses the Boyer-Moore-Horspool search algorithm, see
* https://en.wikipedia.org/wiki/Boyer–Moore–Horspool_algorithm
*
* For our tiny needles and non-pathologic haystacks this borders on
* overkill, but meh.
*/
static inline void *mem_mem(const void *haystack, size_t hlen,
const void *needle, size_t nlen) {
size_t k, skip[256];
const uint8_t *hst = (const uint8_t *)haystack;
const uint8_t *ndl = (const uint8_t *)needle;
if (nlen == 0)
return (void *)haystack;
/* Set up the finite state machine we use. */
for (k = 0; k < 256; ++k)
skip[k] = nlen;
for (k = 0; k < nlen - 1; ++k)
skip[ndl[k]] = nlen - k - 1;
/* Do the search. */
for (k = nlen - 1; k < hlen; k += skip[hst[k]]) {
int i, j;
for (j = nlen - 1, i = k; j >= 0 && hst[i] == ndl[j]; j--)
i--;
if (j == -1)
return (void *)(hst + i + 1);
}
return NULL;
}
/*
* use_basename:
* 0: retain directory structure: a/b/foo.in -> output/a/b/foo/042.riff
* 1: create flat output directory: a/b/foo.in -> output/001_foo_042.riff
*
* use_label:
* 0: create output filename from stream index number only
* 1: scan for label in stream and use it for output filename
*
* guess_length:
* 0: read the stream length from the RIFF header size field
* 1: assume the stream ends at the beginning of the next (or EOF)
*/
static struct {
int use_basename;
int use_label;
int guess_length;
int verbose;
int endianess; /* No cmd line option for this, we figure it out. */
} cfg = {
0,
0,
0,
0,
0,
};
static inline void usage(const char *argv0) {
LOG("Usage: %s [-b] [-g] [-l] [-v] infile ... [outdir]\n"
" -b : create flat output directory\n"
" -g : ignore size fields, guess stream length (imprecise!)\n"
" -l : use extracted labels in filenames (unreliable!)\n"
" -v : be more verbose\n"
, argv0);
exit(EXIT_FAILURE);
}
static inline int config(int argc, char *argv[]) {
int opt;
while ((opt = getopt(argc, argv, "+:bglv")) != -1) {
switch (opt) {
case 'b':
cfg.use_basename = 1;
break;
case 'g':
cfg.guess_length = 1;
break;
case 'l':
cfg.use_label = 1;
break;
case 'v':
cfg.verbose = 1;
break;
default: /* '?' || ':' */
usage(argv[0]);
break;
}
}
return optind;
}
static inline uint32_t get_ui32(const void *p) {
const uint8_t *b = p;
if (!cfg.endianess) /* Little Endian byte order (RIFF) */
return b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24;
/* Big Endian byte order (RIFX) */
return b[3] | b[2] << 8 | b[1] << 16 | b[0] << 24;
}
/*
* Try to find a suitable "labl" chunk.
* We should really parse the RIFF structure. Instead, we are satisfied
* with the first null-terminated label string with length > 0.
*/
static inline const char *labl(const void *p, size_t len) {
static char lab[201] = "";
const uint8_t *b;
size_t l, ll;
*lab = '\0';
b = p;
l = len;
while (l > 8 && NULL != (b = mem_mem(b, l, "labl", 4))) {
b += 4; /* skip 'labl' */
l = len - (b - (uint8_t *)p);
ll = get_ui32(b);
if (ll > l - 4)
ll = l - 4;
/* The label we want? 200 is a magic number, 6 isn't (ID + 1 + '\0').
* We want it null terminated and start with a printable character! */
if (ll <= 200 && ll >= 6 && isprint(b[8]) && b[ll+4-1] == '\0') {
strcpy(lab, (const char *)(b + 8)); /* skip label size and ID */
break;
}
}
/* Sanitize label */
for (char *p = lab; *p; ++p) {
if (!isprint((unsigned char)*p) || strchr("/\\ ", *p))
*p = '_';
}
return lab;
}
/*
* Dump RIFF stream.
* Write a data blob of length len starting at b to a file whose name is
* constructed from prefix, an optional label, a numeric id and a suffix.
*/
static inline int dump(const char *prefix, size_t id, const void *b, size_t len) {
int fd;
const char *suffix[] = {"riff", "rifx"}; /* dump filename suffix */
char of[strlen(prefix) + 255];
const char *lab;
/* Construct file name from prefix and label or id: */
lab = cfg.use_label ? labl(b, len) : "";
snprintf(of, sizeof of, "%s%s%s%06zu.%s",
prefix, lab, *lab?"_":"", id, suffix[cfg.endianess]);
if (cfg.verbose)
LOG(": %8zu -> %s\n", len, of);
/* Caveat: This will overwrite any existing file with the same name! */
fd = creat(of, 0644);
if (0 > fd){
LOG("Failed to create %s: %s\n", of, strerror(errno));
return -1;
} else {
write(fd, b, len);
close(fd);
}
return 0;
}
/*
* Traverse file fd and dump anything that looks like a RIFF stream.
*/
int extract(int fd, const char *pfx) {
const char *RIF_[] = {"RIFF", "RIFX"};
size_t id, rsize;
off_t fsize, remsize;
const uint8_t *riff, *mfile;
fsize = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
mfile = mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, fd, 0);
if (mfile == MAP_FAILED) {
LOG("mmap failed: %s\n", strerror(errno));
return -1;
}
id = 0;
/* Where there's no RIFF, there might be a RIFX ... */
for (cfg.endianess = 0; cfg.endianess < 2; ++cfg.endianess )
if (NULL != (riff = mem_mem(mfile, fsize, RIF_[cfg.endianess], 4)))
break;
if (!riff) /* ... or nothing at all. */
return 0;
remsize = fsize - (riff - mfile);
while (remsize > 8 && NULL != riff) {
const uint8_t *next;
next = mem_mem(riff + 4, remsize - 4, RIF_[cfg.endianess], 4);
/* Read length info or guess stream length: */
if (cfg.guess_length) {
rsize = next ? next - riff : remsize;
}
else {
rsize = get_ui32(riff + 4) + 8; /* size + 'RIFF' + uint32 */
if ((off_t)rsize > remsize)
rsize = remsize;
}
/* Dump RIFF stream: */
LOG("%sEntry %5zu", cfg.verbose?"":"\r", id);
dump(pfx, id, riff, rsize);
/* Skip to next segment: */
++id;
riff = next;
remsize = riff ? fsize - (riff - mfile) : 0;
}
munmap((void *)mfile, fsize);
return id;
}
int main(int argc, char *argv[]) {
int i, total, argidx = 1;
const char *odir;
struct stat st;
argidx = config(argc, argv);
if (argc - argidx < 1)
usage(argv[0]);
/* If the last argument does not designate an existing file, we
* attempt to interpret it as the name of the output directory: */
if (0 != stat(argv[argc - 1], &st) || S_ISDIR(st.st_mode)) {
odir = argv[--argc];
if (argc - argidx < 1)
usage(argv[0]);
}
else
odir = "output";
LOG("Using \"%s\" as output directory\n", odir);
i = stat(odir, &st);
if (0 != i) {
LOG("Creating \"%s\"\n", odir);
mkdirp(odir, 0755);
i = stat(odir, &st);
}
if (0 != i || !S_ISDIR(st.st_mode)) {
LOG("%s is not a valid output directory\n", odir);
exit(EXIT_FAILURE);
}
/* Loop over remaining arguments as input files: */
for (total = 0, i = argidx; i < argc; i++) {
int fd, cnt, n;
char fpfx[PATH_MAX], tfn[PATH_MAX], *x;
fd = -1;
if (0 == stat(argv[i], &st)) {
if (S_ISREG(st.st_mode))
fd = open(argv[i], O_RDONLY);
else
errno = ENOTSUP;
}
if (fd < 0){
LOG("Skipping %s (failed to open: %s)\n", argv[i], strerror(errno));
continue;
}
LOG("Processing %s\n", argv[i]);
strcpy(tfn, argv[i]);
if ( NULL != (x = strrchr(tfn, '.')))
*x = 0;
if (cfg.use_basename) {
x = strrchr(tfn, '/');
x = x ? x + 1 : tfn;
n = snprintf(fpfx, sizeof fpfx, "%s/%03d_%s_", odir, i - argidx, x);
}
else {
n = snprintf(fpfx, sizeof fpfx, "%s/%s/", odir, tfn);
}
if ( (size_t)n >= sizeof fpfx ) {
LOG("output directory path truncated: '%s'\n", fpfx);
exit(EXIT_FAILURE);
}
if (!cfg.use_basename)
mkdirp(fpfx, 0755);
LOG("Dumping to %s...\n", fpfx);
cnt = extract(fd, fpfx);
close(fd);
LOG("%sDumped %d entries \n", cfg.verbose?"":"\r", cnt);
total += cnt;
}
LOG("\rDumped a total of %d entries.\n", total);
exit(EXIT_SUCCESS);
}