diff --git a/Source/Archive/GPak.cpp b/Source/Archive/GPak.cpp index 25c3fea7e..d46da769b 100644 --- a/Source/Archive/GPak.cpp +++ b/Source/Archive/GPak.cpp @@ -129,8 +129,8 @@ namespace { static_assert(offsetof(PakAuxiliaryFile, identifier) == 4); // INLINED - std::uint8_t SwapNibbles(std::uint8_t byte) { - return static_cast((byte << 4) | (byte >> 4)); + char SwapNibbles(char c) { + return static_cast((c << 4) | ((c >> 4) & 0x0F)); } __declspec(noinline) std::uint64_t ReadAuxiliaryIdentifier(const void* data, std::size_t size); @@ -541,49 +541,47 @@ struct VectorUint32_654D10 { // FUNCTION: fn_05051 __declspec(noinline) void VectorUint32_654D10::AssignFill(unsigned int count, unsigned int val) { unsigned int old_size = static_cast(this->last - this->first); - if (count <= old_size) { - FillUint32(this->first, this->last, val); - return; - } - if (count > 0x3fffffff) { - std::_Throw_bad_array_new_length(); - } - unsigned int bytes = count * sizeof(unsigned int); - unsigned int* new_ptr; - if (bytes < 0x1000) { - if (bytes == 0) { - new_ptr = nullptr; - } else { - new_ptr = static_cast(::operator new(bytes)); - } - } else { - if (bytes + 0x23 <= bytes) { + if (old_size < count) { + if (count > 0x3fffffff) { std::_Throw_bad_array_new_length(); } - void* raw = ::operator new(bytes + 0x23); - if (!raw) { - _invalid_parameter_noinfo_noreturn(); - } - new_ptr = reinterpret_cast((reinterpret_cast(raw) + 0x23) & ~static_cast(0x1f)); - reinterpret_cast(new_ptr)[-1] = raw; - } - if (old_size != 0) { - unsigned int old_bytes = old_size * sizeof(unsigned int); - void* old_raw = this->first; - if (old_bytes >= 0x1000) { - old_raw = reinterpret_cast(this->first)[-1]; - if ((reinterpret_cast(this->first) - reinterpret_cast(old_raw) - 4) > 0x1f) { + unsigned int bytes = count * sizeof(unsigned int); + unsigned int* new_ptr; + if (bytes >= 0x1000) { + if (bytes + 0x23 <= bytes) { + std::_Throw_bad_array_new_length(); + } + void* raw = ::operator new(bytes + 0x23); + if (!raw) { _invalid_parameter_noinfo_noreturn(); } - old_bytes += 0x23; + new_ptr = reinterpret_cast((reinterpret_cast(raw) + 0x23) & ~static_cast(0x1f)); + reinterpret_cast(new_ptr)[-1] = raw; + } else if (bytes != 0) { + new_ptr = static_cast(::operator new(bytes)); + } else { + new_ptr = nullptr; } - ::operator delete(old_raw, old_bytes); - } - this->first = new_ptr; - this->last = new_ptr + count; - this->end = new_ptr + count; - for (unsigned int* p = new_ptr; p != this->last; ++p) { - *p = val; + if (old_size != 0) { + unsigned int old_bytes = old_size * sizeof(unsigned int); + void* old_raw = this->first; + if (old_bytes >= 0x1000) { + old_raw = reinterpret_cast(this->first)[-1]; + if ((reinterpret_cast(this->first) - reinterpret_cast(old_raw) - 4) > 0x1f) { + _invalid_parameter_noinfo_noreturn(); + } + old_bytes += 0x23; + } + ::operator delete(old_raw, old_bytes); + } + this->first = new_ptr; + this->last = new_ptr + count; + this->end = new_ptr + count; + for (unsigned int* p = new_ptr; p != this->last; ++p) { + *p = val; + } + } else { + FillUint32(this->first, this->last, val); } } @@ -604,49 +602,47 @@ struct VectorInt32_654E00 { // FUNCTION: fn_05052 __declspec(noinline) void VectorInt32_654E00::AssignFill(unsigned int count, int val) { unsigned int old_size = static_cast(this->last - this->first); - if (count <= old_size) { - FillInt32(this->first, this->last, val); - return; - } - if (count > 0x3fffffff) { - std::_Throw_bad_array_new_length(); - } - unsigned int bytes = count * sizeof(int); - int* new_ptr; - if (bytes < 0x1000) { - if (bytes == 0) { - new_ptr = nullptr; - } else { - new_ptr = static_cast(::operator new(bytes)); - } - } else { - if (bytes + 0x23 <= bytes) { + if (old_size < count) { + if (count > 0x3fffffff) { std::_Throw_bad_array_new_length(); } - void* raw = ::operator new(bytes + 0x23); - if (!raw) { - _invalid_parameter_noinfo_noreturn(); - } - new_ptr = reinterpret_cast((reinterpret_cast(raw) + 0x23) & ~static_cast(0x1f)); - reinterpret_cast(new_ptr)[-1] = raw; - } - if (old_size != 0) { - unsigned int old_bytes = old_size * sizeof(int); - void* old_raw = this->first; - if (old_bytes >= 0x1000) { - old_raw = reinterpret_cast(this->first)[-1]; - if ((reinterpret_cast(this->first) - reinterpret_cast(old_raw) - 4) > 0x1f) { + unsigned int bytes = count * sizeof(int); + int* new_ptr; + if (bytes >= 0x1000) { + if (bytes + 0x23 <= bytes) { + std::_Throw_bad_array_new_length(); + } + void* raw = ::operator new(bytes + 0x23); + if (!raw) { _invalid_parameter_noinfo_noreturn(); } - old_bytes += 0x23; + new_ptr = reinterpret_cast((reinterpret_cast(raw) + 0x23) & ~static_cast(0x1f)); + reinterpret_cast(new_ptr)[-1] = raw; + } else if (bytes != 0) { + new_ptr = static_cast(::operator new(bytes)); + } else { + new_ptr = nullptr; } - ::operator delete(old_raw, old_bytes); - } - this->first = new_ptr; - this->last = new_ptr + count; - this->end = new_ptr + count; - for (int* p = new_ptr; p != this->last; ++p) { - *p = val; + if (old_size != 0) { + unsigned int old_bytes = old_size * sizeof(int); + void* old_raw = this->first; + if (old_bytes >= 0x1000) { + old_raw = reinterpret_cast(this->first)[-1]; + if ((reinterpret_cast(this->first) - reinterpret_cast(old_raw) - 4) > 0x1f) { + _invalid_parameter_noinfo_noreturn(); + } + old_bytes += 0x23; + } + ::operator delete(old_raw, old_bytes); + } + this->first = new_ptr; + this->last = new_ptr + count; + this->end = new_ptr + count; + for (int* p = new_ptr; p != this->last; ++p) { + *p = val; + } + } else { + FillInt32(this->first, this->last, val); } } @@ -1347,7 +1343,7 @@ bool CGPak::ReadTableV1() { std::int32_t length = static_cast(std::strlen(reinterpret_cast(cursor)) + 1); for (std::int32_t j = 0; j < length; ++j) { - name[j] = static_cast(SwapNibbles(cursor[j])); + name[j] = SwapNibbles(static_cast(cursor[j])); } } else { if (this->m_version > 0x103) { @@ -1358,7 +1354,7 @@ bool CGPak::ReadTableV1() { std::int32_t length = static_cast(nameSize - 6); for (std::int32_t j = 0; j < length; ++j) { - name[j] = static_cast(SwapNibbles(cursor[j])); + name[j] = SwapNibbles(static_cast(cursor[j])); } std::int32_t number = legacyKey; @@ -2221,6 +2217,7 @@ namespace GPakEx { Base() : m_key{} { } + std::size_t BlockSize() const override; std::size_t OptimalDataAlignment() const override; void SetKey(std::span key) override; @@ -3434,18 +3431,19 @@ __forceinline bool DecryptWithAes(PakBlockTransform transform, std::uint32_t key AES cipher; bool result = false; if (cipher.Initialize(const_cast(key.data()), key.size(), std::span(iv.data(), iv.size()), mode)) { + const std::size_t encryptedSize = chunkSize + 16; if (input.size() <= prefixSize) { XorWithKey16(mask, input, output); result = true; + } else if (input.size() <= prefixSize + encryptedSize) { + XorWithKey16(mask, input.first(prefixSize), output.first(prefixSize)); + const std::span remaining = output.subspan(prefixSize); + if (cipher.Decrypt(const_cast(input.data()) + prefixSize, input.size() - prefixSize, remaining.data(), remaining.size(), 1) == remaining.size()) { + result = true; + } } else { XorWithKey16(mask, input.first(prefixSize), output.first(prefixSize)); - const std::size_t encryptedSize = chunkSize + 16; - if (input.size() <= prefixSize + encryptedSize) { - const std::span remaining = output.subspan(prefixSize); - if (cipher.Decrypt(const_cast(input.data()) + prefixSize, input.size() - prefixSize, remaining.data(), remaining.size(), 1) == remaining.size()) { - result = true; - } - } else if (cipher.Decrypt(const_cast(input.data()) + prefixSize, encryptedSize, output.data() + prefixSize, chunkSize, 1) == chunkSize) { + if (cipher.Decrypt(const_cast(input.data()) + prefixSize, encryptedSize, output.data() + prefixSize, chunkSize, 1) == chunkSize) { XorWithKey16(mask, input.subspan(prefixSize + encryptedSize), output.subspan(prefixSize + chunkSize)); result = true; } @@ -3550,7 +3548,7 @@ bool PakDecryptor::Decrypt00F(std::span data, std::span __forceinline bool DecryptWithCipherMode(PakBlockTransform transform, std::uint32_t keyOffset, std::uint32_t keySize, std::uint32_t ivOffset, std::uint32_t ivSize, std::size_t prefixSize, std::span data, std::span seed, - std::span block, const std::span& input, std::span output) { + std::span block, const std::span& input, const std::span& output) { if (input.empty()) { return true; } @@ -3571,12 +3569,15 @@ __forceinline bool DecryptWithCipherMode(PakBlockTransform transform, std::uint3 if (input.size() <= prefixSize) { XorWithKey16(mask, input, output); succeeded = true; + } else if (input.size() <= prefixSize + encryptedSize) { + XorWithKey16(mask, input.first(prefixSize), output.first(prefixSize)); + const std::span remaining = output.subspan(prefixSize); + if (cipher.Process(input.subspan(prefixSize), remaining, true) == remaining.size()) { + succeeded = true; + } } else { XorWithKey16(mask, input.first(prefixSize), output.first(prefixSize)); - if (input.size() <= prefixSize + encryptedSize) { - const std::span remaining = output.subspan(prefixSize); - succeeded = cipher.Process(input.subspan(prefixSize), remaining, true) == remaining.size(); - } else if (cipher.Process(input.subspan(prefixSize, encryptedSize), output.subspan(prefixSize, 0x400), true) == 0x400) { + if (cipher.Process(input.subspan(prefixSize, encryptedSize), output.subspan(prefixSize, 0x400), true) == 0x400) { XorWithKey16(mask, input.subspan(prefixSize + encryptedSize), output.subspan(prefixSize + 0x400)); succeeded = true; }