Skip to content

Commit

Permalink
IsWiiElf: Optimise inner loop.
Browse files Browse the repository at this point in the history
Instead of swaping each word of the elf code section(s) looking
for a match to our pattern, we swap the pattern just once (at
compile  time) and test against our swapped pattern.
  • Loading branch information
phire committed Nov 1, 2014
1 parent a5184b4 commit 5688a3c
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions Source/Core/Core/Boot/Boot_ELF.cpp
Expand Up @@ -20,7 +20,7 @@ bool CBoot::IsElfWii(const std::string& filename)

{
File::IOFile f(filename, "rb");
f.ReadBytes(elf.get(), (size_t)filesize);
f.ReadBytes(elf.get(), filesize);
}

// Use the same method as the DOL loader uses: search for mfspr from HID4,
Expand All @@ -29,21 +29,20 @@ bool CBoot::IsElfWii(const std::string& filename)
// Likely to have some false positives/negatives, patches implementing a
// better heuristic are welcome.

u32 HID4_pattern = 0x7c13fba6;
u32 HID4_mask = 0xfc1fffff;
// Swap these once, instead of swapping every word in the file.
u32 HID4_pattern = Common::swap32(0x7c13fba6);
u32 HID4_mask = Common::swap32(0xfc1fffff);
ElfReader reader(elf.get());

for (int i = 0; i < reader.GetNumSections(); ++i)
{
if (reader.IsCodeSection(i))
{
for (unsigned int j = 0; j < reader.GetSectionSize(i) / sizeof (u32); ++j)
u32* code = (u32*)reader.GetSectionDataPtr(i);
for (u32 j = 0; j < reader.GetSectionSize(i) / sizeof(u32); ++j)
{
u32 word = Common::swap32(((u32*)reader.GetSectionDataPtr(i))[j]);
if ((word & HID4_mask) == HID4_pattern)
{
if ((code[j] & HID4_mask) == HID4_pattern)
return true;
}
}
}
}
Expand Down

0 comments on commit 5688a3c

Please sign in to comment.