Clean up hexadecimal<->string conversion

This commit is contained in:
Ilari Liusvaara 2013-12-20 18:59:11 +02:00
parent 78cade7daa
commit 59a5b4eefa
30 changed files with 341 additions and 247 deletions

View file

@ -1,19 +0,0 @@
#ifndef _library__bintohex__hpp__included__
#define _library__bintohex__hpp__included__
#include <cstdint>
#include <string>
#include <cstdlib>
/**
* Transform binary data into hex string.
*
* Parameter data: The data to transform.
* Parameter datalen: Number of bytes of data.
* Returns: Hex string.
* Throws std::bad_alloc: Not enough memory.
*/
std::string binary_to_hex(const uint8_t* data, size_t datalen) throw(std::bad_alloc);
#endif

109
include/library/hex.hpp Normal file
View file

@ -0,0 +1,109 @@
#ifndef _library__hex__hpp__included__
#define _library__hex__hpp__included__
#include <cstdint>
#include <string>
#include <cstdlib>
#include <stdexcept>
namespace hex
{
/**
* Transform binary data into hex string.
*
* Parameter data: The data to transform.
* Parameter datalen: Number of bytes of data.
* Parameter uppercase: Use uppercase?
* Returns: Hex string.
* Throws std::bad_alloc: Not enough memory.
*/
std::string b_to(const uint8_t* data, size_t datalen, bool uppercase = false) throw(std::bad_alloc);
/**
* Transform unsigned integer into full-width hexadecimal.
*
* Parameter data: The number to transform.
* Parameter prefix: Add the '0x' prefix if true.
* Returns: The hex string.
* Throws std::bad_alloc: Not enough memory.
*/
template<typename T> std::string to(T data, bool prefix = false) throw(std::bad_alloc);
/**
* Transform uint8 into full-width hexadecimal.
*
* Parameter data: The number to transform.
* Parameter prefix: Add the '0x' prefix if true.
* Returns: The hex string.
* Throws std::bad_alloc: Not enough memory.
*/
inline std::string to8(uint8_t data, bool prefix = false) throw(std::bad_alloc) { return to<uint8_t>(data, prefix); }
/**
* Transform uint16 into full-width hexadecimal.
*
* Parameter data: The number to transform.
* Parameter prefix: Add the '0x' prefix if true.
* Returns: The hex string.
* Throws std::bad_alloc: Not enough memory.
*/
inline std::string to16(uint16_t data, bool prefix = false) throw(std::bad_alloc)
{
return to<uint16_t>(data, prefix);
}
/**
* Transform uint24 into full-width hexadecimal.
*
* Parameter data: The number to transform.
* Parameter prefix: Add the '0x' prefix if true.
* Returns: The hex string.
* Throws std::bad_alloc: Not enough memory.
*/
std::string to24(uint32_t data, bool prefix = false) throw(std::bad_alloc);
/**
* Transform uint32 into full-width hexadecimal.
*
* Parameter data: The number to transform.
* Parameter prefix: Add the '0x' prefix if true.
* Returns: The hex string.
* Throws std::bad_alloc: Not enough memory.
*/
inline std::string to32(uint32_t data, bool prefix = false) throw(std::bad_alloc)
{
return to<uint32_t>(data, prefix);
}
/**
* Transform uint64 into full-width hexadecimal.
*
* Parameter data: The number to transform.
* Parameter prefix: Add the '0x' prefix if true.
* Returns: The hex string.
* Throws std::bad_alloc: Not enough memory.
*/
inline std::string to64(uint64_t data, bool prefix = false) throw(std::bad_alloc)
{
return to<uint64_t>(data, prefix);
}
/**
* Transform hexadecimal into binary.
*
* Parameter buf: Buffer to write binary to. The size needs to be half of the hexadecimal string, rounded up.
* Parameter hex: The hexadecimal string.
* Throws std::runtime_error: Bad hexadecimal character in string.
*/
void b_from(uint8_t* buf, const std::string& hex) throw(std::runtime_error);
/**
* Transform hexadecimal into unsigned integer.
*
* Parameter hex: The hexadecimal string.
* Throws std::runtime_error: Bad hexadecimal character in string.
*/
template<typename T> T from(const std::string& hex) throw(std::runtime_error);
}
#endif

View file

