Split `SettingsHandler` into separate reader and writer classes

This commit is contained in:
Niel Lebeck 2024-04-27 20:07:17 -07:00
parent f412e2488c
commit 1d79991ec4
6 changed files with 107 additions and 112 deletions

View File

@ -17,72 +17,72 @@
namespace Common namespace Common
{ {
SettingsHandler::SettingsHandler() : m_buffer{}, m_position{0}, m_key{INITIAL_SEED}, decoded{""} namespace
{
// Key used to encrypt/decrypt setting.txt contents
constexpr u32 INITIAL_SEED = 0x73B5DBFA;
} // namespace
SettingsWriter::SettingsWriter() : m_buffer{}, m_position{0}, m_key{INITIAL_SEED}
{ {
} }
SettingsHandler::SettingsHandler(const Buffer& buffer) : SettingsHandler() const SettingsBuffer& SettingsWriter::GetBytes() const
{
m_buffer = buffer;
Decrypt();
}
const SettingsHandler::Buffer& SettingsHandler::GetBytes() const
{ {
return m_buffer; return m_buffer;
} }
std::string SettingsHandler::GetValue(std::string_view key) const std::string SettingsReader::GetValue(std::string_view key) const
{ {
constexpr char delim[] = "\n"; constexpr char delim[] = "\n";
std::string toFind = std::string(delim).append(key).append("="); std::string toFind = std::string(delim).append(key).append("=");
size_t found = decoded.find(toFind); size_t found = m_decoded.find(toFind);
if (found != std::string_view::npos) if (found != std::string_view::npos)
{ {
size_t delimFound = decoded.find(delim, found + toFind.length()); size_t delimFound = m_decoded.find(delim, found + toFind.length());
if (delimFound == std::string_view::npos) if (delimFound == std::string_view::npos)
delimFound = decoded.length() - 1; delimFound = m_decoded.length() - 1;
return decoded.substr(found + toFind.length(), delimFound - (found + toFind.length())); return m_decoded.substr(found + toFind.length(), delimFound - (found + toFind.length()));
} }
else else
{ {
toFind = std::string(key).append("="); toFind = std::string(key).append("=");
found = decoded.find(toFind); found = m_decoded.find(toFind);
if (found == 0) if (found == 0)
{ {
size_t delimFound = decoded.find(delim, found + toFind.length()); size_t delimFound = m_decoded.find(delim, found + toFind.length());
if (delimFound == std::string_view::npos) if (delimFound == std::string_view::npos)
delimFound = decoded.length() - 1; delimFound = m_decoded.length() - 1;
return decoded.substr(found + toFind.length(), delimFound - (found + toFind.length())); return m_decoded.substr(found + toFind.length(), delimFound - (found + toFind.length()));
} }
} }
return ""; return "";
} }
void SettingsHandler::Decrypt() SettingsReader::SettingsReader(const SettingsBuffer& buffer) : m_decoded{""}
{ {
while (m_position < m_buffer.size()) u32 key = INITIAL_SEED;
for (u32 position = 0; position < buffer.size(); ++position)
{ {
decoded.push_back((u8)(m_buffer[m_position] ^ m_key)); m_decoded.push_back((u8)(buffer[position] ^ key));
m_position++; key = (key >> 31) | (key << 1);
m_key = (m_key >> 31) | (m_key << 1);
} }
// The decoded data normally uses CRLF line endings, but occasionally // The decoded data normally uses CRLF line endings, but occasionally
// (see the comment in WriteLine), lines can be separated by CRLFLF. // (see the comment in WriteLine), lines can be separated by CRLFLF.
// To handle this, we remove every CR and treat LF as the line ending. // To handle this, we remove every CR and treat LF as the line ending.
// (We ignore empty lines.) // (We ignore empty lines.)
std::erase(decoded, '\x0d'); std::erase(m_decoded, '\x0d');
} }
void SettingsHandler::AddSetting(std::string_view key, std::string_view value) void SettingsWriter::AddSetting(std::string_view key, std::string_view value)
{ {
WriteLine(fmt::format("{}={}\r\n", key, value)); WriteLine(fmt::format("{}={}\r\n", key, value));
} }
void SettingsHandler::WriteLine(std::string_view str) void SettingsWriter::WriteLine(std::string_view str)
{ {
const u32 old_position = m_position; const u32 old_position = m_position;
const u32 old_key = m_key; const u32 old_key = m_key;
@ -106,7 +106,7 @@ void SettingsHandler::WriteLine(std::string_view str)
} }
} }
void SettingsHandler::WriteByte(u8 b) void SettingsWriter::WriteByte(u8 b)
{ {
if (m_position >= m_buffer.size()) if (m_position >= m_buffer.size())
return; return;
@ -116,7 +116,7 @@ void SettingsHandler::WriteByte(u8 b)
m_key = (m_key >> 31) | (m_key << 1); m_key = (m_key >> 31) | (m_key << 1);
} }
std::string SettingsHandler::GenerateSerialNumber() std::string SettingsWriter::GenerateSerialNumber()
{ {
const std::time_t t = std::time(nullptr); const std::time_t t = std::time(nullptr);

View File

@ -13,34 +13,35 @@
namespace Common namespace Common
{ {
class SettingsHandler using SettingsBuffer = std::array<u8, 0x100>;
class SettingsWriter
{ {
public: public:
enum SettingsWriter();
{
SETTINGS_SIZE = 0x100,
// Key used to encrypt/decrypt setting.txt contents
INITIAL_SEED = 0x73B5DBFA
};
using Buffer = std::array<u8, SETTINGS_SIZE>;
SettingsHandler();
explicit SettingsHandler(const Buffer& buffer);
void AddSetting(std::string_view key, std::string_view value); void AddSetting(std::string_view key, std::string_view value);
const Buffer& GetBytes() const; const SettingsBuffer& GetBytes() const;
std::string GetValue(std::string_view key) const;
static std::string GenerateSerialNumber(); static std::string GenerateSerialNumber();
private: private:
void Decrypt();
void WriteLine(std::string_view str); void WriteLine(std::string_view str);
void WriteByte(u8 b); void WriteByte(u8 b);
std::array<u8, SETTINGS_SIZE> m_buffer; SettingsBuffer m_buffer;
u32 m_position, m_key; u32 m_position, m_key;
std::string decoded; };
class SettingsReader
{
public:
explicit SettingsReader(const SettingsBuffer& buffer);
std::string GetValue(std::string_view key) const;
private:
std::string m_decoded;
}; };
} // namespace Common } // namespace Common

View File

@ -371,12 +371,12 @@ bool CBoot::SetupWiiMemory(Core::System& system, IOS::HLE::IOSC::ConsoleType con
const auto fs = system.GetIOS()->GetFS(); const auto fs = system.GetIOS()->GetFS();
{ {
Common::SettingsHandler::Buffer data; Common::SettingsBuffer data;
const auto file = fs->OpenFile(IOS::SYSMENU_UID, IOS::SYSMENU_GID, settings_file_path, const auto file = fs->OpenFile(IOS::SYSMENU_UID, IOS::SYSMENU_GID, settings_file_path,
IOS::HLE::FS::Mode::Read); IOS::HLE::FS::Mode::Read);
if (file && file->Read(data.data(), data.size())) if (file && file->Read(data.data(), data.size()))
{ {
Common::SettingsHandler settings_reader(data); const Common::SettingsReader settings_reader(data);
serno = settings_reader.GetValue("SERNO"); serno = settings_reader.GetValue("SERNO");
model = settings_reader.GetValue("MODEL"); model = settings_reader.GetValue("MODEL");
@ -413,7 +413,7 @@ bool CBoot::SetupWiiMemory(Core::System& system, IOS::HLE::IOSC::ConsoleType con
if (Core::WantsDeterminism()) if (Core::WantsDeterminism())
serno = "123456789"; serno = "123456789";
else else
serno = Common::SettingsHandler::GenerateSerialNumber(); serno = Common::SettingsWriter::GenerateSerialNumber();
INFO_LOG_FMT(BOOT, "No previous serial number found, generated one instead: {}", serno); INFO_LOG_FMT(BOOT, "No previous serial number found, generated one instead: {}", serno);
} }
else else
@ -421,20 +421,21 @@ bool CBoot::SetupWiiMemory(Core::System& system, IOS::HLE::IOSC::ConsoleType con
INFO_LOG_FMT(BOOT, "Using serial number: {}", serno); INFO_LOG_FMT(BOOT, "Using serial number: {}", serno);
} }
Common::SettingsHandler gen; Common::SettingsWriter settings_writer;
gen.AddSetting("AREA", region_setting.area); settings_writer.AddSetting("AREA", region_setting.area);
gen.AddSetting("MODEL", model); settings_writer.AddSetting("MODEL", model);
gen.AddSetting("DVD", "0"); settings_writer.AddSetting("DVD", "0");
gen.AddSetting("MPCH", "0x7FFE"); settings_writer.AddSetting("MPCH", "0x7FFE");
gen.AddSetting("CODE", region_setting.code); settings_writer.AddSetting("CODE", region_setting.code);
gen.AddSetting("SERNO", serno); settings_writer.AddSetting("SERNO", serno);
gen.AddSetting("VIDEO", region_setting.video); settings_writer.AddSetting("VIDEO", region_setting.video);
gen.AddSetting("GAME", region_setting.game); settings_writer.AddSetting("GAME", region_setting.game);
constexpr IOS::HLE::FS::Mode rw_mode = IOS::HLE::FS::Mode::ReadWrite; constexpr IOS::HLE::FS::Mode rw_mode = IOS::HLE::FS::Mode::ReadWrite;
const auto settings_file = fs->CreateAndOpenFile(IOS::SYSMENU_UID, IOS::SYSMENU_GID, const auto settings_file = fs->CreateAndOpenFile(IOS::SYSMENU_UID, IOS::SYSMENU_GID,
settings_file_path, {rw_mode, rw_mode, rw_mode}); settings_file_path, {rw_mode, rw_mode, rw_mode});
if (!settings_file || !settings_file->Write(gen.GetBytes().data(), gen.GetBytes().size())) if (!settings_file ||
!settings_file->Write(settings_writer.GetBytes().data(), settings_writer.GetBytes().size()))
{ {
PanicAlertFmtT("SetupWiiMemory: Can't create setting.txt file"); PanicAlertFmtT("SetupWiiMemory: Can't create setting.txt file");
return false; return false;
@ -443,7 +444,7 @@ bool CBoot::SetupWiiMemory(Core::System& system, IOS::HLE::IOSC::ConsoleType con
auto& memory = system.GetMemory(); auto& memory = system.GetMemory();
// Write the 256 byte setting.txt to memory. // Write the 256 byte setting.txt to memory.
memory.CopyToEmu(0x3800, gen.GetBytes().data(), gen.GetBytes().size()); memory.CopyToEmu(0x3800, settings_writer.GetBytes().data(), settings_writer.GetBytes().size());
INFO_LOG_FMT(BOOT, "Setup Wii Memory..."); INFO_LOG_FMT(BOOT, "Setup Wii Memory...");

View File

@ -133,13 +133,13 @@ IPCReply GetRealProductCode(Core::System& system, const IOCtlVRequest& request)
if (!file) if (!file)
return IPCReply(IPC_ENOENT); return IPCReply(IPC_ENOENT);
Common::SettingsHandler::Buffer data; Common::SettingsBuffer data;
if (!file.ReadBytes(data.data(), data.size())) if (!file.ReadBytes(data.data(), data.size()))
return IPCReply(IPC_ENOENT); return IPCReply(IPC_ENOENT);
Common::SettingsHandler gen(data); const Common::SettingsReader settings_reader(data);
const std::string code = gen.GetValue("CODE"); const std::string code = settings_reader.GetValue("CODE");
const size_t length = std::min<size_t>(request.io_vectors[0].size, code.length()); const size_t length = std::min<size_t>(request.io_vectors[0].size, code.length());
if (length == 0) if (length == 0)

View File

@ -923,7 +923,7 @@ IPCReply NetKDRequestDevice::HandleRequestRegisterUserId(const IOS::HLE::IOCtlRe
return IPCReply{IPC_SUCCESS}; return IPCReply{IPC_SUCCESS};
} }
Common::SettingsHandler::Buffer data; Common::SettingsBuffer data;
if (!file->Read(data.data(), data.size())) if (!file->Read(data.data(), data.size()))
{ {
WriteReturnValue(memory, NWC24::WC24_ERR_FILE_READ, request.buffer_out); WriteReturnValue(memory, NWC24::WC24_ERR_FILE_READ, request.buffer_out);
@ -931,8 +931,8 @@ IPCReply NetKDRequestDevice::HandleRequestRegisterUserId(const IOS::HLE::IOCtlRe
return IPCReply{IPC_SUCCESS}; return IPCReply{IPC_SUCCESS};
} }
const Common::SettingsHandler gen{data}; const Common::SettingsReader settings_reader{data};
const std::string serno = gen.GetValue("SERNO"); const std::string serno = settings_reader.GetValue("SERNO");
const std::string form_data = const std::string form_data =
fmt::format("mlid=w{}&hdid={}&rgncd={}", m_config.Id(), m_ios.GetIOSC().GetDeviceId(), serno); fmt::format("mlid=w{}&hdid={}&rgncd={}", m_config.Id(), m_ios.GetIOSC().GetDeviceId(), serno);
const Common::HttpRequest::Response response = m_http.Post(m_config.GetAccountURL(), form_data); const Common::HttpRequest::Response response = m_http.Post(m_config.GetAccountURL(), form_data);
@ -1076,12 +1076,12 @@ std::optional<IPCReply> NetKDRequestDevice::IOCtl(const IOCtlRequest& request)
const auto fs = m_ios.GetFS(); const auto fs = m_ios.GetFS();
if (const auto file = fs->OpenFile(PID_KD, PID_KD, settings_file_path, FS::Mode::Read)) if (const auto file = fs->OpenFile(PID_KD, PID_KD, settings_file_path, FS::Mode::Read))
{ {
Common::SettingsHandler::Buffer data; Common::SettingsBuffer data;
if (file->Read(data.data(), data.size())) if (file->Read(data.data(), data.size()))
{ {
const Common::SettingsHandler gen{data}; const Common::SettingsReader settings_reader{data};
area = gen.GetValue("AREA"); area = settings_reader.GetValue("AREA");
model = gen.GetValue("MODEL"); model = settings_reader.GetValue("MODEL");
} }
} }

View File

@ -9,15 +9,15 @@ namespace
{ {
// The encrypted bytes corresponding to the following settings, in order: // The encrypted bytes corresponding to the following settings, in order:
// "key" = "val" // "key" = "val"
Common::SettingsHandler::Buffer BUFFER_A{0x91, 0x91, 0x90, 0xEE, 0xD1, 0x2F, 0xF0, 0x34, 0x79}; Common::SettingsBuffer BUFFER_A{0x91, 0x91, 0x90, 0xEE, 0xD1, 0x2F, 0xF0, 0x34, 0x79};
// The encrypted bytes corresponding to the following settings, in order: // The encrypted bytes corresponding to the following settings, in order:
// "key1" = "val1" // "key1" = "val1"
// "key2" = "val2" // "key2" = "val2"
// "foo" = "bar" // "foo" = "bar"
Common::SettingsHandler::Buffer BUFFER_B{ Common::SettingsBuffer BUFFER_B{0x91, 0x91, 0x90, 0xE2, 0x9A, 0x38, 0xFD, 0x55, 0x42, 0xEA, 0xC4,
0x91, 0x91, 0x90, 0xE2, 0x9A, 0x38, 0xFD, 0x55, 0x42, 0xEA, 0xC4, 0xF6, 0x5E, 0xF, 0xDF, 0xE7, 0xF6, 0x5E, 0xF, 0xDF, 0xE7, 0xC3, 0x0A, 0xBB, 0x9C, 0x50, 0xB1,
0xC3, 0x0A, 0xBB, 0x9C, 0x50, 0xB1, 0x10, 0x82, 0xB4, 0x8A, 0x0D, 0xBE, 0xCD, 0x72, 0xF4}; 0x10, 0x82, 0xB4, 0x8A, 0x0D, 0xBE, 0xCD, 0x72, 0xF4};
// The encrypted bytes corresponding to the following setting: // The encrypted bytes corresponding to the following setting:
// "\xFA" = "a" // "\xFA" = "a"
@ -25,7 +25,7 @@ Common::SettingsHandler::Buffer BUFFER_B{
// This setting triggers the edge case fixed in PR #8704: the key's first and only byte matches the // This setting triggers the edge case fixed in PR #8704: the key's first and only byte matches the
// first byte of the initial encryption key, resulting in a null encoded byte on the first attempt // first byte of the initial encryption key, resulting in a null encoded byte on the first attempt
// to encode the line. // to encode the line.
Common::SettingsHandler::Buffer BUFFER_C{0xF0, 0x0E, 0xD4, 0xB2, 0xAA, 0x44}; Common::SettingsBuffer BUFFER_C{0xF0, 0x0E, 0xD4, 0xB2, 0xAA, 0x44};
// The encrypted bytes corresponding to the following setting: // The encrypted bytes corresponding to the following setting:
// "\xFA\xE9" = "a" // "\xFA\xE9" = "a"
@ -38,76 +38,69 @@ Common::SettingsHandler::Buffer BUFFER_C{0xF0, 0x0E, 0xD4, 0xB2, 0xAA, 0x44};
// 2. The key's second byte matches the first byte of the encryption key after two // 2. The key's second byte matches the first byte of the encryption key after two
// rotations, resulting in a null encoded byte on the second attempt to encode the line (with a // rotations, resulting in a null encoded byte on the second attempt to encode the line (with a
// single LF inserted before the line). // single LF inserted before the line).
Common::SettingsHandler::Buffer BUFFER_D{0xF0, 0xFE, 0x13, 0x3A, 0x9A, 0x2F, 0x91, 0x33}; Common::SettingsBuffer BUFFER_D{0xF0, 0xFE, 0x13, 0x3A, 0x9A, 0x2F, 0x91, 0x33};
} // namespace } // namespace
TEST(SettingsHandlerTest, EncryptSingleSetting) TEST(SettingsWriterTest, EncryptSingleSetting)
{ {
Common::SettingsHandler handler; Common::SettingsWriter writer;
handler.AddSetting("key", "val"); writer.AddSetting("key", "val");
Common::SettingsHandler::Buffer buffer = handler.GetBytes(); Common::SettingsBuffer buffer = writer.GetBytes();
EXPECT_TRUE(std::ranges::equal(buffer, BUFFER_A)); EXPECT_TRUE(std::ranges::equal(buffer, BUFFER_A));
} }
TEST(SettingsHandlerTest, DecryptSingleSetting) TEST(SettingsReaderTest, DecryptSingleSetting)
{ {
Common::SettingsHandler handler(BUFFER_A); const Common::SettingsReader reader(BUFFER_A);
EXPECT_EQ(handler.GetValue("key"), "val"); EXPECT_EQ(reader.GetValue("key"), "val");
} }
TEST(SettingsHandlerTest, EncryptMultipleSettings) TEST(SettingsWriterTest, EncryptMultipleSettings)
{ {
Common::SettingsHandler handler; Common::SettingsWriter writer;
handler.AddSetting("key1", "val1"); writer.AddSetting("key1", "val1");
handler.AddSetting("key2", "val2"); writer.AddSetting("key2", "val2");
handler.AddSetting("foo", "bar"); writer.AddSetting("foo", "bar");
Common::SettingsHandler::Buffer buffer = handler.GetBytes(); Common::SettingsBuffer buffer = writer.GetBytes();
EXPECT_TRUE(std::ranges::equal(buffer, BUFFER_B)); EXPECT_TRUE(std::ranges::equal(buffer, BUFFER_B));
} }
TEST(SettingsHandlerTest, DecryptMultipleSettings) TEST(SettingsReaderTest, DecryptMultipleSettings)
{ {
Common::SettingsHandler handler(BUFFER_B); const Common::SettingsReader reader(BUFFER_B);
EXPECT_EQ(handler.GetValue("key1"), "val1"); EXPECT_EQ(reader.GetValue("key1"), "val1");
EXPECT_EQ(handler.GetValue("key2"), "val2"); EXPECT_EQ(reader.GetValue("key2"), "val2");
EXPECT_EQ(handler.GetValue("foo"), "bar"); EXPECT_EQ(reader.GetValue("foo"), "bar");
} }
TEST(SettingsHandlerTest, GetValueOnSameInstance) TEST(SettingsWriterTest, EncryptAddsLFOnNullChar)
{ {
Common::SettingsHandler handler; Common::SettingsWriter writer;
handler.AddSetting("key", "val"); writer.AddSetting("\xFA", "a");
EXPECT_EQ(handler.GetValue("key"), ""); Common::SettingsBuffer buffer = writer.GetBytes();
}
TEST(SettingsHandlerTest, EncryptAddsLFOnNullChar)
{
Common::SettingsHandler handler;
handler.AddSetting("\xFA", "a");
Common::SettingsHandler::Buffer buffer = handler.GetBytes();
EXPECT_TRUE(std::ranges::equal(buffer, BUFFER_C)); EXPECT_TRUE(std::ranges::equal(buffer, BUFFER_C));
} }
TEST(SettingsHandlerTest, EncryptAddsLFOnNullCharTwice) TEST(SettingsWriterTest, EncryptAddsLFOnNullCharTwice)
{ {
Common::SettingsHandler handler; Common::SettingsWriter writer;
handler.AddSetting("\xFA\xE9", "a"); writer.AddSetting("\xFA\xE9", "a");
Common::SettingsHandler::Buffer buffer = handler.GetBytes(); Common::SettingsBuffer buffer = writer.GetBytes();
EXPECT_TRUE(std::ranges::equal(buffer, BUFFER_D)); EXPECT_TRUE(std::ranges::equal(buffer, BUFFER_D));
} }
TEST(SettingsHandlerTest, DecryptSingleAddedLF) TEST(SettingsReaderTest, DecryptSingleAddedLF)
{ {
Common::SettingsHandler handler(BUFFER_C); const Common::SettingsReader reader(BUFFER_C);
EXPECT_EQ(handler.GetValue("\xFA"), "a"); EXPECT_EQ(reader.GetValue("\xFA"), "a");
} }
TEST(SettingsHandlerTest, DecryptTwoAddedLFs) TEST(SettingsReaderTest, DecryptTwoAddedLFs)
{ {
Common::SettingsHandler handler(BUFFER_D); const Common::SettingsReader reader(BUFFER_D);
EXPECT_EQ(handler.GetValue("\xFA\xE9"), "a"); EXPECT_EQ(reader.GetValue("\xFA\xE9"), "a");
} }