1 #ifndef _misc__hpp__included__
2 #define _misc__hpp__included__
6 #include <boost/lexical_cast.hpp>
9 * \brief Get random hexes
11 * Get string of random hex characters of specified length.
13 * \param length The number of hex characters to return.
14 * \return The random hexadecimal string.
15 * \throws std::bad_alloc Not enough memory.
17 std::string
get_random_hexstring(size_t length
) throw(std::bad_alloc
);
20 * \brief Set random seed
22 * This function sets the random seed to use.
24 * \param seed The value to use as seed.
25 * \throw std::bad_alloc Not enough memory.
27 void set_random_seed(const std::string
& seed
) throw(std::bad_alloc
);
30 * \brief Set random seed to (hopefully) unique value
32 * This function sets the random seed to value that should only be used once. Note, the value is not necressarily
33 * crypto-secure, even if it is unique.
35 * \throw std::bad_alloc Not enough memory.
37 void set_random_seed() throw(std::bad_alloc
);
42 * Given commandline arguments, load a ROM.
44 * \param cmdline The command line.
45 * \return The loaded ROM set.
46 * \throws std::bad_alloc Not enough memory.
47 * \throws std::runtime_error Can't load the ROMset.
49 struct loaded_rom
load_rom_from_commandline(std::vector
<std::string
> cmdline
) throw(std::bad_alloc
,
53 * \brief Dump listing of regions to graphics system messages.
55 * \throws std::bad_alloc Not enough memory.
57 void dump_region_map() throw(std::bad_alloc
);
64 void fatal_error() throw();
67 * \brief Get path to config directory.
69 * \return The config directory path.
70 * \throw std::bad_alloc Not enough memory.
72 std::string
get_config_path() throw(std::bad_alloc
);
75 * \brief Panic on OOM.
80 * messages -> window::out().
82 std::ostream
& _messages();
83 #define messages _messages()
86 * \brief Typeconvert string.
88 template<typename T
> inline T
parse_value(const std::string
& value
) throw(std::bad_alloc
, std::runtime_error
)
91 //Hack, since lexical_cast lets negative values slip through.
92 if(!std::numeric_limits
<T
>::is_signed
&& value
.length() && value
[0] == '-') {
93 throw std::runtime_error("Unsigned values can't be negative");
95 return boost::lexical_cast
<T
>(value
);
96 } catch(std::exception
& e
) {
97 throw std::runtime_error("Can't parse value '" + value
+ "': " + e
.what());
101 template<> inline std::string
parse_value(const std::string
& value
) throw(std::bad_alloc
, std::runtime_error
)
106 void create_lsnesrc();
109 * \brief Opaque internal state of SHA256
111 struct sha256_opaque
;
114 * \brief SHA-256 function
116 * This class implements interface to SHA-256.
122 * \brief Create new SHA-256 context
124 * Creates new SHA-256 context, initially containing empty data.
126 sha256() throw(std::bad_alloc
);
134 * \brief Append data to be hashed
136 * This function appends specified data to be hashed. Don't call after calling read().
138 * \param data The data to write.
139 * \param datalen The length of data written.
141 void write(const uint8_t* data
, size_t datalen
) throw();
144 * \brief Read the hash value
146 * Reads the hash of data written. Can be called multiple times, but after the first call, data can't be appended
149 * \param hashout 32-byte buffer to store the hash to.
151 void read(uint8_t* hashout
) throw();
154 * \brief Read the hash value
156 * Similar to read(uint8_t*) but instead returns the hash as hexadecimal string.
158 * \return The hash in hex form.
159 * \throw std::bad_alloc Not enough memory.
161 std::string
read() throw(std::bad_alloc
);
164 * \brief Hash block of data.
166 * Hashes block of data.
168 * \param hashout 32-byte buffer to write the hash to.
169 * \param data The data to hash.
170 * \param datalen The length of data hashed.
172 static void hash(uint8_t* hashout
, const uint8_t* data
, size_t datalen
) throw();
175 * \brief Hash block of data.
177 * Hashes block of data.
179 * \param hashout 32-byte buffer to write the hash to.
180 * \param data The data to hash.
182 static void hash(uint8_t* hashout
, const std::vector
<uint8_t>& data
) throw()
184 hash(hashout
, &data
[0], data
.size());
188 * \brief Hash block of data.
190 * Hashes block of data.
192 * \param hashout 32-byte buffer to write the hash to.
193 * \param data The data to hash.
195 static void hash(uint8_t* hashout
, const std::vector
<char>& data
) throw()
197 hash(hashout
, reinterpret_cast<const uint8_t*>(&data
[0]), data
.size());
201 * \brief Hash block of data.
203 * Hashes block of data.
205 * \param data The data to hash.
206 * \param datalen The length of data hashed.
207 * \return Hexadecimal hash of the data.
209 static std::string
hash(const uint8_t* data
, size_t datalen
) throw(std::bad_alloc
)
212 hash(hashout
, data
, datalen
);
213 return tostring(hashout
);
217 * \brief Hash block of data.
219 * Hashes block of data.
221 * \param data The data to hash.
222 * \return Hexadecimal hash of the data.
224 static std::string
hash(const std::vector
<uint8_t>& data
) throw(std::bad_alloc
)
227 hash(hashout
, &data
[0], data
.size());
228 return tostring(hashout
);
232 * \brief Hash block of data.
234 * Hashes block of data.
236 * \param data The data to hash.
237 * \return Hexadecimal hash of the data.
239 static std::string
hash(const std::vector
<char>& data
) throw(std::bad_alloc
)
242 hash(hashout
, reinterpret_cast<const uint8_t*>(&data
[0]), data
.size());
243 return tostring(hashout
);
247 * \brief Translate binary hash to hexadecimal hash
249 * Reads 32-byte binary hash from hashout and returns 64-hex hexadecimal hash.
251 * \param hashout The binary hash
252 * \return Hexadecimal hash
253 * \throws std::bad_alloc Not enough memory.
255 static std::string
tostring(const uint8_t* hashout
) throw(std::bad_alloc
);
257 sha256(const sha256
& x
) throw();
258 sha256
& operator=(const sha256
& x
) throw();
259 sha256_opaque
* opaque
;