@ -1,7 +1,7 @@
#ifndef _library__sha256__hpp__included__
#define _library__sha256__hpp__included__
#include "bintohex.hpp"
#include "hex.hpp"
#include <cstdint>
#include <cstring>
#include <vector>
@ -67,7 +67,7 @@ public:
*/
static std::string tostring(const uint8_t* hashout) throw(std::bad_alloc)
{
return binary_to_hex(hashout, 32);
return hex::b_to(hashout, 32);
}
/**

View file

@ -1,6 +1,6 @@
#include "core/command.hpp"
#include "interface/disassembler.hpp"
#include "library/bintohex.hpp"
#include "library/hex.hpp"
#include "library/minmax.hpp"
#include "core/memorymanip.hpp"
#include "core/window.hpp"
@ -69,9 +69,7 @@ namespace
std::vector<unsigned char> tmp;
tmp.resize(i.len);
lsnes_memory.read_range(i.addr, &tmp[0], i.len);
std::string l = (stringfmt() << std::setw(16) << std::setfill('0') << std::hex
<< i.addr).str() + " " + binary_to_hex(&tmp[0], i.len) + " "
+ i.disasm;
std::string l = hex::to(i.addr) + " " + hex::b_to(&tmp[0], i.len) + " " + i.disasm;
(*strm) << l << std::endl;
}
if(file != "")

View file

@ -5,6 +5,7 @@
#include "core/rom.hpp"
#include "core/rrdata.hpp"
#include "interface/romtype.hpp"
#include "library/hex.hpp"
#include "library/string.hpp"
#include "library/int24.hpp"
#include "library/minmax.hpp"
@ -162,7 +163,7 @@ namespace
std::string _command;
};
template<typename ret, ret (memory_space::*_rfn)(uint64_t addr)>
template<typename ret, ret (memory_space::*_rfn)(uint64_t addr), bool hexd>
class read_command : public memorymanip_command
{
public:
@ -177,11 +178,14 @@ namespace
throw std::runtime_error("Syntax: " + _command + " <address>");
{
std::ostringstream x;
if(sizeof(ret) > 1)
x << "0x" << std::hex << address << " -> " << std::dec
if(hexd)
x << hex::to(address, true) << " -> "
<< hex::to((lsnes_memory.*_rfn)(address), true);
else if(sizeof(ret) > 1)
x << hex::to(address, true) << " -> " << std::dec
<< (lsnes_memory.*_rfn)(address);
else
x << "0x" << std::hex << address << " -> " << std::dec
x << hex::to(address, true) << " -> " << std::dec
<< (int)(lsnes_memory.*_rfn)(address);
messages << x.str() << std::endl;
}
@ -245,17 +249,22 @@ namespace
}
};
read_command<uint8_t, &memory_space::read<uint8_t>> ru1("read-byte");
read_command<uint16_t, &memory_space::read<uint16_t>> ru2("read-word");
read_command<ss_uint24_t, &memory_space::read<ss_uint24_t>> ru3("read-hword");
read_command<uint32_t, &memory_space::read<uint32_t>> ru4("read-dword");
read_command<uint64_t, &memory_space::read<uint64_t>> ru8("read-qword");
read_command<int8_t, &memory_space::read<int8_t>> rs1("read-sbyte");
read_command<int16_t, &memory_space::read<int16_t>> rs2("read-sword");
read_command<ss_int24_t, &memory_space::read<ss_int24_t>> rs3("read-shword");
read_command<int32_t, &memory_space::read<int32_t>> rs4("read-sdword");
read_command<float, &memory_space::read<float>> rf4("read-float");
read_command<double, &memory_space::read<double>> rf8("read-double");
read_command<uint8_t, &memory_space::read<uint8_t>, false> ru1("read-byte");
read_command<uint16_t, &memory_space::read<uint16_t>, false> ru2("read-word");
read_command<ss_uint24_t, &memory_space::read<ss_uint24_t>, false> ru3("read-hword");
read_command<uint32_t, &memory_space::read<uint32_t>, false> ru4("read-dword");
read_command<uint64_t, &memory_space::read<uint64_t>, false> ru8("read-qword");
read_command<uint8_t, &memory_space::read<uint8_t>, true> rh1("read-byte-hex");
read_command<uint16_t, &memory_space::read<uint16_t>, true> rh2("read-word-hex");
read_command<ss_uint24_t, &memory_space::read<ss_uint24_t>, true> rh3("read-hword-hex");
read_command<uint32_t, &memory_space::read<uint32_t>, true> rh4("read-dword-hex");
read_command<uint64_t, &memory_space::read<uint64_t>, true> rh8("read-qword-hex");
read_command<int8_t, &memory_space::read<int8_t>, false> rs1("read-sbyte");
read_command<int16_t, &memory_space::read<int16_t>, false> rs2("read-sword");
read_command<ss_int24_t, &memory_space::read<ss_int24_t>, false> rs3("read-shword");
read_command<int32_t, &memory_space::read<int32_t>, false> rs4("read-sdword");
read_command<float, &memory_space::read<float>, false> rf4("read-float");
read_command<double, &memory_space::read<double>, false> rf8("read-double");
write_command<uint8_t, -128, 0xFF, &memory_space::write<uint8_t>> w1("write-byte");
write_command<uint16_t, -32768, 0xFFFF, &memory_space::write<uint16_t>> w2("write-word");
write_command<ss_uint24_t, -8388608, 0xFFFFFF, &memory_space::write<ss_uint24_t>> w3("write-hword");

View file

@ -10,6 +10,7 @@
#include "core/settings.hpp"
#include "core/window.hpp"
#include "library/directory.hpp"
#include "library/hex.hpp"
#include "library/loadlib.hpp"
#include "library/sha256.hpp"
#include "library/string.hpp"
@ -177,7 +178,6 @@ namespace
//% is intentionally missing.
const char* allowed_filename_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
"^&'@{}[],$?!-#().+~_";
const char* hexes = "0123456789ABCDEF";
}
std::string safe_filename(const std::string& str)
@ -188,7 +188,7 @@ std::string safe_filename(const std::string& str)
if(strchr(allowed_filename_chars, ch))
o << str[i];
else
o << "%" << hexes[ch / 16] << hexes[ch % 16];
o << "%" << hex::to8(ch);
}
return o.str();
}
@ -282,9 +282,7 @@ void dump_region_map() throw(std::bad_alloc)
std::list<struct memory_region*> regions = lsnes_memory.get_regions();
for(auto i : regions) {
std::ostringstream x;
x << std::setfill('0') << std::setw(16) << std::hex << i->base << "-";
x << std::setfill('0') << std::setw(16) << std::hex << i->last_address() << " ";
x << std::setfill('0') << std::setw(16) << std::hex << i->size << " ";
x << hex::to(i->base) << "-" << hex::to(i->last_address()) << " " << hex::to(i->size) << " ";
messages << x.str() << (i->readonly ? "R-" : "RW") << endian_char(i->endian)
<< (i->special ? 'I' : 'M') << " " << i->name << std::endl;
}
@ -353,10 +351,7 @@ uint32_t gcd(uint32_t a, uint32_t b) throw()
std::string format_address(void* addr)
{
unsigned long x = (unsigned long)addr;
std::ostringstream y;
y << "0x" << std::hex << std::setfill('0') << std::setw(2 * sizeof(unsigned long)) << x;
return y.str();
return hex::to((uint64_t)addr);
}
bool in_global_ctors()
@ -434,7 +429,7 @@ void random_mix_timing_entropy()
void highrandom_256(uint8_t* buf)
{
uint8_t tmp[104];
std::string s = get_random_hexstring(0);
std::string s = get_random_hexstring(64);
std::copy(s.begin(), s.end(), reinterpret_cast<char*>(tmp));
arch_random_256(tmp + 64);
serialization::u64b(tmp + 96, arch_get_tsc());

View file

@ -35,6 +35,7 @@
#include "interface/setting.hpp"
#include "interface/callbacks.hpp"
#include "library/framebuffer-pixfmt-lrgb.hpp"
#include "library/hex.hpp"
#include "library/string.hpp"
#include "library/controller-data.hpp"
#include "library/framebuffer.hpp"
@ -632,8 +633,6 @@ namespace
ecore_callbacks->memory_write(0x1000000 + _addr, val);
}
const char* hexes = "0123456789ABCDEF";
void redraw_cover_fbinfo();
struct _bsnes_core : public core_core
@ -1203,7 +1202,7 @@ again2:
unsigned char ch = SNES::bus.read(busaddr);
#endif
if(ch < 32 || ch > 126)
name << "<" << hexes[ch / 16] << hexes[ch % 16] << ">";
name << "<" << hex::to8(ch) << ">";
else
name << ch;
}

View file

@ -1,20 +1,11 @@
#include "interface/disassembler.hpp"
#include "library/hex.hpp"
#include "library/string.hpp"
#include <sstream>
#include <iomanip>
namespace
{
std::string tohex(uint8_t x)
{
return (stringfmt() << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(x)).str();
}
std::string tohex(uint16_t x)
{
return (stringfmt() << std::setw(4) << std::setfill('0') << std::hex << x).str();
}
template<bool k> struct varsize {};
template<> struct varsize<false> { typedef uint8_t type_t; };
template<> struct varsize<true> { typedef uint16_t type_t; };
@ -202,30 +193,30 @@ namespace
else {
switch(ins[i + 1]) {
case '0':
o << "$" << tohex(x);
o << "$" << hex::to(x);
break;
case 'a':
o << "$" << tohex(fetch_le<typename varsize<lacc>::type_t>(fetchpc));
o << "$" << hex::to(fetch_le<typename varsize<lacc>::type_t>(fetchpc));
break;
case 'b':
o << "$" << tohex(fetch_le<uint8_t>(fetchpc));
o << "$" << hex::to(fetch_le<uint8_t>(fetchpc));
break;
case 'l':
o << "$" << tohex(fetch_le<uint8_t>(fetchpc));
o << tohex(fetch_le<uint16_t>(fetchpc));
o << "$" << hex::to(fetch_le<uint8_t>(fetchpc));
o << hex::to(fetch_le<uint16_t>(fetchpc));
break;
case 'r':
o << "$" << tohex(static_cast<uint16_t>(base + 2 +
o << "$" << hex::to(static_cast<uint16_t>(base + 2 +
fetch_le<int8_t>(fetchpc)));
break;
case 'R':
o << "$" << tohex(static_cast<uint16_t>(base + 3 +
o << "$" << hex::to(static_cast<uint16_t>(base + 3 +
fetch_le<int16_t>(fetchpc)));
case 'w':
o << "$" << tohex(fetch_le<uint16_t>(fetchpc));
o << "$" << hex::to(fetch_le<uint16_t>(fetchpc));
break;
case 'x':
o << "$" << tohex(fetch_le<typename varsize<laddr>::type_t>(fetchpc));
o << "$" << hex::to(fetch_le<typename varsize<laddr>::type_t>(fetchpc));
break;
}
i++;
@ -251,24 +242,25 @@ namespace
else {
switch(ins[i + 1]) {
case '0':
o << "$" << tohex(x);
o << "$" << hex::to(x);
break;
case 'b':
o << "$" << tohex(fetch_le<uint8_t>(fetchpc));
o << "$" << hex::to(fetch_le<uint8_t>(fetchpc));
break;
case 'c':
tmp = fetch_le<uint16_t>(fetchpc);
o << "$" << tohex(static_cast<uint16_t>(tmp & 0x1FFF)) << ":" << (tmp >> 13);
o << "$" << hex::to(static_cast<uint16_t>(tmp & 0x1FFF)) << ":"
<< (tmp >> 13);
break;
case 'r':
o << "$" << tohex(static_cast<uint16_t>(base + 2 +
o << "$" << hex::to(static_cast<uint16_t>(base + 2 +
fetch_le<int8_t>(fetchpc)));
break;
case 'R':
o << "$" << tohex(static_cast<uint16_t>(base + 3 +
o << "$" << hex::to(static_cast<uint16_t>(base + 3 +
fetch_le<int8_t>(fetchpc)));
case 'w':
o << "$" << tohex(fetch_le<uint16_t>(fetchpc));
o << "$" << hex::to(fetch_le<uint16_t>(fetchpc));
break;
}
i++;

View file

@ -34,6 +34,7 @@
#include "interface/cover.hpp"
#include "interface/romtype.hpp"
#include "library/framebuffer-pixfmt-rgb32.hpp"
#include "library/hex.hpp"
#include "library/string.hpp"
#include "library/controller-data.hpp"
#include "library/serialization.hpp"
@ -194,16 +195,6 @@ namespace
instance->get_cpureg(gambatte::GB::REG_E);
}
std::string hex4(uint16_t v)
{
return (stringfmt() << std::setw(4) << std::setfill('0') << std::hex << v).str();
}
std::string hex2(uint8_t v)
{
return (stringfmt() << std::setw(2) << std::setfill('0') << std::hex << (int)v).str();
}
//0 => None or already done.
//1 => BC
//2 => DE
@ -245,10 +236,10 @@ namespace
v = instance->bus_read(addr);
disable_breakpoints = true;
#endif
s << hex2(v);
s << hex::to8(v);
return v;
};
s << hex4(pc) << " ";
s << hex::to16(pc) << " ";
auto d = disassemble_gb_opcode(pc, fetch, addr, opcode);
while(s.str().length() < 12) s << " ";
s << d;
@ -265,15 +256,15 @@ namespace
} else
s << " ";
s << "A:" << hex2(instance->get_cpureg(gambatte::GB::REG_A));
s << " B:" << hex2(instance->get_cpureg(gambatte::GB::REG_B));
s << " C:" << hex2(instance->get_cpureg(gambatte::GB::REG_C));
s << " D:" << hex2(instance->get_cpureg(gambatte::GB::REG_D));
s << " E:" << hex2(instance->get_cpureg(gambatte::GB::REG_E));
s << " H:" << hex2(instance->get_cpureg(gambatte::GB::REG_H));
s << " L:" << hex2(instance->get_cpureg(gambatte::GB::REG_L));
s << " PC:" << hex4(instance->get_cpureg(gambatte::GB::REG_PC));
s << " SP:" << hex4(instance->get_cpureg(gambatte::GB::REG_SP));
s << "A:" << hex::to8(instance->get_cpureg(gambatte::GB::REG_A));
s << " B:" << hex::to8(instance->get_cpureg(gambatte::GB::REG_B));
s << " C:" << hex::to8(instance->get_cpureg(gambatte::GB::REG_C));
s << " D:" << hex::to8(instance->get_cpureg(gambatte::GB::REG_D));
s << " E:" << hex::to8(instance->get_cpureg(gambatte::GB::REG_E));
s << " H:" << hex::to8(instance->get_cpureg(gambatte::GB::REG_H));
s << " L:" << hex::to8(instance->get_cpureg(gambatte::GB::REG_L));
s << " PC:" << hex::to16(instance->get_cpureg(gambatte::GB::REG_PC));
s << " SP:" << hex::to16(instance->get_cpureg(gambatte::GB::REG_SP));
s << " F:"
<< (instance->get_cpureg(gambatte::GB::REG_CF) ? "C" : "-")
<< (instance->get_cpureg(gambatte::GB::REG_ZF) ? "-" : "Z")

View file

@ -1,22 +1,13 @@
#include "disassemble-gb.hpp"
#include "interface/disassembler.hpp"
#include "library/string.hpp"
#include "library/hex.hpp"
#include <functional>
#include <sstream>
#include <iomanip>
namespace
{
std::string hex2(uint8_t v)
{
return (stringfmt() << std::setw(2) << std::setfill('0') << std::hex << (int)v).str();
}
std::string hex4(uint16_t v)
{
return (stringfmt() << std::setw(4) << std::setfill('0') << std::hex << v).str();
}
std::string signdec(uint8_t v, bool psign = false);
std::string signdec(uint8_t v, bool psign)
{
@ -112,17 +103,17 @@ std::string disassemble_gb_opcode(uint16_t pc, std::function<uint8_t()> fetch, i
else {
switch(ins[i + 1]) {
case 'b':
o << "0x" << hex2(fetch());
o << "0x" << hex::to8(fetch());
break;
case 'B':
o << "0x" << hex2(tmp = fetch());
o << "0x" << hex::to8(tmp = fetch());
addr = 0xFF00 + tmp;
break;
case 'w':
o << "0x" << hex4(fetch2());
o << "0x" << hex::to16(fetch2());
break;
case 'W':
o << "0x" << hex4(tmp = fetch2());
o << "0x" << hex::to16(tmp = fetch2());
addr = tmp;
break;
case 'R':

View file

@ -4,7 +4,6 @@
#include "physics.hpp"
#include "library/string.hpp"
#include "library/sha256.hpp"
#include "library/bintohex.hpp"
#include "library/zip.hpp"
namespace sky

View file

@ -2,6 +2,7 @@
#include <cstring>
#include <iomanip>
#include <cmath>
#include "library/hex.hpp"
#include "library/minmax.hpp"
#include "library/ogg.hpp"
#include "library/opus-ogg.hpp"
@ -278,8 +279,8 @@ namespace sky
i.second.eos_seen = true;
for(auto& i : psids)
if(!i.second.eos_seen)
messages << "Warning: No EOS on stream " << (stringfmt() << std::hex << std::setw(8)
<< std::setfill('0') << i.second.oggid).str() << std::endl;
messages << "Warning: No EOS on stream " << hex::to(i.second.oggid, true)
<< std::endl;
delete_undefined_substreams();
if(ssid_to_lsid.empty())
throw std::runtime_error("No valid Oggopus streams found");
@ -328,8 +329,7 @@ namespace sky
}
uint32_t psid = lsid_to_psid[i.second];
std::cerr << std::endl << "\tPhysical stream (PSID#" << psid << ", Ogg stream " <<
(stringfmt() << std::hex << std::setw(8) << std::setfill('0')
<< psids[psid].oggid).str() << "):" << std::endl;
hex::to(psids[psid].oggid) << "):" << std::endl;
auto c = mscharacteristics[psid];
std::cerr << "\t\t" << (int)c.channels << " channels (" << (int)c.streams << " streams, "
<< (int)c.coupled << " coupled) @" << (c.gain / 256.0) << "dB" << std::endl;

View file

@ -3,7 +3,7 @@
#include "tasdemos.hpp"
#include "physics.hpp"
#include "instance.hpp"
#include "library/bintohex.hpp"
#include "library/hex.hpp"
#include "library/string.hpp"
#include "library/zip.hpp"
#include <sys/time.h>
@ -176,7 +176,7 @@ int main(int argc, char** argv)
sky::level& l = sky::levels[lvl];
uint8_t hash[32];
l.sha256_hash(hash);
std::cerr << "Level hash is " << binary_to_hex(hash, 32) << std::endl;
std::cerr << "Level hash is " << hex::b_to(hash, 32) << std::endl;
sky::demo d;
try {
if(lvl)

View file

@ -37,6 +37,7 @@
#include "library/serialization.hpp"
#include "library/minmax.hpp"
#include "library/framebuffer.hpp"
#include "library/hex.hpp"
namespace
{
@ -88,7 +89,7 @@ namespace
str << ecore_callbacks->get_input(1, 0, i) << " ";
for(unsigned i = 0; i < 15; i++)
if(ecore_callbacks->get_input(1, 0, i + 6)) k |= (1 << i);
str << std::hex << std::setw(4) << std::setfill('0') << k;
str << hex::to16(k);
cover_render_string(cover_fbmem, 0, 0, str.str(), 0xFFFFFF, 0x00000, 480, 432, 1920, 4);
}
{
@ -98,7 +99,7 @@ namespace
str << ecore_callbacks->get_input(2, 0, i) << " ";
for(unsigned i = 0; i < 15; i++)
if(ecore_callbacks->get_input(2, 0, i + 6)) k |= (1 << i);
str << std::hex << std::setw(4) << std::setfill('0') << k;
str << hex::to16(k);
cover_render_string(cover_fbmem, 0, 16, str.str(), 0xFFFFFF, 0x00000, 480, 432, 1920, 4);
}
}

View file

@ -1,11 +0,0 @@
#include "bintohex.hpp"
#include <sstream>
#include <iomanip>
std::string binary_to_hex(const uint8_t* data, size_t datalen) throw(std::bad_alloc)
{
std::ostringstream y;
for(size_t i = 0; i < datalen; i++)
y << std::setw(2) << std::setfill('0') << std::hex << static_cast<unsigned>(data[i]);
return y.str();
}

111
src/library/hex.cpp Normal file
View file

@ -0,0 +1,111 @@
#include "hex.hpp"
#include "int24.hpp"
#include "string.hpp"
#include "eatarg.hpp"
#include <sstream>
#include <iomanip>
namespace hex
{
const char* chars = "0123456789abcdef";
const char* charsu = "0123456789abcdef";
std::string to24(uint32_t data, bool prefix) throw(std::bad_alloc)
{
return to<ss_uint24_t>(data, prefix);
}
std::string b_to(const uint8_t* data, size_t datalen, bool uppercase) throw(std::bad_alloc)
{
const char* cset = uppercase ? charsu : chars;
std::string s;
s.resize(2 * datalen);
for(size_t i = 0; i < datalen; i++) {
s[2 * i + 0] = cset[data[i] >> 4];
s[2 * i + 1] = cset[data[i] & 15];
}
return s;
}
template<typename T> std::string to(T data, bool prefix) throw(std::bad_alloc)
{
return (stringfmt() << (prefix ? "0x" : "") << std::hex << std::setfill('0') << std::setw(2 * sizeof(T))
<< (uint64_t)data).str();
}
void b_from(uint8_t* buf, const std::string& hex) throw(std::runtime_error)
{
if(hex.length() & 1)
throw std::runtime_error("hex::frombinary: Length of string must be even");
size_t len = hex.length();
bool parity = false;
unsigned char tmp = 0;
for(size_t i = 0; i < len; i++) {
tmp <<= 4;
char ch = hex[i];
if(ch >= '0' && ch <= '9')
tmp += (ch - '0');
else if(ch >= 'A' && ch <= 'F')
tmp += (ch - 'A' + 10);
else if(ch >= 'a' && ch <= 'f')
tmp += (ch - 'a' + 10);
else
throw std::runtime_error("hex::frombinary: Bad hex character");
parity = !parity;
if(!parity)
buf[i >> 1] = tmp;
}
}
template<typename T> T from(const std::string& hex) throw(std::runtime_error)
{
if(hex.length() > 2 * sizeof(T))
throw std::runtime_error("hex::from: Hexadecimal value too long");
uint64_t tmp = 0;
size_t len = hex.length();
for(size_t i = 0; i < len; i++) {
tmp <<= 4;
char ch = hex[i];
if(ch >= '0' && ch <= '9')
tmp += (ch - '0');
else if(ch >= 'A' && ch <= 'F')
tmp += (ch - 'A' + 10);
else if(ch >= 'a' && ch <= 'f')
tmp += (ch - 'a' + 10);
else
throw std::runtime_error("hex::from<T>: Bad hex character");
}
return tmp;
}
template<typename T> void _ref2()
{
eat_argument(from<T>);
eat_argument(to<T>);
}
void _ref()
{
_ref2<uint8_t>();
_ref2<uint16_t>();
_ref2<ss_uint24_t>();
_ref2<uint32_t>();
_ref2<uint64_t>();
}
}
#ifdef TEST_HEX_ROUTINES
#include <cstring>
int main(int argc, char** argv)
{
std::cerr << hex::to64(100) << std::endl;
/*
std::string a1 = argv[1];
typedef ss_uint24_t type_t;
type_t val;
val = hex::from<type_t>(a1);
std::cerr << (uint64_t)val << std::endl;
*/
}
#endif

