-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadbytes.cpp
116 lines (100 loc) · 3.26 KB
/
readbytes.cpp
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
/* Bytes Reader
Copyright 2024 KeyofBlueS
Bytes Reader is free software;
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation;
either version 3, or (at your option) any later version.
See the file COPYING for more details.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
#include <vector>
#include <cstdlib>
// Function to extract bytes from a binary file and return them as a hexadecimal string.
std::string extractBytes(const std::string& filePath, int offset, int length) {
std::ifstream file(filePath, std::ios::binary);
if (!file.is_open()) {
std::cerr << "* ERROR: Unable to open file: " << filePath << std::endl;
return "";
}
file.seekg(offset, std::ios::beg);
std::vector<char> buffer(length);
file.read(buffer.data(), length);
file.close();
std::stringstream hexStream;
for (int i = 0; i < length; ++i) {
hexStream << std::hex << std::setw(2) << std::setfill('0') << (int)(unsigned char)buffer[i];
}
return hexStream.str();
}
// Function to print the help message
void printHelpMessage() {
std::cout << "\n";
std::cout << "Bytes Reader v0.0.1\n";
std::cout << "\n";
std::cout << "Usage: readbytes <input_file> [options]\n";
std::cout << "\n";
std::cout << "Options:\n";
std::cout << " -s, --seek <offset> Set the offset (default is 0)\n";
std::cout << " -l, --length <length> Set the number of bytes to read (default is 4)\n";
std::cout << " -h, --help Show this help message and exit\n";
std::cout << "\n";
std::cout << "Copyright © 2024 KeyofBlueS: <https://github.com/KeyofBlueS>.\n";
std::cout << "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.\n";
std::cout << "This is free software: you are free to change and redistribute it.\n";
std::cout << "There is NO WARRANTY, to the extent permitted by law.\n";
}
// Main function
int main(int argc, char* argv[]) {
std::string inputFile;
int offset = 0;
int length = 4;
// Parse command-line arguments
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "-h" || arg == "--help") {
printHelpMessage();
return 0;
} else if (arg == "-s" || arg == "--seek") {
if (i + 1 < argc) {
offset = std::stoi(argv[++i]);
} else {
std::cerr << "* ERROR: Missing value for option '" << arg << "'\n";
return 1;
}
} else if (arg == "-l" || arg == "--length") {
if (i + 1 < argc) {
length = std::stoi(argv[++i]);
} else {
std::cerr << "* ERROR: Missing value for option '" << arg << "'\n";
return 1;
}
} else {
inputFile = arg; // Assume any other argument is the input file
}
}
// Validate input file
if (inputFile.empty()) {
std::cerr << "* ERROR: No input file specified.\n";
printHelpMessage();
return 1;
}
// Validate length
if (length <= 0) {
std::cerr << "* ERROR: Length must be greater than 0.\n";
return 1;
}
// Validate offset
if (offset < 0) {
std::cerr << "* ERROR: Offset must not be negative.\n";
return 1;
}
// Extract bytes from the file at the specified offset and length
std::string bytesHex = extractBytes(inputFile, offset, length);
// Print the extracted bytes in hexadecimal format
std::cout << bytesHex << std::endl;
return 0;
}