Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diff #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions display.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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]));
Expand All @@ -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], ("."));
}
}

Expand Down
43 changes: 43 additions & 0 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 18 additions & 6 deletions hexedit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,7 +52,8 @@ const char * const usage = "usage: %s [-s | --sector] [-m | --maximize] [-l<n> |
#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";


/*******************************************************************************/
Expand All @@ -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;
Expand All @@ -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()) {
Expand Down
9 changes: 9 additions & 0 deletions hexedit.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#ifndef HEXEDIT_H
#define HEXEDIT_H


#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <getopt.h>
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down