View file

@ -1,3 +1,4 @@
#include "hex.hpp"
#include "httpauth.hpp"
#include "string.hpp"
#include <cstdint>
@ -10,14 +11,6 @@
namespace
{
std::string encode_hex(const uint8_t* data, size_t datasize)
{
std::ostringstream x;
for(size_t i = 0; i < datasize; i++)
x << std::hex << std::setw(2) << std::setfill('0') << (int)data[i];
return x.str();
}
//Can only handle 9, 32-126, 128-255.
std::string quote_field(const std::string& field)
{
@ -241,26 +234,6 @@ namespace
if(!tmp.empty()) params.push_back(tmp);
return true;
}
//Parse a hex char.
inline uint8_t hparse(char _ch)
{
uint8_t ch = _ch;
uint8_t itbl[] = {9,1,16,2,10,3,11,4,12,5,13,6,14,7,15,8,0};
return itbl[(uint8_t)(2*ch + 22*(ch>>5)) % 17];
}
//Undo hex encoding.
void unhex(uint8_t* buf, const std::string& str)
{
bool polarity = false;
size_t ptr = 0;
uint8_t val = 0;
while(ptr < str.length()) {
val = val * 16 + hparse(str[ptr++]);
if(!(polarity = !polarity)) *(buf++) = val;
}
}
}
dh25519_http_auth::dh25519_http_auth(const uint8_t* _privkey)
@ -273,7 +246,7 @@ dh25519_http_auth::dh25519_http_auth(const uint8_t* _privkey)
std::string dh25519_http_auth::format_get_session_request()
{
return "dh25519 key="+encode_hex(pubkey,32);
return "dh25519 key="+hex::b_to(pubkey,32);
}
dh25519_http_auth::request_hash dh25519_http_auth::start_request(const std::string& url, const std::string& verb)
@ -310,8 +283,8 @@ std::string dh25519_http_auth::request_hash::get_authorization()
uint8_t response[32];
sprintf(buf, "%u", nonce);
h.read(response);
return "dh25519 id="+quote_field(id)+",key="+encode_hex(pubkey,32)+",nonce="+identity(buf)+
",response="+encode_hex(response,32)+",response2="+encode_hex(prereq,8)+",noprotocol=1";
return "dh25519 id="+quote_field(id)+",key="+hex::b_to(pubkey,32)+",nonce="+identity(buf)+
",response="+hex::b_to(response,32)+",response2="+hex::b_to(prereq,8)+",noprotocol=1";
}
void dh25519_http_auth::parse_auth_response(const std::string& response)
@ -336,7 +309,7 @@ void dh25519_http_auth::parse_auth_response(std::map<std::string, std::string> p
std::string challenge = pparse["challenge"];
if(challenge.length() != 64) goto no_reseed;
uint8_t _challenge[32];
unhex(_challenge, challenge);
hex::b_from(_challenge, challenge);
curve25519(ssecret, privkey, _challenge);
nonce = 0;
reseeded = true;

View file

@ -1,4 +1,5 @@
#include "json.hpp"
#include "hex.hpp"
#include "string.hpp"
#include <limits>
#include <climits>
@ -831,9 +832,6 @@ bad:
return json_token(json_token::TINVALID);
};
const char* hexes = "0123456789abcdef";
std::string json_string_escape(const std::u32string& c)
{
std::ostringstream out;
@ -846,7 +844,7 @@ bad:
else if(c[i] == '\t') out << "\\t";
else if(c[i] == '\f') out << "\\f";
else if((c[i] & 0xFFFFFFE0) == 0)
out << "\\u00" << hexes[c[i] >> 4] << hexes[c[i] % 16];
out << "\\u" << hex::to16(c[i]);
else if(c[i] == U'\\')
out << "\\\\";
else if(c[i] == U'\"')

View file

@ -1,6 +1,7 @@
#include "ogg.hpp"
#include "serialization.hpp"
#include "minmax.hpp"
#include "hex.hpp"
#include <cstring>
#include <zlib.h>
#include <algorithm>
@ -139,12 +140,10 @@ bool demuxer::complain_lost_page(uint32_t new_seq, uint32_t stream)
uint64_t last_missing = page_fullseq(new_seq) - 1;
if(first_missing == last_missing)
errors_to << "Warning: Ogg demux: Page " << first_missing << " missing on stream "
<< (stringfmt() << std::hex << std::setw(8) << std::setfill('0') << stream).str()
<< std::endl;
<< hex::to(stream) << std::endl;
else
errors_to << "Warning: Ogg demux: Pages " << first_missing << "-" << last_missing
<< " missing on stream " << (stringfmt() << std::hex << std::setw(8)
<< std::setfill('0') << stream).str() << std::endl;
<< " missing on stream " << hex::to(stream) << std::endl;
return true;
}
return false;
@ -554,7 +553,7 @@ bool page::scan(const char* buffer, size_t bufferlen, bool eof, size_t& advance)
std::string page::stream_debug_id() const throw(std::bad_alloc)
{
return (stringfmt() << "Stream " << std::hex << std::setfill('0') << std::setw(8) << stream).str();
return (stringfmt() << "Stream " << hex::to(stream)).str();
}
std::string page::page_debug_id() const throw(std::bad_alloc)

