-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxxd.cc
More file actions
100 lines (94 loc) · 3.02 KB
/
xxd.cc
File metadata and controls
100 lines (94 loc) · 3.02 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
#include <sys/stat.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
#include <sstream>
static const char *option_string = "s:abc:EeghHil:ouv";
static const char *__version = "\033[1;31m0.0.1\033[0m";
std::string ContentToHex(char Content, bool upper = false) {
std::stringstream stream_tmp;
stream_tmp
<< std::hex
<< ((upper) ? std::uppercase // If upper = true,return uppercase strings
: std::nouppercase)
<< (Content & 0xFF); // else return lowercase (Default)
return stream_tmp.str();
}
// Function that returns help infomation string
const std::string PrintHelp() {
std::stringstream stream_tmp;
stream_tmp << "Usage:\n\t\t\033[1;31mxxd\033[0m \033[2;38m[options]\033[0m "
"[infile [outfile]]\n\tor\n\t\t\033[1;31mxxd\033[0m -r "
"[-s [-]offset [-c cols] [-ps] [infile [outfile]]]";
return stream_tmp.str();
}
inline bool CheckFileExist(char *&file_name) {
return (access(file_name, F_OK) != -1);
}
const std::string ConvertToHex(std::ifstream &input_file, bool upper = false,
int width = 16) {
std::string file_contents; // String to store file contents
input_file.seekg(0, std::ios::end);
long file_length = input_file.tellg();
file_contents.resize(file_length); // read file sieze
input_file.seekg(0, std::ios::beg);
input_file.read(&file_contents[0],
file_contents.size()); // read file buff to string
// now start converting
std::stringstream hex_stream;
for (auto byte_content : file_contents) {
hex_stream << ContentToHex(byte_content, upper); // Convert byte-to-byte
}
return hex_stream.str();
}
std::string ConvertToHex(std::stringstream user_input) {
// catch stdin
}
int main(int argc, char **argv) {
std::ifstream input_file;
std::ofstream output_file;
int seek = 0;
if (argc < 2) // No inputs?
std::cout << PrintHelp();
else {
int params = 0;
bool upper = false;
params = getopt(argc, argv, option_string); // parse command options
while (params != -1) {
switch (params) {
case 'v':
std::cout<<"\033[1;34mxxd-cpp\033[0m version: " << __version<<std::endl;
return 0;
break;
case 'H':
case 'h':
case '?':
std::cout << PrintHelp();
return 0;
break;
case 'u':
upper = true;
break;
case 's':
seek = optind;
break;
case 'o':
output_file.open(argv[optind], std::ios::binary);
break;
}
params = getopt(argc, argv, option_string);
}
if (!CheckFileExist(argv[optind])) {
std::cout << "xxd: " << argv[1] << ": No such file or directory."
<< std::endl; // file not exist
return -1;
}
if (input_file.good()) input_file.open(argv[optind], std::ios::binary);
std::string hexdump = ConvertToHex(input_file, upper);
if (output_file.is_open())
output_file << hexdump;
else
std::cout << hexdump<<std::endl;
}
return 0;
}