| 
									
										
										
										
											2013-08-28 00:57:08 +12:00
										 |  |  | // Copyright 2013 Dolphin Emulator Project
 | 
					
						
							|  |  |  | // Licensed under GPLv2
 | 
					
						
							|  |  |  | // 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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-29 21:41:35 +13:00
										 |  |  | #include "Common.h"
 | 
					
						
							|  |  |  | #include "CommonPaths.h"
 | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | #include "Timer.h"
 | 
					
						
							|  |  |  | #include "SettingsHandler.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-29 21:41:35 +13:00
										 |  |  | #include <time.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | #include <Windows.h>
 | 
					
						
							|  |  |  | #include <mmsystem.h>
 | 
					
						
							|  |  |  | #include <sys/timeb.h>
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #include <sys/time.h>
 | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SettingsHandler::SettingsHandler() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Reset(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const u8* SettingsHandler::GetData() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return m_buffer; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 21:04:07 -08:00
										 |  |  | const std::string SettingsHandler::GetValue(const std::string key) | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | { | 
					
						
							|  |  |  | 	std::string delim = std::string("\r\n"); | 
					
						
							|  |  |  | 	std::string toFind = delim + key + "="; | 
					
						
							|  |  |  | 	size_t found = decoded.find(toFind); | 
					
						
							| 
									
										
										
										
											2012-02-09 21:04:07 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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 | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | 		toFind = key + "="; | 
					
						
							| 
									
										
										
										
											2013-08-29 01:33:24 -04:00
										 |  |  | 		found = decoded.find(toFind); | 
					
						
							| 
									
										
										
										
											2012-02-09 21:04:07 -08:00
										 |  |  | 		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())); | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ""; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SettingsHandler::Decrypt() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const u8 *str = m_buffer; | 
					
						
							| 
									
										
										
										
											2012-02-09 21:04:07 -08:00
										 |  |  | 	while (*str != 0) | 
					
						
							| 
									
										
										
										
											2013-08-25 02:49:58 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | 		if (m_position >= SETTINGS_SIZE) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		decoded.push_back((u8)(m_buffer[m_position] ^ m_key)); | 
					
						
							|  |  |  | 		m_position++; | 
					
						
							|  |  |  | 		str++; | 
					
						
							| 
									
										
										
										
											2012-02-09 21:04:07 -08:00
										 |  |  | 		m_key = (m_key >> 31) | (m_key << 1); | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SettingsHandler::Reset() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	decoded = ""; | 
					
						
							|  |  |  | 	m_position = 0; | 
					
						
							| 
									
										
										
										
											2013-08-28 00:57:08 +12:00
										 |  |  | 	m_key = INITIAL_SEED; | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | 	memset(m_buffer, 0, SETTINGS_SIZE); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SettingsHandler::AddSetting(const char *key, const char *value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	while (*key != 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		WriteByte(*key); | 
					
						
							|  |  |  | 		key++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	WriteByte('='); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (*value != 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		WriteByte(*value); | 
					
						
							|  |  |  | 		value++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	WriteByte(13); | 
					
						
							|  |  |  | 	WriteByte(10); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SettingsHandler::WriteByte(u8 b) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (m_position >= SETTINGS_SIZE) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m_buffer[m_position] = b ^ m_key; | 
					
						
							|  |  |  | 	m_position++; | 
					
						
							| 
									
										
										
										
											2012-02-09 21:04:07 -08:00
										 |  |  | 	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
										 |  |  | { | 
					
						
							|  |  |  | 	time_t rawtime; | 
					
						
							| 
									
										
										
										
											2012-02-09 21:04:07 -08:00
										 |  |  | 	tm *timeinfo; | 
					
						
							|  |  |  | 	char buffer[12]; | 
					
						
							|  |  |  | 	char serialNumber[12]; | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 21:04:07 -08:00
										 |  |  | 	time(&rawtime); | 
					
						
							|  |  |  | 	timeinfo = localtime(&rawtime); | 
					
						
							|  |  |  | 	strftime(buffer, 11, "%j%H%M%S", timeinfo); | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 21:04:07 -08:00
										 |  |  | 	snprintf(serialNumber, 11, "%s%i", buffer, (Common::Timer::GetTimeMs() >> 1) & 0xF); | 
					
						
							| 
									
										
										
										
											2012-01-31 17:16:31 +13:00
										 |  |  | 	serialNumber[10] = 0; | 
					
						
							|  |  |  | 	return std::string(serialNumber); | 
					
						
							| 
									
										
										
										
											2012-02-09 21:04:07 -08:00
										 |  |  | } |