View file

@ -1,6 +1,7 @@
#include "png.hpp"
#include "serialization.hpp"
#include "minmax.hpp"
#include "hex.hpp"
#include "zip.hpp"
#include <iostream>
#include <fstream>
@ -292,7 +293,7 @@ namespace
goto badtype;
return (stringfmt() << t1 << t2 << t3 << t4).str();
badtype:
return (stringfmt() << "0x" << std::hex << std::setw(8) << std::setfill('0') << type).str();
return hex::to(type, true);
}
//=========================================================
@ -1064,14 +1065,14 @@ int main(int argc, char** argv)
for(size_t i = 0; i < img.data.size(); i++) {
if(i > 0 && i % img.width == 0)
std::cout << std::endl;
std::cout << std::hex << std::setw(8) << std::setfill('0') << img.palette[img.data[i]]
<< "<" << std::hex << std::setw(4) << std::setfill('0') << img.data[i] << "> ";
std::cout << hex::to(img.palette[img.data[i]])
<< "<" << hex::to(img.data[i]) << "> ";
}
} else {
for(size_t i = 0; i < img.data.size(); i++) {
if(i > 0 && i % img.width == 0)
std::cout << std::endl;
std::cout << std::hex << std::setw(8) << std::setfill('0') << img.data[i] << " ";
std::cout << hex::to(img.data[i]) << " ";
}
}
std::cout << std::endl;

