-
Notifications
You must be signed in to change notification settings - Fork 5
/
fileinfo.c
70 lines (66 loc) · 2.09 KB
/
fileinfo.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
#include "fileinfo.h"
uint64_t readintobuf(char *filename, char **out) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr, "Error: unable to open file '%s'\n", filename);
exit(2);
}
fseek(file, 0, SEEK_END);
uint64_t length = ftell(file);
fseek(file, 0, SEEK_SET);
*out = (char *)malloc(length * sizeof(char));
fread(*out, length, 1, file);
fclose(file);
return length;
}
filedata *loadfile(char *filename) {
filedata *ret = (filedata *)calloc(1, sizeof(filedata));
ret->length = readintobuf(filename, &(ret->contents));
printf("[File] Name: %s | Size: %lX bytes\n", filename, ret->length);
parse(ret);
return ret;
}
void parse(filedata *fd) {
if (strncmp(fd->contents, "<!--", 4) == 0) {
fprintf(stderr, "Parsing as dmx...\n");
fd->filetype = UNK_DMX;
} else if (*((uint32_t *)fd->contents) == 0x55aa1234) {
fprintf(stderr, "Parsing as vpk...\n");
fd->filetype = VPK;
} else if (*((uint32_t *)fd->contents) == 0xFADEBEAD) {
fprintf(stderr, "Parsing as gnv...\n");
fd->filetype = GNV;
} else if (strncmp(fd->contents, "VBSP", 4) == 0) {
fprintf(stderr, "Parsing as bsp. Currently not handled and out "
"of scope.\n");
fd->filetype = BSP;
} else if (strncmp(fd->contents, "vcs2", 4) == 0) {
fprintf(stderr, "Parsing as vcs...\n");
parse_vcs(fd);
} else if (strncmp(fd->contents, "VBKV", 4) == 0) {
fprintf(stderr,
"Parsing as vbkv. Currently not handled...and it's "
"_super_ uncommon.\n");
fd->filetype = VBKV;
} else if (strncmp(fd->contents, "// file", 7) == 0) {
fprintf(stderr, "Parsing as cloth...\n");
fd->filetype = KV_CLOTH;
} else if (*((uint32_t *)fd->contents) == fd->length) {
fprintf(stderr, "Parsing as SVF...\n");
parse_svf(fd);
} else if (*((uint16_t *)(fd->contents + 4)) == 0x0C) {
fprintf(
stderr,
"File length wrong, but speculatively parsing as SVF...\n");
parse_svf(fd);
} else {
fprintf(stderr, "Unknown file type.\n");
fd->filetype = UNKNOWN;
}
}
void fd_free(filedata *fd) {
if (fd == NULL) {
fprintf(stderr, "Error: Tried to fd_free null pointer!\n");
return;
}
}