rodecomp/Source/Utility/Cipher/CryptName.h
ragnarok-clanker a00597cea9 Maintenance: CRYPT_NAME pass 2 (replaces returned 17152 and 17203): CryptName.h builds the text with consteval + index_sequence so a
Batch #17228 by ragnarok-clanker: no function's compiled code changes; checked with 46 compiled unit(s), including every includer of Source/Utility/Cipher/CryptName.h.
2026-11-11 15:28:55 +00:00

48 lines
1.4 KiB
C++

#ifndef RAGNAROK_SOURCE_UTILITY_CIPHER_CRYPTNAME_H
#define RAGNAROK_SOURCE_UTILITY_CIPHER_CRYPTNAME_H
#include <array>
#include <cstddef>
#include <cstdint>
#include <utility>
namespace utility {
template <std::size_t Length>
struct CryptedText {
char data[Length];
// INLINED
template <typename Element>
constexpr operator std::array<Element, Length>() const {
std::array<Element, Length> out{};
for (std::size_t index = 0; index < Length; ++index) {
out[index] = static_cast<Element>(this->data[index]);
}
return out;
}
};
// INLINED
template <std::uint64_t Key, std::size_t Length>
consteval char CryptChar(const char (&text)[Length], std::size_t index) {
return static_cast<char>(static_cast<std::uint8_t>(text[index]) ^ static_cast<std::uint8_t>(Key >> (8 * (index & 7))));
}
// INLINED
template <std::uint64_t Key, std::size_t Length, std::size_t... Index>
consteval CryptedText<Length> CryptText(const char (&text)[Length], std::index_sequence<Index...>) {
return CryptedText<Length>{{CryptChar<Key>(text, Index)...}};
}
// INLINED
template <std::uint64_t Key, std::size_t Length>
consteval CryptedText<Length> CryptText(const char (&text)[Length]) {
return CryptText<Key>(text, std::make_index_sequence<Length>{});
}
}
#define CRYPT_NAME(text, key) (::utility::CryptText<key>(text))
#endif