From 13e828fd4d7220cf51e0e3a3ee09e09d89509a45 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 2 Jun 2018 14:54:06 -0400 Subject: [PATCH 1/3] WiimoteDevice: Move CBigEndianBuffer to the cpp file This is only used internally, so we don't need to expose it in the header. This also allows getting rid of inclusion of the byte swapping utilities in the header as well. --- .../Core/IOS/USB/Bluetooth/WiimoteDevice.cpp | 17 +++++++++++++++++ .../Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h | 17 ----------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp index 82a0a888d7..40970ba79c 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp +++ b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp @@ -15,6 +15,7 @@ #include "Common/Logging/Log.h" #include "Common/MsgHandler.h" #include "Common/StringUtil.h" +#include "Common/Swap.h" #include "Core/Core.h" #include "Core/HW/Wiimote.h" #include "Core/Host.h" @@ -26,6 +27,22 @@ namespace IOS { namespace HLE { +class CBigEndianBuffer +{ +public: + CBigEndianBuffer(u8* pBuffer) : m_pBuffer(pBuffer) {} + u8 Read8(u32 offset) const { return m_pBuffer[offset]; } + u16 Read16(u32 offset) const { return Common::swap16(*(u16*)&m_pBuffer[offset]); } + u32 Read32(u32 offset) const { return Common::swap32(*(u32*)&m_pBuffer[offset]); } + void Write8(u32 offset, u8 data) { m_pBuffer[offset] = data; } + void Write16(u32 offset, u16 data) { *(u16*)&m_pBuffer[offset] = Common::swap16(data); } + void Write32(u32 offset, u32 data) { *(u32*)&m_pBuffer[offset] = Common::swap32(data); } + u8* GetPointer(u32 offset) { return &m_pBuffer[offset]; } + +private: + u8* m_pBuffer; +}; + WiimoteDevice::WiimoteDevice(Device::BluetoothEmu* host, int number, bdaddr_t bd, bool ready) : m_BD(bd), m_Name(number == WIIMOTE_BALANCE_BOARD ? "Nintendo RVL-WBC-01" : "Nintendo RVL-CNT-01"), diff --git a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h index 89773a6fe5..94bd4b723e 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h +++ b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h @@ -9,7 +9,6 @@ #include #include "Common/CommonTypes.h" -#include "Common/Swap.h" #include "Core/IOS/USB/Bluetooth/hci.h" class PointerWrap; @@ -23,22 +22,6 @@ namespace Device class BluetoothEmu; } -class CBigEndianBuffer -{ -public: - CBigEndianBuffer(u8* pBuffer) : m_pBuffer(pBuffer) {} - u8 Read8(u32 offset) const { return m_pBuffer[offset]; } - u16 Read16(u32 offset) const { return Common::swap16(*(u16*)&m_pBuffer[offset]); } - u32 Read32(u32 offset) const { return Common::swap32(*(u32*)&m_pBuffer[offset]); } - void Write8(u32 offset, u8 data) { m_pBuffer[offset] = data; } - void Write16(u32 offset, u16 data) { *(u16*)&m_pBuffer[offset] = Common::swap16(data); } - void Write32(u32 offset, u32 data) { *(u32*)&m_pBuffer[offset] = Common::swap32(data); } - u8* GetPointer(u32 offset) { return &m_pBuffer[offset]; } - -private: - u8* m_pBuffer; -}; - class WiimoteDevice { public: From 4601136b0876c26105bfc45153128a8dc5065efd Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 2 Jun 2018 14:57:16 -0400 Subject: [PATCH 2/3] WiimoteDevice: Get rid of pointer casting in CBigEndianBuffer Gets rid of more places where we can violate alignment requirements and aliasing rules. --- .../Core/IOS/USB/Bluetooth/WiimoteDevice.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp index 40970ba79c..d805121978 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp +++ b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp @@ -32,11 +32,19 @@ class CBigEndianBuffer public: CBigEndianBuffer(u8* pBuffer) : m_pBuffer(pBuffer) {} u8 Read8(u32 offset) const { return m_pBuffer[offset]; } - u16 Read16(u32 offset) const { return Common::swap16(*(u16*)&m_pBuffer[offset]); } - u32 Read32(u32 offset) const { return Common::swap32(*(u32*)&m_pBuffer[offset]); } + u16 Read16(u32 offset) const { return Common::swap16(&m_pBuffer[offset]); } + u32 Read32(u32 offset) const { return Common::swap32(&m_pBuffer[offset]); } void Write8(u32 offset, u8 data) { m_pBuffer[offset] = data; } - void Write16(u32 offset, u16 data) { *(u16*)&m_pBuffer[offset] = Common::swap16(data); } - void Write32(u32 offset, u32 data) { *(u32*)&m_pBuffer[offset] = Common::swap32(data); } + void Write16(u32 offset, u16 data) + { + const u16 swapped = Common::swap16(data); + std::memcpy(&m_pBuffer[offset], &swapped, sizeof(u16)); + } + void Write32(u32 offset, u32 data) + { + const u32 swapped = Common::swap32(data); + std::memcpy(&m_pBuffer[offset], &swapped, sizeof(u32)); + } u8* GetPointer(u32 offset) { return &m_pBuffer[offset]; } private: From f6c4c05d93ac3a587a28a863f946b76c34f071d6 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 2 Jun 2018 15:10:48 -0400 Subject: [PATCH 3/3] WiimoteDevice: Make CBigEndianBuffer's constructor explicit Prevents implicit construction of buffers. --- Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp index d805121978..69e7c083c4 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp +++ b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp @@ -30,7 +30,7 @@ namespace HLE class CBigEndianBuffer { public: - CBigEndianBuffer(u8* pBuffer) : m_pBuffer(pBuffer) {} + explicit CBigEndianBuffer(u8* pBuffer) : m_pBuffer(pBuffer) {} u8 Read8(u32 offset) const { return m_pBuffer[offset]; } u16 Read16(u32 offset) const { return Common::swap16(&m_pBuffer[offset]); } u32 Read32(u32 offset) const { return Common::swap32(&m_pBuffer[offset]); }