From 802b0f244a4d689af467d896169f73b06f13621f Mon Sep 17 00:00:00 2001 From: Jan Laupetin Date: Tue, 19 Aug 2025 13:22:39 +0200 Subject: [PATCH] chore: dump fastfile data when xenon t6 fastfile is detected --- src/Common/Game/IGame.h | 12 ++ src/ZoneCommon/Game/T6/ZoneConstantsT6.h | 28 ++-- .../Zone/XChunk/AbstractSalsa20Processor.cpp | 34 ++-- .../Zone/XChunk/AbstractSalsa20Processor.h | 51 +++--- src/ZoneCommon/Zone/XChunk/IXChunkProcessor.h | 8 +- .../Zone/XChunk/XChunkProcessorDeflate.cpp | 2 +- .../Zone/XChunk/XChunkProcessorDeflate.h | 2 +- .../Zone/XChunk/XChunkProcessorInflate.cpp | 9 +- .../Zone/XChunk/XChunkProcessorInflate.h | 2 +- .../XChunkProcessorSalsa20Decryption.cpp | 10 +- .../XChunk/XChunkProcessorSalsa20Decryption.h | 4 +- .../XChunkProcessorSalsa20Encryption.cpp | 8 +- .../XChunk/XChunkProcessorSalsa20Encryption.h | 4 +- .../Game/T6/ZoneLoaderFactoryT6.cpp | 152 ++++++++++-------- .../Loading/Processor/ProcessorXChunks.cpp | 21 ++- .../Loading/Processor/ProcessorXChunks.h | 6 +- .../Loading/Steps/StepDumpData.cpp | 14 +- src/ZoneLoading/Loading/Steps/StepDumpData.h | 3 +- .../Game/T6/ZoneWriterFactoryT6.cpp | 8 +- 19 files changed, 226 insertions(+), 152 deletions(-) diff --git a/src/Common/Game/IGame.h b/src/Common/Game/IGame.h index 449005f8..d9868724 100644 --- a/src/Common/Game/IGame.h +++ b/src/Common/Game/IGame.h @@ -16,6 +16,18 @@ enum class GameId COUNT }; +enum class GameEndianness +{ + LITTLE_ENDIAN, + BIG_ENDIAN +}; + +enum class GameWordSize +{ + ARCH_32, + ARCH_64 +}; + static constexpr const char* GameId_Names[]{ "IW3", "IW4", diff --git a/src/ZoneCommon/Game/T6/ZoneConstantsT6.h b/src/ZoneCommon/Game/T6/ZoneConstantsT6.h index 10213294..785c3742 100644 --- a/src/ZoneCommon/Game/T6/ZoneConstantsT6.h +++ b/src/ZoneCommon/Game/T6/ZoneConstantsT6.h @@ -12,21 +12,25 @@ namespace T6 ZoneConstants() = default; public: - static constexpr const char* MAGIC_SIGNED_TREYARCH = "TAff0100"; - static constexpr const char* MAGIC_SIGNED_OAT = "ABff0100"; + static constexpr const char* MAGIC_SIGNED_PC_TREYARCH = "TAff0100"; + static constexpr const char* MAGIC_SIGNED_XENON_TREYARCH = "TAffx100"; + static constexpr const char* MAGIC_SIGNED_PC_OAT = "ABff0100"; static constexpr const char* MAGIC_UNSIGNED = "TAffu100"; static constexpr const char* MAGIC_UNSIGNED_SERVER = "TAsvu100"; - static_assert(std::char_traits::length(MAGIC_SIGNED_TREYARCH) == sizeof(ZoneHeader::m_magic)); - static_assert(std::char_traits::length(MAGIC_SIGNED_OAT) == sizeof(ZoneHeader::m_magic)); + static_assert(std::char_traits::length(MAGIC_SIGNED_PC_TREYARCH) == sizeof(ZoneHeader::m_magic)); + static_assert(std::char_traits::length(MAGIC_SIGNED_XENON_TREYARCH) == sizeof(ZoneHeader::m_magic)); + static_assert(std::char_traits::length(MAGIC_SIGNED_PC_OAT) == sizeof(ZoneHeader::m_magic)); static_assert(std::char_traits::length(MAGIC_UNSIGNED) == sizeof(ZoneHeader::m_magic)); static_assert(std::char_traits::length(MAGIC_UNSIGNED_SERVER) == sizeof(ZoneHeader::m_magic)); - static constexpr int ZONE_VERSION = 147; - static constexpr int STREAM_COUNT = 4; - static constexpr int XCHUNK_SIZE = 0x8000; - static constexpr int XCHUNK_MAX_WRITE_SIZE = XCHUNK_SIZE - 0x40; - static constexpr int VANILLA_BUFFER_SIZE = 0x80000; + static constexpr unsigned ZONE_VERSION_PC = 147; + static constexpr unsigned ZONE_VERSION_XENON = 146; + + static constexpr unsigned STREAM_COUNT = 4; + static constexpr unsigned XCHUNK_SIZE = 0x8000; + static constexpr unsigned XCHUNK_MAX_WRITE_SIZE = XCHUNK_SIZE - 0x40; + static constexpr unsigned VANILLA_BUFFER_SIZE = 0x80000; static constexpr unsigned OFFSET_BLOCK_BIT_COUNT = 3u; static constexpr block_t INSERT_BLOCK = XFILE_BLOCK_VIRTUAL; @@ -34,10 +38,14 @@ namespace T6 static constexpr size_t FILE_SUFFIX_ZERO_ALIGN = 0x40; static constexpr const char* MAGIC_AUTH_HEADER = "PHEEBs71"; - inline static const uint8_t SALSA20_KEY_TREYARCH[]{ + inline static const uint8_t SALSA20_KEY_TREYARCH_PC[]{ 0x64, 0x1D, 0x8A, 0x2F, 0xE3, 0x1D, 0x3A, 0xA6, 0x36, 0x22, 0xBB, 0xC9, 0xCE, 0x85, 0x87, 0x22, 0x9D, 0x42, 0xB0, 0xF8, 0xED, 0x9B, 0x92, 0x41, 0x30, 0xBF, 0x88, 0xB6, 0x5E, 0xDC, 0x50, 0xBE, }; + inline static const uint8_t SALSA20_KEY_TREYARCH_XENON[]{ + 0x0E, 0x50, 0xF4, 0x9F, 0x41, 0x23, 0x17, 0x09, 0x60, 0x38, 0x66, 0x56, 0x22, 0xDD, 0x09, 0x13, + 0x32, 0xA2, 0x09, 0xBA, 0x0A, 0x05, 0xA0, 0x0E, 0x13, 0x77, 0xCE, 0xDB, 0x0A, 0x3C, 0xB1, 0xD3, + }; inline static const uint8_t RSA_PUBLIC_KEY_TREYARCH[]{ 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc7, 0x9d, 0x33, 0xe0, 0x75, 0xaf, 0xef, 0x08, 0x08, 0x2b, 0x89, 0xd9, 0x3b, 0xf3, diff --git a/src/ZoneCommon/Zone/XChunk/AbstractSalsa20Processor.cpp b/src/ZoneCommon/Zone/XChunk/AbstractSalsa20Processor.cpp index 377a74be..63018e4c 100644 --- a/src/ZoneCommon/Zone/XChunk/AbstractSalsa20Processor.cpp +++ b/src/ZoneCommon/Zone/XChunk/AbstractSalsa20Processor.cpp @@ -2,24 +2,16 @@ #include -AbstractSalsa20Processor::AbstractSalsa20Processor(const int streamCount, const std::string& zoneName, const uint8_t* salsa20Key, const size_t keySize) +AbstractSalsa20Processor::AbstractSalsa20Processor(const unsigned streamCount, const std::string& zoneName, const uint8_t* salsa20Key, const unsigned keySize) : m_stream_count(streamCount), - m_stream_contexts(std::make_unique(streamCount)), - m_stream_block_indices(std::make_unique(streamCount)) + m_stream_contexts(streamCount), + m_block_hashes(BLOCK_HASHES_COUNT * streamCount * SHA1_HASH_SIZE), + m_stream_block_indices(streamCount) { - m_block_hashes = std::make_unique(BLOCK_HASHES_COUNT * streamCount * SHA1_HASH_SIZE); InitStreams(zoneName, salsa20Key, keySize); } -uint8_t* AbstractSalsa20Processor::GetHashBlock(const int streamNumber) const -{ - const auto blockIndexOffset = m_stream_block_indices[streamNumber] * m_stream_count * SHA1_HASH_SIZE; - const auto streamOffset = static_cast(streamNumber) * SHA1_HASH_SIZE; - - return &m_block_hashes[blockIndexOffset + streamOffset]; -} - -void AbstractSalsa20Processor::InitStreams(const std::string& zoneName, const uint8_t* salsa20Key, const size_t keySize) const +void AbstractSalsa20Processor::InitStreams(const std::string& zoneName, const uint8_t* salsa20Key, const size_t keySize) { // Original buffer must have been 32 bytes because the zoneName can at most be 31 characters be long before being cut off const auto zoneNameLength = std::min(zoneName.length(), 31uz); @@ -29,14 +21,14 @@ void AbstractSalsa20Processor::InitStreams(const std::string& zoneName, const ui assert(blockHashBufferSize % 4 == 0); size_t zoneNameOffset = 0; - for (size_t i = 0; i < blockHashBufferSize; i += 4) + for (auto i = 0uz; i < blockHashBufferSize; i += 4) { - *reinterpret_cast(&m_block_hashes[i]) = 0x1010101 * zoneName[zoneNameOffset++]; + memset(&m_block_hashes[i], zoneName[zoneNameOffset++], 4u); zoneNameOffset %= zoneNameLength; } - for (auto stream = 0; stream < m_stream_count; stream++) + for (auto stream = 0u; stream < m_stream_count; stream++) { m_stream_block_indices[stream] = 0; @@ -45,11 +37,19 @@ void AbstractSalsa20Processor::InitStreams(const std::string& zoneName, const ui } } +uint8_t* AbstractSalsa20Processor::GetHashBlock(const unsigned streamNumber) +{ + const auto blockIndexOffset = m_stream_block_indices[streamNumber] * m_stream_count * SHA1_HASH_SIZE; + const auto streamOffset = static_cast(streamNumber) * SHA1_HASH_SIZE; + + return &m_block_hashes[blockIndexOffset + streamOffset]; +} + void AbstractSalsa20Processor::GetCapturedData(const uint8_t** pCapturedData, size_t* pSize) { assert(pCapturedData != nullptr); assert(pSize != nullptr); - *pCapturedData = m_block_hashes.get(); + *pCapturedData = m_block_hashes.data(); *pSize = BLOCK_HASHES_COUNT * m_stream_count * SHA1_HASH_SIZE; } diff --git a/src/ZoneCommon/Zone/XChunk/AbstractSalsa20Processor.h b/src/ZoneCommon/Zone/XChunk/AbstractSalsa20Processor.h index 01d56a63..b5045c54 100644 --- a/src/ZoneCommon/Zone/XChunk/AbstractSalsa20Processor.h +++ b/src/ZoneCommon/Zone/XChunk/AbstractSalsa20Processor.h @@ -1,40 +1,22 @@ #pragma once #include "Cryptography.h" -#include "Utils/ClassUtils.h" #include "Utils/ICapturedDataProvider.h" #include #include #include +#include + +class Salsa20StreamContext +{ +public: + std::unique_ptr m_salsa20; + std::unique_ptr m_sha1; +}; class AbstractSalsa20Processor : public ICapturedDataProvider { -protected: - static constexpr int BLOCK_HASHES_COUNT = 200; - static constexpr int SHA1_HASH_SIZE = 20; - static constexpr int SALSA20_IV_SIZE = 8; - - class StreamContext - { - public: - std::unique_ptr m_salsa20; - std::unique_ptr m_sha1; - }; - - int m_stream_count; - std::unique_ptr m_stream_contexts; - - // m_block_hashes[BLOCK_HASHES_COUNT][numStreams][HASH_SIZE] - std::unique_ptr m_block_hashes; - std::unique_ptr m_stream_block_indices; - - AbstractSalsa20Processor(int streamCount, const std::string& zoneName, const uint8_t* salsa20Key, size_t keySize); - - _NODISCARD uint8_t* GetHashBlock(int streamNumber) const; - - void InitStreams(const std::string& zoneName, const uint8_t* salsa20Key, size_t keySize) const; - public: virtual ~AbstractSalsa20Processor() = default; AbstractSalsa20Processor(const AbstractSalsa20Processor& other) = delete; @@ -43,4 +25,21 @@ public: AbstractSalsa20Processor& operator=(AbstractSalsa20Processor&& other) noexcept = default; void GetCapturedData(const uint8_t** pCapturedData, size_t* pSize) override; + +protected: + static constexpr auto BLOCK_HASHES_COUNT = 200u; + static constexpr auto SHA1_HASH_SIZE = 20u; + static constexpr auto SALSA20_IV_SIZE = 8u; + + AbstractSalsa20Processor(unsigned streamCount, const std::string& zoneName, const uint8_t* salsa20Key, unsigned keySize); + void InitStreams(const std::string& zoneName, const uint8_t* salsa20Key, size_t keySize); + + [[nodiscard]] uint8_t* GetHashBlock(unsigned streamNumber); + + unsigned m_stream_count; + std::vector m_stream_contexts; + + // m_block_hashes[BLOCK_HASHES_COUNT][numStreams][SHA1_HASH_SIZE] + std::vector m_block_hashes; + std::vector m_stream_block_indices; }; diff --git a/src/ZoneCommon/Zone/XChunk/IXChunkProcessor.h b/src/ZoneCommon/Zone/XChunk/IXChunkProcessor.h index 22e444b1..bcc744ff 100644 --- a/src/ZoneCommon/Zone/XChunk/IXChunkProcessor.h +++ b/src/ZoneCommon/Zone/XChunk/IXChunkProcessor.h @@ -6,6 +6,12 @@ class IXChunkProcessor { public: + IXChunkProcessor() = default; virtual ~IXChunkProcessor() = default; - virtual size_t Process(int streamNumber, const uint8_t* input, size_t inputLength, uint8_t* output, size_t outputBufferSize) = 0; + IXChunkProcessor(const IXChunkProcessor& other) = default; + IXChunkProcessor(IXChunkProcessor&& other) noexcept = default; + IXChunkProcessor& operator=(const IXChunkProcessor& other) = default; + IXChunkProcessor& operator=(IXChunkProcessor&& other) noexcept = default; + + virtual size_t Process(unsigned streamNumber, const uint8_t* input, size_t inputLength, uint8_t* output, size_t outputBufferSize) = 0; }; diff --git a/src/ZoneCommon/Zone/XChunk/XChunkProcessorDeflate.cpp b/src/ZoneCommon/Zone/XChunk/XChunkProcessorDeflate.cpp index 5cff8933..ed546ab3 100644 --- a/src/ZoneCommon/Zone/XChunk/XChunkProcessorDeflate.cpp +++ b/src/ZoneCommon/Zone/XChunk/XChunkProcessorDeflate.cpp @@ -6,7 +6,7 @@ #include #include -size_t XChunkProcessorDeflate::Process(int streamNumber, const uint8_t* input, const size_t inputLength, uint8_t* output, const size_t outputBufferSize) +size_t XChunkProcessorDeflate::Process(unsigned streamNumber, const uint8_t* input, const size_t inputLength, uint8_t* output, const size_t outputBufferSize) { z_stream stream{}; stream.zalloc = Z_NULL; diff --git a/src/ZoneCommon/Zone/XChunk/XChunkProcessorDeflate.h b/src/ZoneCommon/Zone/XChunk/XChunkProcessorDeflate.h index 7c158c86..9dda1869 100644 --- a/src/ZoneCommon/Zone/XChunk/XChunkProcessorDeflate.h +++ b/src/ZoneCommon/Zone/XChunk/XChunkProcessorDeflate.h @@ -4,5 +4,5 @@ class XChunkProcessorDeflate final : public IXChunkProcessor { public: - size_t Process(int streamNumber, const uint8_t* input, size_t inputLength, uint8_t* output, size_t outputBufferSize) override; + size_t Process(unsigned streamNumber, const uint8_t* input, size_t inputLength, uint8_t* output, size_t outputBufferSize) override; }; diff --git a/src/ZoneCommon/Zone/XChunk/XChunkProcessorInflate.cpp b/src/ZoneCommon/Zone/XChunk/XChunkProcessorInflate.cpp index bc8420ff..a9fdec9d 100644 --- a/src/ZoneCommon/Zone/XChunk/XChunkProcessorInflate.cpp +++ b/src/ZoneCommon/Zone/XChunk/XChunkProcessorInflate.cpp @@ -2,10 +2,12 @@ #include "XChunkException.h" +#include +#include #include #include -size_t XChunkProcessorInflate::Process(int streamNumber, const uint8_t* input, const size_t inputLength, uint8_t* output, const size_t outputBufferSize) +size_t XChunkProcessorInflate::Process(unsigned streamNumber, const uint8_t* input, const size_t inputLength, uint8_t* output, const size_t outputBufferSize) { z_stream stream{}; stream.zalloc = Z_NULL; @@ -23,7 +25,10 @@ size_t XChunkProcessorInflate::Process(int streamNumber, const uint8_t* input, c ret = inflate(&stream, Z_FULL_FLUSH); if (ret != Z_STREAM_END) - throw XChunkException("Zone has invalid or unsupported compression. Inflate failed"); + { + std::cerr << std::format("inflate of stream failed with error code {}: {}\n", streamNumber, ret, stream.msg); + throw XChunkException(std::format("Zone has invalid or unsupported compression: {}", stream.msg)); + } const size_t outputSize = stream.total_out; diff --git a/src/ZoneCommon/Zone/XChunk/XChunkProcessorInflate.h b/src/ZoneCommon/Zone/XChunk/XChunkProcessorInflate.h index c328219f..c6e24a1c 100644 --- a/src/ZoneCommon/Zone/XChunk/XChunkProcessorInflate.h +++ b/src/ZoneCommon/Zone/XChunk/XChunkProcessorInflate.h @@ -4,5 +4,5 @@ class XChunkProcessorInflate final : public IXChunkProcessor { public: - size_t Process(int streamNumber, const uint8_t* input, size_t inputLength, uint8_t* output, size_t outputBufferSize) override; + size_t Process(unsigned streamNumber, const uint8_t* input, size_t inputLength, uint8_t* output, size_t outputBufferSize) override; }; diff --git a/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Decryption.cpp b/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Decryption.cpp index cf1bfb05..72de9270 100644 --- a/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Decryption.cpp +++ b/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Decryption.cpp @@ -4,9 +4,11 @@ #include "Cryptography.h" #include +#include +#include -XChunkProcessorSalsa20Decryption::XChunkProcessorSalsa20Decryption(const int streamCount, - std::string& zoneName, +XChunkProcessorSalsa20Decryption::XChunkProcessorSalsa20Decryption(const unsigned streamCount, + const std::string& zoneName, const uint8_t* salsa20Key, const size_t keySize) : AbstractSalsa20Processor(streamCount, zoneName, salsa20Key, keySize) @@ -14,9 +16,9 @@ XChunkProcessorSalsa20Decryption::XChunkProcessorSalsa20Decryption(const int str } size_t XChunkProcessorSalsa20Decryption::Process( - const int streamNumber, const uint8_t* input, const size_t inputLength, uint8_t* output, const size_t outputBufferSize) + const unsigned streamNumber, const uint8_t* input, const size_t inputLength, uint8_t* output, const size_t outputBufferSize) { - assert(streamNumber >= 0 && streamNumber < m_stream_count); + assert(streamNumber < m_stream_count); assert(input != nullptr); assert(output != nullptr); assert(inputLength <= outputBufferSize); diff --git a/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Decryption.h b/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Decryption.h index c5c57d51..fa32e9b0 100644 --- a/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Decryption.h +++ b/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Decryption.h @@ -7,7 +7,7 @@ class XChunkProcessorSalsa20Decryption final : public IXChunkProcessor, public AbstractSalsa20Processor { public: - XChunkProcessorSalsa20Decryption(int streamCount, std::string& zoneName, const uint8_t* salsa20Key, size_t keySize); + XChunkProcessorSalsa20Decryption(unsigned streamCount, const std::string& zoneName, const uint8_t* salsa20Key, size_t keySize); - size_t Process(int streamNumber, const uint8_t* input, size_t inputLength, uint8_t* output, size_t outputBufferSize) override; + size_t Process(unsigned streamNumber, const uint8_t* input, size_t inputLength, uint8_t* output, size_t outputBufferSize) override; }; diff --git a/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Encryption.cpp b/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Encryption.cpp index 80ca8928..43899e18 100644 --- a/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Encryption.cpp +++ b/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Encryption.cpp @@ -2,7 +2,7 @@ #include -XChunkProcessorSalsa20Encryption::XChunkProcessorSalsa20Encryption(const int streamCount, +XChunkProcessorSalsa20Encryption::XChunkProcessorSalsa20Encryption(const unsigned streamCount, const std::string& zoneName, const uint8_t* salsa20Key, const size_t keySize) @@ -11,14 +11,14 @@ XChunkProcessorSalsa20Encryption::XChunkProcessorSalsa20Encryption(const int str } size_t XChunkProcessorSalsa20Encryption::Process( - const int streamNumber, const uint8_t* input, const size_t inputLength, uint8_t* output, const size_t outputBufferSize) + const unsigned streamNumber, const uint8_t* input, const size_t inputLength, uint8_t* output, const size_t outputBufferSize) { - assert(streamNumber >= 0 && streamNumber < m_stream_count); + assert(streamNumber < m_stream_count); assert(input != nullptr); assert(output != nullptr); assert(inputLength <= outputBufferSize); - auto& streamContext = m_stream_contexts[streamNumber]; + const auto& streamContext = m_stream_contexts[streamNumber]; // Hash not yet encrypted XChunk uint8_t blockSha1Hash[SHA1_HASH_SIZE]; diff --git a/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Encryption.h b/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Encryption.h index 89272df3..12249613 100644 --- a/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Encryption.h +++ b/src/ZoneCommon/Zone/XChunk/XChunkProcessorSalsa20Encryption.h @@ -8,7 +8,7 @@ class XChunkProcessorSalsa20Encryption final : public IXChunkProcessor, public AbstractSalsa20Processor { public: - XChunkProcessorSalsa20Encryption(int streamCount, const std::string& zoneName, const uint8_t* salsa20Key, size_t keySize); + XChunkProcessorSalsa20Encryption(unsigned streamCount, const std::string& zoneName, const uint8_t* salsa20Key, size_t keySize); - size_t Process(int streamNumber, const uint8_t* input, size_t inputLength, uint8_t* output, size_t outputBufferSize) override; + size_t Process(unsigned streamNumber, const uint8_t* input, size_t inputLength, uint8_t* output, size_t outputBufferSize) override; }; diff --git a/src/ZoneLoading/Game/T6/ZoneLoaderFactoryT6.cpp b/src/ZoneLoading/Game/T6/ZoneLoaderFactoryT6.cpp index f014f4f6..7c0a9718 100644 --- a/src/ZoneLoading/Game/T6/ZoneLoaderFactoryT6.cpp +++ b/src/ZoneLoading/Game/T6/ZoneLoaderFactoryT6.cpp @@ -9,6 +9,7 @@ #include "Loading/Processor/ProcessorXChunks.h" #include "Loading/Steps/StepAddProcessor.h" #include "Loading/Steps/StepAllocXBlocks.h" +#include "Loading/Steps/StepDumpData.h" #include "Loading/Steps/StepLoadSignature.h" #include "Loading/Steps/StepLoadZoneContent.h" #include "Loading/Steps/StepLoadZoneSizes.h" @@ -16,16 +17,20 @@ #include "Loading/Steps/StepVerifyFileName.h" #include "Loading/Steps/StepVerifyMagic.h" #include "Loading/Steps/StepVerifySignature.h" -#include "Utils/ClassUtils.h" +#include "Utils/Endianness.h" #include "Zone/XChunk/XChunkProcessorInflate.h" #include "Zone/XChunk/XChunkProcessorSalsa20Decryption.h" #include +#include #include +#include +#include #include #include using namespace T6; +namespace fs = std::filesystem; namespace { @@ -44,46 +49,53 @@ namespace return GameLanguage::LANGUAGE_NONE; } - bool CanLoad(const ZoneHeader& header, bool* isSecure, bool* isOfficial, bool* isEncrypted) + bool CanLoad(const ZoneHeader& header, bool& isBigEndian, bool& isSecure, bool& isOfficial, bool& isEncrypted) { - assert(isSecure != nullptr); - assert(isOfficial != nullptr); - - if (header.m_version != ZoneConstants::ZONE_VERSION) + if (endianness::FromLittleEndian(header.m_version) == ZoneConstants::ZONE_VERSION_PC) { - return false; + isBigEndian = false; + if (!memcmp(header.m_magic, ZoneConstants::MAGIC_SIGNED_PC_TREYARCH, 8)) + { + isSecure = true; + isOfficial = true; + isEncrypted = true; + return true; + } + + if (!memcmp(header.m_magic, ZoneConstants::MAGIC_SIGNED_PC_OAT, 8)) + { + isSecure = true; + isOfficial = false; + isEncrypted = true; + return true; + } + + if (!memcmp(header.m_magic, ZoneConstants::MAGIC_UNSIGNED, 8)) + { + isSecure = false; + isOfficial = true; + isEncrypted = true; + return true; + } + + if (!memcmp(header.m_magic, ZoneConstants::MAGIC_UNSIGNED_SERVER, 8)) + { + isSecure = false; + isOfficial = true; + isEncrypted = false; + return true; + } } - - if (!memcmp(header.m_magic, ZoneConstants::MAGIC_SIGNED_TREYARCH, 8)) + else if (endianness::FromBigEndian(header.m_version) == ZoneConstants::ZONE_VERSION_XENON) { - *isSecure = true; - *isOfficial = true; - *isEncrypted = true; - return true; - } - - if (!memcmp(header.m_magic, ZoneConstants::MAGIC_SIGNED_OAT, 8)) - { - *isSecure = true; - *isOfficial = false; - *isEncrypted = true; - return true; - } - - if (!memcmp(header.m_magic, ZoneConstants::MAGIC_UNSIGNED, 8)) - { - *isSecure = false; - *isOfficial = true; - *isEncrypted = true; - return true; - } - - if (!memcmp(header.m_magic, ZoneConstants::MAGIC_UNSIGNED_SERVER, 8)) - { - *isSecure = false; - *isOfficial = true; - *isEncrypted = false; - return true; + isBigEndian = true; + if (!memcmp(header.m_magic, ZoneConstants::MAGIC_SIGNED_XENON_TREYARCH, 8)) + { + isSecure = true; + isOfficial = true; + isEncrypted = true; + return true; + } } return false; @@ -145,16 +157,21 @@ namespace return signatureLoadStepPtr; } - ICapturedDataProvider* AddXChunkProcessor(const bool isEncrypted, ZoneLoader& zoneLoader, std::string& fileName) + ICapturedDataProvider* AddXChunkProcessor(const bool isBigEndian, const bool isEncrypted, ZoneLoader& zoneLoader, std::string& fileName) { ICapturedDataProvider* result = nullptr; - auto xChunkProcessor = processor::CreateProcessorXChunks(ZoneConstants::STREAM_COUNT, ZoneConstants::XCHUNK_SIZE, ZoneConstants::VANILLA_BUFFER_SIZE); + auto xChunkProcessor = processor::CreateProcessorXChunks(ZoneConstants::STREAM_COUNT, + ZoneConstants::XCHUNK_SIZE, + isBigEndian ? GameEndianness::BIG_ENDIAN : GameEndianness::LITTLE_ENDIAN, + ZoneConstants::VANILLA_BUFFER_SIZE); + + const uint8_t (&salsa20Key)[32] = isBigEndian ? ZoneConstants::SALSA20_KEY_TREYARCH_XENON : ZoneConstants::SALSA20_KEY_TREYARCH_PC; if (isEncrypted) { // If zone is encrypted, the decryption is applied before the decompression. T6 Zones always use Salsa20. - auto chunkProcessorSalsa20 = std::make_unique( - ZoneConstants::STREAM_COUNT, fileName, ZoneConstants::SALSA20_KEY_TREYARCH, sizeof(ZoneConstants::SALSA20_KEY_TREYARCH)); + auto chunkProcessorSalsa20 = + std::make_unique(ZoneConstants::STREAM_COUNT, fileName, salsa20Key, sizeof(salsa20Key)); result = chunkProcessorSalsa20.get(); xChunkProcessor->AddChunkProcessor(std::move(chunkProcessorSalsa20)); } @@ -170,12 +187,10 @@ namespace std::unique_ptr ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& header, std::string& fileName) const { - bool isSecure; - bool isOfficial; - bool isEncrypted; + bool isBigEndian, isSecure, isOfficial, isEncrypted; // Check if this file is a supported T6 zone. - if (!CanLoad(header, &isSecure, &isOfficial, &isEncrypted)) + if (!CanLoad(header, isBigEndian, isSecure, isOfficial, isEncrypted)) return nullptr; // Create new zone @@ -196,26 +211,37 @@ std::unique_ptr ZoneLoaderFactory::CreateLoaderForHeader(ZoneHeader& ISignatureProvider* signatureProvider = AddAuthHeaderSteps(isSecure, *zoneLoader, fileName); // Setup loading XChunks from the zone from this point on. - ICapturedDataProvider* signatureDataProvider = AddXChunkProcessor(isEncrypted, *zoneLoader, fileName); + ICapturedDataProvider* signatureDataProvider = AddXChunkProcessor(isBigEndian, isEncrypted, *zoneLoader, fileName); - // Start of the XFile struct - zoneLoader->AddLoadingStep(step::CreateStepLoadZoneSizes()); - zoneLoader->AddLoadingStep(step::CreateStepAllocXBlocks()); - - // Start of the zone content - zoneLoader->AddLoadingStep(step::CreateStepLoadZoneContent( - [zonePtr](ZoneInputStream& stream) - { - return std::make_unique(*zonePtr, stream); - }, - 32u, - ZoneConstants::OFFSET_BLOCK_BIT_COUNT, - ZoneConstants::INSERT_BLOCK, - zonePtr->Memory())); - - if (isSecure) + if (!isBigEndian) { - zoneLoader->AddLoadingStep(step::CreateStepVerifySignature(std::move(rsa), signatureProvider, signatureDataProvider)); + // Start of the XFile struct + zoneLoader->AddLoadingStep(step::CreateStepLoadZoneSizes()); + zoneLoader->AddLoadingStep(step::CreateStepAllocXBlocks()); + + // Start of the zone content + zoneLoader->AddLoadingStep(step::CreateStepLoadZoneContent( + [zonePtr](ZoneInputStream& stream) + { + return std::make_unique(*zonePtr, stream); + }, + 32u, + ZoneConstants::OFFSET_BLOCK_BIT_COUNT, + ZoneConstants::INSERT_BLOCK, + zonePtr->Memory())); + + if (isSecure) + { + zoneLoader->AddLoadingStep(step::CreateStepVerifySignature(std::move(rsa), signatureProvider, signatureDataProvider)); + } + } + else + { + fs::path dumpFileNamePath = fs::path(fileName).filename(); + dumpFileNamePath.replace_extension(".dat"); + std::string dumpFileName = dumpFileNamePath.string(); + std::cerr << std::format("Dumping xbox assets is not supported, making a full fastfile data dump to {}\n", dumpFileName); + zoneLoader->AddLoadingStep(step::CreateStepDumpData(dumpFileName, 0xFFFFFFFF)); } return zoneLoader; diff --git a/src/ZoneLoading/Loading/Processor/ProcessorXChunks.cpp b/src/ZoneLoading/Loading/Processor/ProcessorXChunks.cpp index 6d73eafc..cb57c662 100644 --- a/src/ZoneLoading/Loading/Processor/ProcessorXChunks.cpp +++ b/src/ZoneLoading/Loading/Processor/ProcessorXChunks.cpp @@ -1,11 +1,14 @@ #include "ProcessorXChunks.h" #include "Loading/Exception/InvalidChunkSizeException.h" +#include "Utils/Endianness.h" #include "Zone/ZoneTypes.h" #include #include #include +#include +#include #include #include #include @@ -125,8 +128,9 @@ namespace class ProcessorXChunks final : public processor::IProcessorXChunks { public: - ProcessorXChunks(const int numStreams, const size_t xChunkSize, const std::optional vanillaBufferSize) + ProcessorXChunks(const int numStreams, const size_t xChunkSize, const GameEndianness endianness, const std::optional vanillaBufferSize) : m_chunk_size(xChunkSize), + m_endianness(endianness), m_vanilla_buffer_size(vanillaBufferSize), m_initialized_streams(false), m_current_stream(0), @@ -227,6 +231,11 @@ namespace return; } + if (m_endianness == GameEndianness::LITTLE_ENDIAN) + chunkSize = endianness::FromLittleEndian(chunkSize); + else + chunkSize = endianness::FromBigEndian(chunkSize); + if (chunkSize > m_chunk_size) { throw InvalidChunkSizeException(chunkSize, m_chunk_size); @@ -280,6 +289,7 @@ namespace std::vector> m_streams; size_t m_chunk_size; + GameEndianness m_endianness; std::optional m_vanilla_buffer_size; std::vector> m_chunk_processors; @@ -297,13 +307,14 @@ namespace namespace processor { - std::unique_ptr CreateProcessorXChunks(int numStreams, const size_t xChunkSize) + std::unique_ptr CreateProcessorXChunks(unsigned numStreams, const size_t xChunkSize, const GameEndianness endianness) { - return std::make_unique(numStreams, xChunkSize, std::nullopt); + return std::make_unique(numStreams, xChunkSize, endianness, std::nullopt); } - std::unique_ptr CreateProcessorXChunks(int numStreams, const size_t xChunkSize, const size_t vanillaBufferSize) + std::unique_ptr + CreateProcessorXChunks(unsigned numStreams, const size_t xChunkSize, GameEndianness endianness, const size_t vanillaBufferSize) { - return std::make_unique(numStreams, xChunkSize, vanillaBufferSize); + return std::make_unique(numStreams, xChunkSize, endianness, vanillaBufferSize); } } // namespace processor diff --git a/src/ZoneLoading/Loading/Processor/ProcessorXChunks.h b/src/ZoneLoading/Loading/Processor/ProcessorXChunks.h index dec161af..6a876231 100644 --- a/src/ZoneLoading/Loading/Processor/ProcessorXChunks.h +++ b/src/ZoneLoading/Loading/Processor/ProcessorXChunks.h @@ -1,4 +1,6 @@ #pragma once + +#include "Game/IGame.h" #include "Loading/StreamProcessor.h" #include "Zone/XChunk/IXChunkProcessor.h" @@ -12,6 +14,6 @@ namespace processor virtual void AddChunkProcessor(std::unique_ptr chunkProcessor) = 0; }; - std::unique_ptr CreateProcessorXChunks(int numStreams, size_t xChunkSize); - std::unique_ptr CreateProcessorXChunks(int numStreams, size_t xChunkSize, size_t vanillaBufferSize); + std::unique_ptr CreateProcessorXChunks(unsigned numStreams, size_t xChunkSize, GameEndianness endianness); + std::unique_ptr CreateProcessorXChunks(unsigned numStreams, size_t xChunkSize, GameEndianness endianness, size_t vanillaBufferSize); } // namespace processor diff --git a/src/ZoneLoading/Loading/Steps/StepDumpData.cpp b/src/ZoneLoading/Loading/Steps/StepDumpData.cpp index 3999dc4d..906fce4b 100644 --- a/src/ZoneLoading/Loading/Steps/StepDumpData.cpp +++ b/src/ZoneLoading/Loading/Steps/StepDumpData.cpp @@ -7,17 +7,18 @@ namespace class StepDumpData final : public ILoadingStep { public: - explicit StepDumpData(const size_t dumpCount) - : m_dump_count(dumpCount) + StepDumpData(std::string fileName, const size_t dumpCount) + : m_file_name(std::move(fileName)), + m_dump_count(dumpCount) { } void PerformStep(ZoneLoader& zoneLoader, ILoadingStream& stream) override { - uint8_t tempBuffer[128]; + uint8_t tempBuffer[0x1000]; auto dumpedBytes = 0uz; - std::ofstream tempFile("dump.dat", std::fstream::out | std::fstream::binary); + std::ofstream tempFile(m_file_name, std::fstream::out | std::fstream::binary); while (dumpedBytes < m_dump_count) { @@ -45,14 +46,15 @@ namespace } private: + std::string m_file_name; size_t m_dump_count; }; } // namespace namespace step { - std::unique_ptr CreateStepDumpData(size_t dumpCount) + std::unique_ptr CreateStepDumpData(std::string fileName, size_t dumpCount) { - return std::make_unique(dumpCount); + return std::make_unique(std::move(fileName), dumpCount); } } // namespace step diff --git a/src/ZoneLoading/Loading/Steps/StepDumpData.h b/src/ZoneLoading/Loading/Steps/StepDumpData.h index 07beac1a..a174a3a3 100644 --- a/src/ZoneLoading/Loading/Steps/StepDumpData.h +++ b/src/ZoneLoading/Loading/Steps/StepDumpData.h @@ -3,8 +3,9 @@ #include "Loading/ILoadingStep.h" #include +#include namespace step { - std::unique_ptr CreateStepDumpData(size_t dumpCount); + std::unique_ptr CreateStepDumpData(std::string fileName, size_t dumpCount); } diff --git a/src/ZoneWriting/Game/T6/ZoneWriterFactoryT6.cpp b/src/ZoneWriting/Game/T6/ZoneWriterFactoryT6.cpp index 3d0d1d70..5edf9dcf 100644 --- a/src/ZoneWriting/Game/T6/ZoneWriterFactoryT6.cpp +++ b/src/ZoneWriting/Game/T6/ZoneWriterFactoryT6.cpp @@ -44,14 +44,14 @@ namespace ZoneHeader CreateHeaderForParams(const bool isSecure, const bool isOfficial, const bool isEncrypted) { ZoneHeader header{}; - header.m_version = ZoneConstants::ZONE_VERSION; + header.m_version = ZoneConstants::ZONE_VERSION_PC; if (isSecure) { if (isOfficial) - memcpy(header.m_magic, ZoneConstants::MAGIC_SIGNED_TREYARCH, sizeof(ZoneHeader::m_magic)); + memcpy(header.m_magic, ZoneConstants::MAGIC_SIGNED_PC_TREYARCH, sizeof(ZoneHeader::m_magic)); else - memcpy(header.m_magic, ZoneConstants::MAGIC_SIGNED_OAT, sizeof(ZoneHeader::m_magic)); + memcpy(header.m_magic, ZoneConstants::MAGIC_SIGNED_PC_OAT, sizeof(ZoneHeader::m_magic)); } else { @@ -82,7 +82,7 @@ namespace { // If zone is encrypted, the decryption is applied before the decompression. T6 Zones always use Salsa20. auto chunkProcessorSalsa20 = std::make_unique( - ZoneConstants::STREAM_COUNT, zone.m_name, ZoneConstants::SALSA20_KEY_TREYARCH, sizeof(ZoneConstants::SALSA20_KEY_TREYARCH)); + ZoneConstants::STREAM_COUNT, zone.m_name, ZoneConstants::SALSA20_KEY_TREYARCH_PC, sizeof(ZoneConstants::SALSA20_KEY_TREYARCH_PC)); // If there is encryption, the signed data of the zone is the final hash blocks provided by the Salsa20 IV adaption algorithm if (dataToSignProviderPtr)