-
Notifications
You must be signed in to change notification settings - Fork 8
/
glide64_cache_extract.c
176 lines (151 loc) · 4.04 KB
/
glide64_cache_extract.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
// SPDX-License-Identifier: GPL-3.0-or-later
/* glide64_cache_extract, Glide64 TexCache Extraction tool for debugging
*
* SPDX-FileCopyrightText: Sven Eckelmann <[email protected]>
*/
/**
* Example usage:
* zcat MUPEN64PLUS.dat | ./glide64_cache_extract -vv -p MUPEN64PLUS > mupen64plus.tar
*/
#include "glide64_cache_extract.h"
#include <errno.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
struct _globals globals;
static int convert_input(void)
{
int ret;
uint32_t config;
ret = get_item(config);
if (ret < 0) {
fprintf(stderr, "Failed to read config header\n");
return ret;
}
ret = parse_config(config);
if (ret < 0) {
fprintf(stderr, "Failed to parse config header\n");
return ret;
}
while (!feof(globals.in)) {
ret = convert_file();
if (ret < 0)
return ret;
}
ret = write_tarblock(tarblock, sizeof(tarblock), 0);
if (ret < 0) {
fprintf(stderr, "Failed to write first EOF tar record\n");
return ret;
}
ret = write_tarblock(tarblock, sizeof(tarblock), 0);
if (ret < 0) {
fprintf(stderr, "Failed to write second EOF tar record\n");
return ret;
}
return 0;
}
static void usage(int argc, char *argv[])
{
const char *cmd = "glide64_cache_extract";
if (argc > 1)
cmd = argv[0];
printf("Usage: %s [options]\n\n", cmd);
printf("options:\n");
printf("\t -i,--input FILE Use FILE as uncompressed input file (default: stdin)\n");
printf("\t -o,--output FILE Use FILE as output file (default: stdout)\n");
printf("\t -p,--prefix NAME Add prefix to each file\n");
printf("\t -t,--type [hires|tex] Type of the input\n");
printf("\t -v,--verbose Print extra information on stderr (repeat for more verbosity)\n");
printf("\t -e,--ignore-error Skip current file when an conversion error is detected\n");
printf("\t -b,--bitmapv5 Use V5 Windows Bitmap files with ImageMagick compatible alpha channels\n");
printf("\t -h,--help Show this message and exit\n");
}
static int init(int argc, char *argv[])
{
int o;
int options_index;
static const struct option long_options[] = {
{"verbose", no_argument, NULL, 'v'},
{"prefix", required_argument, NULL, 'p'},
{"type", required_argument, NULL, 'p'},
{"help", no_argument, NULL, 'h'},
{"ignore-error", no_argument, NULL, 'e'},
{"bitmapv5", no_argument, NULL, 'b'},
{"input", required_argument, NULL, 'i'},
{"output", required_argument, NULL, 'o'},
{NULL, 0, NULL, 0 },
};
memset(&globals, 0, sizeof(globals));
memset(tarblock, 0, sizeof(tarblock));
globals.in = stdin;
globals.out = stdout;
while ((o = getopt_long(argc, argv, "vp:t:ebhi:o:", long_options, &options_index)) != -1) {
switch (o) {
case 'v':
globals.verbose++;
break;
case 'p':
globals.prefix = strdup(optarg);
if (!globals.prefix) {
fprintf(stderr, "Could not save prefix\n");
return -ENOMEM;
}
break;
case 'h':
usage(argc, argv);
exit(0);
break;
case 't':
if (strcasecmp(optarg, "hires") == 0) {
globals.type = INPUT_HIRES;
} else if (strcasecmp(optarg, "tex") == 0) {
globals.type = INPUT_TEX;
} else {
fprintf(stderr, "Invalid type %s\n", optarg);
return -EINVAL;
}
break;
case 'e':
globals.ignore_error = 1;
break;
case 'b':
globals.bitmapv5 = 1;
break;
case 'i':
if (globals.in != stdin)
fclose(globals.in);
globals.in = fopen(optarg, "rb");
if (!globals.in) {
fprintf(stderr, "Could not open input file %s\n", optarg);
return -ENOENT;
}
break;
case 'o':
if (globals.out != stdout)
fclose(globals.out);
globals.out = fopen(optarg, "wb");
if (!globals.out) {
fprintf(stderr, "Could not open output file %s\n", optarg);
return -ENOENT;
}
break;
default:
usage(argc, argv);
return -EINVAL;
}
}
return 0;
}
int main(int argc, char *argv[])
{
int ret;
ret = init(argc, argv);
if (ret < 0) {
usage(argc, argv);
return 1;
}
ret = convert_input();
if (ret < 0)
return 2;
return 0;
}