-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathsm64geo.c
220 lines (207 loc) · 4.9 KB
/
sm64geo.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "utils.h"
#define SM64GEO_VERSION "0.1"
typedef struct
{
char *in_filename;
char *out_filename;
unsigned int offset;
unsigned int length;
} arg_config;
static arg_config default_config =
{
NULL,
NULL,
0,
0
};
static void print_spaces(FILE *fp, int count)
{
int i;
for (i = 0; i < count; i++) {
fputc(' ', fp);
}
}
void print_geo(FILE *out, unsigned char *data, unsigned int offset, unsigned int length)
{
unsigned int a = offset;
int indent = 0;
int i;
while (a < offset + length) {
switch (data[a]) {
case 0x00: // end
case 0x01:
case 0x03:
case 0x04:
case 0x05:
case 0x09:
case 0x0B:
case 0x0C:
case 0x17:
case 0x20:
i = 4;
break;
case 0x02:
case 0x0D:
case 0x0E:
case 0x12:
case 0x14:
case 0x15:
case 0x16:
case 0x18:
case 0x19:
i = 8;
break;
case 0x08:
case 0x13:
case 0x1C:
i = 12;
break;
case 0x10:
i = 16;
break;
case 0x0F:
i = 20;
break;
case 0x0A:
i = 8;
if (data[a+1]) {
i += 4;
}
break;
case 0x11:
case 0x1D:
i = 8;
if (data[a+1] & 0x80) {
i += 4;
}
break;
default:
i = 4;
ERROR("WHY? %06X %2X\n", a, data[a]);
}
if (data[a] == 0x05 && indent > 1) {
indent -= 2;
}
if (data[a] == 0x01) {
indent = 0;
}
fprintf(out, "%4X: ", a);
print_spaces(out, indent);
fprintf(out, "[ ");
fprint_hex(out, &data[a], i);
fprintf(out, "]\n");
if (data[a] == 0x04) {
indent += 2;
}
a += i;
}
}
static void print_usage(void)
{
ERROR("Usage: sm64geo [-l LENGTH] [-o OFFSET] FILE\n"
"\n"
"sm64geo v" SM64GEO_VERSION ": Super Mario 64 geometry layout decoder\n"
"\n"
"Optional arguments:\n"
" -l LENGTH length of data to decode in bytes (default: length of file)\n"
" -o OFFSET starting offset in FILE (default: 0)\n"
"\n"
"File arguments:\n"
" FILE input file\n"
" [OUTPUT] output file (default: stdout)\n");
exit(1);
}
// parse command line arguments
static void parse_arguments(int argc, char *argv[], arg_config *config)
{
int i;
int file_count = 0;
if (argc < 2) {
print_usage();
exit(1);
}
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'l':
if (++i >= argc) {
print_usage();
}
config->length = strtoul(argv[i], NULL, 0);
break;
case 'o':
if (++i >= argc) {
print_usage();
}
config->offset = strtoul(argv[i], NULL, 0);
break;
default:
print_usage();
break;
}
} else {
switch (file_count) {
case 0:
config->in_filename = argv[i];
break;
case 1:
config->out_filename = argv[i];
break;
default: // too many
print_usage();
break;
}
file_count++;
}
}
if (file_count < 1) {
print_usage();
}
}
int main(int argc, char *argv[])
{
arg_config config;
FILE *fout;
unsigned char *data;
long size;
// get configuration from arguments
config = default_config;
parse_arguments(argc, argv, &config);
if (config.out_filename == NULL) {
fout = stdout;
} else {
fout = fopen(config.out_filename, "w");
if (fout == NULL) {
perror("Error opening output file");
return EXIT_FAILURE;
}
}
// operation
size = read_file(config.in_filename, &data);
if (size < 0) {
perror("Error opening input file");
return EXIT_FAILURE;
}
if (config.length == 0) {
config.length = size - config.offset;
}
if (config.offset >= (unsigned int)size) {
ERROR("Error: offset greater than file size (%X > %X)\n",
config.offset, (unsigned int)size);
return EXIT_FAILURE;
}
if (config.offset + config.length > (unsigned int)size) {
ERROR("Warning: length goes beyond file size (%X > %X), truncating\n",
config.offset + config.length, (unsigned int)size);
config.length = size - config.offset;
}
print_geo(fout, data, config.offset, config.length);
free(data);
if (fout != stdout) {
fclose(fout);
}
return EXIT_SUCCESS;
}