3 #include "core/memorymanip.hpp"
4 #include "core/misc.hpp"
5 #include "core/rom.hpp"
6 #include "core/window.hpp"
18 #include <boost/filesystem.hpp>
20 #ifdef NO_TIME_INTERCEPT
21 time_t __real_time(time_t* t
)
30 uint64_t rcounter
= 0;
32 std::string
get_random_hexstring_64(size_t index
)
34 std::ostringstream str
;
35 str
<< rseed
<< " " << __real_time(NULL
) << " " << (rcounter
++) << " " << index
;
36 std::string s
= str
.str();
39 std::copy(s
.begin(), s
.end(), x
.begin());
40 return sha256::hash(reinterpret_cast<uint8_t*>(&x
[0]), x
.size());
43 std::string
collect_identifying_information()
45 //TODO: Collect as much identifying information as possible.
46 std::ostringstream str
;
47 time_t told
= __real_time(NULL
);
53 tnew
= __real_time(NULL
);
57 str
<< (loops
- base
) << " ";
66 std::string
get_random_hexstring(size_t length
) throw(std::bad_alloc
)
69 for(size_t i
= 0; i
< length
; i
+= 64)
70 out
= out
+ get_random_hexstring_64(i
);
71 return out
.substr(0, length
);
74 void set_random_seed(const std::string
& seed
) throw(std::bad_alloc
)
76 std::ostringstream str
;
77 str
<< seed
.length() << " " << seed
;
81 void set_random_seed() throw(std::bad_alloc
)
83 //Try /dev/urandom first.
85 std::ifstream
r("/dev/urandom", std::ios::binary
);
89 std::string
s(buf
, 64);
95 std::ostringstream str
;
96 str
<< collect_identifying_information() << " " << __real_time(NULL
);
97 set_random_seed(str
.str());
103 #include <nall/sha256.hpp>
105 using foobar::nall::sha256_ctx
;
106 using foobar::nall::sha256_init
;
107 using foobar::nall::sha256_final
;
108 using foobar::nall::sha256_hash
;
109 using foobar::nall::sha256_chunk
;
112 * \brief Opaque internal state of SHA256
117 * \brief Opaque internal state structure of SHA256
122 sha256::sha256() throw(std::bad_alloc
)
124 opaque
= new sha256_opaque();
126 sha256_init(&opaque
->shactx
);
129 sha256::~sha256() throw()
134 void sha256::write(const uint8_t* data
, size_t datalen
) throw()
136 sha256_chunk(&opaque
->shactx
, data
, datalen
);
139 void sha256::read(uint8_t* hashout
) throw()
142 sha256_final(&opaque
->shactx
);
144 sha256_hash(&opaque
->shactx
, hashout
);
147 std::string
sha256::read() throw(std::bad_alloc
)
151 return sha256::tostring(b
);
154 void sha256::hash(uint8_t* hashout
, const uint8_t* data
, size_t datalen
) throw()
157 s
.write(data
, datalen
);
161 std::string
sha256::tostring(const uint8_t* hashout
) throw(std::bad_alloc
)
163 std::ostringstream str
;
164 for(unsigned i
= 0; i
< 32; i
++)
165 str
<< std::hex
<< std::setw(2) << std::setfill('0') << (unsigned)hashout
[i
];
169 struct loaded_rom
load_rom_from_commandline(std::vector
<std::string
> cmdline
) throw(std::bad_alloc
,
174 f
= rom_files(cmdline
);
175 f
.resolve_relative();
176 } catch(std::bad_alloc
& e
) {
178 } catch(std::exception
& e
) {
179 throw std::runtime_error(std::string("Can't resolve ROM files: ") + e
.what());
181 messages
<< "ROM type: " << gtype::tostring(f
.rtype
, f
.region
) << std::endl
;
182 if(f
.rom
!= "") messages
<< name_subrom(f
.rtype
, 0) << " file: '" << f
.rom
<< "'" << std::endl
;
183 if(f
.rom_xml
!= "") messages
<< name_subrom(f
.rtype
, 1) << " file: '" << f
.rom_xml
<< "'"
185 if(f
.slota
!= "") messages
<< name_subrom(f
.rtype
, 2) << " file: '" << f
.slota
<< "'" << std::endl
;
186 if(f
.slota_xml
!= "") messages
<< name_subrom(f
.rtype
, 3) << " file: '" << f
.slota_xml
<< "'"
188 if(f
.slotb
!= "") messages
<< name_subrom(f
.rtype
, 4) << " file: '" << f
.slotb
<< "'" << std::endl
;
189 if(f
.slotb_xml
!= "") messages
<< name_subrom(f
.rtype
, 5) << " file: '" << f
.slotb_xml
<< "'"
196 } catch(std::bad_alloc
& e
) {
198 } catch(std::exception
& e
) {
199 throw std::runtime_error(std::string("Can't load ROM: ") + e
.what());
202 std::string not_present
= "N/A";
203 if(r
.rom
.valid
) messages
<< name_subrom(f
.rtype
, 0) << " hash: " << r
.rom
.sha256
<< std::endl
;
204 if(r
.rom_xml
.valid
) messages
<< name_subrom(f
.rtype
, 1) << " hash: " << r
.rom_xml
.sha256
<< std::endl
;
205 if(r
.slota
.valid
) messages
<< name_subrom(f
.rtype
, 2) << " hash: " << r
.slota
.sha256
<< std::endl
;
206 if(r
.slota_xml
.valid
) messages
<< name_subrom(f
.rtype
, 3) << " hash: " << r
.slota_xml
.sha256
208 if(r
.slotb
.valid
) messages
<< name_subrom(f
.rtype
, 4) << " hash: " << r
.slotb
.sha256
<< std::endl
;
209 if(r
.slotb_xml
.valid
) messages
<< name_subrom(f
.rtype
, 5) << " hash: " << r
.slotb_xml
.sha256
214 void dump_region_map() throw(std::bad_alloc
)
216 std::vector
<struct memory_region
> regions
= get_regions();
217 for(auto i
: regions
) {
219 sprintf(buf
, "Region: %08X-%08X %08X %s%c %s", i
.baseaddr
, i
.lastaddr
, i
.size
,
220 i
.readonly
? "R-" : "RW", i
.native_endian
? 'N' : 'B', i
.region_name
.c_str());
221 messages
<< buf
<< std::endl
;
225 void fatal_error() throw()
227 window::fatal_error();
228 std::cout
<< "PANIC: Fatal error, can't continue." << std::endl
;
232 std::string
get_config_path() throw(std::bad_alloc
)
236 if((tmp
= getenv("APPDATA"))) {
237 //If $APPDATA exists, it is the base directory
239 } else if((tmp
= getenv("XDG_CONFIG_HOME"))) {
240 //If $XDG_CONFIG_HOME exists, it is the base directory
242 } else if((tmp
= getenv("HOME"))) {
243 //If $HOME exists, the base directory is '.config' there.
244 basedir
= std::string(tmp
) + "/.config";
246 //Last chance: Return current directory.
249 //Try to create 'lsnes'. If it exists (or is created) and is directory, great. Otherwise error out.
250 std::string lsnes_path
= basedir
+ "/lsnes";
251 boost::filesystem::path
p(lsnes_path
);
252 if(!boost::filesystem::create_directories(p
) && !boost::filesystem::is_directory(p
)) {
253 messages
<< "FATAL: Can't create configuration directory '" << lsnes_path
<< "'" << std::endl
;
256 //Yes, this is racy, but portability is more important than being absolutely correct...
257 std::string tfile
= lsnes_path
+ "/test";
258 remove(tfile
.c_str());
260 if(!(x
= fopen(tfile
.c_str(), "w+"))) {
261 messages
<< "FATAL: Configuration directory '" << lsnes_path
<< "' is not writable" << std::endl
;
265 remove(tfile
.c_str());
269 extern const char* lsnesrc_file
;
271 void create_lsnesrc()
273 std::string rcfile
= get_config_path() + "/lsnes.rc";
274 std::ifstream
x(rcfile
.c_str());
279 std::ofstream
y(rcfile
.c_str());
281 messages
<< "FATAL: lsnes.rc (" << rcfile
<< ") doesn't exist nor it can be created" << std::endl
;
284 y
.write(lsnesrc_file
, strlen(lsnesrc_file
));
291 messages
<< "FATAL: Out of memory!" << std::endl
;
295 std::ostream
& _messages()
297 return window::out();
300 uint32_t gcd(uint32_t a
, uint32_t b
) throw()
305 return gcd(b
, a
% b
);
308 std::string bsnes_core_version
;
309 std::string lsnes_version
= "0-β19";