dolphin/Source/Core/Common/SettingsHandler.cpp

132 lines
2.8 KiB
C++
Raw Normal View History

// Copyright 2012 Dolphin Emulator Project
2015-05-17 17:08:10 -06:00
// Licensed under GPLv2+
2013-08-27 06:57:08 -06:00
// Refer to the license.txt file included.
2012-01-30 21:16:31 -07:00
// Thanks to Treeki for writing the original class - 29/01/2012
#include "Common/SettingsHandler.h"
#include <cstddef>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <string>
#include "Common/CommonTypes.h"
namespace Common
{
2012-01-30 21:16:31 -07:00
SettingsHandler::SettingsHandler()
{
Reset();
2012-01-30 21:16:31 -07:00
}
SettingsHandler::SettingsHandler(Buffer&& buffer)
{
SetBytes(std::move(buffer));
}
const SettingsHandler::Buffer& SettingsHandler::GetBytes() const
{
return m_buffer;
}
void SettingsHandler::SetBytes(Buffer&& buffer)
2012-01-30 21:16:31 -07:00
{
Reset();
m_buffer = std::move(buffer);
Decrypt();
2012-01-30 21:16:31 -07:00
}
2018-05-10 12:34:20 -06:00
std::string SettingsHandler::GetValue(const std::string& key) const
2012-01-30 21:16:31 -07: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-30 21:16:31 -07:00
}
void SettingsHandler::Decrypt()
{
const u8* str = m_buffer.data();
while (*str != 0)
{
if (m_position >= m_buffer.size())
return;
decoded.push_back((u8)(m_buffer[m_position] ^ m_key));
m_position++;
str++;
m_key = (m_key >> 31) | (m_key << 1);
}
2012-01-30 21:16:31 -07:00
}
void SettingsHandler::Reset()
{
decoded = "";
m_position = 0;
m_key = INITIAL_SEED;
m_buffer = {};
2012-01-30 21:16:31 -07:00
}
void SettingsHandler::AddSetting(const std::string& key, const std::string& value)
2012-01-30 21:16:31 -07:00
{
for (const char& c : key)
{
WriteByte(c);
}
2012-01-30 21:16:31 -07:00
WriteByte('=');
2012-01-30 21:16:31 -07:00
for (const char& c : value)
{
WriteByte(c);
}
2012-01-30 21:16:31 -07:00
WriteByte(13);
WriteByte(10);
2012-01-30 21:16:31 -07:00
}
void SettingsHandler::WriteByte(u8 b)
{
if (m_position >= m_buffer.size())
return;
2012-01-30 21:16:31 -07:00
m_buffer[m_position] = b ^ m_key;
m_position++;
m_key = (m_key >> 31) | (m_key << 1);
2012-01-30 21:16:31 -07:00
}
std::string SettingsHandler::GenerateSerialNumber()
2012-01-30 21:16:31 -07: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-09 22:04:07 -07:00
}
} // namespace Common