forked from nextgal/pfs_upk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unpack.cpp
177 lines (154 loc) · 5.42 KB
/
unpack.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
167
168
169
170
171
172
173
174
175
176
177
/*
This file is part of pfs_upk.
pfs_upk is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
pfs_upk is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
pfs_upk. If not, see <https://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "pfs_upk.hpp"
using namespace std;
bool unpack(const std::string& path) {
Artemis_Header header;
ifstream arc(path, ios::in | ios::binary);
arc.read((char *)&header, sizeof(header));
// check if a vaild Artemis archive
if (memcmp(ARCHIVE_MAGIC, header.magic, ARCHIVE_MAGIC_SIZE)) { // Check magic
cout << "Invaild Artemis PFS archive!" << endl;
return false;
}
cout << "Found ";
// look for PFS revision
switch (header.pack_version) {
case '2':
cout << "vaild PFS Version 2 archive." << endl;
break;
case '6':
cout << "vaild PFS Version 6 archive." << endl;
break;
case '8':
cout << "vaild PFS Version 8 archive." << endl;
break;
default:
cout << "a unknown PFS version,and the program will exit." << endl;
return false;
}
// read for file counts & index size
cout << "Index size: 0x" << std::hex << header.index_size << "\t";
cout << "File Count: 0x" << std::hex << header.file_count << endl;
cout << endl;
// add it into vector!
vector<Artemis_Entry> index(header.file_count);
// A PFS Entry:
// uint32_t FILENAME_LENGTH
// char FILENAME[FILENAME_LENGTH]
// uint32_t RESERVED // keep 0x00000000
// uint32_t OFFSET
// uint32_t FILE_SIZE
//
for (uint32_t i = 1; i <= header.file_count; ++i) {
Artemis_Entry entry;
// get path length
uint32_t path_length = 0;
arc.read((char *)&path_length, sizeof(uint32_t));
// get path
char *buf = new char[MAX_PATH];
arc.read(buf, path_length); // NOTICE: it's UTF-8 string.
buf[path_length] = 0x00; // cut off string with 0x00
entry.path = buf;
delete[] buf;
// reserved or unknown.
arc.seekg(sizeof(uint32_t), ios::cur);
// get size and offset
arc.read(reinterpret_cast<char *>(&entry.offset), sizeof(uint32_t));
arc.read(reinterpret_cast<char *>(&entry.size), sizeof(uint32_t));
// add to index
index.push_back(entry);
}
// get basepath for unpack
filesystem::path pfspath(path); // /foo/bar/foobar.pfs
filesystem::path stem = pfspath.stem(); // foobar
pfspath = pfspath.parent_path(); // /foo/bar/
filesystem::path new_fs_prefix =
pfspath / stem / ""; // extract to /foo/bar/foobar/<VFS_PATH>
#ifdef DEBUG
cout << "FIles will oupput to: " << new_fs_prefix << endl;
#endif // DEBUG
char xor_key[20] = { 0 };
if (header.pack_version == '8') {
// compute SHA1 for XOR decrypt
// read header (again)
char* header_buf = new char[header.index_size];
arc.seekg(sizeof(Artemis_Header) - sizeof(uint32_t), ios::beg);
arc.read(header_buf, header.index_size);
SHA1_CTX hash;
SHA1Init(&hash);
SHA1Update(&hash, (unsigned char*)header_buf, header.index_size);
SHA1Final((unsigned char*)xor_key, &hash);
delete[] header_buf;
}
#ifdef _WIN32
uint32_t counter = 0;
#endif // _WIN32
for (vector<Artemis_Entry>::iterator it = index.begin(); it != index.end();
it++) {
Artemis_Entry file = *it;
// skip if entry is empty
if (file.offset == 0) {
continue;
} else {
// create folders when not exist
#ifdef _WIN32
filesystem::path vfs_subfix = filesystem::u8path(file.path);
#else
filesystem::path vfs_subfix = filesystem::path(file.path);
#endif
filesystem::path newpath = new_fs_prefix;
#ifndef _WIN32
string vfs_subfix_posix = vfs_subfix.string();
std::replace(vfs_subfix_posix.begin(), vfs_subfix_posix.end(), '\\', '/');
newpath /= filesystem::path(vfs_subfix_posix);
#else
newpath /= vfs_subfix;
#endif
filesystem::create_directories(newpath.parent_path());
// open file
#ifdef _WIN32
// extra infomation for Windows
wchar_t Buffer[32];
swprintf(Buffer, 32, L"%u / %u", counter, header.file_count);
SetConsoleTitleW(Buffer);
counter++;
#endif // _WIN32
fstream handle(newpath, ios::binary | ios::out);
cout << "Extracting \"" << file.path.c_str() << "\" ..." << endl;
if (handle.is_open() != true) {
cout << "We encountered a problem while extracting the file." << endl;
return false;
} else {
BYTE *buf = new BYTE[file.size];
arc.seekg(file.offset, ios::beg); // seek to offset
arc.read((char *)buf, file.size);
// decrypt
if (header.pack_version == '8') {
xorcrypt((char*)buf, file.size, xor_key, 20);
}
// write back
handle.write((char *)buf, file.size);
delete[] buf;
handle.close();
}
}
}
return true;
}