2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2012 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-08-27 12:57:08 +00:00
|
|
|
// Refer to the license.txt file included.
|
2012-01-31 04:16:31 +00:00
|
|
|
|
|
|
|
// Thanks to Treeki for writing the original class - 29/01/2012
|
|
|
|
|
2018-05-12 17:43:59 +00:00
|
|
|
#include "Common/SettingsHandler.h"
|
|
|
|
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <cstddef>
|
2014-02-19 00:54:11 +00:00
|
|
|
#include <ctime>
|
2017-08-08 15:23:46 +00:00
|
|
|
#include <iomanip>
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <string>
|
2012-01-29 08:41:35 +00:00
|
|
|
|
2020-02-04 20:00:33 +00:00
|
|
|
#include <fmt/chrono.h>
|
2019-07-16 08:23:18 +00:00
|
|
|
|
2014-02-20 03:11:52 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-19 00:54:11 +00:00
|
|
|
|
2018-05-12 17:39:35 +00:00
|
|
|
namespace Common
|
|
|
|
{
|
2012-01-31 04:16:31 +00:00
|
|
|
SettingsHandler::SettingsHandler()
|
|
|
|
{
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
2018-05-09 18:40:56 +00:00
|
|
|
SettingsHandler::SettingsHandler(Buffer&& buffer)
|
2017-01-27 15:01:25 +00:00
|
|
|
{
|
2018-05-09 18:40:56 +00:00
|
|
|
SetBytes(std::move(buffer));
|
2017-01-27 15:01:25 +00:00
|
|
|
}
|
|
|
|
|
2018-05-09 18:40:56 +00:00
|
|
|
const SettingsHandler::Buffer& SettingsHandler::GetBytes() const
|
2017-01-27 15:01:25 +00:00
|
|
|
{
|
2018-05-09 18:40:56 +00:00
|
|
|
return m_buffer;
|
2017-01-27 15:01:25 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 17:46:24 +00:00
|
|
|
void SettingsHandler::SetBytes(Buffer&& buffer)
|
2012-01-31 04:16:31 +00:00
|
|
|
{
|
2018-05-09 18:40:56 +00:00
|
|
|
Reset();
|
|
|
|
m_buffer = std::move(buffer);
|
|
|
|
Decrypt();
|
2012-01-31 04:16:31 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 08:11:53 +00:00
|
|
|
std::string SettingsHandler::GetValue(std::string_view key) const
|
2012-01-31 04:16:31 +00:00
|
|
|
{
|
2020-03-14 21:27:26 +00:00
|
|
|
constexpr char delim[] = "\n";
|
2019-07-16 08:11:53 +00:00
|
|
|
std::string toFind = std::string(delim).append(key).append("=");
|
2012-01-31 04:16:31 +00:00
|
|
|
size_t found = decoded.find(toFind);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-07-16 08:11:53 +00:00
|
|
|
if (found != std::string_view::npos)
|
2012-02-10 05:04:07 +00:00
|
|
|
{
|
|
|
|
size_t delimFound = decoded.find(delim, found + toFind.length());
|
2019-07-16 08:11:53 +00:00
|
|
|
if (delimFound == std::string_view::npos)
|
2012-02-10 05:04:07 +00:00
|
|
|
delimFound = decoded.length() - 1;
|
|
|
|
return decoded.substr(found + toFind.length(), delimFound - (found + toFind.length()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-07-16 08:11:53 +00:00
|
|
|
toFind = std::string(key).append("=");
|
2013-08-29 05:33:24 +00:00
|
|
|
found = decoded.find(toFind);
|
2012-02-10 05:04:07 +00:00
|
|
|
if (found == 0)
|
|
|
|
{
|
|
|
|
size_t delimFound = decoded.find(delim, found + toFind.length());
|
2019-07-16 08:11:53 +00:00
|
|
|
if (delimFound == std::string_view::npos)
|
2012-02-10 05:04:07 +00:00
|
|
|
delimFound = decoded.length() - 1;
|
|
|
|
return decoded.substr(found + toFind.length(), delimFound - (found + toFind.length()));
|
2012-01-31 04:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2012-01-31 04:16:31 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void SettingsHandler::Decrypt()
|
|
|
|
{
|
2017-01-27 15:01:25 +00:00
|
|
|
const u8* str = m_buffer.data();
|
2020-03-20 15:07:35 +00:00
|
|
|
while (m_position < m_buffer.size())
|
2013-08-25 00:49:58 +00:00
|
|
|
{
|
2012-01-31 04:16:31 +00:00
|
|
|
decoded.push_back((u8)(m_buffer[m_position] ^ m_key));
|
|
|
|
m_position++;
|
|
|
|
str++;
|
2012-02-10 05:04:07 +00:00
|
|
|
m_key = (m_key >> 31) | (m_key << 1);
|
2012-01-31 04:16:31 +00:00
|
|
|
}
|
2020-03-14 21:27:26 +00:00
|
|
|
|
|
|
|
// Decryption done. Now get rid of all CR in the output.
|
|
|
|
// The decoded file is supposed to contain Windows line endings
|
|
|
|
// (CR-LF), but sometimes also contains CR-LF-LF endings which
|
|
|
|
// confuse the parsing code, so let's just get rid of all CR
|
|
|
|
// line endings.
|
|
|
|
|
|
|
|
decoded.erase(std::remove(decoded.begin(), decoded.end(), '\x0d'), decoded.end());
|
2012-01-31 04:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SettingsHandler::Reset()
|
|
|
|
{
|
|
|
|
decoded = "";
|
|
|
|
m_position = 0;
|
2013-08-27 12:57:08 +00:00
|
|
|
m_key = INITIAL_SEED;
|
2017-01-27 15:01:25 +00:00
|
|
|
m_buffer = {};
|
2012-01-31 04:16:31 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 08:11:53 +00:00
|
|
|
void SettingsHandler::AddSetting(std::string_view key, std::string_view value)
|
2012-01-31 04:16:31 +00:00
|
|
|
{
|
2014-08-30 20:14:56 +00:00
|
|
|
for (const char& c : key)
|
|
|
|
{
|
2014-02-08 05:50:37 +00:00
|
|
|
WriteByte(c);
|
2012-01-31 04:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WriteByte('=');
|
|
|
|
|
2014-08-30 20:14:56 +00:00
|
|
|
for (const char& c : value)
|
|
|
|
{
|
2014-02-08 05:50:37 +00:00
|
|
|
WriteByte(c);
|
2012-01-31 04:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WriteByte(13);
|
|
|
|
WriteByte(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SettingsHandler::WriteByte(u8 b)
|
|
|
|
{
|
2017-01-27 15:01:25 +00:00
|
|
|
if (m_position >= m_buffer.size())
|
2012-01-31 04:16:31 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_buffer[m_position] = b ^ m_key;
|
|
|
|
m_position++;
|
2012-02-10 05:04:07 +00:00
|
|
|
m_key = (m_key >> 31) | (m_key << 1);
|
2012-01-31 04:16:31 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 15:11:36 +00:00
|
|
|
std::string SettingsHandler::GenerateSerialNumber()
|
2012-01-31 04:16:31 +00:00
|
|
|
{
|
2017-08-08 15:23:46 +00: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.
|
2019-07-16 08:23:18 +00:00
|
|
|
return fmt::format("{:%j%H%M%S}", *std::localtime(&t));
|
2012-02-10 05:04:07 +00:00
|
|
|
}
|
2018-05-12 17:39:35 +00:00
|
|
|
} // namespace Common
|