Add endiannes swapping helper functions

Avoid using compiler functions manually to create a cleaner interface,
and allow swapping the endianness of arbritary lengths.
This commit is contained in:
Billy Laws 2021-02-13 12:51:22 +00:00 committed by ◱ Mark
parent def9f96b02
commit 937e3d6f68
4 changed files with 22 additions and 4 deletions

View File

@ -311,6 +311,24 @@ namespace skyline {
return result >> (offset + 4);
}
template<size_t N>
constexpr std::array<u8, N> SwapEndianness(std::array<u8, N> 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
*/

View File

@ -21,7 +21,7 @@ namespace skyline::crypto {
*/
static std::array<u8, 0x10> GetTweak(size_t sector) {
std::array<u8, 0x10> tweak{};
size_t le{__builtin_bswap64(sector)};
size_t le{util::SwapEndianness(sector)};
std::memcpy(tweak.data() + 8, &le, 8);
return tweak;
}

View File

@ -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);
}

View File

@ -81,8 +81,8 @@ namespace skyline::vfs {
auto key{!(rightsIdEmpty || useKeyArea) ? GetTitleKey() : GetKeyAreaKey(sectionHeader.encryptionType)};
std::array<u8, 0x10> 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);