From 5df5d5e1c2acbcabace6936ee7b763f7995f9d0a Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Mon, 27 Feb 2023 19:50:41 -0400 Subject: PSOV3Encryption: Add, and fully match `PSOV3Encryption` Boy, that took way too long to match, mainly due to regalloc issues in `update_stream()`. --- context.h | 28 ++++++++++++++++ include/pso/PSOV3Encryption.h | 36 ++++++++++++++++++++ obj_files.mk | 1 + src/pso/PSOV3Encryption.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 include/pso/PSOV3Encryption.h create mode 100644 src/pso/PSOV3Encryption.cpp diff --git a/context.h b/context.h index de848b6..142e060 100644 --- a/context.h +++ b/context.h @@ -829,3 +829,31 @@ private: return this->task_flags & flags; }; }; + +// pso/PSOV3Encryption.h +class PSOEncryption { +public: + PSOEncryption(); + virtual void update_stream() = 0; + virtual ~PSOEncryption(); + virtual void encrypt(u32 seed) = 0; + virtual u32 next() = 0; +}; + +class PSOV3Encryption : public PSOEncryption { +public: + PSOV3Encryption(); + virtual void update_stream() override; + virtual ~PSOV3Encryption(); + virtual void encrypt(u32 seed) override; + virtual u32 next() override; + + PRIVATE_MEMBER_ACCESSORS_ARRAY(u32, buffer, 522); + PRIVATE_MEMBER_ACCESSORS(u32 *, buffer_start); + PRIVATE_MEMBER_ACCESSORS(u32 *, buffer_end); + +private: + TArray m_buffer; + u32 *m_buffer_start; + u32 *m_buffer_end; +}; diff --git a/include/pso/PSOV3Encryption.h b/include/pso/PSOV3Encryption.h new file mode 100644 index 0000000..5def0f2 --- /dev/null +++ b/include/pso/PSOV3Encryption.h @@ -0,0 +1,36 @@ +#ifndef PSOV3ENCRYPTION_H +#define PSOV3ENCRYPTION_H + +#include +#include +#include +#include + +class PSOEncryption { +public: + PSOEncryption(); + virtual void update_stream() = 0; + virtual ~PSOEncryption(); + virtual void encrypt(u32 seed) = 0; + virtual u32 next() = 0; +}; + +class PSOV3Encryption : public PSOEncryption { +public: + PSOV3Encryption(); + virtual void update_stream() override; + virtual ~PSOV3Encryption(); + virtual void encrypt(u32 seed) override; + virtual u32 next() override; + + PRIVATE_MEMBER_ACCESSORS_ARRAY(u32, buffer, 522); + PRIVATE_MEMBER_ACCESSORS(u32 *, buffer_start); + PRIVATE_MEMBER_ACCESSORS(u32 *, buffer_end); + +private: + TArray m_buffer; + u32 *m_buffer_start; + u32 *m_buffer_end; +}; + +#endif diff --git a/obj_files.mk b/obj_files.mk index 2098c26..522cf50 100644 --- a/obj_files.mk +++ b/obj_files.mk @@ -6,6 +6,7 @@ O_FILES := $(BUILD_DIR)/src/main.o \ $(BUILD_DIR)/src/pso/TMainTask.o \ $(BUILD_DIR)/src/pso/TObject.o \ $(BUILD_DIR)/src/pso/TObject2.o \ + $(BUILD_DIR)/src/pso/PSOV3Encryption.o \ $(BUILD_DIR)/src/Dolphin/PPCArch.o \ $(BUILD_DIR)/src/Dolphin/db.o \ $(BUILD_DIR)/src/Dolphin/os/OS.o \ diff --git a/src/pso/PSOV3Encryption.cpp b/src/pso/PSOV3Encryption.cpp new file mode 100644 index 0000000..465249e --- /dev/null +++ b/src/pso/PSOV3Encryption.cpp @@ -0,0 +1,77 @@ +#include +#include +#include "pso/macros.h" +#include "pso/PSOV3Encryption.h" +#include "pso/TArray.h" + +u32 PSOV3Encryption::next() { + if (++m_buffer_start == m_buffer_end) { + update_stream(); + } + return *m_buffer_start; +} + +void PSOV3Encryption::update_stream() { + u32 *ptr; + u32 *first_end; + u32 *start; + + start = m_buffer.start(); + m_buffer_start = m_buffer.start(); + + for (ptr = m_buffer.start(), first_end = &ptr[489]; first_end != m_buffer_end;) { + *ptr++ ^= *first_end++; + } + + for (u32 *p = ptr; p != m_buffer_end; *p++ ^= *start++); +} + +void PSOV3Encryption::encrypt(u32 seed) { + const size_t size = m_buffer.size(); + u32 thing; + m_buffer_end = &m_buffer[size]; + m_buffer_start = m_buffer.start(); + u32 value = 0; + for (int i = 0; i <= 16; ++i, *m_buffer_start++ = value) { + for (int j = 32; j; --j) { + seed *= 0x5d588b65; + value = (++seed & (1 << 31)) | (value >> 1); + } + } + + //u32 *smth = &m_buffer[0]; + //u32 *smth2 = &m_buffer[1]; + --m_buffer_start; + thing = m_buffer[0xf]; + *m_buffer_start = (m_buffer[0] >> 9) ^ (*m_buffer_start << 23) ^ thing; + //u32 *buf_val = &m_buffer[0]; + //u32 *next_buf_val = &m_buffer[1]; + + for (u32 *buf_val = &m_buffer[0], *next_buf_val = &m_buffer[1], *buf = m_buffer_start++; m_buffer_start != m_buffer_end;) { + *m_buffer_start++ = (*buf_val++ << 23) ^ (*next_buf_val++ >> 9) ^ *buf++; + } + + update_stream(); + update_stream(); + update_stream(); + m_buffer_start = m_buffer.end(); +} + +PSOV3Encryption::~PSOV3Encryption() { + m_buffer.fill(0); + m_buffer_start = nullptr; + m_buffer_end = nullptr; +} + +PSOV3Encryption::PSOV3Encryption() : PSOEncryption() { + encrypt(0); +} + + +PSOEncryption::~PSOEncryption() { + +} + +PSOEncryption::PSOEncryption() { + +} -- cgit v1.2.3-13-gbd6f