2018-08-14 07:23:02 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
2018-04-07 07:57:33 +00:00
|
|
|
#include <cstdio>
|
2018-08-14 07:23:02 +00:00
|
|
|
#include <fnd/SimpleTextOutput.h>
|
2018-04-07 07:57:33 +00:00
|
|
|
|
|
|
|
void fnd::SimpleTextOutput::hxdStyleDump(const byte_t* data, size_t len, size_t row_len, size_t byte_grouping_size)
|
|
|
|
{
|
|
|
|
// iterate over blocks
|
|
|
|
for (size_t i = 0; i < (len / row_len); i++)
|
|
|
|
{
|
2018-06-29 07:26:11 +00:00
|
|
|
printf("%08" PRIx64 " | ", (uint64_t)(i * row_len));
|
2018-04-07 07:57:33 +00:00
|
|
|
// for block i print each byte
|
|
|
|
for (size_t j = 0; j < row_len; j++)
|
|
|
|
{
|
|
|
|
printf("%02X", data[(i * row_len) + j]);
|
|
|
|
if (((j+1) % byte_grouping_size) == 0)
|
|
|
|
{
|
|
|
|
putchar(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf(" ");
|
|
|
|
for (size_t j = 0; j < row_len; j++)
|
|
|
|
{
|
2018-06-01 12:22:09 +00:00
|
|
|
printf("%c", iscntrl(data[(i * row_len) + j]) ? '.' : data[(i * row_len) + j]);
|
2018-04-07 07:57:33 +00:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
2018-04-25 11:36:21 +00:00
|
|
|
if ((len % row_len) > 0)
|
|
|
|
{
|
|
|
|
size_t i = (len / row_len);
|
2018-06-29 07:26:11 +00:00
|
|
|
printf("%08" PRIx64 " | ", (uint64_t)(i * row_len));
|
2018-04-25 11:36:21 +00:00
|
|
|
// for block i print each byte
|
|
|
|
for (size_t j = 0; j < row_len; j++)
|
|
|
|
{
|
|
|
|
if (j < (len % row_len))
|
|
|
|
printf("%02X", data[(i * row_len) + j]);
|
|
|
|
else
|
|
|
|
printf(" ");
|
|
|
|
if (((j+1) % byte_grouping_size) == 0)
|
|
|
|
{
|
|
|
|
putchar(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf(" ");
|
|
|
|
for (size_t j = 0; j < row_len; j++)
|
|
|
|
{
|
|
|
|
if (j < (len % row_len))
|
2018-06-01 12:22:09 +00:00
|
|
|
printf("%c", iscntrl(data[(i * row_len) + j]) ? '.' : data[(i * row_len) + j]);
|
2018-04-25 11:36:21 +00:00
|
|
|
else
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
2018-04-07 07:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void fnd::SimpleTextOutput::hxdStyleDump(const byte_t* data, size_t len)
|
|
|
|
{
|
|
|
|
hxdStyleDump(data, len, kDefaultRowLen, kDefaultByteGroupingSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fnd::SimpleTextOutput::hexDump(const byte_t* data, size_t len, size_t row_len, size_t indent_len)
|
|
|
|
{
|
2018-08-14 07:23:02 +00:00
|
|
|
for (size_t i = 0; i < len; i += row_len)
|
2018-04-07 07:57:33 +00:00
|
|
|
{
|
2018-08-14 07:23:02 +00:00
|
|
|
for (size_t j = 0; j < indent_len; j++)
|
|
|
|
std::cout << " ";
|
|
|
|
std::cout << arrayToString(data+i, _MIN(len-i, row_len), true, "") << std::endl;
|
2018-04-07 07:57:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void fnd::SimpleTextOutput::hexDump(const byte_t* data, size_t len)
|
|
|
|
{
|
2018-08-14 07:23:02 +00:00
|
|
|
std::cout << arrayToString(data, len, true, "") << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string fnd::SimpleTextOutput::arrayToString(const byte_t* data, size_t len, bool upper_case, const std::string& separator)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
if (upper_case)
|
|
|
|
ss << std::uppercase;
|
2018-04-07 07:57:33 +00:00
|
|
|
for (size_t i = 0; i < len; i++)
|
|
|
|
{
|
2018-08-14 07:23:02 +00:00
|
|
|
ss << std::hex << std::setw(2) << std::setfill('0') << (uint32_t)data[i];
|
|
|
|
if (i+1 < len)
|
|
|
|
ss << separator;
|
2018-04-07 07:57:33 +00:00
|
|
|
}
|
2018-08-14 07:23:02 +00:00
|
|
|
return ss.str();
|
2018-04-07 07:57:33 +00:00
|
|
|
}
|