From 937e3d6f680ba598fea213d960d29d760007116c Mon Sep 17 00:00:00 2001 From: Billy Laws Date: Sat, 13 Feb 2021 12:51:22 +0000 Subject: [PATCH] Add endiannes swapping helper functions Avoid using compiler functions manually to create a cleaner interface, and allow swapping the endianness of arbritary lengths. --- app/src/main/cpp/skyline/common.h | 18 ++++++++++++++++++ app/src/main/cpp/skyline/crypto/aes_cipher.h | 2 +- .../cpp/skyline/vfs/ctr_encrypted_backing.cpp | 2 +- app/src/main/cpp/skyline/vfs/nca.cpp | 4 ++-- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/src/main/cpp/skyline/common.h b/app/src/main/cpp/skyline/common.h index a0e77323..9328e4a1 100644 --- a/app/src/main/cpp/skyline/common.h +++ b/app/src/main/cpp/skyline/common.h @@ -311,6 +311,24 @@ namespace skyline { return result >> (offset + 4); } + template + constexpr std::array SwapEndianness(std::array in) { + std::reverse(in.begin(), in.end()); + return in; + } + + constexpr u64 SwapEndianness(u64 in) { + return __builtin_bswap64(in); + } + + constexpr u32 SwapEndianness(u32 in) { + return __builtin_bswap32(in); + } + + constexpr u16 SwapEndianness(u16 in) { + return __builtin_bswap16(in); + } + /** * @brief A compile-time hash function as std::hash isn't constexpr */ diff --git a/app/src/main/cpp/skyline/crypto/aes_cipher.h b/app/src/main/cpp/skyline/crypto/aes_cipher.h index 211f7d03..b6c6c048 100644 --- a/app/src/main/cpp/skyline/crypto/aes_cipher.h +++ b/app/src/main/cpp/skyline/crypto/aes_cipher.h @@ -21,7 +21,7 @@ namespace skyline::crypto { */ static std::array GetTweak(size_t sector) { std::array tweak{}; - size_t le{__builtin_bswap64(sector)}; + size_t le{util::SwapEndianness(sector)}; std::memcpy(tweak.data() + 8, &le, 8); return tweak; } diff --git a/app/src/main/cpp/skyline/vfs/ctr_encrypted_backing.cpp b/app/src/main/cpp/skyline/vfs/ctr_encrypted_backing.cpp index d36f946a..9cfb93d0 100644 --- a/app/src/main/cpp/skyline/vfs/ctr_encrypted_backing.cpp +++ b/app/src/main/cpp/skyline/vfs/ctr_encrypted_backing.cpp @@ -13,7 +13,7 @@ namespace skyline::vfs { void CtrEncryptedBacking::UpdateCtr(u64 offset) { offset >>= 4; - size_t le{__builtin_bswap64(offset)}; + size_t le{util::SwapEndianness(offset)}; std::memcpy(ctr.data() + 8, &le, 8); cipher.SetIV(ctr); } diff --git a/app/src/main/cpp/skyline/vfs/nca.cpp b/app/src/main/cpp/skyline/vfs/nca.cpp index 6871eca3..9b19e81d 100644 --- a/app/src/main/cpp/skyline/vfs/nca.cpp +++ b/app/src/main/cpp/skyline/vfs/nca.cpp @@ -81,8 +81,8 @@ namespace skyline::vfs { auto key{!(rightsIdEmpty || useKeyArea) ? GetTitleKey() : GetKeyAreaKey(sectionHeader.encryptionType)}; std::array ctr{}; - u32 secureValueLE{__builtin_bswap32(sectionHeader.secureValue)}; - u32 generationLE{__builtin_bswap32(sectionHeader.generation)}; + u32 secureValueLE{util::SwapEndianness(sectionHeader.secureValue)}; + u32 generationLE{util::SwapEndianness(sectionHeader.generation)}; std::memcpy(ctr.data(), &secureValueLE, 4); std::memcpy(ctr.data() + 4, &generationLE, 4);