Pack movie data in memory
[lsnes.git] / src / core / misc.cpp
blobaab6fead2b55d1ab906a37d0c587d3da5646ef58
1 #include "lsnes.hpp"
3 #include "core/memorymanip.hpp"
4 #include "core/misc.hpp"
5 #include "core/rom.hpp"
6 #include "core/window.hpp"
8 #include <sstream>
9 #include <iostream>
10 #include <iomanip>
11 #include <fstream>
12 #include <string>
13 #include <vector>
14 #include <algorithm>
15 #include <ctime>
16 #include <cstdlib>
17 #include <cstring>
18 #include <boost/filesystem.hpp>
20 namespace
22 std::string rseed;
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();
30 std::vector<char> x;
31 x.resize(s.length());
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);
41 time_t tnew;
42 uint64_t loops = 0;
43 uint64_t base = 0;
44 int cnt = 0;
45 while(cnt < 3) {
46 tnew = time(NULL);
47 if(tnew > told) {
48 told = tnew;
49 cnt++;
50 str << (loops - base) << " ";
51 base = loops;
53 loops++;
55 return str.str();
59 std::string get_random_hexstring(size_t length) throw(std::bad_alloc)
61 std::string out;
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;
71 rseed = str.str();
74 void set_random_seed() throw(std::bad_alloc)
76 //Try /dev/urandom first.
78 std::ifstream r("/dev/urandom", std::ios::binary);
79 if(r.is_open()) {
80 char buf[64];
81 r.read(buf, 64);
82 std::string s(buf, 64);
83 set_random_seed(s);
84 return;
87 //Fall back to time.
88 std::ostringstream str;
89 str << collect_identifying_information() << " " << time(NULL);
90 set_random_seed(str.str());
93 //VERY dirty hack.
94 namespace foobar
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
107 struct sha256_opaque
110 * \brief Opaque internal state structure of SHA256
112 sha256_ctx shactx;
115 sha256::sha256() throw(std::bad_alloc)
117 opaque = new sha256_opaque();
118 finished = false;
119 sha256_init(&opaque->shactx);
122 sha256::~sha256() throw()
124 delete opaque;
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()
134 if(!finished)
135 sha256_final(&opaque->shactx);
136 finished = true;
137 sha256_hash(&opaque->shactx, hashout);
140 std::string sha256::read() throw(std::bad_alloc)
142 uint8_t b[32];
143 read(b);
144 return sha256::tostring(b);
147 void sha256::hash(uint8_t* hashout, const uint8_t* data, size_t datalen) throw()
149 sha256 s;
150 s.write(data, datalen);
151 s.read(hashout);
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];
159 return str.str();
162 struct loaded_rom load_rom_from_commandline(std::vector<std::string> cmdline) throw(std::bad_alloc,
163 std::runtime_error)
165 struct rom_files f;
166 try {
167 f = rom_files(cmdline);
168 f.resolve_relative();
169 } catch(std::bad_alloc& e) {
170 OOM_panic();
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 << "'"
177 << std::endl;
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 << "'"
180 << std::endl;
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 << "'"
183 << std::endl;
185 struct loaded_rom r;
186 try {
187 r = loaded_rom(f);
188 r.do_patch(cmdline);
189 } catch(std::bad_alloc& e) {
190 OOM_panic();
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
200 << std::endl;
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
203 << std::endl;
204 return r;
207 void dump_region_map() throw(std::bad_alloc)
209 std::vector<struct memory_region> regions = get_regions();
210 for(auto i : regions) {
211 char buf[256];
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;
222 exit(1);
225 std::string get_config_path() throw(std::bad_alloc)
227 const char* tmp;
228 std::string basedir;
229 if((tmp = getenv("APPDATA"))) {
230 //If $APPDATA exists, it is the base directory
231 basedir = tmp;
232 } else if((tmp = getenv("XDG_CONFIG_HOME"))) {
233 //If $XDG_CONFIG_HOME exists, it is the base directory
234 basedir = tmp;
235 } else if((tmp = getenv("HOME"))) {
236 //If $HOME exists, the base directory is '.config' there.
237 basedir = std::string(tmp) + "/.config";
238 } else {
239 //Last chance: Return current directory.
240 return ".";
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;
247 fatal_error();
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());
252 FILE* x;
253 if(!(x = fopen(tfile.c_str(), "w+"))) {
254 messages << "FATAL: Configuration directory '" << lsnes_path << "' is not writable" << std::endl;
255 fatal_error();
257 fclose(x);
258 remove(tfile.c_str());
259 return lsnes_path;
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());
268 if(x) {
269 x.close();
270 return;
272 std::ofstream y(rcfile.c_str());
273 if(!y) {
274 messages << "FATAL: lsnes.rc (" << rcfile << ") doesn't exist nor it can be created" << std::endl;
275 fatal_error();
277 y.write(lsnesrc_file, strlen(lsnesrc_file));
278 y.close();
282 void OOM_panic()
284 messages << "FATAL: Out of memory!" << std::endl;
285 fatal_error();
288 std::ostream& _messages()
290 return platform::out();
293 uint32_t gcd(uint32_t a, uint32_t b) throw()
295 if(b == 0)
296 return a;
297 else
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;
306 return y.str();
309 std::string bsnes_core_version;
310 std::string lsnes_version = "1-β2";