-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvbl.cpp
More file actions
3961 lines (3030 loc) · 121 KB
/
vbl.cpp
File metadata and controls
3961 lines (3030 loc) · 121 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
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
//--------------------------------------------------------------------
//
// VBinDiff for Linux
//
// Hex viewer, differ, dumper and editor
//
// Copyright 2021-2025 by linuxCowboy
//
// vbindiff by Christopher J. Madsen
// 64GB by Bradley Grainger
// dynamic width by Christophe Bucher
//
// Version:
// 1.x classic vbindiff interface, fix 32 byte
// -----------------------------------------------
// 2.0 dynamic 16/24/32 byte width
// 2.1 256 terabyte files
// 2.2 kick panels
// 2.3 ascii mode
// 2.4 speedup differ
// 2.5 full help
// 2.6 cursor color
// 2.7 use deque
// 2.8 kick map
// 2.9 kick iostream
// 2.10 kick sstream
// 2.11 kick algorithm
// 2.12 ignore case
// 2.13 relative jumps
// 2.14 goto back
// 2.15 repeat offset
// 2.16 seekNotChar ascii
// ---- meanwhile almost completely rewritten ----
// 3.0 edit insert/delete
// 3.1 InputManager
// 3.2 progress bar
// 3.3 goto prefix
// 3.4 edit diff
// 3.5 set last
// 3.6 golf search
// 3.6.1 turbo zero
// 3.6.2 SIMD case
// 3.7 start addr
// ------------------
// 4.0 SSE2 SIMD
// 4.1 search all
// 4.2 jump addr
// 4.3 dump mode
// 4.4 diff mode
//
// This program 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 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// For the GNU General Public License see <https://www.gnu.org/licenses/>.
//--------------------------------------------------------------------------
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <err.h>
#include <x86gprintrin.h>
#include <emmintrin.h>
#include <ncurses.h>
#include <string>
#include <deque>
using namespace std;
#define VBL_VERSION "4.4"
// ###################
// ##### options #####
// ###################
/* set Cursor Color in input window with Operating System Command */
#ifndef SET_CURSOR_COLOR
#define SET_CURSOR_COLOR 0
#endif
/* show a summary in edit insert/delete after large writes + wait */
#ifndef SHOW_WRITE_SUMMARY
#define SHOW_WRITE_SUMMARY 0
#endif
/* thousands separator (or '\0') */
#define THOU_SEP '.'
// ###################
/* const: gcc optimizes macros away */
const bool debug = 0; // ##:q
/* curses debug:
f=/tmp/.vbl
tail -F $f
vbl file 2>$f; cat $f
*/
#define mPI(x) if (debug) fprintf(stderr, "\r%s: 0x%lX %ld\n", #x, (long) x, (long) x);
#define mPU(x) if (debug) fprintf(stderr, "\r%s: 0x%lX %lu\n", #x, (Full) x, (Full) x);
#define mPF(x) if (debug) fprintf(stderr, "\r%s: %f\n", #x, (float) x);
#define mPS(x) if (debug) fprintf(stderr, "\r%s: %s\n", #x, x);
#define mPsec(x) if (debug) fprintf(stderr, "\r%s: %ld sec\n", #x, (long) x);
#define mPms(x) if (debug) fprintf(stderr, "\r%s: %.3f msec\n", #x, (float) x / 1000000);
/* profiling: sec, msec; init: {1,} */
#define mTs(x,init...) if (debug) {if (1 != 1##init) {x = time(NULL);}\
else {x = time(NULL) - x; mPsec(x)}}
#define mTms(x,init...) if (debug) {if (1 != 1##init) {x = timer();}\
else {x = timer(2,x); mPms(x)}} // one-shot
#define mZ(x) if (debug) {x = 0;} // reset
#define mTpp(x,init...) if (debug) {if (1 != 1##init) timer(1); else {x += timer(2);}} // sum-up (t0)
/* w/o redirection */
#define mPP(x) if (debug) sleep(x);
#define mPK if (debug) file1.readKeyF();
/* hex dump: pointer, count */
#define mPX(x, c) if (debug) {fprintf(stderr, "\r%s, %d \n\r", #x, (int) c);\
for (Word I=0; I < c; ++I) fprintf(stderr, "%X ", (Byte) (x)[I]);\
fprintf(stderr, "\n");}
#define mCeil(x, y) x / y + (x % y ? 1 : 0)
#define mEdit historyPos = history.size();
#define mScale count / (scale ? scale : 1)
#define KEY_CTRL_C 0x03
#define KEY_TAB 0x09
#define KEY_CTRL_K 0x0B
#define KEY_RETURN 0x0D
#define KEY_CTRL_U 0x15
#define KEY_ESCAPE 0x1B
#define KEY_DELETE 0x7F
//====================================================================
// Color Enumerations
enum ColorPair {
pairWhiteBlue = 1,
pairBlackWhite,
pairRedWhite,
pairYellowBlue,
pairGreenBlue,
pairBlackCyan,
pairGreenBlack,
pairWhiteCyan,
pairWhiteRed,
pairWhiteGreen,
pairBlackYellow
};
enum Style {
cMainWin,
cInputWin,
cHelpWin,
cName,
cDiff,
cEdit,
cInsert,
cSearch,
cSeek,
cMatch,
cRaster,
cAddress,
cHotkey,
cHighFile,
cHighBusy,
cHighBus2,
cHighEdit
};
static const ColorPair colorStyle[] = {
pairWhiteBlue, // cMainWin
pairWhiteBlue, // cInputWin
pairWhiteBlue, // cHelpWin
pairBlackWhite, // cName
pairGreenBlack, // cDiff
pairYellowBlue, // cEdit
pairGreenBlue, // cInsert
pairWhiteRed, // cSearch
pairWhiteGreen, // cSeek
pairRedWhite, // cMatch
pairBlackCyan, // cRaster
pairYellowBlue, // cAddress
pairGreenBlue, // cHotkey
pairWhiteCyan, // cHighFile
pairWhiteRed, // cHighBusy
pairWhiteGreen, // cHighBus2
pairBlackYellow // cHighEdit
};
static const attr_t attribStyle[] = {
COLOR_PAIR(colorStyle[ cMainWin ]),
COLOR_PAIR(colorStyle[ cInputWin ]),
COLOR_PAIR(colorStyle[ cHelpWin ]),
COLOR_PAIR(colorStyle[ cName ]),
A_BOLD | COLOR_PAIR(colorStyle[ cDiff ]),
A_BOLD | COLOR_PAIR(colorStyle[ cEdit ]),
A_BOLD | COLOR_PAIR(colorStyle[ cInsert ]),
A_BOLD | COLOR_PAIR(colorStyle[ cSearch ]),
A_BOLD | COLOR_PAIR(colorStyle[ cSeek ]),
COLOR_PAIR(colorStyle[ cMatch ]),
COLOR_PAIR(colorStyle[ cRaster ]),
A_BOLD | COLOR_PAIR(colorStyle[ cAddress ]),
A_BOLD | COLOR_PAIR(colorStyle[ cHotkey ]),
A_BOLD | COLOR_PAIR(colorStyle[ cHighFile ]),
A_BOLD | COLOR_PAIR(colorStyle[ cHighBusy ]),
A_BOLD | COLOR_PAIR(colorStyle[ cHighBus2 ]),
COLOR_PAIR(colorStyle[ cHighEdit ])
};
//====================================================================
// Type definitions
typedef unsigned char Byte;
typedef unsigned short Word;
typedef unsigned int Half;
typedef unsigned long Full;
typedef __m128i Quad;
typedef Byte Command;
typedef int File;
typedef off_t FPos; // long int
typedef ssize_t Size; // long int
typedef deque<string> StrDeq;
typedef deque<Byte> BytDeq;
enum LockState { lockNeither, lockTop, lockBottom };
//====================================================================
// Constants ##:cmd
const Command cmgGoto = 0x80; // Main cmd
const Command cmgGotoTop = 0x08; // Flag
const Command cmgGotoBottom = 0x04; // Flag
const Command cmgGotoForw = 0x40;
const Command cmgGotoBack = 0x20;
const Command cmgGotoMask = 0x13; // Mask
const Command cmgGotoLGet = 0x01;
const Command cmgGotoLSet = 0x02;
const Command cmgGotoLOff = 0x03;
const Command cmgGotoNOff = 0x11;
const Command cmgGotoJGet = 0x12;
const Command cmgGotoJSet = 0x13;
const Command cmfFind = 0x40; // Main cmd
const Command cmfFindNext = 0x20;
const Command cmfFindPrev = 0x10;
const Command cmfNotCharDn = 0x02;
const Command cmfNotCharUp = 0x01;
const Command cmmMove = 0x20; // Main cmd
const Command cmmMoveForward = 0x10;
const Command cmmMoveMask = 0x03; // Mask
const Command cmmMoveByte = 0x00; // Move 1 byte
const Command cmmMoveLine = 0x01; // Move 1 line
const Command cmmMovePage = 0x02; // Move 1 page
const Command cmmMoveAll = 0x03; // Move to begin or end
const Command cmNothing = 0;
const Command cmUseTop = 1;
const Command cmUseBottom = 2;
const Command cmNextDiff = 3;
const Command cmPrevDiff = 4;
const Command cmEditTop = 5;
const Command cmEditBottom = 6;
const Command cmSyncUp = 7;
const Command cmSyncDn = 8;
const Command cmShowAscii = 9;
const Command cmIgnoreCase = 10;
const Command cmShowRaster = 11;
const Command cmShowHelp = 12;
const Command cmSmartScroll = 13;
const Command cmQuit = 14;
//--------------------------------------------------------------------
const Size minScreenHeight = 24, // Enforced minimum height
minScreenWidth = 79, // Enforced minimum width
skipForw = 4, // Percent to skip forward
skipBack = 1, // Percent to skip backward
staticSize = 1L << 25, // size global buffers
warnResize = 1L << 29, // confirmation threshold
barDelay = 6, // msec, smoothness vs. speed
maxHistory = 20, // find and goto
dumpDef = 16, // terminal dump: both
dumpMax = 32; // full dump only
const char *hexDigits = "0123456789ABCDEF", // search
*hexDigitsGoto = "0123456789ABCDEFabcdef%Xx+-kmgtKMGTsS", // goto
*colorInsert = "#00BBBB", // cursor color "normal"
*colorDelete = "#EE0000"; // cursor color "very visible"
const wchar_t barSyms[] = {L'▏', L'▎', L'▍', L'▌', L'▋', L'▊', L'▉', L'█'};
const char sPrefix[] = "skmgtSKMGT";
const Size aPrefix[] = { 512, 1024, 1048576, 1073741824, 1099511627776,
4096, 1000, 1000000, 1000000000, 1000000000000 };
//--------------------------------------------------------------------
// Help screen text - max 21 lines (minScreenHeight - 3) ##:x
const char *aHelp[] = {
" ",
" Move: left right up down home end space backspace",
" ",
" Find Next Prev PgDn PgUp == next/prev diff byte",
" ",
" Goto [+-]{dec hex 0x x$}[%|sSkmgtKMGT] +4% + * = -1% -",
" Last addr: ' < Jump Addr: \" last off: . neg off: ,",
" ",
" Edit file show Raster Ignore case Quit",
" ",
" --- One File ---",
" Enter == sm4rtscroll Ascii mode",
" ",
" --- Two Files ---",
" Enter == next diff # \\ == prev diff 1 2 == sync views",
" use only Top, use only Bottom",
" ",
" --- Edit ---",
" Enter == copy byte from other file; Insert Ctrl-U",
" Tab == HEX <> ASCII, Esc == done; Delete Ctrl-K",
" "
};
const int longestLine = 57; // adjust!
const Byte aBold[] = { // hotkeys, start y:1, x:1
4,3, 4,10, 4,15,
6,3, 6,46, 6,48, 6,50, 6,57,
7,3, 7,14, 7,16, 7,20, 7,31, 7,45, 7,57,
9,3, 9,20, 9,29, 9,54,
12,26,
15,23, 15,25, 15,41, 15,43,
16,32, 16,47,
0
};
const char *helpVersion = " VBinDiff for Linux " VBL_VERSION " ";
const int helpWidth = 1 + longestLine + 2 + 1,
helpHeight = 1 + sizeof(aHelp) / sizeof(aHelp[0]) + 1;
//====================================================================
// Global Variables ##:vars
WINDOW *winInput,
*winHelp;
alignas(0x1000)
Byte bufFile1[staticSize],
bufFile2[staticSize];
Byte *buffer = bufFile1;
FPos *sm4rt;
char bufTimer[64];
bool singleFile,
showRaster,
sizeTera,
modeAscii,
ignoreCase,
stopRead,
useSSE2,
haveDiff;
LockState lockState;
string lastSearch,
lastSearchIgnCase;
StrDeq hexSearchHistory,
textSearchHistory,
positionHistory;
BytDeq editBytes,
editColor;
// Set dynamically for 16/24/32 byte width
Size screenWidth, // Number of columns in curses
linesTotal, // Number of lines in curses
numLines, // Number of lines of each file to display
bufSize, // Number of bytes of each file to display
lineWidth, // Number of bytes displayed per line
lineWidthAsc, // Number of bytes displayed per line ascii
inWidth, // Number of digits in input window
leftMar, // Starting column of hex display
leftMar2, // Starting column of ASCII display
searchIndent, // Lines of search result indentation
steps[4], // Number of bytes to move for each step
diffMode, // diff files to terminal
dumpMode, // dump file to terminal
dumpBeg,
dumpEnd,
dumpLen,
dumpWid;
// debug timer 1-9, init 0
__attribute__ ((unused)) static Size t1, t2, t3, t4, t5, t6, t7, t8, t9, t0;
char *program,
#if THOU_SEP_COMMA
thouSep = ',';
#else
thouSep = THOU_SEP;
#endif
//====================================================================
// Global Functions
//--------------------------------------------------------------------
// Global timer / profiling
// modes: 0-set 1-sum_up 2-nsec 3-msec
Size timer(int mode=0, Size var=t0)
{
timespec ts;
clock_gettime(CLOCK_TAI, &ts);
Size ret = ts.tv_sec * 1000000000 + ts.tv_nsec;
if (mode == 1) {
t0 = ret;
}
else if (mode == 2) {
ret -= var;
}
else if (mode == 3) {
ret = (ret - var) / 1000000;
}
return ret;
}
//--------------------------------------------------------------------
// FileIO
File OpenFile(const char* path, bool writable=false)
{
return open(path, (writable ? O_RDWR : O_RDONLY));
}
bool WriteFile(File file, const Byte* buf, Size cnt)
{
while (cnt > 0) {
Size bytesWritten = write(file, buf, cnt);
if (bytesWritten < 1) {
if (errno == EINTR)
bytesWritten = 0;
else
return false;
}
buf += bytesWritten;
cnt -= bytesWritten;
}
return true;
}
Size ReadFile(File file, Byte* buf, Size cnt)
{
Size ret = read(file, buf, cnt);
/* interrupt the searches */
timeout(0);
switch(getch()) {
case KEY_ESCAPE:
stopRead = true;
}
timeout(-1);
/* mitigate read errors */
if (ret < 0) {
ret = 0;
stopRead = true;
}
return ret;
}
FPos SeekFile(File file, FPos position, int whence=SEEK_SET)
{
return lseek(file, position, whence);
}
//--------------------------------------------------------------------
// Dumper (no curses)
void dumpFile(char* file)
{
File fd;
if ((fd = OpenFile(file)) < 0) {
err(2, file);
}
if (! dumpLen) {
if (! dumpEnd) {
dumpEnd = SeekFile(fd, 0, SEEK_END);
}
dumpLen = dumpEnd - dumpBeg;
}
if (SeekFile(fd, dumpBeg) < 0) {
err(3, "seek");
}
Size bytesRead, cnt, len;
if (dumpMode == 2) { // binary
Byte* pb = buffer;
for (; ; dumpLen -= cnt) {
if ((bytesRead = read(fd, pb, staticSize)) < 0) {
err(4, "read");
}
if (! (cnt = min(bytesRead, dumpLen))) {
break;
}
for (Size i=0; i < cnt; ++i) {
putchar(pb[i]);
}
}
}
else {
Size tera = dumpBeg + dumpLen >= 68719476736 ? 3 : 0;
if (dumpWid > dumpMax) {
dumpWid = dumpMax;
}
char addr[9];
sprintf(addr, "%%0%dlX ", tera ? 12 : 9);
Size lcnt = 9 + tera + 2 + (dumpWid - 1) / 8 + dumpWid * 3 + 1 + dumpWid;
char line[lcnt + 1] = { 0 };
for (; ; dumpLen -= cnt, dumpBeg += cnt) {
if ((bytesRead = read(fd, buffer, staticSize)) < 0) {
err(4, "read");
}
if ((cnt = min(bytesRead, dumpLen)) <= 0) {
printf(addr, dumpBeg);
putchar(10);
break;
}
for (Size i=0; i < cnt; i += len) {
if (i > cnt - dumpWid) {
memset(line, ' ', lcnt);
}
char *pbufHex = line;
pbufHex += sprintf(line, addr, dumpBeg + i);
len = min(cnt - i, dumpWid);
for (Size j=0; j < len; ++j) {
if (! (j % 8)) {
*pbufHex++ = ' ';
}
Byte b = buffer[i + j];
pbufHex += sprintf(pbufHex, "%02X ", b);
line[lcnt - dumpWid + j] = b > '~' || b < ' ' ? '.' : b;
}
*pbufHex = ' ';
puts(line);
}
}
}
close(fd);
exit(0);
} // end dumpFile
//--------------------------------------------------------------------
// Cmdline differ
FPos diffFile(char* file1, char* file2)
{
File fd1, fd2;
if ((fd1 = OpenFile(file1)) < 0) {
err(2, file1);
}
if ((fd2 = OpenFile(file2)) < 0) {
close(fd1);
err(2, file2);
}
FPos off = 0;
Size size, size1, size2;
do {
if ((size1 = read(fd1, bufFile1, staticSize)) < 0) {
err(4, file1);
}
if ((size2 = read(fd2, bufFile2, staticSize)) < 0) {
err(4, file2);
}
if (size1 == 0 && size2 == 0) {
off = -1;
break;
}
size = min(size1, size2);
if (size == 0) {
break;
}
if (memcmp(bufFile1, bufFile2, size)) {
break;
}
if (size < staticSize) {
if (size1 == size2) {
off = -1;
}
break;
}
off += staticSize;
} while (1);
close(fd1);
close(fd2);
return off;
} // end diffFile
//--------------------------------------------------------------------
// Initialize ncurses ##:i
bool initialize()
{
setlocale(LC_ALL, ""); // for Unicode blocks
if (! initscr()) {
return false;
}
set_escdelay(10);
keypad(stdscr, true);
nonl();
cbreak();
noecho();
if (has_colors()) {
start_color();
init_pair(pairWhiteBlue, COLOR_WHITE, COLOR_BLUE);
init_pair(pairBlackWhite, COLOR_BLACK, COLOR_WHITE);
init_pair(pairRedWhite, COLOR_RED, COLOR_WHITE);
init_pair(pairYellowBlue, COLOR_YELLOW, COLOR_BLUE);
init_pair(pairGreenBlue, COLOR_GREEN, COLOR_BLUE);
init_pair(pairBlackCyan, COLOR_BLACK, COLOR_CYAN);
init_pair(pairGreenBlack, COLOR_GREEN, COLOR_BLACK);
init_pair(pairWhiteCyan, COLOR_WHITE, COLOR_CYAN);
init_pair(pairWhiteRed, COLOR_WHITE, COLOR_RED);
init_pair(pairWhiteGreen, COLOR_WHITE, COLOR_GREEN);
init_pair(pairBlackYellow, COLOR_BLACK, COLOR_YELLOW);
}
curs_set(0);
return true;
} // end initialize
//--------------------------------------------------------------------
// Visible difference between insert and overstrike mode
void showCursor(bool over=false)
{
over ? curs_set(2) : curs_set(1);
#if SET_CURSOR_COLOR
over ? printf("\e]12;%s\a", colorDelete) : printf("\e]12;%s\a", colorInsert);
fflush(stdout);
#endif
}
void hideCursor()
{
curs_set(0);
}
//--------------------------------------------------------------------
// Shutdown ncurses
void shutdown()
{
free(sm4rt);
delwin(winInput);
delwin(winHelp);
showCursor();
endwin();
}
//--------------------------------------------------------------------
// Error exit ncurses
void exitMsg(int status, const char* message)
{
shutdown();
errx(status, message);
}
//--------------------------------------------------------------------
// Reset variables for ascii mode
void setViewMode()
{
lineWidth = modeAscii ? lineWidthAsc : lineWidthAsc / 4;
bufSize = numLines * lineWidth;
searchIndent = lineWidth * 3;
steps[cmmMoveByte] = 1;
steps[cmmMoveLine] = lineWidth;
steps[cmmMovePage] = bufSize - lineWidth;
steps[cmmMoveAll] = 0;
}
//--------------------------------------------------------------------
// Set variables for dynamic width ##:y
void calcScreenLayout()
{
if (COLS < minScreenWidth) {
string err("The screen must be at least " + to_string(minScreenWidth) + " characters wide.");
exitMsg(31, err.c_str());
}
if (LINES < minScreenHeight) {
string err("The screen must be at least " + to_string(minScreenHeight) + " lines high.");
exitMsg(32, err.c_str());
}
short tera = sizeTera ? 3 : 0; // use large addresses only if needed
leftMar = 11 + tera;
if (COLS >= 140 + tera) {
lineWidth = 32;
screenWidth = 140 + tera;
leftMar2 = 108 + tera;
}
else if (COLS >= 108 + tera) {
lineWidth = 24;
screenWidth = 108 + tera;
leftMar2 = 84 + tera;
}
else {
lineWidth = 16;
screenWidth = 76 + tera;
leftMar2 = 60 + tera;
}
lineWidthAsc = lineWidth * 4;
inWidth = (sizeTera ? 15 : 11) + 1; // sign
linesTotal = LINES;
numLines = linesTotal / (singleFile ? 1 : 2) - 1;
setViewMode();
} // end calcScreenLayout
//--------------------------------------------------------------------
// Convert a character to uppercase
int upCase(int c)
{
return (c >= 'a' && c <= 'z') ? c & ~0x20 : c;
}
//--------------------------------------------------------------------
// Convert buffer to lowercase
void lowCase(Byte* buf, Size len)
{
Byte* b = bufFile1; // movdqu ==> movdqa
if (len == staticSize) { // SIMD
for (Size i=0; i < staticSize; ++i) {
b[i] = b[i] >= 'A' && b[i] <= 'Z' ? b[i] | 0x20 : b[i];
}
}
else {
for (Size i=0; i < len; ++i) {
if (buf[i] <= 'Z' && buf[i] >= 'A') {
buf[i] |= 0x20;
}
}
}
}
//--------------------------------------------------------------------
// Convert buffer for ascii
void setAscii(Size len)
{
Byte* b = bufFile1; // enregister
if (len == staticSize) { // SIMD
for (Size i=0; i < staticSize; ++i) {
b[i] = b[i] > '~' || b[i] < ' ' ? ' ' : b[i];
}
}
else {
for (Size i=0; i < len; ++i) {
if (b[i] > '~' || b[i] < ' ') {
b[i] = ' ';
}
}
}
}
//--------------------------------------------------------------------
// Compare lines for smartScroll: 16 / 32 / 64 / 96 / 128 Byte
bool cmpLine(Byte* ln1, Byte* ln2, Full cnt)
{
Quad q1 = _mm_load_si128((Quad*) ln1),
q2 = _mm_load_si128((Quad*) ln2);
q1 = _mm_cmpeq_epi8(q1, q2);
if (_mm_movemask_epi8(q1) != 0xFFFF) {
return true;
}
if (cnt == 16) {
return false;
}
q1 = _mm_load_si128((Quad*) (ln1 + 16));
q2 = _mm_load_si128((Quad*) (ln2 + 16));
q1 = _mm_cmpeq_epi8(q1, q2);
if (_mm_movemask_epi8(q1) != 0xFFFF) {
return true;
}
for (Full i = 32; i < cnt; i += 16) {
q1 = _mm_load_si128((Quad*) (ln1 + i));
q2 = _mm_load_si128((Quad*) (ln2 + i));
q1 = _mm_cmpeq_epi8(q1, q2);
if (_mm_movemask_epi8(q1) != 0xFFFF) {
return true;
}
}
return false;
} // end cmpLine
//--------------------------------------------------------------------
// Compare lines for smartScroll (unaligned): 24 Byte
bool cmpLineU(Byte* ln1, Byte* ln2, Full cnt)
{
Quad q1 = _mm_loadu_si128((Quad*) ln1),
q2 = _mm_loadu_si128((Quad*) ln2);
q1 = _mm_cmpeq_epi8(q1, q2);
if (_mm_movemask_epi8(q1) != 0xFFFF) {
return true;
}
q1 = _mm_loadu_si128((Quad*) (ln1 + 8));
q2 = _mm_loadu_si128((Quad*) (ln2 + 8));
q1 = _mm_cmpeq_epi8(q1, q2);
if (_mm_movemask_epi8(q1) != 0xFFFF) {
return true;
}
return false;
}
//--------------------------------------------------------------------
// Convert hex string to bytes
int packHex(char* buf)
{
Byte *pb = (Byte*) buf,
*po = pb;
for (Byte b; (b = *pb); ++pb) {
if (b == ' ') {
continue;
}
else {
b = (b - (*pb++ > 64 ? 55 : 48)) << 4;
b |= *pb - (*pb > 0x40 ? 0x37 : 0x30);
*po++ = b;
}
}
return po - (Byte*) buf;
}
//--------------------------------------------------------------------
// My pretty printer
char *pretty(char *buffer, FPos *size, int sign)
{
char aBuf[64],
*pa = aBuf,
*pb = buffer;
sprintf(aBuf, (sign ? "%+ld" : "%ld"), *size);
int len = strlen(aBuf);
while (len) {
*pb++ = *pa++;
if (sign) {
--sign;
--len;
}
else {
if (--len && ! (len % 3)) {
if (thouSep) {
*pb++ = thouSep;
}
}
}
}
*pb = 0;
return buffer;
} // end pretty