6#include <cryptopp/aes.h>
7#include <cryptopp/modes.h>
8#include <cryptopp/filters.h>
20 if (!std::filesystem::exists(path)) {
25 auto* gcf =
new GCF{path};
26 auto packFile = std::unique_ptr<PackFile>(gcf);
28 FileStream reader(gcf->fullFilePath);
32 reader.read(gcf->header);
33 if (gcf->header.dummy1 != 1 && gcf->header.dummy2 != 1 && gcf->header.gcfversion != 6) {
43 if (gcf->header.filesize != std::filesystem::file_size(gcf->fullFilePath)) {
48 reader.read(gcf->blockheader);
49 if (gcf->blockheader.count != gcf->header.blockcount) {
56 if (gcf->blockheader.count +
57 gcf->blockheader.used +
58 gcf->blockheader.dummy1 +
59 gcf->blockheader.dummy2 +
60 gcf->blockheader.dummy3 +
61 gcf->blockheader.dummy4 +
62 gcf->blockheader.dummy5 != gcf->blockheader.checksum) {
67 bool requestKey =
false;
68 for (
int i = 0; i < gcf->header.blockcount; i++) {
69 Block& block = gcf->blockdata.emplace_back();
77 if (key.size() != gcf->decryption_key.size()) {
80 std::ranges::copy(key, gcf->decryption_key.begin());
95 auto blkcount = reader.read<uint32_t>();
96 auto d1 = reader.read<uint32_t>();
97 auto d2 = reader.read<uint32_t>();
98 auto checksum = reader.read<uint32_t>();
100 if (blkcount + d1 + d2 != checksum || blkcount != gcf->blockheader.count) {
106 for (
int i = 0; i < blkcount; i++) {
107 gcf->fragmap.push_back(reader.read<uint32_t>());
113 uint64_t temp = reader.tell_in();
114 reader.read(gcf->dirheader);
116 uint64_t diroffset = reader.tell_in() + (gcf->dirheader.itemcount * 28);
118 std::vector<DirectoryEntry2> direntries{};
120 for (
int i = 0; i < gcf->dirheader.itemcount; i++) {
123 auto currentoffset = reader.tell_in();
131 dirname.insert(0,
"/");
132 dirname.insert(0, current_filename_entry.
filename);
135 auto gcfEntryPath = gcf->cleanEntryPath(dirname);
136 if (!gcfEntryPath.empty()) {
146 gcf->entries.emplace(gcfEntryPath, gcfEntry);
149 callback(gcfEntryPath, gcfEntry);
152 reader.seek_in_u(currentoffset);
158 reader.seek_in_u(temp + gcf->dirheader.dirsize);
164 for (
int i = 0; i < gcf->dirheader.itemcount; i++) {
171 reader.skip_in<uint32_t>();
172 auto checksumsize = reader.read<uint32_t>();
173 std::size_t checksums_start = reader.tell_in();
180 if (chksummapheader.dummy1 != 0x14893721 || chksummapheader.dummy2 != 0x1) {
186 for (
int i = 0; i < chksummapheader.item_count; i++) {
187 auto& cur_entry = gcf->chksum_map.emplace_back();
188 reader.read(cur_entry);
191 for (
int i = 0; i < chksummapheader.checksum_count; i++) {
192 auto& currentChecksum = gcf->checksums.emplace_back();
193 reader.read(currentChecksum);
198 reader.seek_in_u(checksums_start + checksumsize);
200 reader.read(gcf->datablockheader);
205 std::vector<std::string> bad;
208 if (!bytes || bytes->empty()) {
211 std::size_t tocheck = bytes->size();
212 uint32_t idx = entry.
crc32;
214 uint32_t checksumstart = this->
chksum_map[idx].firstindex;
215 for (
int i = 0; i < count; i++) {
216 uint32_t csum = this->
checksums[checksumstart + i];
217 std::size_t toread = std::min(
static_cast<std::size_t
>(0x8000), tocheck);
218 const auto* data = bytes->data() + (i * 0x8000);
220 if (checksum != csum) {
229std::optional<std::vector<std::byte>>
GCF::readEntry(
const std::string& path_)
const {
235 if (entry->unbaked) {
239 std::vector<std::byte> filedata;
240 if (entry->length == 0) {
245 uint32_t dir_index = entry->offset;
248 std::vector<Block> toread;
250 if (v.dir_index == dir_index && (v.flags & 0x8000)) {
255 if (toread.empty()) {
259 std::sort(toread.begin(), toread.end(), [](
const Block& lhs,
const Block& rhs) {
260 return lhs.file_data_offset < rhs.file_data_offset;
268 uint64_t remaining = entry->length;
269 bool needs_decrypt =
false;
271 for (
auto& block : toread) {
272 uint32_t currindex = block.first_data_block_index;
274 uint64_t curfilepos =
static_cast<uint64_t
>(this->
datablockheader.
firstblockoffset) + (
static_cast<std::uint64_t
>(0x2000) *
static_cast<std::uint64_t
>(currindex));
275 stream.seek_in_u(curfilepos);
277 uint32_t toreadAmt = std::min(remaining,
static_cast<uint64_t
>(0x2000));
278 auto streamvec = stream.read_bytes(toreadAmt);
279 filedata.insert(filedata.end(), streamvec.begin(), streamvec.end());
280 remaining -= toreadAmt;
281 currindex = this->
fragmap[currindex];
284 needs_decrypt = block.isEncrypted();
285 filemode = block.getCompressionType();
288 CryptoPP::byte iv[16] = {};
295 auto real_size = filedata.size();
296 if (filedata.size() % 10 != 0) {
297 filedata.resize(filedata.size() + (0x10 - (filedata.size() % 10)), {});
299 auto remaining_encrypted = filedata.size();
301 while (remaining_encrypted) {
302 CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption d;
303 d.SetKeyWithIV(
reinterpret_cast<const CryptoPP::byte*
>(this->
decryption_key.data()), 16, iv);
304 auto current_batch = std::min(remaining_encrypted,
static_cast<size_t>(0x8000));
305 d.ProcessData(
reinterpret_cast<CryptoPP::byte*
>(filedata.data() + offset),
reinterpret_cast<CryptoPP::byte*
>(filedata.data() + offset), current_batch);
306 remaining_encrypted -= current_batch;
307 offset +=
static_cast<int>(current_batch);
309 filedata.resize(real_size);
312 case COMPRESSED_AND_ENCRYPTED: {
313 std::vector<std::byte> processed_data;
314 BufferStream s(filedata.data(), filedata.size());
315 while (s.size() != s.tell()) {
316 static constexpr auto allocate_block = [](std::vector<std::byte>& vec,
size_t x) {
317 const size_t old = vec.size();
319 return vec.data() + old;
322 auto encrypted_size = s.read<uint32_t>();
323 mz_ulong decompressed_size = s.read<uint32_t>();
325 auto buffer = s.read_bytes(encrypted_size);
326 auto real_size = buffer.size();
327 if (buffer.size() % 10 != 0) {
328 buffer.resize(buffer.size() + (0x10 - (buffer.size() % 10)), {});
330 CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption d;
331 d.SetKeyWithIV(
reinterpret_cast<const CryptoPP::byte*
>(this->
decryption_key.data()), 16, iv);
332 d.ProcessData(
reinterpret_cast<CryptoPP::byte*
>(buffer.data()),
reinterpret_cast<CryptoPP::byte*
>(buffer.data()), buffer.size());
333 buffer.resize(real_size);
335 auto start = allocate_block(processed_data, decompressed_size);
336 if (mz_uncompress(
reinterpret_cast<uint8_t*
>(start), &decompressed_size,
reinterpret_cast<uint8_t*
>(buffer.data()), buffer.size()) != MZ_OK) {
341 if (entry->length == processed_data.size()) {
344 s.seek_u(s.tell() + (0x8000 - s.tell() % 0x8000));
346 filedata = processed_data;
359GCF::operator std::string()
const {
360 return PackFileReadOnly::operator std::string() +
361 " | Version v" + std::to_string(this->header.gcfversion) +
362 " | AppID " + std::to_string(this->header.appid) +
363 " | App Version v" + std::to_string(this->header.appversion);
This class represents the metadata that a file has inside a PackFile.
uint64_t offset
Offset, format-specific meaning - 0 if unused, or if the offset genuinely is 0.
uint32_t crc32
CRC32 checksum - 0 if unused.
uint64_t length
Length in bytes (in formats with compression, this is the uncompressed length)
static std::unique_ptr< PackFile > open(const std::string &path, const EntryCallback &callback=nullptr, const OpenPropertyRequest &requestProperty=nullptr)
std::vector< Block > blockdata
std::vector< ChecksumMapEntry > chksum_map
std::array< std::byte, 16 > decryption_key
uint32_t getVersion() const
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 ...
uint32_t getAppVersion() const
DataBlockHeader datablockheader
Attribute getSupportedEntryAttributes() const override
Returns a list of supported entry attributes Mostly for GUI programs that show entries and their meta...
std::vector< uint32_t > checksums
uint32_t getAppID() const
std::optional< std::vector< std::byte > > readEntry(const std::string &path_) const override
Try to read the entry's data to a bytebuffer.
std::vector< uint32_t > fragmap
EntryCallbackBase< void > EntryCallback
std::optional< Entry > findEntry(const std::string &path_, bool includeUnbaked=true) const
Try to find an entry given the file path.
std::function< std::vector< std::byte >(PackFile *packFile, OpenProperty property)> OpenPropertyRequest
void runForAllEntries(const EntryCallback &operation, bool includeUnbaked=true) const
Run a callback for each entry in the pack file.
std::string cleanEntryPath(const std::string &path) const
static Entry createNewEntry()
static std::optional< std::vector< std::byte > > readUnbakedEntry(const Entry &entry)
uint32_t computeAdler32(std::span< const std::byte > buffer)
uint32_t computeCRC32(std::span< const std::byte > buffer)
DirectoryEntry entry_real