summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2016-10-28 11:47:06 +0200
committerSzczepan Zalega <szczepan@nitrokey.com>2016-11-26 18:56:25 +0100
commite262c978f4cb8b448087102b68bbdcc731a090a0 (patch)
tree4031c1a0a5eb367eca08d8d0753ea287dea019d0
parent9772055e79da77c6ec9ea6992cc77891efc1265a (diff)
downloadlibnitrokey-e262c978f4cb8b448087102b68bbdcc731a090a0.tar.gz
libnitrokey-e262c978f4cb8b448087102b68bbdcc731a090a0.tar.bz2
Print ascii representation of dump in hexdump
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
-rw-r--r--misc.cc21
1 files changed, 18 insertions, 3 deletions
diff --git a/misc.cc b/misc.cc
index 9b339cd..c9d38cb 100644
--- a/misc.cc
+++ b/misc.cc
@@ -34,6 +34,7 @@ std::vector<uint8_t> hex_string_to_byte(const char* hexString){
return data;
};
+#include <cctype>
std::string hexdump(const char *p, size_t size, bool print_header) {
std::stringstream out;
char formatbuf[128];
@@ -45,9 +46,23 @@ std::string hexdump(const char *p, size_t size, bool print_header) {
out << formatbuf;
}
- for (const char *le = p + 16; p < le && p < pend; p++) {
- snprintf(formatbuf, 128, "%02x ", uint8_t(*p));
- out << formatbuf;
+ const char* pp = p;
+ for (const char *le = p + 16; p < le; p++) {
+ if (p < pend){
+ snprintf(formatbuf, 128, "%02x ", uint8_t(*p));
+ out << formatbuf;
+ } else {
+ out << "-- ";
+ }
+
+ }
+ out << "\t";
+
+ for (const char *le = pp + 16; pp < le && pp < pend; pp++) {
+ if (std::isgraph(*pp))
+ out << uint8_t(*pp);
+ else
+ out << '.';
}
out << std::endl;
}