From ae9604faba4fe1b43879efb24665e717397c094d Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Fri, 5 Jul 2019 18:50:50 -0400 Subject: [PATCH] pl_u: Expose method to encrypt TTF to BFTTF --- src/core/hle/service/ns/pl_u.cpp | 26 ++++++++++++-------------- src/core/hle/service/ns/pl_u.h | 2 ++ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/core/hle/service/ns/pl_u.cpp b/src/core/hle/service/ns/pl_u.cpp index 2a522136d..6d6fdb4ed 100644 --- a/src/core/hle/service/ns/pl_u.cpp +++ b/src/core/hle/service/ns/pl_u.cpp @@ -6,13 +6,6 @@ #include #include -#include -#include -#include -#include -#include -#include - #include "common/assert.h" #include "common/common_paths.h" #include "common/common_types.h" @@ -96,13 +89,18 @@ static void DecryptSharedFont(const std::vector& input, Kernel::PhysicalMem static void EncryptSharedFont(const std::vector& input, Kernel::PhysicalMemory& output, std::size_t& offset) { - ASSERT_MSG(offset + input.size() + 8 < SHARED_FONT_MEM_SIZE, "Shared fonts exceeds 17mb!"); - const u32 KEY = EXPECTED_MAGIC ^ EXPECTED_RESULT; - std::memcpy(output.data() + offset, &EXPECTED_RESULT, sizeof(u32)); // Magic header - const u32 ENC_SIZE = static_cast(input.size()) ^ KEY; - std::memcpy(output.data() + offset + sizeof(u32), &ENC_SIZE, sizeof(u32)); - std::memcpy(output.data() + offset + (sizeof(u32) * 2), input.data(), input.size()); - offset += input.size() + (sizeof(u32) * 2); + ASSERT_MSG(offset + (input.size() * sizeof(u32)) < SHARED_FONT_MEM_SIZE, + "Shared fonts exceeds 17mb!"); + + const auto key = Common::swap32(EXPECTED_RESULT ^ EXPECTED_MAGIC); + std::vector transformed_font(input.size() + 2); + transformed_font[0] = Common::swap32(EXPECTED_MAGIC); + transformed_font[1] = Common::swap32(input.size() * sizeof(u32)) ^ key; + std::transform(input.begin(), input.end(), transformed_font.begin() + 2, + [key](u32 in) { return in ^ key; }); + std::memcpy(output.data() + offset, transformed_font.data(), + transformed_font.size() * sizeof(u32)); + offset += transformed_font.size() * sizeof(u32); } // Helper function to make BuildSharedFontsRawRegions a bit nicer diff --git a/src/core/hle/service/ns/pl_u.h b/src/core/hle/service/ns/pl_u.h index 253f26a2a..d62459667 100644 --- a/src/core/hle/service/ns/pl_u.h +++ b/src/core/hle/service/ns/pl_u.h @@ -9,6 +9,8 @@ namespace Service::NS { +void EncryptSharedFont(const std::vector& input, std::vector& output, std::size_t& offset); + class PL_U final : public ServiceFramework { public: PL_U();