-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhexdump.cpp
More file actions
139 lines (120 loc) · 4.34 KB
/
Copy pathhexdump.cpp
File metadata and controls
139 lines (120 loc) · 4.34 KB
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
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <string_view>
#include <cfbox/applet.hpp>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
#include <cfbox/error.hpp>
#include <cfbox/io.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "hexdump",
.version = CFBOX_VERSION_STRING,
.one_line = "display file contents in hexadecimal",
.usage = "hexdump [-C] [-n LENGTH] [-s OFFSET] [FILE]",
.options = " -C canonical hex+ASCII display\n"
" -n N interpret only N bytes\n"
" -s N skip first N bytes",
.extra = "",
};
auto hexdump_canonical(const unsigned char* data, std::size_t len,
std::uint64_t base_offset) -> void {
for (std::size_t i = 0; i < len; i += 16) {
auto chunk = static_cast<int>(len - i > 16 ? 16 : len - i);
// Offset
std::printf("%08llx ", static_cast<unsigned long long>(base_offset + i));
// Hex bytes (two groups of 8)
for (int j = 0; j < 16; ++j) {
if (j == 8) std::printf(" ");
if (j < chunk) {
std::printf("%02x ", data[i + static_cast<size_t>(j)]);
} else {
std::printf(" ");
}
}
// ASCII
std::printf(" |");
for (int j = 0; j < chunk; ++j) {
auto c = data[i + static_cast<size_t>(j)];
std::putchar(static_cast<int>(c >= 32 && c < 127 ? c : '.'));
}
std::printf("|\n");
}
}
} // anonymous namespace
auto hexdump_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'C', false, "canonical"},
cfbox::args::OptSpec{'n', true, "length"},
cfbox::args::OptSpec{'s', true, "skip"},
});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
bool canonical = parsed.has('C') || parsed.has_long("canonical");
std::size_t max_bytes = 0;
std::uint64_t skip_bytes = 0;
if (auto v = parsed.get('n')) {
auto parsed_n = cfbox::args::parse_int(*v);
if (!parsed_n) {
CFBOX_ERR("hexdump", "%s", parsed_n.error().msg.c_str());
return 2;
}
max_bytes = static_cast<std::size_t>(*parsed_n);
}
if (auto v = parsed.get('s')) {
auto parsed_s = cfbox::args::parse_int(*v);
if (!parsed_s) {
CFBOX_ERR("hexdump", "%s", parsed_s.error().msg.c_str());
return 2;
}
skip_bytes = static_cast<std::uint64_t>(*parsed_s);
}
const auto& pos = parsed.positional();
std::string filename = pos.empty() ? "" : std::string(pos[0]);
auto do_dump = [&](std::FILE* f) -> int {
if (skip_bytes > 0) {
std::fseek(f, static_cast<long>(skip_bytes), SEEK_SET);
}
unsigned char buf[16];
std::uint64_t offset = skip_bytes;
std::size_t total = 0;
while (true) {
auto to_read = static_cast<std::size_t>(16);
if (max_bytes > 0 && total + to_read > max_bytes) {
to_read = max_bytes - total;
}
if (to_read == 0) break;
auto n = std::fread(buf, 1, to_read, f);
if (n == 0) break;
if (canonical) {
hexdump_canonical(buf, n, offset);
} else {
// Default format: similar but without ASCII
for (std::size_t i = 0; i < n; i += 16) {
auto chunk = static_cast<int>(n - i > 16 ? 16 : n - i);
std::printf("%08llx ", static_cast<unsigned long long>(offset + i));
for (int j = 0; j < chunk; ++j) {
std::printf("%02x ", buf[i + static_cast<size_t>(j)]);
}
std::printf("\n");
}
}
offset += n;
total += n;
}
return 0;
};
if (filename.empty() || filename == "-") {
return do_dump(stdin);
}
auto file_result = cfbox::io::open_file(filename, "rb");
if (!file_result) {
CFBOX_ERR("hexdump", "cannot open %s", filename.c_str());
return 1;
}
auto f = std::move(*file_result);
return do_dump(f.get());
}