Reorganize the window code a bit
[lsnes.git] / generic / misc.cpp
blobcdca8f53da9a48aa1f737fb439d0de0678c138a1
1 #include "lsnes.hpp"
2 #include "memorymanip.hpp"
3 #include "window.hpp"
4 #include "misc.hpp"
5 #include "rom.hpp"
6 #include <sstream>
7 #include <iostream>
8 #include <iomanip>
9 #include <fstream>
10 #include <string>
11 #include <vector>
12 #include <algorithm>
13 #include <ctime>
14 #include <cstdlib>
15 #include <cstring>
16 #include <boost/filesystem.hpp>
18 #ifdef NO_TIME_INTERCEPT
19 time_t __real_time(time_t* t)
21 return time(t);
23 #endif
25 namespace
27 std::string rseed;
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();
35 std::vector<char> x;
36 x.resize(s.length());
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);
46 time_t tnew;
47 uint64_t loops = 0;
48 uint64_t base = 0;
49 int cnt = 0;
50 while(cnt < 3) {
51 tnew = __real_time(NULL);
52 if(tnew > told) {
53 told = tnew;
54 cnt++;
55 str << (loops - base) << " ";
56 base = loops;
58 loops++;
60 return str.str();
64 std::string get_random_hexstring(size_t length) throw(std::bad_alloc)
66 std::string out;
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;
76 rseed = str.str();
79 void set_random_seed() throw(std::bad_alloc)
81 //Try /dev/urandom first.
83 std::ifstream r("/dev/urandom", std::ios::binary);
84 if(r.is_open()) {
85 char buf[64];
86 r.read(buf, 64);
87 std::string s(buf, 64);
88 set_random_seed(s);
89 return;
92 //Fall back to time.
93 std::ostringstream str;
94 str << collect_identifying_information() << " " << __real_time(NULL);
95 set_random_seed(str.str());
98 //VERY dirty hack.
99 namespace foobar
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
112 struct sha256_opaque
115 * \brief Opaque internal state structure of SHA256
117 sha256_ctx shactx;
120 sha256::sha256() throw(std::bad_alloc)
122 opaque = new sha256_opaque();
123 finished = false;
124 sha256_init(&opaque->shactx);
127 sha256::~sha256() throw()
129 delete opaque;
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()
139 if(!finished)
140 sha256_final(&opaque->shactx);
141 finished = true;
142 sha256_hash(&opaque->shactx, hashout);
145 std::string sha256::read() throw(std::bad_alloc)
147 uint8_t b[32];
148 read(b);
149 return sha256::tostring(b);
152 void sha256::hash(uint8_t* hashout, const uint8_t* data, size_t datalen) throw()
154 sha256 s;
155 s.write(data, datalen);
156 s.read(hashout);
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];
164 return str.str();
167 struct loaded_rom load_rom_from_commandline(std::vector<std::string> cmdline) throw(std::bad_alloc,
168 std::runtime_error)
170 struct rom_files f;
171 try {
172 f = rom_files(cmdline);
173 f.resolve_relative();
174 } catch(std::bad_alloc& e) {
175 OOM_panic();
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 << "'"
182 << std::endl;
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 << "'"
185 << std::endl;
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 << "'"
188 << std::endl;
190 struct loaded_rom r;
191 try {
192 r = loaded_rom(f);
193 r.do_patch(cmdline);
194 } catch(std::bad_alloc& e) {
195 OOM_panic();
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
205 << std::endl;
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
208 << std::endl;
209 return r;
212 void dump_region_map() throw(std::bad_alloc)
214 std::vector<struct memory_region> regions = get_regions();
215 for(auto i : regions) {
216 char buf[256];
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;
227 exit(1);
230 std::string get_config_path() throw(std::bad_alloc)
232 const char* tmp;
233 std::string basedir;
234 if((tmp = getenv("APPDATA"))) {
235 //If $APPDATA exists, it is the base directory
236 basedir = tmp;
237 } else if((tmp = getenv("XDG_CONFIG_HOME"))) {
238 //If $XDG_CONFIG_HOME exists, it is the base directory
239 basedir = tmp;
240 } else if((tmp = getenv("HOME"))) {
241 //If $HOME exists, the base directory is '.config' there.
242 basedir = std::string(tmp) + "/.config";
243 } else {
244 //Last chance: Return current directory.
245 return ".";
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;
252 fatal_error();
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());
257 FILE* x;
258 if(!(x = fopen(tfile.c_str(), "w+"))) {
259 messages << "FATAL: Configuration directory '" << lsnes_path << "' is not writable" << std::endl;
260 fatal_error();
262 fclose(x);
263 remove(tfile.c_str());
264 return lsnes_path;
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());
273 if(x) {
274 x.close();
275 return;
277 std::ofstream y(rcfile.c_str());
278 if(!y) {
279 messages << "FATAL: lsnes.rc (" << rcfile << ") doesn't exist nor it can be created" << std::endl;
280 fatal_error();
282 y.write(lsnesrc_file, strlen(lsnesrc_file));
283 y.close();
287 void OOM_panic()
289 messages << "FATAL: Out of memory!" << std::endl;
290 fatal_error();
293 std::ostream& _messages()
295 return window::out();
298 uint32_t gcd(uint32_t a, uint32_t b) throw()
300 if(b == 0)
301 return a;
302 else
303 return gcd(b, a % b);
306 std::string bsnes_core_version;
307 std::string lsnes_version = "0-β17";