-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunvivtool.c
417 lines (387 loc) · 13.4 KB
/
unvivtool.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
unvivtool.c - VIV/BIG decoder/encoder CLI
unvivtool Copyright (C) 2020-2024 Benjamin Futasz <https://github.com/bfut>
Portions copyright, see each source file for more information.
You may not redistribute this program without its source code.
README.md may not be removed or altered from any unvivtool redistribution.
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 <https://www.gnu.org/licenses/>.
*/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h> /* GetModuleFileNameA */
#endif
#define UVTUTF8
#define SCL_DEBUG 0
#include "./libnfsviv.h"
static
void Usage(void)
{
printf("Usage: unvivtool d [<options>...] <path/to/input.viv> [<path/to/output_directory>]\n"
" unvivtool e [<options>...] <path/to/output.viv> <paths/to/input_files>...\n"
" unvivtool <path/to/input.viv>\n"
" unvivtool <paths/to/input_files>...\n"
"\n");
printf("Commands:\n"
" d Decode and extract files from VIV/BIG archive\n"
" e Encode files in new VIV/BIG archive\n"
"\n");
printf("Options:\n"
" -aot decoder Overwrite mode: auto rename existing file\n"
" -dnl<N> decode/encode, set fixed Directory eNtry Length (<N> >= 10)\n"
" -i<N> decode file at 1-based Index <N>\n"
" -f<name> decode File <name> (cAse-sEnsitivE) from archive, overrides -i\n"
" -x decode/encode to/from filenames in base16/heXadecimal\n"
" -fmt<format> encode to Format 'BIGF' (default), 'BIGH' or 'BIG4' (w/o quotes)\n"
" -p Print archive contents, do not write to disk (dry run)\n");
printf(" -we Write re-Encode command to path/to/input.viv.txt (keep files in order)\n"
" -v print archive contents, Verbose\n");
fflush(stdout);
}
#ifdef _WIN32
/* Should be safe enough with sz >= 256 * 4 */
void UVT_GetExePath(char *buf, const size_t sz)
{
if (GetModuleFileName(NULL, buf, sz) > 1)
LIBNFSVIV_BkwdToFwdSlash(buf);
}
#else
/* Assumes sizeof(buf) >= 4096 */
void UVT_GetExePath(char *buf)
{
char *ptr = realpath("/proc/self/exe", buf);
if (!ptr) buf[0] = '\0';
}
#endif
void UVT_CreateWENCFile(int *retv, const int argc, char **argv, const char *viv_name)
{
char buf_[LIBNFSVIV_FilenameMaxLen];
FILE *file_ = NULL;
int i;
memcpy(buf_, viv_name, LIBNFSVIV_min(strlen(viv_name) + 1, sizeof(buf_)));
if (!LIBNFSVIV_AppendFileEnding(buf_, sizeof(buf_), LIBNFSVIV_WENCFileEnding))
{
fprintf(stderr, "Cannot use option '-we'\n");
*retv = -1;
return;
}
file_ = fopen(buf_, "w");
if (!file_)
{
fprintf(stderr, "Cannot create '%s' (option -we)\n", buf_);
*retv = -1;
return;
}
printf("Writing re-Encoding command to '%s' (option -we)\n", buf_);
buf_[0] = '\0';
#ifdef _WIN32
UVT_GetExePath(buf_, sizeof(buf_));
#else
UVT_GetExePath(buf_);
#endif
if (buf_[0])
{
fprintf(file_, "%s ", buf_);
fprintf(file_, "e ");
for (i = 2; i < argc; ++i)
{
const size_t sz = strlen(argv[i]);
if (argv[i][0] == '-'
&& strcmp(argv[i], "-we")
&& (sz > 2 && (strncmp(argv[i], "-i", 2) || strncmp(argv[i], "-f", 2)))
&& (sz > 4 && (strncmp(argv[i], "-fmt", 4))))
fprintf(file_, "%s ", argv[i]);
}
fflush(file_);
}
else *retv = -1;
fclose(file_);
}
void UVT_RemoveWENCFile(const char *viv_name)
{
char buf_[LIBNFSVIV_FilenameMaxLen];
memcpy(buf_, viv_name, LIBNFSVIV_min(strlen(viv_name) + 1, sizeof(buf_)));
if (!LIBNFSVIV_AppendFileEnding(buf_, sizeof(buf_), LIBNFSVIV_WENCFileEnding))
{
fprintf(stderr, "Cannot remove re-Encoding file\n");
return;
}
remove(buf_);
}
int main(int argc, char **argv)
{
int retv = 0;
char viv_name[LIBNFSVIV_FilenameMaxLen];
char *out_dir = NULL;
char **infiles_paths = NULL;
size_t infiles_paths_sz = 0;
int count_infiles = 0;
char *request_file_name = NULL;
int request_file_idx = 0;
int opt_direnlenfixed = 0;
int opt_filenameshex = 0;
int opt_faithfulencode = 0;
char opt_requestfmt[5] = "BIGF"; /* Viv() only */
int opt_requestendian = 0xe;
int opt_dryrun = 0;
int opt_wenccommand = 0;
int opt_printlvl = 0;
int opt_overwrite = 0;
int i;
SCL_assert(sizeof(VivDirEntr) == 16);
SCL_assert(sizeof(VivDirectory) == 64);
#if defined(_WIN64) || defined(__LP64__) || defined(_M_X64) || defined(__x86_64__)
SCL_assert(sizeof(LIBNFSVIV_CircBuf) == 24);
#else
SCL_assert(sizeof(LIBNFSVIV_CircBuf) == 16);
#endif
printf("unvivtool " UVTVERS " - " UVTCOPYRIGHT " "
#ifdef __cplusplus
"|C++"
#endif
#ifndef UVTUTF8
"|no-UTF8"
#endif
#if SCL_DEBUG > 0
"|debug"
#endif
"\n\n");
if (argc < 2)
{
Usage();
return 0;
}
viv_name[0] = '\0';
/** Drag-and-drop mode
always: argv[1] used to copy/derive viv_name
decode if argv[1] has viv/big bytes
needs viv_name
malloc's and gets out_dir as parent_dir of viv_name
else encode **argv to argv[1].viv
needs infiles_paths
gets viv_name from argv[1] with appended extension ".viv"
no options
*/
if (strlen(argv[1]) > 1)
{
memcpy(viv_name, argv[1], LIBNFSVIV_min(strlen(argv[1]) + 1, sizeof(viv_name) - 6)); /* leave 5 bytes for ".viv" */
viv_name[sizeof(viv_name) - 6] = '\0';
if (/* LIBNFSVIV_IsFile(viv_name) && */ LIBNFSVIV_GetVivVersion_FromPath(viv_name) > 0) /* decoder */
{
out_dir = (char *)calloc(LIBNFSVIV_FilenameMaxLen * sizeof(*out_dir), 1);
if (!out_dir)
{
fprintf(stderr, "unvivtool: Memory allocation failed.\n");
retv = -1;
}
else
{
memcpy(out_dir, argv[1], LIBNFSVIV_min(strlen(argv[1]), LIBNFSVIV_FilenameMaxLen - 1));
LIBNFSVIV_GetParentDir(out_dir);
}
}
else if (LIBNFSVIV_IsFile(viv_name) && !LIBNFSVIV_IsDir(viv_name)) /* encoder */
{
if (!LIBNFSVIV_AppendFileEnding(viv_name, sizeof(viv_name), ".viv"))
{
fprintf(stderr, "Unviv: Cannot append extension '.viv' to '%s'\n", viv_name);
retv = -1;
}
infiles_paths_sz = 0; /* not malloc'd */
infiles_paths = &argv[1];
count_infiles = argc - 1;
}
else
{
fprintf(stderr, "unvivtool: Invalid file or directory: '%s' (== '%s')\n", argv[1], viv_name);
Usage();
retv = -1;
}
}
/** Command mode
strlen(argv[1]) == 1
argv[1] == 'd' || 'e'
argv[i>=2] == viv_name
argv[i>=3] == out_dir or infiles_paths
and options
decode:
needs viv_name
gets out_dir from args or as parent dir of viv_name
opt_weenccommand and no dry-run: create file
*/
else if (argc >= 3 && (argv[1][0] == 'd' || argv[1][0] == 'e'))
{
for (i = 2; i < argc; ++i)
{
if (argv[i][0] != '-')
{
memcpy(viv_name, argv[i], LIBNFSVIV_min(strlen(argv[i]) + 1, sizeof(viv_name)));
viv_name[sizeof(viv_name) - 1] = '\0';
break;
}
}
if (viv_name[0] == '\0') { Usage(); retv = -1; }
/* Decode: get output directory */
if (retv == 0 && argv[1][0] == 'd')
{
out_dir = (char *)malloc(LIBNFSVIV_FilenameMaxLen * sizeof(*out_dir));
if (!out_dir) { fprintf(stderr, "unvivtool: Memory allocation failed.\n"); retv = -1; }
else
{
out_dir[0] = '\0';
for (i = 2; i < argc; ++i) /* out_dir from args */
{
if (argv[i][0] != '-' && strcmp(argv[i], viv_name))
{
memcpy(out_dir, argv[i], LIBNFSVIV_min(strlen(argv[i]) + 1, LIBNFSVIV_FilenameMaxLen));
break;
}
}
if (out_dir[0] == '\0') /*out_dir as parent dir of viv_name */
{
memcpy(out_dir, viv_name, strlen(viv_name) + 1);
LIBNFSVIV_GetParentDir(out_dir);
}
}
} /* if 'd' */
/* Encode: get input files paths */
else if (retv == 0 && argv[1][0] == 'e')
{
infiles_paths = (char **)calloc((argc - 3) * sizeof(*infiles_paths), 1);
infiles_paths_sz = (argc - 3) * sizeof(*infiles_paths);
if (!infiles_paths) { fprintf(stderr, "unvivtool: Memory allocation failed.\n"); retv = -1; }
else
{
count_infiles = 0;
for (i = 3; i < argc; ++i)
{
if (argv[i][0] != '-' && strcmp(argv[i], viv_name))
{
infiles_paths[count_infiles] = argv[i];
++count_infiles;
}
}
}
} /* if 'e' */
/** Get options
*/
for (i = 2; i < argc && retv == 0; ++i)
{
if (argv[i][0] == '-')
{
const int sz = (int)strlen(argv[i]);
if (sz > 2) /* only consider sufficiently long candidates */
{
const char *ptr = argv[i];
if (sz > 4 && !strncmp(argv[i], "-dnl", 4)) /* fixed directory length (clamped) */
{
ptr += 4;
opt_direnlenfixed = (int)strtol(ptr, NULL, 10);
if (opt_direnlenfixed != 0)
opt_direnlenfixed = LIBNFSVIV_Clamp_opt_direnlenfixed(opt_direnlenfixed, 1);
}
else if (/* sz > 2 && */ !request_file_name && !strncmp(argv[i], "-i", 2)) /* decode: request filename (overrides file idx request) */
{
ptr += 2;
request_file_idx = LIBNFSVIV_clamp(strtol(ptr, NULL, 10), 0, INT_MAX / 100); /* 1-based index, 0 means no file requested */
if (request_file_idx > 0) printf("Requested file at index: %d\n", request_file_idx);
}
else if (/* sz > 2 && */ argv[1][0] == 'd' && !strncmp(argv[i], "-f", 2)) /* decode: request filename (overrides file idx request) */
{
ptr += 2;
if (strlen(ptr) + 1 > LIBNFSVIV_FilenameMaxLen / 2)
{
fprintf(stderr, "Requested filename too long (max %d): len %d\n", LIBNFSVIV_FilenameMaxLen / 2, (int)strlen(ptr) + 1);
retv = -1;
break;
}
request_file_name = (char *)malloc((strlen(ptr) + 1) * sizeof(*request_file_name));
if (!request_file_name) { retv = -1; fprintf(stderr, "unvivtool: Memory allocation failed.\n"); break; }
memcpy(request_file_name, ptr, strlen(ptr) + 1);
printf("Requested file: %s\n", request_file_name);
if (request_file_idx > 0) printf("Overriding requested file index: %d\n", request_file_idx);
request_file_idx = 0; /* override option -i */
}
else if (sz >= 4 && argv[1][0] == 'e' && !strncmp(argv[i], "-fmt", 4)) /* encode: request encoding format */
{
ptr += 4;
memcpy(opt_requestfmt, ptr, LIBNFSVIV_min(strlen(ptr) + 1, 5));
if (strlen(ptr) + 1 != 5
&& (strncmp(opt_requestfmt, "BIGF", 5)
&& strncmp(opt_requestfmt, "BIGH", 5)
&& strncmp(opt_requestfmt, "BIG4", 5)))
{
Usage();
retv = -1;
break;
}
printf("Requested format: %.4s\n", opt_requestfmt);
}
} /* (sz > 2) */
if (!strcmp(argv[i], "-aot")) { opt_overwrite = 1; }
else if (!strcmp(argv[i], "-x")) { opt_filenameshex = 1; }
else if (!strcmp(argv[i], "-p")) { opt_dryrun = 1; opt_printlvl = 1; }
else if (!strcmp(argv[i], "-v")) { opt_printlvl = 1; }
else if (!strcmp(argv[i], "-we")) { opt_wenccommand = 1; }
else continue;
} /* if (argv[i][0] == '-') */
} /* Get options: for i */
} /* command mode */
if (retv == 0 && argv[1][0] == 'd' && opt_wenccommand && !opt_dryrun)
UVT_CreateWENCFile(&retv, argc, argv, viv_name);
/** Decoder
*/
if (retv == 0 && out_dir)
{
if (!LIBNFSVIV_Unviv(viv_name, out_dir,
request_file_idx, request_file_name,
opt_dryrun, opt_printlvl, opt_direnlenfixed,
LIBNFSVIV_Fix_opt_filenameshex(opt_filenameshex, opt_direnlenfixed),
opt_wenccommand, opt_overwrite))
{
printf("Decoder failed.\n");
retv = -1;
if (opt_wenccommand && !opt_dryrun)
UVT_RemoveWENCFile(viv_name); /* cleanup */
}
else
printf("Decoder successful.\n");
}
/** Encoder
*/
else if (retv == 0 && infiles_paths)
{
if (!LIBNFSVIV_Viv(viv_name,
infiles_paths, count_infiles,
opt_dryrun, opt_printlvl, opt_direnlenfixed,
LIBNFSVIV_Fix_opt_filenameshex(opt_filenameshex, opt_direnlenfixed),
opt_requestfmt, opt_requestendian, opt_faithfulencode))
{
printf("Encoder failed.\n");
retv = -1;
}
else
printf("Encoder successful.\n");
}
/* Print usage */
else if (retv == 0)
{
Usage();
retv = -1;
}
if (out_dir) free(out_dir);
if (infiles_paths_sz > 0) free(infiles_paths);
if (request_file_name) free(request_file_name);
return retv;
}