├── TODO ├── README.md ├── .gitignore ├── autogen.sh ├── .github └── workflows │ └── full-check.yml ├── Makefile.in ├── Makefile-build.in ├── configure.ac ├── search.c ├── hexedit.c ├── file.c ├── misc.c ├── page.c ├── mark.c ├── install-sh ├── hexedit.h ├── Changes ├── display.c ├── hexedit.1.in ├── interact.c └── COPYING /TODO: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | To build from git: 2 | 3 | ``` 4 | ./autogen.sh && ./configure && make 5 | ``` 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | #output 2 | hexedit 3 | 4 | #build system dirt 5 | configure 6 | config.h* 7 | Makefile 8 | Makefile-build 9 | 10 | #compiled object files 11 | *.o 12 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | test -f configure.ac || { 4 | echo "Please, run this script in the top level project directory." 5 | exit 6 | } 7 | 8 | autoconf 9 | autoheader 10 | -------------------------------------------------------------------------------- /.github/workflows/full-check.yml: -------------------------------------------------------------------------------- 1 | name: full-check 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | steps: 11 | - uses: actions/checkout@v2 12 | - name: install_dependencies 13 | run: sudo apt install libncurses5-dev 14 | - name: first_build 15 | run: | 16 | ./autogen.sh 17 | ./configure 18 | make 19 | sudo make install 20 | # sudo make uninstall 21 | # make distclean 22 | # - name: second_build_to_test_build_twice 23 | # run: | 24 | # ./autogen.sh 25 | # ./configure 26 | # make 27 | # sudo make install 28 | -------------------------------------------------------------------------------- /Makefile.in: -------------------------------------------------------------------------------- 1 | # Makefile for hexedit 2 | 3 | PRODUCT = @PRODUCT@ 4 | VERSION = @VERSION@ 5 | 6 | SHELL = /bin/sh 7 | 8 | CC = @CC@ 9 | CFLAGS = @CFLAGS@ 10 | CPPFLAGS = @CPPFLAGS@ 11 | 12 | LDFLAGS = @LDFLAGS@ 13 | LIBS = @LIBS@ 14 | DEFS = @DEFS@ 15 | INSTALL = @INSTALL@ 16 | 17 | # Installation directories 18 | prefix = @prefix@ 19 | datarootdir = @datarootdir@ 20 | exec_prefix = @exec_prefix@ 21 | mandir = @mandir@ 22 | bindir = @bindir@ 23 | 24 | INCL = @INCL@ 25 | SRCS = @SRCS@ 26 | OBJS = $(SRCS:.c=.o) 27 | 28 | .SUFFIXES: .c .o 29 | 30 | .c.o: 31 | $(CC) $(DEFS) $(CFLAGS) $(CPPFLAGS) -c $< 32 | 33 | all: $(PRODUCT) 34 | 35 | $(PRODUCT): $(OBJS) 36 | $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) 37 | 38 | clean: 39 | rm -rf *~ *.o core *.cache config.status config.log $(PRODUCT) $(PRODUCT).1 40 | 41 | distclean: clean 42 | rm -f Makefile config.h Makefile-build 43 | 44 | install: $(PRODUCT) 45 | $(INSTALL) -d -m 755 $(DESTDIR)$(bindir) 46 | $(INSTALL) -m 755 $(PRODUCT) $(DESTDIR)$(bindir) 47 | $(INSTALL) -d -m 755 $(DESTDIR)$(mandir)/man1 48 | $(INSTALL) -m 644 $(PRODUCT).1 $(DESTDIR)$(mandir)/man1 49 | -------------------------------------------------------------------------------- /Makefile-build.in: -------------------------------------------------------------------------------- 1 | # Makefile-build for hexedit 2 | 3 | PRODUCT = @PRODUCT@ 4 | VERSION = @VERSION@ 5 | 6 | prefix = @prefix@ 7 | datarootdir = @datarootdir@ 8 | exec_prefix = @exec_prefix@ 9 | mandir = @mandir@/man1 10 | bindir = @bindir@ 11 | srcdir = @srcdir@ 12 | 13 | INCL = @INCL@ 14 | SRCS = @SRCS@ 15 | OTHER = @OTHER@ 16 | ALL = $(INCL) $(SRCS) $(OTHER) 17 | TARSOURCE = $(PRODUCT)-$(VERSION).src.tgz 18 | DYNAMICBIN = $(PRODUCT)-$(VERSION).bin.i386.dynamic.tgz 19 | HTMLSITE = $(PRODUCT)-html-site.tar 20 | HTMLFILES = $(PRODUCT).html 21 | TMPFILES = $(DYNAMICBIN) $(TARSOURCE) $(HTMLFILES) 22 | FROMINFILES = hexedit.1 23 | 24 | dynamic: 25 | $(MAKE) $(PRODUCT) 26 | install -d -m 755 usr/bin 27 | install -s -m 755 $(PRODUCT) usr/bin 28 | install -d -m 755 usr/man/man1 29 | install -m 644 $(PRODUCT).1 usr/man/man1 30 | tar cfz $(DYNAMICBIN) usr 31 | rm -rf usr 32 | 33 | tar: $(ALL) 34 | cd .. ; tar cfz $(PRODUCT)/$(TARSOURCE) $(ALL:%=$(PRODUCT)/%) 35 | 36 | docs: 37 | groff -mandoc -Thtml $(PRODUCT).1 > $(PRODUCT).html 38 | 39 | hexedit.1: hexedit.1.in 40 | m4 -DVERSION=$(VERSION) $< > $@ 41 | 42 | html-site: tar dynamic docs 43 | tar cf $(HTMLSITE) $(TMPFILES) 44 | 45 | clean: 46 | [ ! -e Makefile ] || $(MAKE) clean 47 | rm -f $(TMPFILES) $(HTMLSITE) TAGS $(FROMINFILES) 48 | 49 | distclean: clean 50 | [ ! -e Makefile ] || $(MAKE) distclean 51 | rm -f Makefile-build configure config.h.in 52 | 53 | ci: clean 54 | ci -l -mdefault * 55 | 56 | TAGS: 57 | etags *.[hc] 58 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | dnl Autoconfigure input file for hexedit 2 | dnl 3 | dnl Process this file with autoconf to produce a configure script. 4 | dnl 5 | 6 | AC_PREREQ([2.69]) 7 | 8 | AC_INIT 9 | AC_CONFIG_SRCDIR([hexedit.c]) 10 | AC_CONFIG_HEADERS(config.h) 11 | 12 | define(TheVERSION, 1.6) 13 | 14 | PRODUCT=hexedit 15 | VERSION=TheVERSION 16 | INCL=hexedit.h 17 | SRCS="hexedit.c display.c mark.c page.c file.c interact.c misc.c search.c" 18 | OTHER="COPYING Changes TODO install-sh configure config.h.in hexedit.1 Makefile.in configure.ac Makefile-build.in" 19 | AC_SUBST(PRODUCT) 20 | AC_SUBST(VERSION) 21 | AC_SUBST(INCL) 22 | AC_SUBST(SRCS) 23 | AC_SUBST(OTHER) 24 | 25 | dnl Enable GNU extensions on systems that have them. 26 | AH_VERBATIM([_GNU_SOURCE], 27 | [/* Enable GNU extensions on systems that have them. */ 28 | #ifndef _GNU_SOURCE 29 | # define _GNU_SOURCE 30 | #endif]) 31 | AC_DEFINE(_GNU_SOURCE) 32 | 33 | dnl Checks for programs. 34 | AC_PROG_CC 35 | AC_PROG_INSTALL 36 | 37 | dnl Checks for libraries. 38 | AC_CHECK_LIB(curses, initscr, LIBS="$LIBS -lcurses", 39 | [AC_CHECK_LIB(ncurses, initscr, LIBS="$LIBS -lncurses", 40 | AC_MSG_ERROR([libcurses/libncurses not found.]))] 41 | ) 42 | AC_CHECK_LIB(tinfo, keypad, LIBS="$LIBS -ltinfo") 43 | AC_CHECK_FUNC(use_default_colors, 44 | AC_DEFINE(HAVE_COLORS, , "Define if you want colored (fruit salad) display option") 45 | ) 46 | 47 | dnl This test must come as early as possible after the compiler configuration 48 | dnl tests, because the choice of the file model can (in principle) affect 49 | dnl whether functions and headers are available, whether they work, etc. 50 | AC_SYS_LARGEFILE 51 | 52 | dnl Checks for header files. 53 | AC_PROG_EGREP 54 | AC_HEADER_STAT 55 | AC_HEADER_SYS_WAIT 56 | AC_CHECK_HEADERS(fcntl.h unistd.h libgen.h sys/mount.h) 57 | 58 | dnl Checks for typedefs, structures, and compiler characteristics. 59 | AC_C_CONST 60 | AC_TYPE_SIZE_T 61 | 62 | dnl Checks for library functions. 63 | AC_CHECK_FUNCS(memcmp memmem memrmem memrchr strdup strerror basename) 64 | if test $ac_cv_func_basename = no; then 65 | # basename not in the default libraries. See if it's elsewhere 66 | AC_CHECK_LIB(gen, basename, [AC_DEFINE(HAVE_BASENAME) 67 | LIBS="$LIBS -lgen"]) 68 | fi 69 | 70 | AC_CHECK_DECLS([dirname, basename, memrchr, memmem, memrmem], , , 71 | $ac_includes_default 72 | #if HAVE_LIBGEN_H 73 | #include 74 | #endif 75 | ) 76 | 77 | AC_CONFIG_FILES([Makefile Makefile-build hexedit.1]) 78 | AC_OUTPUT 79 | -------------------------------------------------------------------------------- /search.c: -------------------------------------------------------------------------------- 1 | /* hexedit -- Hexadecimal Editor for Binary Files 2 | Copyright (C) 1998 Pixel (Pascal Rigaux) 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2, or (at your option) 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/ 17 | #include "hexedit.h" 18 | 19 | static int searchA(char **string, int *sizea, char *tmp, int tmp_size); 20 | static void searchB(INT loc, char *string); 21 | 22 | /*******************************************************************************/ 23 | /* Search functions */ 24 | /*******************************************************************************/ 25 | static int searchA(char **string, int *sizea, char *tmp, int tmp_size) 26 | { 27 | char *msg = hexOrAscii ? "Hexa string to search: " : "Ascii string to search: "; 28 | char **last = hexOrAscii ? &lastAskHexString : &lastAskAsciiString; 29 | 30 | if (!ask_about_save_and_redisplay()) return FALSE; 31 | if (!displayMessageAndGetString(msg, last, tmp, tmp_size)) return FALSE; 32 | 33 | *sizea = strlen(tmp); 34 | if (hexOrAscii) if (!hexStringToBinString(tmp, sizea)) return FALSE; 35 | 36 | *string = malloc(*sizea); 37 | if (!*string) { 38 | displayMessageAndWaitForKey("Can't allocate memory"); 39 | return FALSE; 40 | } 41 | memcpy(*string, tmp, *sizea); 42 | 43 | nodelay(stdscr, TRUE); 44 | displayTwoLineMessage("searching...", "(press any key to cancel)"); 45 | return TRUE; 46 | } 47 | 48 | static void searchB(INT loc, char *string) 49 | { 50 | nodelay(stdscr, FALSE); 51 | free(string); 52 | 53 | if (loc >= 0) set_cursor(loc); 54 | else { 55 | if (loc == -3) displayMessageAndWaitForKey("not found"); 56 | } 57 | } 58 | 59 | void search_forward(void) 60 | { 61 | char *p, *string, tmp[BLOCK_SEARCH_SIZE], tmpstr[BLOCK_SEARCH_SIZE]; 62 | int quit, sizea, sizeb; 63 | INT blockstart; 64 | 65 | if (!searchA(&string, &sizea, tmp, sizeof(tmp))) return; 66 | quit = -1; 67 | blockstart = base + cursor - BLOCK_SEARCH_SIZE + sizea; 68 | do { 69 | blockstart += BLOCK_SEARCH_SIZE - sizea + 1; 70 | if (LSEEK_(fd, blockstart) == -1) { quit = -3; break; } 71 | if ((sizeb = read(fd, tmp, BLOCK_SEARCH_SIZE)) < sizea) quit = -3; 72 | else if (getch() != ERR) quit = -2; 73 | else if ((p = mymemmem(tmp, sizeb, string, sizea))) quit = p - tmp; 74 | 75 | sprintf(tmpstr,"searching... 0x%08llX", (long long) blockstart); 76 | nodelay(stdscr, TRUE); 77 | displayTwoLineMessage(tmpstr, "(press any key to cancel)"); 78 | 79 | } while (quit == -1); 80 | 81 | searchB(quit + (quit >= 0 ? blockstart : 0), string); 82 | } 83 | 84 | void search_backward(void) 85 | { 86 | char *p, *string, tmp[BLOCK_SEARCH_SIZE], tmpstr[BLOCK_SEARCH_SIZE]; 87 | int quit, sizea, sizeb; 88 | INT blockstart; 89 | 90 | if (!searchA(&string, &sizea, tmp, sizeof(tmp))) return; 91 | quit = -1; 92 | blockstart = base + cursor - sizea + 1; 93 | do { 94 | blockstart -= BLOCK_SEARCH_SIZE - sizea + 1; 95 | sizeb = BLOCK_SEARCH_SIZE; 96 | if (blockstart < 0) { sizeb -= -blockstart; blockstart = 0; } 97 | 98 | if (sizeb < sizea) quit = -3; 99 | else { 100 | if (LSEEK_(fd, blockstart) == -1) { quit = -3; break; } 101 | if (sizeb != read(fd, tmp, sizeb)) quit = -3; 102 | else if (getch() != ERR) quit = -2; 103 | else if ((p = mymemrmem(tmp, sizeb, string, sizea))) quit = p - tmp; 104 | } 105 | 106 | sprintf(tmpstr,"searching... 0x%08llX", (long long) blockstart); 107 | nodelay(stdscr, TRUE); 108 | displayTwoLineMessage(tmpstr, "(press any key to cancel)"); 109 | 110 | } while (quit == -1); 111 | 112 | searchB(quit + (quit >= 0 ? blockstart : 0), string); 113 | } 114 | 115 | 116 | -------------------------------------------------------------------------------- /hexedit.c: -------------------------------------------------------------------------------- 1 | /* hexedit -- Hexadecimal Editor for Binary Files 2 | Copyright (C) 1998 Pixel (Pascal Rigaux) 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2, or (at your option) 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/ 17 | #include "hexedit.h" 18 | 19 | 20 | /*******************************************************************************/ 21 | /* Global variables */ 22 | /*******************************************************************************/ 23 | INT lastEditedLoc, biggestLoc, fileSize; 24 | INT mark_min, mark_max, mark_set; 25 | INT base, oldbase; 26 | int normalSpaces, cursor, cursorOffset, hexOrAscii; 27 | int cursor, blocSize, lineLength, colsUsed, page; 28 | int fd, nbBytes, oldcursor, oldattr, oldcursorOffset; 29 | int nAddrDigits; 30 | int sizeCopyBuffer, *bufferAttr; 31 | char *progName, *fileName, *baseName; 32 | unsigned char *buffer, *copyBuffer; 33 | typePage *edited; 34 | 35 | char *lastFindFile = NULL, *lastYankToAFile = NULL, *lastAskHexString = NULL, *lastAskAsciiString = NULL, *lastFillWithStringHexa = NULL, *lastFillWithStringAscii = NULL; 36 | 37 | 38 | const modeParams modes[LAST] = { 39 | { 8, 16, 256 }, 40 | { 4, 0, 0 }, 41 | }; 42 | modeType mode = maximized; 43 | int colored = FALSE; 44 | int isReadOnly = FALSE; 45 | 46 | const char * const usage = "usage: %s [-s | --sector] [-m | --maximize] [-l | --linelength ] [-r | --readonly]" 47 | #ifdef HAVE_COLORS 48 | " [--color]" 49 | #endif 50 | " [-h | --help] filename\n"; 51 | 52 | 53 | /*******************************************************************************/ 54 | /* main */ 55 | /*******************************************************************************/ 56 | int main(int argc, char **argv) 57 | { 58 | progName = basename(argv[0]); 59 | argv++; argc--; 60 | 61 | for (; argc > 0; argv++, argc--) 62 | { 63 | if (streq(*argv, "-s") || streq(*argv, "--sector")) 64 | mode = bySector; 65 | else if (streq(*argv, "-r") || streq(*argv, "--readonly")) 66 | isReadOnly = TRUE; 67 | else if (streq(*argv, "-m") || streq(*argv, "--maximize")) { 68 | mode = maximized; 69 | lineLength = 0; 70 | } 71 | #ifdef HAVE_COLORS 72 | else if (streq(*argv, "--color")) 73 | colored = TRUE; 74 | #endif 75 | else if (strbeginswith(*argv, "-l") || strbeginswith(*argv, "--linelength")) { 76 | if (strbeginswith(*argv, "-l") && strlen(*argv) > 2) 77 | lineLength = atoi(*argv + 2); 78 | else { 79 | argv++; argc--; 80 | lineLength = atoi(*argv); 81 | } 82 | if (lineLength < 0 || lineLength > 4096) 83 | DIE("%s: illegal line length\n") 84 | } else if (streq(*argv, "--")) { 85 | argv++; argc--; 86 | break; 87 | } else if (*argv[0] == '-') 88 | 89 | DIE(usage) 90 | else break; 91 | } 92 | if (argc > 1) DIE(usage); 93 | 94 | init(); 95 | if (argc == 1) { 96 | fileName = strdup(*argv); 97 | openFile(); 98 | } 99 | initCurses(); 100 | if (fileName == NULL) { 101 | if (!findFile()) { 102 | exitCurses(); 103 | DIE("%s: No such file\n"); 104 | } 105 | openFile(); 106 | } 107 | readFile(); 108 | do display(); 109 | while (key_to_function(getch())); 110 | quit(); 111 | return 0; /* for no warning */ 112 | } 113 | 114 | 115 | 116 | /*******************************************************************************/ 117 | /* other functions */ 118 | /*******************************************************************************/ 119 | void init(void) 120 | { 121 | page = 0; /* page == 0 means initCurses is not done */ 122 | normalSpaces = 3; 123 | hexOrAscii = TRUE; 124 | copyBuffer = NULL; 125 | edited = NULL; 126 | } 127 | 128 | void quit(void) 129 | { 130 | exitCurses(); 131 | free(fileName); 132 | free(buffer); 133 | free(bufferAttr); 134 | FREE(copyBuffer); 135 | discardEdited(); 136 | FREE(lastFindFile); FREE(lastYankToAFile); FREE(lastAskHexString); FREE(lastAskAsciiString); FREE(lastFillWithStringHexa); FREE(lastFillWithStringAscii); 137 | exit(0); 138 | } 139 | 140 | 141 | 142 | 143 | 144 | -------------------------------------------------------------------------------- /file.c: -------------------------------------------------------------------------------- 1 | /* hexedit -- Hexadecimal Editor for Binary Files 2 | Copyright (C) 1998 Pixel (Pascal Rigaux) 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2, or (at your option) 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/ 17 | #include "hexedit.h" 18 | 19 | 20 | // Compute number of hex-digits needed to display an address. 21 | // We only increase this by full bytes (2 digits). 22 | // Because the input is uint64_t, the maximum result is 16 (64bit = 16*4bit). 23 | int compute_nDigits(uint64_t maxAddr) 24 | { 25 | int digits = 0; 26 | while (maxAddr) { 27 | digits+=2; 28 | maxAddr >>= 8; 29 | } 30 | 31 | if (digits==0) { 32 | return 2; 33 | } 34 | 35 | return digits; 36 | } 37 | 38 | 39 | void openFile(void) 40 | { 41 | struct stat st; 42 | 43 | if (!is_file(fileName)) { 44 | fprintf(stderr, "%s: %s: Not a file.\n", progName, fileName); 45 | exit(1); 46 | } 47 | 48 | /* edited should be cleaned here (assert(edited == NULL)) */ 49 | if (isReadOnly || (fd = open(fileName, O_RDWR)) == -1) { 50 | isReadOnly = TRUE; 51 | if ((fd = open(fileName, O_RDONLY)) == -1) { 52 | if (page) exitCurses(); 53 | if (fileName[0] == '-') DIE(usage); 54 | fprintf(stderr, "%s: ", progName); 55 | perror(fileName); 56 | exit(1); 57 | } 58 | } else isReadOnly = FALSE; 59 | baseName = basename(fileName); 60 | mark_set = FALSE; 61 | lastEditedLoc = base = cursor = cursorOffset = 0; 62 | 63 | /* check file size- doesn't work on devices. I considered implementing a conquer-and-divide algorithm, but it would unpleasant on tape drives and such. */ 64 | if (fstat(fd, &st) != -1 && st.st_size > 0) 65 | fileSize = st.st_size; 66 | else { 67 | #ifdef BLKGETSIZE 68 | unsigned long i; 69 | if (ioctl(fd, BLKGETSIZE, &i) == 0) 70 | fileSize = (INT) i * 512; 71 | else 72 | #endif 73 | fileSize = 0; 74 | } 75 | biggestLoc = fileSize; 76 | 77 | nAddrDigits = compute_nDigits(biggestLoc); 78 | 79 | // use at least 8 digits (4 byte addresses) 80 | if (nAddrDigits < 8) { 81 | nAddrDigits = 8; 82 | } 83 | } 84 | 85 | void readFile(void) 86 | { 87 | typePage *p; 88 | INT i; 89 | 90 | memset(buffer, 0, page * sizeof(*buffer)); 91 | 92 | LSEEK(fd, base); 93 | nbBytes = read(fd, buffer, page); 94 | if (nbBytes < 0) 95 | nbBytes = 0; 96 | else if (nbBytes && base + nbBytes > biggestLoc) 97 | biggestLoc = base + nbBytes; 98 | memset(bufferAttr, A_NORMAL, page * sizeof(*bufferAttr)); 99 | for (p = edited; p; p = p->next) { 100 | for (i = MAX(base, p->base); i < MIN(p->base + p->size, base + page); i++) { 101 | if (buffer[i - base] != p->vals[i - p->base]) { 102 | buffer[i - base] = p->vals[i - p->base]; 103 | bufferAttr[i - base] |= MODIFIED; 104 | } 105 | } 106 | if (p->base + p->size > base + nbBytes) { /* Check for modifications past EOF */ 107 | for(; p->base + p->size > base + nbBytes && nbBytes < page; nbBytes++) 108 | bufferAttr[nbBytes] |= MODIFIED; 109 | } 110 | } 111 | if (mark_set) markSelectedRegion(); 112 | 113 | } 114 | 115 | int findFile(void) 116 | { 117 | char *p, tmp[BLOCK_SEARCH_SIZE]; 118 | p = lastFindFile ? strdup(lastFindFile) : NULL; 119 | if (!displayMessageAndGetString("File name: ", &p, tmp, sizeof(tmp))) return FALSE; 120 | if (!is_file(tmp)) { 121 | FREE(p); 122 | return FALSE; 123 | } 124 | FREE(lastFindFile); lastFindFile = fileName; 125 | fileName = p; 126 | return TRUE; 127 | } 128 | 129 | 130 | INT getfilesize(void) 131 | { 132 | return MAX(lastEditedLoc, biggestLoc); 133 | } 134 | 135 | 136 | /* tryloc: 137 | * returns TRUE if loc is an apropriate location to place the cursor 138 | * assumes the file won't shrink. 139 | * returns TRUE if the cursor is one past EOF to allow appending 140 | */ 141 | 142 | int tryloc(INT loc) 143 | { 144 | char c; 145 | if (loc < 0) 146 | return FALSE; 147 | if (loc <= lastEditedLoc) 148 | return TRUE; 149 | 150 | if (loc <= biggestLoc) 151 | return TRUE; 152 | 153 | if (LSEEK_(fd, loc - 1) != -1 && /* don't have to worry about loc - 1 < 0 */ 154 | read(fd, &c, 1) == 1) { 155 | biggestLoc = loc; 156 | return TRUE; 157 | } 158 | return FALSE; 159 | } 160 | 161 | int is_file(char *name) 162 | { 163 | struct stat st; 164 | return stat(name, &st) != -1 && !S_ISDIR(st.st_mode); 165 | } 166 | 167 | 168 | -------------------------------------------------------------------------------- /misc.c: -------------------------------------------------------------------------------- 1 | /* hexedit -- Hexadecimal Editor for Binary Files 2 | Copyright (C) 1998 Pixel (Pascal Rigaux) 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2, or (at your option) 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/ 17 | #include "hexedit.h" 18 | 19 | 20 | int LSEEK_(int fd, INT where) 21 | { 22 | INT result; 23 | 24 | result = lseek(fd, where, SEEK_SET); 25 | return (result == where) ? 1 : -1; 26 | } 27 | 28 | void LSEEK(int fd, INT where) 29 | { 30 | INT result; 31 | 32 | result = lseek(fd, where, SEEK_SET); 33 | if (result != where) { 34 | exitCurses(); 35 | fprintf(stderr, "the long seek failed (%lld instead of %lld), leaving :(\n", (long long) result, (long long) where); 36 | exit(1); 37 | } 38 | } 39 | 40 | /*******************************************************************************/ 41 | /* Functions provided for OSs that don't have them */ 42 | /*******************************************************************************/ 43 | #ifndef HAVE_BASENAME 44 | char *basename(char *file) { 45 | char *p = strrchr(file, '/'); 46 | return p ? p + 1 : file; 47 | } 48 | #endif 49 | 50 | #ifndef HAVE_STRERROR 51 | char *strerror(int errnum) { 52 | extern char *sys_errlist[]; 53 | extern int sys_nerr; 54 | 55 | if (errnum > 0 && errnum <= sys_nerr) 56 | return sys_errlist[errnum]; 57 | return _("Unknown system error"); 58 | } 59 | #endif 60 | 61 | #ifndef HAVE_STRDUP 62 | char *strdup(const char *str) 63 | { 64 | size_t len = strlen(str) + 1; 65 | void *new = malloc(len); 66 | if (new == NULL) return NULL; 67 | 68 | return (char *) bcopy(str, new, len); 69 | } 70 | #endif 71 | 72 | /*******************************************************************************/ 73 | /* Small common functions */ 74 | /*******************************************************************************/ 75 | int streq(const char *s1, const char *s2) { return strcmp(s1, s2) == 0; } 76 | INT myfloor(INT a, INT b) { return a - a % b; } 77 | int setLowBits(int p, int val) { return (p & 0xF0) + val; } 78 | int setHighBits(int p, int val) { return (p & 0x0F) + val * 0x10; } 79 | 80 | int strbeginswith(const char *a, const char *prefix) 81 | { 82 | return strncmp(a, prefix, strlen(prefix)) == 0; 83 | } 84 | 85 | char *strconcat3(char *a, char *b, char *c) 86 | { 87 | size_t la = a ? strlen(a) : 0; 88 | size_t lb = b ? strlen(b) : 0; 89 | size_t lc = c ? strlen(c) : 0; 90 | char *p = malloc(la + lb + lc + 1); 91 | if (a) memcpy(p, a, la); 92 | if (b) memcpy(p + la, b, lb); 93 | if (c) memcpy(p + la + lb, c, lc); 94 | p[la + lb + lc] = '\0'; 95 | return p; 96 | } 97 | 98 | int hexCharToInt(int c) 99 | { 100 | if (isdigit(c)) return c - '0'; 101 | return tolower(c) - 'a' + 10; 102 | } 103 | 104 | int not(int b) { return b ? FALSE: TRUE; } 105 | 106 | #ifndef HAVE_MEMRCHR 107 | void *memrchr(const void *s, int c, size_t n) 108 | { 109 | int i; 110 | const char *cs = s; 111 | for (i = n - 1; i >= 0; i--) if (cs[i] == c) return (void *) &cs[i]; 112 | return NULL; 113 | } 114 | #endif 115 | 116 | char *mymemmem(char *a, int sizea, char *b, int sizeb) 117 | { 118 | #ifdef HAVE_MEMMEM 119 | return memmem(a, sizea, b, sizeb); 120 | #else 121 | char *p; 122 | int i = sizea - sizeb + 1; 123 | 124 | if (i <= 0) return NULL; 125 | 126 | for (; (p = memchr(a, b[0], i)); i -= p - a + 1, a = p + 1) 127 | { 128 | if ((memcmp(p + 1, b + 1, sizeb - 1)) == 0) { 129 | return p; 130 | } 131 | } 132 | return NULL; 133 | #endif 134 | } 135 | 136 | char *mymemrmem(char *a, int sizea, char *b, int sizeb) 137 | { 138 | #ifdef HAVE_MEMRMEM 139 | return memrmem(a, sizea, b, sizeb); 140 | #else 141 | char *p; 142 | int i = sizea - sizeb + 1; 143 | 144 | if (i <= 0) return NULL; 145 | 146 | a += sizea - 1; 147 | for (; (p = memrchr(a - i + 1, b[sizeb - 1], i)); i -= a - p + 1, a = p - 1) 148 | { 149 | if ((memcmp(p - sizeb + 1, b, sizeb - 1)) == 0) return p; 150 | } 151 | return NULL; 152 | #endif 153 | } 154 | 155 | 156 | int hexStringToBinString(char *p, int *l) 157 | { 158 | int i; 159 | int j; 160 | 161 | for (i = 0, j = 0; i < *l; i++, j++) { 162 | if( isspace(p[i]) ) { 163 | j--; 164 | continue; 165 | } 166 | if (!isxdigit(p[i])) { 167 | displayMessageAndWaitForKey("Invalid hexa string"); 168 | return FALSE; 169 | } 170 | p[j / 2] = ((j % 2) ? setLowBits : setHighBits)(p[j / 2], hexCharToInt(p[i])); 171 | } 172 | 173 | if ((j % 2)) { 174 | displayMessageAndWaitForKey("Must be an even number of chars"); 175 | return FALSE; 176 | } 177 | *l = j / 2; 178 | return TRUE; 179 | } 180 | 181 | -------------------------------------------------------------------------------- /page.c: -------------------------------------------------------------------------------- 1 | /* hexedit -- Hexadecimal Editor for Binary Files 2 | Copyright (C) 1998 Pixel (Pascal Rigaux) 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2, or (at your option) 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/ 17 | #include "hexedit.h" 18 | 19 | 20 | void setToChar(int i, unsigned char c) 21 | { 22 | if (i >= nbBytes) { /* If appending, always mark as modified */ 23 | buffer[i] = c; 24 | bufferAttr[i] |= MODIFIED; 25 | addToEdited(base + i, 1, &c); 26 | nbBytes = i+1; 27 | } else if (buffer[i] != c) { 28 | buffer[i] = c; 29 | bufferAttr[i] |= MODIFIED; 30 | addToEdited(base + i, 1, &c); 31 | } 32 | } 33 | 34 | /*******************************************************************************/ 35 | /* Pages handling functions */ 36 | /*******************************************************************************/ 37 | void updatelastEditedLoc(void) 38 | { 39 | typePage *p; 40 | lastEditedLoc = 0; 41 | for (p = edited; p; p = p->next) { 42 | if (p->base + p->size > lastEditedLoc) 43 | lastEditedLoc = p->base + p->size; 44 | } 45 | } 46 | 47 | void discardEdited(void) 48 | { 49 | typePage *p, *q; 50 | for (p = edited; p; p = q) { 51 | q = p->next; 52 | freePage(p); 53 | } 54 | edited = NULL; 55 | lastEditedLoc = 0; 56 | if (base + cursor > biggestLoc) set_cursor(biggestLoc); 57 | if (mark_max >= biggestLoc) mark_max = biggestLoc - 1; 58 | } 59 | 60 | void addToEdited(INT base, int size, unsigned char *vals) 61 | { 62 | typePage *p, *q = NULL; 63 | for (p = edited; p; q = p, p = p->next) { 64 | if (base + size <= p->base) break; 65 | if (base <= p->base && p->base + p->size <= base + size) { 66 | if (q) q->next = p->next; else edited = p->next; 67 | freePage(p); p = q; 68 | if (q == NULL) { 69 | p = edited; 70 | break; 71 | } 72 | } 73 | } 74 | 75 | if (q && base <= q->base + q->size && q->base <= base + size) { 76 | /* chevauchement (?? how to say it in english ??) */ 77 | INT min, max; 78 | unsigned char *s; 79 | min = MIN(q->base, base); 80 | if (p && base + size == p->base) { 81 | max = p->base + p->size; 82 | s = malloc(max - min); 83 | memcpy(s + q->base - min, q->vals, q->size); 84 | memcpy(s + base - min, vals, size); 85 | memcpy(s + p->base - min, p->vals, p->size); 86 | free(q->vals); q->vals = s; 87 | q->next = p->next; 88 | freePage(p); 89 | } else { 90 | max = MAX(q->base + q->size, base + size); 91 | s = malloc(max - min); 92 | memcpy(s + q->base - min, q->vals, q->size); 93 | memcpy(s + base - min, vals, size); 94 | free(q->vals); q->vals = s; 95 | } 96 | q->base = min; 97 | q->size = max - min; 98 | } else if (p && base + size == p->base) { 99 | unsigned char *s = malloc(p->base + p->size - base); 100 | memcpy(s, vals, size); 101 | memcpy(s + p->base - base, p->vals, p->size); 102 | free(p->vals); p->vals = s; 103 | p->size = p->base + p->size - base; 104 | p->base = base; 105 | } else { 106 | typePage *r = newPage(base, size); 107 | memcpy(r->vals, vals, size); 108 | if (q) q->next = r; else edited = r; 109 | r->next = p; 110 | } 111 | updatelastEditedLoc(); 112 | } 113 | 114 | void removeFromEdited(INT base, int size) 115 | { 116 | typePage *p, *q = NULL; 117 | for (p = edited; p; p ? (q = p, p = p->next) : (q = NULL, p = edited)) { 118 | if (base + size <= p->base) break; 119 | if (base <= p->base) { 120 | if (p->base + p->size <= base + size) { 121 | if (q) q->next = p->next; else edited = p->next; 122 | freePage(p); 123 | p = q; 124 | } else { 125 | p->size -= base + size - p->base; 126 | memmove(p->vals, p->vals + base + size - p->base, p->size); 127 | p->base = base + size; 128 | } 129 | } else if (p->base + p->size <= base + size) { 130 | if (base < p->base + p->size) p->size -= p->base + p->size - base; 131 | } else { 132 | q = newPage(base + size, p->base + p->size - base - size); 133 | memcpy(q->vals, p->vals + base + size - p->base, q->size); 134 | q->next = p->next; 135 | p->next = q; 136 | p->size -= p->base + p->size - base; 137 | break; 138 | } 139 | } 140 | updatelastEditedLoc(); 141 | } 142 | 143 | typePage *newPage(INT base, int size) 144 | { 145 | typePage *p = (typePage *) malloc(sizeof(typePage)); 146 | p->base = base; 147 | p->size = size; 148 | p->vals = malloc(size); 149 | return p; 150 | } 151 | 152 | void freePage(typePage *page) 153 | { 154 | free(page->vals); 155 | free(page); 156 | } 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | -------------------------------------------------------------------------------- /mark.c: -------------------------------------------------------------------------------- 1 | /* hexedit -- Hexadecimal Editor for Binary Files 2 | Copyright (C) 1998 Pixel (Pascal Rigaux) 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2, or (at your option) 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/ 17 | #include "hexedit.h" 18 | 19 | /*******************************************************************************/ 20 | /* Mark functions */ 21 | /*******************************************************************************/ 22 | void markRegion(INT a, INT b) { int i; for (i = MAX(a - base, 0); i <= MIN(b - base, nbBytes - 1); i++) markIt(i); } 23 | void unmarkRegion(INT a, INT b) { int i; for (i = MAX(a - base, 0); i <= MIN(b - base, nbBytes - 1); i++) unmarkIt(i); } 24 | void markSelectedRegion(void) { markRegion(mark_min, mark_max); } 25 | void unmarkAll(void) { unmarkRegion(base, base + nbBytes - 1); } 26 | void markIt(int i) { bufferAttr[i] |= MARKED; } 27 | void unmarkIt(int i) { bufferAttr[i] &= ~MARKED; } 28 | 29 | void copy_region(void) 30 | { 31 | typePage *p; 32 | 33 | if (!mark_set) { displayMessageAndWaitForKey("Nothing to copy"); return; } 34 | sizeCopyBuffer = mark_max - mark_min + 1; 35 | if (sizeCopyBuffer == 0) return; 36 | if (sizeCopyBuffer > BIGGEST_COPYING) { 37 | displayTwoLineMessage("Hey, don't you think that's too big?!", "Really copy (Yes/No)"); 38 | if (tolower(getch()) != 'y') return; 39 | } 40 | FREE(copyBuffer); 41 | if ((copyBuffer = malloc(sizeCopyBuffer)) == NULL) { 42 | displayMessageAndWaitForKey("Can't allocate that much memory"); 43 | return; 44 | } 45 | if (LSEEK_(fd, mark_min) == -1 || read(fd, copyBuffer, sizeCopyBuffer) == -1) { 46 | displayMessageAndWaitForKey(strerror(errno)); 47 | return; 48 | } 49 | 50 | for (p = edited; p; p = p->next) { 51 | if (mark_min < p->base + p->size && p->base <= mark_max) { 52 | INT min = MIN(p->base, mark_min); 53 | memcpy(copyBuffer + p->base - min, 54 | p->vals + mark_min - min, 55 | MIN(MIN(p->base + p->size, mark_max) - MAX(p->base, mark_min) + 1, p->size)); 56 | } 57 | } 58 | unmarkAll(); 59 | mark_set = FALSE; 60 | } 61 | 62 | void yank(void) 63 | { 64 | if (copyBuffer == NULL) { displayMessageAndWaitForKey("Nothing to paste"); return; } 65 | if (isReadOnly) { displayMessageAndWaitForKey("File is read-only!"); return; } 66 | addToEdited(base + cursor, sizeCopyBuffer, copyBuffer); 67 | readFile(); 68 | } 69 | 70 | void yank_to_a_file(void) 71 | { 72 | char tmp[BLOCK_SEARCH_SIZE]; 73 | int f; 74 | 75 | if (copyBuffer == NULL) { displayMessageAndWaitForKey("Nothing to paste"); return; } 76 | 77 | if (!displayMessageAndGetString("File name: ", &lastYankToAFile, tmp, sizeof(tmp))) return; 78 | 79 | if ((f = open(tmp, O_RDONLY)) != -1) { 80 | close(f); 81 | displayTwoLineMessage("File exists", "Overwrite it (Yes/No)"); 82 | if (tolower(getch()) != 'y') return; 83 | } 84 | if ((f = creat(tmp, 0666)) == -1 || write(f, copyBuffer, sizeCopyBuffer) == -1) { 85 | displayMessageAndWaitForKey(strerror(errno)); 86 | } 87 | 88 | close(f); 89 | } 90 | 91 | void fill_with_string(void) 92 | { 93 | char *msg = hexOrAscii ? "Hexa string to fill with: " : "Ascii string to fill with: "; 94 | char **last = hexOrAscii ? &lastFillWithStringHexa : &lastFillWithStringAscii; 95 | char tmp2[BLOCK_SEARCH_SIZE]; 96 | unsigned char *tmp1; 97 | int i, l1, l2; 98 | 99 | if (!mark_set) return; 100 | if (isReadOnly) { displayMessageAndWaitForKey("File is read-only!"); return; } 101 | if (sizeCopyBuffer > BIGGEST_COPYING) { 102 | displayTwoLineMessage("Hey, don't you think that's too big?!", "Really fill (Yes/No)"); 103 | if (tolower(getch()) != 'y') return; 104 | } 105 | if (!displayMessageAndGetString(msg, last, tmp2, sizeof(tmp2))) return; 106 | l1 = mark_max - mark_min + 1; 107 | l2 = strlen(tmp2); 108 | if (hexOrAscii) { 109 | if (strlen(tmp2) == 1) { 110 | if (!isxdigit(*tmp2)) { displayMessageAndWaitForKey("Invalid hexa string"); return; } 111 | *tmp2 = hexCharToInt(*tmp2); 112 | } else if (!hexStringToBinString(tmp2, &l2)) return; 113 | } 114 | tmp1 = malloc(l1); 115 | if (!tmp1) { 116 | displayMessageAndWaitForKey("Can't allocate memory"); 117 | return; 118 | } 119 | for (i = 0; i < l1 - l2 + 1; i += l2) memcpy(tmp1 + i, tmp2, l2); 120 | memcpy(tmp1 + i, tmp2, l1 - i); 121 | addToEdited(mark_min, l1, tmp1); 122 | readFile(); 123 | free(tmp1); 124 | } 125 | 126 | 127 | void updateMarked(void) 128 | { 129 | if (base + cursor > oldbase + oldcursor) { 130 | 131 | if (mark_min == mark_max) { 132 | mark_max = base + cursor; 133 | } else if (oldbase + oldcursor == mark_min) { 134 | if (base + cursor <= mark_max) { 135 | mark_min = base + cursor; 136 | unmarkRegion(oldbase + oldcursor, mark_min - 1); 137 | } else { 138 | unmarkRegion(oldbase + oldcursor, mark_max); 139 | mark_min = mark_max; 140 | mark_max = base + cursor; 141 | } 142 | } else { 143 | mark_max = base + cursor; 144 | } 145 | 146 | } else if (base + cursor < oldbase + oldcursor){ 147 | if (mark_min == mark_max) { 148 | mark_min = base + cursor; 149 | } else if (oldbase + oldcursor == mark_max) { 150 | if (base + cursor >= mark_min) { 151 | mark_max = base + cursor; 152 | unmarkRegion(mark_max + 1, oldbase + oldcursor); 153 | } else { 154 | unmarkRegion(mark_min, oldbase + oldcursor); 155 | markRegion(base + cursor, mark_min - 1); 156 | mark_max = mark_min; 157 | mark_min = base + cursor; 158 | } 159 | } else { 160 | mark_min = base + cursor; 161 | } 162 | } 163 | if (mark_max >= getfilesize()) mark_max = getfilesize() - 1; 164 | markSelectedRegion(); 165 | } 166 | -------------------------------------------------------------------------------- /install-sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # 3 | # install - install a program, script, or datafile 4 | # This comes from X11R5 (mit/util/scripts/install.sh). 5 | # 6 | # Copyright 1991 by the Massachusetts Institute of Technology 7 | # 8 | # Permission to use, copy, modify, distribute, and sell this software and its 9 | # documentation for any purpose is hereby granted without fee, provided that 10 | # the above copyright notice appear in all copies and that both that 11 | # copyright notice and this permission notice appear in supporting 12 | # documentation, and that the name of M.I.T. not be used in advertising or 13 | # publicity pertaining to distribution of the software without specific, 14 | # written prior permission. M.I.T. makes no representations about the 15 | # suitability of this software for any purpose. It is provided "as is" 16 | # without express or implied warranty. 17 | # 18 | # Calling this script install-sh is preferred over install.sh, to prevent 19 | # `make' implicit rules from creating a file called install from it 20 | # when there is no Makefile. 21 | # 22 | # This script is compatible with the BSD install script, but was written 23 | # from scratch. It can only install one file at a time, a restriction 24 | # shared with many OS's install programs. 25 | 26 | 27 | # set DOITPROG to echo to test this script 28 | 29 | # Don't use :- since 4.3BSD and earlier shells don't like it. 30 | doit="${DOITPROG-}" 31 | 32 | 33 | # put in absolute paths if you don't have them in your path; or use env. vars. 34 | 35 | mvprog="${MVPROG-mv}" 36 | cpprog="${CPPROG-cp}" 37 | chmodprog="${CHMODPROG-chmod}" 38 | chownprog="${CHOWNPROG-chown}" 39 | chgrpprog="${CHGRPPROG-chgrp}" 40 | stripprog="${STRIPPROG-strip}" 41 | rmprog="${RMPROG-rm}" 42 | mkdirprog="${MKDIRPROG-mkdir}" 43 | 44 | transformbasename="" 45 | transform_arg="" 46 | instcmd="$mvprog" 47 | chmodcmd="$chmodprog 0755" 48 | chowncmd="" 49 | chgrpcmd="" 50 | stripcmd="" 51 | rmcmd="$rmprog -f" 52 | mvcmd="$mvprog" 53 | src="" 54 | dst="" 55 | dir_arg="" 56 | 57 | while [ x"$1" != x ]; do 58 | case $1 in 59 | -c) instcmd="$cpprog" 60 | shift 61 | continue;; 62 | 63 | -d) dir_arg=true 64 | shift 65 | continue;; 66 | 67 | -m) chmodcmd="$chmodprog $2" 68 | shift 69 | shift 70 | continue;; 71 | 72 | -o) chowncmd="$chownprog $2" 73 | shift 74 | shift 75 | continue;; 76 | 77 | -g) chgrpcmd="$chgrpprog $2" 78 | shift 79 | shift 80 | continue;; 81 | 82 | -s) stripcmd="$stripprog" 83 | shift 84 | continue;; 85 | 86 | -t=*) transformarg=`echo $1 | sed 's/-t=//'` 87 | shift 88 | continue;; 89 | 90 | -b=*) transformbasename=`echo $1 | sed 's/-b=//'` 91 | shift 92 | continue;; 93 | 94 | *) if [ x"$src" = x ] 95 | then 96 | src=$1 97 | else 98 | # this colon is to work around a 386BSD /bin/sh bug 99 | : 100 | dst=$1 101 | fi 102 | shift 103 | continue;; 104 | esac 105 | done 106 | 107 | if [ x"$src" = x ] 108 | then 109 | echo "install: no input file specified" 110 | exit 1 111 | else 112 | true 113 | fi 114 | 115 | if [ x"$dir_arg" != x ]; then 116 | dst=$src 117 | src="" 118 | 119 | if [ -d $dst ]; then 120 | instcmd=: 121 | else 122 | instcmd=mkdir 123 | fi 124 | else 125 | 126 | # Waiting for this to be detected by the "$instcmd $src $dsttmp" command 127 | # might cause directories to be created, which would be especially bad 128 | # if $src (and thus $dsttmp) contains '*'. 129 | 130 | if [ -f $src -o -d $src ] 131 | then 132 | true 133 | else 134 | echo "install: $src does not exist" 135 | exit 1 136 | fi 137 | 138 | if [ x"$dst" = x ] 139 | then 140 | echo "install: no destination specified" 141 | exit 1 142 | else 143 | true 144 | fi 145 | 146 | # If destination is a directory, append the input filename; if your system 147 | # does not like double slashes in filenames, you may need to add some logic 148 | 149 | if [ -d $dst ] 150 | then 151 | dst="$dst"/`basename $src` 152 | else 153 | true 154 | fi 155 | fi 156 | 157 | ## this sed command emulates the dirname command 158 | dstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'` 159 | 160 | # Make sure that the destination directory exists. 161 | # this part is taken from Noah Friedman's mkinstalldirs script 162 | 163 | # Skip lots of stat calls in the usual case. 164 | if [ ! -d "$dstdir" ]; then 165 | defaultIFS=' 166 | ' 167 | IFS="${IFS-${defaultIFS}}" 168 | 169 | oIFS="${IFS}" 170 | # Some sh's can't handle IFS=/ for some reason. 171 | IFS='%' 172 | set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'` 173 | IFS="${oIFS}" 174 | 175 | pathcomp='' 176 | 177 | while [ $# -ne 0 ] ; do 178 | pathcomp="${pathcomp}${1}" 179 | shift 180 | 181 | if [ ! -d "${pathcomp}" ] ; 182 | then 183 | $mkdirprog "${pathcomp}" 184 | else 185 | true 186 | fi 187 | 188 | pathcomp="${pathcomp}/" 189 | done 190 | fi 191 | 192 | if [ x"$dir_arg" != x ] 193 | then 194 | $doit $instcmd $dst && 195 | 196 | if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi && 197 | if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi && 198 | if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi && 199 | if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi 200 | else 201 | 202 | # If we're going to rename the final executable, determine the name now. 203 | 204 | if [ x"$transformarg" = x ] 205 | then 206 | dstfile=`basename $dst` 207 | else 208 | dstfile=`basename $dst $transformbasename | 209 | sed $transformarg`$transformbasename 210 | fi 211 | 212 | # don't allow the sed command to completely eliminate the filename 213 | 214 | if [ x"$dstfile" = x ] 215 | then 216 | dstfile=`basename $dst` 217 | else 218 | true 219 | fi 220 | 221 | # Make a temp file name in the proper directory. 222 | 223 | dsttmp=$dstdir/#inst.$$# 224 | 225 | # Move or copy the file name to the temp name 226 | 227 | $doit $instcmd $src $dsttmp && 228 | 229 | trap "rm -f ${dsttmp}" 0 && 230 | 231 | # and set any options; do chmod last to preserve setuid bits 232 | 233 | # If any of these fail, we abort the whole thing. If we want to 234 | # ignore errors from any of these, just make sure not to ignore 235 | # errors from the above "$doit $instcmd $src $dsttmp" command. 236 | 237 | if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi && 238 | if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi && 239 | if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi && 240 | if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi && 241 | 242 | # Now rename the file to the real destination. 243 | 244 | $doit $rmcmd -f $dstdir/$dstfile && 245 | $doit $mvcmd $dsttmp $dstdir/$dstfile 246 | 247 | fi && 248 | 249 | 250 | exit 0 251 | -------------------------------------------------------------------------------- /hexedit.h: -------------------------------------------------------------------------------- 1 | #ifndef HEXEDIT_H 2 | #define HEXEDIT_H 3 | 4 | #include "config.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #if HAVE_FCNTL_H 11 | #include 12 | #endif 13 | #if HAVE_UNISTD_H 14 | #include 15 | #endif 16 | #include 17 | #include 18 | #if HAVE_SYS_WAIT_H 19 | #include 20 | #endif 21 | #include 22 | #if HAVE_LIBGEN_H 23 | #include 24 | #endif 25 | #if HAVE_SYS_MOUNT_H 26 | #include /* for BLKGETSIZE */ 27 | #endif 28 | 29 | 30 | #define INT off_t 31 | 32 | /*******************************************************************************/ 33 | /* Macros */ 34 | /*******************************************************************************/ 35 | #define BIGGEST_COPYING (1 * 1024 * 1024) 36 | #define BLOCK_SEARCH_SIZE (4 * 1024) 37 | #define SECTOR_SIZE ((INT) 512) 38 | #ifndef CTRL 39 | #define CTRL(c) ((c) & 0x1F) 40 | #endif 41 | #define ALT(c) ((c) | 0xa0) 42 | #define DIE(M) { exitCurses(); fprintf(stderr, M, progName); exit(1); } 43 | #define FREE(p) if (p) free(p) 44 | #ifndef MIN 45 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) 46 | #endif 47 | #ifndef MAX 48 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) 49 | #endif 50 | #define NORMAL A_NORMAL 51 | #define MARKED A_REVERSE 52 | #define MODIFIED A_BOLD 53 | #define ATTRPRINTW(attr, a) do { if (oldattr != (attr)) attrset(attr), oldattr = (attr); printw a; } while (0) 54 | #define MAXATTRPRINTW(attr, a) do { if (oldattr & (~(attr))) attrset(attr & oldattr), oldattr &= (attr); printw a; } while (0) 55 | #define PRINTW(a) ATTRPRINTW(NORMAL, a) 56 | #ifndef getnstr 57 | #define getnstr(str, n) wgetnstr(stdscr, str, n) 58 | #endif 59 | 60 | 61 | /*******************************************************************************/ 62 | /* Configuration parameters */ 63 | /*******************************************************************************/ 64 | typedef enum { bySector, maximized, LAST } modeType; 65 | typedef struct { 66 | int blocSize, lineLength, page; 67 | } modeParams; 68 | 69 | extern const modeParams modes[LAST]; 70 | extern modeType mode; 71 | extern int colored; 72 | extern const char * const usage; 73 | 74 | #define pressAnyKey "(press any key)" 75 | 76 | 77 | /*******************************************************************************/ 78 | /* Pages handling */ 79 | /*******************************************************************************/ 80 | typedef struct _typePage { 81 | struct _typePage *next; 82 | 83 | INT base; 84 | int size; 85 | unsigned char *vals; 86 | } typePage; 87 | 88 | 89 | /*******************************************************************************/ 90 | /* Global variables */ 91 | /*******************************************************************************/ 92 | 93 | extern INT lastEditedLoc, biggestLoc, fileSize; 94 | extern INT mark_min, mark_max, mark_set; 95 | extern INT base, oldbase; 96 | extern int normalSpaces, cursor, cursorOffset, hexOrAscii; 97 | extern int cursor, blocSize, lineLength, colsUsed, page; 98 | extern int isReadOnly, fd, nbBytes, oldcursor, oldattr, oldcursorOffset; 99 | extern int nAddrDigits; 100 | extern int sizeCopyBuffer, *bufferAttr; 101 | extern char *progName, *fileName, *baseName; 102 | extern unsigned char *buffer, *copyBuffer; 103 | extern typePage *edited; 104 | 105 | extern char *lastFindFile, *lastYankToAFile, *lastAskHexString, *lastAskAsciiString, *lastFillWithStringHexa, *lastFillWithStringAscii; 106 | 107 | 108 | /*******************************************************************************/ 109 | /* Miscellaneous functions declaration */ 110 | /*******************************************************************************/ 111 | INT getfilesize(void); 112 | int key_to_function(int key); 113 | void init(void); 114 | void quit(void); 115 | int tryloc(INT loc); 116 | void openFile(void); 117 | void readFile(void); 118 | int findFile(void); 119 | int computeLineSize(void); 120 | int computeCursorXCurrentPos(void); 121 | int computeCursorXPos(int cursor, int hexOrAscii); 122 | void updateMarked(void); 123 | int ask_about_save(void); 124 | int ask_about_save_and_redisplay(void); 125 | void ask_about_save_and_quit(void); 126 | int setTo(int c); 127 | void setToChar(int i, unsigned char c); 128 | 129 | /*******************************************************************************/ 130 | /* Pages handling functions declaration */ 131 | /*******************************************************************************/ 132 | void discardEdited(void); 133 | void addToEdited(INT base, int size, unsigned char *vals); 134 | void removeFromEdited(INT base, int size); 135 | typePage *newPage(INT base, int size); 136 | void freePage(typePage *page); 137 | 138 | 139 | /*******************************************************************************/ 140 | /* Cursor manipulation function declarations */ 141 | /*******************************************************************************/ 142 | int move_cursor(INT delta); 143 | int set_cursor(INT loc); 144 | int move_base(INT delta); 145 | int set_base(INT loc); 146 | 147 | /*******************************************************************************/ 148 | /* Curses functions declaration */ 149 | /*******************************************************************************/ 150 | void initCurses(void); 151 | void initDisplay(void); 152 | void exitCurses(void); 153 | void display(void); 154 | void displayLine(int offset, int max); 155 | void clr_line(int line); 156 | void displayCentered(const char *msg, int line); 157 | void displayOneLineMessage(const char *msg); 158 | void displayTwoLineMessage(const char *msg1, const char *msg2); 159 | void displayMessageAndWaitForKey(const char *msg); 160 | int displayMessageAndGetString(const char *msg, char **last, char *p, int p_size); 161 | void ungetstr(char *s); 162 | int get_number(INT *i); 163 | 164 | 165 | /*******************************************************************************/ 166 | /* Search functions declaration */ 167 | /*******************************************************************************/ 168 | void search_forward(void); 169 | void search_backward(void); 170 | 171 | 172 | /*******************************************************************************/ 173 | /* Mark functions declaration */ 174 | /*******************************************************************************/ 175 | void markRegion(INT a, INT b); 176 | void unmarkRegion(INT a, INT b); 177 | void markSelectedRegion(void); 178 | void unmarkAll(void); 179 | void markIt(int i); 180 | void unmarkIt(int i); 181 | void copy_region(void); 182 | void yank(void); 183 | void yank_to_a_file(void); 184 | void fill_with_string(void); 185 | 186 | 187 | /*******************************************************************************/ 188 | /* Small common functions declaration */ 189 | /*******************************************************************************/ 190 | int streq(const char *s1, const char *s2); 191 | int strbeginswith(const char *a, const char *prefix); 192 | int myceil(int a, int b); 193 | INT myfloor(INT a, INT b); 194 | int setLowBits(int p, int val); 195 | int setHighBits(int p, int val); 196 | char *strconcat3(char *a, char *b, char *c); 197 | int hexCharToInt(int c); 198 | int not(int b); 199 | char *mymemmem(char *a, int sizea, char *b, int sizeb); 200 | char *mymemrmem(char *a, int sizea, char *b, int sizeb); 201 | int is_file(char *name); 202 | int hexStringToBinString(char *p, int *l); 203 | 204 | /*******************************************************************************/ 205 | /* Functions provided for OSs that don't have them */ 206 | /*******************************************************************************/ 207 | void LSEEK(int fd, INT where); 208 | int LSEEK_(int fd, INT where); 209 | 210 | #ifndef HAVE_DECL_MEMRCHR 211 | char *memrchr(void *s, char c, int n); 212 | #endif 213 | 214 | #ifndef HAVE_DECL_MEMMEM 215 | void *memmem(void *a, size_t sizea, void *b, size_t sizeb); 216 | #endif 217 | 218 | #ifndef HAVE_DECL_MEMRMEM 219 | void *memrmem(void *a, size_t sizea, void *b, size_t sizeb); 220 | #endif 221 | 222 | #ifndef HAVE_DECL_BASENAME 223 | char *basename(const char *file); 224 | #endif 225 | 226 | #ifndef HAVE_STRERROR 227 | char *strerror(int errnum); 228 | #endif 229 | 230 | #ifndef HAVE_STRDUP 231 | char *strdup(const char *str) 232 | #endif 233 | 234 | #ifndef HAVE_MEMCMP 235 | #define memcmp(s1, s2, n) bcmp(s2, s1, n) 236 | #endif 237 | 238 | #endif /* HEXEDIT_H */ 239 | -------------------------------------------------------------------------------- /Changes: -------------------------------------------------------------------------------- 1 | ## [Unreleased] (date) 2 | - 3 | 4 | ## 1.6 (03/2022) 5 | - configure script must error-out when (n)curses isn't found 6 | - Move SIGWINCH handling from handler to NCURSES 7 | - Make configure.ac compliant with autoconf >= 2.70 8 | - Check that malloc did not return NULL 9 | - Fix a possible memory leak in findFile() 10 | - Prevent division by zero on empty files. 11 | 12 | ## 1.5 (08/2020) 13 | - Fix searching very long string 14 | - Keep status of firstTime beween calls. (#35) 15 | - man page: use simple double quotes (instead of weird ```xxx''') 16 | - Properly handle off_t on non-lfs 32 bit systems. 17 | - Fixed compiler warnings. 18 | - Do not allow negative line length. (#34) 19 | - fix overflow on excessively long escape sequence. (#32) 20 | - Ignore KEY_RESIZE in main loop. (#33) 21 | - Show percentage of cursor relative to filesize. 22 | - Bug copy & paste (#29) 23 | 24 | ## 1.4.2 (09/2017) 25 | - fix spelling errors in manpage 26 | - fix build 27 | 28 | ## 1.4 (09/2017) 29 | - terminal resizing support 30 | - fix build 31 | 32 | ## (11/2016) 33 | - mark cursor position in both HEX and ASCII 34 | - skip whitespace when parsing hex-strings 35 | 36 | ## (03/2014) 37 | - fix small but in DIE() which was leaving the terminal in a bad state 38 | - added --linelength / -l command line option 39 | 40 | ## (02/2013) 41 | - fix displaying sector number when above 2^31 42 | - fix potential file descriptor leak (thanks to Rich Burridge) 43 | - add DESTDIR support to the makefiles 44 | - preprocessor flags should use CPPFLAGS, not CFLAGS 45 | - fix a small issue in mymemmem/mymemrmem when HAVE_MEMMEM/HAVE_MEMRMEM is not defined 46 | 47 | ## 1.2.12 (09/2005) 48 | - colored (fruit salad) display built by default (if use_default_colors available), 49 | available through option --color (based on a patch from Yuri D'Elia) 50 | 51 | ## 1.2.11 (09/2005) 52 | - add --enable-colors to build and have a colored (fruit salad) display 53 | - allow entering goto "position" and "sector" in hexadecimal or not 54 | - when prompting, propose previously entered value, and give ability to modify it 55 | - fix setting mark after scrolling 56 | - some cleanup 57 | 58 | ## 1.2.10 (05/2004) 59 | - handle getnstr() not defined (needed for SGI IRIX 6.5.22) 60 | 61 | ## 1.2.9 (04/2004) 62 | - version 1.2.9 (brown paper bag version) 63 | - fix searching larger than 3 characters long strings 64 | 65 | ## 1.2.8 (01/2004) 66 | - replace the unsafe getstr() with getnstr() (thanks to Richard Chadderton) 67 | 68 | ## 1.2.7 (08/2003) 69 | - check the presence of before including it 70 | - fix build on Digital UNIX 4.0f (remove -Wall) 71 | - use the full width of the terminal (the trick is to stop relying on 72 | printing "\n", but using move() instead) 73 | - fix displaying the offsets at beginning of each line on big endian 74 | boxes (thanks to Martin Schaffner) 75 | 76 | ## 1.2.6 (06/2003) 77 | - fix core dump when searching backward (thanks to Jean Delvare) 78 | (the culprit is memrmem, but also fixing provided memrchr) 79 | 80 | ## 1.2.5 (06/2003) 81 | - fix build on Solaris (which doesn't have BLKGETSIZE and all compilers don't have -W) 82 | 83 | ## 1.2.4 (03/2003) 84 | - use BLKGETSIZE to try to get the size of a device 85 | - do not die horribly when accessing after the end 86 | - fix provided memrchr() (thanks to Yue Luo) 87 | - fix displaying after searching >32bit files (thanks to Paul Mattal) 88 | 89 | ## 1.2.3 (06/2002) 90 | - add some KEY_HOME KEY_END (^[[7~ and ^[[8~) 91 | - fix for HAVE_MEMMEM 92 | - fix my memrchr not behaving the same as libc's 93 | - fix the use of memrchr to behave as libc's 94 | - use "(void)" in prototype of functions having no parameters 95 | - call "raw()" when help() is over 96 | 97 | ## 1.2.2 (10/2001) 98 | - yet again some s/int/INT/ (mainly in the "edited" list manipulation) 99 | 100 | ## 1.2.1 (06/2001) 101 | - replace llseek with lseek + use of _FILE_OFFSET_BITS=64 102 | - memrchr&memmem now exists in some glibc's, so define it only if not provided 103 | - cleanup basename declaration 104 | - various cleanup in configure.in, now using autoheader 105 | - a few bug fixes 106 | 107 | ## 1.1.1 (04/1999) 108 | - replaced lots of int by INT which 64bits 109 | - replaced lseek by llseek (!! portability=linux :( !!) 110 | 111 | ## (11/01/1999) 112 | - (:-<>, it's been a long time) 113 | - added many shortcuts so that F1..F12 and some others works on more 114 | terms (xterm, rxvt, sgi's winterm...) 115 | - removed the scanw that's badly handled on sgi (replaced by getstr + 116 | sscanf) 117 | 118 | ## (24/09/1998) 119 | - now displays the reached offset while searching 120 | 121 | ## 1.0.0 (10/09/1998) 122 | - hexedit 1.0.0 release 123 | 124 | ## (08/09/1998) 125 | - Oliver Kiddle's changes: 126 | - changed code where a function that returns a void was returning the 127 | result of another function. This avoids errors in stricter compilers 128 | - used an if where ?: was being used to the left of an assignment 129 | - created Makefile.in and configure.in to replace Makefile 130 | - added alternative strdup, strerror and memcmp for lacking systems 131 | 132 | ## 0.9.5 (26/08/1998) 133 | - hexedit 0.9.5 release 134 | 135 | ## (26/08/1998) 136 | - removed the total file size in modeline for devices 137 | 138 | ## (19/08/1998) 139 | - Evin Robertson's changes: 140 | - split hexedit.c into display.c, file.c, hexedit.c, hexedit.h, 141 | interact.c, mark.c, misc.c, page.c, search.c 142 | - Makefile changes - now defaults to installing in /usr/local/bin 143 | - scrolls one line at a time (but not in sector mode) 144 | - allow appending to the file 145 | - errors during writing are now non-fatal 146 | - show total file size in modeline (not really correct for devices) 147 | - gives better error messages when functions return -1 148 | - added newlines to the end of fatal error messages 149 | - changed sizeof(bufferAttr) to sizeof(*bufferAttr) 150 | - grammatical corrections to the man page 151 | - removed dashes so the display is 16 bytes wide on an 80 column 152 | screen 153 | - backspace now moves back an entire byte in hex mode (I thought the 154 | previous behavior was inconsistent; it moved the cursor one 155 | nybble, but undid an entire byte) 156 | - refuse to open directories 157 | - disallow using return to go past the end of the file 158 | - added truncating ability (M-T) 159 | 160 | ## (07/08/1998) 161 | - ability to fill the selection with a string 162 | 163 | ## (06/08/1998) 164 | - forgot to mark all the things while doing them. Here is a list of 165 | what's been added: 166 | - make the display start at the current cursor position 167 | - go to a sector when in --sector mode 168 | - added a modeline 169 | - ability to open another file. Nice for copy&paste between files 170 | - you're no more forced to save the file per page. This means the 171 | cut&paste can now always be undone. It's much simplier that way (idea 172 | from Evin C Robertson, thanks) 173 | - the backspace acts as a small undo of the previous character. It's 174 | nice when typing (another idea from Evin C Robertson) 175 | - the modified-unmodified bytes are not shown in bold (eg: you replace 176 | 'a' by 'a') 177 | 178 | ## (26/07/1998) 179 | - added a TODO file 180 | 181 | ## (26/07/1998) 182 | - added save copied region in a file 183 | 184 | ## (26/07/1998) 185 | - added search backward 186 | 187 | ## (25/07/1998) 188 | - added copy&paste functions 189 | 190 | ## (25/07/1998) 191 | - you can now select a part of the buffer 192 | 193 | ## (21/07/1998) 194 | - modified bytes are shown in bold. 195 | 196 | ## (21/07/1998) 197 | - corrected a bug, when you call function goto_char and the file is 198 | modified, you're now asked if you want to save (before that the changes 199 | were lost). You're only asked if the goto_char gets you to a different 200 | page of the file. 201 | 202 | ## (21/07/1998) 203 | - replace the searchInBuffer function by memmem. Then replaced memmem 204 | by mymemmem to counter the libc bug. All this permits a good speedup 205 | when searching. 206 | 207 | ## (21/07/1998) 208 | - removed the bug in the goto_char function. Now giving an empty 209 | string leaves the cursor unmoved. A small bug remains if you give an 210 | invalid hexadecimal string starting legally. The scanw(3ncurses) 211 | function returns a number corresponding at the legal beginning substring 212 | (eg: 1z2 becomes 1). 213 | 214 | ## (21/07/1998) 215 | - added an install entry in the Makefile (the one Robert Woodcock 216 | made for the debian package, thanks Robert Woodcock). 217 | 218 | ## (21/07/1998) 219 | - creation of the Changes file 220 | -------------------------------------------------------------------------------- /display.c: -------------------------------------------------------------------------------- 1 | /* hexedit -- Hexadecimal Editor for Binary Files 2 | Copyright (C) 1998 Pixel (Pascal Rigaux) 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2, or (at your option) 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/ 17 | #include "hexedit.h" 18 | 19 | int move_cursor(INT delta) 20 | { 21 | return set_cursor(base + cursor + delta); 22 | } 23 | 24 | int set_cursor(INT loc) 25 | { 26 | if (loc < 0 && base % lineLength) 27 | loc = 0; 28 | 29 | if (!tryloc(loc)) 30 | return FALSE; 31 | 32 | if (loc < base) { 33 | if (loc - base % lineLength < 0) 34 | set_base(0); 35 | else if (!move_base(myfloor(loc - base % lineLength, lineLength) + base % lineLength - base)) 36 | return FALSE; 37 | cursor = loc - base; 38 | } else if (loc >= base + page) { 39 | if (!move_base(myfloor(loc - base % lineLength, lineLength) + base % lineLength - page + lineLength - base)) 40 | return FALSE; 41 | cursor = loc - base; 42 | } else if (loc > base + nbBytes) { 43 | return FALSE; 44 | } else 45 | cursor = loc - base; 46 | 47 | if (mark_set) 48 | updateMarked(); 49 | 50 | return TRUE; 51 | } 52 | 53 | int move_base(INT delta) 54 | { 55 | if (mode == bySector) { 56 | if (delta > 0 && delta < page) 57 | delta = page; 58 | else if (delta < 0 && delta > -page) 59 | delta = -page; 60 | } 61 | return set_base(base + delta); 62 | } 63 | 64 | int set_base(INT loc) 65 | { 66 | if (loc < 0) loc = 0; 67 | 68 | if (!tryloc(loc)) return FALSE; 69 | base = loc; 70 | readFile(); 71 | 72 | if (mode != bySector && nbBytes < page - lineLength && base != 0) { 73 | base -= myfloor(page - nbBytes - lineLength, lineLength); 74 | if (base < 0) base = 0; 75 | readFile(); 76 | } 77 | 78 | if (cursor > nbBytes) cursor = nbBytes; 79 | return TRUE; 80 | } 81 | 82 | 83 | int computeLineSize(void) { return computeCursorXPos(lineLength - 1, 0) + 1; } 84 | int computeCursorXCurrentPos(void) { return computeCursorXPos(cursor, hexOrAscii); } 85 | int computeCursorXPos(int cursor, int hexOrAscii) 86 | { 87 | int r = nAddrDigits + 3; 88 | int x = cursor % lineLength; 89 | int h = (hexOrAscii ? x : lineLength - 1); 90 | 91 | r += normalSpaces * (h % blocSize) + (h / blocSize) * (normalSpaces * blocSize + 1) + (hexOrAscii && cursorOffset); 92 | 93 | if (!hexOrAscii) r += x + normalSpaces + 1; 94 | 95 | return r; 96 | } 97 | 98 | 99 | 100 | /*******************************************************************************/ 101 | /* Curses functions */ 102 | /*******************************************************************************/ 103 | 104 | void initCurses(void) 105 | { 106 | initscr(); 107 | 108 | #ifdef HAVE_COLORS 109 | if (colored) { 110 | start_color(); 111 | use_default_colors(); 112 | init_pair(1, COLOR_RED, -1); /* null zeros */ 113 | init_pair(2, COLOR_GREEN, -1); /* control chars */ 114 | init_pair(3, COLOR_BLUE, -1); /* extended chars */ 115 | } 116 | init_pair(4, COLOR_BLUE, COLOR_YELLOW); /* current cursor position*/ 117 | #endif 118 | 119 | initDisplay(); 120 | } 121 | 122 | void initDisplay(void) 123 | { 124 | refresh(); 125 | raw(); 126 | noecho(); 127 | keypad(stdscr, TRUE); 128 | 129 | if (mode == bySector) { 130 | lineLength = modes[bySector].lineLength; 131 | page = modes[bySector].page; 132 | page = myfloor((LINES - 1) * lineLength, page); 133 | blocSize = modes[bySector].blocSize; 134 | if (computeLineSize() > COLS) DIE("%s: term is too small for sectored view (width)\n"); 135 | if (page == 0) DIE("%s: term is too small for sectored view (height)\n"); 136 | } else { /* mode == maximized */ 137 | if (LINES <= 4) DIE("%s: term is too small (height)\n"); 138 | 139 | blocSize = modes[maximized].blocSize; 140 | if (lineLength == 0) { 141 | /* put as many "blocSize" as can fit on a line */ 142 | for (lineLength = blocSize; computeLineSize() <= COLS; lineLength += blocSize); 143 | lineLength -= blocSize; 144 | if (lineLength == 0) DIE("%s: term is too small (width)\n"); 145 | } else { 146 | if (computeLineSize() > COLS) 147 | DIE("%s: term is too small (width) for selected line length\n"); 148 | } 149 | page = lineLength * (LINES - 1); 150 | } 151 | colsUsed = computeLineSize(); 152 | buffer = realloc(buffer,page); 153 | bufferAttr = realloc(bufferAttr,page * sizeof(*bufferAttr)); 154 | } 155 | 156 | void exitCurses(void) 157 | { 158 | close(fd); 159 | clear(); 160 | refresh(); 161 | endwin(); 162 | } 163 | 164 | static void printaddr(uint64_t addr) 165 | { 166 | char templ[7]; // maximum string is "%016lX", which is 6 chars + 1 null byte 167 | sprintf(templ,"%%0%dlX", nAddrDigits); 168 | PRINTW((templ, addr)); 169 | } 170 | 171 | void display(void) 172 | { 173 | long long fsize; 174 | int i; 175 | 176 | for (i = 0; i < nbBytes; i += lineLength) { 177 | move(i / lineLength, 0); 178 | displayLine(i, nbBytes); 179 | } 180 | for (; i < page; i += lineLength) { 181 | int j; 182 | move(i / lineLength, 0); 183 | for (j = 0; j < colsUsed; j++) printw(" "); /* cleanup the line */ 184 | move(i / lineLength, 0); 185 | printaddr(base+i); 186 | } 187 | 188 | attrset(NORMAL); 189 | move(LINES - 1, 0); 190 | for (i = 0; i < colsUsed; i++) printw("-"); 191 | move(LINES - 1, 0); 192 | if (isReadOnly) i = '%'; 193 | else if (edited) i = '*'; 194 | else i = '-'; 195 | printw("-%c%c %s --0x%llX", i, i, baseName, (long long) base + cursor); 196 | fsize = getfilesize(); 197 | if (MAX(fileSize, lastEditedLoc)) printw("/0x%llX", fsize); 198 | printw("--%i%%", fsize == 0 ? 0 : 100 * (base + cursor + fsize/200) / fsize ); 199 | if (mode == bySector) printw("--sector %lld", (long long) ((base + cursor) / SECTOR_SIZE)); 200 | 201 | move(cursor / lineLength, computeCursorXCurrentPos()); 202 | } 203 | 204 | void displayLine(int offset, int max) 205 | { 206 | int i,mark_color=0; 207 | #ifdef HAVE_COLORS 208 | mark_color = COLOR_PAIR(4) | A_BOLD; 209 | #endif 210 | printaddr(base + offset); 211 | PRINTW((" ")); 212 | for (i = offset; i < offset + lineLength; i++) { 213 | if (i > offset) MAXATTRPRINTW(bufferAttr[i] & MARKED, (((i - offset) % blocSize) ? " " : " ")); 214 | if (i < max) { 215 | ATTRPRINTW( 216 | #ifdef HAVE_COLORS 217 | (!colored ? 0 : 218 | (cursor == i && hexOrAscii == 0 ? mark_color : 219 | buffer[i] == 0 ? (int) COLOR_PAIR(1) : 220 | buffer[i] < ' ' ? (int) COLOR_PAIR(2) : 221 | buffer[i] >= 127 ? (int) COLOR_PAIR(3) : 0) 222 | ) | 223 | #endif 224 | bufferAttr[i], ("%02X", buffer[i])); 225 | } 226 | else PRINTW((" ")); 227 | } 228 | PRINTW((" ")); 229 | for (i = offset; i < offset + lineLength; i++) { 230 | if (i >= max) PRINTW((" ")); 231 | else if (buffer[i] >= ' ' && buffer[i] < 127) ATTRPRINTW((cursor == i && hexOrAscii==1 ? mark_color : 0) | bufferAttr[i], ("%c", buffer[i])); 232 | else ATTRPRINTW((cursor == i && hexOrAscii == 1 ? mark_color : 0) | bufferAttr[i], (".")); 233 | } 234 | } 235 | 236 | void clr_line(int line) { move(line, 0); clrtoeol(); } 237 | 238 | void displayCentered(const char *msg, int line) 239 | { 240 | clr_line(line); 241 | move(line, (COLS - strlen(msg)) / 2); 242 | PRINTW(("%s", msg)); 243 | } 244 | 245 | void displayOneLineMessage(const char *msg) 246 | { 247 | int center = page / lineLength / 2; 248 | clr_line(center - 1); 249 | clr_line(center + 1); 250 | displayCentered(msg, center); 251 | } 252 | 253 | void displayTwoLineMessage(const char *msg1, const char *msg2) 254 | { 255 | int center = page / lineLength / 2; 256 | clr_line(center - 2); 257 | clr_line(center + 1); 258 | displayCentered(msg1, center - 1); 259 | displayCentered(msg2, center); 260 | } 261 | 262 | void displayMessageAndWaitForKey(const char *msg) 263 | { 264 | displayTwoLineMessage(msg, pressAnyKey); 265 | getch(); 266 | } 267 | 268 | int displayMessageAndGetString(const char *msg, char **last, char *p, int p_size) 269 | { 270 | int ret = TRUE; 271 | 272 | displayOneLineMessage(msg); 273 | ungetstr(*last); 274 | echo(); 275 | getnstr(p, p_size - 1); 276 | noecho(); 277 | if (*p == '\0') { 278 | if (*last) strcpy(p, *last); else ret = FALSE; 279 | } else { 280 | FREE(*last); 281 | *last = strdup(p); 282 | } 283 | return ret; 284 | } 285 | 286 | void ungetstr(char *s) 287 | { 288 | char *p; 289 | if (s) 290 | for (p = s + strlen(s) - 1; p >= s; p--) ungetch(*p); 291 | } 292 | 293 | int get_number(INT *i) 294 | { 295 | unsigned long long n; 296 | int err; 297 | char tmp[BLOCK_SEARCH_SIZE]; 298 | echo(); 299 | getnstr(tmp, BLOCK_SEARCH_SIZE - 1); 300 | noecho(); 301 | if (strbeginswith(tmp, "0x")) 302 | err = sscanf(tmp + strlen("0x"), "%llx", &n); 303 | else 304 | err = sscanf(tmp, "%llu", &n); 305 | *i = (off_t)n; 306 | if (*i < 0 || n != (unsigned long long) *i) 307 | err = 0; 308 | return err == 1; 309 | } 310 | -------------------------------------------------------------------------------- /hexedit.1.in: -------------------------------------------------------------------------------- 1 | \# no table of contents: 2 | .HX 0 3 | .TH HEXEDIT 1 "12 July 1998" 4 | .SH NAME 5 | hexedit \- view and edit files in hexadecimal or in ASCII 6 | .SH SYNOPSIS 7 | .I hexedit 8 | [\-s | \-\-sector] [\-m | \-\-maximize] [\-l | \-\-linelength ] [\-h | \-\-help] [filename] 9 | .SH DESCRIPTION 10 | .LP 11 | .I hexedit 12 | shows a file both in ASCII and in hexadecimal. The file can be a device 13 | as the file is read a piece at a time. You can modify the file and search through it. 14 | .SH OPTIONS 15 | .TP 16 | .I "\-s, \-\-sector" 17 | Format the display to have entire sectors. 18 | .TP 19 | .I "\-m, \-\-maximize" 20 | Try to maximize the display. 21 | .TP 22 | .I "\-\-color" 23 | Display colors. 24 | This feature is only available if your operating system supports it. 25 | .TP 26 | .I "\-l, \-\-linelength " 27 | Explicitly set the number of bytes to display per line to . 28 | .TP 29 | .I "\-h, \-\-help" 30 | Show the usage. 31 | .SH COMMANDS (quickly) 32 | .SS Moving 33 | .nf 34 | <, > : go to start/end of the file 35 | Right: next character 36 | Left: previous character 37 | Down: next line 38 | Up: previous line 39 | Home: beginning of line 40 | End: end of line 41 | PUp: page forward 42 | PDown: page backward 43 | .fi 44 | .SS Miscellaneous 45 | .nf 46 | F2: save 47 | F3: load file 48 | F1: help 49 | Ctrl-L: redraw 50 | Ctrl-Z: suspend 51 | Ctrl-X: save and exit 52 | Ctrl-C: exit without saving 53 | 54 | Tab: toggle hex/ascii 55 | Return: go to 56 | Backspace: undo previous character 57 | Ctrl-U: undo all 58 | Ctrl-S: search forward 59 | Ctrl-R: search backward 60 | .fi 61 | .SS Cut&Paste 62 | .nf 63 | Ctrl-Space: set mark 64 | Esc-W: copy 65 | Ctrl-Y: paste 66 | Esc-Y: paste into a file 67 | Esc-I: fill 68 | .fi 69 | .SH COMMANDS (full and detailed) 70 | o \fIRight-Arrow\fR, \fILeft-Arrow\fR, \fIDown-Arrow\fR, \fIUp-Arrow\fR \- move the cursor. 71 | .br 72 | o \fICtrl+F\fR, \fICtrl+B\fR, \fICtrl+N\fR, \fICtrl+P\fR \- move the cursor. 73 | .br 74 | o \fICtrl+Right-Arrow\fR, \fICtrl+Left-Arrow\fR, \fICtrl+Down-Arrow\fR, \fICtrl+Up-Arrow\fR \- move n times the cursor. 75 | .br 76 | o \fIEsc+Right-Arrow\fR, \fIEsc+Left-Arrow\fR, \fIEsc+Down-Arrow\fR, \fIEsc+Up-Arrow\fR \- move n times the cursor. 77 | .br 78 | o \fIEsc+F\fR, \fIEsc+B\fR, \fIEsc+N\fR, \fIEsc+P\fR \- move n times the cursor. 79 | .br 80 | o \fIHome\fR, \fICtrl+A\fR \- go the beginning of the line. 81 | .br 82 | o \fIEnd\fR, \fICtrl+E\fR \- go to the end of the line. 83 | .br 84 | o \fIPage up\fR, \fIEsc+V\fR, \fIF5\fR \- go up in the file by one page. 85 | .br 86 | o \fIPage down\fR, \fICtrl+V\fR, \fIF6\fR \- go down in the file by one page. 87 | .br 88 | o \fI<\fR, \fIEsc+<\fR, \fIEsc+Home\fR \- go to the beginning of the file. 89 | .br 90 | o \fI>\fR, \fIEsc+>\fR, \fIEsc+End\fR \- go to the end of the file (for regular files that have a size). 91 | .br 92 | o \fICtrl+Z\fR \- suspend hexedit. 93 | .br 94 | o \fICtrl+U\fR, \fICtrl+_\fR, \fICtrl+/\fR \- undo all (forget the modifications). 95 | .br 96 | o \fICtrl+Q\fR \- read next input character and insert it (this is useful for 97 | inserting control characters and bound keys). 98 | .br 99 | o \fITab\fR, \fICtrl+T\fR \- toggle between ASCII and hexadecimal. 100 | .br 101 | o \fI/\fR, \fICtrl+S\fR \- search forward (in ASCII or in hexadecimal, use \fITAB\fR to change). 102 | .br 103 | o \fICtrl+R\fR \- search backward. 104 | .br 105 | o \fICtrl+G\fR, \fIF4\fR \- go to a position in the file. 106 | .br 107 | o \fIReturn\fR \- go to a sector in the file if \fI\-\-sector\fR is used, otherwise go 108 | to a position in the file. 109 | .br 110 | o \fIEsc+L\fR \- display the page starting at the current cursor position. 111 | .br 112 | o \fIF2\fR, \fICtrl+W\fR \- save the modifications. 113 | .br 114 | o \fIF1\fR, \fIEsc+H\fR \- help (show the man page). 115 | .br 116 | o \fICtrl+O\fR, \fIF3\fR \- open another file 117 | .br 118 | o \fICtrl+L\fR \- redisplay (refresh) the display (useful when your terminal screws up). 119 | .br 120 | o \fIBackspace\fR, \fICtrl+H\fR \- undo the modifications made on the previous byte. 121 | .br 122 | o \fIEsc+Ctrl+H\fR \- undo the modifications made on the previous bytes. 123 | .br 124 | o \fICtrl+Space\fR, \fIF9\fR \- set mark where cursor is. 125 | .br 126 | o \fIEsc+W\fR, \fIDelete\fR, \fIF7\fR \- copy selected region. 127 | .br 128 | o \fICtrl+Y\fR, \fIInsert\fR, \fIF8\fR \- paste (yank) previously copied region. 129 | .br 130 | o \fIEsc+Y\fR, \fIF11\fR \- save previously copied region to a file. 131 | .br 132 | o \fIEsc+I\fR, \fIF12\fR \- fill the selection with a string 133 | .br 134 | o \fIEsc+T\fR \- truncate the file at the current location 135 | .br 136 | o \fICtrl+C\fR \- unconditional quit (without saving). 137 | .br 138 | o \fIF10\fR, \fICtrl+X\fR \- quit. 139 | .PP 140 | For the \fIEsc\fR commands, it sometimes works to use \fIAlt\fR instead of 141 | \fIEsc\fR. Funny things here (especially for froggies :) egrave = Alt+H , 142 | ccedilla = Alt+G, Alt+Y = ugrave. 143 | .br 144 | .SS Modeline 145 | At the bottom of the display you have the modeline (copied from emacs). As in 146 | emacs, you have the indications --, ** and %% meaning unmodified, modified and 147 | read-only. Then you have the name of the file you're currently editing. Next to 148 | it is the current position of the cursor in the file followed by the total file 149 | size. The total file size isn't quite correct for devices. 150 | .br 151 | While in --sector mode, it shows the sector the cursor is in. 152 | .SS Editing 153 | You can edit in ASCII or in hexadecimal. You can switch between the two with 154 | \fITab\fR. When the file is read-only, you can't edit it. When trying to edit a 155 | read-only file, a message "File is read-only" tells you it is non-writable. 156 | .br 157 | The modifications are shown in bold until they are saved. 158 | The modeline indicates whether you have modified the file or not. 159 | .br 160 | When editing in hexadecimal, only \fI0\fR,\fI1\fR,...,\fI9\fR, 161 | \fIa\fR,\fIb\fR,...,\fIf\fR, \fIA\fR,\fIB\fR,...\fIF\fR are legal. 162 | Other keys are unbound. The first time you hit an unbound key, the help pops up. 163 | It won't pop again unless you call the help directly (with \fIF1\fR). 164 | .br 165 | When editing in ascii, you can find it difficult to enter characters like 166 | \fI/\fR which are bound to a function. The solution is to use the quoted insert 167 | function \fICtrl+Q\fR, the key after the quoted insert function is not processed 168 | by \fIhexedit\fR (like emacs' quoted-insert, or like the \\ character in C). 169 | .SS Searching 170 | You can search for a string in ASCII or in hexadecimal. You can switch 171 | between the two with \fITab\fR. If the string is found, the cursor is moved to 172 | the beginning of the matching location. If the search failed, a message "not found" 173 | tells you so. You can cancel the search by pressing a key. 174 | .br 175 | The search in hexadecimal is a bit confusing. You must give a hexadecimal string 176 | with an even number of characters. The search can then be done byte by byte. If 177 | you want to search a long number (eg: a 32 bit number), you must know the 178 | internal representation of that number (little/big endian problem) and give it 179 | the way it is in memory. For example, on an Intel processor (little endian), you 180 | must swap every bytes: 0x12345678 is written 0x78563412 in memory and that's the 181 | string you must give to the search engine. 182 | .br 183 | Before searching you are asked if you want to save the changes, if the file is 184 | edited. 185 | .PP 186 | For more sophisticated search, see Volker Schatz's patch at 187 | . 188 | .SS Selecting, copying, pasting, filling 189 | First, select the part of the buffer you want to copy: start setting the mark 190 | where you want. Then go to the end of the area you want to copy (you can use the 191 | go to function and the search functions). Then copy it. You can then paste the 192 | copied area in the current file or in another file. 193 | .PP 194 | You can also fill the selected area with a string or a character: start choosing 195 | the block you want to fill in (set mark then move to the end of the block), and 196 | call the fill function (\fIF12\fR). \fIhexedit\fR ask you the string you want to 197 | fill the block with. 198 | .br 199 | The code is not tuned for huge filling as it keeps the modifications in memory 200 | until you save them. That's why \fIhexedit\fR will warn you if you try to fill 201 | in a big block. 202 | .PP 203 | When the mark is set, the selection is shown in reverse mode. 204 | .br 205 | Be aware that the copied area contains the modifications done at the time of the 206 | copy. But if you undo the modifications, it does not change the content of the 207 | copy buffer. It seems obvious but it's worth saying. 208 | .SS Scrolling 209 | The scrolling is different whether you are in \fI\-\-sector\fR mode or not. In 210 | normal mode, the scrolling is line by line. In sector mode, the scrolling is 211 | sector by sector. In both modes, you can force the display to start at a given 212 | position using \fIEsc+L\fR. 213 | .SH SEE ALSO 214 | od(1), hdump(1), hexdump(1), bpe(1), hexed(1), beav(1). 215 | .SH AUTHOR 216 | Pixel (Pascal Rigaux) , 217 | .br 218 | Home page is . 219 | .SH UNRESTRICTIONS 220 | .I hexedit 221 | is Open Source; anyone may redistribute copies of 222 | .I hexedit 223 | to 224 | anyone under the terms stated in the GNU General Public License. 225 | .PP 226 | You can find 227 | .I hexedit 228 | at 229 | .br 230 | 231 | .SH TODO 232 | Anything you think could be nice... 233 | .SH LIMITATIONS 234 | There are problems with the curses library given with Redhat 5.0 that make 235 | \fIhexedit\fR think the terminal is huge. The result is that hexedit is 236 | not usable. 237 | .PP 238 | The shortcuts work on some machines, and not on others. That's why there are 239 | many shortcuts for each function. The Ctrl+Arrows and the Alt+. do not work 240 | work as they should most of the time. On SUNs, you must do Ctrl+V-Ctrl+V instead 241 | of Ctrl+V (!); and the Alt key is the diamond one. 242 | .PP 243 | While searching, it could be interesting to know which position the search has 244 | reached. It's always nice to see something moving to help waiting. 245 | .PP 246 | The hexadecimal search could be able to search modulo 4 bits instead of 8 bits. 247 | Another feature could be to complete padd odd length hexadecimal searches with 248 | zeros. 249 | .SH BUGS 250 | I have an example where the display is completely screwed up. It seems to be a 251 | bug in ncurses (or maybe in xterm and rxvt)?? Don't know if it's me using 252 | ncurses badly or not... It seems to happen when \fIhexedit\fR leaves only one 253 | space at the end of the lines... If anyone has a (or the) solution, please tell 254 | me! 255 | .PP 256 | If you have any problem with the program (even a small one), please do report it 257 | to me. Remarks of any kind are also welcome. 258 | .PP 259 | -------------------------------------------------------------------------------- /interact.c: -------------------------------------------------------------------------------- 1 | /* hexedit -- Hexadecimal Editor for Binary Files 2 | Copyright (C) 1998 Pixel (Pascal Rigaux) 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2, or (at your option) 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/ 17 | #include "hexedit.h" 18 | 19 | 20 | static void goto_char(void); 21 | static void goto_sector(void); 22 | static void save_buffer(void); 23 | static void escaped_command(void); 24 | static void help(void); 25 | static void short_help(void); 26 | 27 | 28 | /*******************************************************************************/ 29 | /* interactive functions */ 30 | /*******************************************************************************/ 31 | 32 | static void forward_char(void) 33 | { 34 | if (!hexOrAscii || cursorOffset) 35 | move_cursor(+1); 36 | if (hexOrAscii) cursorOffset = (cursorOffset + 1) % 2; 37 | } 38 | 39 | static void backward_char(void) 40 | { 41 | if (!hexOrAscii || !cursorOffset) 42 | move_cursor(-1); 43 | if (hexOrAscii) cursorOffset = (cursorOffset + 1) % 2; 44 | } 45 | 46 | static void next_line(void) 47 | { 48 | move_cursor(+lineLength); 49 | } 50 | 51 | static void previous_line(void) 52 | { 53 | move_cursor(-lineLength); 54 | } 55 | 56 | static void forward_chars(void) 57 | { 58 | move_cursor(+blocSize); 59 | } 60 | 61 | static void backward_chars(void) 62 | { 63 | move_cursor(-blocSize); 64 | } 65 | 66 | static void next_lines(void) 67 | { 68 | move_cursor(+lineLength * blocSize); 69 | } 70 | 71 | static void previous_lines(void) 72 | { 73 | move_cursor(-lineLength * blocSize); 74 | } 75 | 76 | static void beginning_of_line(void) 77 | { 78 | cursorOffset = 0; 79 | move_cursor(-(cursor % lineLength)); 80 | } 81 | 82 | static void end_of_line(void) 83 | { 84 | cursorOffset = 0; 85 | if (!move_cursor(lineLength - 1 - cursor % lineLength)) 86 | move_cursor(nbBytes - cursor); 87 | } 88 | 89 | static void scroll_up(void) 90 | { 91 | move_base(+page); 92 | 93 | if (mark_set) 94 | updateMarked(); 95 | } 96 | 97 | static void scroll_down(void) 98 | { 99 | move_base(-page); 100 | 101 | if (mark_set) 102 | updateMarked(); 103 | } 104 | 105 | static void beginning_of_buffer(void) 106 | { 107 | cursorOffset = 0; 108 | set_cursor(0); 109 | } 110 | 111 | static void end_of_buffer(void) 112 | { 113 | INT s = getfilesize(); 114 | cursorOffset = 0; 115 | if (mode == bySector) set_base(myfloor(s, page)); 116 | set_cursor(s); 117 | } 118 | 119 | static void suspend(void) { kill(getpid(), SIGTSTP); } 120 | static void undo(void) { discardEdited(); readFile(); } 121 | static void quoted_insert(void) { setTo(getch()); } 122 | static void toggle(void) { hexOrAscii = (hexOrAscii + 1) % 2; } 123 | 124 | static void recenter(void) 125 | { 126 | if (cursor) { 127 | base = base + cursor; 128 | cursor = 0; 129 | readFile(); 130 | } 131 | } 132 | 133 | static void find_file(void) 134 | { 135 | if (!ask_about_save_and_redisplay()) return; 136 | if (!findFile()) { displayMessageAndWaitForKey("No such file or directory"); return; } 137 | openFile(); 138 | readFile(); 139 | } 140 | 141 | static void redisplay(void) { clear(); } 142 | 143 | static void delete_backward_char(void) 144 | { 145 | backward_char(); 146 | removeFromEdited(base + cursor, 1); 147 | readFile(); 148 | cursorOffset = 0; 149 | if (!tryloc(base + cursor)) end_of_buffer(); 150 | } 151 | 152 | static void delete_backward_chars(void) 153 | { 154 | backward_chars(); 155 | removeFromEdited(base + cursor, blocSize); 156 | readFile(); 157 | cursorOffset = 0; 158 | if (!tryloc(base + cursor)) end_of_buffer(); 159 | } 160 | 161 | static void truncate_file(void) 162 | { 163 | displayOneLineMessage("Really truncate here? (y/N)"); 164 | if (tolower(getch()) == 'y') { 165 | if (biggestLoc > base+cursor && ftruncate(fd, base+cursor) == -1) 166 | displayMessageAndWaitForKey(strerror(errno)); 167 | else { 168 | removeFromEdited(base+cursor, lastEditedLoc - (base+cursor)); 169 | if (mark_set) { 170 | if (mark_min >= base + cursor || mark_max >= base + cursor) 171 | unmarkAll(); 172 | } 173 | if (biggestLoc > base+cursor) 174 | biggestLoc = base+cursor; 175 | readFile(); 176 | } 177 | } 178 | } 179 | 180 | static void firstTimeHelp(void) 181 | { 182 | static int firstTime = TRUE; 183 | 184 | if (firstTime) { 185 | firstTime = FALSE; 186 | short_help(); 187 | } 188 | } 189 | 190 | static void set_mark_command(void) 191 | { 192 | unmarkAll(); 193 | if ((mark_set = not(mark_set))) { 194 | markIt(cursor); 195 | mark_min = mark_max = base + cursor; 196 | } 197 | } 198 | 199 | 200 | int setTo(int c) 201 | { 202 | int val; 203 | 204 | if (cursor > nbBytes) return FALSE; 205 | if (hexOrAscii) { 206 | if (!isxdigit(c)) return FALSE; 207 | val = hexCharToInt(c); 208 | val = cursorOffset ? setLowBits(buffer[cursor], val) : setHighBits(buffer[cursor], val); 209 | } 210 | else val = c; 211 | 212 | if (isReadOnly) { 213 | displayMessageAndWaitForKey("File is read-only!"); 214 | } else { 215 | setToChar(cursor, val); 216 | forward_char(); 217 | } 218 | return TRUE; 219 | } 220 | 221 | 222 | /**************************************************** 223 | ask_about_* or functions that present a prompt 224 | ****************************************************/ 225 | 226 | 227 | int ask_about_save(void) 228 | { 229 | if (edited) { 230 | displayOneLineMessage("Save changes (Yes/No/Cancel) ?"); 231 | 232 | switch (tolower(getch())) 233 | { 234 | case 'y': save_buffer(); break; 235 | case 'n': discardEdited(); break; 236 | 237 | default: 238 | return FALSE; 239 | } 240 | return TRUE; 241 | } 242 | return -TRUE; 243 | } 244 | 245 | int ask_about_save_and_redisplay(void) 246 | { 247 | int b = ask_about_save(); 248 | if (b == TRUE) { 249 | readFile(); 250 | display(); 251 | } 252 | return b; 253 | } 254 | 255 | void ask_about_save_and_quit(void) 256 | { 257 | if (ask_about_save()) quit(); 258 | } 259 | 260 | static void goto_char(void) 261 | { 262 | INT i; 263 | 264 | displayOneLineMessage("New position ? "); 265 | ungetstr("0x"); 266 | if (!get_number(&i) || !set_cursor(i)) displayMessageAndWaitForKey("Invalid position!"); 267 | } 268 | 269 | static void goto_sector(void) 270 | { 271 | INT i; 272 | 273 | displayOneLineMessage("New sector ? "); 274 | if (get_number(&i) && set_base(i * SECTOR_SIZE)) 275 | set_cursor(i * SECTOR_SIZE); 276 | else 277 | displayMessageAndWaitForKey("Invalid sector!"); 278 | } 279 | 280 | 281 | 282 | static void save_buffer(void) 283 | { 284 | int displayedmessage = FALSE; 285 | typePage *p, *q; 286 | for (p = edited; p; p = q) { 287 | if (LSEEK_(fd, p->base) == -1 || write(fd, p->vals, p->size) == -1) 288 | if (!displayedmessage) { /* It would be annoying to display lots of error messages when we can't write. */ 289 | displayMessageAndWaitForKey(strerror(errno)); 290 | displayedmessage = TRUE; 291 | } 292 | q = p->next; 293 | freePage(p); 294 | } 295 | edited = NULL; 296 | if (lastEditedLoc > fileSize) fileSize = lastEditedLoc; 297 | lastEditedLoc = 0; 298 | memset(bufferAttr, A_NORMAL, page * sizeof(*bufferAttr)); 299 | if (displayedmessage) { 300 | displayMessageAndWaitForKey("Unwritten changes have been discarded"); 301 | readFile(); 302 | if (cursor > nbBytes) set_cursor(getfilesize()); 303 | } 304 | if (mark_set) markSelectedRegion(); 305 | } 306 | 307 | static void help(void) 308 | { 309 | char *args[3]; 310 | int status; 311 | 312 | args[0] = "man"; 313 | args[1] = "hexedit"; 314 | args[2] = NULL; 315 | endwin(); 316 | if (fork() == 0) { 317 | execvp(args[0], args); 318 | exit(1); 319 | } 320 | wait(&status); 321 | refresh(); 322 | raw(); 323 | } 324 | 325 | static void short_help(void) 326 | { 327 | displayMessageAndWaitForKey("Unknown command, press F1 for help"); 328 | } 329 | 330 | 331 | 332 | /*******************************************************************************/ 333 | /* key_to_function */ 334 | /*******************************************************************************/ 335 | int key_to_function(int key) 336 | { 337 | oldcursor = cursor; 338 | oldcursorOffset = cursorOffset; 339 | oldbase = base; 340 | /*printf("*******%d******\n", key);*/ 341 | 342 | switch (key) 343 | { 344 | case KEY_RESIZE: 345 | /*Close and refresh window to get new size*/ 346 | endwin(); 347 | refresh(); 348 | 349 | /*Reset to trigger recalculation*/ 350 | lineLength = 0; 351 | 352 | clear(); 353 | initDisplay(); 354 | readFile(); 355 | display(); 356 | break; 357 | 358 | case ERR: 359 | break; 360 | 361 | case KEY_RIGHT: 362 | case CTRL('F'): 363 | forward_char(); 364 | break; 365 | 366 | case KEY_LEFT: 367 | case CTRL('B'): 368 | backward_char(); 369 | break; 370 | 371 | case KEY_DOWN: 372 | case CTRL('N'): 373 | next_line(); 374 | break; 375 | 376 | case KEY_UP: 377 | case CTRL('P'): 378 | previous_line(); 379 | break; 380 | 381 | case ALT('F'): 382 | forward_chars(); 383 | break; 384 | 385 | case ALT('B'): 386 | backward_chars(); 387 | break; 388 | 389 | case ALT('N'): 390 | next_lines(); 391 | break; 392 | 393 | case ALT('P'): 394 | previous_lines(); 395 | break; 396 | 397 | case CTRL('A'): 398 | case KEY_HOME: 399 | beginning_of_line(); 400 | break; 401 | 402 | case CTRL('E'): 403 | case KEY_END: 404 | end_of_line(); 405 | break; 406 | 407 | case KEY_NPAGE: 408 | case CTRL('V'): 409 | case KEY_F(6): 410 | scroll_up(); 411 | break; 412 | 413 | case KEY_PPAGE: 414 | case ALT('V'): 415 | case KEY_F(5): 416 | scroll_down(); 417 | break; 418 | 419 | case '<': 420 | case ALT('<'): 421 | beginning_of_buffer(); 422 | break; 423 | 424 | case '>': 425 | case ALT('>'): 426 | end_of_buffer(); 427 | break; 428 | 429 | case KEY_SUSPEND: 430 | case CTRL('Z'): 431 | suspend(); 432 | break; 433 | 434 | case CTRL('U'): 435 | case CTRL('_'): 436 | undo(); 437 | break; 438 | 439 | case CTRL('Q'): 440 | quoted_insert(); 441 | break; 442 | 443 | case CTRL('T'): 444 | case '\t': 445 | toggle(); 446 | break; 447 | 448 | case '/': 449 | case CTRL('S'): 450 | search_forward(); 451 | break; 452 | 453 | case CTRL('R'): 454 | search_backward(); 455 | break; 456 | 457 | case CTRL('G'): 458 | case KEY_F(4): 459 | goto_char(); 460 | break; 461 | 462 | case ALT('L'): 463 | recenter(); 464 | break; 465 | 466 | case '\n': 467 | case '\r': 468 | case KEY_ENTER: 469 | if (mode == bySector) goto_sector(); else goto_char(); 470 | break; 471 | 472 | case CTRL('W'): 473 | case KEY_F(2): 474 | save_buffer(); 475 | break; 476 | 477 | case CTRL('['): /* escape */ 478 | escaped_command(); 479 | break; 480 | 481 | case KEY_F(1): 482 | case ALT('H'): 483 | help(); 484 | break; 485 | 486 | case KEY_F(3): 487 | case CTRL('O'): 488 | find_file(); 489 | break; 490 | 491 | case CTRL('L'): 492 | redisplay(); 493 | break; 494 | 495 | case CTRL('H'): 496 | case KEY_BACKSPACE: 497 | delete_backward_char(); 498 | break; 499 | 500 | case CTRL('H') | 0x80: /* CTRL-ALT-H */ 501 | delete_backward_chars(); 502 | break; 503 | 504 | case CTRL(' '): 505 | case KEY_F(9): 506 | set_mark_command(); 507 | break; 508 | 509 | case CTRL('D'): 510 | case ALT('W'): 511 | case KEY_DC: 512 | case KEY_F(7): 513 | case 0x7F: /* found on a sun */ 514 | copy_region(); 515 | break; 516 | 517 | case CTRL('Y'): 518 | case KEY_IC: 519 | case KEY_F(8): 520 | yank(); 521 | break; 522 | 523 | case ALT('Y'): 524 | case KEY_F(11): 525 | yank_to_a_file(); 526 | break; 527 | 528 | case KEY_F(12): 529 | case ALT('I'): 530 | fill_with_string(); 531 | break; 532 | 533 | case CTRL('C'): 534 | quit(); 535 | break; 536 | 537 | case ALT('T'): 538 | truncate_file(); 539 | break; 540 | 541 | case KEY_F(0): 542 | case KEY_F(10): 543 | case CTRL('X'): 544 | ask_about_save_and_quit(); 545 | break; 546 | 547 | default: 548 | if ((key >= 256 || !setTo(key))) firstTimeHelp(); 549 | } 550 | 551 | return TRUE; 552 | } 553 | 554 | 555 | 556 | static void escaped_command(void) 557 | { 558 | char tmp[BLOCK_SEARCH_SIZE]; 559 | int c, i; 560 | 561 | c = getch(); 562 | switch (c) 563 | { 564 | case KEY_RIGHT: 565 | case 'f': 566 | forward_chars(); 567 | break; 568 | 569 | case KEY_LEFT: 570 | case 'b': 571 | backward_chars(); 572 | break; 573 | 574 | case KEY_DOWN: 575 | case 'n': 576 | next_lines(); 577 | break; 578 | 579 | case KEY_UP: 580 | case 'p': 581 | previous_lines(); 582 | break; 583 | 584 | case 'v': 585 | scroll_down(); 586 | break; 587 | 588 | case KEY_HOME: 589 | case '<': 590 | beginning_of_buffer(); 591 | break; 592 | 593 | case KEY_END: 594 | case '>': 595 | end_of_buffer(); 596 | break; 597 | 598 | case 'l': 599 | recenter(); 600 | break; 601 | 602 | case 'h': 603 | help(); 604 | break; 605 | 606 | case CTRL('H'): 607 | delete_backward_chars(); 608 | break; 609 | 610 | case 'w': 611 | copy_region(); 612 | break; 613 | 614 | case 'y': 615 | yank_to_a_file(); 616 | break; 617 | 618 | case 'i': 619 | fill_with_string(); 620 | break; 621 | 622 | case 't': 623 | truncate_file(); 624 | break; 625 | 626 | case '': 627 | c = getch(); 628 | if (c == 'O') { 629 | switch (c = getch()) 630 | { 631 | case 'C': 632 | forward_chars(); 633 | break; 634 | 635 | case 'D': 636 | backward_chars(); 637 | break; 638 | 639 | case 'B': 640 | next_lines(); 641 | break; 642 | 643 | case 'A': 644 | previous_lines(); 645 | break; 646 | 647 | case 'H': 648 | beginning_of_buffer(); 649 | break; 650 | 651 | case 'F': 652 | end_of_buffer(); 653 | break; 654 | 655 | case 'P': /* F1 on a xterm */ 656 | help(); 657 | break; 658 | 659 | case 'Q': /* F2 on a xterm */ 660 | save_buffer(); 661 | break; 662 | 663 | case 'R': /* F3 on a xterm */ 664 | find_file(); 665 | break; 666 | 667 | case 'S': /* F4 on a xterm */ 668 | goto_char(); 669 | break; 670 | 671 | default: 672 | firstTimeHelp(); 673 | } 674 | } else firstTimeHelp(); 675 | break; 676 | 677 | case '[': 678 | for (i = 0; i < BLOCK_SEARCH_SIZE - 1; i++) { tmp[i] = c = getch(); if (!isdigit(c)) break; } 679 | tmp[i < BLOCK_SEARCH_SIZE - 1 ? i + 1 : i] = '\0'; 680 | 681 | if (0); 682 | else if (streq(tmp, "2~")) yank(); 683 | else if (streq(tmp, "5~")) scroll_down(); 684 | else if (streq(tmp, "6~")) scroll_up(); 685 | else if (streq(tmp, "7~")) beginning_of_buffer(); 686 | else if (streq(tmp, "8~")) end_of_buffer(); 687 | else if (streq(tmp, "010q" /* F10 on a sgi's winterm */)) ask_about_save_and_quit(); 688 | else if (streq(tmp, "193z")) fill_with_string(); 689 | else if (streq(tmp, "214z")) beginning_of_line(); 690 | else if (streq(tmp, "216z")) scroll_down(); 691 | else if (streq(tmp, "220z")) end_of_line(); 692 | else if (streq(tmp, "222z")) scroll_up(); 693 | else if (streq(tmp, "233z")) ask_about_save_and_quit(); 694 | else if (streq(tmp, "234z" /* F11 on a sun */)) yank_to_a_file(); 695 | else if (streq(tmp, "247z")) yank(); 696 | else if (streq(tmp, "11~" /* F1 on a rxvt */)) help(); 697 | else if (streq(tmp, "12~" /* F2 on a rxvt */)) save_buffer(); 698 | else if (streq(tmp, "13~" /* F3 on a rxvt */)) find_file(); 699 | else if (streq(tmp, "14~" /* F4 on a rxvt */)) goto_char(); 700 | else if (streq(tmp, "15~" /* F5 on a rxvt */)) scroll_down(); 701 | else if (streq(tmp, "17~" /* F6 on a rxvt */)) scroll_up(); 702 | else if (streq(tmp, "18~" /* F7 on a rxvt */)) copy_region(); 703 | else if (streq(tmp, "19~" /* F8 on a rxvt */)) yank(); 704 | else if (streq(tmp, "20~" /* F9 on a rxvt */)) set_mark_command(); 705 | else if (streq(tmp, "21~" /* F10 on a rxvt */)) ask_about_save_and_quit(); 706 | else if (streq(tmp, "23~" /* F11 on a rxvt */)) yank_to_a_file(); 707 | else if (streq(tmp, "24~" /* F12 on a rxvt */)) fill_with_string(); 708 | else firstTimeHelp(); 709 | break; 710 | 711 | default: 712 | firstTimeHelp(); 713 | } 714 | } 715 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Library General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 19yy 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License 307 | along with this program; if not, write to the Free Software 308 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 309 | 310 | 311 | Also add information on how to contact you by electronic and paper mail. 312 | 313 | If the program is interactive, make it output a short notice like this 314 | when it starts in an interactive mode: 315 | 316 | Gnomovision version 69, Copyright (C) 19yy name of author 317 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 318 | This is free software, and you are welcome to redistribute it 319 | under certain conditions; type `show c' for details. 320 | 321 | The hypothetical commands `show w' and `show c' should show the appropriate 322 | parts of the General Public License. Of course, the commands you use may 323 | be called something other than `show w' and `show c'; they could even be 324 | mouse-clicks or menu items--whatever suits your program. 325 | 326 | You should also get your employer (if you work as a programmer) or your 327 | school, if any, to sign a "copyright disclaimer" for the program, if 328 | necessary. Here is a sample; alter the names: 329 | 330 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 331 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 332 | 333 | , 1 April 1989 334 | Ty Coon, President of Vice 335 | 336 | This General Public License does not permit incorporating your program into 337 | proprietary programs. If your program is a subroutine library, you may 338 | consider it more useful to permit linking proprietary applications with the 339 | library. If this is what you want to do, use the GNU Library General 340 | Public License instead of this License. 341 | --------------------------------------------------------------------------------