-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvfs_unpacker.c
220 lines (171 loc) · 5.55 KB
/
vfs_unpacker.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
/**
* compile commands:
* unix - gcc vfs_unpacker.c -o vfs_unpacker
* windows - cl.exe vfs_unpacker.c /link /out:vfs_unpacker.exe
*/
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h> // mkdir
typedef struct
{
char signature[4];
char unknown[4];
uint32_t entriesCount;
} VFS_Header;
typedef struct
{
uint32_t contentLength;
uint32_t contentOffset;
uint16_t unknown1;
uint16_t unknown2;
uint16_t unknown3;
uint16_t unknown4;
} VFS_EntryPayload;
typedef struct
{
uint8_t nameLength;
char *name;
VFS_EntryPayload payload;
} VFS_Entry;
int VFS_CheckSignature(VFS_Header *header)
{
return (strncmp(header->signature, "LP1C", 4) == 0);
}
VFS_Entry *VFS_ReadEntries(VFS_Header *header, FILE *file)
{
VFS_Entry *entries = calloc(header->entriesCount, sizeof(VFS_Entry));
if (entries) {
for (int i = 0; i < header->entriesCount; ++i) {
VFS_Entry *entry = &entries[i];
// read entry name length
if (fread(&entry->nameLength, sizeof(uint8_t), 1, file) != 1) {
printf("Warning: failed to read entry #%d\n", i);
continue;
}
// read entry name
entry->name = malloc(entry->nameLength);
if (fread(entry->name, entry->nameLength, 1, file) != 1) {
printf("Warning: failed to entry #%d name\n", i);
continue;
}
// read rest entry payload
if (fread(&entry->payload, sizeof(VFS_EntryPayload), 1, file) != 1) {
printf("Warning: failed to entry #%d payload\n", i);
continue;
}
}
return entries;
}
return NULL;
}
void VFS_DestroyEntries(VFS_Header *header, VFS_Entry *entries)
{
for (int i = 0; i < header->entriesCount; ++i) {
VFS_Entry *entry = &entries[i];
free(entry->name);
}
}
void PathJoin(char *path, ...)
{
}
int VFS_Entry_Unpack(VFS_Entry *entry, FILE *file, const char *dirname, size_t dirnameLength)
{
if (entry == NULL || dirname == NULL) {
return 1;
}
if (dirnameLength == 0) {
dirnameLength = strlen(dirname);
}
const size_t pathSize = 255;
char path[255];
snprintf(path, pathSize, "%.*s/%.*s", (int)dirnameLength, dirname, (int)entry->nameLength, entry->name);
const size_t bufferSize = 1024;
char buffer[1024];
FILE *out = fopen(path, "wb");
if (out) {
fseek(file, entry->payload.contentOffset, SEEK_SET);
size_t contentLength = entry->payload.contentLength;
while (contentLength > 0)
{
size_t toRead = contentLength;
if (toRead > bufferSize) {
toRead = bufferSize;
}
size_t read = fread(buffer, 1, toRead, file);
contentLength -= read;
if (fwrite(buffer, read, 1, out) == 0) {
break;
}
}
fclose(out);
return 0;
}
return 1;
}
int main(int argc, char *argv[])
{
// parse arguments
if (argc < 2) {
printf("Usage: %s <path-to-vfs>\n", argv[0]);
return 0;
}
int error = 1;
char *path = argv[1];
printf("Path: \"%s\"\n", path);
// create output directory
char output[255];
memcpy(output, path, strlen(path));
char *indexOfVFS = strstr(output, ".vfs");
if (indexOfVFS) {
output[indexOfVFS - output] = '\0';
}
struct stat st;
if (stat(output, &st) != 0 && ((st.st_mode) & S_IFMT) != S_IFDIR) {
if (mkdir(output, 0755) != 0) {
printf("Error: failed to create output directory\n");
return 1;
}
}
printf("Output: \"%s\"\n", path);
// open file for reading
FILE *file = fopen(path, "rb");
if (file) {
// read header
VFS_Header header;
if (fread(&header, sizeof(VFS_Header), 1, file) == 1) {
// verify file
if (VFS_CheckSignature(&header)) {
printf("Entries count: %d\n", header.entriesCount);
// read entries
VFS_Entry *entries = VFS_ReadEntries(&header, file);
if (entries) {
for (int i = 0; i < header.entriesCount; ++i) {
VFS_Entry *entry = &entries[i];
printf("[%d/%d] %.*s (strlen: %d) { length: %d, offset: %#08x }\n",
i, header.entriesCount,
entry->nameLength, entry->name,
entry->nameLength,
entry->payload.contentLength,
entry->payload.contentOffset);
if (VFS_Entry_Unpack(entry, file, output, 0) != 0) {
printf("Error: unpack failed!\n");
}
}
// clean up
VFS_DestroyEntries(&header, entries);
error = 0; // success
}
} else {
printf("Error: signature check failed\n");
}
} else {
printf("Error: failed to read file header\n");
}
fclose(file); // close opened file
} else {
printf("Error: unable to open file for reading\n");
}
return error;
}