-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
326 lines (296 loc) · 7.6 KB
/
main.cpp
File metadata and controls
326 lines (296 loc) · 7.6 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <algorithm>
#include <climits>
#include <cstdint>
#include <cstring>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include "buffer.h"
struct options
{
std::string search_string;
std::unique_ptr<buffer> search_bytes;
std::list<std::string> input_files;
int16_t context_before;
int16_t context_after;
};
void save_file(const std::string& filename, const buffer& buf)
{
std::ofstream outfile(filename, std::ios::out | std::ios::binary);
if (!outfile) {
std::cerr << "Could not save file " << filename << "!\n";
return;
}
outfile.write((char*)&buf[0], buf.length());
outfile.close();
}
void usage()
{
std::cerr << "Usage: gb [-s] <string> [<filename> <filename> ...] \n"
<< " or: gb -b <byte#> <byte> [-b ...] [<filename> <filename> ...]\n"
<< " or: gb -be <big-endian value> [<filename> <filename> ...]\n"
<< " or: gb -le <little-endian value> [<filename> <filename> ...]\n";
}
bool get_window_dimensions(uint32_t& rows, uint32_t& cols)
{
struct winsize ws;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
return false;
}
rows = ws.ws_row;
cols = ws.ws_col;
return true;
}
uint32_t get_default_context_len(uint32_t needle_len)
{
uint32_t rows, cols;
if (!get_window_dimensions(rows, cols)) {
return 16;
}
// Each byte of context and needle requires 4 characters
// There's two contexts (before and after) for an additional /2
// 17 characters of "slush"
// Extra -1 in there because sometimes the numbers don't divide evenly
return ((((cols - 17 - (needle_len * 4)) / 4) - 1) / 2);
}
bool get_opts(int argc, char** argv, options& opts)
{
bool got_needle = false;
opts.context_before = -1;
opts.context_after = -1;
std::vector<uint8_t> needle_bytes;
std::string needle_string;
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
// Read the option
switch (argv[i][1]) {
//
// Get the needle
//
case 'b':
if (argv[i][2] == '\0') { // -b
if (++i == argc) {
return false;
}
std::stringstream ss(argv[i]);
uint32_t idx;
ss >> std::dec >> idx;
if (++i == argc) {
return false;
}
ss.str(argv[i]);
ss.clear();
uint32_t byte;
ss >> std::hex >> byte;
if (needle_bytes.size() < (idx + 1)) {
needle_bytes.resize(idx + 1);
}
needle_bytes[idx] = byte;
got_needle = true;
} else if (argv[i][2] == 'e' && argv[i][3] == '\0') { // -be
if (++i == argc) {
std::cerr << "-be requires an argument\n";
return false;
}
if (got_needle) {
std::cerr << "Only one search pattern can be specified\n";
return false;
}
opts.search_bytes = buffer_conversion::number_string_to_buffer(argv[i], true, true);
if (opts.search_bytes) {
got_needle = true;
}
} else {
std::cerr << "Unrecognized option " << argv[i] << '\n';
return false;
}
break;
case 'l':
if (argv[i][2] == 'e' && argv[i][3] == '\0') {
if (++i == argc) {
std::cerr << "-be requires an argument\n";
return false;
}
if (got_needle) {
std::cerr << "Only one search pattern can be specified\n";
return false;
}
opts.search_bytes = buffer_conversion::number_string_to_buffer(argv[i], false, true);
if (opts.search_bytes) {
got_needle = true;
}
} else {
std::cerr << "Unrecognized option " << argv[i] << '\n';
return false;
}
break;
case 's':
if (argv[i][2] == '\0') {
if (++i == argc || needle_bytes.size() > 0 || !needle_string.empty()) {
return false;
}
needle_string = argv[i];
} else {
std::cerr << "Unrecognized option " << argv[i] << '\n';
return false;
}
got_needle = true;
break;
//
// Display options
//
case 'A':
case 'B':
if (argv[i][2] == '\0') { // -B, -A
char c = argv[i][1];
if (++i == argc) {
return false;
}
std::stringstream ss(argv[i]);
uint32_t ctx;
ss >> std::dec >> ctx;
if (c == 'B') {
opts.context_before = ctx;
} else if (c == 'A') {
opts.context_after = ctx;
}
}
break;
default:
std::cerr << "Unrecognized option " << argv[i] << '\n';
return false;
}
} else {
if (!got_needle) {
// Assume this is the search string
if (!needle_bytes.empty()) {
std::cerr << "Error: Can't specify both a search string and search bytes\n";
return false;
}
needle_string = argv[i];
got_needle = true;
} else {
opts.input_files.emplace_back(argv[i]);
}
}
}
if (!needle_bytes.empty()) {
opts.search_bytes = std::make_unique<arraybuf>(needle_bytes);
} else if (!needle_string.empty()) {
opts.search_bytes = std::make_unique<strbuf>(needle_string);
}
return got_needle;
}
void print_match(const buffer& buf,
uint32_t offset,
uint32_t needle_len,
int16_t context_before,
int16_t context_after)
{
const char red_on[] = "\x1B[31m";
const char red_off[] = "\033[0m";
int16_t default_context_len = get_default_context_len(needle_len);
if (context_before < 0) {
context_before = default_context_len;
}
if (context_after < 0) {
context_after = default_context_len;
}
uint32_t len = context_before + context_after + needle_len;
uint32_t start = offset;
if (start > (uint16_t)context_before) {
start -= context_before;
} else {
start = 0;
}
// A line should look like:
// <offset>: <context-before><match><context-after> | ASCII........ |
std::cout << std::hex << std::setw(8) << std::setfill(' ') << start << ": ";
for (uint32_t i = start; i < start + len; ++i) {
if (i >= buf.length()) {
break;
}
if (i == offset) {
std::cout << red_on;
}
std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)buf[i] << ' ';
if (i == offset + needle_len - 1) {
std::cout << red_off;
}
}
std::cout << " | ";
// Now do it again to print the ASCII representation...
for (uint32_t i = start; i < start + len; ++i) {
if (i >= buf.length()) {
break;
}
if (i == offset) {
std::cout << red_on;
}
if (isprint(buf[i])) {
std::cout << std::setw(0) << (char)buf[i];
} else {
std::cout << std::setw(0) << '.';
}
if (i == offset + needle_len - 1) {
std::cout << red_off;
}
}
std::cout << " |" << std::endl;
}
int main(int argc, char** argv)
{
/*
* TODO:
* - Find and replace
* - More flexible search terms / options
* - Turn buffer code into a separate library
*/
options opts;
if (!get_opts(argc, argv, opts)) {
usage();
return -1;
}
if (opts.input_files.empty()) {
// Read from stdin
opts.input_files.push_back("-");
}
// Go through each input file
for (const std::string& savefile : opts.input_files) {
if (opts.input_files.size() > 1) {
std::cout << savefile << ':' << std::endl;
}
// Read the file
std::unique_ptr<buffer> buf;
if (savefile == "-") {
buf = std::make_unique<arraybuf>(std::cin);
} else {
buf = std::make_unique<arraybuf>(savefile);
}
if (buf->length() == 0) {
std::cerr << "Could not read file " << savefile << std::endl;
return -2;
}
// Search the file
uint32_t needle_len = opts.search_bytes->length();
if (needle_len == 0) {
std::cerr << "Null search string\n";
return -3;
}
std::list<uint32_t> offsets = buf->find_all(*opts.search_bytes);
// Print each output with context
for (uint32_t offset : offsets) {
print_match(*buf, offset, needle_len, opts.context_before, opts.context_after);
}
}
return 0;
}