SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
GCF.cpp
Go to the documentation of this file.
1#include <vpkpp/format/GCF.h>
2
3#include <algorithm>
4#include <filesystem>
5#include <format>
6
7#include <cryptopp/aes.h>
8#include <cryptopp/modes.h>
9#include <cryptopp/filters.h>
10#include <FileStream.h>
11#include <miniz.h>
14
15using namespace sourcepp;
16using namespace vpkpp;
17
18std::unique_ptr<PackFile> GCF::open(const std::string& path, const EntryCallback& callback, const OpenPropertyRequest& requestProperty) {
19 // TODO: Add v5 and perhaps v4 support
20
21 if (!std::filesystem::exists(path)) {
22 // File does not exist
23 return nullptr;
24 }
25
26 auto* gcf = new GCF{path};
27 auto packFile = std::unique_ptr<PackFile>(gcf);
28
29 FileStream reader(gcf->fullFilePath);
30 reader.seek_in(0);
31
32 // read the main header here (not the block header)
33 reader.read(gcf->header);
34 if (gcf->header.dummy1 != 1 && gcf->header.dummy2 != 1 && gcf->header.gcfversion != 6) {
41 return nullptr;
42 }
43
44 if (gcf->header.filesize != std::filesystem::file_size(gcf->fullFilePath)) {
45 // again, this should never occur with a valid gcf file
46 return nullptr;
47 }
48
49 reader.read(gcf->blockheader);
50 if (gcf->blockheader.count != gcf->header.blockcount) {
51 return nullptr;
52 }
53
54 // if you're having a headache reading the if statement below heres a quick explaination of what it actually does
55 // it just adds all blockheader entries together and compares it against the checksum
56 // if it's not the same then we bail out with a nullptr
57 if (gcf->blockheader.count +
58 gcf->blockheader.used +
59 gcf->blockheader.dummy1 +
60 gcf->blockheader.dummy2 +
61 gcf->blockheader.dummy3 +
62 gcf->blockheader.dummy4 +
63 gcf->blockheader.dummy5 != gcf->blockheader.checksum) {
64 return nullptr;
65 }
66
67 // block headers!!!!!!
68 bool requestKey = false;
69 for (int i = 0; i < gcf->header.blockcount; i++) {
70 Block& block = gcf->blockdata.emplace_back();
71 reader.read(block);
72 if (block.isEncrypted()) {
73 requestKey = true;
74 }
75 }
76 if (requestKey) {
77 const auto key = requestProperty(gcf, OpenProperty::DECRYPTION_KEY);
78 if (key.size() != gcf->decryption_key.size()) {
79 return nullptr;
80 }
81 std::ranges::copy(key, gcf->decryption_key.begin());
82 }
83
84 // Fragmentation Map header
85 // not worth keeping around after verifying stuff so no struct def
86 // if you want one this is how one should look like
93 // actually if we want to implement writing later this might be a better idea
94 // if anyone wants to touch this piece of shit format again anyways
95
96 auto blkcount = reader.read<uint32_t>();
97 auto d1 = reader.read<uint32_t>();
98 auto d2 = reader.read<uint32_t>();
99 auto checksum = reader.read<uint32_t>();
100
101 if (blkcount + d1 + d2 != checksum || blkcount != gcf->blockheader.count) {
102 return nullptr;
103 }
104
105 // Fragmentation Map (list of dwords)
106
107 for (int i = 0; i < blkcount; i++) {
108 gcf->fragmap.push_back(reader.read<uint32_t>());
109 }
110
111 // Directory stuff starts here
112
113 //Reading the header
114 uint64_t temp = reader.tell_in();
115 reader.read(gcf->dirheader);
116
117 uint64_t diroffset = reader.tell_in() + (gcf->dirheader.itemcount * 28);
118
119 std::vector<DirectoryEntry2> direntries{};
120
121 for (int i = 0; i < gcf->dirheader.itemcount; i++) {
122 DirectoryEntry2& entry = direntries.emplace_back();
123 reader.read(entry.entry_real);
124 auto currentoffset = reader.tell_in();
125 reader.seek_in_u(diroffset + entry.entry_real.nameoffset);
126 reader.read(entry.filename);
127 if (entry.entry_real.dirtype != 0) { // if not directory
128 std::string dirname;
129 DirectoryEntry2 current_filename_entry = entry;
130 while (current_filename_entry.entry_real.parentindex != 0xffffffff) {
131 current_filename_entry = direntries[current_filename_entry.entry_real.parentindex];
132 dirname.insert(0, "/");
133 dirname.insert(0, current_filename_entry.filename);
134 }
135
136 auto gcfEntryPath = gcf->cleanEntryPath(dirname);
137 if (!gcfEntryPath.empty()) {
138 gcfEntryPath += '/';
139 }
140 gcfEntryPath += entry.filename;
141
142 Entry gcfEntry = createNewEntry();
143 gcfEntry.length = entry.entry_real.itemsize;
144 gcfEntry.crc32 = entry.entry_real.fileid; // INDEX INTO THE CHECKSUM MAP VECTOR NOT CRC32!!!
145 gcfEntry.offset = i; // THIS IS THE STRUCT INDEX NOT SOME OFFSET!!!
146 //printf("%s\n", gcfEntry.path.c_str());
147 gcf->entries.emplace(gcfEntryPath, gcfEntry);
148
149 if (callback) {
150 callback(gcfEntryPath, gcfEntry);
151 }
152 }
153 reader.seek_in_u(currentoffset);
154 }
155
156 // Directory Map
157
158 // Directory Map header
159 reader.seek_in_u(temp + gcf->dirheader.dirsize);
160
161 //auto dmap = reader.read<DirectoryMapHeader>();
162 reader.skip_in<DirectoryMapHeader>();
163
164 // Directory Map entries
165 for (int i = 0; i < gcf->dirheader.itemcount; i++) {
166 DirectoryMapEntry& entry = gcf->dirmap_entries.emplace_back();
167 reader.read(entry);
168 }
169
170 // Checksum header
171 //auto dummy0 = reader.read<uint32_t>();
172 reader.skip_in<uint32_t>();
173 auto checksumsize = reader.read<uint32_t>();
174 std::size_t checksums_start = reader.tell_in();
175
176 //printf("checksums start: %llu\n", checksums_start);
177 //printf("%lu %lu %lu %lu\n", gcf->header.blockcount, gcf->blockheader.used, gcf->header.appid, gcf->header.appversion);
178 // map header
179
180 auto chksummapheader = reader.read<ChecksumMapHeader>();
181 if (chksummapheader.dummy1 != 0x14893721 || chksummapheader.dummy2 != 0x1) {
182 return nullptr;
183 }
184
185 //printf("%lu %lu\n", chksummapheader.checksum_count, chksummapheader.item_count);
186
187 for (int i = 0; i < chksummapheader.item_count; i++) {
188 auto& cur_entry = gcf->chksum_map.emplace_back();
189 reader.read(cur_entry);
190 }
191
192 for (int i = 0; i < chksummapheader.checksum_count; i++) {
193 auto& currentChecksum = gcf->checksums.emplace_back();
194 reader.read(currentChecksum);
195 }
196 //printf("current pos: %llu, block header: %llu should be: %llu", reader.tellInput(), reader.tellInput() + 0x80, checksums_start + checksumsize);
197 // TODO: check the checksum RSA signature... later.. if ever...
198
199 reader.seek_in_u(checksums_start + checksumsize);
200
201 reader.read(gcf->datablockheader);
202 return packFile;
203}
204
205std::vector<std::string> GCF::verifyEntryChecksums() const {
206 std::vector<std::string> bad;
207 this->runForAllEntries([this, &bad](const std::string& path, const Entry& entry) {
208 auto bytes = this->readEntry(path);
209 if (!bytes || bytes->empty()) {
210 return;
211 }
212 std::size_t tocheck = bytes->size();
213 uint32_t idx = entry.crc32;
214 uint32_t count = this->chksum_map[idx].count;
215 uint32_t checksumstart = this->chksum_map[idx].firstindex;
216 for (int i = 0; i < count; i++) {
217 uint32_t csum = this->checksums[checksumstart + i];
218 std::size_t toread = std::min(static_cast<std::size_t>(0x8000), tocheck);
219 const auto* data = bytes->data() + (i * 0x8000);
220 uint32_t checksum = crypto::computeCRC32({data, toread}) ^ crypto::computeAdler32({data, toread});
221 if (checksum != csum) {
222 bad.push_back(path);
223 }
224 tocheck -= toread;
225 }
226 });
227 return bad;
228}
229
230std::optional<std::vector<std::byte>> GCF::readEntry(const std::string& path_) const {
231 auto path = this->cleanEntryPath(path_);
232 auto entry = this->findEntry(path);
233 if (!entry) {
234 return std::nullopt;
235 }
236 if (entry->unbaked) {
237 return readUnbakedEntry(*entry);
238 }
239
240 std::vector<std::byte> filedata;
241 if (entry->length == 0) {
242 // don't bother
243 return filedata;
244 }
245
246 uint32_t dir_index = entry->offset;
247 //printf(" extracting file: %s\n", entry.path.c_str());
248
249 std::vector<Block> toread;
250 for (const auto& v : this->blockdata) {
251 if (v.dir_index == dir_index && (v.flags & 0x8000)) { // need to check for 0x8000 here because valve dumps uninitialized memory
252 toread.push_back(v);
253 }
254 }
255
256 if (toread.empty()) {
257 //printf("could not find any directory index for %lu", entry.vpk_offset);
258 return std::nullopt;
259 }
260 std::sort(toread.begin(), toread.end(), [](const Block& lhs, const Block& rhs) {
261 return lhs.file_data_offset < rhs.file_data_offset;
262 });
263
264 FileStream stream{this->fullFilePath};
265 if (!stream) {
266 return std::nullopt;
267 }
268
269 uint64_t remaining = entry->length;
270 bool needs_decrypt = false;
272 for (auto& block : toread) {
273 uint32_t currindex = block.first_data_block_index;
274 while (currindex <= this->blockheader.count) {
275 uint64_t curfilepos = static_cast<uint64_t>(this->datablockheader.firstblockoffset) + (static_cast<std::uint64_t>(0x2000) * static_cast<std::uint64_t>(currindex));
276 stream.seek_in_u(curfilepos);
277 //printf("off %lli block %lu toread %lli should be %llu\n", stream.tellInput(), currindex, remaining, curfilepos);
278 uint32_t toreadAmt = std::min(remaining, static_cast<uint64_t>(0x2000));
279 auto streamvec = stream.read_bytes(toreadAmt);
280 filedata.insert(filedata.end(), streamvec.begin(), streamvec.end());
281 remaining -= toreadAmt;
282 currindex = this->fragmap[currindex];
283 //printf("curridx now: %lu\n", currindex);
284 }
285 needs_decrypt = block.isEncrypted();
286 filemode = block.getCompressionType();
287 }
288 if (needs_decrypt) {
289 CryptoPP::byte iv[16] = {};
290 switch (filemode) {
291 using enum Block::CompressionType;
292 case UNCOMPRESSED:
293 case COMPRESSED:
294 break;
295 case ENCRYPTED: {
296 auto real_size = filedata.size();
297 if (filedata.size() % 10 != 0) {
298 filedata.resize(filedata.size() + (0x10 - (filedata.size() % 10)), {});
299 }
300 auto remaining_encrypted = filedata.size();
301 auto offset = 0;
302 while (remaining_encrypted) {
303 CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption d;
304 d.SetKeyWithIV(reinterpret_cast<const CryptoPP::byte*>(this->decryption_key.data()), 16, iv);
305 auto current_batch = std::min(remaining_encrypted, static_cast<size_t>(0x8000));
306 d.ProcessData(reinterpret_cast<CryptoPP::byte*>(filedata.data() + offset), reinterpret_cast<CryptoPP::byte*>(filedata.data() + offset), current_batch);
307 remaining_encrypted -= current_batch;
308 offset += static_cast<int>(current_batch);
309 }
310 filedata.resize(real_size);
311 break;
312 }
313 case COMPRESSED_AND_ENCRYPTED: {
314 std::vector<std::byte> processed_data;
315 BufferStream s(filedata.data(), filedata.size());
316 while (s.size() != s.tell()) {
317 static constexpr auto allocate_block = [](std::vector<std::byte>& vec, size_t x) {
318 const size_t old = vec.size();
319 vec.resize(old + x);
320 return vec.data() + old;
321 };
322
323 auto encrypted_size = s.read<uint32_t>();
324 mz_ulong decompressed_size = s.read<uint32_t>();
325
326 auto buffer = s.read_bytes(encrypted_size);
327 auto real_size = buffer.size();
328 if (buffer.size() % 10 != 0) {
329 buffer.resize(buffer.size() + (0x10 - (buffer.size() % 10)), {});
330 }
331 CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption d;
332 d.SetKeyWithIV(reinterpret_cast<const CryptoPP::byte*>(this->decryption_key.data()), 16, iv);
333 d.ProcessData(reinterpret_cast<CryptoPP::byte*>(buffer.data()), reinterpret_cast<CryptoPP::byte*>(buffer.data()), buffer.size());
334 buffer.resize(real_size);
335
336 auto start = allocate_block(processed_data, decompressed_size);
337 if (mz_uncompress(reinterpret_cast<uint8_t*>(start), &decompressed_size, reinterpret_cast<uint8_t*>(buffer.data()), buffer.size()) != MZ_OK) {
338 return std::nullopt;
339 }
340
341 //auto remaining = s.size() - s.tell();
342 if (entry->length == processed_data.size()) {
343 break;
344 }
345 s.seek_u(s.tell() + (0x8000 - s.tell() % 0x8000));
346 }
347 filedata = processed_data;
348 break;
349 }
350 }
351 }
352 return filedata;
353}
354
356 using enum Attribute;
357 return LENGTH;
358}
359
360GCF::operator std::string() const {
361 return PackFileReadOnly::operator std::string() + std::format(" | Version v{} | AppID {} | App Version v{}", this->header.gcfversion, this->header.appid, this->header.appversion);
362}
363
364uint32_t GCF::getVersion() const {
365 return this->header.gcfversion;
366}
367
368uint32_t GCF::getAppID() const {
369 return this->header.appid;
370}
371
372uint32_t GCF::getAppVersion() const {
373 return this->header.appversion;
374}
This class represents the metadata that a file has inside a PackFile.
Definition Entry.h:14
uint64_t offset
Offset, format-specific meaning - 0 if unused, or if the offset genuinely is 0.
Definition Entry.h:33
uint32_t crc32
CRC32 checksum - 0 if unused.
Definition Entry.h:40
uint64_t length
Length in bytes (in formats with compression, this is the uncompressed length).
Definition Entry.h:26
static std::unique_ptr< PackFile > open(const std::string &path, const EntryCallback &callback=nullptr, const OpenPropertyRequest &requestProperty=nullptr)
Definition GCF.cpp:18
Header header
Definition GCF.h:168
std::vector< Block > blockdata
Definition GCF.h:170
BlockHeader blockheader
Definition GCF.h:169
std::vector< ChecksumMapEntry > chksum_map
Definition GCF.h:176
std::array< std::byte, 16 > decryption_key
Definition GCF.h:178
uint32_t getVersion() const
Definition GCF.cpp:364
std::vector< std::string > verifyEntryChecksums() const override
Verify the checksums of each file, if a file fails the check its path will be added to the vector If ...
Definition GCF.cpp:205
uint32_t getAppVersion() const
Definition GCF.cpp:372
DataBlockHeader datablockheader
Definition GCF.h:175
Attribute getSupportedEntryAttributes() const override
Returns a list of supported entry attributes Mostly for GUI programs that show entries and their meta...
Definition GCF.cpp:355
std::vector< uint32_t > checksums
Definition GCF.h:177
uint32_t getAppID() const
Definition GCF.cpp:368
std::optional< std::vector< std::byte > > readEntry(const std::string &path_) const override
Try to read the entry's data to a bytebuffer.
Definition GCF.cpp:230
std::vector< uint32_t > fragmap
Definition GCF.h:171
EntryCallbackBase< void > EntryCallback
Definition PackFile.h:38
std::optional< Entry > findEntry(const std::string &path_, bool includeUnbaked=true) const
Try to find an entry given the file path.
Definition PackFile.cpp:172
std::string fullFilePath
Definition PackFile.h:231
std::function< std::vector< std::byte >(PackFile *packFile, OpenProperty property)> OpenPropertyRequest
Definition PackFile.h:33
void runForAllEntries(const EntryCallback &operation, bool includeUnbaked=true) const
Run a callback for each entry in the pack file.
Definition PackFile.cpp:529
std::string cleanEntryPath(const std::string &path) const
Definition PackFile.cpp:706
static Entry createNewEntry()
Definition PackFile.cpp:715
static std::optional< std::vector< std::byte > > readUnbakedEntry(const Entry &entry)
Definition PackFile.cpp:719
uint32_t computeAdler32(std::span< const std::byte > buffer)
Definition Adler32.cpp:39
uint32_t computeCRC32(std::span< const std::byte > buffer)
Definition CRC32.cpp:7
Attribute
Definition Attribute.h:7
bool isEncrypted() const
Definition GCF.h:72
std::string filename
Definition GCF.h:106
DirectoryEntry entry_real
Definition GCF.h:105