-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdat-extract.c
187 lines (155 loc) · 4.5 KB
/
dat-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
177
178
179
180
181
182
183
184
185
186
187
/*
written by Missingno_force a.k.a. Missingmew
Copyright (c) 2014-2018
see LICENSE for details
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#define FILENAMELENGTH 16
#define DIRNAMELENGTH 8
#define BLOCKSIZE 0x800
#ifdef _WIN32
#include <direct.h>
#define createDirectory(dirname) mkdir(dirname)
#else
#include <sys/stat.h>
#include <sys/types.h>
#define createDirectory(dirname) mkdir(dirname, 0777)
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#define FILENAMELENGTH 16
#define DIRNAMELENGTH 8
#define BLOCKSIZE 0x800
#ifdef _WIN32
#include <direct.h>
#define createDirectory(dirname) mkdir(dirname)
#else
#include <sys/stat.h>
#include <sys/types.h>
#define createDirectory(dirname) mkdir(dirname, 0777)
#endif
int writeFile( FILE *input, int length, FILE *output ) {
unsigned char dataBuffer[1024];
unsigned int bytesLeft = length;
while(bytesLeft) {
unsigned int wantedRead;
if(bytesLeft >= sizeof(dataBuffer))
wantedRead = sizeof(dataBuffer);
else
wantedRead = bytesLeft;
unsigned int haveRead = fread(dataBuffer, 1, wantedRead, input);
if(haveRead != wantedRead) {
printf("haveRead != wantedRead: %d != %d\n", haveRead, wantedRead);
perror("This broke");
return 0;
}
unsigned int haveWrite = fwrite(dataBuffer, 1, haveRead, output);
if(haveWrite != haveRead) {
printf("haveWrite != haveRead: %d != %d\n", haveWrite, haveRead);
return 0;
}
bytesLeft -= haveRead;
}
return 1;
}
typedef struct {
char name[DIRNAMELENGTH];
uint32_t subDirCount;
uint32_t subDirOffset;
uint32_t subFileCount;
uint32_t subFileOffset;
uint32_t unknown2;
uint32_t unknown3;
}__attribute__((packed)) tocDirectoryEntry;
typedef struct {
char name[FILENAMELENGTH];
uint32_t fileOffset;
uint32_t fileSize;
uint32_t unknown1;
uint32_t unknown2;
}__attribute__((packed)) tocFileEntry;
char directoryState[4][16];
char completeName[48] = { 0 };
int dirlevel = 0;
char rootchar[4] = "ZOE";
void processDirectoryListing( FILE *f, tocDirectoryEntry entry, char *curpath, unsigned int tgsfix ) {
tocFileEntry file;
unsigned int i;
FILE *o;
char *filepath = NULL;
filepath = malloc(strlen(curpath)+1+strlen(entry.name)+1+16+1); // max filename length is 16
sprintf(filepath, "%s/%s", curpath, entry.name);
printf("processDirectoryListing - Current directory: %s at %08lx\n", filepath, ftell(f) );
createDirectory( filepath );
for( i = 0; i < entry.subFileCount; i++ ) {
fseek( f, (entry.subFileOffset*BLOCKSIZE)+(32*i), SEEK_SET );
fread( &file, sizeof(file), 1, f );
if(!file.name[0]) {
printf("skipping invalid toc entry %d of %d\n", i+1, entry.subFileCount);
continue;
}
if( tgsfix ) file.fileOffset -= 0x18;
sprintf(filepath, "%s/%s/%s", curpath, entry.name, file.name);
printf("Current file: %s at %08x\n", filepath, file.fileOffset*BLOCKSIZE );
if( !(o = fopen( filepath, "wb" ))) {
printf("Couldnt open file %s\n", filepath);
return;
}
fseek( f, file.fileOffset*BLOCKSIZE, SEEK_SET );
writeFile( f, file.fileSize, o );
fclose(o);
}
free(filepath);
return;
}
void processDirectory( FILE *f, tocDirectoryEntry entry, char *curpath, unsigned int tgsfix ) {
memset( completeName, 0, 48 );
tocDirectoryEntry workentry;
unsigned int i;
char *newpath = NULL;
if(!curpath) {
newpath = malloc(sizeof("ZOE")+1);
sprintf(newpath, "ZOE");
}
else {
newpath = malloc(strlen(curpath)+1+strlen(entry.name)+1);
sprintf(newpath, "%s/%s", curpath, entry.name);
}
printf("processDirectory - Current directory: %s at %08lx\n", newpath, ftell(f) );
createDirectory( newpath );
for( i = 0; i < entry.subDirCount; i++ ) {
fseek( f, entry.subDirOffset+(i*32), SEEK_SET );
fread( &workentry, sizeof(workentry), 1, f );
if( tgsfix && workentry.subFileOffset ) workentry.subFileOffset -= 0x18;
if( workentry.subDirCount ) processDirectory( f, workentry, newpath, tgsfix );
else processDirectoryListing( f, workentry, newpath, tgsfix );
}
free(newpath);
return;
}
int main( int argc, char **argv ) {
tocDirectoryEntry workdir;
unsigned int tgsfix = 0;
FILE *f;
memset( workdir.name, 0, DIRNAMELENGTH );
if ( argc < 2 ) {
printf("Not enough args!\nUse: %s DAT-file\n", argv[0]);
return 1;
}
if( argc == 3 ) tgsfix = 1;
if( !(f = fopen( argv[1], "rb" ))) {
printf("Couldnt open file %s\n", argv[1]);
return 1;
}
fseek( f, 0, SEEK_SET );
fread( &workdir, sizeof(workdir), 1, f );
processDirectory( f, workdir, NULL, tgsfix );
printf("Done.\n");
fclose(f);
return 0;
}