1 #ifndef _misc__hpp__included__
2 #define _misc__hpp__included__
6 #include <boost/lexical_cast.hpp>
9 * \brief Recognize "foo", "foo (anything)" and "foo\t(anything)".
11 * \param haystack The string to search.
12 * \param needle The string to find.
13 * \return True if found, false if not.
15 bool is_cmd_prefix(const std::string
& haystack
, const std::string
& needle
) throw();
18 * \brief Get random hexes
20 * Get string of random hex characters of specified length.
22 * \param length The number of hex characters to return.
23 * \return The random hexadecimal string.
24 * \throws std::bad_alloc Not enough memory.
26 std::string
get_random_hexstring(size_t length
) throw(std::bad_alloc
);
29 * \brief Set random seed
31 * This function sets the random seed to use.
33 * \param seed The value to use as seed.
34 * \throw std::bad_alloc Not enough memory.
36 void set_random_seed(const std::string
& seed
) throw(std::bad_alloc
);
39 * \brief Set random seed to (hopefully) unique value
41 * This function sets the random seed to value that should only be used once. Note, the value is not necressarily
42 * crypto-secure, even if it is unique.
44 * \throw std::bad_alloc Not enough memory.
46 void set_random_seed() throw(std::bad_alloc
);
51 * Given commandline arguments, load a ROM.
53 * \param cmdline The command line.
54 * \return The loaded ROM set.
55 * \throws std::bad_alloc Not enough memory.
56 * \throws std::runtime_error Can't load the ROMset.
58 struct loaded_rom
load_rom_from_commandline(std::vector
<std::string
> cmdline
) throw(std::bad_alloc
,
62 * \brief Dump listing of regions to graphics system messages.
64 * \throws std::bad_alloc Not enough memory.
66 void dump_region_map() throw(std::bad_alloc
);
73 void fatal_error() throw();
76 * \brief Get path to config directory.
78 * \return The config directory path.
79 * \throw std::bad_alloc Not enough memory.
81 std::string
get_config_path() throw(std::bad_alloc
);
84 * \brief Panic on OOM.
89 * \brief Typeconvert string.
91 template<typename T
> inline T
parse_value(const std::string
& value
) throw(std::bad_alloc
, std::runtime_error
)
94 //Hack, since lexical_cast lets negative values slip through.
95 if(!std::numeric_limits
<T
>::is_signed
&& value
.length() && value
[0] == '-') {
96 throw std::runtime_error("Unsigned values can't be negative");
98 return boost::lexical_cast
<T
>(value
);
99 } catch(std::exception
& e
) {
100 throw std::runtime_error("Can't parse value '" + value
+ "': " + e
.what());
104 template<> inline std::string
parse_value(const std::string
& value
) throw(std::bad_alloc
, std::runtime_error
)
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.
125 * \brief Create new SHA-256 context
127 * Creates new SHA-256 context, initially containing empty data.
129 sha256() throw(std::bad_alloc
);
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
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
)
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
)
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
)
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
);
260 sha256(const sha256
& x
) throw();
261 sha256
& operator=(const sha256
& x
) throw();
262 sha256_opaque
* opaque
;