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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
/* hexedit.c - Hexadecimal file editor
*
* Copyright 2015 Rob Landley <rob@landley.net>
*
* No standard
USE_HEXEDIT(NEWTOY(hexedit, "<1>1r", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_LOCALE))
config HEXEDIT
bool "hexedit"
default y
help
usage: hexedit FILE
Hexadecimal file editor/viewer. All changes are written to disk immediately.
-r Read only (display but don't edit)
Keys:
Arrows Move left/right/up/down by one line/column
PgUp/PgDn Move up/down by one page
Home/End Start/end of line (start/end of file with ctrl)
0-9, a-f Change current half-byte to hexadecimal value
^J or : Jump (+/- for relative offset, otherwise absolute address)
^F or / Find string (^G/n: next, ^D/p: previous match)
u Undo
x Toggle bw/color display
q/^C/^Q/Esc Quit
*/
#define FOR_hexedit
#include "toys.h"
GLOBALS(
char *data, *search, keybuf[16], input[80];
long long len, base, pos;
int numlen, undo, undolen, mode;
unsigned rows, cols;
)
#define UNDO_LEN (sizeof(toybuf)/(sizeof(long long)+1))
static void show_error(char *what)
{
tty_jump(0, TT.rows);
printf("\e[41m\e[37m\e[K\e[1m%s\e[0m", what);
xflush(1);
msleep(500);
}
// TODO: support arrow keys, insertion, and scrolling (and reuse in vi)
static int prompt(char *prompt, char *initial_value)
{
int yes = 0, key, len = strlen(initial_value);
strcpy(TT.input, initial_value);
while (1) {
tty_jump(0, TT.rows);
tty_esc("K");
printf("\e[1m%s: \e[0m%s", prompt, TT.input);
tty_esc("?25h");
xflush(1);
key = scan_key(TT.keybuf, -1);
if (key < 0 || key == 27) break;
if (key == '\r') {
yes = len; // Hitting enter with no input counts as cancellation.
break;
}
if (key == 0x7f && (len > 0)) TT.input[--len] = 0;
else if (key == 'U'-'@') while (len > 0) TT.input[--len] = 0;
else if (key >= ' ' && key < 0x7f && len < sizeof(TT.input))
TT.input[len++] = key;
}
tty_esc("?25l");
return yes;
}
// Render all characters printable, using color to distinguish.
static void draw_char(int ch)
{
if (ch >= ' ' && ch < 0x7f) {
putchar(ch);
return;
}
if (TT.mode) {
if (ch>127) {
tty_esc("2m");
ch &= 127;
}
if (ch<32 || ch==127) {
tty_esc("7m");
if (ch==127) ch = 32;
else ch += 64;
}
xputc(ch);
} else {
if (ch < ' ') printf("\e[31m%c", ch + '@');
else tty_esc("35m?");
}
tty_esc("0m");
}
static void draw_status(void)
{
char line[80];
tty_jump(0, TT.rows);
tty_esc("K");
snprintf(line, sizeof(line), "\"%s\"%s, %#llx/%#llx", *toys.optargs,
FLAG(r) ? " [readonly]" : "", TT.pos, TT.len);
draw_trim(line, -1, TT.cols);
}
static void draw_byte(int byte)
{
if (byte) printf("%02x", byte);
else printf("\e[2m00\e[0m");
}
static void draw_line(long long yy)
{
int x, xx = 16;
yy = (TT.base+yy)*16;
if (yy+xx>=TT.len) xx = TT.len-yy;
if (yy<TT.len) {
printf("\r\e[%dm%0*llx\e[0m ", 33*!TT.mode, TT.numlen, yy);
for (x=0; x<xx; x++) {
putchar(' ');
draw_byte(TT.data[yy+x]);
}
printf("%*s", 2+3*(16-xx), "");
for (x=0; x<xx; x++) draw_char(TT.data[yy+x]);
printf("%*s", 16-xx, "");
}
tty_esc("K");
}
static void draw_page(void)
{
int y;
tty_jump(0, 0);
for (y = 0; y<TT.rows; y++) {
if (y) printf("\r\n");
draw_line(y);
}
draw_status();
}
// side: 0 = editing left, 1 = editing right, 2 = clear, 3 = read only
static void highlight(int xx, int yy, int side)
{
char cc = TT.data[16*(TT.base+yy)+xx];
int i;
// Display cursor in hex area.
tty_jump(2+TT.numlen+3*xx, yy);
tty_esc("0m");
if (side!=2) tty_esc("7m");
if (side>1) draw_byte(cc);
else for (i=0; i<2;) {
if (side==i) tty_esc("32m");
printf("%x", (cc>>(4*(1&++i)))&15);
}
tty_jump(TT.numlen+17*3+xx, yy);
// Display cursor in text area.
if (side!=2) tty_esc("7m");
draw_char(cc);
}
static void find_next(int pos)
{
char *p;
p = memmem(TT.data+pos, TT.len-pos, TT.search, strlen(TT.search));
if (p) TT.pos = p - TT.data;
else show_error("No match!");
}
static void find_prev(int pos)
{
size_t len = strlen(TT.search);
for (; pos >= 0; pos--) {
if (!memcmp(TT.data+pos, TT.search, len)) {
TT.pos = pos;
return;
}
}
show_error("No match!");
}
void hexedit_main(void)
{
long long y;
int x, i, side = 0, key, fd;
// Terminal setup
TT.cols = 80;
TT.rows = 24;
terminal_size(&TT.cols, &TT.rows);
if (TT.rows) TT.rows--;
xsignal(SIGWINCH, generic_signal);
sigatexit(tty_sigreset);
tty_esc("0m");
tty_esc("?25l");
xflush(1);
xset_terminal(1, 1, 0, 0);
if (access(*toys.optargs, W_OK)) toys.optflags |= FLAG_r;
fd = xopen(*toys.optargs, FLAG(r) ? O_RDONLY : O_RDWR);
if ((TT.len = fdlength(fd))<1) error_exit("bad length");
if (sizeof(long)==32 && TT.len>SIZE_MAX) TT.len = SIZE_MAX;
// count file length hex in digits, rounded up to multiple of 4
for (TT.pos = TT.len, TT.numlen = 0; TT.pos; TT.pos >>= 4, TT.numlen++);
TT.numlen += (4-TT.numlen)&3;
TT.data=xmmap(0, TT.len, PROT_READ|(PROT_WRITE*!FLAG(r)), MAP_SHARED, fd, 0);
close(fd);
draw_page();
for (;;) {
// Scroll display if necessary
if (TT.pos<0) TT.pos = 0;
if (TT.pos>=TT.len) TT.pos = TT.len-1;
x = TT.pos&15;
y = TT.pos/16;
while (y<TT.base) {
if (TT.base-y>(TT.rows/2)) {
TT.base = y;
draw_page();
} else {
TT.base--;
tty_jump(0, 0);
tty_esc("1L");
draw_line(0);
}
}
while (y>=TT.base+TT.rows) {
if (y-(TT.base+TT.rows)>(TT.rows/2)) {
TT.base = y-TT.rows-1;
draw_page();
} else {
TT.base++;
tty_jump(0, 0);
tty_esc("1M");
tty_jump(0, TT.rows-1);
draw_line(TT.rows-1);
}
}
draw_status();
y -= TT.base;
// Display cursor and flush output
highlight(x, y, FLAG(r) ? 3 : side);
xflush(1);
// Wait for next key
key = scan_key(TT.keybuf, -1);
// Window resized?
if (key == -3) {
toys.signal = 0;
terminal_size(&TT.cols, &TT.rows);
if (TT.rows) TT.rows--;
draw_page();
continue;
}
if (key == 'x') {
TT.mode = !TT.mode;
tty_esc("0m");
draw_page();
continue;
}
// Various popular ways to quit...
if (key==-1||key==('C'-'@')||key==('Q'-'@')||key==27||key=='q') break;
highlight(x, y, 2);
if (key == ('J'-'@') || key == ':' || key == '-' || key == '+') {
// Jump (relative or absolute)
char initial[2] = {}, *s = 0;
long long val;
if (key == '-' || key == '+') *initial = key;
if (!prompt("Jump to", initial)) continue;
val = estrtol(TT.input, &s, 0);
if (!errno && s && !*s) {
if (*TT.input == '-' || *TT.input == '+') TT.pos += val;
else TT.pos = val;
}
continue;
} else if (key == ('F'-'@') || key == '/') { // Find
if (!prompt("Find", TT.search ? TT.search : "")) continue;
// TODO: parse hex escapes in input, and record length to support \0
free(TT.search);
TT.search = xstrdup(TT.input);
find_next(TT.pos);
} else if (TT.search && (key == ('G'-'@') || key == 'n')) { // Find next
if (TT.pos < TT.len) find_next(TT.pos+1);
} else if (TT.search && (key == ('D'-'@') || key == 'p')) { // Find previous
if (TT.pos > 0) find_prev(TT.pos-1);
}
// Remove cursor
highlight(x, y, 2);
// Hex digit?
if (key>='a' && key<='f') key-=32;
if (!FLAG(r) && ((key>='0' && key<='9') || (key>='A' && key<='F'))) {
if (!side) {
long long *ll = (long long *)toybuf;
ll[TT.undo] = TT.pos;
toybuf[(sizeof(long long)*UNDO_LEN)+TT.undo++] = TT.data[TT.pos];
if (TT.undolen < UNDO_LEN) TT.undolen++;
TT.undo %= UNDO_LEN;
}
i = key - '0';
if (i>9) i -= 7;
TT.data[TT.pos] &= 15<<(4*side);
TT.data[TT.pos] |= i<<(4*!side);
if (++side==2) {
highlight(x, y, side);
side = 0;
++TT.pos;
}
} else side = 0;
if (key=='u') {
if (TT.undolen) {
long long *ll = (long long *)toybuf;
TT.undolen--;
if (!TT.undo) TT.undo = UNDO_LEN;
TT.pos = ll[--TT.undo];
TT.data[TT.pos] = toybuf[sizeof(long long)*UNDO_LEN+TT.undo];
}
}
if (key>=256) {
key -= 256;
if (key==KEY_UP) TT.pos -= 16;
else if (key==KEY_DOWN) TT.pos += 16;
else if (key==KEY_RIGHT) {
if (TT.pos<TT.len) TT.pos++;
} else if (key==KEY_LEFT) {
if (TT.pos>0) TT.pos--;
} else if (key==KEY_PGUP) {
TT.pos -= 16*TT.rows;
if (TT.pos < 0) TT.pos = 0;
TT.base = TT.pos/16;
draw_page();
} else if (key==KEY_PGDN) {
TT.pos += 16*TT.rows;
if (TT.pos > TT.len-1) TT.pos = TT.len-1;
TT.base = TT.pos/16;
draw_page();
} else if (key==KEY_HOME) TT.pos = TT.pos & ~0xf;
else if (key==KEY_END) TT.pos = TT.pos | 0xf;
else if (key==(KEY_CTRL|KEY_HOME)) TT.pos = 0;
else if (key==(KEY_CTRL|KEY_END)) TT.pos = TT.len-1;
}
}
munmap(TT.data, TT.len);
tty_reset();
}
|