This repository has been archived by the owner on Oct 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
166 lines (124 loc) · 3.15 KB
/
main.cpp
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
#include <windows.h>
#include <stdio.h>
#include <sys/stat.h>
typedef unsigned long long QWORD;
#pragma pack(push, 1)
struct ArchiveHeader
{
char Header[64];
QWORD FilesCount;
};
struct ArchiveEntry
{
char FileName[128];
QWORD StartOffset;
QWORD Length;
};
#pragma pack(pop)
#define SIG_PACKONLY ("PackOnly")
#define SIG_PACKPLUS ("PackPlus")
#define TYPE_PACKONLY 0
#define TYPE_PACKPLUS 1
#define SAFE_EXIT(x); fclose(x); return 0;
inline BOOL dexist(const char* dir)
{
struct stat buf;
return (stat(dir, &buf) == 0 && buf.st_mode & S_IFDIR);
}
int main(int argc, char* argv[])
{
if (argc < 3)
{
printf("Usage: %s <filename> <out_directory>\n", argv[0]);
return 0;
}
FILE* file = fopen(argv[1], "rb");
if (file)
{
ArchiveHeader header;
if (fread(&header, 1, sizeof(ArchiveHeader), file) != sizeof(ArchiveHeader))
{
printf("Can't read file header!\n");
SAFE_EXIT(file);
}
BYTE archive_type = (strcmp(header.Header, SIG_PACKONLY) != 0);
if (archive_type && strcmp(header.Header, SIG_PACKPLUS) != 0)
{
printf("This file is not a CC archive!\n");
SAFE_EXIT(file);
}
if (header.FilesCount <= 0)
{
printf("There's no files in this archive!\n");
SAFE_EXIT(file);
}
fseek(file, 0, SEEK_END);
if (ftell(file) < (sizeof(ArchiveHeader) + sizeof(ArchiveEntry) * header.FilesCount))
{
printf("Bad header (invalid entry count)\n");
SAFE_EXIT(file);
}
fseek(file, sizeof(ArchiveHeader), SEEK_SET);
ArchiveEntry* arch_files = new ArchiveEntry[(DWORD)header.FilesCount];
QWORD biggest_offset = 0;
for (QWORD i = 0; i < header.FilesCount; i++)
{
if (fread(&arch_files[i], 1, sizeof(ArchiveEntry), file) != sizeof(ArchiveEntry))
{
printf("Can't read file entry (%llu)!\n", i);
SAFE_EXIT(file);
}
QWORD sum = arch_files[i].StartOffset + arch_files[i].Length;
if (sum > biggest_offset)
biggest_offset = sum;
}
fseek(file, 0, SEEK_END);
if (ftell(file) < biggest_offset)
{
printf("Invalid data-block size\n");
SAFE_EXIT(file);
}
if (!dexist(argv[2]) && !CreateDirectory(argv[2], NULL))
{
printf("Unable to create output directory!\n");
SAFE_EXIT(file);
}
BYTE brk = FALSE;
char path[MAX_PATH + 1];
for (QWORD i = 0; i < header.FilesCount; i++)
{
fseek(file, (long)arch_files[i].StartOffset, SEEK_SET);
QWORD to_read = arch_files[i].Length;
strcpy(path, argv[2]);
strcat(path, "\\");
strcat(path, arch_files[i].FileName);
FILE* file_out = fopen(path, "wb");
if (!file_out)
{
printf("Unable to create output file (%s)!", arch_files[i].FileName);
SAFE_EXIT(file);
}
while (to_read > 0)
{
int c = fgetc(file);
if (c == EOF)
{
brk = TRUE;
break;
}
fputc((archive_type == TYPE_PACKONLY) ? c : ~c, file_out);
to_read--;
}
fclose(file_out);
if (brk) break;
}
if (brk)
{
printf("Unexpected end of file!\n");
SAFE_EXIT(file);
}
fclose(file);
}
else printf("Can't find specified file!\n");
return 0;
}