SettingsHandler.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2012 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. // Thanks to Treeki for writing the original class - 29/01/2012
  5. #include <cstddef>
  6. #include <cstdio>
  7. #include <cstring>
  8. #include <ctime>
  9. #include <string>
  10. #ifdef _WIN32
  11. #include <mmsystem.h>
  12. #include <sys/timeb.h>
  13. #include <windows.h>
  14. #include "Common/CommonFuncs.h" // snprintf
  15. #endif
  16. #include "Common/CommonTypes.h"
  17. #include "Common/SettingsHandler.h"
  18. #include "Common/Timer.h"
  19. SettingsHandler::SettingsHandler()
  20. {
  21. Reset();
  22. }
  23. const u8* SettingsHandler::GetData() const
  24. {
  25. return m_buffer;
  26. }
  27. const std::string SettingsHandler::GetValue(const std::string& key)
  28. {
  29. std::string delim = std::string("\r\n");
  30. std::string toFind = delim + key + "=";
  31. size_t found = decoded.find(toFind);
  32. if (found != decoded.npos)
  33. {
  34. size_t delimFound = decoded.find(delim, found + toFind.length());
  35. if (delimFound == decoded.npos)
  36. delimFound = decoded.length() - 1;
  37. return decoded.substr(found + toFind.length(), delimFound - (found + toFind.length()));
  38. }
  39. else
  40. {
  41. toFind = key + "=";
  42. found = decoded.find(toFind);
  43. if (found == 0)
  44. {
  45. size_t delimFound = decoded.find(delim, found + toFind.length());
  46. if (delimFound == decoded.npos)
  47. delimFound = decoded.length() - 1;
  48. return decoded.substr(found + toFind.length(), delimFound - (found + toFind.length()));
  49. }
  50. }
  51. return "";
  52. }
  53. void SettingsHandler::Decrypt()
  54. {
  55. const u8 *str = m_buffer;
  56. while (*str != 0)
  57. {
  58. if (m_position >= SETTINGS_SIZE)
  59. return;
  60. decoded.push_back((u8)(m_buffer[m_position] ^ m_key));
  61. m_position++;
  62. str++;
  63. m_key = (m_key >> 31) | (m_key << 1);
  64. }
  65. }
  66. void SettingsHandler::Reset()
  67. {
  68. decoded = "";
  69. m_position = 0;
  70. m_key = INITIAL_SEED;
  71. memset(m_buffer, 0, SETTINGS_SIZE);
  72. }
  73. void SettingsHandler::AddSetting(const std::string& key, const std::string& value)
  74. {
  75. for (const char& c : key)
  76. {
  77. WriteByte(c);
  78. }
  79. WriteByte('=');
  80. for (const char& c : value)
  81. {
  82. WriteByte(c);
  83. }
  84. WriteByte(13);
  85. WriteByte(10);
  86. }
  87. void SettingsHandler::WriteByte(u8 b)
  88. {
  89. if (m_position >= SETTINGS_SIZE)
  90. return;
  91. m_buffer[m_position] = b ^ m_key;
  92. m_position++;
  93. m_key = (m_key >> 31) | (m_key << 1);
  94. }
  95. const std::string SettingsHandler::generateSerialNumber()
  96. {
  97. time_t rawtime;
  98. tm *timeinfo;
  99. char buffer[12];
  100. char serialNumber[12];
  101. time(&rawtime);
  102. timeinfo = localtime(&rawtime);
  103. strftime(buffer, 11, "%j%H%M%S", timeinfo);
  104. snprintf(serialNumber, 11, "%s%i", buffer, (Common::Timer::GetTimeMs() >> 1) & 0xF);
  105. serialNumber[10] = 0;
  106. return std::string(serialNumber);
  107. }