File: HexDump.cpp

package info (click to toggle)
librepfunc 1.11.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 468 kB
  • sloc: cpp: 1,768; makefile: 270
file content (171 lines) | stat: -rw-r--r-- 5,055 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*******************************************************************************
 * librepfunc - a collection of common functions, classes and tools.
 * See the README file for copyright information and how to reach the author.
 ******************************************************************************/
#include <repfunc.h>
#include <iostream>  // std::cout, std::cerr
#include <sstream>   // std::stringstream
#include <iomanip>   // std::uppercase, std::setfill, std::setw, std::hex
#include <cctype>    // std::isprint


std::string hex(std::intmax_t n, size_t width) {
  std::stringstream ss;
  ss << std::uppercase
     << std::setfill('0')
     << std::setw(width)
     << std::hex
     << n;
  return ss.str();
}

void HexDump(std::string intro, const unsigned char* buf, size_t len, bool to_stderr) {
  constexpr size_t hdr_len = 70;
  size_t addr_len = hex(len,2).size();
  size_t len1 = 1;
  size_t len2 = 1;

  std::stringstream ss;
  std::string s;

  if (buf == nullptr)
     len = 0;

  if (intro.empty())
     intro = "(null)";

  /*****************************************************************************
   * write caption and number of bytes.
   ****************************************************************************/
  if (intro.size() <= (hdr_len - 4)) {
     len1 = (hdr_len - 2)/2 - (size_t) (0.5 + intro.size()/2.0);
     len2 = (hdr_len - 2)   - (len1 + intro.size());
     }

  ss << "   "
     << std::string(len1, '=')
     << ' ' << intro << ' '
     << std::string(len2, '=')
     << std::endl
     << "   len = " << len << std::endl;


  /*****************************************************************************
   * now, print rows of 16 bytes and it's 7bit ASCII equivalent.
   ****************************************************************************/
  for(size_t i=0; i<len; i++) {
     size_t r = i % 16;
     unsigned char c = *(buf + i);

     if (r == 0) {
        if (i) ss << std::endl;
        ss << "   " << hex((i/16)*16, addr_len) << ": ";
        s = "                ";
        }

     ss << hex(c, 2) << ' ';
     if (std::isprint(c) == 0)
        c = 32;
     s[i % 16] = c;
     if (r == 15)
        ss << "; " << s;
     if (i == len-1) {
        size_t n = len % 16;
        if (n > 0)
           ss << std::string((16-n)*3, ' ') << "; " << s;
        }
     }

  ss << std::endl << "   " << std::string(hdr_len, '=') << std::endl;

  if (to_stderr)
     std::cerr << ss.str() << std::endl;
  else
     std::cout << ss.str() << std::endl;
}

void HexDumpW(std::wstring intro, const unsigned char* buf, size_t len, bool to_stderr) {
  if (intro.empty())
     intro = L"(null)";
  HexDump(WStrToStr(intro), buf, len, to_stderr);
}

void HexDump(std::string intro, std::vector<unsigned char>& vec, bool to_stderr) {
  HexDump(intro, vec.data(), vec.size(), to_stderr);
}

void HexDumpW(std::wstring intro, std::vector<unsigned char>& vec, bool to_stderr) {
  if (intro.empty())
     intro = L"(null)";
  HexDump(WStrToStr(intro), vec.data(), vec.size(), to_stderr);
}

template<class T> void IntDump(std::string intro, const T* buf, size_t len, bool to_stderr) {
  size_t addr_len = hex(len,2).size();
  size_t len1 = 1;
  size_t len2 = 1;
  size_t len3 = sizeof(T) * 2;
  size_t hdr_len = (addr_len + 1) + (16 * (1 + len3));

  std::stringstream ss;

  if (buf == nullptr)
     len = 0;

  if (intro.empty())
     intro = "(null)";

  /*****************************************************************************
   * write caption and number of bytes.
   ****************************************************************************/
  if (intro.size() <= (hdr_len - 4)) {
     len1 = (hdr_len - 2)/2 - (size_t) (0.5 + intro.size()/2.0);
     len2 = (hdr_len - 2)   - (len1 + intro.size());
     }

  ss << "   "
     << std::string(len1, '=')
     << ' ' << intro << ' '
     << std::string(len2, '=')
     << std::endl
     << "   len = " << len << std::endl;


  /*****************************************************************************
   * now, print rows of 16 T's
   ****************************************************************************/
  for(size_t i=0; i<len; i++) {
     size_t r = i % 16;
     T u = *(buf + i);

     if (r == 0) {
        if (i) ss << std::endl;
        ss << "   " << hex((i/16)*16, addr_len) << ": ";
        }

     ss << hex(u, len3) << ' ';
     }

  ss << std::endl << "   " << std::string(hdr_len, '=') << std::endl;

  if (to_stderr)
     std::cerr << ss.str() << std::endl;
  else
     std::cout << ss.str() << std::endl;
}

void ByteDump(std::string intro, const uint8_t* buf, size_t len, bool to_stderr) {
  IntDump(intro, buf, len, to_stderr);
}

void WordDump(std::string intro, const uint16_t* buf, size_t len, bool to_stderr) {
  IntDump(intro, buf, len, to_stderr);
}

void DwordDump(std::string intro, const uint32_t* buf, size_t len, bool to_stderr) {
  IntDump(intro, buf, len, to_stderr);
}

void QwordDump(std::string intro, const uint64_t* buf, size_t len, bool to_stderr) {
  IntDump(intro, buf, len, to_stderr);
}