2 #include "memorymanip.hpp"
16 #include <boost/filesystem.hpp>
18 #ifdef NO_TIME_INTERCEPT
19 time_t __real_time(time_t* t
)
28 uint64_t rcounter
= 0;
30 std::string
get_random_hexstring_64(size_t index
)
32 std::ostringstream str
;
33 str
<< rseed
<< " " << __real_time(NULL
) << " " << (rcounter
++) << " " << index
;
34 std::string s
= str
.str();
37 std::copy(s
.begin(), s
.end(), x
.begin());
38 return sha256::hash(reinterpret_cast<uint8_t*>(&x
[0]), x
.size());
41 std::string
collect_identifying_information()
43 //TODO: Collect as much identifying information as possible.
44 std::ostringstream str
;
45 time_t told
= __real_time(NULL
);
51 tnew
= __real_time(NULL
);
55 str
<< (loops
- base
) << " ";
64 std::string
get_random_hexstring(size_t length
) throw(std::bad_alloc
)
67 for(size_t i
= 0; i
< length
; i
+= 64)
68 out
= out
+ get_random_hexstring_64(i
);
69 return out
.substr(0, length
);
72 void set_random_seed(const std::string
& seed
) throw(std::bad_alloc
)
74 std::ostringstream str
;
75 str
<< seed
.length() << " " << seed
;
79 void set_random_seed() throw(std::bad_alloc
)
81 //Try /dev/urandom first.
83 std::ifstream
r("/dev/urandom", std::ios::binary
);
87 std::string
s(buf
, 64);
93 std::ostringstream str
;
94 str
<< collect_identifying_information() << " " << __real_time(NULL
);
95 set_random_seed(str
.str());
101 #include <nall/sha256.hpp>
103 using foobar::nall::sha256_ctx
;
104 using foobar::nall::sha256_init
;
105 using foobar::nall::sha256_final
;
106 using foobar::nall::sha256_hash
;
107 using foobar::nall::sha256_chunk
;
110 * \brief Opaque internal state of SHA256
115 * \brief Opaque internal state structure of SHA256
120 sha256::sha256() throw(std::bad_alloc
)
122 opaque
= new sha256_opaque();
124 sha256_init(&opaque
->shactx
);
127 sha256::~sha256() throw()
132 void sha256::write(const uint8_t* data
, size_t datalen
) throw()
134 sha256_chunk(&opaque
->shactx
, data
, datalen
);
137 void sha256::read(uint8_t* hashout
) throw()
140 sha256_final(&opaque
->shactx
);
142 sha256_hash(&opaque
->shactx
, hashout
);
145 std::string
sha256::read() throw(std::bad_alloc
)
149 return sha256::tostring(b
);
152 void sha256::hash(uint8_t* hashout
, const uint8_t* data
, size_t datalen
) throw()
155 s
.write(data
, datalen
);
159 std::string
sha256::tostring(const uint8_t* hashout
) throw(std::bad_alloc
)
161 std::ostringstream str
;
162 for(unsigned i
= 0; i
< 32; i
++)
163 str
<< std::hex
<< std::setw(2) << std::setfill('0') << (unsigned)hashout
[i
];
167 struct loaded_rom
load_rom_from_commandline(std::vector
<std::string
> cmdline
) throw(std::bad_alloc
,
172 f
= rom_files(cmdline
);
173 f
.resolve_relative();
174 } catch(std::bad_alloc
& e
) {
176 } catch(std::exception
& e
) {
177 throw std::runtime_error(std::string("Can't resolve ROM files: ") + e
.what());
179 messages
<< "ROM type: " << gtype::tostring(f
.rtype
, f
.region
) << std::endl
;
180 if(f
.rom
!= "") messages
<< name_subrom(f
.rtype
, 0) << " file: '" << f
.rom
<< "'" << std::endl
;
181 if(f
.rom_xml
!= "") messages
<< name_subrom(f
.rtype
, 1) << " file: '" << f
.rom_xml
<< "'"
183 if(f
.slota
!= "") messages
<< name_subrom(f
.rtype
, 2) << " file: '" << f
.slota
<< "'" << std::endl
;
184 if(f
.slota_xml
!= "") messages
<< name_subrom(f
.rtype
, 3) << " file: '" << f
.slota_xml
<< "'"
186 if(f
.slotb
!= "") messages
<< name_subrom(f
.rtype
, 4) << " file: '" << f
.slotb
<< "'" << std::endl
;
187 if(f
.slotb_xml
!= "") messages
<< name_subrom(f
.rtype
, 5) << " file: '" << f
.slotb_xml
<< "'"
194 } catch(std::bad_alloc
& e
) {
196 } catch(std::exception
& e
) {
197 throw std::runtime_error(std::string("Can't load ROM: ") + e
.what());
200 std::string not_present
= "N/A";
201 if(r
.rom
.valid
) messages
<< name_subrom(f
.rtype
, 0) << " hash: " << r
.rom
.sha256
<< std::endl
;
202 if(r
.rom_xml
.valid
) messages
<< name_subrom(f
.rtype
, 1) << " hash: " << r
.rom_xml
.sha256
<< std::endl
;
203 if(r
.slota
.valid
) messages
<< name_subrom(f
.rtype
, 2) << " hash: " << r
.slota
.sha256
<< std::endl
;
204 if(r
.slota_xml
.valid
) messages
<< name_subrom(f
.rtype
, 3) << " hash: " << r
.slota_xml
.sha256
206 if(r
.slotb
.valid
) messages
<< name_subrom(f
.rtype
, 4) << " hash: " << r
.slotb
.sha256
<< std::endl
;
207 if(r
.slotb_xml
.valid
) messages
<< name_subrom(f
.rtype
, 5) << " hash: " << r
.slotb_xml
.sha256
212 void dump_region_map() throw(std::bad_alloc
)
214 std::vector
<struct memory_region
> regions
= get_regions();
215 for(auto i
: regions
) {
217 sprintf(buf
, "Region: %08X-%08X %08X %s%c %s", i
.baseaddr
, i
.lastaddr
, i
.size
,
218 i
.readonly
? "R-" : "RW", i
.native_endian
? 'N' : 'B', i
.region_name
.c_str());
219 messages
<< buf
<< std::endl
;
223 void fatal_error() throw()
225 window::fatal_error();
226 std::cout
<< "PANIC: Fatal error, can't continue." << std::endl
;
230 std::string
get_config_path() throw(std::bad_alloc
)
234 if((tmp
= getenv("APPDATA"))) {
235 //If $APPDATA exists, it is the base directory
237 } else if((tmp
= getenv("XDG_CONFIG_HOME"))) {
238 //If $XDG_CONFIG_HOME exists, it is the base directory
240 } else if((tmp
= getenv("HOME"))) {
241 //If $HOME exists, the base directory is '.config' there.
242 basedir
= std::string(tmp
) + "/.config";
244 //Last chance: Return current directory.
247 //Try to create 'lsnes'. If it exists (or is created) and is directory, great. Otherwise error out.
248 std::string lsnes_path
= basedir
+ "/lsnes";
249 boost::filesystem::path
p(lsnes_path
);
250 if(!boost::filesystem::create_directories(p
) && !boost::filesystem::is_directory(p
)) {
251 messages
<< "FATAL: Can't create configuration directory '" << lsnes_path
<< "'" << std::endl
;
254 //Yes, this is racy, but portability is more important than being absolutely correct...
255 std::string tfile
= lsnes_path
+ "/test";
256 remove(tfile
.c_str());
258 if(!(x
= fopen(tfile
.c_str(), "w+"))) {
259 messages
<< "FATAL: Configuration directory '" << lsnes_path
<< "' is not writable" << std::endl
;
263 remove(tfile
.c_str());
267 extern const char* lsnesrc_file
;
269 void create_lsnesrc()
271 std::string rcfile
= get_config_path() + "/lsnes.rc";
272 std::ifstream
x(rcfile
.c_str());
277 std::ofstream
y(rcfile
.c_str());
279 messages
<< "FATAL: lsnes.rc (" << rcfile
<< ") doesn't exist nor it can be created" << std::endl
;
282 y
.write(lsnesrc_file
, strlen(lsnesrc_file
));
289 messages
<< "FATAL: Out of memory!" << std::endl
;
293 std::ostream
& _messages()
295 return window::out();
298 uint32_t gcd(uint32_t a
, uint32_t b
) throw()
303 return gcd(b
, a
% b
);
306 std::string bsnes_core_version
;
307 std::string lsnes_version
= "0-β17";