diff --git a/display.c b/display.c index 8f593c7..f89aeb9 100644 --- a/display.c +++ b/display.c @@ -133,6 +133,8 @@ void initCurses(void) init_pair(3, COLOR_BLUE, -1); /* extended chars */ } init_pair(4, COLOR_BLUE, COLOR_YELLOW); /* current cursor position*/ + init_pair(5, COLOR_WHITE, COLOR_RED); /* color for marking different bytes */ + #endif initDisplay(); @@ -169,6 +171,8 @@ void initDisplay(void) } colsUsed = computeLineSize(); buffer = realloc(buffer,page); + if(difffileName && *difffileName!='\0') + diffBuffer = malloc(page); bufferAttr = realloc(bufferAttr,page * sizeof(*bufferAttr)); } @@ -207,26 +211,35 @@ void display(void) if (MAX(fileSize, lastEditedLoc)) printw("/0x%llX", getfilesize()); if (mode == bySector) printw("--sector %lld", (base + cursor) / SECTOR_SIZE); + if (difffileName && *difffileName != '\0') + printw(" DIFF: 0x%02X - 0x%02X <- %s ", buffer[cursor], diffBuffer[cursor], difffileName); move(cursor / lineLength, computeCursorXCurrentPos()); } void displayLine(int offset, int max) { - int i,mark_color=0; + int i,mark_color=0,diff_color=0; + bool differs = FALSE; #ifdef HAVE_COLORS mark_color = COLOR_PAIR(4) | A_BOLD; + diff_color = COLOR_PAIR(5) | A_BOLD; + #endif PRINTW(("%08lX ", (int) (base + offset))); for (i = offset; i < offset + lineLength; i++) { if (i > offset) MAXATTRPRINTW(bufferAttr[i] & MARKED, (((i - offset) % blocSize) ? " " : " ")); if (i < max) { + differs = difffileName && *difffileName != '\0' && buffer[i] != diffBuffer[i]; ATTRPRINTW( #ifdef HAVE_COLORS (!colored ? 0 : (cursor == i && hexOrAscii == 0 ? mark_color : + + (differs ? diff_color : buffer[i] == 0 ? COLOR_PAIR(1) : buffer[i] < ' ' ? COLOR_PAIR(2) : - buffer[i] >= 127 ? COLOR_PAIR(3) : 0) + buffer[i] >= 127 ? COLOR_PAIR(3) : 0)) + ) | #endif bufferAttr[i], ("%02X", buffer[i])); @@ -235,9 +248,10 @@ void displayLine(int offset, int max) } PRINTW((" ")); for (i = offset; i < offset + lineLength; i++) { + differs = difffileName && *difffileName != '\0' && buffer[i] != diffBuffer[i]; if (i >= max) PRINTW((" ")); - else if (buffer[i] >= ' ' && buffer[i] < 127) ATTRPRINTW((cursor == i && hexOrAscii==1 ? mark_color : 0) | bufferAttr[i], ("%c", buffer[i])); - else ATTRPRINTW((cursor == i && hexOrAscii == 1 ? mark_color : 0) | bufferAttr[i], (".")); + else if (buffer[i] >= ' ' && buffer[i] < 127) ATTRPRINTW((cursor == i && hexOrAscii==1 ? mark_color : differs ? diff_color : 0) | bufferAttr[i], ("%c", buffer[i])); + else ATTRPRINTW((cursor == i && hexOrAscii == 1 ? mark_color : differs ? diff_color : 0) | bufferAttr[i], (".")); } } diff --git a/file.c b/file.c index b14fa4f..7cd1696 100644 --- a/file.c +++ b/file.c @@ -55,6 +55,38 @@ void openFile(void) biggestLoc = fileSize; } +void openDiffFile(void) { + struct stat st; + + if (!is_file(difffileName)) { + fprintf(stderr, "%s: %s: Not a file.\n", progName, difffileName); + exit(1); + } + + if ((difffd = open(difffileName, O_RDONLY)) == -1) { + if (page) + exitCurses(); + if (difffileName[0] == '-') + DIE(usage); + fprintf(stderr, "%s: ", progName); + perror(difffileName); + exit(1); + } + + if (fstat(difffd, &st) != -1 && st.st_size > 0) + diffFileSize = st.st_size; + else { +#ifdef BLKGETSIZE + unsigned long i; + if (ioctl(difffd, BLKGETSIZE, &i) == 0) + diffFileSize = (INT)i * 512; + else +#endif + diffFileSize = 0; + } +} + + void readFile(void) { typePage *p; @@ -82,7 +114,18 @@ void readFile(void) } } if (mark_set) markSelectedRegion(); + if(difffileName && *difffileName != '\0') + readDiffFile(); +} + +void readDiffFile(void) { + typePage *p; + INT i; + + memset(diffBuffer, 0, page * sizeof(*diffBuffer)); + LSEEK(difffd, base); + diffnbBytes = read(difffd, diffBuffer, page); } int findFile(void) diff --git a/hexedit.c b/hexedit.c index 0f634e7..f391e25 100644 --- a/hexedit.c +++ b/hexedit.c @@ -29,6 +29,13 @@ int isReadOnly, fd, nbBytes, oldcursor, oldattr, oldcursorOffset; int sizeCopyBuffer, *bufferAttr; char *progName, *fileName, *baseName; unsigned char *buffer, *copyBuffer; + +unsigned char *diffBuffer; +char *difffileName; +INT diffFileSize; +int difffd, diffnbBytes; + + typePage *edited; char *lastFindFile = NULL, *lastYankToAFile = NULL, *lastAskHexString = NULL, *lastAskAsciiString = NULL, *lastFillWithStringHexa = NULL, *lastFillWithStringAscii = NULL; @@ -45,7 +52,8 @@ const char * const usage = "usage: %s [-s | --sector] [-m | --maximize] [-l | #ifdef HAVE_COLORS " [--color]" #endif - " [-h | --help] filename\n"; + " [-h | --help] filename[ diffWithFile]\n" + "If you want mark differences between two files specify diffWithFile - only with --color\n"; /*******************************************************************************/ @@ -56,7 +64,7 @@ int main(int argc, char **argv) progName = basename(argv[0]); argv++; argc--; - for (; argc > 0; argv++, argc--) + for (; argc > 0; argv++, argc--) { if (streq(*argv, "-s") || streq(*argv, "--sector")) mode = bySector; @@ -83,13 +91,17 @@ int main(int argc, char **argv) DIE(usage) else break; } - if (argc > 1) DIE(usage); + if (argc < 1) DIE(usage); init(); - if (argc == 1) { - fileName = strdup(*argv); - openFile(); + fileName = strdup(*argv); + argv++; argc--; + openFile(); + if(argc == 1){ + difffileName = strdup(*argv); + openDiffFile(); } + initCurses(); if (fileName == NULL) { if (!findFile()) { diff --git a/hexedit.h b/hexedit.h index fc57aa9..a397a05 100644 --- a/hexedit.h +++ b/hexedit.h @@ -1,12 +1,14 @@ #ifndef HEXEDIT_H #define HEXEDIT_H + #include "config.h" #include #include #include #include #include +#include #if HAVE_FCNTL_H #include #endif @@ -100,6 +102,11 @@ extern int isReadOnly, fd, nbBytes, oldcursor, oldattr, oldcursorOffset; extern int sizeCopyBuffer, *bufferAttr; extern char *progName, *fileName, *baseName; extern unsigned char *buffer, *copyBuffer; +extern INT diffFileSize; +extern unsigned char *diffBuffer; +extern char *difffileName; +extern int difffd, diffnbBytes; + extern typePage *edited; extern char *lastFindFile, *lastYankToAFile, *lastAskHexString, *lastAskAsciiString, *lastFillWithStringHexa, *lastFillWithStringAscii; @@ -115,6 +122,8 @@ void quit(void); int tryloc(INT loc); void openFile(void); void readFile(void); +void openDiffFile(void); +void readDiffFile(void); int findFile(void); int computeLineSize(void); int computeCursorXCurrentPos(void);