From 2ff4ea80379de0a583b63490b7955f39db77ea9d Mon Sep 17 00:00:00 2001 From: jakcron Date: Sat, 7 Apr 2018 15:57:33 +0800 Subject: [PATCH] [fnd] Add SimpleTextOutput utility class. --- lib/libfnd/include/fnd/SimpleTextOutput.h | 18 +++++++ lib/libfnd/source/SimpleTextOutput.cpp | 61 +++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 lib/libfnd/include/fnd/SimpleTextOutput.h create mode 100644 lib/libfnd/source/SimpleTextOutput.cpp diff --git a/lib/libfnd/include/fnd/SimpleTextOutput.h b/lib/libfnd/include/fnd/SimpleTextOutput.h new file mode 100644 index 0000000..bbf8e73 --- /dev/null +++ b/lib/libfnd/include/fnd/SimpleTextOutput.h @@ -0,0 +1,18 @@ +#pragma once +#include + +namespace fnd +{ + class SimpleTextOutput + { + public: + static void hxdStyleDump(const byte_t* data, size_t len, size_t row_len, size_t byte_grouping_size); + static void hxdStyleDump(const byte_t* data, size_t len); + static void hexDump(const byte_t* data, size_t len, size_t row_len, size_t indent_len); + static void hexDump(const byte_t* data, size_t len); + private: + static const size_t kDefaultRowLen = 0x10; + static const size_t kDefaultByteGroupingSize = 1; + }; + +} \ No newline at end of file diff --git a/lib/libfnd/source/SimpleTextOutput.cpp b/lib/libfnd/source/SimpleTextOutput.cpp new file mode 100644 index 0000000..8703386 --- /dev/null +++ b/lib/libfnd/source/SimpleTextOutput.cpp @@ -0,0 +1,61 @@ +#include +#include + +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++) + { + // 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++) + { + printf("%c", isalnum(data[(i * row_len) + j]) ? data[(i * row_len) + j] : '.'); + } + printf("\n"); + } +} + +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) +{ + for (size_t i = 0; i < len; i++) + { + if ((i % row_len) == 0) + { + if (i > 0) + putchar('\n'); + for (size_t j = 0; j < indent_len; j++) + { + putchar(' '); + } + } + printf("%02X", data[i]); + if ((i+1) >= len) + { + putchar('\n'); + } + + } +} + +void fnd::SimpleTextOutput::hexDump(const byte_t* data, size_t len) +{ + for (size_t i = 0; i < len; i++) + { + printf("%02X", data[i]); + } + putchar('\n'); +} \ No newline at end of file