NetKDRequest: Minor adjustments to NWC24MakeUserID

- Make use of std::array
- Move helper functions inside it as lambdas
- Eliminate sign-conversions that were occurring (promotion of u8 to int)
This commit is contained in:
Lioncash 2021-08-24 07:13:06 -04:00
parent e7a30dd468
commit ff3947a1b9
1 changed files with 27 additions and 24 deletions

View File

@ -3,6 +3,7 @@
#include "Core/IOS/Network/KD/NetKDRequest.h"
#include <array>
#include <map>
#include <string>
@ -201,34 +202,35 @@ u8 NetKDRequestDevice::GetHardwareModel(const std::string& model) const
return MODEL_ELSE;
}
static u8 u64_get_byte(u64 value, u8 shift)
{
return (u8)(value >> (shift * 8));
}
static u64 u64_insert_byte(u64 value, u8 shift, u8 byte)
{
u64 mask = 0x00000000000000FFULL << (shift * 8);
u64 inst = (u64)byte << (shift * 8);
return (value & ~mask) | inst;
}
s32 NetKDRequestDevice::NWC24MakeUserID(u64* nwc24_id, u32 hollywood_id, u16 id_ctr,
u8 hardware_model, u8 area_code)
{
const u8 table2[8] = {0x1, 0x5, 0x0, 0x4, 0x2, 0x3, 0x6, 0x7};
const u8 table1[16] = {0x4, 0xB, 0x7, 0x9, 0xF, 0x1, 0xD, 0x3,
0xC, 0x2, 0x6, 0xE, 0x8, 0x0, 0xA, 0x5};
static constexpr std::array<u8, 8> table2{
0x1, 0x5, 0x0, 0x4, 0x2, 0x3, 0x6, 0x7,
};
static constexpr std::array<u8, 16> table1{
0x4, 0xB, 0x7, 0x9, 0xF, 0x1, 0xD, 0x3, 0xC, 0x2, 0x6, 0xE, 0x8, 0x0, 0xA, 0x5,
};
u64 mix_id = ((u64)area_code << 50) | ((u64)hardware_model << 47) | ((u64)hollywood_id << 15) |
((u64)id_ctr << 10);
u64 mix_id_copy1 = mix_id;
constexpr auto u64_get_byte = [](u64 value, u32 shift) -> u8 {
return u8(value >> (shift * 8));
};
int ctr = 0;
constexpr auto u64_insert_byte = [](u64 value, u32 shift, u8 byte) -> u64 {
const u64 mask = 0x00000000000000FFULL << (shift * 8);
const u64 inst = u64{byte} << (shift * 8);
return (value & ~mask) | inst;
};
u64 mix_id = (u64{area_code} << 50) | (u64{hardware_model} << 47) | (u64{hollywood_id} << 15) |
(u64{id_ctr} << 10);
const u64 mix_id_copy1 = mix_id;
u32 ctr = 0;
for (ctr = 0; ctr <= 42; ctr++)
{
u64 value = mix_id >> (52 - ctr);
if (value & 1)
if ((value & 1) != 0)
{
value = 0x0000000000000635ULL << (42 - ctr);
mix_id ^= value;
@ -240,15 +242,16 @@ s32 NetKDRequestDevice::NWC24MakeUserID(u64* nwc24_id, u32 hollywood_id, u16 id_
for (ctr = 0; ctr <= 5; ctr++)
{
u8 ret = u64_get_byte(mix_id, ctr);
u8 foobar = ((table1[(ret >> 4) & 0xF]) << 4) | (table1[ret & 0xF]);
const u8 ret = u64_get_byte(mix_id, ctr);
const u8 foobar = u8((u32{table1[(ret >> 4) & 0xF]} << 4) | table1[ret & 0xF]);
mix_id = u64_insert_byte(mix_id, ctr, foobar & 0xff);
}
u64 mix_id_copy2 = mix_id;
const u64 mix_id_copy2 = mix_id;
for (ctr = 0; ctr <= 5; ctr++)
{
u8 ret = u64_get_byte(mix_id_copy2, ctr);
const u8 ret = u64_get_byte(mix_id_copy2, ctr);
mix_id = u64_insert_byte(mix_id, table2[ctr], ret);
}