lsnes rr0-β23
[lsnes.git] / include / core / misc.hpp
blobc85ab95212cb2fe59882c4cb922b3a96ede8de42
1 #ifndef _misc__hpp__included__
2 #define _misc__hpp__included__
4 #include <string>
5 #include <vector>
6 #include <stdexcept>
7 #include <boost/lexical_cast.hpp>
9 /**
10 * \brief Get random hexes
12 * Get string of random hex characters of specified length.
14 * \param length The number of hex characters to return.
15 * \return The random hexadecimal string.
16 * \throws std::bad_alloc Not enough memory.
18 std::string get_random_hexstring(size_t length) throw(std::bad_alloc);
20 /**
21 * \brief Set random seed
23 * This function sets the random seed to use.
25 * \param seed The value to use as seed.
26 * \throw std::bad_alloc Not enough memory.
28 void set_random_seed(const std::string& seed) throw(std::bad_alloc);
30 /**
31 * \brief Set random seed to (hopefully) unique value
33 * This function sets the random seed to value that should only be used once. Note, the value is not necressarily
34 * crypto-secure, even if it is unique.
36 * \throw std::bad_alloc Not enough memory.
38 void set_random_seed() throw(std::bad_alloc);
40 /**
41 * \brief Load a ROM.
43 * Given commandline arguments, load a ROM.
45 * \param cmdline The command line.
46 * \return The loaded ROM set.
47 * \throws std::bad_alloc Not enough memory.
48 * \throws std::runtime_error Can't load the ROMset.
50 struct loaded_rom load_rom_from_commandline(std::vector<std::string> cmdline) throw(std::bad_alloc,
51 std::runtime_error);
53 /**
54 * \brief Dump listing of regions to graphics system messages.
56 * \throws std::bad_alloc Not enough memory.
58 void dump_region_map() throw(std::bad_alloc);
60 /**
61 * \brief Fatal error.
63 * Fatal error.
65 void fatal_error() throw();
67 /**
68 * \brief Get path to config directory.
70 * \return The config directory path.
71 * \throw std::bad_alloc Not enough memory.
73 std::string get_config_path() throw(std::bad_alloc);
75 /**
76 * \brief Panic on OOM.
78 void OOM_panic();
80 /**
81 * messages -> window::out().
83 std::ostream& _messages();
84 #define messages _messages()
86 /**
87 * \brief Typeconvert string.
89 template<typename T> inline T parse_value(const std::string& value) throw(std::bad_alloc, std::runtime_error)
91 try {
92 //Hack, since lexical_cast lets negative values slip through.
93 if(!std::numeric_limits<T>::is_signed && value.length() && value[0] == '-') {
94 throw std::runtime_error("Unsigned values can't be negative");
96 return boost::lexical_cast<T>(value);
97 } catch(std::exception& e) {
98 throw std::runtime_error("Can't parse value '" + value + "': " + e.what());
102 template<> inline std::string parse_value(const std::string& value) throw(std::bad_alloc, std::runtime_error)
104 return value;
107 uint32_t gcd(uint32_t a, uint32_t b) throw();
109 void create_lsnesrc();
112 * \brief Opaque internal state of SHA256
114 struct sha256_opaque;
117 * \brief SHA-256 function
119 * This class implements interface to SHA-256.
121 class sha256
123 public:
125 * \brief Create new SHA-256 context
127 * Creates new SHA-256 context, initially containing empty data.
129 sha256() throw(std::bad_alloc);
132 * \brief Destructor
134 ~sha256() throw();
137 * \brief Append data to be hashed
139 * This function appends specified data to be hashed. Don't call after calling read().
141 * \param data The data to write.
142 * \param datalen The length of data written.
144 void write(const uint8_t* data, size_t datalen) throw();
147 * \brief Read the hash value
149 * Reads the hash of data written. Can be called multiple times, but after the first call, data can't be appended
150 * anymore.
152 * \param hashout 32-byte buffer to store the hash to.
154 void read(uint8_t* hashout) throw();
157 * \brief Read the hash value
159 * Similar to read(uint8_t*) but instead returns the hash as hexadecimal string.
161 * \return The hash in hex form.
162 * \throw std::bad_alloc Not enough memory.
164 std::string read() throw(std::bad_alloc);
167 * \brief Hash block of data.
169 * Hashes block of data.
171 * \param hashout 32-byte buffer to write the hash to.
172 * \param data The data to hash.
173 * \param datalen The length of data hashed.
175 static void hash(uint8_t* hashout, const uint8_t* data, size_t datalen) throw();
178 * \brief Hash block of data.
180 * Hashes block of data.
182 * \param hashout 32-byte buffer to write the hash to.
183 * \param data The data to hash.
185 static void hash(uint8_t* hashout, const std::vector<uint8_t>& data) throw()
187 hash(hashout, &data[0], data.size());
191 * \brief Hash block of data.
193 * Hashes block of data.
195 * \param hashout 32-byte buffer to write the hash to.
196 * \param data The data to hash.
198 static void hash(uint8_t* hashout, const std::vector<char>& data) throw()
200 hash(hashout, reinterpret_cast<const uint8_t*>(&data[0]), data.size());
204 * \brief Hash block of data.
206 * Hashes block of data.
208 * \param data The data to hash.
209 * \param datalen The length of data hashed.
210 * \return Hexadecimal hash of the data.
212 static std::string hash(const uint8_t* data, size_t datalen) throw(std::bad_alloc)
214 uint8_t hashout[32];
215 hash(hashout, data, datalen);
216 return tostring(hashout);
220 * \brief Hash block of data.
222 * Hashes block of data.
224 * \param data The data to hash.
225 * \return Hexadecimal hash of the data.
227 static std::string hash(const std::vector<uint8_t>& data) throw(std::bad_alloc)
229 uint8_t hashout[32];
230 hash(hashout, &data[0], data.size());
231 return tostring(hashout);
235 * \brief Hash block of data.
237 * Hashes block of data.
239 * \param data The data to hash.
240 * \return Hexadecimal hash of the data.
242 static std::string hash(const std::vector<char>& data) throw(std::bad_alloc)
244 uint8_t hashout[32];
245 hash(hashout, reinterpret_cast<const uint8_t*>(&data[0]), data.size());
246 return tostring(hashout);
250 * \brief Translate binary hash to hexadecimal hash
252 * Reads 32-byte binary hash from hashout and returns 64-hex hexadecimal hash.
254 * \param hashout The binary hash
255 * \return Hexadecimal hash
256 * \throws std::bad_alloc Not enough memory.
258 static std::string tostring(const uint8_t* hashout) throw(std::bad_alloc);
259 private:
260 sha256(const sha256& x) throw();
261 sha256& operator=(const sha256& x) throw();
262 sha256_opaque* opaque;
263 bool finished;
266 #endif