View file

@ -1,15 +1,11 @@
#include "rrdata.hpp"
#include "hex.hpp"
#include <cstring>
#include <limits>
#include <cassert>
#define MAXRUN 16843009
namespace
{
const char* hexes = "0123456789ABCDEF";
}
rrdata_set::instance::instance() throw()
{
memset(bytes, 0, RRDATA_BYTES);
@ -360,10 +356,7 @@ void rrdata_set::set_internal(const instance& b) throw()
std::ostream& operator<<(std::ostream& os, const struct rrdata_set::instance& j)
{
for(unsigned i = 0; i < 32; i++) {
os << hexes[j.bytes[i] / 16] << hexes[j.bytes[i] % 16];
}
return os;
os << hex::b_to(j.bytes, 32, true);
}
bool rrdata_set::_add(const instance& b)

View file

@ -1,4 +1,5 @@
#include "sha256.hpp"
#include "hex.hpp"
#include <cstdint>
#include <sstream>
#include <iostream>
@ -63,14 +64,9 @@ namespace
return (k & a) | ((~k) & b);
}
std::string format32(uint32_t num)
{
std::ostringstream y;
y << std::hex << std::setw(8) << std::setfill('0') << num;
return y.str();
}
#define SHOW(a,b,c,d,e,f,g,h) "\t" << format32(a) << "\t" << format32(b) << "\t" << format32(c) << "\t" << format32(d) << "\t" << format32(e) << "\t" << format32(f) << "\t" << format32(g) << "\t" << format32(h)
#define SHOW(a,b,c,d,e,f,g,h) "\t" << hex::to32(a) << "\t" << hex::to32(b) << "\t" << hex::to32(c) << "\t" \
<< hex::to32(d) << "\t" << hex::to32(e) << "\t" << hex::to32(f) << "\t" << hex::to32(g) << "\t" \
<< hex::to32(h)
#define WROUND(i, shift) \
Xsigma0 = esigma0(datablock[(i + shift + 1) & 15]); \

