-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
1202 lines (968 loc) · 22.8 KB
/
main.c
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <sys/stat.h>
#include <ncurses.h>
#define BUFFER_SIZE 16 * 1024
#define MAX_COMMAND_LEN 256
#define MAX_ERROR_LEN 64
#define KEY_ESC 27
#define KEY_RETURN 10
#define KEY_DELETE 127
#define STYLE_ERROR 13
#define STYLE_CURSOR 14
#define CHARS_PER_BYTE 3
#define ESCAPE_SEQUENCE_MAX_TIME_MS 50
typedef struct
{
WINDOW* window;
int left;
int top;
int width;
int height;
} pane;
#define right(pane) (pane.left + pane.width)
#define bottom(pane) (pane.top + pane.height)
#define PANES_LEN 3
#define PANE_HEX 0
#define PANE_ASCII 1
#define PANE_DETAIL 2
pane panes[PANES_LEN];
typedef struct
{
int x;
int y;
} point;
unsigned char* source = NULL;
int source_len;
char* original_filename;
int cursor_byte = 0;
int cursor_nibble = 0;
int scroll_start = 0;
int max_x;
int max_y;
char command[MAX_COMMAND_LEN];
int command_len;
bool command_entering = false;
// 2 hex digits plus space
#define MAX_SEARCH_TERM_LEN MAX_COMMAND_LEN / 3
unsigned char search_term[MAX_SEARCH_TERM_LEN];
int search_term_len;
char error_text[MAX_ERROR_LEN];
bool error_displayed = false;
void set_error(const char* text)
{
error_displayed = true;
strncpy(error_text, text, MAX_ERROR_LEN);
}
void setup_pane(pane* pane)
{
if (pane->window)
{
delwin(pane->window);
}
pane->window = newwin(pane->height, pane->width, pane->top, pane->left);
}
char nibble_to_hex(unsigned char nibble)
{
if (nibble < 10)
{
return '0' + nibble;
}
return 'a' + nibble - 10;
}
unsigned char hex_to_nibble(char hex)
{
if (hex >= 'a')
{
return hex - 'a' + 10;
}
return hex - '0';
}
unsigned char first_nibble(unsigned char byte)
{
return byte >> 4;
}
unsigned char second_nibble(unsigned char byte)
{
return byte & 0x0f;
}
void byte_to_hex(unsigned char byte, char* hex)
{
hex[0] = nibble_to_hex(first_nibble(byte));
hex[1] = nibble_to_hex(second_nibble(byte));
}
unsigned char nibbles_to_byte(unsigned char n0, unsigned char n1)
{
return n0 << 4 | n1;
}
int bytes_per_line()
{
return panes[PANE_HEX].width / CHARS_PER_BYTE;
}
int byte_in_line(int byte_offset)
{
return byte_offset / bytes_per_line();
}
int byte_in_column(int byte_offset)
{
return byte_offset % bytes_per_line() * CHARS_PER_BYTE;
}
int first_byte_in_line(int line_index)
{
return line_index * bytes_per_line();
}
int last_byte_in_line(int line_index)
{
return first_byte_in_line(line_index + 1) - 1;
}
int first_visible_byte()
{
return first_byte_in_line(scroll_start);
}
int last_visible_line()
{
return scroll_start + panes[PANE_HEX].height - 1;
}
int last_visible_byte()
{
int ret = last_byte_in_line(last_visible_line());
return ret < source_len ? ret : source_len - 1;
}
void handle_sizing()
{
static int last_max_x = -1;
static int last_max_y = -1;
getmaxyx(stdscr, max_y, max_x);
if (max_y == last_max_y && max_x == last_max_x)
{
return;
}
// Terminal resized
last_max_x = max_x;
last_max_y = max_y;
panes[PANE_DETAIL].height = 7;
panes[PANE_HEX].left = 1;
panes[PANE_HEX].top = 0;
panes[PANE_HEX].width = (max_x - 1) * 0.75;
panes[PANE_HEX].height = max_y - panes[PANE_DETAIL].height;
setup_pane(&panes[PANE_HEX]);
panes[PANE_ASCII].left = panes[PANE_HEX].left + panes[PANE_HEX].width;
panes[PANE_ASCII].top = panes[PANE_HEX].top;
panes[PANE_ASCII].width = (max_x - 1) * 0.25;
panes[PANE_ASCII].height = max_y - panes[PANE_DETAIL].height;
setup_pane(&panes[PANE_ASCII]);
panes[PANE_DETAIL].left = 0;
panes[PANE_DETAIL].top = max_y - panes[PANE_DETAIL].height;
panes[PANE_DETAIL].width = max_x;
setup_pane(&panes[PANE_DETAIL]);
}
void handle_start_command(char first_char)
{
command_entering = true;
command[0] = first_char;
command_len = 1;
}
void handle_cancel_command()
{
command_entering = false;
}
void quit()
{
if (source != NULL)
{
free(source);
}
endwin();
exit(0);
}
void handle_write()
{
char* subcommand = command + 2;
int remaining = command_len - 2;
// Check for quit
bool also_quit = false;
if (remaining > 0 && subcommand[0] == 'q')
{
also_quit = true;
subcommand++;
remaining--;
}
// Check for filename in command
char* filename = original_filename;
if (remaining > 0 && subcommand[0] == ' ')
{
filename = subcommand + 1;
}
// Write buffer to disk
FILE* file = fopen(filename, "w");
if (!file)
{
set_error("Error opening file: path not found or permissions?");
return;
}
size_t bytes_written = 0;
size_t bytes_left = source_len;
while (bytes_left > 0)
{
size_t size = BUFFER_SIZE < bytes_left ? BUFFER_SIZE : bytes_left;
size_t written = fwrite(source + bytes_written, 1, size, file);
if (written != size)
{
set_error("Encountered error while writing file; may be corrupt.");
return;
}
bytes_written += written;
bytes_left -= written;
}
fclose(file);
if (also_quit)
{
quit();
}
}
void handle_jump_offset()
{
// Make sure the command is an ASCII integer
for (int i = 1; i < command_len; i++)
{
if (command[i] < '0' || command[i] > '9')
{
return;
}
}
// Move cursor to requested offset
cursor_byte = atoi(command + 1);
cursor_nibble = 0;
}
bool is_hex_digit(char c)
{
return (c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F');
}
void set_search_term(char* hex_ascii, int len)
{
int cur = 0;
search_term_len = 0;
while (true)
{
// Skip any whitespace
while (cur < len && isspace(hex_ascii[cur]))
{
cur++;
}
if (cur >= len)
{
break;
}
unsigned char first = hex_ascii[cur++];
unsigned char second = hex_ascii[cur++];
if (!is_hex_digit(first) || !is_hex_digit(second))
{
search_term_len = 0;
set_error("Invalid search term format");
return;
}
if (search_term_len >= MAX_SEARCH_TERM_LEN)
{
search_term_len = 0;
set_error("Search term storage overflow");
return;
}
search_term[search_term_len++] = nibbles_to_byte(
hex_to_nibble(first), hex_to_nibble(second));
}
}
void handle_search_next()
{
if (!search_term_len)
{
return;
}
int cur = cursor_byte + 1;
while (cur != cursor_byte)
{
if (cur + search_term_len >= source_len)
{
cur = 0;
}
bool match = true;
if (cur + search_term_len >= source_len)
{
continue;
}
for (int i = 0; i < search_term_len; i++)
{
if (source[cur + i] != search_term[i])
{
match = false;
break;
}
}
if (match)
{
cursor_byte = cur;
cursor_nibble = 0;
return;
}
cur++;
}
set_error("Search term not found");
}
void handle_search_previous()
{
if (!search_term_len)
{
return;
}
int cur = cursor_byte - 1;
while (cur != cursor_byte)
{
if (cur < 0)
{
cur = source_len - search_term_len;
}
bool match = true;
for (int i = 0; i < search_term_len; i++)
{
if (source[cur + i] != search_term[i])
{
match = false;
break;
}
}
if (match)
{
cursor_byte = cur;
cursor_nibble = 0;
return;
}
cur--;
}
set_error("Search term not found");
}
void handle_submit_command()
{
command[command_len] = 0;
command_entering = false;
if (command[0] == '/')
{
set_search_term(&command[1], command_len - 1);
handle_search_next();
return;
}
if (strncmp(command, ":q", MAX_COMMAND_LEN) == 0)
{
quit();
return;
}
if (command[1] == 'w')
{
handle_write();
return;
}
handle_jump_offset();
}
void handle_backspace_command()
{
if (command_len == 1)
{
handle_cancel_command();
return;
}
command_len--;
}
void handle_add_to_command(int event)
{
if (event < 32 || event > '~')
{
return;
}
if (command_len >= MAX_COMMAND_LEN)
{
return;
}
command[command_len++] = event;
}
void handle_command_event(int event)
{
switch (event)
{
case KEY_ESC: handle_cancel_command(); break;
case KEY_RETURN: handle_submit_command(); break;
case KEY_DELETE: handle_backspace_command(); break;
default: handle_add_to_command(event); break;
}
}
void render_command()
{
if (!command_entering)
{
return;
}
int start = 0;
int len = command_len;
// Scroll command horizontally
if (command_len >= max_x - 1)
{
start = command_len - max_x + 1;
len = max_x - 1;
}
move(max_y - 1, 0);
// Print command to screen
for (int i = 0; i < len; i++)
{
addch(command[start + i]);
}
// Clear the rest of the command line
for (int i = len; i < max_x; i++)
{
addch(' ');
}
}
void handle_key_left()
{
if (cursor_nibble == 1)
{
cursor_nibble = 0;
}
else
{
cursor_nibble = 1;
cursor_byte--;
}
}
void handle_key_right()
{
if (cursor_nibble == 0)
{
cursor_nibble = 1;
}
else
{
cursor_nibble = 0;
cursor_byte++;
}
}
void handle_key_up()
{
int temp = cursor_byte - bytes_per_line();
if (temp >= 0)
{
cursor_byte = temp;
}
}
void handle_key_down()
{
int temp = cursor_byte + bytes_per_line();
if (temp < source_len)
{
cursor_byte = temp;
}
}
void handle_previous_byte()
{
cursor_byte--;
}
void handle_next_byte()
{
cursor_byte++;
}
void handle_overwrite(int event)
{
// Convert A-F to lower case
if (event >= 'A' && event <= 'F')
{
event += 'a' - 'A';
}
// Only process 0-9 and a-f
if (!((event >= '0' && event <= '9') || (event >= 'a' && event <= 'f')))
{
return;
}
unsigned char* byte = &source[cursor_byte];
unsigned char first = first_nibble(*byte);
unsigned char second = second_nibble(*byte);
unsigned char* nibble = cursor_nibble ? &second : &first;
*nibble = hex_to_nibble(event);
*byte = nibbles_to_byte(first, second);
handle_key_right();
}
void handle_page_up()
{
cursor_byte -= panes[PANE_HEX].height * bytes_per_line();
}
void handle_page_down()
{
cursor_byte += panes[PANE_HEX].height * bytes_per_line();
scroll_start += panes[PANE_HEX].height;
}
void handle_end_of_buffer()
{
cursor_byte = source_len - 1;
cursor_nibble = 1;
}
// Handle compound commands that start with g (ex: gg).
// Returns true if the event was handled.
bool handle_g_chord(int event)
{
static bool g_pressed = false;
if (!g_pressed)
{
// If g was pressed, remember that for next key in the chord.
// Otherwise, don't consume the event.
g_pressed = event == 'g';
return g_pressed;
}
// Handle second key in the chord.
switch (event)
{
case 'g':
// Chord 'gg' - move to beginning of buffer
cursor_byte = 0;
cursor_nibble = 0;
break;
}
g_pressed = false;
return true;
}
// Given an x,y location, find the pane at that position.
// Return -1 if there is no pane at that position.
int get_pane_under_coords(int x, int y)
{
for (int pane = 0; pane < PANES_LEN; pane++)
{
if (x >= panes[pane].left && x <= right(panes[pane]) &&
y >= panes[pane].top && y <= bottom(panes[pane]))
{
return pane;
}
}
return -1;
}
// Convert screen-space coords to pane-space coords
point screen_to_pane(pane* pane, int x, int y)
{
point ret;
ret.x = x - pane->left;
ret.y = y - pane->top;
return ret;
}
void handle_mouse_pressed(MEVENT mouse_event)
{
int pane = get_pane_under_coords(mouse_event.x, mouse_event.y);
if (pane != PANE_HEX && pane != PANE_ASCII)
{
return;
}
point coords = screen_to_pane(&panes[pane], mouse_event.x, mouse_event.y);
if (pane == PANE_HEX)
{
if (coords.x >= bytes_per_line() * CHARS_PER_BYTE - 1)
{
return;
}
cursor_byte = first_byte_in_line(coords.y + scroll_start) +
coords.x / CHARS_PER_BYTE;
cursor_nibble = coords.x % CHARS_PER_BYTE;
if (cursor_nibble == 2)
{
cursor_byte++;
cursor_nibble = 0;
}
}
else if (pane == PANE_ASCII)
{
if (coords.x >= bytes_per_line())
{
return;
}
cursor_byte = first_byte_in_line(coords.y + scroll_start) + coords.x;
cursor_nibble = 0;
}
}
void handle_key_home()
{
cursor_byte = first_byte_in_line(byte_in_line(cursor_byte));
cursor_nibble = 0;
}
void handle_key_end()
{
cursor_byte = last_byte_in_line(byte_in_line(cursor_byte));
cursor_nibble = 1;
}
// Calculate the milliseconds elapsed between start and end
unsigned ms_taken(struct timespec start, struct timespec end)
{
return ((end.tv_sec - start.tv_sec) * 1000) +
((end.tv_nsec - start.tv_nsec) / 1000000);
}
bool handle_escape_sequence(int event)
{
#define FULL_SEQUENCE_LEN 4
static char sequence[FULL_SEQUENCE_LEN];
static int sequence_len = 0;
static struct timespec start;
static struct timespec end;
// Sequence must start with an escape key. If sequence is unstarted then
// the only valid key is the escape key.
if (!sequence_len && event != KEY_ESC)
{
return false;
}
// Start the timer on the first key of the sequence.
if (!sequence_len)
{
clock_gettime(CLOCK_REALTIME, &start);
}
// Check the timer on subsequent keys of the sequence.
else
{
clock_gettime(CLOCK_REALTIME, &end);
// Reset the sequence and don't consume the keypress if the sequence
// has taken too long to complete.
if (ms_taken(start, end) > ESCAPE_SEQUENCE_MAX_TIME_MS)
{
sequence_len = 0;
return false;
}
}
sequence[sequence_len++] = event;
// Sequence is not yet complete
if (sequence_len != FULL_SEQUENCE_LEN)
{
return true;
}
// Handle sequence and reset
switch (sequence[2])
{
case 49:
handle_key_home();
break;
case 52:
handle_key_end();
break;
}
sequence_len = 0;
return true;
}
void handle_event(int event)
{
if (command_entering)
{
handle_command_event(event);
return;
}
if (handle_g_chord(event))
{
return;
}
if (handle_escape_sequence(event))
{
return;
}
switch (event)
{
case 'h':
case 'H':
case KEY_LEFT:
handle_key_left();
break;
case 'l':
case 'L':
case KEY_RIGHT:
handle_key_right();
break;
case 'k':
case 'K':
case KEY_UP:
handle_key_up();
break;
case 'j':
case 'J':
case KEY_DOWN:
handle_key_down();
break;
case 'q':
case 'Q':
handle_previous_byte();
break;
case 'w':
case 'W':
handle_next_byte();
break;
case 'G':
handle_end_of_buffer();
break;
case KEY_PPAGE:
handle_page_up();
break;
case KEY_NPAGE:
handle_page_down();
break;
case ':':
case '/':
handle_start_command(event);
break;
case 'n':
handle_search_next();
break;
case 'N':
handle_search_previous();
break;
default:
handle_overwrite(event);
break;
}
MEVENT mouse_event;
if (getmouse(&mouse_event) == OK)
{
if (mouse_event.bstate & BUTTON1_PRESSED)
{
handle_mouse_pressed(mouse_event);
return;
}
}
}
void clamp_scrolling()
{
// Clamp to start of buffer
if (cursor_byte < 0)
{
cursor_byte = 0;
cursor_nibble = 0;
}
// Clamp to end of buffer
if (cursor_byte >= source_len)
{
cursor_byte = source_len - 1;
cursor_nibble = 1;
}
// Scroll up if cursor has left viewport
while (cursor_byte < first_visible_byte())
{
scroll_start--;
}
// Scroll down if cursor has left viewport
while (cursor_byte > last_visible_byte())
{
scroll_start++;
}
// Clamp scroll start to beginning of buffer
if (scroll_start < 0)
{
scroll_start = 0;
}
}
void render_hex()
{
wclear(panes[PANE_HEX].window);
char hex[2];
for (int i = first_visible_byte(); i <= last_visible_byte(); i++)
{
byte_to_hex(source[i], hex);
int out_y = byte_in_line(i) - scroll_start;
int out_x = byte_in_column(i);
mvwprintw(panes[PANE_HEX].window, out_y, out_x, "%c%c ", hex[0],
hex[1]);
}
}
void render_ascii()
{
wclear(panes[PANE_ASCII].window);
for (int i = first_visible_byte(); i <= last_visible_byte(); i++)
{
int out_y = byte_in_line(i) - scroll_start;
int out_x = i % bytes_per_line();
char output = '.';
if (source[i] >= ' ' && source[i] <= '~')
{
output = source[i];
}
if (i == cursor_byte)
{
wattron(panes[PANE_ASCII].window, COLOR_PAIR(STYLE_CURSOR));
}
mvwprintw(panes[PANE_ASCII].window, out_y, out_x, "%c", output);
if (i == cursor_byte)
{
wattroff(panes[PANE_ASCII].window, COLOR_PAIR(STYLE_CURSOR));
}
}
}
// Turn something like "1234" to "1,234". str must have enough space to add
// the commas (plus one for null termination): (len - 1) / 3 + 1
void add_commas(char* str, int len)
{
// Skip dash if negative integer
if (str[0] == '-')
{
str++;
len--;
}
int commas = (len - 1) / 3;