-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinny.c
1000 lines (909 loc) · 27.6 KB
/
binny.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
/*NOTE: This could be curses.h or ncurses.h. Depends on the distro. */
#include <ncurses.h>
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS /*stops VC++ from complaining about insecure somthingwhatnot*/
#include "pgetopt.h"
#define opterr popterr
#define getopt pgetopt
#define optarg poptarg
#define optind poptind
#else
#include <getopt.h>
#endif
#define PROG_NAME "binny"
#define VERSION "1.2"
#define NEW_FILE_BUFFER_SIZE 0x10
#define BYTES_PER_LINE_DEFAULT 0x10
#define BYTES_PER_GROUP_DEFAULT 4
#define RIGHT_OFFSET 13
#define ASCII_OFFSET 0
#define SEPARATOR '|'
#define BUFFER_LENGTH 255
#define POPUP_WIDTH 36
#define POPUP_HEIGHT 3
#define MODE_BINARY 0
#define MODE_ASCII 1
FILE * fp;
char filename[BUFFER_LENGTH];
unsigned char* buffer;
char userInput[BUFFER_LENGTH];
char userOutput[BUFFER_LENGTH];
unsigned long bufferLength;
WINDOW * borderWin;
WINDOW * editorWin;
WINDOW * userWin;
WINDOW * popupWin;
int bufferModified = 0;
int curBufPos = 0;
int curBufPosHalf = 0;
int topLineOfScreen = 0; /* used to track how far down it's scrolled*/
int bytesPerLine = BYTES_PER_LINE_DEFAULT;
int bytesPerGroup = BYTES_PER_GROUP_DEFAULT;
int showASCII = 0;
int mode = MODE_BINARY;
/*FUNCTION PROTOTYPES*/
void printHelp();
int parseOptions(int argc, char ** argv);
int resizeBuffer(int newSize);
void attemptCleanExit(int status);
void resizeSignalHandler(int signum);
void sigintHandler(int signum);
int setupScreen();
void drawASCII(int n);
void drawEditorWin();
void drawBorderWin();
void drawUserWin();
void moveEditorCursorUp();
void moveEditorCursorDown();
void moveEditorCursorLeft();
void moveEditorCursorRight();
void moveCursorToScreenPos();
void handleInput(int c);
void handleScrolling();
int leastOf(int x, int y);
void inputPopup();
int saveBuffer();
int main(int argc, char** argv)
{
int ch = 0;
if (parseOptions(argc, argv))
{
return EXIT_FAILURE;
}
/*===FILE IO OPERATIONS=== */
/* Check to see if the file given is a directory. */
fp = fopen(filename, "r");
if (fp != NULL)
{
/*File doesn't exist, create it*/
/*fp = fopen(filename, "w+");
fp = */
/*printf("%s: Couldn't open %s.\n", PROG_NAME, filename);*/
/*return EXIT_FAILURE;*/
/* after this point we can attempt to exit cleanly.*/
if (fseek(fp, 0, SEEK_END) != 0)
{
printf("%s: Couldn't read %s.\n", PROG_NAME, filename);
attemptCleanExit(EXIT_FAILURE);
}
/*get the filesize*/
bufferLength = (unsigned long) ftell(fp);
if (bufferLength < 0)
{
printf("%s: Couldn't get size of %s.\n", PROG_NAME, filename);
attemptCleanExit(EXIT_FAILURE);
}
fseek(fp, 0, SEEK_SET);
buffer = malloc(sizeof(char) * (bufferLength));
fread(buffer, sizeof(char), bufferLength, fp);
}
else
{
bufferLength = NEW_FILE_BUFFER_SIZE;
buffer = malloc(sizeof(char) * (bufferLength));
memset(buffer, 0, bufferLength);
bufferModified = 1;
}
/*SIGNALS HANDLING*/
signal(SIGINT, sigintHandler);
#ifdef SIGWINCH
signal(SIGWINCH, resizeSignalHandler);/*when the terminal is resized*/
#endif
if (setupScreen())
{
attemptCleanExit(EXIT_FAILURE);
}
/*begin main loop*/
while (1)
{
ch = getch();
handleInput(ch);
drawUserWin();
drawEditorWin();
}
/*We should never get here*/
return EXIT_FAILURE;
}
void printHelp()
{
printf("%s v%s\n", PROG_NAME, VERSION);
printf("A simple in-place binary editor.\n");
printf("Usage:\n\t%s [OPTIONS] FILENAME\n", PROG_NAME);
printf("Options:\n\t-h\t\tPrint Help\n\t-a\t\tShow ASCII\n\t-l bytes\tSet bytes displayed per line, default 0x10\n\t-g bytes\tSet byte grouping, default 4\n");
printf("Commands:\nAll commands are issued with shift-<command key>.\n");
printf("\tQ\t\tquit - Exit the program\n");
printf("\tS\t\tsave - Save the buffer to the file\n");
printf("\tG\t\tgoto - Jump to a position in the buffer\n");
printf("\tR\t\tresize - Resize the current buffer\n");
printf("\tA\t\tascii_insert - Insert a string of ascii\n");
printf("\tB\t\tbatch_insert - Insert a value repeatedly\n");
}
int parseOptions(int argc, char** argv)
{
int ch;
/*===OPTIONS PARSING===*/
opterr = 0;
while ((ch = getopt(argc, argv, "hal:g:")) != -1)
{
if (ch == 'h')
{
printHelp();
exit(EXIT_SUCCESS); }
else if (ch == 'l')
{
if (strtol(optarg, NULL, 0) == 0 || strtol(optarg, NULL, 0) < 1)
{
printf("%s: Bad argument '%s' in option '%c'. Use '%s -h' for Help.\n", PROG_NAME, optarg, ch, PROG_NAME);
return EXIT_FAILURE;
}
bytesPerLine = strtol(optarg, NULL, 0);
}
else if (ch == 'a')
{
showASCII = 1;
}
else if (ch == 'g')
{
if (strtol(optarg, NULL, 0) == 0 || strtol(optarg, NULL, 0) < 1)
{
printf("%s: Bad argument '%s' in option '%c'. Use '%s -h' for Help.\n", PROG_NAME, optarg, ch, PROG_NAME);
return -1;
}
bytesPerGroup = strtol(optarg, NULL, 0);
}
else
{
printf("%s: Invalid option. Use '%s -h' for Help.\n", PROG_NAME, PROG_NAME);
return -1;
}
}
if (optind >= argc)
{
printf("%s: No filename specified. Use '%s -h' for Help.\n", PROG_NAME, PROG_NAME);
return -1;
}
/*More Error checking on user prefs*/
if (bytesPerLine < bytesPerGroup)
{
bytesPerGroup = bytesPerLine;
}
sprintf(filename, "%s", argv[optind]);
return 0;
}
//Will resize the buffer and copy all of buffer (that it can) into the new one. Returns the new size or -1 on Error*/
int resizeBuffer(int newSize)
{
if (newSize <= 0)
{
sprintf(userOutput, "Error: New buffer size must be greater than 0.");
return -1;
}
unsigned char * newBuf = malloc(sizeof(char) * (newSize));
if (newBuf == NULL) return -1;
memset(newBuf, 0, newSize); /*zero the buffer just in case*/
memcpy(newBuf, buffer, leastOf(newSize, bufferLength));
free(buffer);
/*basically reset everything */
buffer = newBuf;
bufferLength = newSize;
curBufPos = 0;
topLineOfScreen = 0;
return 0;
}
/* called when the terminal resize, then redraws everything.*/
void resizeSignalHandler(int signum)
{
endwin();
setupScreen();
}
/*Handles CTRL-C AKA SIGINT */
void sigintHandler(int signum)
{
attemptCleanExit(EXIT_SUCCESS);
}
/*Does basic screen setup, with error checking*/
int setupScreen()
{
/*===NCURSES OPERATIONS===*/
/*make sure we can init ncurses properly*/
int rows, cols;
borderWin = initscr();
if (borderWin == NULL)
{
printf("Error: Couldn't initialize main screen.\n");
return -1;
}
/*raw();*/
cbreak();
keypad(stdscr, TRUE);
noecho(); /*Turns off character echoing to the screen*/
#ifdef _WIN32
curs_set(2);
#endif
getmaxyx(borderWin, rows, cols);
editorWin = newwin(rows - 4, cols - 2, 1, 1);
userWin = newwin(2, cols - 2, rows - 3, 1);
/*first Draw*/
drawBorderWin();
drawUserWin();
drawEditorWin();
return 0;
}
void attemptCleanExit(int status)
{
delwin(editorWin);
endwin();
if (fp != NULL) {
fclose(fp);
fp = NULL;
}
free(buffer);
exit(status);
}
void moveEditorCursorUp()
{
curBufPos -= bytesPerLine;
curBufPosHalf = 0;
if (curBufPos < 0) curBufPos = 0;
}
void moveEditorCursorDown()
{
curBufPos += bytesPerLine;
curBufPosHalf = 0;
if (curBufPos >= bufferLength) curBufPos = bufferLength - 1;
}
void moveEditorCursorLeft()
{
curBufPosHalf = 0;
curBufPos -= 1;
if (curBufPos < 0) curBufPos = 0;
}
void moveEditorCursorRight()
{
curBufPosHalf = 0;
curBufPos += 1;
if (curBufPos >= bufferLength) curBufPos = bufferLength - 1;
}
void moveCursorToScreenPos()
{
int row = (curBufPos - (curBufPos % bytesPerLine)) / bytesPerLine;
int x = (curBufPos - (bytesPerLine * row));
int col = x * 2 + (x / bytesPerGroup) + curBufPosHalf + RIGHT_OFFSET;
row = row - topLineOfScreen;
#ifdef _WIN32
move(row+1, col+1);
#else
wmove(editorWin, row, col);
#endif
}
void handleInput(int c)
{
if (mode == MODE_ASCII)
{
if (c == KEY_END)
{
mode = MODE_BINARY;
sprintf(userOutput, "ASCII mode disabled.");
}
else if (c == KEY_BACKSPACE)
{
moveEditorCursorLeft();
buffer[curBufPos] = 0;
bufferModified = 1;
}
else if (c == KEY_RIGHT)
{
moveEditorCursorRight();
}
else if (c == KEY_LEFT)
{
moveEditorCursorLeft();
}
else if (c == KEY_UP)
{
moveEditorCursorUp();
}
else if (c == KEY_DOWN)
{
moveEditorCursorDown();
}
else
{
buffer[curBufPos] = c;
moveEditorCursorRight();
bufferModified = 1;
}
}
else if (mode == MODE_BINARY)
{
if (c == 'R')
{
sprintf(userOutput, "Resize Buffer to:");
inputPopup(userOutput);
if (strtol(userInput, NULL, 0) <= 0)
{
sprintf(userOutput, "Error: invalid number");
return;
}
resizeBuffer(strtol(userInput, NULL, 0));
sprintf(userOutput, "Buffer resized to 0x%lX / %ld", bufferLength, bufferLength);
bufferModified = 1;
}
else if (c == 'G')
{
sprintf(userOutput, "Goto:");
inputPopup(userOutput);
if (strtol(userInput, NULL, 0) < 0)
{
sprintf(userOutput, "Error: invalid number");
return;
}
curBufPos = leastOf(strtol(userInput, NULL, 0), bufferLength - 1);
sprintf(userOutput, "Moved cursor");
}
else if (c == 'A')
{
mode = MODE_ASCII;
sprintf(userOutput, "ASCII mode enabled. Press END to disable.");
/* OLD ASCII INSERT CODE
sprintf(userOutput, "ASCII Insert:");
inputPopup(userOutput);
if (strlen(userInput) == 0) {
sprintf(userOutput, "Error: empty string");
return;
}
strncpy(&buffer[curBufPos], userInput, leastOf(strlen(userInput),
bufferLength - curBufPos));
sprintf(userOutput, "String inserted");
bufferModified = 1;
*/
}
else if (c == 'B')
{
/*Batch insert*/
char charToInsert;
int numberToInsert;
sprintf(userOutput, "Batch Insert Character Value:");
inputPopup(userOutput);
if (strlen(userInput) == 0)
{
sprintf(userOutput, "Error: Empty string.");
return;
}
else if (strtol(userInput, NULL, 0) < 0 || strtol(userInput, NULL, 0) > 255)
{
sprintf(userOutput, "Error: Bad character value.");
return;
}
charToInsert = strtol(userInput, NULL, 0);
sprintf(userOutput, "Number to Insert:");
inputPopup(userOutput);
if (strlen(userInput) == 0)
{
sprintf(userOutput, "Error: Empty string.");
return;
}
else if (strtol(userInput, NULL, 0) < 0)
{
sprintf(userOutput, "Error: Bad value.");
return;
}
numberToInsert = strtol(userInput, NULL, 0);
memset(&buffer[curBufPos], charToInsert, leastOf(numberToInsert, bufferLength - curBufPos));
sprintf(userOutput, "Character 0x%02x inserted", charToInsert);
bufferModified = 1;
}
else if (c == 'S')
{
saveBuffer();
}
else if (c == 'Q')
{
if (bufferModified)
{
sprintf(userOutput, "Buffer is modified. Save? [Y/n]");
inputPopup(userOutput);
if (userInput[0] != 'n' && userInput[0] != 'N')
{
int ret = saveBuffer();
if (ret)
{
return;
/* basically, do not exit, there was a problem. save buffer will output its own status*/
}
}
}
attemptCleanExit(EXIT_SUCCESS);
}
else if (c == KEY_RIGHT)
{
moveEditorCursorRight();
}
else if (c == KEY_LEFT)
{
moveEditorCursorLeft();
}
else if (c == KEY_UP)
{
moveEditorCursorUp();
}
else if (c == KEY_DOWN)
{
moveEditorCursorDown();
}
else if (c == '0')
{
if (curBufPosHalf == 0)
{
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] = buffer[curBufPos] >> 4;
curBufPosHalf = 1;
}
else
{
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] = buffer[curBufPos] << 4;
moveEditorCursorRight();
}
bufferModified = 1;
}
else if (c == '1')
{
if (curBufPosHalf == 0)
{
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] |= 0x10;
curBufPosHalf = 1;
}
else
{
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] |= 0x01;
moveEditorCursorRight();
}
bufferModified = 1;
}
else if (c == '2')
{
if (curBufPosHalf == 0)
{
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] |= 0x20;
curBufPosHalf = 1;
}
else
{
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] |= 0x02;
moveEditorCursorRight();
}
bufferModified = 1;
}
else if (c == '3')
{
if (curBufPosHalf == 0)
{
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] |= 0x30;
curBufPosHalf = 1;
}
else
{
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] |= 0x03;
moveEditorCursorRight();
}
bufferModified = 1;
}
else if (c == '4')
{
if (curBufPosHalf == 0)
{
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] |= 0x40;
curBufPosHalf = 1;
}
else
{
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] |= 0x04;
moveEditorCursorRight();
}
bufferModified = 1;
}
else if (c == '5')
{
if (curBufPosHalf == 0)
{
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] |= 0x50;
curBufPosHalf = 1;
}
else
{
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] |= 0x05;
moveEditorCursorRight();
}
bufferModified = 1;
}
else if (c == '6')
{
if (curBufPosHalf == 0)
{
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] |= 0x60;
curBufPosHalf = 1;
}
else
{
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] |= 0x06;
moveEditorCursorRight();
}
bufferModified = 1;
}
else if (c == '7')
{
if (curBufPosHalf == 0)
{
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] |= 0x70;
curBufPosHalf = 1;
}
else
{
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] |= 0x07;
moveEditorCursorRight();
}
bufferModified = 1;
}
else if (c == '8')
{
if (curBufPosHalf == 0)
{
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] |= 0x80;
curBufPosHalf = 1;
}
else
{
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] |= 0x08;
moveEditorCursorRight();
}
bufferModified = 1;
}
else if (c == '9')
{
if (curBufPosHalf == 0)
{
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] |= 0x90;
curBufPosHalf = 1;
}
else
{
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] |= 0x09;
moveEditorCursorRight();
}
bufferModified = 1;
}
else if (c == 'a')
{
if (curBufPosHalf == 0)
{
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] |= 0xa0;
curBufPosHalf = 1;
}
else
{
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] |= 0x0a;
moveEditorCursorRight();
}
bufferModified = 1;
}
else if (c == 'b')
{
if (curBufPosHalf == 0)
{
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] |= 0xb0;
curBufPosHalf = 1;
}
else
{
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] |= 0x0b;
moveEditorCursorRight();
}
bufferModified = 1;
}
else if (c == 'c')
{
if (curBufPosHalf == 0)
{
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] |= 0xc0;
curBufPosHalf = 1;
}
else
{
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] |= 0x0c;
moveEditorCursorRight();
}
bufferModified = 1;
}
else if (c == 'd')
{
if (curBufPosHalf == 0)
{
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] |= 0xd0;
curBufPosHalf = 1;
}
else
{
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] |= 0x0d;
moveEditorCursorRight();
}
bufferModified = 1;
}
else if (c == 'e')
{
if (curBufPosHalf == 0)
{
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] |= 0xe0;
curBufPosHalf = 1;
}
else
{
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] |= 0x0e;
moveEditorCursorRight();
}
bufferModified = 1;
}
else if (c == 'f')
{
if (curBufPosHalf == 0)
{
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] |= 0xf0;
curBufPosHalf = 1;
}
else
{
buffer[curBufPos] = buffer[curBufPos] >> 4;
buffer[curBufPos] = buffer[curBufPos] << 4;
buffer[curBufPos] |= 0x0f;
moveEditorCursorRight();
}
bufferModified = 1;
}
}
}
void drawASCII(int line)
{
int displayRow = line - topLineOfScreen;
int displayCol = (bytesPerLine * 2) + (bytesPerLine / bytesPerGroup) + RIGHT_OFFSET + ASCII_OFFSET;
int i;
if (!showASCII) return;
wmove(editorWin, displayRow, displayCol);
wprintw(editorWin, "%c ", SEPARATOR);
for (i = line * bytesPerLine; i < (line + 1) * bytesPerLine; i++)
{
if (i >= bufferLength)
{
break;
}
if (i == curBufPos) wattron(editorWin, A_REVERSE);
/*check to see if its a printable ASCII char*/
if (buffer[i] >= 0x20 && buffer[i] <= 0x7E)
{
wprintw(editorWin, "%c", buffer[i]);
}
else
{
wprintw(editorWin, ".");
}
if (i == curBufPos) wattroff(editorWin, A_REVERSE);
}
}
/* draw the screen, which is updated with every change. Does not include the border*/
void drawEditorWin()
{
int n, rows, startByte, endByte;
handleScrolling();
werase(editorWin);/*difference between clear and erase is that clear calls refresh directly after. With lots of keypresses you get flicker. Hence, erase. */
wmove(editorWin, 0, 0);
getmaxyx(editorWin, rows, n);
startByte = topLineOfScreen * bytesPerLine; /* first byte on the top displayed line*/
endByte = leastOf(bufferLength, startByte + (rows * bytesPerLine)); /* last byte to be displayed. Basically startByte + total Number of bytes that can be displayed*/
for (n = startByte; n < endByte; n++)
{
if (n % bytesPerLine == 0)
{
/* print the line header */
if (n != startByte) wprintw(editorWin, "\n");
wattron(editorWin, A_BOLD);
wprintw(editorWin, "0x%08X", n);
wattroff(editorWin, A_BOLD);
wprintw(editorWin, " %c ", SEPARATOR);
}
else if (n % bytesPerGroup == 0)
{
/*add a space between groups of bytes*/
wprintw(editorWin, " ");
}
wprintw(editorWin, "%02x", buffer[n]);
/*if it's the end of a line, or if it's the last char, then show the ASCII representation*/
if ((n + 1) % bytesPerLine == 0 || n == endByte - 1)
{
drawASCII((n - (n % bytesPerLine)) / bytesPerLine);
}
}
moveCursorToScreenPos();
wrefresh(editorWin);
}
void drawUserWin()
{
werase(userWin);
wmove(userWin, 0, 0);
wattron(userWin, A_REVERSE);
wborder(userWin, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
wprintw(userWin, "Position: 0x%X / %d of 0x%X / %d bytes", curBufPos, curBufPos, bufferLength, bufferLength);
wmove(userWin, 1, 0);
wprintw(userWin, "Status : %s", userOutput);
wattroff(userWin, A_REVERSE);
wrefresh(userWin);
}
/* the border, which does not update and will only be drawn once*/
void drawBorderWin()
{
char temp[255];
int y, x;
getmaxyx(borderWin, y, x);
erase();
attron(A_REVERSE);
/*draw borders*/
wborder(borderWin, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
wmove(borderWin, 0, 3);
sprintf(temp, "%s v%s - %s", PROG_NAME, VERSION, filename);
wprintw(borderWin, temp);
move(y - 1, 1);
y = x;/*this is literally only so the warning about not using x will stop popping up*/
printw("Commands: 'Q'uit 'S'ave 'G'oto 'R'esize 'A'scii_mode 'B'atch_insert");
attroff(A_REVERSE);
refresh();
}
/*returns the lesser of two numbers*/
int leastOf(int x, int y)
{
if (x > y) return y;
return x;
}
void handleScrolling()
{
int done = 0;
int rows, startByte, endByte;
/*Scroll until we get to see the cursor again. Used for Gotos*/
while (!done)
{
startByte = topLineOfScreen * bytesPerLine; /* first byte on the top displayed line*/
getmaxyx(editorWin, rows, endByte);
endByte = leastOf(bufferLength, startByte + (rows * bytesPerLine)); /* last byte to be displayed. Basically startByte + total Number of bytes that can be displayed*/
if (curBufPos < startByte)
{
topLineOfScreen--;
}
else if (curBufPos >= endByte)
{
topLineOfScreen++;
}
else
{
done = 1;
}
}
}
void inputPopup(char * title)
{
int totalRows, totalCols;
getmaxyx(borderWin, totalRows, totalCols);
/* center the new popup window*/
popupWin = newwin(POPUP_HEIGHT, POPUP_WIDTH, (totalRows / 2) - (POPUP_HEIGHT / 2), (totalCols / 2) - (POPUP_WIDTH / 2));
wattron(popupWin, A_REVERSE);
wborder(popupWin, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
/*print the title*/
mvwaddnstr(popupWin, 0, 1, title, POPUP_WIDTH - 2);
wattroff(popupWin, A_REVERSE);
/*move the cursor into position*/
move((totalRows / 2) - (POPUP_HEIGHT / 2) + 1, (totalCols / 2) - (POPUP_WIDTH / 2) + 1);
wrefresh(popupWin);
echo();
getnstr(userInput, POPUP_WIDTH - 2);
noecho();
}
int saveBuffer()
{
if (fp != NULL){
//sprintf(userOutput, "Error: File still open.");
//return -1;
fclose(fp);
fp = NULL;
}
fp = fopen(filename, "w+");
if (fp == NULL)
{
sprintf(userOutput, "Error: Couldn't save to %s", filename);
return -1;
}
fseek(fp, 0, SEEK_SET); /* go to beginning of file*/
fwrite(buffer, sizeof(unsigned char), bufferLength, fp);
fclose(fp);
fp = NULL;
bufferModified = 0;
sprintf(userOutput, "Buffer saved to %s", filename);
return 0;
}