View file

@ -6,6 +6,7 @@
#include <iomanip>
#include "skein512c.inc"
#include "arch-detect.hpp"
#include "hex.hpp"
//Jerry Solinas was not here.
@ -15,7 +16,7 @@ static void show_array(const char* prefix, const uint64_t* a, size_t e)
{
std::cerr << prefix;
for(size_t i = 0; i < e; i++) {
std::cerr << std::hex << std::setw(16) << std::setfill('0') << a[i];
std::cerr << hex::to(a[i]);
if(i < e - 1)
std::cerr << ", ";
}
@ -26,7 +27,7 @@ static void show_array(const char* prefix, const uint8_t* a, size_t e)
{
std::cerr << prefix;
for(size_t i = 0; i < e; i++) {
std::cerr << std::hex << std::setw(2) << std::setfill('0') << (int)a[i];
std::cerr << hex::to(a[i]);
}
std::cerr << std::endl;
}

View file

@ -1,7 +1,7 @@
#include "lua/internal.hpp"
#include "interface/disassembler.hpp"
#include "interface/romtype.hpp"
#include "library/bintohex.hpp"
#include "library/hex.hpp"
#include "core/memorymanip.hpp"
#include "core/moviedata.hpp"
@ -35,7 +35,7 @@ namespace
tmp.resize(bytes);
lsnes_memory.read_range(laddr, &tmp[0], bytes);
L.pushstring("bytes");
L.pushlstring(binary_to_hex(&tmp[0], bytes));
L.pushlstring(hex::b_to(&tmp[0], bytes));
L.settable(-3);
L.settable(-3);
laddr += bytes;

View file

@ -9,6 +9,7 @@
#include "library/sha256.hpp"
#include "library/string.hpp"
#include "library/minmax.hpp"
#include "library/hex.hpp"
#include "library/int24.hpp"
namespace
@ -466,11 +467,7 @@ namespace
char hash[64];
auto x = our_rom.save_core_state();
size_t offset = x.size() - 32;
for(unsigned i = 0; i < 32; i++) {
hash[2 * i + 0] = hexes[static_cast<unsigned char>(x[offset + i]) >> 4];
hash[2 * i + 1] = hexes[static_cast<unsigned char>(x[offset + i]) & 0xF];
}
L.pushlstring(hash, 64);
L.pushlstring(hex::b_to((uint8_t*)&x[offset], 32));
return 1;
});

