diff options
| author | Joel Klinghed <the_jk@yahoo.com> | 2017-07-29 01:36:05 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@yahoo.com> | 2017-07-29 01:36:05 +0200 |
| commit | 965e7208ad8a22c2e203e94258ec1dc42ee531ef (patch) | |
| tree | 6787b732ca77975ab0e966d0a18f1f14c104a2c6 | |
| parent | ee34164a6c1c4f905332cfcfef938a0ccb48333b (diff) | |
Reuse HexDump in monitor-cmd
| -rw-r--r-- | src/Makefile.am | 7 | ||||
| -rw-r--r-- | src/gui_hexdump.cc | 34 | ||||
| -rw-r--r-- | src/gui_hexdump.hh | 3 | ||||
| -rw-r--r-- | src/gui_plainattrtext.cc | 45 | ||||
| -rw-r--r-- | src/gui_plainattrtext.hh | 16 | ||||
| -rw-r--r-- | src/monitor-cmd.cc | 46 |
6 files changed, 99 insertions, 52 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 0144d21..07ce35f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -51,14 +51,15 @@ tp_genca_SOURCES = genca.cc logger.cc tp_genca_LDADD = libmitm.a libtp.a @SSL_LIBS@ tp_genca_CXXFLAGS = $(AM_CXXFLAGS) -DVERSION='"@VERSION@"' -libmonitor_a_SOURCES = monitor.cc +libmonitor_a_SOURCES = monitor.cc gui_hexdump.cc libmonitor_a_CXXFLAGS = $(AM_CXXFLAGS) -DVERSION='"@VERSION@"' @THREAD_CFLAGS@ -tp_monitor_SOURCES = monitor-cmd.cc resolver.cc +tp_monitor_SOURCES = monitor-cmd.cc resolver.cc \ + gui_plainattrtext.cc gui_attrtext.cc tp_monitor_LDADD = libmonitor.a libtp.a @THREAD_LIBS@ tp_monitor_CXXFLAGS = $(AM_CXXFLAGS) -DVERSION='"@VERSION@"' @THREAD_CFLAGS@ -libmonitor_gui_a_SOURCES = monitor-gui.cc gui_hexdump.cc gui_config.cc +libmonitor_gui_a_SOURCES = monitor-gui.cc gui_config.cc libmonitor_gui_a_CXXFLAGS = $(AM_CXXFLAGS) -DVERSION='"@VERSION@"' \ @THREAD_CFLAGS@ @PCAP_CFLAGS@ diff --git a/src/gui_hexdump.cc b/src/gui_hexdump.cc index e7da2d4..8f6b172 100644 --- a/src/gui_hexdump.cc +++ b/src/gui_hexdump.cc @@ -19,13 +19,18 @@ inline size_t append(char* out, size_t max, char const* in, size_t len) { return 0; } -inline size_t safe(char* out, size_t max, char const* in, size_t len) { +inline size_t safe(char* out, size_t max, char const* in, size_t len, + bool ascii) { size_t ret = 0; for (; len--; ++in) { if (*in >= ' ' && *in < '\x7f') { ret += append(out + ret, max - ret, in, 1); continue; } + if (ascii) { + ret += append(out + ret, max - ret, ".", 1); + continue; + } switch (*in) { case '\r': ret += append(out + ret, max - ret, "\xe2\x86\xb5", 3); // U+21B5 @@ -51,17 +56,17 @@ inline size_t safe(char* out, size_t max, char const* in, size_t len) { // static -void HexDump::write(AttributedText* text, uint8_t flags, std::string const& str, +void HexDump::write(AttributedText* text, uint8_t flags, char const* data, size_t start, size_t length) { - if (start >= str.size()) return; - length = std::min(length, str.size() - start); - if (length == 0) return; + if (!data || length == 0) return; AttributedText::Attribute box(0x90, 0x90, 0x90); box.set_bold(true); + bool const ascii = flags & ASCII; + size_t i = 0; - auto data = str.data() + start; + data = data + start; char tmp[80]; char tmp2[64]; int len; @@ -77,7 +82,7 @@ void HexDump::write(AttributedText* text, uint8_t flags, std::string const& str, data[i + 3] & 0xff, data[i + 4] & 0xff, data[i + 5] & 0xff, data[i + 6] & 0xff, data[i + 7] & 0xff); if (flags & CHARS) { - x += safe(x, sizeof(tmp2) - (x - tmp2), data + i, 8); + x += safe(x, sizeof(tmp2) - (x - tmp2), data + i, 8, ascii); } i += 8; text->append(tmp, len); @@ -103,7 +108,7 @@ void HexDump::write(AttributedText* text, uint8_t flags, std::string const& str, data[i + 3] & 0xff, data[i + 4] & 0xff, data[i + 5] & 0xff, data[i + 6] & 0xff, data[i + 7] & 0xff); if (flags & CHARS) { - x += safe(x, sizeof(tmp2) - (x - tmp2), data + i, 8); + x += safe(x, sizeof(tmp2) - (x - tmp2), data + i, 8, ascii); } i += 8; text->append(tmp, len); @@ -111,7 +116,8 @@ void HexDump::write(AttributedText* text, uint8_t flags, std::string const& str, } for (; i < length; ++i) { len = snprintf(tmp, sizeof(tmp), "%02x ", data[i] & 0xff); - if (flags & CHARS) x += safe(x, sizeof(tmp2) - (x - tmp2), data + i, 1); + if (flags & CHARS) x += safe(x, sizeof(tmp2) - (x - tmp2), data + i, 1, + ascii); text->append(tmp, len); } if (flags & CHARS) { @@ -134,6 +140,16 @@ void HexDump::write(AttributedText* text, uint8_t flags, std::string const& str, } // static +void HexDump::write(AttributedText* text, uint8_t flags, std::string const& str, + size_t start, size_t length) { + if (start >= str.size()) return; + length = std::min(length, str.size() - start); + write(text, flags, str.data(), start, length); +} + +// static uint8_t const HexDump::ADDRESS = 1 << 0; // static uint8_t const HexDump::CHARS = 1 << 1; +// static +uint8_t const HexDump::ASCII = 1 << 2; diff --git a/src/gui_hexdump.hh b/src/gui_hexdump.hh index d3c41cf..f21479e 100644 --- a/src/gui_hexdump.hh +++ b/src/gui_hexdump.hh @@ -11,7 +11,10 @@ class HexDump { public: static uint8_t const ADDRESS; static uint8_t const CHARS; + static uint8_t const ASCII; + static void write(AttributedText* text, uint8_t flags, char const* data, + size_t start, size_t length); static void write(AttributedText* text, uint8_t flags, std::string const& data, size_t start = 0, size_t length = std::string::npos); diff --git a/src/gui_plainattrtext.cc b/src/gui_plainattrtext.cc new file mode 100644 index 0000000..a588172 --- /dev/null +++ b/src/gui_plainattrtext.cc @@ -0,0 +1,45 @@ +// -*- mode: c++; c-basic-offset: 2; -*- + +#include "common.hh" + +#include "gui_plainattrtext.hh" + +namespace { + +class PlainAttributedTextImpl : public PlainAttributedText { +public: + void append(const char* str, size_t len, Attribute const& UNUSED(attr), + size_t start, size_t length) override { + if (!str || start >= len) return; + length = std::min(len - start, length); + if (length == 0) return; + text_.append(str + start, length); + } + + void add(Attribute const& UNUSED(attr), size_t UNUSED(start), + size_t UNUSED(length)) override { + } + void set(Attribute const& UNUSED(attr), size_t UNUSED(start), + size_t UNUSED(length)) override { + } + void clear(size_t UNUSED(start), size_t UNUSED(length)) override { + } + + std::string text() const override { + return text_; + } + + void reset() override { + text_.clear(); + } + +private: + std::string text_; +}; + +} // namespace + +// static +PlainAttributedText* PlainAttributedText::create() { + return new PlainAttributedTextImpl(); +} diff --git a/src/gui_plainattrtext.hh b/src/gui_plainattrtext.hh new file mode 100644 index 0000000..8d492b5 --- /dev/null +++ b/src/gui_plainattrtext.hh @@ -0,0 +1,16 @@ +// -*- mode: c++; c-basic-offset: 2; -*- + +#ifndef GUI_PLAINATTRTEXT_HH +#define GUI_PLAINATTRTEXT_HH + +#include "gui_attrtext.hh" + +class PlainAttributedText : public AttributedText { +public: + static PlainAttributedText* create(); + +protected: + PlainAttributedText() {} +}; + +#endif // GUI_PLAINATTRTEXT_HH diff --git a/src/monitor-cmd.cc b/src/monitor-cmd.cc index ba02703..8aa8b99 100644 --- a/src/monitor-cmd.cc +++ b/src/monitor-cmd.cc @@ -11,6 +11,8 @@ #include "args.hh" #include "buffer.hh" +#include "gui_hexdump.hh" +#include "gui_plainattrtext.hh" #include "io.hh" #include "ios_save.hh" #include "looper.hh" @@ -120,50 +122,14 @@ private: } out_ << "* Size: " << size << '\n'; if (size > 0) { - ios_save save(out_); - auto d = reinterpret_cast<uint8_t const*>(data); - out_.flags(std::ios::hex); - out_.fill('0'); - for (size_t i = 0; i < size; i += 16) { - out_ << std::setw(8) << i << ' '; - unsigned j = 0; - for (; j < 8; ++j) { - auto k = i + j; - if (k >= size) break; - out_ << ' ' << std::setw(2) << static_cast<int>(d[k]); - } - for (; j < 8; ++j) { - out_ << " "; - } - out_ << ' '; - for (; j < 16; ++j) { - auto k = i + j; - if (k >= size) break; - out_ << ' ' << std::setw(2) << static_cast<int>(d[k]); - } - for (; j < 16; ++j) { - out_ << " "; - } - out_ << " |"; - j = 0; - for (; j < 16; ++j) { - auto k = i + j; - if (k >= size) break; - out_ << printable(data[k]); - } - for (; j < 16; ++j) { - out_ << ' '; - } - out_ << "|\n"; - } + std::unique_ptr<PlainAttributedText> text(PlainAttributedText::create()); + HexDump::write(text.get(), HexDump::ADDRESS | HexDump::CHARS + | HexDump::ASCII, data, 0, size); + out_ << text->text(); } out_ << std::endl; } - static char printable(char c) { - return (c & 0x80 || c < ' ' || c >= 0x7f) ? '.' : c; - } - std::ostream& out_; bool interleave_; Looper* looper_; |
