Dolphin, the GameCube and Wii emulator - Forums

Full Version: General fault: using swap*() where big endian transforming is wanted
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Wiimm

I working to integrate WDF support. As template I use WbfsBlob.*. In the code review I found, that the swap functions swap16() and swap32() are used. But this works only on little endian systems. I know, that Dolphin will run only an little endian system until now, so it's only a cosmetic bug. However, it is wrong and using for example ntohl() or be32toh() is the correct solution.

And then I have looked into other source files and saw, that this mistake is done many times.

Here is a patch for WbfsBlob.cpp:
Code:
diff --git a/Source/Core/DiscIO/WbfsBlob.cpp b/Source/Core/DiscIO/WbfsBlob.cpp
index 7a4d8d9..568dc7a 100644
--- a/Source/Core/DiscIO/WbfsBlob.cpp
+++ b/Source/Core/DiscIO/WbfsBlob.cpp
@@ -5,6 +5,12 @@
#include "WbfsBlob.h"
#include "FileUtil.h"

+#ifdef _MSC_VER
+  #include <Winsock2.h>
+#else
+  #include <arpa/inet.h>
+#endif
+
namespace DiscIO
{
const u64 wii_sector_size = 0x8000;
@@ -78,7 +84,7 @@ bool WbfsFileReader::ReadHeader()

        // Read hd size info
        m_files[0]->file.ReadBytes(&hd_sector_count, 4);
-       hd_sector_count = Common::swap32(hd_sector_count);
+       hd_sector_count = ntohl(hd_sector_count);

        m_files[0]->file.ReadBytes(&hd_sector_shift, 1);
        hd_sector_size = 1ull << hd_sector_shift;
@@ -139,7 +145,7 @@ File::IOFile& WbfsFileReader::SeekToCluster(u64 offset, u64* available)
        u64 base_cluster = offset >> wbfs_sector_shift;
        if(base_cluster < m_blocks_per_disc)
        {
-               u64 cluster_address = wbfs_sector_size * Common::swap16(m_wlba_table[base_cluster]);
+               u64 cluster_address = wbfs_sector_size * ntohs(m_wlba_table[base_cluster]);
                u64 cluster_offset = offset & (wbfs_sector_size - 1);
                u64 final_address = cluster_address + cluster_offset;
I guess you should have posted here:

https://forums.dolphin-emu.org/Forum-code-patches
Moved to code patches.
Please create a fork of our project on github, apply your patch and create a pull request for it.

https://github.com/dolphin-emu/dolphin
Tbh I don't think it's worth it. Dolphin is so dependent on running on a little endian system that fixing it in one place is 1. useless; 2. borderline inconsistent.
Think of the PS360 and 68K Amiga/Mac ports!