View file

@ -2,13 +2,13 @@
#include "lua/unsaferewind.hpp"
#include "core/misc.hpp"
#include "library/string.hpp"
#include "library/hex.hpp"
namespace
{
uint64_t randnum()
{
std::string x = "0x" + get_random_hexstring(16);
return parse_value<uint64_t>(x);
return hex::from<uint64_t>(get_random_hexstring(16));
}
uint64_t randnum_mod(uint64_t y)

View file

@ -4,6 +4,7 @@
#include "core/project.hpp"
#include "core/memorymanip.hpp"
#include "library/memorysearch.hpp"
#include "library/hex.hpp"
#include "platform/wxwidgets/platform.hpp"
#include "platform/wxwidgets/textrender.hpp"
@ -47,7 +48,7 @@ namespace
return (stringfmt() << (int)x[0]).str();
}},
{"1 byte (hex)", 1, false, "BH2", [](const uint8_t* x) -> std::string {
return (stringfmt() << std::hex << std::setw(2) << std::setfill('0') << (int)x[0]).str();
return hex::to(x[0]);
}},
{"2 bytes (signed)", 2, false, "w", [](const uint8_t* x) -> std::string {
return (stringfmt() << *(int16_t*)x).str();
@ -56,7 +57,7 @@ namespace
return (stringfmt() << *(uint16_t*)x).str();
}},
{"2 bytes (hex)", 2, false, "WH4", [](const uint8_t* x) -> std::string {
return (stringfmt() << std::hex << std::setw(4) << std::setfill('0') << *(uint16_t*)x).str();
return hex::to(*(uint16_t*)x);
}},
{"3 bytes (signed)", 3, true, "o", [](const uint8_t* x) -> std::string {
int32_t a = 0;
@ -79,7 +80,7 @@ namespace
a |= (uint32_t)x[0] << 16;
a |= (uint32_t)x[1] << 8;
a |= (uint32_t)x[2];
return (stringfmt() << std::hex << std::setw(6) << std::setfill('0') << a).str();
return hex::to24(a);
}},
{"4 bytes (signed)", 4, false, "d", [](const uint8_t* x) -> std::string {
return (stringfmt() << *(int32_t*)x).str();
@ -88,7 +89,7 @@ namespace
return (stringfmt() << *(uint32_t*)x).str();
}},
{"4 bytes (hex)", 4, false, "DH8", [](const uint8_t* x) -> std::string {
return (stringfmt() << std::hex << std::setw(8) << std::setfill('0') << *(uint32_t*)x).str();
return hex::to(*(uint32_t*)x);
}},
{"4 bytes (float)", 4, false, "f", [](const uint8_t* x) -> std::string {
return (stringfmt() << *(float*)x).str();
@ -100,7 +101,7 @@ namespace
return (stringfmt() << *(uint64_t*)x).str();
}},
{"8 bytes (hex)", 8, false, "QHG", [](const uint8_t* x) -> std::string {
return (stringfmt() << std::hex << std::setw(16) << std::setfill('0') << *(uint64_t*)x).str();
return hex::to(*(uint64_t*)x);
}},
{"8 bytes (float)", 8, false, "F", [](const uint8_t* x) -> std::string {
return (stringfmt() << *(double*)x).str();

View file

@ -10,6 +10,7 @@
#include <wx/radiobut.h>
#include "library/string.hpp"
#include "library/hex.hpp"
#include "interface/romtype.hpp"
class wxeditor_watchexpr : public wxDialog

View file

@ -2,6 +2,7 @@
#include "core/memorymanip.hpp"
#include "core/memorywatch.hpp"
#include "core/project.hpp"
#include "library/hex.hpp"
#include "library/string.hpp"
#include "library/memorysearch.hpp"
#include "library/int24.hpp"
@ -76,13 +77,6 @@ namespace
search_fn_t searches[DATATYPES];
};
std::string hexformat_address(uint64_t addr)
{
std::ostringstream x;
x << std::setfill('0') << std::setw(16) << std::hex << addr;
return x.str();
}
template<typename T> std::string format_number_signed(T val, bool hex);
template<typename T> std::string format_number_unsigned(T val, bool hex);
@ -723,7 +717,7 @@ void wxwindow_memorysearch::panel::prepare_paint()
std::list<uint64_t> addrs2 = ms->get_candidates();
long j = 0;
for(auto i : addrs2) {
std::string row = hexformat_address(i) + " ";
std::string row = hex::to(i) + " ";
row += (_parent->*displays[_parent->typecode])(i, _parent->hexmode, false);
row += " (Was: ";
row += (_parent->*displays[_parent->typecode])(i, _parent->hexmode, true);
@ -793,7 +787,7 @@ void wxwindow_memorysearch::dump_candidates_text()
std::list<uint64_t> addrs2 = ms->get_candidates();
long j = 0;
for(auto i : addrs2) {
std::string row = hexformat_address(i) + " ";
std::string row = hex::to(i) + " ";
row += (this->*displays[this->typecode])(i, this->hexmode, false);
row += " (Was: ";
row += (this->*displays[this->typecode])(i, this->hexmode, true);

View file

@ -4,6 +4,7 @@
#include "video/sox.hpp"
#include "library/serialization.hpp"
#include "library/string.hpp"
#include "library/hex.hpp"
#include <fcntl.h>
#include <iomanip>
@ -76,29 +77,6 @@ namespace
return out;
}
unsigned char hex(char ch)
{
switch(ch) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'a': case 'A': return 10;
case 'b': case 'B': return 11;
case 'c': case 'C': return 12;
case 'd': case 'D': return 13;
case 'e': case 'E': return 14;
case 'f': case 'F': return 15;
};
return 16;
}
class pipedec_avsnoop : public information_dispatch
{
public:
@ -122,10 +100,7 @@ namespace
last_height = 0;
last_fps_n = 0;
last_fps_d = 0;
std::string tmp2 = get_random_hexstring(8);
segid = 0;
for(unsigned i = 0; i < tmp2.length(); i++)
segid = 16 * segid + hex(tmp2[i]);
segid = hex::from<uint32_t>(get_random_hexstring(8));
}
~pipedec_avsnoop() throw()