2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2012 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2012-01-31 05:16:31 +01:00
|
|
|
|
|
|
|
// Thanks to Treeki for writing the original class - 29/01/2012
|
|
|
|
|
2018-05-12 19:43:59 +02:00
|
|
|
#include "Common/SettingsHandler.h"
|
|
|
|
|
2020-03-24 16:58:54 +01:00
|
|
|
#include <algorithm>
|
2014-02-20 04:11:52 +01:00
|
|
|
#include <cstddef>
|
2014-02-19 01:54:11 +01:00
|
|
|
#include <ctime>
|
2017-08-08 17:23:46 +02:00
|
|
|
#include <iomanip>
|
2014-02-20 04:11:52 +01:00
|
|
|
#include <string>
|
2012-01-29 09:41:35 +01:00
|
|
|
|
2020-02-04 21:00:33 +01:00
|
|
|
#include <fmt/chrono.h>
|
2019-07-16 10:23:18 +02:00
|
|
|
|
2014-02-20 04:11:52 +01:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-19 01:54:11 +01:00
|
|
|
|
2018-05-12 19:39:35 +02:00
|
|
|
namespace Common
|
|
|
|
{
|
2024-04-23 06:51:09 +02:00
|
|
|
SettingsHandler::SettingsHandler() : m_buffer{}, m_position{0}, m_key{INITIAL_SEED}, decoded{""}
|
2012-01-31 05:16:31 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-04-23 06:51:09 +02:00
|
|
|
SettingsHandler::SettingsHandler(const Buffer& buffer) : SettingsHandler()
|
2017-01-27 16:01:25 +01:00
|
|
|
{
|
2024-04-23 06:51:09 +02:00
|
|
|
m_buffer = buffer;
|
|
|
|
Decrypt();
|
2017-01-27 16:01:25 +01:00
|
|
|
}
|
|
|
|
|
2018-05-09 20:40:56 +02:00
|
|
|
const SettingsHandler::Buffer& SettingsHandler::GetBytes() const
|
2017-01-27 16:01:25 +01:00
|
|
|
{
|
2018-05-09 20:40:56 +02:00
|
|
|
return m_buffer;
|
2017-01-27 16:01:25 +01:00
|
|
|
}
|
|
|
|
|
2019-07-16 10:11:53 +02:00
|
|
|
std::string SettingsHandler::GetValue(std::string_view key) const
|
2012-01-31 05:16:31 +01:00
|
|
|
{
|
2020-03-14 22:27:26 +01:00
|
|
|
constexpr char delim[] = "\n";
|
2019-07-16 10:11:53 +02:00
|
|
|
std::string toFind = std::string(delim).append(key).append("=");
|
2012-01-31 05:16:31 +01:00
|
|
|
size_t found = decoded.find(toFind);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2019-07-16 10:11:53 +02:00
|
|
|
if (found != std::string_view::npos)
|
2012-02-10 06:04:07 +01:00
|
|
|
{
|
|
|
|
size_t delimFound = decoded.find(delim, found + toFind.length());
|
2019-07-16 10:11:53 +02:00
|
|
|
if (delimFound == std::string_view::npos)
|
2012-02-10 06:04:07 +01:00
|
|
|
delimFound = decoded.length() - 1;
|
|
|
|
return decoded.substr(found + toFind.length(), delimFound - (found + toFind.length()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-07-16 10:11:53 +02:00
|
|
|
toFind = std::string(key).append("=");
|
2013-08-29 07:33:24 +02:00
|
|
|
found = decoded.find(toFind);
|
2012-02-10 06:04:07 +01:00
|
|
|
if (found == 0)
|
|
|
|
{
|
|
|
|
size_t delimFound = decoded.find(delim, found + toFind.length());
|
2019-07-16 10:11:53 +02:00
|
|
|
if (delimFound == std::string_view::npos)
|
2012-02-10 06:04:07 +01:00
|
|
|
delimFound = decoded.length() - 1;
|
|
|
|
return decoded.substr(found + toFind.length(), delimFound - (found + toFind.length()));
|
2012-01-31 05:16:31 +01:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2012-01-31 05:16:31 +01:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void SettingsHandler::Decrypt()
|
|
|
|
{
|
2020-03-20 16:07:35 +01:00
|
|
|
while (m_position < m_buffer.size())
|
2013-08-25 02:49:58 +02:00
|
|
|
{
|
2012-01-31 05:16:31 +01:00
|
|
|
decoded.push_back((u8)(m_buffer[m_position] ^ m_key));
|
|
|
|
m_position++;
|
2012-02-10 06:04:07 +01:00
|
|
|
m_key = (m_key >> 31) | (m_key << 1);
|
2012-01-31 05:16:31 +01:00
|
|
|
}
|
2020-03-14 22:27:26 +01:00
|
|
|
|
2020-03-24 16:58:54 +01:00
|
|
|
// The decoded data normally uses CRLF line endings, but occasionally
|
|
|
|
// (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.
|
|
|
|
// (We ignore empty lines.)
|
2023-12-11 13:49:40 +01:00
|
|
|
std::erase(decoded, '\x0d');
|
2012-01-31 05:16:31 +01:00
|
|
|
}
|
|
|
|
|
2023-12-11 13:53:10 +01:00
|
|
|
void SettingsHandler::AddSetting(std::string_view key, std::string_view value)
|
2012-01-31 05:16:31 +01:00
|
|
|
{
|
2023-12-11 13:53:10 +01:00
|
|
|
WriteLine(fmt::format("{}={}\r\n", key, value));
|
2020-03-24 16:58:54 +01:00
|
|
|
}
|
2012-01-31 05:16:31 +01:00
|
|
|
|
2023-12-11 13:53:10 +01:00
|
|
|
void SettingsHandler::WriteLine(std::string_view str)
|
2020-03-24 16:58:54 +01:00
|
|
|
{
|
|
|
|
const u32 old_position = m_position;
|
|
|
|
const u32 old_key = m_key;
|
2012-01-31 05:16:31 +01:00
|
|
|
|
2020-03-24 16:58:54 +01:00
|
|
|
// Encode and write the line
|
|
|
|
for (char c : str)
|
2014-02-08 06:50:37 +01:00
|
|
|
WriteByte(c);
|
2012-01-31 05:16:31 +01:00
|
|
|
|
2020-03-24 16:58:54 +01:00
|
|
|
// If the encoded data contains a null byte, Nintendo's decoder will stop at that null byte
|
|
|
|
// instead of decoding all the data. To avoid this: If the data we just wrote contains
|
|
|
|
// a null byte, add an LF right before the line to prod the values into being different,
|
|
|
|
// just like Nintendo does. Due to the chosen key, LF itself never encodes into a null byte.
|
|
|
|
const auto begin = m_buffer.cbegin() + old_position;
|
|
|
|
const auto end = m_buffer.cbegin() + m_position;
|
|
|
|
if (std::find(begin, end, 0) != end)
|
|
|
|
{
|
|
|
|
m_key = old_key;
|
|
|
|
m_position = old_position;
|
|
|
|
WriteByte('\n');
|
|
|
|
WriteLine(str);
|
|
|
|
}
|
2012-01-31 05:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SettingsHandler::WriteByte(u8 b)
|
|
|
|
{
|
2017-01-27 16:01:25 +01:00
|
|
|
if (m_position >= m_buffer.size())
|
2012-01-31 05:16:31 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_buffer[m_position] = b ^ m_key;
|
|
|
|
m_position++;
|
2012-02-10 06:04:07 +01:00
|
|
|
m_key = (m_key >> 31) | (m_key << 1);
|
2012-01-31 05:16:31 +01:00
|
|
|
}
|
|
|
|
|
2017-01-27 16:11:36 +01:00
|
|
|
std::string SettingsHandler::GenerateSerialNumber()
|
2012-01-31 05:16:31 +01:00
|
|
|
{
|
2017-08-08 17:23:46 +02:00
|
|
|
const std::time_t t = std::time(nullptr);
|
|
|
|
|
|
|
|
// Must be 9 characters at most; otherwise the serial number will be rejected by SDK libraries,
|
|
|
|
// as there is a check to ensure the string length is strictly lower than 10.
|
|
|
|
// 3 for %j, 2 for %H, 2 for %M, 2 for %S.
|
2021-10-15 22:49:13 +02:00
|
|
|
return fmt::format("{:%j%H%M%S}", fmt::localtime(t));
|
2012-02-10 06:04:07 +01:00
|
|
|
}
|
2018-05-12 19:39:35 +02:00
|
|
|
} // namespace Common
|