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>
23 uint64_t rcounter
= 0;
25 std::string
get_random_hexstring_64(size_t index
)
27 std::ostringstream str
;
28 str
<< rseed
<< " " << time(NULL
) << " " << (rcounter
++) << " " << index
;
29 std::string s
= str
.str();
32 std::copy(s
.begin(), s
.end(), x
.begin());
33 return sha256::hash(reinterpret_cast<uint8_t*>(&x
[0]), x
.size());
36 std::string
collect_identifying_information()
38 //TODO: Collect as much identifying information as possible.
39 std::ostringstream str
;
40 time_t told
= time(NULL
);
50 str
<< (loops
- base
) << " ";
59 std::string
get_random_hexstring(size_t length
) throw(std::bad_alloc
)
62 for(size_t i
= 0; i
< length
; i
+= 64)
63 out
= out
+ get_random_hexstring_64(i
);
64 return out
.substr(0, length
);
67 void set_random_seed(const std::string
& seed
) throw(std::bad_alloc
)
69 std::ostringstream str
;
70 str
<< seed
.length() << " " << seed
;
74 void set_random_seed() throw(std::bad_alloc
)
76 //Try /dev/urandom first.
78 std::ifstream
r("/dev/urandom", std::ios::binary
);
82 std::string
s(buf
, 64);
88 std::ostringstream str
;
89 str
<< collect_identifying_information() << " " << time(NULL
);
90 set_random_seed(str
.str());
96 #include <nall/sha256.hpp>
98 using foobar::nall::sha256_ctx
;
99 using foobar::nall::sha256_init
;
100 using foobar::nall::sha256_final
;
101 using foobar::nall::sha256_hash
;
102 using foobar::nall::sha256_chunk
;
105 * \brief Opaque internal state of SHA256
110 * \brief Opaque internal state structure of SHA256
115 sha256::sha256() throw(std::bad_alloc
)
117 opaque
= new sha256_opaque();
119 sha256_init(&opaque
->shactx
);
122 sha256::~sha256() throw()
127 void sha256::write(const uint8_t* data
, size_t datalen
) throw()
129 sha256_chunk(&opaque
->shactx
, data
, datalen
);
132 void sha256::read(uint8_t* hashout
) throw()
135 sha256_final(&opaque
->shactx
);
137 sha256_hash(&opaque
->shactx
, hashout
);
140 std::string
sha256::read() throw(std::bad_alloc
)
144 return sha256::tostring(b
);
147 void sha256::hash(uint8_t* hashout
, const uint8_t* data
, size_t datalen
) throw()
150 s
.write(data
, datalen
);
154 std::string
sha256::tostring(const uint8_t* hashout
) throw(std::bad_alloc
)
156 std::ostringstream str
;
157 for(unsigned i
= 0; i
< 32; i
++)
158 str
<< std::hex
<< std::setw(2) << std::setfill('0') << (unsigned)hashout
[i
];
162 struct loaded_rom
load_rom_from_commandline(std::vector
<std::string
> cmdline
) throw(std::bad_alloc
,
167 f
= rom_files(cmdline
);
168 f
.resolve_relative();
169 } catch(std::bad_alloc
& e
) {
171 } catch(std::exception
& e
) {
172 throw std::runtime_error(std::string("Can't resolve ROM files: ") + e
.what());
174 messages
<< "ROM type: " << gtype::tostring(f
.rtype
, f
.region
) << std::endl
;
175 if(f
.rom
!= "") messages
<< name_subrom(f
.rtype
, 0) << " file: '" << f
.rom
<< "'" << std::endl
;
176 if(f
.rom_xml
!= "") messages
<< name_subrom(f
.rtype
, 1) << " file: '" << f
.rom_xml
<< "'"
178 if(f
.slota
!= "") messages
<< name_subrom(f
.rtype
, 2) << " file: '" << f
.slota
<< "'" << std::endl
;
179 if(f
.slota_xml
!= "") messages
<< name_subrom(f
.rtype
, 3) << " file: '" << f
.slota_xml
<< "'"
181 if(f
.slotb
!= "") messages
<< name_subrom(f
.rtype
, 4) << " file: '" << f
.slotb
<< "'" << std::endl
;
182 if(f
.slotb_xml
!= "") messages
<< name_subrom(f
.rtype
, 5) << " file: '" << f
.slotb_xml
<< "'"
189 } catch(std::bad_alloc
& e
) {
191 } catch(std::exception
& e
) {
192 throw std::runtime_error(std::string("Can't load ROM: ") + e
.what());
195 std::string not_present
= "N/A";
196 if(r
.rom
.valid
) messages
<< name_subrom(f
.rtype
, 0) << " hash: " << r
.rom
.sha256
<< std::endl
;
197 if(r
.rom_xml
.valid
) messages
<< name_subrom(f
.rtype
, 1) << " hash: " << r
.rom_xml
.sha256
<< std::endl
;
198 if(r
.slota
.valid
) messages
<< name_subrom(f
.rtype
, 2) << " hash: " << r
.slota
.sha256
<< std::endl
;
199 if(r
.slota_xml
.valid
) messages
<< name_subrom(f
.rtype
, 3) << " hash: " << r
.slota_xml
.sha256
201 if(r
.slotb
.valid
) messages
<< name_subrom(f
.rtype
, 4) << " hash: " << r
.slotb
.sha256
<< std::endl
;
202 if(r
.slotb_xml
.valid
) messages
<< name_subrom(f
.rtype
, 5) << " hash: " << r
.slotb_xml
.sha256
207 void dump_region_map() throw(std::bad_alloc
)
209 std::vector
<struct memory_region
> regions
= get_regions();
210 for(auto i
: regions
) {
212 sprintf(buf
, "Region: %08X-%08X %08X %s%c %s", i
.baseaddr
, i
.lastaddr
, i
.size
,
213 i
.readonly
? "R-" : "RW", i
.native_endian
? 'N' : 'L', i
.region_name
.c_str());
214 messages
<< buf
<< std::endl
;
218 void fatal_error() throw()
220 platform::fatal_error();
221 std::cout
<< "PANIC: Fatal error, can't continue." << std::endl
;
225 std::string
get_config_path() throw(std::bad_alloc
)
229 if((tmp
= getenv("APPDATA"))) {
230 //If $APPDATA exists, it is the base directory
232 } else if((tmp
= getenv("XDG_CONFIG_HOME"))) {
233 //If $XDG_CONFIG_HOME exists, it is the base directory
235 } else if((tmp
= getenv("HOME"))) {
236 //If $HOME exists, the base directory is '.config' there.
237 basedir
= std::string(tmp
) + "/.config";
239 //Last chance: Return current directory.
242 //Try to create 'lsnes'. If it exists (or is created) and is directory, great. Otherwise error out.
243 std::string lsnes_path
= basedir
+ "/lsnes";
244 boost::filesystem::path
p(lsnes_path
);
245 if(!boost::filesystem::create_directories(p
) && !boost::filesystem::is_directory(p
)) {
246 messages
<< "FATAL: Can't create configuration directory '" << lsnes_path
<< "'" << std::endl
;
249 //Yes, this is racy, but portability is more important than being absolutely correct...
250 std::string tfile
= lsnes_path
+ "/test";
251 remove(tfile
.c_str());
253 if(!(x
= fopen(tfile
.c_str(), "w+"))) {
254 messages
<< "FATAL: Configuration directory '" << lsnes_path
<< "' is not writable" << std::endl
;
258 remove(tfile
.c_str());
262 extern const char* lsnesrc_file
;
264 void create_lsnesrc()
266 std::string rcfile
= get_config_path() + "/lsnes.rc";
267 std::ifstream
x(rcfile
.c_str());
272 std::ofstream
y(rcfile
.c_str());
274 messages
<< "FATAL: lsnes.rc (" << rcfile
<< ") doesn't exist nor it can be created" << std::endl
;
277 y
.write(lsnesrc_file
, strlen(lsnesrc_file
));
284 messages
<< "FATAL: Out of memory!" << std::endl
;
288 std::ostream
& _messages()
290 return platform::out();
293 uint32_t gcd(uint32_t a
, uint32_t b
) throw()
298 return gcd(b
, a
% b
);
301 std::string
format_address(void* addr
)
303 unsigned long x
= (unsigned long)addr
;
304 std::ostringstream y
;
305 y
<< "0x" << std::hex
<< std::setfill('0') << std::setw(2 * sizeof(unsigned long)) << x
;
309 std::string bsnes_core_version
;
310 std::string lsnes_version
= "1-β2";