| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2012 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2013-08-28 00:57:08 +12:00
										 |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Thanks to Treeki for writing the original class - 29/01/2012
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include <cstddef>
 | 
					
						
							|  |  |  | #include <cstdio>
 | 
					
						
							|  |  |  | #include <cstring>
 | 
					
						
							| 
									
										
										
										
											2014-02-19 01:54:11 +01:00
										 |  |  | #include <ctime>
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2012-01-29 21:41:35 +13:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | #include <mmsystem.h>
 | 
					
						
							|  |  |  | #include <sys/timeb.h>
 | 
					
						
							| 
									
										
										
										
											2014-02-19 01:54:11 +01:00
										 |  |  | #include <windows.h>
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | #include "Common/CommonFuncs.h"  // snprintf
 | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include "Common/CommonTypes.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-19 01:54:11 +01:00
										 |  |  | #include "Common/SettingsHandler.h"
 | 
					
						
							|  |  |  | #include "Common/Timer.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | SettingsHandler::SettingsHandler() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   Reset(); | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const u8* SettingsHandler::GetData() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return m_buffer; | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 18:50:37 +13:00
										 |  |  | const std::string SettingsHandler::GetValue(const std::string& key) | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02: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 17:16:31 +13:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SettingsHandler::Decrypt() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   const u8* str = m_buffer; | 
					
						
							|  |  |  |   while (*str != 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     if (m_position >= SETTINGS_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-31 17:16:31 +13:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SettingsHandler::Reset() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   decoded = ""; | 
					
						
							|  |  |  |   m_position = 0; | 
					
						
							|  |  |  |   m_key = INITIAL_SEED; | 
					
						
							|  |  |  |   memset(m_buffer, 0, SETTINGS_SIZE); | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 18:50:37 +13:00
										 |  |  | void SettingsHandler::AddSetting(const std::string& key, const std::string& value) | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   for (const char& c : key) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     WriteByte(c); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   WriteByte('='); | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   for (const char& c : value) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     WriteByte(c); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   WriteByte(13); | 
					
						
							|  |  |  |   WriteByte(10); | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SettingsHandler::WriteByte(u8 b) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (m_position >= SETTINGS_SIZE) | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   m_buffer[m_position] = b ^ m_key; | 
					
						
							|  |  |  |   m_position++; | 
					
						
							|  |  |  |   m_key = (m_key >> 31) | (m_key << 1); | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 21:04:07 -08:00
										 |  |  | const std::string SettingsHandler::generateSerialNumber() | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   time_t rawtime; | 
					
						
							|  |  |  |   tm* timeinfo; | 
					
						
							|  |  |  |   char buffer[12]; | 
					
						
							|  |  |  |   char serialNumber[12]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   time(&rawtime); | 
					
						
							|  |  |  |   timeinfo = localtime(&rawtime); | 
					
						
							|  |  |  |   strftime(buffer, 11, "%j%H%M%S", timeinfo); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   snprintf(serialNumber, 11, "%s%i", buffer, (Common::Timer::GetTimeMs() >> 1) & 0xF); | 
					
						
							|  |  |  |   serialNumber[10] = 0; | 
					
						
							|  |  |  |   return std::string(serialNumber); | 
					
						
							| 
									
										
										
										
											2012-02-09 21:04:07 -08:00
										 |  |  | } |