helper.cpp revision 99b09c72803e43ed9a85819214c0570e45afb352
1#include "helper.h"
2
3#include <iostream>
4#include <iomanip>
5
6#include <stdlib.h>
7
8#include "term.h"
9
10using namespace std;
11using namespace term::color;
12
13void dump_hex(unsigned char const *data,
14              size_t size, size_t begin, size_t end) {
15  if (end <= begin) {
16    // Nothing to print now.  Return directly.
17    return;
18  }
19
20  size_t lower = begin & (~0xfUL);
21  size_t upper = (end & (~0xfUL)) ? end : ((end + 16UL) & (~0xfUL));
22
23  for (size_t i = lower; i < upper; i += 16) {
24    cout << hex << setfill('0') << setw(8) << right << i << left << ':';
25
26    if (i < begin) {
27      cout << dark::magenta();
28    }
29
30    for (size_t j = i, k = i + 16; j < k; ++j) {
31      if (j == begin) {
32        cout << normal();
33      }
34
35      if (j == end) {
36        cout << dark::magenta();
37      }
38
39      if (j < size) {
40        cout << ' ' << hex << setfill('0') << setw(2) << (unsigned)data[j];
41      }
42    }
43
44    cout << normal() << endl;
45  }
46}
47