From 2badaf43b0a9abeeaa50bcc7bc112225ac3dff2c Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Fri, 21 Sep 2018 19:53:14 -0400 Subject: [PATCH 1/7] common/swap: add swap template for enum --- src/common/swap.h | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/common/swap.h b/src/common/swap.h index 32af0b6ac..466096f58 100644 --- a/src/common/swap.h +++ b/src/common/swap.h @@ -17,6 +17,8 @@ #pragma once +#include + #if defined(_MSC_VER) #include #elif defined(__linux__) @@ -605,6 +607,44 @@ struct swap_double_t { } }; +template +struct swap_enum_t { + static_assert(std::is_enum_v); + using base = std::underlying_type_t; + +public: + swap_enum_t() = default; + swap_enum_t(const T& v) : value(swap(v)) {} + + swap_enum_t& operator=(const T& v) { + value = swap(v); + return *this; + } + + operator T() const { + return swap(value); + } + + explicit operator base() const { + return static_cast(swap(value)); + } + +protected: + T value{}; + // clang-format off + using swap_t = std::conditional_t< + std::is_same_v, swap_16_t, std::conditional_t< + std::is_same_v, swap_16_t, std::conditional_t< + std::is_same_v, swap_32_t, std::conditional_t< + std::is_same_v, swap_32_t, std::conditional_t< + std::is_same_v, swap_64_t, std::conditional_t< + std::is_same_v, swap_64_t, void>>>>>>; + // clang-format on + static T swap(T x) { + return static_cast(swap_t::swap(static_cast(x))); + } +}; + #if COMMON_LITTLE_ENDIAN using u16_le = u16; using u32_le = u32; @@ -614,6 +654,9 @@ using s16_le = s16; using s32_le = s32; using s64_le = s64; +template +using enum_le = std::enable_if_t, T>; + using float_le = float; using double_le = double; @@ -626,6 +669,9 @@ using s32_be = swap_struct_t>; using u16_be = swap_struct_t>; using s16_be = swap_struct_t>; +template +using enum_be = swap_enum_t; + using float_be = swap_struct_t>; using double_be = swap_struct_t>; #else @@ -639,6 +685,9 @@ using s32_le = swap_struct_t>; using u16_le = swap_struct_t>; using s16_le = swap_struct_t>; +template +using enum_le = swap_enum_t; + using float_le = swap_struct_t>; using double_le = swap_struct_t>; @@ -650,6 +699,9 @@ using s16_be = s16; using s32_be = s32; using s64_be = s64; +template +using enum_be = std::enable_if_t, T>; + using float_be = float; using double_be = double; From 9564b4b2924323c8ce2aefbc8f4bb4777fd71194 Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Fri, 21 Sep 2018 20:09:47 -0400 Subject: [PATCH 2/7] filesys/archive_ncch: specify endianness for enum NCCHArchivePath::media_type is unchanged because its underlying type is unclear --- src/core/file_sys/archive_ncch.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/core/file_sys/archive_ncch.cpp b/src/core/file_sys/archive_ncch.cpp index 94c0dd580..a8ad69698 100644 --- a/src/core/file_sys/archive_ncch.cpp +++ b/src/core/file_sys/archive_ncch.cpp @@ -12,6 +12,7 @@ #include "common/file_util.h" #include "common/logging/log.h" #include "common/string_util.h" +#include "common/swap.h" #include "core/core.h" #include "core/file_sys/archive_ncch.h" #include "core/file_sys/errors.h" @@ -35,9 +36,9 @@ struct NCCHArchivePath { static_assert(sizeof(NCCHArchivePath) == 0x10, "NCCHArchivePath has wrong size!"); struct NCCHFilePath { - u32_le open_type; + enum_le open_type; u32_le content_index; - u32_le filepath_type; + enum_le filepath_type; std::array exefs_filepath; }; static_assert(sizeof(NCCHFilePath) == 0x14, "NCCHFilePath has wrong size!"); @@ -55,9 +56,9 @@ Path MakeNCCHArchivePath(u64 tid, Service::FS::MediaType media_type) { Path MakeNCCHFilePath(NCCHFileOpenType open_type, u32 content_index, NCCHFilePathType filepath_type, std::array& exefs_filepath) { NCCHFilePath path; - path.open_type = static_cast(open_type); + path.open_type = open_type; path.content_index = static_cast(content_index); - path.filepath_type = static_cast(filepath_type); + path.filepath_type = filepath_type; path.exefs_filepath = exefs_filepath; std::vector file(sizeof(path)); std::memcpy(&file[0], &path, sizeof(path)); @@ -88,15 +89,14 @@ ResultVal> NCCHArchive::OpenFile(const Path& path, std::unique_ptr file; // NCCH RomFS - NCCHFilePathType filepath_type = static_cast(openfile_path.filepath_type); - if (filepath_type == NCCHFilePathType::RomFS) { + if (openfile_path.filepath_type == NCCHFilePathType::RomFS) { std::shared_ptr romfs_file; result = ncch_container.ReadRomFS(romfs_file); std::unique_ptr delay_generator = std::make_unique(); file = std::make_unique(std::move(romfs_file), std::move(delay_generator)); - } else if (filepath_type == NCCHFilePathType::Code || - filepath_type == NCCHFilePathType::ExeFS) { + } else if (openfile_path.filepath_type == NCCHFilePathType::Code || + openfile_path.filepath_type == NCCHFilePathType::ExeFS) { std::vector buffer; // Load NCCH .code or icon/banner/logo @@ -104,7 +104,8 @@ ResultVal> NCCHArchive::OpenFile(const Path& path, std::unique_ptr delay_generator = std::make_unique(); file = std::make_unique(std::move(buffer), std::move(delay_generator)); } else { - LOG_ERROR(Service_FS, "Unknown NCCH archive type {}!", openfile_path.filepath_type); + LOG_ERROR(Service_FS, "Unknown NCCH archive type {}!", + static_cast(openfile_path.filepath_type)); result = Loader::ResultStatus::Error; } From 39feb0610bcecc710dd3d1462b0b1b994eeb4bc5 Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Fri, 21 Sep 2018 20:11:29 -0400 Subject: [PATCH 3/7] filesys/archive_selfncch: specify endiannes for enum --- src/core/file_sys/archive_selfncch.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/file_sys/archive_selfncch.cpp b/src/core/file_sys/archive_selfncch.cpp index 6cadacbf5..14be1f966 100644 --- a/src/core/file_sys/archive_selfncch.cpp +++ b/src/core/file_sys/archive_selfncch.cpp @@ -25,7 +25,7 @@ enum class SelfNCCHFilePathType : u32 { }; struct SelfNCCHFilePath { - u32_le type; + enum_le type; std::array exefs_filename; }; static_assert(sizeof(SelfNCCHFilePath) == 12, "NCCHFilePath has wrong size!"); @@ -102,7 +102,7 @@ public: SelfNCCHFilePath file_path; std::memcpy(&file_path, binary.data(), sizeof(SelfNCCHFilePath)); - switch (static_cast(file_path.type)) { + switch (file_path.type) { case SelfNCCHFilePathType::UpdateRomFS: return OpenUpdateRomFS(); From 18e94897c45369c0810040b9ad8abe49b783eab3 Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Fri, 21 Sep 2018 20:15:55 -0400 Subject: [PATCH 4/7] hle/swkbd: specify endianness for enum and other members --- src/core/hle/applets/swkbd.cpp | 7 +++-- src/core/hle/applets/swkbd.h | 56 +++++++++++++++++----------------- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/core/hle/applets/swkbd.cpp b/src/core/hle/applets/swkbd.cpp index 1b19b7d68..d13050002 100644 --- a/src/core/hle/applets/swkbd.cpp +++ b/src/core/hle/applets/swkbd.cpp @@ -110,7 +110,7 @@ void SoftwareKeyboard::Update() { break; default: LOG_CRITICAL(Applet_SWKBD, "Unknown button config {}", - static_cast(config.num_buttons_m1)); + static_cast(config.num_buttons_m1)); UNREACHABLE(); } @@ -143,8 +143,9 @@ Frontend::KeyboardConfig SoftwareKeyboard::ToFrontendConfig( const SoftwareKeyboardConfig& config) const { using namespace Frontend; KeyboardConfig frontend_config; - frontend_config.button_config = static_cast(config.num_buttons_m1); - frontend_config.accept_mode = static_cast(config.valid_input); + frontend_config.button_config = + static_cast(static_cast(config.num_buttons_m1)); + frontend_config.accept_mode = static_cast(static_cast(config.valid_input)); frontend_config.multiline_mode = config.multiline; frontend_config.max_text_length = config.max_text_length; frontend_config.max_digits = config.max_digits; diff --git a/src/core/hle/applets/swkbd.h b/src/core/hle/applets/swkbd.h index d75d696ec..246d2f4c5 100644 --- a/src/core/hle/applets/swkbd.h +++ b/src/core/hle/applets/swkbd.h @@ -121,20 +121,20 @@ enum class SoftwareKeyboardResult : s32 { }; struct SoftwareKeyboardConfig { - SoftwareKeyboardType type; - SoftwareKeyboardButtonConfig num_buttons_m1; - SoftwareKeyboardValidInput valid_input; - SoftwareKeyboardPasswordMode password_mode; - s32 is_parental_screen; - s32 darken_top_screen; - u32 filter_flags; - u32 save_state_flags; - u16 max_text_length; - u16 dict_word_count; - u16 max_digits; - std::array, MAX_BUTTON> button_text; - std::array numpad_keys; - std::array + enum_le type; + enum_le num_buttons_m1; + enum_le valid_input; + enum_le password_mode; + s32_le is_parental_screen; + s32_le darken_top_screen; + u32_le filter_flags; + u32_le save_state_flags; + u16_le max_text_length; + u16_le dict_word_count; + u16_le max_digits; + std::array, MAX_BUTTON> button_text; + std::array numpad_keys; + std::array hint_text; ///< Text to display when asking the user for input bool predictive_input; bool multiline; @@ -145,25 +145,25 @@ struct SoftwareKeyboardConfig { bool unknown; bool default_qwerty; std::array button_submits_text; - u16 language; + u16_le language; - u32 initial_text_offset; ///< Offset of the default text in the output SharedMemory - u32 dict_offset; - u32 initial_status_offset; - u32 initial_learning_offset; - u32 shared_memory_size; ///< Size of the SharedMemory - u32 version; + u32_le initial_text_offset; ///< Offset of the default text in the output SharedMemory + u32_le dict_offset; + u32_le initial_status_offset; + u32_le initial_learning_offset; + u32_le shared_memory_size; ///< Size of the SharedMemory + u32_le version; - SoftwareKeyboardResult return_code; + enum_le return_code; - u32 status_offset; - u32 learning_offset; + u32_le status_offset; + u32_le learning_offset; - u32 text_offset; ///< Offset in the SharedMemory where the output text starts - u16 text_length; ///< Length in characters of the output text + u32_le text_offset; ///< Offset in the SharedMemory where the output text starts + u16_le text_length; ///< Length in characters of the output text - int callback_result; - std::array callback_msg; + s32_le callback_result; + std::array callback_msg; bool skip_at_check; INSERT_PADDING_BYTES(0xAB); }; From 16b36b602508e31507e8e83966a6df243da9c09e Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Fri, 21 Sep 2018 20:21:49 -0400 Subject: [PATCH 5/7] service/cro: specify endianness for enum --- src/core/hle/service/ldr_ro/cro_helper.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/hle/service/ldr_ro/cro_helper.h b/src/core/hle/service/ldr_ro/cro_helper.h index dc14039fb..3e4da2a03 100644 --- a/src/core/hle/service/ldr_ro/cro_helper.h +++ b/src/core/hle/service/ldr_ro/cro_helper.h @@ -236,7 +236,7 @@ private: struct SegmentEntry { u32_le offset; u32_le size; - SegmentType type; + enum_le type; static constexpr HeaderField TABLE_OFFSET_FIELD = SegmentTableOffset; }; From e0336403eee31f31ed969def8d36ddc8cda428c5 Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Fri, 21 Sep 2018 20:25:53 -0400 Subject: [PATCH 6/7] nwm/uds_connection: specify endiannes for enum --- src/core/hle/service/nwm/uds_connection.cpp | 9 ++++----- src/core/hle/service/nwm/uds_connection.h | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/core/hle/service/nwm/uds_connection.cpp b/src/core/hle/service/nwm/uds_connection.cpp index c74f51253..e52569287 100644 --- a/src/core/hle/service/nwm/uds_connection.cpp +++ b/src/core/hle/service/nwm/uds_connection.cpp @@ -15,7 +15,7 @@ constexpr u16 DefaultExtraCapabilities = 0x0431; std::vector GenerateAuthenticationFrame(AuthenticationSeq seq) { AuthenticationFrame frame{}; - frame.auth_seq = static_cast(seq); + frame.auth_seq = seq; std::vector data(sizeof(frame)); std::memcpy(data.data(), &frame, sizeof(frame)); @@ -27,7 +27,7 @@ AuthenticationSeq GetAuthenticationSeqNumber(const std::vector& body) { AuthenticationFrame frame; std::memcpy(&frame, body.data(), sizeof(frame)); - return static_cast(frame.auth_seq); + return frame.auth_seq; } /** @@ -58,7 +58,7 @@ static std::vector GenerateSSIDTag(u32 network_id) { std::vector GenerateAssocResponseFrame(AssocStatus status, u16 association_id, u32 network_id) { AssociationResponseFrame frame{}; frame.capabilities = DefaultExtraCapabilities; - frame.status_code = static_cast(status); + frame.status_code = status; // The association id is ORed with this magic value (0xC000) constexpr u16 AssociationIdMagic = 0xC000; frame.assoc_id = association_id | AssociationIdMagic; @@ -80,8 +80,7 @@ std::tuple GetAssociationResult(const std::vector& body) { memcpy(&frame, body.data(), sizeof(frame)); constexpr u16 AssociationIdMask = 0x3FFF; - return std::make_tuple(static_cast(frame.status_code), - frame.assoc_id & AssociationIdMask); + return std::make_tuple(frame.status_code, frame.assoc_id & AssociationIdMask); } } // namespace NWM diff --git a/src/core/hle/service/nwm/uds_connection.h b/src/core/hle/service/nwm/uds_connection.h index a664f8471..201d23e3f 100644 --- a/src/core/hle/service/nwm/uds_connection.h +++ b/src/core/hle/service/nwm/uds_connection.h @@ -23,16 +23,16 @@ enum class AuthStatus : u16 { Successful = 0 }; enum class AssocStatus : u16 { Successful = 0 }; struct AuthenticationFrame { - u16_le auth_algorithm = static_cast(AuthAlgorithm::OpenSystem); - u16_le auth_seq; - u16_le status_code = static_cast(AuthStatus::Successful); + enum_le auth_algorithm = AuthAlgorithm::OpenSystem; + enum_le auth_seq; + enum_le status_code = AuthStatus::Successful; }; static_assert(sizeof(AuthenticationFrame) == 6, "AuthenticationFrame has wrong size"); struct AssociationResponseFrame { u16_le capabilities; - u16_le status_code; + enum_le status_code; u16_le assoc_id; }; From 41d53cee1f291a989315462815ba00dd522dcf2e Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Fri, 21 Sep 2018 21:05:47 -0400 Subject: [PATCH 7/7] nwm/uds_data: specify endianness for enum --- src/core/hle/service/nwm/uds_data.cpp | 6 ++---- src/core/hle/service/nwm/uds_data.h | 8 ++++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/core/hle/service/nwm/uds_data.cpp b/src/core/hle/service/nwm/uds_data.cpp index 1f227de6d..57414f343 100644 --- a/src/core/hle/service/nwm/uds_data.cpp +++ b/src/core/hle/service/nwm/uds_data.cpp @@ -26,7 +26,7 @@ using MacAddress = std::array; */ static std::vector GenerateLLCHeader(EtherType protocol) { LLCHeader header{}; - header.protocol = static_cast(protocol); + header.protocol = protocol; std::vector buffer(sizeof(header)); memcpy(buffer.data(), &header, sizeof(header)); @@ -313,9 +313,7 @@ std::vector GenerateEAPoLStartFrame(u16 association_id, const NodeInfo& node EtherType GetFrameEtherType(const std::vector& frame) { LLCHeader header; std::memcpy(&header, frame.data(), sizeof(header)); - - u16 ethertype = header.protocol; - return static_cast(ethertype); + return header.protocol; } u16 GetEAPoLFrameType(const std::vector& frame) { diff --git a/src/core/hle/service/nwm/uds_data.h b/src/core/hle/service/nwm/uds_data.h index 59906f677..2ebd416b4 100644 --- a/src/core/hle/service/nwm/uds_data.h +++ b/src/core/hle/service/nwm/uds_data.h @@ -26,11 +26,11 @@ enum class EtherType : u16 { SecureData = 0x876D, EAPoL = 0x888E }; * and the OUI is always 0. */ struct LLCHeader { - u8 dsap = static_cast(SAP::SNAPExtensionUsed); - u8 ssap = static_cast(SAP::SNAPExtensionUsed); - u8 control = static_cast(PDUControl::UnnumberedInformation); + SAP dsap = SAP::SNAPExtensionUsed; + SAP ssap = SAP::SNAPExtensionUsed; + PDUControl control = PDUControl::UnnumberedInformation; std::array OUI = {}; - u16_be protocol; + enum_be protocol; }; static_assert(sizeof(LLCHeader) == 8, "LLCHeader has the wrong size");