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
|
|
|
|
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2014-02-19 00:54:11 +00:00
|
|
|
#include <ctime>
|
2017-08-08 15:23:46 +00:00
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <string>
|
2012-01-29 08:41:35 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2017-01-23 07:08:45 +00:00
|
|
|
#include <windows.h>
|
2012-01-29 08:41:35 +00:00
|
|
|
#include <mmsystem.h>
|
|
|
|
#include <sys/timeb.h>
|
2012-01-31 04:16:31 +00:00
|
|
|
#endif
|
|
|
|
|
2014-02-20 03:11:52 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2017-01-15 20:46:32 +00:00
|
|
|
#include "Common/File.h"
|
2017-01-27 15:01:25 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2014-02-19 00:54:11 +00:00
|
|
|
#include "Common/SettingsHandler.h"
|
|
|
|
#include "Common/Timer.h"
|
|
|
|
|
2012-01-31 04:16:31 +00:00
|
|
|
SettingsHandler::SettingsHandler()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
Reset();
|
2012-01-31 04:16:31 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 15:01:25 +00:00
|
|
|
bool SettingsHandler::Open(const std::string& settings_file_path)
|
|
|
|
{
|
|
|
|
Reset();
|
|
|
|
|
|
|
|
File::IOFile file{settings_file_path, "rb"};
|
|
|
|
if (!file.ReadBytes(m_buffer.data(), m_buffer.size()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Decrypt();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SettingsHandler::Save(const std::string& destination_file_path) const
|
|
|
|
{
|
|
|
|
if (!File::CreateFullPath(destination_file_path))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
File::IOFile file{destination_file_path, "wb"};
|
|
|
|
return file.WriteBytes(m_buffer.data(), m_buffer.size());
|
|
|
|
}
|
|
|
|
|
2012-01-31 04:16:31 +00:00
|
|
|
const u8* SettingsHandler::GetData() const
|
|
|
|
{
|
2017-01-27 15:01:25 +00:00
|
|
|
return m_buffer.data();
|
2012-01-31 04:16:31 +00:00
|
|
|
}
|
|
|
|
|
2014-02-08 05:50:37 +00:00
|
|
|
const std::string SettingsHandler::GetValue(const std::string& key)
|
2012-01-31 04:16:31 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
std::string delim = std::string("\r\n");
|
|
|
|
std::string toFind = delim + key + "=";
|
|
|
|
size_t found = decoded.find(toFind);
|
|
|
|
|
|
|
|
if (found != decoded.npos)
|
|
|
|
{
|
|
|
|
size_t delimFound = decoded.find(delim, found + toFind.length());
|
|
|
|
if (delimFound == decoded.npos)
|
|
|
|
delimFound = decoded.length() - 1;
|
|
|
|
return decoded.substr(found + toFind.length(), delimFound - (found + toFind.length()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
toFind = key + "=";
|
|
|
|
found = decoded.find(toFind);
|
|
|
|
if (found == 0)
|
|
|
|
{
|
|
|
|
size_t delimFound = decoded.find(delim, found + toFind.length());
|
|
|
|
if (delimFound == decoded.npos)
|
|
|
|
delimFound = decoded.length() - 1;
|
|
|
|
return decoded.substr(found + toFind.length(), delimFound - (found + toFind.length()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
2012-01-31 04:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SettingsHandler::Decrypt()
|
|
|
|
{
|
2017-01-27 15:01:25 +00:00
|
|
|
const u8* str = m_buffer.data();
|
2016-06-24 08:43:46 +00:00
|
|
|
while (*str != 0)
|
|
|
|
{
|
2017-01-27 15:01:25 +00:00
|
|
|
if (m_position >= m_buffer.size())
|
2016-06-24 08:43:46 +00:00
|
|
|
return;
|
|
|
|
decoded.push_back((u8)(m_buffer[m_position] ^ m_key));
|
|
|
|
m_position++;
|
|
|
|
str++;
|
|
|
|
m_key = (m_key >> 31) | (m_key << 1);
|
|
|
|
}
|
2012-01-31 04:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SettingsHandler::Reset()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
decoded = "";
|
|
|
|
m_position = 0;
|
|
|
|
m_key = INITIAL_SEED;
|
2017-01-27 15:01:25 +00:00
|
|
|
m_buffer = {};
|
2012-01-31 04:16:31 +00:00
|
|
|
}
|
|
|
|
|
2014-02-08 05:50:37 +00:00
|
|
|
void SettingsHandler::AddSetting(const std::string& key, const std::string& value)
|
2012-01-31 04:16:31 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
for (const char& c : key)
|
|
|
|
{
|
|
|
|
WriteByte(c);
|
|
|
|
}
|
2012-01-31 04:16:31 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
WriteByte('=');
|
2012-01-31 04:16:31 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
for (const char& c : value)
|
|
|
|
{
|
|
|
|
WriteByte(c);
|
|
|
|
}
|
2012-01-31 04:16:31 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
WriteByte(13);
|
|
|
|
WriteByte(10);
|
2012-01-31 04:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SettingsHandler::WriteByte(u8 b)
|
|
|
|
{
|
2017-01-27 15:01:25 +00:00
|
|
|
if (m_position >= m_buffer.size())
|
2016-06-24 08:43:46 +00:00
|
|
|
return;
|
2012-01-31 04:16:31 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_buffer[m_position] = b ^ m_key;
|
|
|
|
m_position++;
|
|
|
|
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.
|
|
|
|
std::stringstream stream;
|
|
|
|
stream << std::put_time(std::localtime(&t), "%j%H%M%S");
|
|
|
|
return stream.str();
|
2012-02-10 05:04:07 +00:00
|
|
|
}
|