commit e4f468da4b49e122028a1b40efecabe93070be51 Author: Tretter Date: Tue Feb 7 20:07:58 2017 +0000 initial checkin from subversion diff --git a/0 b/0 new file mode 100644 index 0000000..e69de29 diff --git a/1 b/1 new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/1 @@ -0,0 +1 @@ + diff --git a/250 b/250 new file mode 100644 index 0000000..0f164a1 --- /dev/null +++ b/250 @@ -0,0 +1 @@ +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 diff --git a/254 b/254 new file mode 100644 index 0000000..4d397ca --- /dev/null +++ b/254 @@ -0,0 +1 @@ +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 diff --git a/255 b/255 new file mode 100644 index 0000000..56deb17 --- /dev/null +++ b/255 @@ -0,0 +1 @@ +0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 diff --git a/256 b/256 new file mode 100644 index 0000000..8b96668 --- /dev/null +++ b/256 @@ -0,0 +1 @@ +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 diff --git a/a.out b/a.out new file mode 100644 index 0000000..6137fc2 Binary files /dev/null and b/a.out differ diff --git a/aas b/aas new file mode 100644 index 0000000..20ceac6 --- /dev/null +++ b/aas @@ -0,0 +1 @@ +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \ No newline at end of file diff --git a/allchars.txt b/allchars.txt new file mode 100644 index 0000000..e9d82e8 --- /dev/null +++ b/allchars.txt @@ -0,0 +1,2 @@ + +  !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ diff --git a/axa b/axa new file mode 100644 index 0000000..b3c3e16 --- /dev/null +++ b/axa @@ -0,0 +1 @@ +axaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxaxax \ No newline at end of file diff --git a/compile b/compile new file mode 100644 index 0000000..a138799 --- /dev/null +++ b/compile @@ -0,0 +1 @@ +g++ -ox $1 -lncurses diff --git a/dbcompile b/dbcompile new file mode 100644 index 0000000..8da6c70 --- /dev/null +++ b/dbcompile @@ -0,0 +1 @@ +g++ -Wall -ox $1 -g -lncurses diff --git a/dblach.txt b/dblach.txt new file mode 100644 index 0000000..553a99f Binary files /dev/null and b/dblach.txt differ diff --git a/drittes.cc b/drittes.cc new file mode 100644 index 0000000..26eecca --- /dev/null +++ b/drittes.cc @@ -0,0 +1,31 @@ +#include + +int main() +{ + WINDOW *stdscr; + const char *str1 = "Eins "; + int z; + +stdscr = initscr(); +int sc=start_color(); + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + int cp=init_pair(1,(COLOR_BLUE),COLOR_BLACK); + cp=init_pair(2,COLOR_BLUE,COLOR_MAGENTA); + attrset(COLOR_PAIR(1)); + attron(WA_BOLD); + box(stdscr,0,0); + for (z=1;z < 5;z++) + { + move(z,1); + attrset(COLOR_PAIR(1)); + addstr(str1); + attrset(COLOR_PAIR(2)); + addstr(str1); + } + refresh(); + sleep(1); + endwin(); + return 0; +} diff --git a/err b/err new file mode 100644 index 0000000..ab348b4 --- /dev/null +++ b/err @@ -0,0 +1,68 @@ +Im in Block0Working with line 0 - Distance is 5 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 5 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 5 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 5 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 5 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 5 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 4 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 4 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 4 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 4 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 4 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 4 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 4 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 4 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 4 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 4 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 4 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 4 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 4 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 4 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 4 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 4 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 3 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 3 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 3 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 3 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 3 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 3 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 3 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 3 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 3 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 3 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 3 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 3 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 3 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 3 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 3 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 3 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 2 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 2 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 2 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 2 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 2 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 2 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 2 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 2 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 2 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 2 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 2 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 2 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 2 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 2 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 2 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 2 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 1 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 1 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 1 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 1 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 1 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 1 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 1 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 1 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 1 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 1 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 1 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 1 +Im in Block1Im in Block2Im in Block0Working with line 0 - Distance is 1 +Im in Block1Im in Block2 \ No newline at end of file diff --git a/erstes.cc b/erstes.cc new file mode 100644 index 0000000..60703fc --- /dev/null +++ b/erstes.cc @@ -0,0 +1,30 @@ +#include + +int main() +{ + WINDOW *stdscr; + const char *str1 = "Eins"; + int z; + +stdscr = initscr(); +int sc=start_color(); + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + int cp=init_pair(1,COLOR_MAGENTA,COLOR_BLUE); + cp=init_pair(2,COLOR_BLUE,COLOR_MAGENTA); + COLOR_PAIR(1); + for (z=1;z < 5;z++) + { + move(z,0); + attrset(COLOR_PAIR(1)); + addstr(str1); + attrset(COLOR_PAIR(2)); + addstr(str1); + usleep(1); + } + refresh(); + sleep(1); + endwin(); + return 0; +} diff --git a/hello b/hello new file mode 100644 index 0000000..617e7d4 --- /dev/null +++ b/hello @@ -0,0 +1,3 @@ +ao, jetzt machen air mal das file atwas größer, damat es auch was zua speichern gibt. das hier ist echat kewl.aaaadasdsaadkjkjkaas wirklach alleadrinne??a ich kaans kaum adauben aieder alasdasdasaasdasdsad-))rerrading moderide-mdae. over helfsadaasdfor testing of the +thex-hex-editor +xx \ No newline at end of file diff --git a/hellod b/hellod new file mode 100644 index 0000000..e69de29 diff --git a/hivals b/hivals new file mode 100644 index 0000000..32e2f22 --- /dev/null +++ b/hivals @@ -0,0 +1 @@ +‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ diff --git a/homepage.tiah/index.html b/homepage.tiah/index.html new file mode 100644 index 0000000..b32e90f --- /dev/null +++ b/homepage.tiah/index.html @@ -0,0 +1,89 @@ + +Homepage of the T.I.A.H Hex-Editor for Linux + + +DOWNLOAD BELOW
+
+
+
+
+
+ +
+ T.I.A.H - Tiah Is A Hex-editor (EN)
+
+
+
+
+The tiah program is a (not yet) fully feauterd hex editor for Linux +
+
+The tiah-program comes with a interactive text-based user interface. +
+
+With tiah it is possible to insert ond delete characters. +
+
+Moving around, Edit in Hex/Ascii, Setting bits is possible in the moment. +
+
+It's planned to include functions for searching and filling in later versions. +
+
+
+ +
+ T.I.A.H - Tiah Is A Hex-editor (DE)
+
+
+
+
+Tiah ist ein noch nicht vollstaendig implementierter Hex-Editor fuer Linux. +
+
+Tiah hat ein interaktives Textinterface +
+
+Mit Tiah ist es moeglich Zeichen einzufuegen und zu löschen +
+
+Momentan sind Cursorbewegungen, Setzen einzelner Bist und editieren in Hex und Ascii moeglich. +
+
+Es ist fuer spaetere Versionen geplant, Funktionen zum suchen und fuellen zu implementieren +
+
+ +DOWNLOAD: + +
+ + + + + + + + +
+gzipped-version: + + tiah05.tar.gz +
+bzipped-version: + + tiah05.tar.bz2 +
+
+ +

You can write eMails to me: +j.tretter@gmx.de + +
+
+ +This Page was visited + +times. + + diff --git a/homepage.tiah/tiah05.tar.bz2 b/homepage.tiah/tiah05.tar.bz2 new file mode 100644 index 0000000..4afbd17 Binary files /dev/null and b/homepage.tiah/tiah05.tar.bz2 differ diff --git a/homepage.tiah/tiah05.tar.gz b/homepage.tiah/tiah05.tar.gz new file mode 100644 index 0000000..23db136 Binary files /dev/null and b/homepage.tiah/tiah05.tar.gz differ diff --git a/j b/j new file mode 100644 index 0000000..59b66ba --- /dev/null +++ b/j @@ -0,0 +1 @@ +xx \ No newline at end of file diff --git a/nix b/nix new file mode 100644 index 0000000..e69de29 diff --git a/out b/out new file mode 100644 index 0000000..dfdab82 --- /dev/null +++ b/out @@ -0,0 +1 @@ +abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz \ No newline at end of file diff --git a/thx.cc b/thx.cc new file mode 100644 index 0000000..befbe82 --- /dev/null +++ b/thx.cc @@ -0,0 +1,628 @@ +#include +#include +#include +#include +#include + +struct data // Eine Zeile Speicher +{ +struct data* plast; +struct data* pnext; +unsigned char vals[16]; +unsigned long zeile; +char len; +}; + +struct cpos // Cursor-Position +{ + char x,y; +}pos; + +int mode=1; +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII "; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +int result; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data daten,*pdaten,*pfirst, *phelp; + +void initvar() +{ + pfirst=&daten; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + (*pfirst).zeile=0; + (*pfirst).len=0; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +void miniinit() +{ +xoffs = " "; +xhex = " "; +xascii = " "; +} + +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Zwei Fenster + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// Scrollen erlauben + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// Tastatureingabe erlauben + keypad(stdscr,1); + keypad(whex,1); + keypad(wascii,1); + +// Echo ausschalten + noecho(); + +// Cursor auf Groß setzen + if(curs_set(2)==ERR) retval=1 ; + +// Color-Pairs anlegen (Farbkombinationen) + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(2)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +char* tonumsysx(int base10,int newbase) +{ + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +unsigned long xto10(char *c, int oldbase) +{ +unsigned long base10=0; +unsigned long rest,erg; +int pos=0; +int i; + + while(c[pos++]); + pos--; + int len=pos-1; + while(pos--) + { + if(isdigit(c[len-pos])) + { + base10 = base10 + (unsigned long)( (double)(c[len-pos]-'0') * ( pow( (double)oldbase,(double)pos ) ) ) ; + } + else + { + base10 = base10 + (unsigned long)( ((double)(c[len-pos]-'A')+10) * ( pow( (double)oldbase,(double)pos ) ) ); + } + } + return(base10); +} + +int loadcontents() +{ + int retval=0; + int x; + char ch; + int k=0; + unsigned long zeile=0; + + + pdaten=pfirst; + + ch=fgetc(fp); + printf("Printing hashmark for every 8 Kbytes.\n"); + while (!(feof(fp))) + { + for(x=0;x<16;x++) + { + ((*pdaten).vals[x])=ch; + if (feof(fp)) //(*pdaten).vals[x]==EOF) + { + (*pdaten).vals[x]=0; + break; + } + (*pdaten).len=x+1; + ch=fgetc(fp); + } + (*pdaten).zeile=zeile++; + if(!(feof(fp))) + { + phelp=pdaten; + if (!(zeile%500)) {fprintf(stderr,"%c",'#');} + pdaten=((*pdaten).pnext)=(malloc(sizeof(daten))); +// BUG: Check for Memory-Full does not work. + if(!pdaten) {abort();} + (*pdaten).plast=phelp; + (*pdaten).pnext=NULL; + } + + } +/* D E B U G O N L Y + + pdaten=pfirst; + +printf("%d\n",zeile); + +while (zeile--) + { + for(x=0;x<16;x++) + { + printf("%c-",(*pdaten).vals[x]); + } + pdaten=(*pdaten).pnext; + } +getchar(); + --- END DEBUG ---*/ + return (retval); +} + +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +void unallocmem() +{ +int zv; + +/* DEBUG ONLY + pdaten=pfirst; +int zeile=16; +int x; +printf("%d\n",zeile); + +while (zeile--) + { + for(x=0;x<16;x++) + { + printf("%c-",(*pdaten).vals[x]); + } + pdaten=(*pdaten).pnext; + } +getchar(); + --- END DEBUG ---*/ + +pdaten=pfirst; +for(;(*pdaten).pnext;pdaten=(*pdaten).pnext) + ; +while((*pdaten).plast) +{ +phelp=(*pdaten).plast; +free(pdaten); +pdaten=phelp; +} +} + + +void scrrefresh() +{ + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(2)); + wattrset(wbot,COLOR_PAIR(2)); + + !mode?wmove(whex,pos.y,pos.x):wmove(wascii,pos.y,pos.x); + + wrefresh(woffs); + wrefresh(wtop); + wrefresh(wbot); + // die Reihenfolge wird anhand des Modus bestimmt (das letzte ist aktiv) + !mode?wrefresh(wascii):wrefresh(whex); + mode?wrefresh(wascii):wrefresh(whex); +} + +void lineout(unsigned long zeile) +{ + int z; + + miniinit(); + + ptv = tonumsysx((*pdaten).zeile*16,16); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,zeile,1,soffs); + for (z=0;z<(*pdaten).len;z++) + { + ptv = tonumsysx((*pdaten).vals[z],16); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,zeile,1,shex); + for (z=0;z<(*pdaten).len;z++) + { +// BUG: Die Ober- und Untergrenze genau bestimmen. + xascii[z] = ((((*pdaten).vals[z]) > 31)&&((*pdaten).vals[z] != 127))?((*pdaten).vals[z]):254; + } + mvwaddstr(wascii,zeile,1,sascii); +} + +void initout() +{ +//Initiale Ausgabe (Alle) +int i; +bool schalter=false; +unsigned long zeile=1; +pdaten=pfirst; + +if ((*pdaten).pnext == NULL) schalter=true; +for(i=0;i<16;i++) + { + lineout(i+1); + if (schalter) break; + pdaten=(*pdaten).pnext; + if ((*pdaten).pnext == NULL) schalter=true; + } + scrrefresh(); + +if(i>15) pdaten=(*pdaten).plast; +} + +void scrolld() +{ + wscrl(woffs,1); + wscrl(whex,1); + wscrl(wascii,1); + + int z; + + miniinit(); + + lineout(16); +} + +void scrollu() +{ + wscrl(woffs,-1); + wscrl(whex,-1); + wscrl(wascii,-1); + + miniinit(); + + lineout(1); +} + +void switchmode() +{ + if(mode==0) + { + mode=1; // modus:Hex->Ascii + // Umrechnen der Cursorposition + pos.x=((pos.x+1)/3); + } + else + { + mode=0; // modue:Ascii->Hex + // Umrechnen der Cursorposition + pos.x=((pos.x*3)-1); + } +} + +void moveleft() +{ + if(mode==0) + { + if(pos.x > 2)(pos.x%3)?pos.x-=2:pos.x--; + } + else + { + if(pos.x > 1)pos.x--; + } +} + +void moveright() +{ + if(mode==0) + { + if(pos.x < 48)(pos.x%3)?pos.x++:pos.x+=2; + } + else + { + if(pos.x < 16)pos.x++; + } +} + +void charin(char ch) +{ + int i=0; + // im Speicher zur Cusrorposition blättern + while(i<(16-pos.y)&&((*pdaten).plast)) + { + i++; + pdaten=(*pdaten).plast; + } + // Wert schreiben + (*pdaten).vals[pos.x-1]=ch; + // Ausgabe aktualisieren + lineout(pos.y); + // wieder zurückblättern + while(i--) + { + pdaten=(*pdaten).pnext; + } +} + +int main(int argc, char *argv[]) +{ +/* --- DEBUG ONLY +printf("DEBUG: 5-%u\n",xto10(tonumsysx(5,2),2)); +printf("DEBUG: 16-%u\n",xto10(tonumsysx(16,16),16)); +printf("DEBUG: 36-%u\n",xto10 (tonumsysx(36,36),36)); +printf("DEBUG: 255-%u\n",xto10(tonumsysx(255,16),16)); +printf("DEBUG: 255-%u\n",xto10("FF",16)); +printf("DEBUG: 125-%u\n",xto10(tonumsysx(125,8),8)); +printf("DEBUG: 255-%u\n",xto10(tonumsysx(255,8),8)); +getchar(); + --- END DEBUG */ +//Variablen + unsigned int i,z; // Allgemeine Zähler + unsigned long zeile=0; // Zeilennummer + bool schalter; // Schalter für lesevorlauf + int inpch; // Eingabezeichen + +//Vorbereitung + printf("GHEX Version 0.5\n"); + //Parameter angegeben? + if(argc < 2) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //Datei öffnen + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // pointer setzen + initvar(); + // sonstige Variableninitialisierung + pos.x = pos.y = 1; + //Datei -> Speicher + if(loadcontents()) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // Terminal initialisieren + if(screeninit()) + { + endscr(); + fprintf(stderr,"!!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + mvwaddstr(wbot,1,1,argv[1]); + + +//Hauptprogramm + +initout(); +scrrefresh(); + +//Eventschleife + while(inpch!=KEY_F(10)) + { + if (mode==0) + { + inpch=wgetch(whex); + } + else + { + inpch=wgetch(wascii); + } + switch(inpch) + { + case KEY_F(9): switchmode(); + break; + case KEY_LEFT: moveleft(); + break; + case KEY_RIGHT:moveright(); + break; + case KEY_DOWN: if(pos.y>15) + { + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + } + else + { + pos.y++; + } + break; + case KEY_UP: if(pos.y<2) + { + z=0; + while(((*pdaten).plast != NULL)&&(z < 16)) + { + pdaten=(*pdaten).plast; + z++; + } + if(z>15) + { + z--; + scrollu(); + } + while(z--) + { + if ((*pdaten).pnext != NULL) + { + pdaten=(*pdaten).pnext; + } + } + } + else + { + pos.y--; + } + break; + case KEY_NPAGE: + for(i=0;i<16;i++) + { + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + } + break; + case KEY_PPAGE: + for(i=0;i<16;i++) + { + z=0; + while(((*pdaten).plast != NULL)&&(z < 16)) + { + pdaten=(*pdaten).plast; + z++; + } + if(z>15) + { + z--; + scrollu(); + } + while(z--) + { + if ((*pdaten).pnext != NULL) + { + pdaten=(*pdaten).pnext; + } + } + } + break; + case KEY_HOME: + initout(); + break; + case KEY_END: + while((*pdaten).pnext) + { + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + } + break; + default: + if(mode==0) + { + if(isdigit(inpch) || (toupper(inpch)>='A' && toupper(inpch)<='F') ) + { + waddch(whex,toupper(inpch)); + char hch[]="FF"; +// TODO hch korrekt füllen. if(pos.x%3) + switchmode(); // um die Koordinaten umzurechnen + charin(xto10(hch,16)); // Umrechnen Hex -> Ascii+charin + switchmode(); // und wieder zurücksetzen + moveright(); + } + } + else + { + //if((inpch > 31)&&(inpch < 256)) + if((inpch < 256)) + { + waddch(wascii,inpch); + charin(inpch); + moveright(); + } + } + }; + scrrefresh(); + } + sleep(1); + endscr(); + unallocmem(); + return (fclose(fp)); +} diff --git a/thxv1.cc b/thxv1.cc new file mode 100644 index 0000000..93c5873 --- /dev/null +++ b/thxv1.cc @@ -0,0 +1,224 @@ +#include +#include + +struct data +{ +struct data* plast; +struct data* pnext; +char vals[16][16]; +}; + +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII"; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +int result; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data daten,*pfirst; + +void initvar() +{ + pfirst=&daten; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Zwei Fenster + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// Scrollen erlauben + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// Color-Pairs anlegen (Farbkombinationen) + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(2)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +char* tohex(int base10) +{ + int newbase=16; + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +int loadcontents() +{ + int retval=0; + int x,y; + for(x=0;x<16;x++) + for(y=0;y<16;y++) + { + daten.vals[y][x]=fgetc(fp); + if (daten.vals[y][x]==EOF) daten.vals[y][x]=0; + } + for(x=0;x<16;x++) + for(y=0;y<16;y++) + printf("%c",daten.vals[y][x]); + + return (retval); +} + +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +int main(int argc, char *argv[]) +{ +//Variablen + unsigned int i,z; + + printf("%s\n",tohex(0)); + printf("%s\n",tohex(10)); + printf("%s\n",tohex(20)); + printf("%s\n",tohex(30)); + printf("%s\n",tohex(40)); + printf("%s\n",tohex(50)); + +//Vorbereitung + //Parameter angegeben? + if(argc < 2) + { + fprintf(stderr,"THX-MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //Datei öffnen + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"THX-MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + //Datei -> Speicher + if(loadcontents()) + { + fprintf(stderr,"THX-MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // pointer setzen + initvar(); + // Terminal initialisieren + if(screeninit()) + { + endscr(); + fprintf(stderr,"!!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + mvwaddstr(wbot,1,1,argv[1]); + + +//Hauptprogramm + + for (i=1;i<17;i++) + { + ptv = tohex(i-1); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,i,1,soffs); + for (z=0;z<16;z++) + { + ptv = tohex(daten.vals[z][i-1]); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,i,1,shex); + for (z=0;z<16;z++) + { + xascii[z] = ((daten.vals[z][i-1]) > 33)?(daten.vals[z][i-1]):254; + } + mvwaddstr(wascii,i,1,sascii); + } + + wrefresh(woffs); + wrefresh(whex); + wrefresh(wascii); + wrefresh(wtop); + wrefresh(wbot); + + + sleep(1); + endscr(); + return (fclose(fp)); +} diff --git a/thxv2.cc b/thxv2.cc new file mode 100644 index 0000000..c125255 --- /dev/null +++ b/thxv2.cc @@ -0,0 +1,286 @@ +#include +#include +#include + +struct data +{ +struct data* plast; +struct data* pnext; +char vals[16][16]; +}; + +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII"; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +int result; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data daten,*pdaten,*pfirst, *phelp; + +void initvar() +{ + pfirst=&daten; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Zwei Fenster + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// Scrollen erlauben + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// Color-Pairs anlegen (Farbkombinationen) + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(2)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +char* tohex(int base10) +{ + int newbase=16; + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +int loadcontents() +{ + int retval=0; + int x,y; + char ch; + int k=0; + + + pdaten=pfirst; + + ch=fgetc(fp); + + while (!(ch==EOF)) + { + k++; + + for(x=0;x<16;x++) + for(y=0;y<16;y++) + { + ((*pdaten).vals[y][x])=ch; + if ((*pdaten).vals[y][x]==EOF) (*pdaten).vals[y][x]=0; + ch=fgetc(fp); + } + + if(!(ch==EOF)) + { + phelp=pdaten; + printf("Vorher:%p\n",pdaten); + pdaten=((*pdaten).pnext)=(malloc(sizeof(daten))); + printf("Nachher:%p\n" ,pdaten); + (*pdaten).plast=phelp; + (*pdaten).pnext=NULL; + } + + } +/* D E B U G O N L Y +getchar(); + + pdaten=pfirst; + + +while (k--) + { + for(x=0;x<16;x++) + for(y=0;y<16;y++) + { + printf("%c-",(*pdaten).vals[y][x]); + } + pdaten=(*pdaten).pnext; + } +getchar(); + --- END DEBUG ---*/ + + return (retval); +} + +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +void unallocmem() +{ +int zv; +pdaten=pfirst; +for(;(*pdaten).pnext;pdaten=(*pdaten).pnext) + ; +while((*pdaten).plast) +{ +phelp=(*pdaten).plast; +free(pdaten); +pdaten=phelp; +} + + + +} + + +int main(int argc, char *argv[]) +{ +//Variablen + unsigned int i,z; + unsigned long zeile=0,pos=0; + + printf("%s\n",tohex(0)); + printf("%s\n",tohex(10)); + printf("%s\n",tohex(20)); + printf("%s\n",tohex(30)); + printf("%s\n",tohex(40)); + printf("%s\n",tohex(50)); + +//Vorbereitung + //Parameter angegeben? + if(argc < 2) + { + fprintf(stderr,"THX-MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //Datei öffnen + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"THX-MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // pointer setzen + initvar(); + //Datei -> Speicher + if(loadcontents()) + { + fprintf(stderr,"THX-MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // Terminal initialisieren + if(screeninit()) + { + endscr(); + fprintf(stderr,"!!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + mvwaddstr(wbot,1,1,argv[1]); + + +//Hauptprogramm + + for (i=1;i<17;i++) + { + ptv = tohex(i-1); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,i,1,soffs); + for (z=0;z<16;z++) + { + ptv = tohex(daten.vals[z][i-1]); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,i,1,shex); + for (z=0;z<16;z++) + { + xascii[z] = ((daten.vals[z][i-1]) > 33)?(daten.vals[z][i-1]):254; + } + mvwaddstr(wascii,i,1,sascii); + } + + wrefresh(woffs); + wrefresh(whex); + wrefresh(wascii); + wrefresh(wtop); + wrefresh(wbot); + + + sleep(1); + endscr(); + unallocmem(); + return (fclose(fp)); +} diff --git a/thxv3.cc b/thxv3.cc new file mode 100644 index 0000000..0534219 --- /dev/null +++ b/thxv3.cc @@ -0,0 +1,463 @@ +#include +#include +#include + +struct data +{ +struct data* plast; +struct data* pnext; +char vals[16]; +unsigned long zeile; +char len; +}; + +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII"; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +int result; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data daten,*pdaten,*pfirst, *phelp; + +void initvar() +{ + pfirst=&daten; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + (*pfirst).zeile=0; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +void miniinit() +{ +xoffs = " "; +xhex = " "; +xascii = " "; +} + +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Zwei Fenster + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// Scrollen erlauben + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// Tastatureingabe erlauben + keypad(stdscr,1); + keypad(whex,1); + keypad(wascii,1); + +// Color-Pairs anlegen (Farbkombinationen) + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(2)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +char* tohex(int base10) +{ + int newbase=16; + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +int loadcontents() +{ + int retval=0; + int x; + char ch; + int k=0; + unsigned long zeile=0; + + + pdaten=pfirst; + + ch=fgetc(fp); + printf("Printing hashmark for every 8 Kbytes.\n"); + while (!(ch==EOF)) + { + for(x=0;x<16;x++) + { + ((*pdaten).vals[x])=ch; + if ((*pdaten).vals[x]==EOF) + { + (*pdaten).vals[x]=0; + break; + } + (*pdaten).len=x+1; + ch=fgetc(fp); + } + (*pdaten).zeile=zeile++; + if(!(ch==EOF)) + { + phelp=pdaten; + if (!(zeile%500)) {fprintf(stderr,"%c",'#');} + pdaten=((*pdaten).pnext)=(malloc(sizeof(daten))); + (*pdaten).plast=phelp; + (*pdaten).pnext=NULL; + } + + } +/* D E B U G O N L Y +getchar(); + + pdaten=pfirst; + +printf("%d\n",zeile); + +while (zeile--) + { + for(x=0;x<16;x++) + { + printf("%c-",(*pdaten).vals[x]); + } + pdaten=(*pdaten).pnext; + } +getchar(); + --- END DEBUG ---*/ + + return (retval); +} + +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +void unallocmem() +{ +int zv; +pdaten=pfirst; +for(;(*pdaten).pnext;pdaten=(*pdaten).pnext) + ; +while((*pdaten).plast) +{ +phelp=(*pdaten).plast; +free(pdaten); +pdaten=phelp; +} +} + + +void scrrefresh() +{ + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(2)); + wattrset(wbot,COLOR_PAIR(2)); + + wrefresh(woffs); + wrefresh(whex); + wrefresh(wascii); + wrefresh(wtop); + wrefresh(wbot); +} + +void lineout(unsigned long zeile) +{ + int z; + + miniinit(); + + ptv = tohex((*pdaten).zeile); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,zeile,1,soffs); + for (z=0;z<(*pdaten).len;z++) + { + ptv = tohex((*pdaten).vals[z]); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,zeile,1,shex); + for (z=0;z<(*pdaten).len;z++) + { + xascii[z] = (((*pdaten).vals[z]) > 33)?((*pdaten).vals[z]):254; + } + mvwaddstr(wascii,zeile,1,sascii); +} + + +void scrolld() +{ + wscrl(woffs,1); + wscrl(whex,1); + wscrl(wascii,1); + + int z; + + miniinit(); + + ptv = tohex((*pdaten).zeile); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,16,1,soffs); + for (z=0;z<(*pdaten).len;z++) + { + ptv = tohex((*pdaten).vals[z]); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,16,1,shex); + for (z=0;z<(*pdaten).len;z++) + { + xascii[z] = (((*pdaten).vals[z]) > 33)?((*pdaten).vals[z]):254; + } + mvwaddstr(wascii,16,1,sascii); +} + +void scrollu() +{ + wscrl(woffs,-1); + wscrl(whex,-1); + wscrl(wascii,-1); + + miniinit(); + + int z; + ptv = tohex((*pdaten).zeile); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,1,1,soffs); + for (z=0;z<(*pdaten).len;z++) + { + ptv = tohex((*pdaten).vals[z]); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,1,1,shex); + for (z=0;z<(*pdaten).len;z++) + { + xascii[z] = (((*pdaten).vals[z]) > 33)?((*pdaten).vals[z]):254; + } + mvwaddstr(wascii,1,1,sascii); +} + + +int main(int argc, char *argv[]) +{ +//Variablen + unsigned int i,z; + unsigned long zeile=0,pos=0; + bool schalter; + int inpch; + +//Vorbereitung + //Parameter angegeben? + if(argc < 2) + { + fprintf(stderr,"THX-MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //Datei öffnen + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"THX-MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // pointer setzen + initvar(); + //Datei -> Speicher + if(loadcontents()) + { + fprintf(stderr,"THX-MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // Terminal initialisieren + if(screeninit()) + { + endscr(); + fprintf(stderr,"!!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + mvwaddstr(wbot,1,1,argv[1]); + + +//Hauptprogramm + +schalter=false; +zeile=1; +pdaten=pfirst; + +//Initiale Ausgabe (Alle) +for(i=0;i<16;i++) + { + lineout(i+1); + if (schalter) break; + pdaten=(*pdaten).pnext; + if ((*pdaten).pnext == NULL) schalter=true; + } + scrrefresh(); + +if(i>15) pdaten=(*pdaten).plast; + +//Eventschleife + while(inpch!=KEY_END) + { + inpch=wgetch(whex); + switch(inpch) + { + case KEY_DOWN: + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + break; + case KEY_UP: + z=0; + while(((*pdaten).plast != NULL)&&(z < 16)) + { + pdaten=(*pdaten).plast; + z++; + } + if(z>15) + { + z--; + scrollu(); + } + while(z--) + { + if ((*pdaten).pnext != NULL) + { + pdaten=(*pdaten).pnext; + } + } + break; + case KEY_NPAGE: + for(i=0;i<16;i++) + { + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + } + break; + case KEY_PPAGE: + for(i=0;i<16;i++) + { + z=0; + while(((*pdaten).plast != NULL)&&(z < 16)) + { + pdaten=(*pdaten).plast; + z++; + } + if(z>15) + { + z--; + scrollu(); + } + while(z--) + { + if ((*pdaten).pnext != NULL) + { + pdaten=(*pdaten).pnext; + } + } + } + break; + }; + scrrefresh(); + } + sleep(1); + endscr(); + unallocmem(); + return (fclose(fp)); +} diff --git a/thxv4.cc b/thxv4.cc new file mode 100644 index 0000000..8c91a47 --- /dev/null +++ b/thxv4.cc @@ -0,0 +1,450 @@ +#include +#include +#include + +struct data +{ +struct data* plast; +struct data* pnext; +char vals[16]; +unsigned long zeile; +char len; +}; + +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII "; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +int result; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data daten,*pdaten,*pfirst, *phelp; + +void initvar() +{ + pfirst=&daten; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + (*pfirst).zeile=0; + (*pfirst).len=0; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +void miniinit() +{ +xoffs = " "; +xhex = " "; +xascii = " "; +} + +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Zwei Fenster + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// Scrollen erlauben + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// Tastatureingabe erlauben + keypad(stdscr,1); + keypad(whex,1); + keypad(wascii,1); + +// Color-Pairs anlegen (Farbkombinationen) + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(2)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +char* tonumsysx(int base10,int newbase) +{ + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +int loadcontents() +{ + int retval=0; + int x; + char ch; + int k=0; + unsigned long zeile=0; + + + pdaten=pfirst; + + ch=fgetc(fp); + printf("Printing hashmark for every 8 Kbytes.\n"); + while (!(feof(fp))) + { + for(x=0;x<16;x++) + { + ((*pdaten).vals[x])=ch; + if (feof(fp)) //(*pdaten).vals[x]==EOF) + { + (*pdaten).vals[x]=0; + break; + } + (*pdaten).len=x+1; + ch=fgetc(fp); + } + (*pdaten).zeile=zeile++; + if(!(feof(fp))) + { + phelp=pdaten; + if (!(zeile%500)) {fprintf(stderr,"%c",'#');} + pdaten=((*pdaten).pnext)=(malloc(sizeof(daten))); +// BUG: Check for Memory-Full does not work. + if(!pdaten) {abort();} + (*pdaten).plast=phelp; + (*pdaten).pnext=NULL; + } + + } +/* D E B U G O N L Y +getchar(); + + pdaten=pfirst; + +printf("%d\n",zeile); + +while (zeile--) + { + for(x=0;x<16;x++) + { + printf("%c-",(*pdaten).vals[x]); + } + pdaten=(*pdaten).pnext; + } +getchar(); + --- END DEBUG ---*/ + return (retval); +} + +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +void unallocmem() +{ +int zv; +pdaten=pfirst; +for(;(*pdaten).pnext;pdaten=(*pdaten).pnext) + ; +while((*pdaten).plast) +{ +phelp=(*pdaten).plast; +free(pdaten); +pdaten=phelp; +} +} + + +void scrrefresh() +{ + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(2)); + wattrset(wbot,COLOR_PAIR(2)); + + wrefresh(woffs); + wrefresh(whex); + wrefresh(wascii); + wrefresh(wtop); + wrefresh(wbot); +} + +void lineout(unsigned long zeile) +{ + int z; + + miniinit(); + + ptv = tonumsysx((*pdaten).zeile*16,16); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,zeile,1,soffs); + for (z=0;z<(*pdaten).len;z++) + { + ptv = tonumsysx((*pdaten).vals[z],16); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,zeile,1,shex); + for (z=0;z<(*pdaten).len;z++) + { + xascii[z] = (((*pdaten).vals[z]) > 31)?((*pdaten).vals[z]):254; + } + mvwaddstr(wascii,zeile,1,sascii); +} + +void initout() +{ +//Initiale Ausgabe (Alle) +int i; +bool schalter=false; +unsigned long zeile=1; +pdaten=pfirst; + +if ((*pdaten).pnext == NULL) schalter=true; +for(i=0;i<16;i++) + { + lineout(i+1); + if (schalter) break; + pdaten=(*pdaten).pnext; + if ((*pdaten).pnext == NULL) schalter=true; + } + scrrefresh(); + +if(i>15) pdaten=(*pdaten).plast; +} + +void scrolld() +{ + wscrl(woffs,1); + wscrl(whex,1); + wscrl(wascii,1); + + int z; + + miniinit(); + + lineout(16); +} + +void scrollu() +{ + wscrl(woffs,-1); + wscrl(whex,-1); + wscrl(wascii,-1); + + miniinit(); + + lineout(1); +} + + +int main(int argc, char *argv[]) +{ +//Variablen + unsigned int i,z; + unsigned long zeile=0,pos=0; + bool schalter; + int inpch; + + printf("GHEX Version 0.5\n"); + +//Vorbereitung + //Parameter angegeben? + if(argc < 2) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //Datei öffnen + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // pointer setzen + initvar(); + //Datei -> Speicher + if(loadcontents()) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // Terminal initialisieren + if(screeninit()) + { + endscr(); + fprintf(stderr,"!!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + mvwaddstr(wbot,1,1,argv[1]); + + +//Hauptprogramm + +initout(); + +//Eventschleife + while(inpch!=KEY_F(10)) + { + inpch=wgetch(whex); + switch(inpch) + { + case KEY_DOWN: + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + break; + case KEY_UP: + z=0; + while(((*pdaten).plast != NULL)&&(z < 16)) + { + pdaten=(*pdaten).plast; + z++; + } + if(z>15) + { + z--; + scrollu(); + } + while(z--) + { + if ((*pdaten).pnext != NULL) + { + pdaten=(*pdaten).pnext; + } + } + break; + case KEY_NPAGE: + for(i=0;i<16;i++) + { + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + } + break; + case KEY_PPAGE: + for(i=0;i<16;i++) + { + z=0; + while(((*pdaten).plast != NULL)&&(z < 16)) + { + pdaten=(*pdaten).plast; + z++; + } + if(z>15) + { + z--; + scrollu(); + } + while(z--) + { + if ((*pdaten).pnext != NULL) + { + pdaten=(*pdaten).pnext; + } + } + } + break; + case KEY_HOME: + initout(); + break; + case KEY_END: + while((*pdaten).pnext) + { + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + } + break; + + }; + scrrefresh(); + } + sleep(1); + endscr(); + unallocmem(); + return (fclose(fp)); +} diff --git a/tiah.cc b/tiah.cc new file mode 100644 index 0000000..3b9044f --- /dev/null +++ b/tiah.cc @@ -0,0 +1,1248 @@ +/* + * TIAH -- an advanced HEX-Editor + * Copyright (C) 1999 by Joerg Tretter + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include +#include + +struct data // one line in Memory +{ +struct data* plast; +struct data* pnext; +unsigned char vals[16]; +unsigned long line; +char len; +}; + +struct cpos // Cursor-Position +{ + char x,y; +}pos; + +char insovr='O'; // Isert/Override-Mode +int mode=1; +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII "; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +char empty78[]=" "; +int result; +char *filename; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data datas, *pdatas, *pfirst, *phelp, *pmax; + +/*************************************************************************** + * FUNCTION: void initvar() * + * GOOD FOR: Initialization of basic-variables * + ***************************************************************************/ +void initvar() +{ + pfirst=&datas; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + (*pfirst).line=0; + (*pfirst).len=0; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +/*************************************************************************** + * FUNCTION: void miniinit() * + * GOOD FOR: Initialization of frequent used variables * + ***************************************************************************/ +void miniinit() +{ +xoffs = " "; +xhex = " "; +xascii = " "; +} + +/*************************************************************************** + * FUNCTION: int screeninit() * + * GOOD FOR: Initialization of the n-curses-screen * + * RESULTS : 1:There was an error / 2:Everything ok. * + ***************************************************************************/ +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Create Curses-space for the windows +//TODO: check whether creation was fine. + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// enable scrolling + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// enable Keyboard-control + keypad(stdscr,1); + keypad(whex,1); + keypad(wascii,1); + +// disable echoing of input + noecho(); + +// swich cursor to big size +// TODO: check why this not works in most cases +// if(curs_set(2)==ERR) retval=1 ; + +// creation of curses "Color-Pairs" + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + if(init_pair(3,COLOR_MAGENTA,COLOR_BLACK)) retval=1; + +// join the windows with the color-pairs + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + +// paint the boxes around the windows (in fact it's inside) + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + // change the color-pairs of the windows again. + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +/*************************************************************************** + * FUNCTION: char* tonumsysx() * + * GOOD FOR: Shift a number to another numbering system * + * INPUTS : - int base10 -The number in decimal system * + * - int newbase -The new system (16->Hex,2->Bin,...) * + * RESULTS : The number in the new system as a pointer of char * + ***************************************************************************/ +char* tonumsysx(int base10,int newbase) +{ + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +/*************************************************************************** + * FUNCTION: unsigned long xto10(char *c, int oldbase) * + * GOOD FOR: Shift a number from a foreign numbering system to decimal * + * INPUTS : - char* c -The number in foreign system * + * - int oldbase -The old system (16->Hex,2->Bin,...) * + * RESULTS : The number in decimal system as a unsigned long * + ***************************************************************************/ +unsigned long xto10(char *c, int oldbase) +{ +unsigned long base10=0; +int pos=0; +int i; + for (i=0;c[i];i++, c[i]=toupper(c[i])); + while(c[pos++]); + pos--; + int len=pos-1; + while(pos--) + { + if(isdigit(c[len-pos])) + { + base10 = base10 + (unsigned long)( (double)(c[len-pos]-'0') * ( pow( (double)oldbase,(double)pos ) ) ) ; + } + else + { + base10 = base10 + (unsigned long)( ((double)(c[len-pos]-'A')+10) * ( pow( (double)oldbase,(double)pos ) ) ); + } + } + return(base10); +} + +/*************************************************************************** + * FUNCTION: int loadcontents() * + * GOOD FOR: Loading the contents of a file into the memory * + * RESULTS : The status of the loading (not used yet) * + ***************************************************************************/ +int loadcontents() +{ + int retval=0; + int x; + char ch; + unsigned long line=0; + + + pdatas=pfirst; + ch=fgetc(fp); + // Zero-byte-file. + if(feof(fp)) + { + fprintf(stderr,"T.I.A.H - Tiah Is A Hex-editor. - opening 0-Byte file"); + pfirst=pmax=pdatas=(data*)(malloc(sizeof(datas))); + (*pdatas).pnext=NULL; + (*pdatas).plast=NULL; + (*pdatas).line=0; + (*pdatas).len=0; + + } + // TODO: how to flush the buffer of stdout??? + printf("Printing hashmark for every 8 Kbytes.\n"); + while (!(feof(fp))) + { + for(x=0;x<16;x++) + { + ((*pdatas).vals[x])=ch; + if (feof(fp)) //(*pdatas).vals[x]==EOF) + { + (*pdatas).vals[x]=0; + break; + } + (*pdatas).len=x+1; + ch=fgetc(fp); + } + (*pdatas).line=line++; + if(!(feof(fp))) + { + phelp=pdatas; + if (!(line%500)) {printf("%c",'#');} + pdatas=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); +// BUG: Check for Memory-Full does not work. + if(!pdatas) {abort();} + (*pdatas).plast=phelp; + (*pdatas).pnext=NULL; + } + pmax=pdatas; + + } + return (retval); +} + +/*************************************************************************** + * FUNCTION: void endscr() * + * GOOD FOR: End the scrren session on curses-level * + ***************************************************************************/ +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +/*************************************************************************** + * FUNCTION: void unallocmem(int save, char* filename) * + * GOOD FOR: Free the memory from the bits & bytes * + * INPUTS : int save: flag (save Y/else key) * + * : cahr* filename: the name of the save-file * + ***************************************************************************/ +void unallocmem(int save, char *filename) +{ + int pos; + save=toupper(save); + if (save=='Y') + { + pdatas=pfirst; + fp=fopen(filename,"w"); + while((*pdatas).pnext) + { + for (pos=0;pos <(*pdatas).len;pos++) + { + fputc((*pdatas).vals[pos],fp); + } + pdatas=(*pdatas).pnext; + } + for (pos=0;pos <(*pdatas).len;pos++) fputc((*pdatas).vals[pos],fp); + close(fp); + } + { + pdatas=pmax; + } + while((*pdatas).plast) + { + phelp=(*pdatas).plast; + free(pdatas); + pdatas=phelp; + } +} + +/*************************************************************************** + * FUNCTION: void scrrefresh() * + * GOOD FOR: Refreah the basic-screen-elements (not the contents) * + ***************************************************************************/ +void scrrefresh() +{ + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + // depending on the mode (ascii/hex) set the cursor-position + !mode?wmove(whex,pos.y,pos.x):wmove(wascii,pos.y,pos.x); + + wrefresh(woffs); + wrefresh(wtop); + wrefresh(wbot); + // the order is given by the mode (ascii/hex). the last window refreshed + // it the one, in which the cursor appears and the input-focus is. + !mode?wrefresh(wascii):wrefresh(whex); + mode?wrefresh(wascii):wrefresh(whex); +} + +/*************************************************************************** + * FUNCTION: void emplineout(unsigned long line) * + * GOOD FOR: Writing one empty line to the scrren * + ***************************************************************************/ +void emplineout(unsigned long line) +{ + mvwaddstr(woffs,line,1," "); + mvwaddstr(whex,line,1," "); + mvwaddstr(wascii,line,1," "); +} + +/*************************************************************************** + * FUNCTION: void lineout(unsigned long line) * + * GOOD FOR: Writing one line (the actual one) from the memory to screen * + ***************************************************************************/ +void lineout(unsigned long line) +{ + int z; + + miniinit(); + + ptv = tonumsysx((*pdatas).line*16,16); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,line,1,soffs); + for (z=0;z<(*pdatas).len;z++) + { + ptv = tonumsysx((*pdatas).vals[z],16); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,line,1,shex); + for (z=0;z<(*pdatas).len;z++) + { +// TODO: There are many other characters that are displayable. + xascii[z] = (isgraph((*pdatas).vals[z]))?((*pdatas).vals[z]):'.'; + } + mvwaddstr(wascii,line,1,sascii); +} + +/*************************************************************************** + * FUNCTION: void initout() * + * GOOD FOR: First output after loading the file // home-Key * + ***************************************************************************/ +void initout() +{ +//Initial output (All) +int i,z; +bool toggleswitch=false; +pdatas=pfirst; + +if ((*pdatas).pnext == NULL) toggleswitch=true; +for(i=0;i<16;i++) + { + lineout(i+1); + if (toggleswitch) break; + pdatas=(*pdatas).pnext; + if ((*pdatas).pnext == NULL) toggleswitch=true; + } + scrrefresh(); + +if(i>15) pdatas=(*pdatas).plast; +} + +/*************************************************************************** + * FUNCTION: int scrolld() * + * GOOD FOR: Scrolling the scrren one line down * + * RESULTS : 1: no more elements // 0: everything was nice * + ***************************************************************************/ +int scrolld() +{ + int resval; + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + wscrl(woffs,1); + wscrl(whex,1); + wscrl(wascii,1); + + miniinit(); + + lineout(16); + resval=1; + } + else + { + resval=0; + } + return(resval); +} + +/*************************************************************************** + * FUNCTION: int scrollu() * + * GOOD FOR: Scrolling the scrren one line up * + * RESULTS : 1: at first element // 0: everything was nice * + ***************************************************************************/ +int scrollu() +{ + int z=0; + int result; + while(((*pdatas).plast != NULL)&&(z < 16)) + { + pdatas=(*pdatas).plast; + z++; + } + result= (z!=15)?1:0; + if(z>15) + { + z--; + wscrl(woffs,-1); + wscrl(whex,-1); + wscrl(wascii,-1); + + miniinit(); + + lineout(1); + } + while(z--) + { + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + } + } + return(result); +} + +/*************************************************************************** + * FUNCTION: void switchmode() * + * GOOD FOR: recalculation of cursor-position ascii<->hex * + ***************************************************************************/ +void switchmode() +{ + if(mode==0) + { + mode=1; // mode-toggle:Hex->Ascii + // recalculate cursor-position + pos.x=((pos.x+1)/3); + } + else + { + mode=0; // mode-toggle:ascii->Hex + // recalculate cursor-position + pos.x=((pos.x*3)-1); + } +} + +/*************************************************************************** + * FUNCTION: void moveleft() * + * GOOD FOR: move the cursor one position to the left * + ***************************************************************************/ +void moveleft() +{ + if(mode==0) + { + if(pos.x > 2) + { + (pos.x%3)?pos.x-=2:pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=48; + } + else + { + if(scrollu())pos.x=48; + } + } + } + else + { + if(pos.x > 1) + { + pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=16; + } + else + { + if(scrollu())pos.x=16; + } + } + } +} + +/*************************************************************************** + * FUNCTION: void moveright() * + * GOOD FOR: move the cursor one position to the right * + ***************************************************************************/ +void moveright() +{ + if(mode==0) + { + if(pos.x < 48) + { + (pos.x%3)?pos.x++:pos.x+=2; + } + else + { + if(pos.y < 16) + { + pos.x=2; + pos.y++; + } + else + { + if(scrolld()) pos.x=2; + } + } + } + else + { + if((pos.x < 16)) + { + pos.x++; + } + else + { + if(pos.y < 16) + { + pos.x=1; + pos.y++; + } + else + { + if(scrolld()) pos.x=1; + } + } + } +} + +/*************************************************************************** + * FUNCTION: unsigned long getactpos() * + * GOOD FOR: get the acutal cursor-position as a file-offset * + * RESULTS : The file-offset on the cursor-position as unsigned long * + ***************************************************************************/ +unsigned long getactpos() +{ + int i=0; + unsigned long actpos; + int distance; + int posxsave=9999; + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the position + actpos=((*pdatas).line*16)+pos.x-1; + // and move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + if (posxsave!=9999) + { + switchmode(); + pos.x=posxsave; + } + + return(actpos); +} + +/*************************************************************************** + * FUNCTION: void charin(char ch, int realmode, int realxpos) * + * GOOD FOR: insert/override one character in the memory * + * INPUTS : - char ch -the char to insert * + * - int realmode -the real mode (hex/ascii) * + * - int realxpos -the real x-position of the cursor * + ***************************************************************************/ +void charin(char ch, int realmode, int realxpos) +{ + int i=0; + int z=0; + int distance=0; + bool toggleswitch; + data *phelp2; + char rbmode; //rollbackmode (if file < 255 chars = 1 else 2) + unsigned long zhelp; + // move to the cursor-position in memory + // Is the file smaller then one page, it has to be handled in another way. + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + rbmode=1; + } + else + { + distance=16-pos.y; + rbmode=2; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // For the insert-mode + if (((insovr=='I') && (realmode==1))||((insovr=='I') && (realmode==0) && (realxpos%3==2)) && (realmode!=3)) + { + phelp=pdatas; + pdatas=pmax; + if ((*pdatas).len < 16) + { + (*pdatas).len++; + } + else + { + // allocate new element + phelp2=pmax; + zhelp=(*pmax).line+1; + pdatas=pmax=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); + if(!pmax) {abort();} + (*pmax).plast=phelp2; + (*pmax).pnext=NULL; + (*pmax).len=1; + (*pmax).line=zhelp; + if (rbmode == 1) phelp=pmax; + } + while(pdatas!=phelp) + { + for(z=(*pdatas).len-1;z>=0;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + (*pdatas).vals[0]=(*(*pdatas).plast).vals[15]; + pdatas=(*pdatas).plast; + } + + for(z=(*pdatas).len-1;z+1>pos.x;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + pdatas=phelp; + } + // Write value + (*pdatas).vals[pos.x-1]=ch; + // refresh output of current line (not needed, I think) + lineout(pos.y); + // moving backwards again and print the lines immediate. + if (rbmode==1) + { + initout(); + } + else + { + while(i--) + { + if((*pdatas).pnext) + { + pdatas=(*pdatas).pnext; + lineout(16-i); + } + } + } +} + +/*************************************************************************** + * FUNCTION: int charout() * + * GOOD FOR: get the char at the actual cursor-position * + * RESULTS : the char as an integer (char should work too normaly) * + ***************************************************************************/ +int charout() +{ + int i=0; + int distance; + int posxsave; + int ch; + // Move in memory to cursor-position + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the value + ch=((*pdatas).vals[pos.x-1]); + // move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + return(ch); +} + +/*************************************************************************** + * FUNCTION: void bottomout() * + * GOOD FOR: refreshing the output of the bottom-window * + ***************************************************************************/ +void bottomout() +{ + int realmode,realposx; + realmode=mode; + + if (realmode == 0) + { + realposx=pos.x; + switchmode(); + } + mvwaddstr(wbot,1,1,empty78); + mvwaddstr(wbot,2,1,empty78); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,1,"CHR:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddch(wbot,1,6,32); + mvwaddch(wbot,1,5,(isgraph(charout()))?charout():'.'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,9,"HEX:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,13,(tonumsysx(charout(),16))+6); + mvwaddch(wbot,1,15,'h'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,18,"DEC:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,22,(tonumsysx(charout(),10))+5); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,27,"OCT:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,31,(tonumsysx(charout(),8))+4); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,37,"BIN:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,41,tonumsysx(charout(),2)); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,52,"Offset:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,59,tonumsysx(getactpos(),16)); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,67,"h -"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,71,tonumsysx(getactpos(),10)); + + mvwaddstr(wbot,2,1,filename); + mvwaddstr(wbot,2,76,(insovr=='O')?"OVR":"INS"); + if (realmode == 0) + { + switchmode(); + pos.x=realposx; + } +} + +/*************************************************************************** + * FUNCTION: void swapbits() * + * GOOD FOR: toggle the bit [par1] an the actual position * + * INPUTS : - char bit the number (1-8) of the bit to toggle * + ***************************************************************************/ +void swapbits(char bit) +{ + char bin[]="12345678",*pbin; // for the Bits + pbin=bin; + int realposx, realmode; + realmode=mode; + if (realmode==0) + { + realposx=pos.x; + switchmode(); + } + pbin=tonumsysx(charout(),2); + pbin[bit]= (pbin[bit]=='0')?'1':'0'; + charin(xto10(pbin,2),3,pos.x); + if (realmode==0) + { + switchmode(); + pos.x=realposx; + } +} + +/*************************************************************************** + * FUNCTION: void delactchar(char ch, int realmode, int realxpos) * + * GOOD FOR: delete the char under the actual cursor-position * + * INPUTS : - int realmode -the real mode (hex/ascii) * + * - int realxpos -the real x-position of the cursor * + ***************************************************************************/ +void delactchar(int realmode, int realxpos) +{ + //BUG: after deletion the last char in the last line it gives me a segfault. + int i=0; + int z=0; + int distance=0; + bool endflag=FALSE; + bool toggleswitch; + char rbmode; //rollbackmode (if file < 255 chars = 1 else 2) + + // move to the cursor-position in memory + // Is the file smaller then one page, it has to be handled in another way. + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + rbmode=1; + } + else + { + distance=16-pos.y; + rbmode=2; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // For the insert-mode + if (((insovr=='I') && (realmode==1))||((insovr=='I') && (realmode==0) && (realxpos%3==2)) && (realmode!=3)) + { + phelp=pdatas; + for(z=realxpos-1;z<15;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + if((*pdatas).pnext) + { + (*pdatas).vals[15]=(*(*pdatas).pnext).vals[0]; + pdatas=(*pdatas).pnext; + } + while(pdatas!=pmax) + { + for(z=0;z<15;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + (*pdatas).vals[15]=(*(*pdatas).pnext).vals[0]; + pdatas=(*pdatas).pnext; + } +// check here for last char in last element + (*pdatas).len--; + if((*pdatas).len <= 0) + { + if(phelp==pmax) + { + if((*phelp).plast) + { + phelp=(*phelp).plast; + } + } + if(pfirst!=pmax) // is when pfirst!=pmax + { + pdatas=(*pdatas).plast; + free((*pdatas).pnext); + (*pdatas).pnext=NULL; + pmax=pdatas; + // TODO: there is one line less now, when we are at the last screen + // we should decrease i by one (only cosmetic) + endflag=TRUE; + } + else + { + (*pdatas).len=0; + } + } + else + { + if (phelp!=pmax) for(z=0;z<(*pdatas).len;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + } + pdatas=phelp; + } + // refresh output of current line (not needed, I think) + lineout(pos.y); + // moving backwards again and print the lines immediate. + if (rbmode==1) + { + // first delete the screen + for(z=0;z<16;z++) + { + emplineout(z+1); + } + // then do an initial output + initout(); + } + else + { + while(i--) + { + if((*pdatas).pnext) + { + pdatas=(*pdatas).pnext; + lineout(16-i); + } + } + } +// The rest is only a dirty workaround for the las (no longer needed) line +// because it was not possible for me to determine out, whether i am really +// at the end. + if (endflag) + { + for(i=0;i<16;i++) + { + if(!scrollu()) endflag=FALSE; + } + if (endflag) + { + if((pos.y < 16) && ((*pmax).line > 15)) pos.y++; + for(i=0;i<16;i++) + { + scrolld(); + } + } + } +} + +int main(int argc, char *argv[]) +{ +//Variables + unsigned int i; // Public counter + int inpch; // The char of input + +//Preperation + printf("T.I.A.H - Tiah Is A Hex-editor. Version 0.5 (C) Joerg Tretter 1999\n"); + //Is there a param? + if(argc < 2) + { + fprintf(stderr,"T.I.A.H -MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //open the file + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"T.I.A.H -MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // set the pointers + initvar(); + // init of other useful varibles + pos.x = pos.y = 1; + filename=argv[1]; + //file -> ram + if(loadcontents()) + { + fprintf(stderr,"T.I.A.H -MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // init of terminal + if(screeninit()) + { + endscr(); + fprintf(stderr,"T.I.A.H -MESSAGE; !!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + + +//Main-procedure +initout(); +bottomout(); +scrrefresh(); + +//Event-loop + while(inpch!=KEY_F(10)) + { + if (mode==0) + { + inpch=wgetch(whex); + } + else + { + inpch=wgetch(wascii); + } + switch(inpch) + { + // The Keys F1-F8 (bit-setting) + case KEY_F(9): switchmode(); + break; + + case KEY_F(1): swapbits(0); + break; + + case KEY_F(2): swapbits(1); + break; + + case KEY_F(3): swapbits(2); + break; + + case KEY_F(4): swapbits(3); + break; + + case KEY_F(5): swapbits(4); + break; + + case KEY_F(6): swapbits(5); + break; + + case KEY_F(7): swapbits(6); + break; + + case KEY_F(8): swapbits(7); + break; + + //the delete character-key + case KEY_DC : if (mode == 1) + { + delactchar(1,pos.x); + } + else + { + switchmode(); + delactchar(1,pos.x); + switchmode(); + } + break; + + //the insert-key, switch insert mode + case KEY_EIC: //fallthru + case KEY_IC : if (insovr=='O') + { + insovr='I'; + } + else + { + insovr='O'; + } + break; + + //Cursor-movement + case KEY_LEFT: moveleft(); + break; + + case KEY_RIGHT:moveright(); + break; + + case KEY_DOWN: if(pos.y>15) + { + scrolld(); + } + else + { + pos.y++; + } + break; + + case KEY_UP: if(pos.y<2) + { + scrollu(); + } + else + { + pos.y--; + } + break; + + case KEY_NPAGE:for(i=0;i<16;i++) + { + scrolld(); + } + break; + + case KEY_PPAGE:for(i=0;i<16;i++) + { + scrollu(); + } + break; + + case KEY_HOME: initout(); + break; + + case KEY_END: if((*pmax).line > 15) + { + pdatas=pmax; + for(i=0;((*pdatas).plast) && (i < 16);i++) + { + pdatas=(*pdatas).plast; + } + while((*pdatas).pnext) + { + scrolld(); + } + } + break; + // input characters + default: if(mode==0) + { + if(isdigit(inpch) || (toupper(inpch)>='A' && toupper(inpch)<='F') ) + { + waddch(whex,toupper(inpch)); + char hch[]="00"; + // fill hch correct (old values) + int xpos=pos.x; // save position + switchmode(); // recalc. coordinates + ptv=tonumsysx(charout(),16); // actual char in Hex. + //* --- DEBUG ONLY + mvwaddstr(wbot,2,30,tonumsysx(charout(),16)); + // --- END DEBUG + switchmode(); // recalc coordinates + pos.x=xpos; // restore coorinates + if (insovr=='O') // Predef. only in Override! + { + hch[0] = *(ptv+6); + hch[1] = *(ptv+7); + } + else // In Insert only first char + { + if(pos.x%3==0) + hch[0] = *(ptv+6); + } + //* --- DEBUG ONLY + mvwaddstr(wbot,2,1,hch); + mvwaddstr(wbot,2,20,ptv); + // --- END DEBUG + hch[(pos.x%3)?0:1]=inpch; + switchmode(); // recalc coordinates + charin(xto10(hch,16),0,xpos); // calc Hex -> Ascii+charin + switchmode(); // and back + pos.x=xpos; // restore positions + moveright(); + } + } + + else + { + if((inpch < 256)) + { + waddch(wascii,inpch); + charin(inpch,1,pos.x); + moveright(); + } + } + }; + + bottomout(); + scrrefresh(); + + } + + WINDOW* s=derwin(whex,3,17,7,17); + wattrset(s,COLOR_PAIR(1)); + box(s,0,0); + wattrset(s,COLOR_PAIR(3)); + mvwaddstr(s,1,1,"SAVE FILE (Y/N)"); + wrefresh(s); + int saveit= wgetch(s); + endscr(); + unallocmem(saveit,argv[1]); + + return (fclose(fp)); +} diff --git a/tiah05/COPYING b/tiah05/COPYING new file mode 100644 index 0000000..a43ea21 --- /dev/null +++ b/tiah05/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 675 Mass Ave, Cambridge, MA 02139, USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + Appendix: How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) 19yy + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19yy name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/tiah05/Makefile b/tiah05/Makefile new file mode 100644 index 0000000..927ab66 --- /dev/null +++ b/tiah05/Makefile @@ -0,0 +1,40 @@ +############################################################################ +# # +# T.I.A.H - THIA IS A HEX-EDITOR # +# Copyright (C) 1999 by Joerg "Maddog" Tretter # +# # +############################################################################ + +PRGFILE = tiah +SRC = tiah.cc +MANFILE = tiah.1.gz + +INSTPATH = /usr/local/bin +MANPATH = /usr/man/man1 +DOCPATH = /usr/doc/tiah + +MAKE = make +CC = c++ +LIBS += -lncurses + +all: + $(MAKE) $(PRGFILE) + + +.cc: + @echo compiling $(SRC) . + @$(CC) $(LIBS) $(SRC) -o$(PRGFILE) + +install: + @install -s -g users -m 755 -o root $(PRGFILE) $(INSTPATH) + @install -d -g root -o root $(MANPATH) + @install -g root -m 644 -o root $(MANFILE) $(MANPATH) + @install -d -g root -o root $(DOCPATH) + @install -g root -m 644 -o root COPYING $(DOCPATH) + @install -g root -m 644 -o root README.PROGRAMMER $(DOCPATH) + @install -g root -m 644 -o root README.INSTALL $(DOCPATH) + @install -g root -m 644 -o root README.BUG $(DOCPATH) + +clean: + @echo removing object $(PRGFILE). + @rm $(PRGFILE) diff --git a/tiah05/README.BUG b/tiah05/README.BUG new file mode 100644 index 0000000..9213e9c --- /dev/null +++ b/tiah05/README.BUG @@ -0,0 +1 @@ +For known bugs see manpage. diff --git a/tiah05/README.INSTALL b/tiah05/README.INSTALL new file mode 100644 index 0000000..84f25af --- /dev/null +++ b/tiah05/README.INSTALL @@ -0,0 +1,24 @@ +TIAH (C) Joerg Tretter + +What do you need: +================= +- linux on a i386+ (other systems perhaps work, too) +- g++ or compatible +- ncurses library (normally installed on every linux-system) + +Steps of installation: +====================== +- edit Makefile if you want an alternative install-dir + (standard is /usr/local/bin) +- make && make install + (you will get some warnings, but that doesn't matter, + if you get an error and can't solve yourself: please report via mail) + +Please send me bug reports and other systems TIAH works on, too. + +If you like the program, please let me know (it's interesting for me to know +how may people are using TIAH.) + +Joerg Tretter +J.Tretter@gmx.de + diff --git a/tiah05/README.PROGRAMMER b/tiah05/README.PROGRAMMER new file mode 100644 index 0000000..7ffff92 --- /dev/null +++ b/tiah05/README.PROGRAMMER @@ -0,0 +1,24 @@ +TIAH (C) Joerg Tretter + +I know, the code abolutely cofusing, but keep in mind: + +This version is only the first prototype of TIAH. + +There is a complete revision of the source planned for the next release. + +So if you plan to adapt new features in this program better wait for +the new versions with a cleaned, object oriented source. + +Nevertheless, please send me every modification you did for fixing bugs. + +Plans for next releases +1. reengineer source-code (using classes, make code readable for others) +2. search-fuction +3. replace-function +4. fill-function +5. internal calculator (source numbering-system <-> target-numbering system) +6. perhaps various goodies :-)) +7. Changes suggested by others. + +Joerg Tretter +J.Tretter@gmx.de diff --git a/tiah05/tiah.1.gz b/tiah05/tiah.1.gz new file mode 100644 index 0000000..468aaaf Binary files /dev/null and b/tiah05/tiah.1.gz differ diff --git a/tiah05/tiah.cc b/tiah05/tiah.cc new file mode 100644 index 0000000..3b9044f --- /dev/null +++ b/tiah05/tiah.cc @@ -0,0 +1,1248 @@ +/* + * TIAH -- an advanced HEX-Editor + * Copyright (C) 1999 by Joerg Tretter + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include +#include + +struct data // one line in Memory +{ +struct data* plast; +struct data* pnext; +unsigned char vals[16]; +unsigned long line; +char len; +}; + +struct cpos // Cursor-Position +{ + char x,y; +}pos; + +char insovr='O'; // Isert/Override-Mode +int mode=1; +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII "; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +char empty78[]=" "; +int result; +char *filename; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data datas, *pdatas, *pfirst, *phelp, *pmax; + +/*************************************************************************** + * FUNCTION: void initvar() * + * GOOD FOR: Initialization of basic-variables * + ***************************************************************************/ +void initvar() +{ + pfirst=&datas; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + (*pfirst).line=0; + (*pfirst).len=0; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +/*************************************************************************** + * FUNCTION: void miniinit() * + * GOOD FOR: Initialization of frequent used variables * + ***************************************************************************/ +void miniinit() +{ +xoffs = " "; +xhex = " "; +xascii = " "; +} + +/*************************************************************************** + * FUNCTION: int screeninit() * + * GOOD FOR: Initialization of the n-curses-screen * + * RESULTS : 1:There was an error / 2:Everything ok. * + ***************************************************************************/ +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Create Curses-space for the windows +//TODO: check whether creation was fine. + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// enable scrolling + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// enable Keyboard-control + keypad(stdscr,1); + keypad(whex,1); + keypad(wascii,1); + +// disable echoing of input + noecho(); + +// swich cursor to big size +// TODO: check why this not works in most cases +// if(curs_set(2)==ERR) retval=1 ; + +// creation of curses "Color-Pairs" + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + if(init_pair(3,COLOR_MAGENTA,COLOR_BLACK)) retval=1; + +// join the windows with the color-pairs + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + +// paint the boxes around the windows (in fact it's inside) + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + // change the color-pairs of the windows again. + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +/*************************************************************************** + * FUNCTION: char* tonumsysx() * + * GOOD FOR: Shift a number to another numbering system * + * INPUTS : - int base10 -The number in decimal system * + * - int newbase -The new system (16->Hex,2->Bin,...) * + * RESULTS : The number in the new system as a pointer of char * + ***************************************************************************/ +char* tonumsysx(int base10,int newbase) +{ + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +/*************************************************************************** + * FUNCTION: unsigned long xto10(char *c, int oldbase) * + * GOOD FOR: Shift a number from a foreign numbering system to decimal * + * INPUTS : - char* c -The number in foreign system * + * - int oldbase -The old system (16->Hex,2->Bin,...) * + * RESULTS : The number in decimal system as a unsigned long * + ***************************************************************************/ +unsigned long xto10(char *c, int oldbase) +{ +unsigned long base10=0; +int pos=0; +int i; + for (i=0;c[i];i++, c[i]=toupper(c[i])); + while(c[pos++]); + pos--; + int len=pos-1; + while(pos--) + { + if(isdigit(c[len-pos])) + { + base10 = base10 + (unsigned long)( (double)(c[len-pos]-'0') * ( pow( (double)oldbase,(double)pos ) ) ) ; + } + else + { + base10 = base10 + (unsigned long)( ((double)(c[len-pos]-'A')+10) * ( pow( (double)oldbase,(double)pos ) ) ); + } + } + return(base10); +} + +/*************************************************************************** + * FUNCTION: int loadcontents() * + * GOOD FOR: Loading the contents of a file into the memory * + * RESULTS : The status of the loading (not used yet) * + ***************************************************************************/ +int loadcontents() +{ + int retval=0; + int x; + char ch; + unsigned long line=0; + + + pdatas=pfirst; + ch=fgetc(fp); + // Zero-byte-file. + if(feof(fp)) + { + fprintf(stderr,"T.I.A.H - Tiah Is A Hex-editor. - opening 0-Byte file"); + pfirst=pmax=pdatas=(data*)(malloc(sizeof(datas))); + (*pdatas).pnext=NULL; + (*pdatas).plast=NULL; + (*pdatas).line=0; + (*pdatas).len=0; + + } + // TODO: how to flush the buffer of stdout??? + printf("Printing hashmark for every 8 Kbytes.\n"); + while (!(feof(fp))) + { + for(x=0;x<16;x++) + { + ((*pdatas).vals[x])=ch; + if (feof(fp)) //(*pdatas).vals[x]==EOF) + { + (*pdatas).vals[x]=0; + break; + } + (*pdatas).len=x+1; + ch=fgetc(fp); + } + (*pdatas).line=line++; + if(!(feof(fp))) + { + phelp=pdatas; + if (!(line%500)) {printf("%c",'#');} + pdatas=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); +// BUG: Check for Memory-Full does not work. + if(!pdatas) {abort();} + (*pdatas).plast=phelp; + (*pdatas).pnext=NULL; + } + pmax=pdatas; + + } + return (retval); +} + +/*************************************************************************** + * FUNCTION: void endscr() * + * GOOD FOR: End the scrren session on curses-level * + ***************************************************************************/ +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +/*************************************************************************** + * FUNCTION: void unallocmem(int save, char* filename) * + * GOOD FOR: Free the memory from the bits & bytes * + * INPUTS : int save: flag (save Y/else key) * + * : cahr* filename: the name of the save-file * + ***************************************************************************/ +void unallocmem(int save, char *filename) +{ + int pos; + save=toupper(save); + if (save=='Y') + { + pdatas=pfirst; + fp=fopen(filename,"w"); + while((*pdatas).pnext) + { + for (pos=0;pos <(*pdatas).len;pos++) + { + fputc((*pdatas).vals[pos],fp); + } + pdatas=(*pdatas).pnext; + } + for (pos=0;pos <(*pdatas).len;pos++) fputc((*pdatas).vals[pos],fp); + close(fp); + } + { + pdatas=pmax; + } + while((*pdatas).plast) + { + phelp=(*pdatas).plast; + free(pdatas); + pdatas=phelp; + } +} + +/*************************************************************************** + * FUNCTION: void scrrefresh() * + * GOOD FOR: Refreah the basic-screen-elements (not the contents) * + ***************************************************************************/ +void scrrefresh() +{ + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + // depending on the mode (ascii/hex) set the cursor-position + !mode?wmove(whex,pos.y,pos.x):wmove(wascii,pos.y,pos.x); + + wrefresh(woffs); + wrefresh(wtop); + wrefresh(wbot); + // the order is given by the mode (ascii/hex). the last window refreshed + // it the one, in which the cursor appears and the input-focus is. + !mode?wrefresh(wascii):wrefresh(whex); + mode?wrefresh(wascii):wrefresh(whex); +} + +/*************************************************************************** + * FUNCTION: void emplineout(unsigned long line) * + * GOOD FOR: Writing one empty line to the scrren * + ***************************************************************************/ +void emplineout(unsigned long line) +{ + mvwaddstr(woffs,line,1," "); + mvwaddstr(whex,line,1," "); + mvwaddstr(wascii,line,1," "); +} + +/*************************************************************************** + * FUNCTION: void lineout(unsigned long line) * + * GOOD FOR: Writing one line (the actual one) from the memory to screen * + ***************************************************************************/ +void lineout(unsigned long line) +{ + int z; + + miniinit(); + + ptv = tonumsysx((*pdatas).line*16,16); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,line,1,soffs); + for (z=0;z<(*pdatas).len;z++) + { + ptv = tonumsysx((*pdatas).vals[z],16); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,line,1,shex); + for (z=0;z<(*pdatas).len;z++) + { +// TODO: There are many other characters that are displayable. + xascii[z] = (isgraph((*pdatas).vals[z]))?((*pdatas).vals[z]):'.'; + } + mvwaddstr(wascii,line,1,sascii); +} + +/*************************************************************************** + * FUNCTION: void initout() * + * GOOD FOR: First output after loading the file // home-Key * + ***************************************************************************/ +void initout() +{ +//Initial output (All) +int i,z; +bool toggleswitch=false; +pdatas=pfirst; + +if ((*pdatas).pnext == NULL) toggleswitch=true; +for(i=0;i<16;i++) + { + lineout(i+1); + if (toggleswitch) break; + pdatas=(*pdatas).pnext; + if ((*pdatas).pnext == NULL) toggleswitch=true; + } + scrrefresh(); + +if(i>15) pdatas=(*pdatas).plast; +} + +/*************************************************************************** + * FUNCTION: int scrolld() * + * GOOD FOR: Scrolling the scrren one line down * + * RESULTS : 1: no more elements // 0: everything was nice * + ***************************************************************************/ +int scrolld() +{ + int resval; + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + wscrl(woffs,1); + wscrl(whex,1); + wscrl(wascii,1); + + miniinit(); + + lineout(16); + resval=1; + } + else + { + resval=0; + } + return(resval); +} + +/*************************************************************************** + * FUNCTION: int scrollu() * + * GOOD FOR: Scrolling the scrren one line up * + * RESULTS : 1: at first element // 0: everything was nice * + ***************************************************************************/ +int scrollu() +{ + int z=0; + int result; + while(((*pdatas).plast != NULL)&&(z < 16)) + { + pdatas=(*pdatas).plast; + z++; + } + result= (z!=15)?1:0; + if(z>15) + { + z--; + wscrl(woffs,-1); + wscrl(whex,-1); + wscrl(wascii,-1); + + miniinit(); + + lineout(1); + } + while(z--) + { + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + } + } + return(result); +} + +/*************************************************************************** + * FUNCTION: void switchmode() * + * GOOD FOR: recalculation of cursor-position ascii<->hex * + ***************************************************************************/ +void switchmode() +{ + if(mode==0) + { + mode=1; // mode-toggle:Hex->Ascii + // recalculate cursor-position + pos.x=((pos.x+1)/3); + } + else + { + mode=0; // mode-toggle:ascii->Hex + // recalculate cursor-position + pos.x=((pos.x*3)-1); + } +} + +/*************************************************************************** + * FUNCTION: void moveleft() * + * GOOD FOR: move the cursor one position to the left * + ***************************************************************************/ +void moveleft() +{ + if(mode==0) + { + if(pos.x > 2) + { + (pos.x%3)?pos.x-=2:pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=48; + } + else + { + if(scrollu())pos.x=48; + } + } + } + else + { + if(pos.x > 1) + { + pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=16; + } + else + { + if(scrollu())pos.x=16; + } + } + } +} + +/*************************************************************************** + * FUNCTION: void moveright() * + * GOOD FOR: move the cursor one position to the right * + ***************************************************************************/ +void moveright() +{ + if(mode==0) + { + if(pos.x < 48) + { + (pos.x%3)?pos.x++:pos.x+=2; + } + else + { + if(pos.y < 16) + { + pos.x=2; + pos.y++; + } + else + { + if(scrolld()) pos.x=2; + } + } + } + else + { + if((pos.x < 16)) + { + pos.x++; + } + else + { + if(pos.y < 16) + { + pos.x=1; + pos.y++; + } + else + { + if(scrolld()) pos.x=1; + } + } + } +} + +/*************************************************************************** + * FUNCTION: unsigned long getactpos() * + * GOOD FOR: get the acutal cursor-position as a file-offset * + * RESULTS : The file-offset on the cursor-position as unsigned long * + ***************************************************************************/ +unsigned long getactpos() +{ + int i=0; + unsigned long actpos; + int distance; + int posxsave=9999; + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the position + actpos=((*pdatas).line*16)+pos.x-1; + // and move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + if (posxsave!=9999) + { + switchmode(); + pos.x=posxsave; + } + + return(actpos); +} + +/*************************************************************************** + * FUNCTION: void charin(char ch, int realmode, int realxpos) * + * GOOD FOR: insert/override one character in the memory * + * INPUTS : - char ch -the char to insert * + * - int realmode -the real mode (hex/ascii) * + * - int realxpos -the real x-position of the cursor * + ***************************************************************************/ +void charin(char ch, int realmode, int realxpos) +{ + int i=0; + int z=0; + int distance=0; + bool toggleswitch; + data *phelp2; + char rbmode; //rollbackmode (if file < 255 chars = 1 else 2) + unsigned long zhelp; + // move to the cursor-position in memory + // Is the file smaller then one page, it has to be handled in another way. + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + rbmode=1; + } + else + { + distance=16-pos.y; + rbmode=2; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // For the insert-mode + if (((insovr=='I') && (realmode==1))||((insovr=='I') && (realmode==0) && (realxpos%3==2)) && (realmode!=3)) + { + phelp=pdatas; + pdatas=pmax; + if ((*pdatas).len < 16) + { + (*pdatas).len++; + } + else + { + // allocate new element + phelp2=pmax; + zhelp=(*pmax).line+1; + pdatas=pmax=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); + if(!pmax) {abort();} + (*pmax).plast=phelp2; + (*pmax).pnext=NULL; + (*pmax).len=1; + (*pmax).line=zhelp; + if (rbmode == 1) phelp=pmax; + } + while(pdatas!=phelp) + { + for(z=(*pdatas).len-1;z>=0;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + (*pdatas).vals[0]=(*(*pdatas).plast).vals[15]; + pdatas=(*pdatas).plast; + } + + for(z=(*pdatas).len-1;z+1>pos.x;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + pdatas=phelp; + } + // Write value + (*pdatas).vals[pos.x-1]=ch; + // refresh output of current line (not needed, I think) + lineout(pos.y); + // moving backwards again and print the lines immediate. + if (rbmode==1) + { + initout(); + } + else + { + while(i--) + { + if((*pdatas).pnext) + { + pdatas=(*pdatas).pnext; + lineout(16-i); + } + } + } +} + +/*************************************************************************** + * FUNCTION: int charout() * + * GOOD FOR: get the char at the actual cursor-position * + * RESULTS : the char as an integer (char should work too normaly) * + ***************************************************************************/ +int charout() +{ + int i=0; + int distance; + int posxsave; + int ch; + // Move in memory to cursor-position + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the value + ch=((*pdatas).vals[pos.x-1]); + // move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + return(ch); +} + +/*************************************************************************** + * FUNCTION: void bottomout() * + * GOOD FOR: refreshing the output of the bottom-window * + ***************************************************************************/ +void bottomout() +{ + int realmode,realposx; + realmode=mode; + + if (realmode == 0) + { + realposx=pos.x; + switchmode(); + } + mvwaddstr(wbot,1,1,empty78); + mvwaddstr(wbot,2,1,empty78); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,1,"CHR:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddch(wbot,1,6,32); + mvwaddch(wbot,1,5,(isgraph(charout()))?charout():'.'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,9,"HEX:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,13,(tonumsysx(charout(),16))+6); + mvwaddch(wbot,1,15,'h'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,18,"DEC:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,22,(tonumsysx(charout(),10))+5); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,27,"OCT:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,31,(tonumsysx(charout(),8))+4); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,37,"BIN:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,41,tonumsysx(charout(),2)); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,52,"Offset:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,59,tonumsysx(getactpos(),16)); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,67,"h -"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,71,tonumsysx(getactpos(),10)); + + mvwaddstr(wbot,2,1,filename); + mvwaddstr(wbot,2,76,(insovr=='O')?"OVR":"INS"); + if (realmode == 0) + { + switchmode(); + pos.x=realposx; + } +} + +/*************************************************************************** + * FUNCTION: void swapbits() * + * GOOD FOR: toggle the bit [par1] an the actual position * + * INPUTS : - char bit the number (1-8) of the bit to toggle * + ***************************************************************************/ +void swapbits(char bit) +{ + char bin[]="12345678",*pbin; // for the Bits + pbin=bin; + int realposx, realmode; + realmode=mode; + if (realmode==0) + { + realposx=pos.x; + switchmode(); + } + pbin=tonumsysx(charout(),2); + pbin[bit]= (pbin[bit]=='0')?'1':'0'; + charin(xto10(pbin,2),3,pos.x); + if (realmode==0) + { + switchmode(); + pos.x=realposx; + } +} + +/*************************************************************************** + * FUNCTION: void delactchar(char ch, int realmode, int realxpos) * + * GOOD FOR: delete the char under the actual cursor-position * + * INPUTS : - int realmode -the real mode (hex/ascii) * + * - int realxpos -the real x-position of the cursor * + ***************************************************************************/ +void delactchar(int realmode, int realxpos) +{ + //BUG: after deletion the last char in the last line it gives me a segfault. + int i=0; + int z=0; + int distance=0; + bool endflag=FALSE; + bool toggleswitch; + char rbmode; //rollbackmode (if file < 255 chars = 1 else 2) + + // move to the cursor-position in memory + // Is the file smaller then one page, it has to be handled in another way. + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + rbmode=1; + } + else + { + distance=16-pos.y; + rbmode=2; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // For the insert-mode + if (((insovr=='I') && (realmode==1))||((insovr=='I') && (realmode==0) && (realxpos%3==2)) && (realmode!=3)) + { + phelp=pdatas; + for(z=realxpos-1;z<15;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + if((*pdatas).pnext) + { + (*pdatas).vals[15]=(*(*pdatas).pnext).vals[0]; + pdatas=(*pdatas).pnext; + } + while(pdatas!=pmax) + { + for(z=0;z<15;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + (*pdatas).vals[15]=(*(*pdatas).pnext).vals[0]; + pdatas=(*pdatas).pnext; + } +// check here for last char in last element + (*pdatas).len--; + if((*pdatas).len <= 0) + { + if(phelp==pmax) + { + if((*phelp).plast) + { + phelp=(*phelp).plast; + } + } + if(pfirst!=pmax) // is when pfirst!=pmax + { + pdatas=(*pdatas).plast; + free((*pdatas).pnext); + (*pdatas).pnext=NULL; + pmax=pdatas; + // TODO: there is one line less now, when we are at the last screen + // we should decrease i by one (only cosmetic) + endflag=TRUE; + } + else + { + (*pdatas).len=0; + } + } + else + { + if (phelp!=pmax) for(z=0;z<(*pdatas).len;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + } + pdatas=phelp; + } + // refresh output of current line (not needed, I think) + lineout(pos.y); + // moving backwards again and print the lines immediate. + if (rbmode==1) + { + // first delete the screen + for(z=0;z<16;z++) + { + emplineout(z+1); + } + // then do an initial output + initout(); + } + else + { + while(i--) + { + if((*pdatas).pnext) + { + pdatas=(*pdatas).pnext; + lineout(16-i); + } + } + } +// The rest is only a dirty workaround for the las (no longer needed) line +// because it was not possible for me to determine out, whether i am really +// at the end. + if (endflag) + { + for(i=0;i<16;i++) + { + if(!scrollu()) endflag=FALSE; + } + if (endflag) + { + if((pos.y < 16) && ((*pmax).line > 15)) pos.y++; + for(i=0;i<16;i++) + { + scrolld(); + } + } + } +} + +int main(int argc, char *argv[]) +{ +//Variables + unsigned int i; // Public counter + int inpch; // The char of input + +//Preperation + printf("T.I.A.H - Tiah Is A Hex-editor. Version 0.5 (C) Joerg Tretter 1999\n"); + //Is there a param? + if(argc < 2) + { + fprintf(stderr,"T.I.A.H -MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //open the file + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"T.I.A.H -MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // set the pointers + initvar(); + // init of other useful varibles + pos.x = pos.y = 1; + filename=argv[1]; + //file -> ram + if(loadcontents()) + { + fprintf(stderr,"T.I.A.H -MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // init of terminal + if(screeninit()) + { + endscr(); + fprintf(stderr,"T.I.A.H -MESSAGE; !!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + + +//Main-procedure +initout(); +bottomout(); +scrrefresh(); + +//Event-loop + while(inpch!=KEY_F(10)) + { + if (mode==0) + { + inpch=wgetch(whex); + } + else + { + inpch=wgetch(wascii); + } + switch(inpch) + { + // The Keys F1-F8 (bit-setting) + case KEY_F(9): switchmode(); + break; + + case KEY_F(1): swapbits(0); + break; + + case KEY_F(2): swapbits(1); + break; + + case KEY_F(3): swapbits(2); + break; + + case KEY_F(4): swapbits(3); + break; + + case KEY_F(5): swapbits(4); + break; + + case KEY_F(6): swapbits(5); + break; + + case KEY_F(7): swapbits(6); + break; + + case KEY_F(8): swapbits(7); + break; + + //the delete character-key + case KEY_DC : if (mode == 1) + { + delactchar(1,pos.x); + } + else + { + switchmode(); + delactchar(1,pos.x); + switchmode(); + } + break; + + //the insert-key, switch insert mode + case KEY_EIC: //fallthru + case KEY_IC : if (insovr=='O') + { + insovr='I'; + } + else + { + insovr='O'; + } + break; + + //Cursor-movement + case KEY_LEFT: moveleft(); + break; + + case KEY_RIGHT:moveright(); + break; + + case KEY_DOWN: if(pos.y>15) + { + scrolld(); + } + else + { + pos.y++; + } + break; + + case KEY_UP: if(pos.y<2) + { + scrollu(); + } + else + { + pos.y--; + } + break; + + case KEY_NPAGE:for(i=0;i<16;i++) + { + scrolld(); + } + break; + + case KEY_PPAGE:for(i=0;i<16;i++) + { + scrollu(); + } + break; + + case KEY_HOME: initout(); + break; + + case KEY_END: if((*pmax).line > 15) + { + pdatas=pmax; + for(i=0;((*pdatas).plast) && (i < 16);i++) + { + pdatas=(*pdatas).plast; + } + while((*pdatas).pnext) + { + scrolld(); + } + } + break; + // input characters + default: if(mode==0) + { + if(isdigit(inpch) || (toupper(inpch)>='A' && toupper(inpch)<='F') ) + { + waddch(whex,toupper(inpch)); + char hch[]="00"; + // fill hch correct (old values) + int xpos=pos.x; // save position + switchmode(); // recalc. coordinates + ptv=tonumsysx(charout(),16); // actual char in Hex. + //* --- DEBUG ONLY + mvwaddstr(wbot,2,30,tonumsysx(charout(),16)); + // --- END DEBUG + switchmode(); // recalc coordinates + pos.x=xpos; // restore coorinates + if (insovr=='O') // Predef. only in Override! + { + hch[0] = *(ptv+6); + hch[1] = *(ptv+7); + } + else // In Insert only first char + { + if(pos.x%3==0) + hch[0] = *(ptv+6); + } + //* --- DEBUG ONLY + mvwaddstr(wbot,2,1,hch); + mvwaddstr(wbot,2,20,ptv); + // --- END DEBUG + hch[(pos.x%3)?0:1]=inpch; + switchmode(); // recalc coordinates + charin(xto10(hch,16),0,xpos); // calc Hex -> Ascii+charin + switchmode(); // and back + pos.x=xpos; // restore positions + moveright(); + } + } + + else + { + if((inpch < 256)) + { + waddch(wascii,inpch); + charin(inpch,1,pos.x); + moveright(); + } + } + }; + + bottomout(); + scrrefresh(); + + } + + WINDOW* s=derwin(whex,3,17,7,17); + wattrset(s,COLOR_PAIR(1)); + box(s,0,0); + wattrset(s,COLOR_PAIR(3)); + mvwaddstr(s,1,1,"SAVE FILE (Y/N)"); + wrefresh(s); + int saveit= wgetch(s); + endscr(); + unallocmem(saveit,argv[1]); + + return (fclose(fp)); +} diff --git a/tiah05/tiah05.tar.bz2 b/tiah05/tiah05.tar.bz2 new file mode 100644 index 0000000..4afbd17 Binary files /dev/null and b/tiah05/tiah05.tar.bz2 differ diff --git a/tiah05/tiah05.tar.gz b/tiah05/tiah05.tar.gz new file mode 100644 index 0000000..23db136 Binary files /dev/null and b/tiah05/tiah05.tar.gz differ diff --git a/tiah05a/CHANGES b/tiah05a/CHANGES new file mode 100644 index 0000000..9892a81 --- /dev/null +++ b/tiah05a/CHANGES @@ -0,0 +1,6 @@ +0.5a: +bugfix, use of proper function strncpy instead of "=" for filling string constants, there was a compile-problem with newer compilers. + +0.5: +first official release + diff --git a/tiah05a/COPYING b/tiah05a/COPYING new file mode 100644 index 0000000..a43ea21 --- /dev/null +++ b/tiah05a/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 675 Mass Ave, Cambridge, MA 02139, USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + Appendix: How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) 19yy + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19yy name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/tiah05a/Makefile b/tiah05a/Makefile new file mode 100644 index 0000000..927ab66 --- /dev/null +++ b/tiah05a/Makefile @@ -0,0 +1,40 @@ +############################################################################ +# # +# T.I.A.H - THIA IS A HEX-EDITOR # +# Copyright (C) 1999 by Joerg "Maddog" Tretter # +# # +############################################################################ + +PRGFILE = tiah +SRC = tiah.cc +MANFILE = tiah.1.gz + +INSTPATH = /usr/local/bin +MANPATH = /usr/man/man1 +DOCPATH = /usr/doc/tiah + +MAKE = make +CC = c++ +LIBS += -lncurses + +all: + $(MAKE) $(PRGFILE) + + +.cc: + @echo compiling $(SRC) . + @$(CC) $(LIBS) $(SRC) -o$(PRGFILE) + +install: + @install -s -g users -m 755 -o root $(PRGFILE) $(INSTPATH) + @install -d -g root -o root $(MANPATH) + @install -g root -m 644 -o root $(MANFILE) $(MANPATH) + @install -d -g root -o root $(DOCPATH) + @install -g root -m 644 -o root COPYING $(DOCPATH) + @install -g root -m 644 -o root README.PROGRAMMER $(DOCPATH) + @install -g root -m 644 -o root README.INSTALL $(DOCPATH) + @install -g root -m 644 -o root README.BUG $(DOCPATH) + +clean: + @echo removing object $(PRGFILE). + @rm $(PRGFILE) diff --git a/tiah05a/README.BUG b/tiah05a/README.BUG new file mode 100644 index 0000000..9213e9c --- /dev/null +++ b/tiah05a/README.BUG @@ -0,0 +1 @@ +For known bugs see manpage. diff --git a/tiah05a/README.INSTALL b/tiah05a/README.INSTALL new file mode 100644 index 0000000..84f25af --- /dev/null +++ b/tiah05a/README.INSTALL @@ -0,0 +1,24 @@ +TIAH (C) Joerg Tretter + +What do you need: +================= +- linux on a i386+ (other systems perhaps work, too) +- g++ or compatible +- ncurses library (normally installed on every linux-system) + +Steps of installation: +====================== +- edit Makefile if you want an alternative install-dir + (standard is /usr/local/bin) +- make && make install + (you will get some warnings, but that doesn't matter, + if you get an error and can't solve yourself: please report via mail) + +Please send me bug reports and other systems TIAH works on, too. + +If you like the program, please let me know (it's interesting for me to know +how may people are using TIAH.) + +Joerg Tretter +J.Tretter@gmx.de + diff --git a/tiah05a/README.PROGRAMMER b/tiah05a/README.PROGRAMMER new file mode 100644 index 0000000..7ffff92 --- /dev/null +++ b/tiah05a/README.PROGRAMMER @@ -0,0 +1,24 @@ +TIAH (C) Joerg Tretter + +I know, the code abolutely cofusing, but keep in mind: + +This version is only the first prototype of TIAH. + +There is a complete revision of the source planned for the next release. + +So if you plan to adapt new features in this program better wait for +the new versions with a cleaned, object oriented source. + +Nevertheless, please send me every modification you did for fixing bugs. + +Plans for next releases +1. reengineer source-code (using classes, make code readable for others) +2. search-fuction +3. replace-function +4. fill-function +5. internal calculator (source numbering-system <-> target-numbering system) +6. perhaps various goodies :-)) +7. Changes suggested by others. + +Joerg Tretter +J.Tretter@gmx.de diff --git a/tiah05a/tiah.1.gz b/tiah05a/tiah.1.gz new file mode 100644 index 0000000..468aaaf Binary files /dev/null and b/tiah05a/tiah.1.gz differ diff --git a/tiah05a/tiah.cc b/tiah05a/tiah.cc new file mode 100644 index 0000000..b327ac6 --- /dev/null +++ b/tiah05a/tiah.cc @@ -0,0 +1,1249 @@ +/* + * TIAH -- an advanced HEX-Editor + * Copyright (C) 1999 by Joerg Tretter + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include +#include +#include + +struct data // one line in Memory +{ +struct data* plast; +struct data* pnext; +unsigned char vals[16]; +unsigned long line; +char len; +}; + +struct cpos // Cursor-Position +{ + char x,y; +}pos; + +char insovr='O'; // Isert/Override-Mode +int mode=1; +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII "; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +char empty78[]=" "; +int result; +char *filename; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data datas, *pdatas, *pfirst, *phelp, *pmax; + +/*************************************************************************** + * FUNCTION: void initvar() * + * GOOD FOR: Initialization of basic-variables * + ***************************************************************************/ +void initvar() +{ + pfirst=&datas; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + (*pfirst).line=0; + (*pfirst).len=0; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +/*************************************************************************** + * FUNCTION: void miniinit() * + * GOOD FOR: Initialization of frequent used variables * + ***************************************************************************/ +void miniinit() +{ +strncpy(xoffs, " ",9); +strncpy(xhex, " ",51); +strncpy(xascii, " ",17); +} + +/*************************************************************************** + * FUNCTION: int screeninit() * + * GOOD FOR: Initialization of the n-curses-screen * + * RESULTS : 1:There was an error / 2:Everything ok. * + ***************************************************************************/ +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Create Curses-space for the windows +//TODO: check whether creation was fine. + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// enable scrolling + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// enable Keyboard-control + keypad(stdscr,1); + keypad(whex,1); + keypad(wascii,1); + +// disable echoing of input + noecho(); + +// swich cursor to big size +// TODO: check why this not works in most cases +// if(curs_set(2)==ERR) retval=1 ; + +// creation of curses "Color-Pairs" + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + if(init_pair(3,COLOR_MAGENTA,COLOR_BLACK)) retval=1; + +// join the windows with the color-pairs + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + +// paint the boxes around the windows (in fact it's inside) + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + // change the color-pairs of the windows again. + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +/*************************************************************************** + * FUNCTION: char* tonumsysx() * + * GOOD FOR: Shift a number to another numbering system * + * INPUTS : - int base10 -The number in decimal system * + * - int newbase -The new system (16->Hex,2->Bin,...) * + * RESULTS : The number in the new system as a pointer of char * + ***************************************************************************/ +char* tonumsysx(int base10,int newbase) +{ + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + strncpy(swap,"********",9); + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +/*************************************************************************** + * FUNCTION: unsigned long xto10(char *c, int oldbase) * + * GOOD FOR: Shift a number from a foreign numbering system to decimal * + * INPUTS : - char* c -The number in foreign system * + * - int oldbase -The old system (16->Hex,2->Bin,...) * + * RESULTS : The number in decimal system as a unsigned long * + ***************************************************************************/ +unsigned long xto10(char *c, int oldbase) +{ +unsigned long base10=0; +int pos=0; +int i; + for (i=0;c[i];i++, c[i]=toupper(c[i])); + while(c[pos++]); + pos--; + int len=pos-1; + while(pos--) + { + if(isdigit(c[len-pos])) + { + base10 = base10 + (unsigned long)( (double)(c[len-pos]-'0') * ( pow( (double)oldbase,(double)pos ) ) ) ; + } + else + { + base10 = base10 + (unsigned long)( ((double)(c[len-pos]-'A')+10) * ( pow( (double)oldbase,(double)pos ) ) ); + } + } + return(base10); +} + +/*************************************************************************** + * FUNCTION: int loadcontents() * + * GOOD FOR: Loading the contents of a file into the memory * + * RESULTS : The status of the loading (not used yet) * + ***************************************************************************/ +int loadcontents() +{ + int retval=0; + int x; + char ch; + unsigned long line=0; + + + pdatas=pfirst; + ch=fgetc(fp); + // Zero-byte-file. + if(feof(fp)) + { + fprintf(stderr,"T.I.A.H - Tiah Is A Hex-editor. - opening 0-Byte file"); + pfirst=pmax=pdatas=(data*)(malloc(sizeof(datas))); + (*pdatas).pnext=NULL; + (*pdatas).plast=NULL; + (*pdatas).line=0; + (*pdatas).len=0; + + } + // TODO: how to flush the buffer of stdout??? + printf("Printing hashmark for every 8 Kbytes.\n"); + while (!(feof(fp))) + { + for(x=0;x<16;x++) + { + ((*pdatas).vals[x])=ch; + if (feof(fp)) //(*pdatas).vals[x]==EOF) + { + (*pdatas).vals[x]=0; + break; + } + (*pdatas).len=x+1; + ch=fgetc(fp); + } + (*pdatas).line=line++; + if(!(feof(fp))) + { + phelp=pdatas; + if (!(line%500)) {printf("%c",'#');} + pdatas=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); +// BUG: Check for Memory-Full does not work. + if(!pdatas) {abort();} + (*pdatas).plast=phelp; + (*pdatas).pnext=NULL; + } + pmax=pdatas; + + } + return (retval); +} + +/*************************************************************************** + * FUNCTION: void endscr() * + * GOOD FOR: End the scrren session on curses-level * + ***************************************************************************/ +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +/*************************************************************************** + * FUNCTION: void unallocmem(int save, char* filename) * + * GOOD FOR: Free the memory from the bits & bytes * + * INPUTS : int save: flag (save Y/else key) * + * : cahr* filename: the name of the save-file * + ***************************************************************************/ +void unallocmem(int save, char *filename) +{ + int pos; + save=toupper(save); + if (save=='Y') + { + pdatas=pfirst; + fp=fopen(filename,"w"); + while((*pdatas).pnext) + { + for (pos=0;pos <(*pdatas).len;pos++) + { + fputc((*pdatas).vals[pos],fp); + } + pdatas=(*pdatas).pnext; + } + for (pos=0;pos <(*pdatas).len;pos++) fputc((*pdatas).vals[pos],fp); + fclose(fp); + } + { + pdatas=pmax; + } + while((*pdatas).plast) + { + phelp=(*pdatas).plast; + free(pdatas); + pdatas=phelp; + } +} + +/*************************************************************************** + * FUNCTION: void scrrefresh() * + * GOOD FOR: Refreah the basic-screen-elements (not the contents) * + ***************************************************************************/ +void scrrefresh() +{ + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + // depending on the mode (ascii/hex) set the cursor-position + !mode?wmove(whex,pos.y,pos.x):wmove(wascii,pos.y,pos.x); + + wrefresh(woffs); + wrefresh(wtop); + wrefresh(wbot); + // the order is given by the mode (ascii/hex). the last window refreshed + // it the one, in which the cursor appears and the input-focus is. + !mode?wrefresh(wascii):wrefresh(whex); + mode?wrefresh(wascii):wrefresh(whex); +} + +/*************************************************************************** + * FUNCTION: void emplineout(unsigned long line) * + * GOOD FOR: Writing one empty line to the scrren * + ***************************************************************************/ +void emplineout(unsigned long line) +{ + mvwaddstr(woffs,line,1," "); + mvwaddstr(whex,line,1," "); + mvwaddstr(wascii,line,1," "); +} + +/*************************************************************************** + * FUNCTION: void lineout(unsigned long line) * + * GOOD FOR: Writing one line (the actual one) from the memory to screen * + ***************************************************************************/ +void lineout(unsigned long line) +{ + int z; + + miniinit(); + + ptv = tonumsysx((*pdatas).line*16,16); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,line,1,soffs); + for (z=0;z<(*pdatas).len;z++) + { + ptv = tonumsysx((*pdatas).vals[z],16); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,line,1,shex); + for (z=0;z<(*pdatas).len;z++) + { +// TODO: There are many other characters that are displayable. + xascii[z] = (isgraph((*pdatas).vals[z]))?((*pdatas).vals[z]):'.'; + } + mvwaddstr(wascii,line,1,sascii); +} + +/*************************************************************************** + * FUNCTION: void initout() * + * GOOD FOR: First output after loading the file // home-Key * + ***************************************************************************/ +void initout() +{ +//Initial output (All) +int i,z; +bool toggleswitch=false; +pdatas=pfirst; + +if ((*pdatas).pnext == NULL) toggleswitch=true; +for(i=0;i<16;i++) + { + lineout(i+1); + if (toggleswitch) break; + pdatas=(*pdatas).pnext; + if ((*pdatas).pnext == NULL) toggleswitch=true; + } + scrrefresh(); + +if(i>15) pdatas=(*pdatas).plast; +} + +/*************************************************************************** + * FUNCTION: int scrolld() * + * GOOD FOR: Scrolling the scrren one line down * + * RESULTS : 1: no more elements // 0: everything was nice * + ***************************************************************************/ +int scrolld() +{ + int resval; + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + wscrl(woffs,1); + wscrl(whex,1); + wscrl(wascii,1); + + miniinit(); + + lineout(16); + resval=1; + } + else + { + resval=0; + } + return(resval); +} + +/*************************************************************************** + * FUNCTION: int scrollu() * + * GOOD FOR: Scrolling the scrren one line up * + * RESULTS : 1: at first element // 0: everything was nice * + ***************************************************************************/ +int scrollu() +{ + int z=0; + int result; + while(((*pdatas).plast != NULL)&&(z < 16)) + { + pdatas=(*pdatas).plast; + z++; + } + result= (z!=15)?1:0; + if(z>15) + { + z--; + wscrl(woffs,-1); + wscrl(whex,-1); + wscrl(wascii,-1); + + miniinit(); + + lineout(1); + } + while(z--) + { + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + } + } + return(result); +} + +/*************************************************************************** + * FUNCTION: void switchmode() * + * GOOD FOR: recalculation of cursor-position ascii<->hex * + ***************************************************************************/ +void switchmode() +{ + if(mode==0) + { + mode=1; // mode-toggle:Hex->Ascii + // recalculate cursor-position + pos.x=((pos.x+1)/3); + } + else + { + mode=0; // mode-toggle:ascii->Hex + // recalculate cursor-position + pos.x=((pos.x*3)-1); + } +} + +/*************************************************************************** + * FUNCTION: void moveleft() * + * GOOD FOR: move the cursor one position to the left * + ***************************************************************************/ +void moveleft() +{ + if(mode==0) + { + if(pos.x > 2) + { + (pos.x%3)?pos.x-=2:pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=48; + } + else + { + if(scrollu())pos.x=48; + } + } + } + else + { + if(pos.x > 1) + { + pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=16; + } + else + { + if(scrollu())pos.x=16; + } + } + } +} + +/*************************************************************************** + * FUNCTION: void moveright() * + * GOOD FOR: move the cursor one position to the right * + ***************************************************************************/ +void moveright() +{ + if(mode==0) + { + if(pos.x < 48) + { + (pos.x%3)?pos.x++:pos.x+=2; + } + else + { + if(pos.y < 16) + { + pos.x=2; + pos.y++; + } + else + { + if(scrolld()) pos.x=2; + } + } + } + else + { + if((pos.x < 16)) + { + pos.x++; + } + else + { + if(pos.y < 16) + { + pos.x=1; + pos.y++; + } + else + { + if(scrolld()) pos.x=1; + } + } + } +} + +/*************************************************************************** + * FUNCTION: unsigned long getactpos() * + * GOOD FOR: get the acutal cursor-position as a file-offset * + * RESULTS : The file-offset on the cursor-position as unsigned long * + ***************************************************************************/ +unsigned long getactpos() +{ + int i=0; + unsigned long actpos; + int distance; + int posxsave=9999; + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the position + actpos=((*pdatas).line*16)+pos.x-1; + // and move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + if (posxsave!=9999) + { + switchmode(); + pos.x=posxsave; + } + + return(actpos); +} + +/*************************************************************************** + * FUNCTION: void charin(char ch, int realmode, int realxpos) * + * GOOD FOR: insert/override one character in the memory * + * INPUTS : - char ch -the char to insert * + * - int realmode -the real mode (hex/ascii) * + * - int realxpos -the real x-position of the cursor * + ***************************************************************************/ +void charin(char ch, int realmode, int realxpos) +{ + int i=0; + int z=0; + int distance=0; + bool toggleswitch; + data *phelp2; + char rbmode; //rollbackmode (if file < 255 chars = 1 else 2) + unsigned long zhelp; + // move to the cursor-position in memory + // Is the file smaller then one page, it has to be handled in another way. + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + rbmode=1; + } + else + { + distance=16-pos.y; + rbmode=2; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // For the insert-mode + if (((insovr=='I') && (realmode==1))||((insovr=='I') && (realmode==0) && (realxpos%3==2)) && (realmode!=3)) + { + phelp=pdatas; + pdatas=pmax; + if ((*pdatas).len < 16) + { + (*pdatas).len++; + } + else + { + // allocate new element + phelp2=pmax; + zhelp=(*pmax).line+1; + pdatas=pmax=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); + if(!pmax) {abort();} + (*pmax).plast=phelp2; + (*pmax).pnext=NULL; + (*pmax).len=1; + (*pmax).line=zhelp; + if (rbmode == 1) phelp=pmax; + } + while(pdatas!=phelp) + { + for(z=(*pdatas).len-1;z>=0;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + (*pdatas).vals[0]=(*(*pdatas).plast).vals[15]; + pdatas=(*pdatas).plast; + } + + for(z=(*pdatas).len-1;z+1>pos.x;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + pdatas=phelp; + } + // Write value + (*pdatas).vals[pos.x-1]=ch; + // refresh output of current line (not needed, I think) + lineout(pos.y); + // moving backwards again and print the lines immediate. + if (rbmode==1) + { + initout(); + } + else + { + while(i--) + { + if((*pdatas).pnext) + { + pdatas=(*pdatas).pnext; + lineout(16-i); + } + } + } +} + +/*************************************************************************** + * FUNCTION: int charout() * + * GOOD FOR: get the char at the actual cursor-position * + * RESULTS : the char as an integer (char should work too normaly) * + ***************************************************************************/ +int charout() +{ + int i=0; + int distance; + int posxsave; + int ch; + // Move in memory to cursor-position + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the value + ch=((*pdatas).vals[pos.x-1]); + // move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + return(ch); +} + +/*************************************************************************** + * FUNCTION: void bottomout() * + * GOOD FOR: refreshing the output of the bottom-window * + ***************************************************************************/ +void bottomout() +{ + int realmode,realposx; + realmode=mode; + + if (realmode == 0) + { + realposx=pos.x; + switchmode(); + } + mvwaddstr(wbot,1,1,empty78); + mvwaddstr(wbot,2,1,empty78); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,1,"CHR:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddch(wbot,1,6,32); + mvwaddch(wbot,1,5,(isgraph(charout()))?charout():'.'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,9,"HEX:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,13,(tonumsysx(charout(),16))+6); + mvwaddch(wbot,1,15,'h'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,18,"DEC:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,22,(tonumsysx(charout(),10))+5); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,27,"OCT:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,31,(tonumsysx(charout(),8))+4); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,37,"BIN:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,41,tonumsysx(charout(),2)); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,52,"Offset:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,59,tonumsysx(getactpos(),16)); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,67,"h -"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,71,tonumsysx(getactpos(),10)); + + mvwaddstr(wbot,2,1,filename); + mvwaddstr(wbot,2,76,(insovr=='O')?"OVR":"INS"); + if (realmode == 0) + { + switchmode(); + pos.x=realposx; + } +} + +/*************************************************************************** + * FUNCTION: void swapbits() * + * GOOD FOR: toggle the bit [par1] an the actual position * + * INPUTS : - char bit the number (1-8) of the bit to toggle * + ***************************************************************************/ +void swapbits(char bit) +{ + char bin[]="12345678",*pbin; // for the Bits + pbin=bin; + int realposx, realmode; + realmode=mode; + if (realmode==0) + { + realposx=pos.x; + switchmode(); + } + pbin=tonumsysx(charout(),2); + pbin[bit]= (pbin[bit]=='0')?'1':'0'; + charin(xto10(pbin,2),3,pos.x); + if (realmode==0) + { + switchmode(); + pos.x=realposx; + } +} + +/*************************************************************************** + * FUNCTION: void delactchar(char ch, int realmode, int realxpos) * + * GOOD FOR: delete the char under the actual cursor-position * + * INPUTS : - int realmode -the real mode (hex/ascii) * + * - int realxpos -the real x-position of the cursor * + ***************************************************************************/ +void delactchar(int realmode, int realxpos) +{ + //BUG: after deletion the last char in the last line it gives me a segfault. + int i=0; + int z=0; + int distance=0; + bool endflag=FALSE; + bool toggleswitch; + char rbmode; //rollbackmode (if file < 255 chars = 1 else 2) + + // move to the cursor-position in memory + // Is the file smaller then one page, it has to be handled in another way. + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + rbmode=1; + } + else + { + distance=16-pos.y; + rbmode=2; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // For the insert-mode + if (((insovr=='I') && (realmode==1))||((insovr=='I') && (realmode==0) && (realxpos%3==2)) && (realmode!=3)) + { + phelp=pdatas; + for(z=realxpos-1;z<15;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + if((*pdatas).pnext) + { + (*pdatas).vals[15]=(*(*pdatas).pnext).vals[0]; + pdatas=(*pdatas).pnext; + } + while(pdatas!=pmax) + { + for(z=0;z<15;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + (*pdatas).vals[15]=(*(*pdatas).pnext).vals[0]; + pdatas=(*pdatas).pnext; + } +// check here for last char in last element + (*pdatas).len--; + if((*pdatas).len <= 0) + { + if(phelp==pmax) + { + if((*phelp).plast) + { + phelp=(*phelp).plast; + } + } + if(pfirst!=pmax) // is when pfirst!=pmax + { + pdatas=(*pdatas).plast; + free((*pdatas).pnext); + (*pdatas).pnext=NULL; + pmax=pdatas; + // TODO: there is one line less now, when we are at the last screen + // we should decrease i by one (only cosmetic) + endflag=TRUE; + } + else + { + (*pdatas).len=0; + } + } + else + { + if (phelp!=pmax) for(z=0;z<(*pdatas).len;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + } + pdatas=phelp; + } + // refresh output of current line (not needed, I think) + lineout(pos.y); + // moving backwards again and print the lines immediate. + if (rbmode==1) + { + // first delete the screen + for(z=0;z<16;z++) + { + emplineout(z+1); + } + // then do an initial output + initout(); + } + else + { + while(i--) + { + if((*pdatas).pnext) + { + pdatas=(*pdatas).pnext; + lineout(16-i); + } + } + } +// The rest is only a dirty workaround for the las (no longer needed) line +// because it was not possible for me to determine out, whether i am really +// at the end. + if (endflag) + { + for(i=0;i<16;i++) + { + if(!scrollu()) endflag=FALSE; + } + if (endflag) + { + if((pos.y < 16) && ((*pmax).line > 15)) pos.y++; + for(i=0;i<16;i++) + { + scrolld(); + } + } + } +} + +int main(int argc, char *argv[]) +{ +//Variables + unsigned int i; // Public counter + int inpch; // The char of input + +//Preperation + printf("T.I.A.H - Tiah Is A Hex-editor. Version 0.5a (C) Joerg Tretter 1999\n"); + //Is there a param? + if(argc < 2) + { + fprintf(stderr,"T.I.A.H -MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //open the file + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"T.I.A.H -MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // set the pointers + initvar(); + // init of other useful varibles + pos.x = pos.y = 1; + filename=argv[1]; + //file -> ram + if(loadcontents()) + { + fprintf(stderr,"T.I.A.H -MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // init of terminal + if(screeninit()) + { + endscr(); + fprintf(stderr,"T.I.A.H -MESSAGE; !!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + + +//Main-procedure +initout(); +bottomout(); +scrrefresh(); + +//Event-loop + while(inpch!=KEY_F(10)) + { + if (mode==0) + { + inpch=wgetch(whex); + } + else + { + inpch=wgetch(wascii); + } + switch(inpch) + { + // The Keys F1-F8 (bit-setting) + case KEY_F(9): switchmode(); + break; + + case KEY_F(1): swapbits(0); + break; + + case KEY_F(2): swapbits(1); + break; + + case KEY_F(3): swapbits(2); + break; + + case KEY_F(4): swapbits(3); + break; + + case KEY_F(5): swapbits(4); + break; + + case KEY_F(6): swapbits(5); + break; + + case KEY_F(7): swapbits(6); + break; + + case KEY_F(8): swapbits(7); + break; + + //the delete character-key + case KEY_DC : if (mode == 1) + { + delactchar(1,pos.x); + } + else + { + switchmode(); + delactchar(1,pos.x); + switchmode(); + } + break; + + //the insert-key, switch insert mode + case KEY_EIC: //fallthru + case KEY_IC : if (insovr=='O') + { + insovr='I'; + } + else + { + insovr='O'; + } + break; + + //Cursor-movement + case KEY_LEFT: moveleft(); + break; + + case KEY_RIGHT:moveright(); + break; + + case KEY_DOWN: if(pos.y>15) + { + scrolld(); + } + else + { + pos.y++; + } + break; + + case KEY_UP: if(pos.y<2) + { + scrollu(); + } + else + { + pos.y--; + } + break; + + case KEY_NPAGE:for(i=0;i<16;i++) + { + scrolld(); + } + break; + + case KEY_PPAGE:for(i=0;i<16;i++) + { + scrollu(); + } + break; + + case KEY_HOME: initout(); + break; + + case KEY_END: if((*pmax).line > 15) + { + pdatas=pmax; + for(i=0;((*pdatas).plast) && (i < 16);i++) + { + pdatas=(*pdatas).plast; + } + while((*pdatas).pnext) + { + scrolld(); + } + } + break; + // input characters + default: if(mode==0) + { + if(isdigit(inpch) || (toupper(inpch)>='A' && toupper(inpch)<='F') ) + { + waddch(whex,toupper(inpch)); + char hch[]="00"; + // fill hch correct (old values) + int xpos=pos.x; // save position + switchmode(); // recalc. coordinates + ptv=tonumsysx(charout(),16); // actual char in Hex. + //* --- DEBUG ONLY + mvwaddstr(wbot,2,30,tonumsysx(charout(),16)); + // --- END DEBUG + switchmode(); // recalc coordinates + pos.x=xpos; // restore coorinates + if (insovr=='O') // Predef. only in Override! + { + hch[0] = *(ptv+6); + hch[1] = *(ptv+7); + } + else // In Insert only first char + { + if(pos.x%3==0) + hch[0] = *(ptv+6); + } + //* --- DEBUG ONLY + mvwaddstr(wbot,2,1,hch); + mvwaddstr(wbot,2,20,ptv); + // --- END DEBUG + hch[(pos.x%3)?0:1]=inpch; + switchmode(); // recalc coordinates + charin(xto10(hch,16),0,xpos); // calc Hex -> Ascii+charin + switchmode(); // and back + pos.x=xpos; // restore positions + moveright(); + } + } + + else + { + if((inpch < 256)) + { + waddch(wascii,inpch); + charin(inpch,1,pos.x); + moveright(); + } + } + }; + + bottomout(); + scrrefresh(); + + } + + WINDOW* s=derwin(whex,3,17,7,17); + wattrset(s,COLOR_PAIR(1)); + box(s,0,0); + wattrset(s,COLOR_PAIR(3)); + mvwaddstr(s,1,1,"SAVE FILE (Y/N)"); + wrefresh(s); + int saveit= wgetch(s); + endscr(); + unallocmem(saveit,argv[1]); + + return (fclose(fp)); +} diff --git a/tiah05a/tiah05a.tar.bz2 b/tiah05a/tiah05a.tar.bz2 new file mode 100644 index 0000000..f3796b1 Binary files /dev/null and b/tiah05a/tiah05a.tar.bz2 differ diff --git a/tiah05b/CHANGES b/tiah05b/CHANGES new file mode 100644 index 0000000..baef432 --- /dev/null +++ b/tiah05b/CHANGES @@ -0,0 +1,10 @@ +0.5b: +flush stdout after printng hashmark while loading +Handle errors during create process of the Windows properly + +0.5a: +bugfix, use of proper function strncpy instead of "=" for filling string constants, there was a compile-problem with newer compilers. + +0.5: +first official release + diff --git a/tiah05b/COPYING b/tiah05b/COPYING new file mode 100644 index 0000000..a43ea21 --- /dev/null +++ b/tiah05b/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 675 Mass Ave, Cambridge, MA 02139, USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + Appendix: How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) 19yy + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19yy name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/tiah05b/Makefile b/tiah05b/Makefile new file mode 100644 index 0000000..a914fc4 --- /dev/null +++ b/tiah05b/Makefile @@ -0,0 +1,40 @@ +############################################################################ +# # +# T.I.A.H - TIAH IS A HEX-EDITOR # +# Copyright (C) 1999 by Joerg "Maddog" Tretter # +# # +############################################################################ + +PRGFILE = tiah +SRC = tiah.cc +MANFILE = tiah.1.gz + +INSTPATH = /usr/local/bin +MANPATH = /usr/man/man1 +DOCPATH = /usr/doc/tiah + +MAKE = make +CC = c++ +LIBS += -lncurses + +all: + $(MAKE) $(PRGFILE) + + +.cc: + @echo compiling $(SRC) . + @$(CC) $(LIBS) $(SRC) -o$(PRGFILE) + +install: + @install -s -g users -m 755 -o root $(PRGFILE) $(INSTPATH) + @install -d -g root -o root $(MANPATH) + @install -g root -m 644 -o root $(MANFILE) $(MANPATH) + @install -d -g root -o root $(DOCPATH) + @install -g root -m 644 -o root COPYING $(DOCPATH) + @install -g root -m 644 -o root README.PROGRAMMER $(DOCPATH) + @install -g root -m 644 -o root README.INSTALL $(DOCPATH) + @install -g root -m 644 -o root README.BUG $(DOCPATH) + +clean: + @echo removing object $(PRGFILE). + @rm $(PRGFILE) diff --git a/tiah05b/README.BUG b/tiah05b/README.BUG new file mode 100644 index 0000000..9213e9c --- /dev/null +++ b/tiah05b/README.BUG @@ -0,0 +1 @@ +For known bugs see manpage. diff --git a/tiah05b/README.INSTALL b/tiah05b/README.INSTALL new file mode 100644 index 0000000..84f25af --- /dev/null +++ b/tiah05b/README.INSTALL @@ -0,0 +1,24 @@ +TIAH (C) Joerg Tretter + +What do you need: +================= +- linux on a i386+ (other systems perhaps work, too) +- g++ or compatible +- ncurses library (normally installed on every linux-system) + +Steps of installation: +====================== +- edit Makefile if you want an alternative install-dir + (standard is /usr/local/bin) +- make && make install + (you will get some warnings, but that doesn't matter, + if you get an error and can't solve yourself: please report via mail) + +Please send me bug reports and other systems TIAH works on, too. + +If you like the program, please let me know (it's interesting for me to know +how may people are using TIAH.) + +Joerg Tretter +J.Tretter@gmx.de + diff --git a/tiah05b/README.PROGRAMMER b/tiah05b/README.PROGRAMMER new file mode 100644 index 0000000..758668d --- /dev/null +++ b/tiah05b/README.PROGRAMMER @@ -0,0 +1,20 @@ +TIAH (C) Joerg Tretter + +I know, the code absolutely cofusing, but keep in mind: + +This version is only a first prototype of TIAH. + +please send me every bugfix / feature you programmed. + +Plans for next releases +1. search-fuction +2. replace-function +3. fill-function +4. internal calculator (source numbering-system <-> target-numbering system) +5. perhaps various goodies :-)) +6. Changes suggested by others. + +Because I have less time currently I would appreciate all patches. + +Joerg Tretter +J.Tretter@gmx.de diff --git a/tiah05b/THANKSTO b/tiah05b/THANKSTO new file mode 100644 index 0000000..0f95950 --- /dev/null +++ b/tiah05b/THANKSTO @@ -0,0 +1 @@ +Fabrice Haberer (for 64 Bit testing on Alpha) diff --git a/tiah05b/tiah.1.gz b/tiah05b/tiah.1.gz new file mode 100644 index 0000000..468aaaf Binary files /dev/null and b/tiah05b/tiah.1.gz differ diff --git a/tiah05b/tiah.cc b/tiah05b/tiah.cc new file mode 100644 index 0000000..948f9ac --- /dev/null +++ b/tiah05b/tiah.cc @@ -0,0 +1,1251 @@ +/* + * TIAH -- an advanced HEX-Editor + * Copyright (C) 1999 by Joerg Tretter + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include +#include +#include + +struct data // one line in Memory +{ +struct data* plast; +struct data* pnext; +unsigned char vals[16]; +unsigned long line; +char len; +}; + +struct cpos // Cursor-Position +{ + char x,y; +}pos; + +char insovr='O'; // Isert/Override-Mode +int mode=1; +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII "; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +char empty78[]=" "; +int result; +char *filename; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data datas, *pdatas, *pfirst, *phelp, *pmax; + +/*************************************************************************** + * FUNCTION: void initvar() * + * GOOD FOR: Initialization of basic-variables * + ***************************************************************************/ +void initvar() +{ + pfirst=&datas; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + (*pfirst).line=0; + (*pfirst).len=0; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +/*************************************************************************** + * FUNCTION: void miniinit() * + * GOOD FOR: Initialization of frequent used variables * + ***************************************************************************/ +void miniinit() +{ +strncpy(xoffs, " ",9); +strncpy(xhex, " ",51); +strncpy(xascii, " ",17); +} + +/*************************************************************************** + * FUNCTION: int screeninit() * + * GOOD FOR: Initialization of the n-curses-screen * + * RESULTS : 1:There was an error / 0:Everything ok. * + ***************************************************************************/ +int screeninit() +{ + int retval =0; + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Create Curses-space for the windows + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + if (wbot == NULL || woffs == NULL || whex == NULL || wascii == NULL || wtop == NULL) { + endwin(); + fprintf(stderr,"ERROR: T.I.A.H Was not able to create all Program windows. Possibly your terminal dimensions are too small. You need a Screen-Size of at least 80x25 characters!"); + exit(5); + } + +// enable scrolling + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// enable Keyboard-control + keypad(stdscr,1); + keypad(whex,1); + keypad(wascii,1); + +// disable echoing of input + noecho(); + +// swich cursor to big size +// TODO: check why this not works in most cases +// if(curs_set(2)==ERR) retval=1 ; + +// creation of curses "Color-Pairs" + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + if(init_pair(3,COLOR_MAGENTA,COLOR_BLACK)) retval=1; + +// join the windows with the color-pairs + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + +// paint the boxes around the windows (in fact it's inside) + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + // change the color-pairs of the windows again. + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +/*************************************************************************** + * FUNCTION: char* tonumsysx() * + * GOOD FOR: Shift a number to another numbering system * + * INPUTS : - int base10 -The number in decimal system * + * - int newbase -The new system (16->Hex,2->Bin,...) * + * RESULTS : The number in the new system as a pointer of char * + ***************************************************************************/ +char* tonumsysx(int base10,int newbase) +{ + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + strncpy(swap,"********",9); + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +/*************************************************************************** + * FUNCTION: unsigned long xto10(char *c, int oldbase) * + * GOOD FOR: Shift a number from a foreign numbering system to decimal * + * INPUTS : - char* c -The number in foreign system * + * - int oldbase -The old system (16->Hex,2->Bin,...) * + * RESULTS : The number in decimal system as a unsigned long * + ***************************************************************************/ +unsigned long xto10(char *c, int oldbase) +{ +unsigned long base10=0; +int pos=0; +int i; + for (i=0;c[i];i++, c[i]=toupper(c[i])); + while(c[pos++]); + pos--; + int len=pos-1; + while(pos--) + { + if(isdigit(c[len-pos])) + { + base10 = base10 + (unsigned long)( (double)(c[len-pos]-'0') * ( pow( (double)oldbase,(double)pos ) ) ) ; + } + else + { + base10 = base10 + (unsigned long)( ((double)(c[len-pos]-'A')+10) * ( pow( (double)oldbase,(double)pos ) ) ); + } + } + return(base10); +} + +/*************************************************************************** + * FUNCTION: int loadcontents() * + * GOOD FOR: Loading the contents of a file into the memory * + * RESULTS : The status of the loading (not used yet) * + ***************************************************************************/ +int loadcontents() +{ + int retval=0; + int x; + char ch; + unsigned long line=0; + + + pdatas=pfirst; + ch=fgetc(fp); + // Zero-byte-file. + if(feof(fp)) + { + fprintf(stderr,"T.I.A.H - Tiah Is A Hex-editor. - opening 0-Byte file"); + pfirst=pmax=pdatas=(data*)(malloc(sizeof(datas))); + (*pdatas).pnext=NULL; + (*pdatas).plast=NULL; + (*pdatas).line=0; + (*pdatas).len=0; + + } + printf("Printing hashmark for every 8 Kbytes.\n"); + while (!(feof(fp))) + { + for(x=0;x<16;x++) + { + ((*pdatas).vals[x])=ch; + if (feof(fp)) //(*pdatas).vals[x]==EOF) + { + (*pdatas).vals[x]=0; + break; + } + (*pdatas).len=x+1; + ch=fgetc(fp); + } + (*pdatas).line=line++; + if(!(feof(fp))) + { + phelp=pdatas; + if (!(line%500)) {fprintf(stdout,"%c",'#'); fflush(stdout);} + pdatas=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); +// BUG: Check for Memory-Full does not work. + if(!pdatas) {abort();} + (*pdatas).plast=phelp; + (*pdatas).pnext=NULL; + } + pmax=pdatas; + + } + return (retval); +} + +/*************************************************************************** + * FUNCTION: void endscr() * + * GOOD FOR: End the scrren session on curses-level * + ***************************************************************************/ +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +/*************************************************************************** + * FUNCTION: void unallocmem(int save, char* filename) * + * GOOD FOR: Free the memory from the bits & bytes * + * INPUTS : int save: flag (save Y/else key) * + * : cahr* filename: the name of the save-file * + ***************************************************************************/ +void unallocmem(int save, char *filename) +{ + int pos; + save=toupper(save); + if (save=='Y') + { + pdatas=pfirst; + fp=fopen(filename,"w"); + while((*pdatas).pnext) + { + for (pos=0;pos <(*pdatas).len;pos++) + { + fputc((*pdatas).vals[pos],fp); + } + pdatas=(*pdatas).pnext; + } + for (pos=0;pos <(*pdatas).len;pos++) fputc((*pdatas).vals[pos],fp); + fclose(fp); + } + { + pdatas=pmax; + } + while((*pdatas).plast) + { + phelp=(*pdatas).plast; + free(pdatas); + pdatas=phelp; + } +} + +/*************************************************************************** + * FUNCTION: void scrrefresh() * + * GOOD FOR: Refreah the basic-screen-elements (not the contents) * + ***************************************************************************/ +void scrrefresh() +{ + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + // depending on the mode (ascii/hex) set the cursor-position + !mode?wmove(whex,pos.y,pos.x):wmove(wascii,pos.y,pos.x); + + wrefresh(woffs); + wrefresh(wtop); + wrefresh(wbot); + // the order is given by the mode (ascii/hex). the last window refreshed + // it the one, in which the cursor appears and the input-focus is. + !mode?wrefresh(wascii):wrefresh(whex); + mode?wrefresh(wascii):wrefresh(whex); +} + +/*************************************************************************** + * FUNCTION: void emplineout(unsigned long line) * + * GOOD FOR: Writing one empty line to the scrren * + ***************************************************************************/ +void emplineout(unsigned long line) +{ + mvwaddstr(woffs,line,1," "); + mvwaddstr(whex,line,1," "); + mvwaddstr(wascii,line,1," "); +} + +/*************************************************************************** + * FUNCTION: void lineout(unsigned long line) * + * GOOD FOR: Writing one line (the actual one) from the memory to screen * + ***************************************************************************/ +void lineout(unsigned long line) +{ + int z; + + miniinit(); + + ptv = tonumsysx((*pdatas).line*16,16); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,line,1,soffs); + for (z=0;z<(*pdatas).len;z++) + { + ptv = tonumsysx((*pdatas).vals[z],16); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,line,1,shex); + for (z=0;z<(*pdatas).len;z++) + { +// TODO: There are many other characters that are displayable. + xascii[z] = (isgraph((*pdatas).vals[z]))?((*pdatas).vals[z]):'.'; + } + mvwaddstr(wascii,line,1,sascii); +} + +/*************************************************************************** + * FUNCTION: void initout() * + * GOOD FOR: First output after loading the file // home-Key * + ***************************************************************************/ +void initout() +{ +//Initial output (All) +int i,z; +bool toggleswitch=false; +pdatas=pfirst; + +if ((*pdatas).pnext == NULL) toggleswitch=true; +for(i=0;i<16;i++) + { + lineout(i+1); + if (toggleswitch) break; + pdatas=(*pdatas).pnext; + if ((*pdatas).pnext == NULL) toggleswitch=true; + } + scrrefresh(); + +if(i>15) pdatas=(*pdatas).plast; +} + +/*************************************************************************** + * FUNCTION: int scrolld() * + * GOOD FOR: Scrolling the scrren one line down * + * RESULTS : 1: no more elements // 0: everything was nice * + ***************************************************************************/ +int scrolld() +{ + int resval; + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + wscrl(woffs,1); + wscrl(whex,1); + wscrl(wascii,1); + + miniinit(); + + lineout(16); + resval=1; + } + else + { + resval=0; + } + return(resval); +} + +/*************************************************************************** + * FUNCTION: int scrollu() * + * GOOD FOR: Scrolling the scrren one line up * + * RESULTS : 1: at first element // 0: everything was nice * + ***************************************************************************/ +int scrollu() +{ + int z=0; + int result; + while(((*pdatas).plast != NULL)&&(z < 16)) + { + pdatas=(*pdatas).plast; + z++; + } + result= (z!=15)?1:0; + if(z>15) + { + z--; + wscrl(woffs,-1); + wscrl(whex,-1); + wscrl(wascii,-1); + + miniinit(); + + lineout(1); + } + while(z--) + { + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + } + } + return(result); +} + +/*************************************************************************** + * FUNCTION: void switchmode() * + * GOOD FOR: recalculation of cursor-position ascii<->hex * + ***************************************************************************/ +void switchmode() +{ + if(mode==0) + { + mode=1; // mode-toggle:Hex->Ascii + // recalculate cursor-position + pos.x=((pos.x+1)/3); + } + else + { + mode=0; // mode-toggle:ascii->Hex + // recalculate cursor-position + pos.x=((pos.x*3)-1); + } +} + +/*************************************************************************** + * FUNCTION: void moveleft() * + * GOOD FOR: move the cursor one position to the left * + ***************************************************************************/ +void moveleft() +{ + if(mode==0) + { + if(pos.x > 2) + { + (pos.x%3)?pos.x-=2:pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=48; + } + else + { + if(scrollu())pos.x=48; + } + } + } + else + { + if(pos.x > 1) + { + pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=16; + } + else + { + if(scrollu())pos.x=16; + } + } + } +} + +/*************************************************************************** + * FUNCTION: void moveright() * + * GOOD FOR: move the cursor one position to the right * + ***************************************************************************/ +void moveright() +{ + if(mode==0) + { + if(pos.x < 48) + { + (pos.x%3)?pos.x++:pos.x+=2; + } + else + { + if(pos.y < 16) + { + pos.x=2; + pos.y++; + } + else + { + if(scrolld()) pos.x=2; + } + } + } + else + { + if((pos.x < 16)) + { + pos.x++; + } + else + { + if(pos.y < 16) + { + pos.x=1; + pos.y++; + } + else + { + if(scrolld()) pos.x=1; + } + } + } +} + +/*************************************************************************** + * FUNCTION: unsigned long getactpos() * + * GOOD FOR: get the acutal cursor-position as a file-offset * + * RESULTS : The file-offset on the cursor-position as unsigned long * + ***************************************************************************/ +unsigned long getactpos() +{ + int i=0; + unsigned long actpos; + int distance; + int posxsave=9999; + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the position + actpos=((*pdatas).line*16)+pos.x-1; + // and move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + if (posxsave!=9999) + { + switchmode(); + pos.x=posxsave; + } + + return(actpos); +} + +/*************************************************************************** + * FUNCTION: void charin(char ch, int realmode, int realxpos) * + * GOOD FOR: insert/override one character in the memory * + * INPUTS : - char ch -the char to insert * + * - int realmode -the real mode (hex/ascii) * + * - int realxpos -the real x-position of the cursor * + ***************************************************************************/ +void charin(char ch, int realmode, int realxpos) +{ + int i=0; + int z=0; + int distance=0; + bool toggleswitch; + data *phelp2; + char rbmode; //rollbackmode (if file < 255 chars = 1 else 2) + unsigned long zhelp; + // move to the cursor-position in memory + // Is the file smaller then one page, it has to be handled in another way. + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + rbmode=1; + } + else + { + distance=16-pos.y; + rbmode=2; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // For the insert-mode + if (((insovr=='I') && (realmode==1))||((insovr=='I') && (realmode==0) && (realxpos%3==2)) && (realmode!=3)) + { + phelp=pdatas; + pdatas=pmax; + if ((*pdatas).len < 16) + { + (*pdatas).len++; + } + else + { + // allocate new element + phelp2=pmax; + zhelp=(*pmax).line+1; + pdatas=pmax=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); + if(!pmax) {abort();} + (*pmax).plast=phelp2; + (*pmax).pnext=NULL; + (*pmax).len=1; + (*pmax).line=zhelp; + if (rbmode == 1) phelp=pmax; + } + while(pdatas!=phelp) + { + for(z=(*pdatas).len-1;z>=0;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + (*pdatas).vals[0]=(*(*pdatas).plast).vals[15]; + pdatas=(*pdatas).plast; + } + + for(z=(*pdatas).len-1;z+1>pos.x;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + pdatas=phelp; + } + // Write value + (*pdatas).vals[pos.x-1]=ch; + // refresh output of current line (not needed, I think) + lineout(pos.y); + // moving backwards again and print the lines immediate. + if (rbmode==1) + { + initout(); + } + else + { + while(i--) + { + if((*pdatas).pnext) + { + pdatas=(*pdatas).pnext; + lineout(16-i); + } + } + } +} + +/*************************************************************************** + * FUNCTION: int charout() * + * GOOD FOR: get the char at the actual cursor-position * + * RESULTS : the char as an integer (char should work too normaly) * + ***************************************************************************/ +int charout() +{ + int i=0; + int distance; + int posxsave; + int ch; + // Move in memory to cursor-position + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the value + ch=((*pdatas).vals[pos.x-1]); + // move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + return(ch); +} + +/*************************************************************************** + * FUNCTION: void bottomout() * + * GOOD FOR: refreshing the output of the bottom-window * + ***************************************************************************/ +void bottomout() +{ + int realmode,realposx; + realmode=mode; + + if (realmode == 0) + { + realposx=pos.x; + switchmode(); + } + mvwaddstr(wbot,1,1,empty78); + mvwaddstr(wbot,2,1,empty78); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,1,"CHR:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddch(wbot,1,6,32); + mvwaddch(wbot,1,5,(isgraph(charout()))?charout():'.'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,9,"HEX:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,13,(tonumsysx(charout(),16))+6); + mvwaddch(wbot,1,15,'h'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,18,"DEC:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,22,(tonumsysx(charout(),10))+5); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,27,"OCT:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,31,(tonumsysx(charout(),8))+4); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,37,"BIN:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,41,tonumsysx(charout(),2)); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,52,"Offset:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,59,tonumsysx(getactpos(),16)); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,67,"h -"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,71,tonumsysx(getactpos(),10)); + + mvwaddstr(wbot,2,1,filename); + mvwaddstr(wbot,2,76,(insovr=='O')?"OVR":"INS"); + if (realmode == 0) + { + switchmode(); + pos.x=realposx; + } +} + +/*************************************************************************** + * FUNCTION: void swapbits() * + * GOOD FOR: toggle the bit [par1] an the actual position * + * INPUTS : - char bit the number (1-8) of the bit to toggle * + ***************************************************************************/ +void swapbits(char bit) +{ + char bin[]="12345678",*pbin; // for the Bits + pbin=bin; + int realposx, realmode; + realmode=mode; + if (realmode==0) + { + realposx=pos.x; + switchmode(); + } + pbin=tonumsysx(charout(),2); + pbin[bit]= (pbin[bit]=='0')?'1':'0'; + charin(xto10(pbin,2),3,pos.x); + if (realmode==0) + { + switchmode(); + pos.x=realposx; + } +} + +/*************************************************************************** + * FUNCTION: void delactchar(char ch, int realmode, int realxpos) * + * GOOD FOR: delete the char under the actual cursor-position * + * INPUTS : - int realmode -the real mode (hex/ascii) * + * - int realxpos -the real x-position of the cursor * + ***************************************************************************/ +void delactchar(int realmode, int realxpos) +{ + //BUG: after deletion the last char in the last line it gives me a segfault. + int i=0; + int z=0; + int distance=0; + bool endflag=FALSE; + bool toggleswitch; + char rbmode; //rollbackmode (if file < 255 chars = 1 else 2) + + // move to the cursor-position in memory + // Is the file smaller then one page, it has to be handled in another way. + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + rbmode=1; + } + else + { + distance=16-pos.y; + rbmode=2; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // For the insert-mode + if (((insovr=='I') && (realmode==1))||((insovr=='I') && (realmode==0) && (realxpos%3==2)) && (realmode!=3)) + { + phelp=pdatas; + for(z=realxpos-1;z<15;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + if((*pdatas).pnext) + { + (*pdatas).vals[15]=(*(*pdatas).pnext).vals[0]; + pdatas=(*pdatas).pnext; + } + while(pdatas!=pmax) + { + for(z=0;z<15;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + (*pdatas).vals[15]=(*(*pdatas).pnext).vals[0]; + pdatas=(*pdatas).pnext; + } +// check here for last char in last element + (*pdatas).len--; + if((*pdatas).len <= 0) + { + if(phelp==pmax) + { + if((*phelp).plast) + { + phelp=(*phelp).plast; + } + } + if(pfirst!=pmax) // is when pfirst!=pmax + { + pdatas=(*pdatas).plast; + free((*pdatas).pnext); + (*pdatas).pnext=NULL; + pmax=pdatas; + // TODO: there is one line less now, when we are at the last screen + // we should decrease i by one (only cosmetic) + endflag=TRUE; + } + else + { + (*pdatas).len=0; + } + } + else + { + if (phelp!=pmax) for(z=0;z<(*pdatas).len;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + } + pdatas=phelp; + } + // refresh output of current line (not needed, I think) + lineout(pos.y); + // moving backwards again and print the lines immediate. + if (rbmode==1) + { + // first delete the screen + for(z=0;z<16;z++) + { + emplineout(z+1); + } + // then do an initial output + initout(); + } + else + { + while(i--) + { + if((*pdatas).pnext) + { + pdatas=(*pdatas).pnext; + lineout(16-i); + } + } + } +// The rest is only a dirty workaround for the las (no longer needed) line +// because it was not possible for me to determine out, whether i am really +// at the end. + if (endflag) + { + for(i=0;i<16;i++) + { + if(!scrollu()) endflag=FALSE; + } + if (endflag) + { + if((pos.y < 16) && ((*pmax).line > 15)) pos.y++; + for(i=0;i<16;i++) + { + scrolld(); + } + } + } +} + +int main(int argc, char *argv[]) +{ +//Variables + unsigned int i; // Public counter + int inpch; // The char of input + +//Preperation + printf("T.I.A.H - Tiah Is A Hex-editor. Version 0.5a (C) Joerg Tretter 1999\n"); + //Is there a param? + if(argc < 2) + { + fprintf(stderr,"T.I.A.H -MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //open the file + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"T.I.A.H -MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // set the pointers + initvar(); + // init of other useful varibles + pos.x = pos.y = 1; + filename=argv[1]; + //file -> ram + if(loadcontents()) + { + fprintf(stderr,"T.I.A.H -MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // init of terminal + if(screeninit() != 0) + { + endscr(); + fprintf(stderr,"T.I.A.H -MESSAGE; !!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + + +//Main-procedure +initout(); +bottomout(); +scrrefresh(); + +//Event-loop + while(inpch!=KEY_F(10)) + { + if (mode==0) + { + inpch=wgetch(whex); + } + else + { + inpch=wgetch(wascii); + } + switch(inpch) + { + // The Keys F1-F8 (bit-setting) + case KEY_F(9): switchmode(); + break; + + case KEY_F(1): swapbits(0); + break; + + case KEY_F(2): swapbits(1); + break; + + case KEY_F(3): swapbits(2); + break; + + case KEY_F(4): swapbits(3); + break; + + case KEY_F(5): swapbits(4); + break; + + case KEY_F(6): swapbits(5); + break; + + case KEY_F(7): swapbits(6); + break; + + case KEY_F(8): swapbits(7); + break; + + //the delete character-key + case KEY_DC : if (mode == 1) + { + delactchar(1,pos.x); + } + else + { + switchmode(); + delactchar(1,pos.x); + switchmode(); + } + break; + + //the insert-key, switch insert mode + case KEY_EIC: //fallthru + case KEY_IC : if (insovr=='O') + { + insovr='I'; + } + else + { + insovr='O'; + } + break; + + //Cursor-movement + case KEY_LEFT: moveleft(); + break; + + case KEY_RIGHT:moveright(); + break; + + case KEY_DOWN: if(pos.y>15) + { + scrolld(); + } + else + { + pos.y++; + } + break; + + case KEY_UP: if(pos.y<2) + { + scrollu(); + } + else + { + pos.y--; + } + break; + + case KEY_NPAGE:for(i=0;i<16;i++) + { + scrolld(); + } + break; + + case KEY_PPAGE:for(i=0;i<16;i++) + { + scrollu(); + } + break; + + case KEY_HOME: initout(); + break; + + case KEY_END: if((*pmax).line > 15) + { + pdatas=pmax; + for(i=0;((*pdatas).plast) && (i < 16);i++) + { + pdatas=(*pdatas).plast; + } + while((*pdatas).pnext) + { + scrolld(); + } + } + break; + // input characters + default: if(mode==0) + { + if(isdigit(inpch) || (toupper(inpch)>='A' && toupper(inpch)<='F') ) + { + waddch(whex,toupper(inpch)); + char hch[]="00"; + // fill hch correct (old values) + int xpos=pos.x; // save position + switchmode(); // recalc. coordinates + ptv=tonumsysx(charout(),16); // actual char in Hex. + //* --- DEBUG ONLY + mvwaddstr(wbot,2,30,tonumsysx(charout(),16)); + // --- END DEBUG + switchmode(); // recalc coordinates + pos.x=xpos; // restore coorinates + if (insovr=='O') // Predef. only in Override! + { + hch[0] = *(ptv+6); + hch[1] = *(ptv+7); + } + else // In Insert only first char + { + if(pos.x%3==0) + hch[0] = *(ptv+6); + } + //* --- DEBUG ONLY + mvwaddstr(wbot,2,1,hch); + mvwaddstr(wbot,2,20,ptv); + // --- END DEBUG + hch[(pos.x%3)?0:1]=inpch; + switchmode(); // recalc coordinates + charin(xto10(hch,16),0,xpos); // calc Hex -> Ascii+charin + switchmode(); // and back + pos.x=xpos; // restore positions + moveright(); + } + } + + else + { + if((inpch < 256)) + { + waddch(wascii,inpch); + charin(inpch,1,pos.x); + moveright(); + } + } + }; + + bottomout(); + scrrefresh(); + + } + + WINDOW* s=derwin(whex,3,17,7,17); + wattrset(s,COLOR_PAIR(1)); + box(s,0,0); + wattrset(s,COLOR_PAIR(3)); + mvwaddstr(s,1,1,"SAVE FILE (Y/N)"); + wrefresh(s); + int saveit= wgetch(s); + endscr(); + unallocmem(saveit,argv[1]); + + return (fclose(fp)); +} diff --git a/tiah05c/CHANGES b/tiah05c/CHANGES new file mode 100644 index 0000000..baef432 --- /dev/null +++ b/tiah05c/CHANGES @@ -0,0 +1,10 @@ +0.5b: +flush stdout after printng hashmark while loading +Handle errors during create process of the Windows properly + +0.5a: +bugfix, use of proper function strncpy instead of "=" for filling string constants, there was a compile-problem with newer compilers. + +0.5: +first official release + diff --git a/tiah05c/COPYING b/tiah05c/COPYING new file mode 100644 index 0000000..a43ea21 --- /dev/null +++ b/tiah05c/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 675 Mass Ave, Cambridge, MA 02139, USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + Appendix: How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) 19yy + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19yy name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/tiah05c/Makefile b/tiah05c/Makefile new file mode 100644 index 0000000..a914fc4 --- /dev/null +++ b/tiah05c/Makefile @@ -0,0 +1,40 @@ +############################################################################ +# # +# T.I.A.H - TIAH IS A HEX-EDITOR # +# Copyright (C) 1999 by Joerg "Maddog" Tretter # +# # +############################################################################ + +PRGFILE = tiah +SRC = tiah.cc +MANFILE = tiah.1.gz + +INSTPATH = /usr/local/bin +MANPATH = /usr/man/man1 +DOCPATH = /usr/doc/tiah + +MAKE = make +CC = c++ +LIBS += -lncurses + +all: + $(MAKE) $(PRGFILE) + + +.cc: + @echo compiling $(SRC) . + @$(CC) $(LIBS) $(SRC) -o$(PRGFILE) + +install: + @install -s -g users -m 755 -o root $(PRGFILE) $(INSTPATH) + @install -d -g root -o root $(MANPATH) + @install -g root -m 644 -o root $(MANFILE) $(MANPATH) + @install -d -g root -o root $(DOCPATH) + @install -g root -m 644 -o root COPYING $(DOCPATH) + @install -g root -m 644 -o root README.PROGRAMMER $(DOCPATH) + @install -g root -m 644 -o root README.INSTALL $(DOCPATH) + @install -g root -m 644 -o root README.BUG $(DOCPATH) + +clean: + @echo removing object $(PRGFILE). + @rm $(PRGFILE) diff --git a/tiah05c/README.BUG b/tiah05c/README.BUG new file mode 100644 index 0000000..9213e9c --- /dev/null +++ b/tiah05c/README.BUG @@ -0,0 +1 @@ +For known bugs see manpage. diff --git a/tiah05c/README.INSTALL b/tiah05c/README.INSTALL new file mode 100644 index 0000000..84f25af --- /dev/null +++ b/tiah05c/README.INSTALL @@ -0,0 +1,24 @@ +TIAH (C) Joerg Tretter + +What do you need: +================= +- linux on a i386+ (other systems perhaps work, too) +- g++ or compatible +- ncurses library (normally installed on every linux-system) + +Steps of installation: +====================== +- edit Makefile if you want an alternative install-dir + (standard is /usr/local/bin) +- make && make install + (you will get some warnings, but that doesn't matter, + if you get an error and can't solve yourself: please report via mail) + +Please send me bug reports and other systems TIAH works on, too. + +If you like the program, please let me know (it's interesting for me to know +how may people are using TIAH.) + +Joerg Tretter +J.Tretter@gmx.de + diff --git a/tiah05c/README.PROGRAMMER b/tiah05c/README.PROGRAMMER new file mode 100644 index 0000000..758668d --- /dev/null +++ b/tiah05c/README.PROGRAMMER @@ -0,0 +1,20 @@ +TIAH (C) Joerg Tretter + +I know, the code absolutely cofusing, but keep in mind: + +This version is only a first prototype of TIAH. + +please send me every bugfix / feature you programmed. + +Plans for next releases +1. search-fuction +2. replace-function +3. fill-function +4. internal calculator (source numbering-system <-> target-numbering system) +5. perhaps various goodies :-)) +6. Changes suggested by others. + +Because I have less time currently I would appreciate all patches. + +Joerg Tretter +J.Tretter@gmx.de diff --git a/tiah05c/THANKSTO b/tiah05c/THANKSTO new file mode 100644 index 0000000..0f95950 --- /dev/null +++ b/tiah05c/THANKSTO @@ -0,0 +1 @@ +Fabrice Haberer (for 64 Bit testing on Alpha) diff --git a/tiah05c/tiah.1.gz b/tiah05c/tiah.1.gz new file mode 100644 index 0000000..468aaaf Binary files /dev/null and b/tiah05c/tiah.1.gz differ diff --git a/tiah05c/tiah.cc b/tiah05c/tiah.cc new file mode 100644 index 0000000..948f9ac --- /dev/null +++ b/tiah05c/tiah.cc @@ -0,0 +1,1251 @@ +/* + * TIAH -- an advanced HEX-Editor + * Copyright (C) 1999 by Joerg Tretter + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include +#include +#include + +struct data // one line in Memory +{ +struct data* plast; +struct data* pnext; +unsigned char vals[16]; +unsigned long line; +char len; +}; + +struct cpos // Cursor-Position +{ + char x,y; +}pos; + +char insovr='O'; // Isert/Override-Mode +int mode=1; +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII "; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +char empty78[]=" "; +int result; +char *filename; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data datas, *pdatas, *pfirst, *phelp, *pmax; + +/*************************************************************************** + * FUNCTION: void initvar() * + * GOOD FOR: Initialization of basic-variables * + ***************************************************************************/ +void initvar() +{ + pfirst=&datas; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + (*pfirst).line=0; + (*pfirst).len=0; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +/*************************************************************************** + * FUNCTION: void miniinit() * + * GOOD FOR: Initialization of frequent used variables * + ***************************************************************************/ +void miniinit() +{ +strncpy(xoffs, " ",9); +strncpy(xhex, " ",51); +strncpy(xascii, " ",17); +} + +/*************************************************************************** + * FUNCTION: int screeninit() * + * GOOD FOR: Initialization of the n-curses-screen * + * RESULTS : 1:There was an error / 0:Everything ok. * + ***************************************************************************/ +int screeninit() +{ + int retval =0; + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Create Curses-space for the windows + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + if (wbot == NULL || woffs == NULL || whex == NULL || wascii == NULL || wtop == NULL) { + endwin(); + fprintf(stderr,"ERROR: T.I.A.H Was not able to create all Program windows. Possibly your terminal dimensions are too small. You need a Screen-Size of at least 80x25 characters!"); + exit(5); + } + +// enable scrolling + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// enable Keyboard-control + keypad(stdscr,1); + keypad(whex,1); + keypad(wascii,1); + +// disable echoing of input + noecho(); + +// swich cursor to big size +// TODO: check why this not works in most cases +// if(curs_set(2)==ERR) retval=1 ; + +// creation of curses "Color-Pairs" + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + if(init_pair(3,COLOR_MAGENTA,COLOR_BLACK)) retval=1; + +// join the windows with the color-pairs + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + +// paint the boxes around the windows (in fact it's inside) + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + // change the color-pairs of the windows again. + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +/*************************************************************************** + * FUNCTION: char* tonumsysx() * + * GOOD FOR: Shift a number to another numbering system * + * INPUTS : - int base10 -The number in decimal system * + * - int newbase -The new system (16->Hex,2->Bin,...) * + * RESULTS : The number in the new system as a pointer of char * + ***************************************************************************/ +char* tonumsysx(int base10,int newbase) +{ + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + strncpy(swap,"********",9); + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +/*************************************************************************** + * FUNCTION: unsigned long xto10(char *c, int oldbase) * + * GOOD FOR: Shift a number from a foreign numbering system to decimal * + * INPUTS : - char* c -The number in foreign system * + * - int oldbase -The old system (16->Hex,2->Bin,...) * + * RESULTS : The number in decimal system as a unsigned long * + ***************************************************************************/ +unsigned long xto10(char *c, int oldbase) +{ +unsigned long base10=0; +int pos=0; +int i; + for (i=0;c[i];i++, c[i]=toupper(c[i])); + while(c[pos++]); + pos--; + int len=pos-1; + while(pos--) + { + if(isdigit(c[len-pos])) + { + base10 = base10 + (unsigned long)( (double)(c[len-pos]-'0') * ( pow( (double)oldbase,(double)pos ) ) ) ; + } + else + { + base10 = base10 + (unsigned long)( ((double)(c[len-pos]-'A')+10) * ( pow( (double)oldbase,(double)pos ) ) ); + } + } + return(base10); +} + +/*************************************************************************** + * FUNCTION: int loadcontents() * + * GOOD FOR: Loading the contents of a file into the memory * + * RESULTS : The status of the loading (not used yet) * + ***************************************************************************/ +int loadcontents() +{ + int retval=0; + int x; + char ch; + unsigned long line=0; + + + pdatas=pfirst; + ch=fgetc(fp); + // Zero-byte-file. + if(feof(fp)) + { + fprintf(stderr,"T.I.A.H - Tiah Is A Hex-editor. - opening 0-Byte file"); + pfirst=pmax=pdatas=(data*)(malloc(sizeof(datas))); + (*pdatas).pnext=NULL; + (*pdatas).plast=NULL; + (*pdatas).line=0; + (*pdatas).len=0; + + } + printf("Printing hashmark for every 8 Kbytes.\n"); + while (!(feof(fp))) + { + for(x=0;x<16;x++) + { + ((*pdatas).vals[x])=ch; + if (feof(fp)) //(*pdatas).vals[x]==EOF) + { + (*pdatas).vals[x]=0; + break; + } + (*pdatas).len=x+1; + ch=fgetc(fp); + } + (*pdatas).line=line++; + if(!(feof(fp))) + { + phelp=pdatas; + if (!(line%500)) {fprintf(stdout,"%c",'#'); fflush(stdout);} + pdatas=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); +// BUG: Check for Memory-Full does not work. + if(!pdatas) {abort();} + (*pdatas).plast=phelp; + (*pdatas).pnext=NULL; + } + pmax=pdatas; + + } + return (retval); +} + +/*************************************************************************** + * FUNCTION: void endscr() * + * GOOD FOR: End the scrren session on curses-level * + ***************************************************************************/ +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +/*************************************************************************** + * FUNCTION: void unallocmem(int save, char* filename) * + * GOOD FOR: Free the memory from the bits & bytes * + * INPUTS : int save: flag (save Y/else key) * + * : cahr* filename: the name of the save-file * + ***************************************************************************/ +void unallocmem(int save, char *filename) +{ + int pos; + save=toupper(save); + if (save=='Y') + { + pdatas=pfirst; + fp=fopen(filename,"w"); + while((*pdatas).pnext) + { + for (pos=0;pos <(*pdatas).len;pos++) + { + fputc((*pdatas).vals[pos],fp); + } + pdatas=(*pdatas).pnext; + } + for (pos=0;pos <(*pdatas).len;pos++) fputc((*pdatas).vals[pos],fp); + fclose(fp); + } + { + pdatas=pmax; + } + while((*pdatas).plast) + { + phelp=(*pdatas).plast; + free(pdatas); + pdatas=phelp; + } +} + +/*************************************************************************** + * FUNCTION: void scrrefresh() * + * GOOD FOR: Refreah the basic-screen-elements (not the contents) * + ***************************************************************************/ +void scrrefresh() +{ + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + // depending on the mode (ascii/hex) set the cursor-position + !mode?wmove(whex,pos.y,pos.x):wmove(wascii,pos.y,pos.x); + + wrefresh(woffs); + wrefresh(wtop); + wrefresh(wbot); + // the order is given by the mode (ascii/hex). the last window refreshed + // it the one, in which the cursor appears and the input-focus is. + !mode?wrefresh(wascii):wrefresh(whex); + mode?wrefresh(wascii):wrefresh(whex); +} + +/*************************************************************************** + * FUNCTION: void emplineout(unsigned long line) * + * GOOD FOR: Writing one empty line to the scrren * + ***************************************************************************/ +void emplineout(unsigned long line) +{ + mvwaddstr(woffs,line,1," "); + mvwaddstr(whex,line,1," "); + mvwaddstr(wascii,line,1," "); +} + +/*************************************************************************** + * FUNCTION: void lineout(unsigned long line) * + * GOOD FOR: Writing one line (the actual one) from the memory to screen * + ***************************************************************************/ +void lineout(unsigned long line) +{ + int z; + + miniinit(); + + ptv = tonumsysx((*pdatas).line*16,16); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,line,1,soffs); + for (z=0;z<(*pdatas).len;z++) + { + ptv = tonumsysx((*pdatas).vals[z],16); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,line,1,shex); + for (z=0;z<(*pdatas).len;z++) + { +// TODO: There are many other characters that are displayable. + xascii[z] = (isgraph((*pdatas).vals[z]))?((*pdatas).vals[z]):'.'; + } + mvwaddstr(wascii,line,1,sascii); +} + +/*************************************************************************** + * FUNCTION: void initout() * + * GOOD FOR: First output after loading the file // home-Key * + ***************************************************************************/ +void initout() +{ +//Initial output (All) +int i,z; +bool toggleswitch=false; +pdatas=pfirst; + +if ((*pdatas).pnext == NULL) toggleswitch=true; +for(i=0;i<16;i++) + { + lineout(i+1); + if (toggleswitch) break; + pdatas=(*pdatas).pnext; + if ((*pdatas).pnext == NULL) toggleswitch=true; + } + scrrefresh(); + +if(i>15) pdatas=(*pdatas).plast; +} + +/*************************************************************************** + * FUNCTION: int scrolld() * + * GOOD FOR: Scrolling the scrren one line down * + * RESULTS : 1: no more elements // 0: everything was nice * + ***************************************************************************/ +int scrolld() +{ + int resval; + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + wscrl(woffs,1); + wscrl(whex,1); + wscrl(wascii,1); + + miniinit(); + + lineout(16); + resval=1; + } + else + { + resval=0; + } + return(resval); +} + +/*************************************************************************** + * FUNCTION: int scrollu() * + * GOOD FOR: Scrolling the scrren one line up * + * RESULTS : 1: at first element // 0: everything was nice * + ***************************************************************************/ +int scrollu() +{ + int z=0; + int result; + while(((*pdatas).plast != NULL)&&(z < 16)) + { + pdatas=(*pdatas).plast; + z++; + } + result= (z!=15)?1:0; + if(z>15) + { + z--; + wscrl(woffs,-1); + wscrl(whex,-1); + wscrl(wascii,-1); + + miniinit(); + + lineout(1); + } + while(z--) + { + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + } + } + return(result); +} + +/*************************************************************************** + * FUNCTION: void switchmode() * + * GOOD FOR: recalculation of cursor-position ascii<->hex * + ***************************************************************************/ +void switchmode() +{ + if(mode==0) + { + mode=1; // mode-toggle:Hex->Ascii + // recalculate cursor-position + pos.x=((pos.x+1)/3); + } + else + { + mode=0; // mode-toggle:ascii->Hex + // recalculate cursor-position + pos.x=((pos.x*3)-1); + } +} + +/*************************************************************************** + * FUNCTION: void moveleft() * + * GOOD FOR: move the cursor one position to the left * + ***************************************************************************/ +void moveleft() +{ + if(mode==0) + { + if(pos.x > 2) + { + (pos.x%3)?pos.x-=2:pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=48; + } + else + { + if(scrollu())pos.x=48; + } + } + } + else + { + if(pos.x > 1) + { + pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=16; + } + else + { + if(scrollu())pos.x=16; + } + } + } +} + +/*************************************************************************** + * FUNCTION: void moveright() * + * GOOD FOR: move the cursor one position to the right * + ***************************************************************************/ +void moveright() +{ + if(mode==0) + { + if(pos.x < 48) + { + (pos.x%3)?pos.x++:pos.x+=2; + } + else + { + if(pos.y < 16) + { + pos.x=2; + pos.y++; + } + else + { + if(scrolld()) pos.x=2; + } + } + } + else + { + if((pos.x < 16)) + { + pos.x++; + } + else + { + if(pos.y < 16) + { + pos.x=1; + pos.y++; + } + else + { + if(scrolld()) pos.x=1; + } + } + } +} + +/*************************************************************************** + * FUNCTION: unsigned long getactpos() * + * GOOD FOR: get the acutal cursor-position as a file-offset * + * RESULTS : The file-offset on the cursor-position as unsigned long * + ***************************************************************************/ +unsigned long getactpos() +{ + int i=0; + unsigned long actpos; + int distance; + int posxsave=9999; + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the position + actpos=((*pdatas).line*16)+pos.x-1; + // and move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + if (posxsave!=9999) + { + switchmode(); + pos.x=posxsave; + } + + return(actpos); +} + +/*************************************************************************** + * FUNCTION: void charin(char ch, int realmode, int realxpos) * + * GOOD FOR: insert/override one character in the memory * + * INPUTS : - char ch -the char to insert * + * - int realmode -the real mode (hex/ascii) * + * - int realxpos -the real x-position of the cursor * + ***************************************************************************/ +void charin(char ch, int realmode, int realxpos) +{ + int i=0; + int z=0; + int distance=0; + bool toggleswitch; + data *phelp2; + char rbmode; //rollbackmode (if file < 255 chars = 1 else 2) + unsigned long zhelp; + // move to the cursor-position in memory + // Is the file smaller then one page, it has to be handled in another way. + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + rbmode=1; + } + else + { + distance=16-pos.y; + rbmode=2; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // For the insert-mode + if (((insovr=='I') && (realmode==1))||((insovr=='I') && (realmode==0) && (realxpos%3==2)) && (realmode!=3)) + { + phelp=pdatas; + pdatas=pmax; + if ((*pdatas).len < 16) + { + (*pdatas).len++; + } + else + { + // allocate new element + phelp2=pmax; + zhelp=(*pmax).line+1; + pdatas=pmax=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); + if(!pmax) {abort();} + (*pmax).plast=phelp2; + (*pmax).pnext=NULL; + (*pmax).len=1; + (*pmax).line=zhelp; + if (rbmode == 1) phelp=pmax; + } + while(pdatas!=phelp) + { + for(z=(*pdatas).len-1;z>=0;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + (*pdatas).vals[0]=(*(*pdatas).plast).vals[15]; + pdatas=(*pdatas).plast; + } + + for(z=(*pdatas).len-1;z+1>pos.x;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + pdatas=phelp; + } + // Write value + (*pdatas).vals[pos.x-1]=ch; + // refresh output of current line (not needed, I think) + lineout(pos.y); + // moving backwards again and print the lines immediate. + if (rbmode==1) + { + initout(); + } + else + { + while(i--) + { + if((*pdatas).pnext) + { + pdatas=(*pdatas).pnext; + lineout(16-i); + } + } + } +} + +/*************************************************************************** + * FUNCTION: int charout() * + * GOOD FOR: get the char at the actual cursor-position * + * RESULTS : the char as an integer (char should work too normaly) * + ***************************************************************************/ +int charout() +{ + int i=0; + int distance; + int posxsave; + int ch; + // Move in memory to cursor-position + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the value + ch=((*pdatas).vals[pos.x-1]); + // move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + return(ch); +} + +/*************************************************************************** + * FUNCTION: void bottomout() * + * GOOD FOR: refreshing the output of the bottom-window * + ***************************************************************************/ +void bottomout() +{ + int realmode,realposx; + realmode=mode; + + if (realmode == 0) + { + realposx=pos.x; + switchmode(); + } + mvwaddstr(wbot,1,1,empty78); + mvwaddstr(wbot,2,1,empty78); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,1,"CHR:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddch(wbot,1,6,32); + mvwaddch(wbot,1,5,(isgraph(charout()))?charout():'.'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,9,"HEX:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,13,(tonumsysx(charout(),16))+6); + mvwaddch(wbot,1,15,'h'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,18,"DEC:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,22,(tonumsysx(charout(),10))+5); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,27,"OCT:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,31,(tonumsysx(charout(),8))+4); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,37,"BIN:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,41,tonumsysx(charout(),2)); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,52,"Offset:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,59,tonumsysx(getactpos(),16)); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,67,"h -"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,71,tonumsysx(getactpos(),10)); + + mvwaddstr(wbot,2,1,filename); + mvwaddstr(wbot,2,76,(insovr=='O')?"OVR":"INS"); + if (realmode == 0) + { + switchmode(); + pos.x=realposx; + } +} + +/*************************************************************************** + * FUNCTION: void swapbits() * + * GOOD FOR: toggle the bit [par1] an the actual position * + * INPUTS : - char bit the number (1-8) of the bit to toggle * + ***************************************************************************/ +void swapbits(char bit) +{ + char bin[]="12345678",*pbin; // for the Bits + pbin=bin; + int realposx, realmode; + realmode=mode; + if (realmode==0) + { + realposx=pos.x; + switchmode(); + } + pbin=tonumsysx(charout(),2); + pbin[bit]= (pbin[bit]=='0')?'1':'0'; + charin(xto10(pbin,2),3,pos.x); + if (realmode==0) + { + switchmode(); + pos.x=realposx; + } +} + +/*************************************************************************** + * FUNCTION: void delactchar(char ch, int realmode, int realxpos) * + * GOOD FOR: delete the char under the actual cursor-position * + * INPUTS : - int realmode -the real mode (hex/ascii) * + * - int realxpos -the real x-position of the cursor * + ***************************************************************************/ +void delactchar(int realmode, int realxpos) +{ + //BUG: after deletion the last char in the last line it gives me a segfault. + int i=0; + int z=0; + int distance=0; + bool endflag=FALSE; + bool toggleswitch; + char rbmode; //rollbackmode (if file < 255 chars = 1 else 2) + + // move to the cursor-position in memory + // Is the file smaller then one page, it has to be handled in another way. + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + rbmode=1; + } + else + { + distance=16-pos.y; + rbmode=2; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // For the insert-mode + if (((insovr=='I') && (realmode==1))||((insovr=='I') && (realmode==0) && (realxpos%3==2)) && (realmode!=3)) + { + phelp=pdatas; + for(z=realxpos-1;z<15;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + if((*pdatas).pnext) + { + (*pdatas).vals[15]=(*(*pdatas).pnext).vals[0]; + pdatas=(*pdatas).pnext; + } + while(pdatas!=pmax) + { + for(z=0;z<15;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + (*pdatas).vals[15]=(*(*pdatas).pnext).vals[0]; + pdatas=(*pdatas).pnext; + } +// check here for last char in last element + (*pdatas).len--; + if((*pdatas).len <= 0) + { + if(phelp==pmax) + { + if((*phelp).plast) + { + phelp=(*phelp).plast; + } + } + if(pfirst!=pmax) // is when pfirst!=pmax + { + pdatas=(*pdatas).plast; + free((*pdatas).pnext); + (*pdatas).pnext=NULL; + pmax=pdatas; + // TODO: there is one line less now, when we are at the last screen + // we should decrease i by one (only cosmetic) + endflag=TRUE; + } + else + { + (*pdatas).len=0; + } + } + else + { + if (phelp!=pmax) for(z=0;z<(*pdatas).len;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + } + pdatas=phelp; + } + // refresh output of current line (not needed, I think) + lineout(pos.y); + // moving backwards again and print the lines immediate. + if (rbmode==1) + { + // first delete the screen + for(z=0;z<16;z++) + { + emplineout(z+1); + } + // then do an initial output + initout(); + } + else + { + while(i--) + { + if((*pdatas).pnext) + { + pdatas=(*pdatas).pnext; + lineout(16-i); + } + } + } +// The rest is only a dirty workaround for the las (no longer needed) line +// because it was not possible for me to determine out, whether i am really +// at the end. + if (endflag) + { + for(i=0;i<16;i++) + { + if(!scrollu()) endflag=FALSE; + } + if (endflag) + { + if((pos.y < 16) && ((*pmax).line > 15)) pos.y++; + for(i=0;i<16;i++) + { + scrolld(); + } + } + } +} + +int main(int argc, char *argv[]) +{ +//Variables + unsigned int i; // Public counter + int inpch; // The char of input + +//Preperation + printf("T.I.A.H - Tiah Is A Hex-editor. Version 0.5a (C) Joerg Tretter 1999\n"); + //Is there a param? + if(argc < 2) + { + fprintf(stderr,"T.I.A.H -MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //open the file + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"T.I.A.H -MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // set the pointers + initvar(); + // init of other useful varibles + pos.x = pos.y = 1; + filename=argv[1]; + //file -> ram + if(loadcontents()) + { + fprintf(stderr,"T.I.A.H -MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // init of terminal + if(screeninit() != 0) + { + endscr(); + fprintf(stderr,"T.I.A.H -MESSAGE; !!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + + +//Main-procedure +initout(); +bottomout(); +scrrefresh(); + +//Event-loop + while(inpch!=KEY_F(10)) + { + if (mode==0) + { + inpch=wgetch(whex); + } + else + { + inpch=wgetch(wascii); + } + switch(inpch) + { + // The Keys F1-F8 (bit-setting) + case KEY_F(9): switchmode(); + break; + + case KEY_F(1): swapbits(0); + break; + + case KEY_F(2): swapbits(1); + break; + + case KEY_F(3): swapbits(2); + break; + + case KEY_F(4): swapbits(3); + break; + + case KEY_F(5): swapbits(4); + break; + + case KEY_F(6): swapbits(5); + break; + + case KEY_F(7): swapbits(6); + break; + + case KEY_F(8): swapbits(7); + break; + + //the delete character-key + case KEY_DC : if (mode == 1) + { + delactchar(1,pos.x); + } + else + { + switchmode(); + delactchar(1,pos.x); + switchmode(); + } + break; + + //the insert-key, switch insert mode + case KEY_EIC: //fallthru + case KEY_IC : if (insovr=='O') + { + insovr='I'; + } + else + { + insovr='O'; + } + break; + + //Cursor-movement + case KEY_LEFT: moveleft(); + break; + + case KEY_RIGHT:moveright(); + break; + + case KEY_DOWN: if(pos.y>15) + { + scrolld(); + } + else + { + pos.y++; + } + break; + + case KEY_UP: if(pos.y<2) + { + scrollu(); + } + else + { + pos.y--; + } + break; + + case KEY_NPAGE:for(i=0;i<16;i++) + { + scrolld(); + } + break; + + case KEY_PPAGE:for(i=0;i<16;i++) + { + scrollu(); + } + break; + + case KEY_HOME: initout(); + break; + + case KEY_END: if((*pmax).line > 15) + { + pdatas=pmax; + for(i=0;((*pdatas).plast) && (i < 16);i++) + { + pdatas=(*pdatas).plast; + } + while((*pdatas).pnext) + { + scrolld(); + } + } + break; + // input characters + default: if(mode==0) + { + if(isdigit(inpch) || (toupper(inpch)>='A' && toupper(inpch)<='F') ) + { + waddch(whex,toupper(inpch)); + char hch[]="00"; + // fill hch correct (old values) + int xpos=pos.x; // save position + switchmode(); // recalc. coordinates + ptv=tonumsysx(charout(),16); // actual char in Hex. + //* --- DEBUG ONLY + mvwaddstr(wbot,2,30,tonumsysx(charout(),16)); + // --- END DEBUG + switchmode(); // recalc coordinates + pos.x=xpos; // restore coorinates + if (insovr=='O') // Predef. only in Override! + { + hch[0] = *(ptv+6); + hch[1] = *(ptv+7); + } + else // In Insert only first char + { + if(pos.x%3==0) + hch[0] = *(ptv+6); + } + //* --- DEBUG ONLY + mvwaddstr(wbot,2,1,hch); + mvwaddstr(wbot,2,20,ptv); + // --- END DEBUG + hch[(pos.x%3)?0:1]=inpch; + switchmode(); // recalc coordinates + charin(xto10(hch,16),0,xpos); // calc Hex -> Ascii+charin + switchmode(); // and back + pos.x=xpos; // restore positions + moveright(); + } + } + + else + { + if((inpch < 256)) + { + waddch(wascii,inpch); + charin(inpch,1,pos.x); + moveright(); + } + } + }; + + bottomout(); + scrrefresh(); + + } + + WINDOW* s=derwin(whex,3,17,7,17); + wattrset(s,COLOR_PAIR(1)); + box(s,0,0); + wattrset(s,COLOR_PAIR(3)); + mvwaddstr(s,1,1,"SAVE FILE (Y/N)"); + wrefresh(s); + int saveit= wgetch(s); + endscr(); + unallocmem(saveit,argv[1]); + + return (fclose(fp)); +} diff --git a/tiah05c/tiah05b.tar b/tiah05c/tiah05b.tar new file mode 100644 index 0000000..cb7e9ba Binary files /dev/null and b/tiah05c/tiah05b.tar differ diff --git a/tiahv1.cc b/tiahv1.cc new file mode 100644 index 0000000..93c5873 --- /dev/null +++ b/tiahv1.cc @@ -0,0 +1,224 @@ +#include +#include + +struct data +{ +struct data* plast; +struct data* pnext; +char vals[16][16]; +}; + +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII"; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +int result; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data daten,*pfirst; + +void initvar() +{ + pfirst=&daten; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Zwei Fenster + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// Scrollen erlauben + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// Color-Pairs anlegen (Farbkombinationen) + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(2)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +char* tohex(int base10) +{ + int newbase=16; + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +int loadcontents() +{ + int retval=0; + int x,y; + for(x=0;x<16;x++) + for(y=0;y<16;y++) + { + daten.vals[y][x]=fgetc(fp); + if (daten.vals[y][x]==EOF) daten.vals[y][x]=0; + } + for(x=0;x<16;x++) + for(y=0;y<16;y++) + printf("%c",daten.vals[y][x]); + + return (retval); +} + +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +int main(int argc, char *argv[]) +{ +//Variablen + unsigned int i,z; + + printf("%s\n",tohex(0)); + printf("%s\n",tohex(10)); + printf("%s\n",tohex(20)); + printf("%s\n",tohex(30)); + printf("%s\n",tohex(40)); + printf("%s\n",tohex(50)); + +//Vorbereitung + //Parameter angegeben? + if(argc < 2) + { + fprintf(stderr,"THX-MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //Datei öffnen + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"THX-MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + //Datei -> Speicher + if(loadcontents()) + { + fprintf(stderr,"THX-MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // pointer setzen + initvar(); + // Terminal initialisieren + if(screeninit()) + { + endscr(); + fprintf(stderr,"!!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + mvwaddstr(wbot,1,1,argv[1]); + + +//Hauptprogramm + + for (i=1;i<17;i++) + { + ptv = tohex(i-1); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,i,1,soffs); + for (z=0;z<16;z++) + { + ptv = tohex(daten.vals[z][i-1]); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,i,1,shex); + for (z=0;z<16;z++) + { + xascii[z] = ((daten.vals[z][i-1]) > 33)?(daten.vals[z][i-1]):254; + } + mvwaddstr(wascii,i,1,sascii); + } + + wrefresh(woffs); + wrefresh(whex); + wrefresh(wascii); + wrefresh(wtop); + wrefresh(wbot); + + + sleep(1); + endscr(); + return (fclose(fp)); +} diff --git a/tiahv10.cc b/tiahv10.cc new file mode 100644 index 0000000..7cda18e --- /dev/null +++ b/tiahv10.cc @@ -0,0 +1,1226 @@ +/* + * GHEX -- an advanced HEX-Editor + * Copyright (C) 1999 by Joerg Tretter + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include +#include + +struct data // one line in Memory +{ +struct data* plast; +struct data* pnext; +unsigned char vals[16]; +unsigned long line; +char len; +}; + +struct cpos // Cursor-Position +{ + char x,y; +}pos; + +char insovr='O'; // Isert/Override-Mode +int mode=1; +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII "; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +char empty78[]=" "; +int result; +char *filename; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data datas, *pdatas, *pfirst, *phelp, *pmax; + +/*************************************************************************** + * FUNCTION: void initvar() * + * GOOD FOR: Initialization of basic-variables * + ***************************************************************************/ +void initvar() +{ + pfirst=&datas; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + (*pfirst).line=0; + (*pfirst).len=0; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +/*************************************************************************** + * FUNCTION: void miniinit() * + * GOOD FOR: Initialization of frequent used variables * + ***************************************************************************/ +void miniinit() +{ +xoffs = " "; +xhex = " "; +xascii = " "; +} + +/*************************************************************************** + * FUNCTION: int screeninit() * + * GOOD FOR: Initialization of the n-curses-screen * + * RESULTS : 1:There was an error / 2:Everything ok. * + ***************************************************************************/ +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Create Curses-space for the windows +//TODO: check whether creation was fine. + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// enable scrolling + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// enable Keyboard-control + keypad(stdscr,1); + keypad(whex,1); + keypad(wascii,1); + +// disable echoing of input + noecho(); + +// swich cursor to big size +// TODO: check why this not works in most cases +// if(curs_set(2)==ERR) retval=1 ; + +// creation of curses "Color-Pairs" + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + if(init_pair(3,COLOR_MAGENTA,COLOR_BLACK)) retval=1; + +// join the windows with the color-pairs + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + +// paint the boxes around the windows (in fact it's inside) + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + // change the color-pairs of the windows again. + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +/*************************************************************************** + * FUNCTION: char* tonumsysx() * + * GOOD FOR: Shift a number to another numbering system * + * INPUTS : - int base10 -The number in decimal system * + * - int newbase -The new system (16->Hex,2->Bin,...) * + * RESULTS : The number in the new system as a pointer of char * + ***************************************************************************/ +char* tonumsysx(int base10,int newbase) +{ + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +/*************************************************************************** + * FUNCTION: unsigned long xto10(char *c, int oldbase) * + * GOOD FOR: Shift a number from a foreign numbering system to decimal * + * INPUTS : - char* c -The number in foreign system * + * - int oldbase -The old system (16->Hex,2->Bin,...) * + * RESULTS : The number in decimal system as a unsigned long * + ***************************************************************************/ +unsigned long xto10(char *c, int oldbase) +{ +unsigned long base10=0; +int pos=0; +int i; + for (i=0;c[i];i++, c[i]=toupper(c[i])); + while(c[pos++]); + pos--; + int len=pos-1; + while(pos--) + { + if(isdigit(c[len-pos])) + { + base10 = base10 + (unsigned long)( (double)(c[len-pos]-'0') * ( pow( (double)oldbase,(double)pos ) ) ) ; + } + else + { + base10 = base10 + (unsigned long)( ((double)(c[len-pos]-'A')+10) * ( pow( (double)oldbase,(double)pos ) ) ); + } + } + return(base10); +} + +/*************************************************************************** + * FUNCTION: int loadcontents() * + * GOOD FOR: Loading the contents of a file into the memory * + * RESULTS : The status of the loading (not used yet) * + ***************************************************************************/ +int loadcontents() +{ + int retval=0; + int x; + char ch; + unsigned long line=0; + + + pdatas=pfirst; + ch=fgetc(fp); + // Zero-byte-file. + if(feof(fp)) + { + fprintf(stderr,"opening 0-Byte file"); + pfirst=pmax=pdatas=(data*)(malloc(sizeof(datas))); + (*pdatas).pnext=NULL; + (*pdatas).plast=NULL; + (*pdatas).line=0; + (*pdatas).len=0; + + } + // TODO: how to flush the buffer of stdout??? + printf("Printing hashmark for every 8 Kbytes.\n"); + while (!(feof(fp))) + { + for(x=0;x<16;x++) + { + ((*pdatas).vals[x])=ch; + if (feof(fp)) //(*pdatas).vals[x]==EOF) + { + (*pdatas).vals[x]=0; + break; + } + (*pdatas).len=x+1; + ch=fgetc(fp); + } + (*pdatas).line=line++; + if(!(feof(fp))) + { + phelp=pdatas; + if (!(line%500)) {printf("%c",'#');} + pdatas=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); +// BUG: Check for Memory-Full does not work. + if(!pdatas) {abort();} + (*pdatas).plast=phelp; + (*pdatas).pnext=NULL; + } + pmax=pdatas; + + } + return (retval); +} + +/*************************************************************************** + * FUNCTION: void endscr() * + * GOOD FOR: End the scrren session on curses-level * + ***************************************************************************/ +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +/*************************************************************************** + * FUNCTION: void unallocmem() * + * GOOD FOR: Free the memory from the bits & bytes * + ***************************************************************************/ +void unallocmem() +{ +pdatas=pmax; +while((*pdatas).plast) +{ +phelp=(*pdatas).plast; +free(pdatas); +pdatas=phelp; +} +} + +/*************************************************************************** + * FUNCTION: void scrrefresh() * + * GOOD FOR: Refreah the basic-screen-elements (not the contents) * + ***************************************************************************/ +void scrrefresh() +{ + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + // depending on the mode (ascii/hex) set the cursor-position + !mode?wmove(whex,pos.y,pos.x):wmove(wascii,pos.y,pos.x); + + wrefresh(woffs); + wrefresh(wtop); + wrefresh(wbot); + // the order is given by the mode (ascii/hex). the last window refreshed + // it the one, in which the cursor appears and the input-focus is. + !mode?wrefresh(wascii):wrefresh(whex); + mode?wrefresh(wascii):wrefresh(whex); +} + +/*************************************************************************** + * FUNCTION: void emplineout(unsigned long line) * + * GOOD FOR: Writing one empty line to the scrren * + ***************************************************************************/ +void emplineout(unsigned long line) +{ + mvwaddstr(woffs,line,1," "); + mvwaddstr(whex,line,1," "); + mvwaddstr(wascii,line,1," "); +} + +/*************************************************************************** + * FUNCTION: void lineout(unsigned long line) * + * GOOD FOR: Writing one line (the actual one) from the memory to screen * + ***************************************************************************/ +void lineout(unsigned long line) +{ + int z; + + miniinit(); + + ptv = tonumsysx((*pdatas).line*16,16); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,line,1,soffs); + for (z=0;z<(*pdatas).len;z++) + { + ptv = tonumsysx((*pdatas).vals[z],16); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,line,1,shex); + for (z=0;z<(*pdatas).len;z++) + { +// TODO: There are many other characters that are displayable. + xascii[z] = (isgraph((*pdatas).vals[z]))?((*pdatas).vals[z]):'.'; + } + mvwaddstr(wascii,line,1,sascii); +} + +/*************************************************************************** + * FUNCTION: void initout() * + * GOOD FOR: First output after loading the file // home-Key * + ***************************************************************************/ +void initout() +{ +//Initial output (All) +int i,z; +bool toggleswitch=false; +pdatas=pfirst; + +if ((*pdatas).pnext == NULL) toggleswitch=true; +for(i=0;i<16;i++) + { + lineout(i+1); + if (toggleswitch) break; + pdatas=(*pdatas).pnext; + if ((*pdatas).pnext == NULL) toggleswitch=true; + } + scrrefresh(); + +if(i>15) pdatas=(*pdatas).plast; +} + +/*************************************************************************** + * FUNCTION: int scrolld() * + * GOOD FOR: Scrolling the scrren one line down * + * RESULTS : 1: no more elements // 0: everything was nice * + ***************************************************************************/ +int scrolld() +{ + int resval; + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + wscrl(woffs,1); + wscrl(whex,1); + wscrl(wascii,1); + + miniinit(); + + lineout(16); + resval=1; + } + else + { + resval=0; + } + return(resval); +} + +/*************************************************************************** + * FUNCTION: int scrollu() * + * GOOD FOR: Scrolling the scrren one line up * + * RESULTS : 1: at first element // 0: everything was nice * + ***************************************************************************/ +int scrollu() +{ + int z=0; + int result; + while(((*pdatas).plast != NULL)&&(z < 16)) + { + pdatas=(*pdatas).plast; + z++; + } + result= (z!=15)?1:0; + if(z>15) + { + z--; + wscrl(woffs,-1); + wscrl(whex,-1); + wscrl(wascii,-1); + + miniinit(); + + lineout(1); + } + while(z--) + { + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + } + } + return(result); +} + +/*************************************************************************** + * FUNCTION: void switchmode() * + * GOOD FOR: recalculation of cursor-position ascii<->hex * + ***************************************************************************/ +void switchmode() +{ + if(mode==0) + { + mode=1; // mode-toggle:Hex->Ascii + // recalculate cursor-position + pos.x=((pos.x+1)/3); + } + else + { + mode=0; // mode-toggle:ascii->Hex + // recalculate cursor-position + pos.x=((pos.x*3)-1); + } +} + +/*************************************************************************** + * FUNCTION: void moveleft() * + * GOOD FOR: move the cursor one position to the left * + ***************************************************************************/ +void moveleft() +{ + if(mode==0) + { + if(pos.x > 2) + { + (pos.x%3)?pos.x-=2:pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=48; + } + else + { + if(scrollu())pos.x=48; + } + } + } + else + { + if(pos.x > 1) + { + pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=16; + } + else + { + if(scrollu())pos.x=16; + } + } + } +} + +/*************************************************************************** + * FUNCTION: void moveright() * + * GOOD FOR: move the cursor one position to the right * + ***************************************************************************/ +void moveright() +{ + if(mode==0) + { + if(pos.x < 48) + { + (pos.x%3)?pos.x++:pos.x+=2; + } + else + { + if(pos.y < 16) + { + pos.x=2; + pos.y++; + } + else + { + if(scrolld()) pos.x=2; + } + } + } + else + { + if((pos.x < 16)) + { + pos.x++; + } + else + { + if(pos.y < 16) + { + pos.x=1; + pos.y++; + } + else + { + if(scrolld()) pos.x=1; + } + } + } +} + +/*************************************************************************** + * FUNCTION: unsigned long getactpos() * + * GOOD FOR: get the acutal cursor-position as a file-offset * + * RESULTS : The file-offset on the cursor-position as unsigned long * + ***************************************************************************/ +unsigned long getactpos() +{ + int i=0; + unsigned long actpos; + int distance; + int posxsave=9999; + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the position + actpos=((*pdatas).line*16)+pos.x-1; + // and move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + if (posxsave!=9999) + { + switchmode(); + pos.x=posxsave; + } + + return(actpos); +} + +/*************************************************************************** + * FUNCTION: void charin(char ch, int realmode, int realxpos) * + * GOOD FOR: insert/override one character in the memory * + * INPUTS : - char ch -the char to insert * + * - int realmode -the real mode (hex/ascii) * + * - int realxpos -the real x-position of the cursor * + ***************************************************************************/ +void charin(char ch, int realmode, int realxpos) +{ + int i=0; + int z=0; + int distance=0; + bool toggleswitch; + data *phelp2; + char rbmode; //rollbackmode (if file < 255 chars = 1 else 2) + unsigned long zhelp; + // move to the cursor-position in memory + // Is the file smaller then one page, it has to be handled in another way. + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + rbmode=1; + } + else + { + distance=16-pos.y; + rbmode=2; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // For the insert-mode + if (((insovr=='I') && (realmode==1))||((insovr=='I') && (realmode==0) && (realxpos%3==2)) && (realmode!=3)) + { + phelp=pdatas; + pdatas=pmax; + if ((*pdatas).len < 16) + { + (*pdatas).len++; + } + else + { + // allocate new element + phelp2=pmax; + zhelp=(*pmax).line+1; + pdatas=pmax=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); + if(!pmax) {abort();} + (*pmax).plast=phelp2; + (*pmax).pnext=NULL; + (*pmax).len=1; + (*pmax).line=zhelp; + } + while(pdatas!=phelp) + { + for(z=(*pdatas).len-1;z>=0;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + (*pdatas).vals[0]=(*(*pdatas).plast).vals[15]; + pdatas=(*pdatas).plast; + } + + for(z=(*pdatas).len-1;z+1>pos.x;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + pdatas=phelp; + } + // Write value + (*pdatas).vals[pos.x-1]=ch; + // refresh output of current line (not needed, I think) + lineout(pos.y); + // moving backwards again and print the lines immediate. + if (rbmode==1) + { + pdatas=pfirst; + if ((*pdatas).pnext == NULL) toggleswitch=true; + for(z=0;z<16;z++) + { + lineout(z+1); + if (toggleswitch) break; + pdatas=(*pdatas).pnext; + if ((*pdatas).pnext == NULL) toggleswitch=true; + } + } + else + { + while(i--) + { + if((*pdatas).pnext) + { + pdatas=(*pdatas).pnext; + lineout(16-i); + } + } + } +} + +/*************************************************************************** + * FUNCTION: int charout() * + * GOOD FOR: get the char at the actual cursor-position * + * RESULTS : the char as an integer (char should work too normaly) * + ***************************************************************************/ +int charout() +{ + int i=0; + int distance; + int posxsave; + int ch; + // Move in memory to cursor-position + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the value + ch=((*pdatas).vals[pos.x-1]); + // move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + return(ch); +} + +/*************************************************************************** + * FUNCTION: void bottomout() * + * GOOD FOR: refreshing the output of the bottom-window * + ***************************************************************************/ +void bottomout() +{ + int realmode,realposx; + realmode=mode; + + if (realmode == 0) + { + realposx=pos.x; + switchmode(); + } + mvwaddstr(wbot,1,1,empty78); + mvwaddstr(wbot,2,1,empty78); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,1,"CHR:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddch(wbot,1,6,32); + mvwaddch(wbot,1,5,(isgraph(charout()))?charout():'.'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,9,"HEX:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,13,(tonumsysx(charout(),16))+6); + mvwaddch(wbot,1,15,'h'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,18,"DEC:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,22,(tonumsysx(charout(),10))+5); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,27,"OCT:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,31,(tonumsysx(charout(),8))+4); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,37,"BIN:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,41,tonumsysx(charout(),2)); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,52,"Offset:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,59,tonumsysx(getactpos(),16)); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,67,"h -"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,71,tonumsysx(getactpos(),10)); + + mvwaddstr(wbot,2,1,filename); + mvwaddstr(wbot,2,76,(insovr=='O')?"OVR":"INS"); + if (realmode == 0) + { + switchmode(); + pos.x=realposx; + } +} + +/*************************************************************************** + * FUNCTION: void swapbits() * + * GOOD FOR: toggle the bit [par1] an the actual position * + * INPUTS : - char bit the number (1-8) of the bit to toggle * + ***************************************************************************/ +void swapbits(char bit) +{ + char bin[]="12345678",*pbin; // for the Bits + pbin=bin; + int realposx, realmode; + realmode=mode; + if (realmode==0) + { + realposx=pos.x; + switchmode(); + } + pbin=tonumsysx(charout(),2); + pbin[bit]= (pbin[bit]=='0')?'1':'0'; + charin(xto10(pbin,2),3,pos.x); + if (realmode==0) + { + switchmode(); + pos.x=realposx; + } +} + +/*************************************************************************** + * FUNCTION: void delactchar(char ch, int realmode, int realxpos) * + * GOOD FOR: delete the char under the actual cursor-position * + * INPUTS : - int realmode -the real mode (hex/ascii) * + * - int realxpos -the real x-position of the cursor * + ***************************************************************************/ +void delactchar(int realmode, int realxpos) +{ + //BUG: after deletion the last char in the last line it gives me a segfault. + int i=0; + int z=0; + int distance=0; + bool endflag=FALSE; + bool toggleswitch; + char rbmode; //rollbackmode (if file < 255 chars = 1 else 2) + + // move to the cursor-position in memory + // Is the file smaller then one page, it has to be handled in another way. + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + rbmode=1; + } + else + { + distance=16-pos.y; + rbmode=2; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // For the insert-mode + if (((insovr=='I') && (realmode==1))||((insovr=='I') && (realmode==0) && (realxpos%3==2)) && (realmode!=3)) + { + phelp=pdatas; + for(z=realxpos-1;z<15;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + if((*pdatas).pnext) + { + (*pdatas).vals[15]=(*(*pdatas).pnext).vals[0]; + pdatas=(*pdatas).pnext; + } + while(pdatas!=pmax) + { + for(z=0;z<15;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + (*pdatas).vals[15]=(*(*pdatas).pnext).vals[0]; + pdatas=(*pdatas).pnext; + } +// check here for last char in last element + (*pdatas).len--; + if((*pdatas).len <= 0) + { + if(phelp==pmax) + { + if((*phelp).plast) + { + phelp=(*phelp).plast; + } + } + if(pfirst!=pmax) // is when pfirst!=pmax + { + pdatas=(*pdatas).plast; + free((*pdatas).pnext); + (*pdatas).pnext=NULL; + pmax=pdatas; + // TODO: there is one line less now, when we are at the last screen + // we should decrease i by one (only cosmetic) + endflag=TRUE; + } + else + { + (*pdatas).len=0; + } + } + else + { + if (phelp!=pmax) for(z=0;z<(*pdatas).len;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + } + pdatas=phelp; + } + // refresh output of current line (not needed, I think) + lineout(pos.y); + // moving backwards again and print the lines immediate. + if (rbmode==1) + { + // first delete the screen + for(z=0;z<16;z++) + { + emplineout(z+1); + } + // then do an initial output + initout(); + } + else + { + while(i--) + { + if((*pdatas).pnext) + { + pdatas=(*pdatas).pnext; + lineout(16-i); + } + } + } +// The rest is only a dirty workaround for the las (no longer needed) line +// because it was not possible for me to determine out, whether i am really +// at the end. + if (endflag) + { + for(i=0;i<16;i++) + { + if(!scrollu()) endflag=FALSE; + } + if (endflag) + { + if((pos.y < 16) && ((*pmax).line > 15)) pos.y++; + for(i=0;i<16;i++) + { + scrolld(); + } + } + } +} + +int main(int argc, char *argv[]) +{ +//Variables + unsigned int i; // Public counter + int inpch; // The char of input + +//Preperation + printf("GHEX Version 0.5\n"); + //Is there a param? + if(argc < 2) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //open the file + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // set the pointers + initvar(); + // init of other useful varibles + pos.x = pos.y = 1; + filename=argv[1]; + //file -> ram + if(loadcontents()) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // init of terminal + if(screeninit()) + { + endscr(); + fprintf(stderr,"!!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + + +//Main-procedure +initout(); +bottomout(); +scrrefresh(); + +//Event-loop + while(inpch!=KEY_F(10)) + { + if (mode==0) + { + inpch=wgetch(whex); + } + else + { + inpch=wgetch(wascii); + } + switch(inpch) + { + // The Keys F1-F8 (bit-setting) + case KEY_F(9): switchmode(); + break; + + case KEY_F(1): swapbits(0); + break; + + case KEY_F(2): swapbits(1); + break; + + case KEY_F(3): swapbits(2); + break; + + case KEY_F(4): swapbits(3); + break; + + case KEY_F(5): swapbits(4); + break; + + case KEY_F(6): swapbits(5); + break; + + case KEY_F(7): swapbits(6); + break; + + case KEY_F(8): swapbits(7); + break; + + //the delete character-key + case KEY_DC : if (mode == 1) + { + delactchar(1,pos.x); + } + else + { + switchmode(); + delactchar(1,pos.x); + switchmode(); + } + break; + + //the insert-key, switch insert mode + case KEY_EIC: //fallthru + case KEY_IC : if (insovr=='O') + { + insovr='I'; + } + else + { + insovr='O'; + } + break; + + //Cursor-movement + case KEY_LEFT: moveleft(); + break; + + case KEY_RIGHT:moveright(); + break; + + case KEY_DOWN: if(pos.y>15) + { + scrolld(); + } + else + { + pos.y++; + } + break; + + case KEY_UP: if(pos.y<2) + { + scrollu(); + } + else + { + pos.y--; + } + break; + + case KEY_NPAGE:for(i=0;i<16;i++) + { + scrolld(); + } + break; + + case KEY_PPAGE:for(i=0;i<16;i++) + { + scrollu(); + } + break; + + case KEY_HOME: initout(); + break; + + case KEY_END: if((*pmax).line > 15) + { + pdatas=pmax; + for(i=0;((*pdatas).plast) && (i < 16);i++) + { + pdatas=(*pdatas).plast; + } + while((*pdatas).pnext) + { + scrolld(); + } + } + break; + // input characters + default: if(mode==0) + { + if(isdigit(inpch) || (toupper(inpch)>='A' && toupper(inpch)<='F') ) + { + waddch(whex,toupper(inpch)); + char hch[]="00"; + // fill hch correct (old values) + int xpos=pos.x; // save position + switchmode(); // recalc. coordinates + ptv=tonumsysx(charout(),16); // actual char in Hex. + //* --- DEBUG ONLY + mvwaddstr(wbot,2,30,tonumsysx(charout(),16)); + // --- END DEBUG + switchmode(); // recalc coordinates + pos.x=xpos; // restore coorinates + if (insovr=='O') // Predef. only in Override! + { + hch[0] = *(ptv+6); + hch[1] = *(ptv+7); + } + else // In Insert only first char + { + if(pos.x%3==0) + hch[0] = *(ptv+6); + } + //* --- DEBUG ONLY + mvwaddstr(wbot,2,1,hch); + mvwaddstr(wbot,2,20,ptv); + // --- END DEBUG + hch[(pos.x%3)?0:1]=inpch; + switchmode(); // recalc coordinates + charin(xto10(hch,16),0,xpos); // calc Hex -> Ascii+charin + switchmode(); // and back + pos.x=xpos; // restore positions + moveright(); + } + } + + else + { + if((inpch < 256)) + { + waddch(wascii,inpch); + charin(inpch,1,pos.x); + moveright(); + } + } + }; + + bottomout(); + scrrefresh(); + + } + endscr(); + unallocmem(); + + return (fclose(fp)); +} diff --git a/tiahv11.cc b/tiahv11.cc new file mode 100644 index 0000000..f0b996e --- /dev/null +++ b/tiahv11.cc @@ -0,0 +1,1248 @@ +/* + * GHEX -- an advanced HEX-Editor + * Copyright (C) 1999 by Joerg Tretter + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include +#include + +struct data // one line in Memory +{ +struct data* plast; +struct data* pnext; +unsigned char vals[16]; +unsigned long line; +char len; +}; + +struct cpos // Cursor-Position +{ + char x,y; +}pos; + +char insovr='O'; // Isert/Override-Mode +int mode=1; +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII "; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +char empty78[]=" "; +int result; +char *filename; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data datas, *pdatas, *pfirst, *phelp, *pmax; + +/*************************************************************************** + * FUNCTION: void initvar() * + * GOOD FOR: Initialization of basic-variables * + ***************************************************************************/ +void initvar() +{ + pfirst=&datas; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + (*pfirst).line=0; + (*pfirst).len=0; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +/*************************************************************************** + * FUNCTION: void miniinit() * + * GOOD FOR: Initialization of frequent used variables * + ***************************************************************************/ +void miniinit() +{ +xoffs = " "; +xhex = " "; +xascii = " "; +} + +/*************************************************************************** + * FUNCTION: int screeninit() * + * GOOD FOR: Initialization of the n-curses-screen * + * RESULTS : 1:There was an error / 2:Everything ok. * + ***************************************************************************/ +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Create Curses-space for the windows +//TODO: check whether creation was fine. + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// enable scrolling + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// enable Keyboard-control + keypad(stdscr,1); + keypad(whex,1); + keypad(wascii,1); + +// disable echoing of input + noecho(); + +// swich cursor to big size +// TODO: check why this not works in most cases +// if(curs_set(2)==ERR) retval=1 ; + +// creation of curses "Color-Pairs" + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + if(init_pair(3,COLOR_MAGENTA,COLOR_BLACK)) retval=1; + +// join the windows with the color-pairs + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + +// paint the boxes around the windows (in fact it's inside) + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + // change the color-pairs of the windows again. + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +/*************************************************************************** + * FUNCTION: char* tonumsysx() * + * GOOD FOR: Shift a number to another numbering system * + * INPUTS : - int base10 -The number in decimal system * + * - int newbase -The new system (16->Hex,2->Bin,...) * + * RESULTS : The number in the new system as a pointer of char * + ***************************************************************************/ +char* tonumsysx(int base10,int newbase) +{ + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +/*************************************************************************** + * FUNCTION: unsigned long xto10(char *c, int oldbase) * + * GOOD FOR: Shift a number from a foreign numbering system to decimal * + * INPUTS : - char* c -The number in foreign system * + * - int oldbase -The old system (16->Hex,2->Bin,...) * + * RESULTS : The number in decimal system as a unsigned long * + ***************************************************************************/ +unsigned long xto10(char *c, int oldbase) +{ +unsigned long base10=0; +int pos=0; +int i; + for (i=0;c[i];i++, c[i]=toupper(c[i])); + while(c[pos++]); + pos--; + int len=pos-1; + while(pos--) + { + if(isdigit(c[len-pos])) + { + base10 = base10 + (unsigned long)( (double)(c[len-pos]-'0') * ( pow( (double)oldbase,(double)pos ) ) ) ; + } + else + { + base10 = base10 + (unsigned long)( ((double)(c[len-pos]-'A')+10) * ( pow( (double)oldbase,(double)pos ) ) ); + } + } + return(base10); +} + +/*************************************************************************** + * FUNCTION: int loadcontents() * + * GOOD FOR: Loading the contents of a file into the memory * + * RESULTS : The status of the loading (not used yet) * + ***************************************************************************/ +int loadcontents() +{ + int retval=0; + int x; + char ch; + unsigned long line=0; + + + pdatas=pfirst; + ch=fgetc(fp); + // Zero-byte-file. + if(feof(fp)) + { + fprintf(stderr,"opening 0-Byte file"); + pfirst=pmax=pdatas=(data*)(malloc(sizeof(datas))); + (*pdatas).pnext=NULL; + (*pdatas).plast=NULL; + (*pdatas).line=0; + (*pdatas).len=0; + + } + // TODO: how to flush the buffer of stdout??? + printf("Printing hashmark for every 8 Kbytes.\n"); + while (!(feof(fp))) + { + for(x=0;x<16;x++) + { + ((*pdatas).vals[x])=ch; + if (feof(fp)) //(*pdatas).vals[x]==EOF) + { + (*pdatas).vals[x]=0; + break; + } + (*pdatas).len=x+1; + ch=fgetc(fp); + } + (*pdatas).line=line++; + if(!(feof(fp))) + { + phelp=pdatas; + if (!(line%500)) {printf("%c",'#');} + pdatas=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); +// BUG: Check for Memory-Full does not work. + if(!pdatas) {abort();} + (*pdatas).plast=phelp; + (*pdatas).pnext=NULL; + } + pmax=pdatas; + + } + return (retval); +} + +/*************************************************************************** + * FUNCTION: void endscr() * + * GOOD FOR: End the scrren session on curses-level * + ***************************************************************************/ +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +/*************************************************************************** + * FUNCTION: void unallocmem(int save, char* filename) * + * GOOD FOR: Free the memory from the bits & bytes * + * INPUTS : int save: flag (save Y/else key) * + * : cahr* filename: the name of the save-file * + ***************************************************************************/ +void unallocmem(int save, char *filename) +{ + int pos; + save=toupper(save); + if (save=='Y') + { + pdatas=pfirst; + fp=fopen(filename,"w"); + while((*pdatas).pnext) + { + for (pos=0;pos <(*pdatas).len;pos++) + { + fputc((*pdatas).vals[pos],fp); + } + pdatas=(*pdatas).pnext; + } + for (pos=0;pos <(*pdatas).len;pos++) fputc((*pdatas).vals[pos],fp); + close(fp); + } + { + pdatas=pmax; + } + while((*pdatas).plast) + { + phelp=(*pdatas).plast; + free(pdatas); + pdatas=phelp; + } +} + +/*************************************************************************** + * FUNCTION: void scrrefresh() * + * GOOD FOR: Refreah the basic-screen-elements (not the contents) * + ***************************************************************************/ +void scrrefresh() +{ + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + // depending on the mode (ascii/hex) set the cursor-position + !mode?wmove(whex,pos.y,pos.x):wmove(wascii,pos.y,pos.x); + + wrefresh(woffs); + wrefresh(wtop); + wrefresh(wbot); + // the order is given by the mode (ascii/hex). the last window refreshed + // it the one, in which the cursor appears and the input-focus is. + !mode?wrefresh(wascii):wrefresh(whex); + mode?wrefresh(wascii):wrefresh(whex); +} + +/*************************************************************************** + * FUNCTION: void emplineout(unsigned long line) * + * GOOD FOR: Writing one empty line to the scrren * + ***************************************************************************/ +void emplineout(unsigned long line) +{ + mvwaddstr(woffs,line,1," "); + mvwaddstr(whex,line,1," "); + mvwaddstr(wascii,line,1," "); +} + +/*************************************************************************** + * FUNCTION: void lineout(unsigned long line) * + * GOOD FOR: Writing one line (the actual one) from the memory to screen * + ***************************************************************************/ +void lineout(unsigned long line) +{ + int z; + + miniinit(); + + ptv = tonumsysx((*pdatas).line*16,16); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,line,1,soffs); + for (z=0;z<(*pdatas).len;z++) + { + ptv = tonumsysx((*pdatas).vals[z],16); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,line,1,shex); + for (z=0;z<(*pdatas).len;z++) + { +// TODO: There are many other characters that are displayable. + xascii[z] = (isgraph((*pdatas).vals[z]))?((*pdatas).vals[z]):'.'; + } + mvwaddstr(wascii,line,1,sascii); +} + +/*************************************************************************** + * FUNCTION: void initout() * + * GOOD FOR: First output after loading the file // home-Key * + ***************************************************************************/ +void initout() +{ +//Initial output (All) +int i,z; +bool toggleswitch=false; +pdatas=pfirst; + +if ((*pdatas).pnext == NULL) toggleswitch=true; +for(i=0;i<16;i++) + { + lineout(i+1); + if (toggleswitch) break; + pdatas=(*pdatas).pnext; + if ((*pdatas).pnext == NULL) toggleswitch=true; + } + scrrefresh(); + +if(i>15) pdatas=(*pdatas).plast; +} + +/*************************************************************************** + * FUNCTION: int scrolld() * + * GOOD FOR: Scrolling the scrren one line down * + * RESULTS : 1: no more elements // 0: everything was nice * + ***************************************************************************/ +int scrolld() +{ + int resval; + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + wscrl(woffs,1); + wscrl(whex,1); + wscrl(wascii,1); + + miniinit(); + + lineout(16); + resval=1; + } + else + { + resval=0; + } + return(resval); +} + +/*************************************************************************** + * FUNCTION: int scrollu() * + * GOOD FOR: Scrolling the scrren one line up * + * RESULTS : 1: at first element // 0: everything was nice * + ***************************************************************************/ +int scrollu() +{ + int z=0; + int result; + while(((*pdatas).plast != NULL)&&(z < 16)) + { + pdatas=(*pdatas).plast; + z++; + } + result= (z!=15)?1:0; + if(z>15) + { + z--; + wscrl(woffs,-1); + wscrl(whex,-1); + wscrl(wascii,-1); + + miniinit(); + + lineout(1); + } + while(z--) + { + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + } + } + return(result); +} + +/*************************************************************************** + * FUNCTION: void switchmode() * + * GOOD FOR: recalculation of cursor-position ascii<->hex * + ***************************************************************************/ +void switchmode() +{ + if(mode==0) + { + mode=1; // mode-toggle:Hex->Ascii + // recalculate cursor-position + pos.x=((pos.x+1)/3); + } + else + { + mode=0; // mode-toggle:ascii->Hex + // recalculate cursor-position + pos.x=((pos.x*3)-1); + } +} + +/*************************************************************************** + * FUNCTION: void moveleft() * + * GOOD FOR: move the cursor one position to the left * + ***************************************************************************/ +void moveleft() +{ + if(mode==0) + { + if(pos.x > 2) + { + (pos.x%3)?pos.x-=2:pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=48; + } + else + { + if(scrollu())pos.x=48; + } + } + } + else + { + if(pos.x > 1) + { + pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=16; + } + else + { + if(scrollu())pos.x=16; + } + } + } +} + +/*************************************************************************** + * FUNCTION: void moveright() * + * GOOD FOR: move the cursor one position to the right * + ***************************************************************************/ +void moveright() +{ + if(mode==0) + { + if(pos.x < 48) + { + (pos.x%3)?pos.x++:pos.x+=2; + } + else + { + if(pos.y < 16) + { + pos.x=2; + pos.y++; + } + else + { + if(scrolld()) pos.x=2; + } + } + } + else + { + if((pos.x < 16)) + { + pos.x++; + } + else + { + if(pos.y < 16) + { + pos.x=1; + pos.y++; + } + else + { + if(scrolld()) pos.x=1; + } + } + } +} + +/*************************************************************************** + * FUNCTION: unsigned long getactpos() * + * GOOD FOR: get the acutal cursor-position as a file-offset * + * RESULTS : The file-offset on the cursor-position as unsigned long * + ***************************************************************************/ +unsigned long getactpos() +{ + int i=0; + unsigned long actpos; + int distance; + int posxsave=9999; + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the position + actpos=((*pdatas).line*16)+pos.x-1; + // and move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + if (posxsave!=9999) + { + switchmode(); + pos.x=posxsave; + } + + return(actpos); +} + +/*************************************************************************** + * FUNCTION: void charin(char ch, int realmode, int realxpos) * + * GOOD FOR: insert/override one character in the memory * + * INPUTS : - char ch -the char to insert * + * - int realmode -the real mode (hex/ascii) * + * - int realxpos -the real x-position of the cursor * + ***************************************************************************/ +void charin(char ch, int realmode, int realxpos) +{ + int i=0; + int z=0; + int distance=0; + bool toggleswitch; + data *phelp2; + char rbmode; //rollbackmode (if file < 255 chars = 1 else 2) + unsigned long zhelp; + // move to the cursor-position in memory + // Is the file smaller then one page, it has to be handled in another way. + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + rbmode=1; + } + else + { + distance=16-pos.y; + rbmode=2; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // For the insert-mode + if (((insovr=='I') && (realmode==1))||((insovr=='I') && (realmode==0) && (realxpos%3==2)) && (realmode!=3)) + { + phelp=pdatas; + pdatas=pmax; + if ((*pdatas).len < 16) + { + (*pdatas).len++; + } + else + { + // allocate new element + phelp2=pmax; + zhelp=(*pmax).line+1; + pdatas=pmax=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); + if(!pmax) {abort();} + (*pmax).plast=phelp2; + (*pmax).pnext=NULL; + (*pmax).len=1; + (*pmax).line=zhelp; + if (rbmode == 1) phelp=pmax; + } + while(pdatas!=phelp) + { + for(z=(*pdatas).len-1;z>=0;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + (*pdatas).vals[0]=(*(*pdatas).plast).vals[15]; + pdatas=(*pdatas).plast; + } + + for(z=(*pdatas).len-1;z+1>pos.x;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + pdatas=phelp; + } + // Write value + (*pdatas).vals[pos.x-1]=ch; + // refresh output of current line (not needed, I think) + lineout(pos.y); + // moving backwards again and print the lines immediate. + if (rbmode==1) + { + initout(); + } + else + { + while(i--) + { + if((*pdatas).pnext) + { + pdatas=(*pdatas).pnext; + lineout(16-i); + } + } + } +} + +/*************************************************************************** + * FUNCTION: int charout() * + * GOOD FOR: get the char at the actual cursor-position * + * RESULTS : the char as an integer (char should work too normaly) * + ***************************************************************************/ +int charout() +{ + int i=0; + int distance; + int posxsave; + int ch; + // Move in memory to cursor-position + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the value + ch=((*pdatas).vals[pos.x-1]); + // move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + return(ch); +} + +/*************************************************************************** + * FUNCTION: void bottomout() * + * GOOD FOR: refreshing the output of the bottom-window * + ***************************************************************************/ +void bottomout() +{ + int realmode,realposx; + realmode=mode; + + if (realmode == 0) + { + realposx=pos.x; + switchmode(); + } + mvwaddstr(wbot,1,1,empty78); + mvwaddstr(wbot,2,1,empty78); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,1,"CHR:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddch(wbot,1,6,32); + mvwaddch(wbot,1,5,(isgraph(charout()))?charout():'.'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,9,"HEX:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,13,(tonumsysx(charout(),16))+6); + mvwaddch(wbot,1,15,'h'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,18,"DEC:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,22,(tonumsysx(charout(),10))+5); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,27,"OCT:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,31,(tonumsysx(charout(),8))+4); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,37,"BIN:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,41,tonumsysx(charout(),2)); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,52,"Offset:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,59,tonumsysx(getactpos(),16)); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,67,"h -"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,71,tonumsysx(getactpos(),10)); + + mvwaddstr(wbot,2,1,filename); + mvwaddstr(wbot,2,76,(insovr=='O')?"OVR":"INS"); + if (realmode == 0) + { + switchmode(); + pos.x=realposx; + } +} + +/*************************************************************************** + * FUNCTION: void swapbits() * + * GOOD FOR: toggle the bit [par1] an the actual position * + * INPUTS : - char bit the number (1-8) of the bit to toggle * + ***************************************************************************/ +void swapbits(char bit) +{ + char bin[]="12345678",*pbin; // for the Bits + pbin=bin; + int realposx, realmode; + realmode=mode; + if (realmode==0) + { + realposx=pos.x; + switchmode(); + } + pbin=tonumsysx(charout(),2); + pbin[bit]= (pbin[bit]=='0')?'1':'0'; + charin(xto10(pbin,2),3,pos.x); + if (realmode==0) + { + switchmode(); + pos.x=realposx; + } +} + +/*************************************************************************** + * FUNCTION: void delactchar(char ch, int realmode, int realxpos) * + * GOOD FOR: delete the char under the actual cursor-position * + * INPUTS : - int realmode -the real mode (hex/ascii) * + * - int realxpos -the real x-position of the cursor * + ***************************************************************************/ +void delactchar(int realmode, int realxpos) +{ + //BUG: after deletion the last char in the last line it gives me a segfault. + int i=0; + int z=0; + int distance=0; + bool endflag=FALSE; + bool toggleswitch; + char rbmode; //rollbackmode (if file < 255 chars = 1 else 2) + + // move to the cursor-position in memory + // Is the file smaller then one page, it has to be handled in another way. + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + rbmode=1; + } + else + { + distance=16-pos.y; + rbmode=2; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // For the insert-mode + if (((insovr=='I') && (realmode==1))||((insovr=='I') && (realmode==0) && (realxpos%3==2)) && (realmode!=3)) + { + phelp=pdatas; + for(z=realxpos-1;z<15;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + if((*pdatas).pnext) + { + (*pdatas).vals[15]=(*(*pdatas).pnext).vals[0]; + pdatas=(*pdatas).pnext; + } + while(pdatas!=pmax) + { + for(z=0;z<15;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + (*pdatas).vals[15]=(*(*pdatas).pnext).vals[0]; + pdatas=(*pdatas).pnext; + } +// check here for last char in last element + (*pdatas).len--; + if((*pdatas).len <= 0) + { + if(phelp==pmax) + { + if((*phelp).plast) + { + phelp=(*phelp).plast; + } + } + if(pfirst!=pmax) // is when pfirst!=pmax + { + pdatas=(*pdatas).plast; + free((*pdatas).pnext); + (*pdatas).pnext=NULL; + pmax=pdatas; + // TODO: there is one line less now, when we are at the last screen + // we should decrease i by one (only cosmetic) + endflag=TRUE; + } + else + { + (*pdatas).len=0; + } + } + else + { + if (phelp!=pmax) for(z=0;z<(*pdatas).len;z++) + { + (*pdatas).vals[z]=(*pdatas).vals[z+1]; + } + } + pdatas=phelp; + } + // refresh output of current line (not needed, I think) + lineout(pos.y); + // moving backwards again and print the lines immediate. + if (rbmode==1) + { + // first delete the screen + for(z=0;z<16;z++) + { + emplineout(z+1); + } + // then do an initial output + initout(); + } + else + { + while(i--) + { + if((*pdatas).pnext) + { + pdatas=(*pdatas).pnext; + lineout(16-i); + } + } + } +// The rest is only a dirty workaround for the las (no longer needed) line +// because it was not possible for me to determine out, whether i am really +// at the end. + if (endflag) + { + for(i=0;i<16;i++) + { + if(!scrollu()) endflag=FALSE; + } + if (endflag) + { + if((pos.y < 16) && ((*pmax).line > 15)) pos.y++; + for(i=0;i<16;i++) + { + scrolld(); + } + } + } +} + +int main(int argc, char *argv[]) +{ +//Variables + unsigned int i; // Public counter + int inpch; // The char of input + +//Preperation + printf("GHEX Version 0.5\n"); + //Is there a param? + if(argc < 2) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //open the file + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // set the pointers + initvar(); + // init of other useful varibles + pos.x = pos.y = 1; + filename=argv[1]; + //file -> ram + if(loadcontents()) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // init of terminal + if(screeninit()) + { + endscr(); + fprintf(stderr,"!!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + + +//Main-procedure +initout(); +bottomout(); +scrrefresh(); + +//Event-loop + while(inpch!=KEY_F(10)) + { + if (mode==0) + { + inpch=wgetch(whex); + } + else + { + inpch=wgetch(wascii); + } + switch(inpch) + { + // The Keys F1-F8 (bit-setting) + case KEY_F(9): switchmode(); + break; + + case KEY_F(1): swapbits(0); + break; + + case KEY_F(2): swapbits(1); + break; + + case KEY_F(3): swapbits(2); + break; + + case KEY_F(4): swapbits(3); + break; + + case KEY_F(5): swapbits(4); + break; + + case KEY_F(6): swapbits(5); + break; + + case KEY_F(7): swapbits(6); + break; + + case KEY_F(8): swapbits(7); + break; + + //the delete character-key + case KEY_DC : if (mode == 1) + { + delactchar(1,pos.x); + } + else + { + switchmode(); + delactchar(1,pos.x); + switchmode(); + } + break; + + //the insert-key, switch insert mode + case KEY_EIC: //fallthru + case KEY_IC : if (insovr=='O') + { + insovr='I'; + } + else + { + insovr='O'; + } + break; + + //Cursor-movement + case KEY_LEFT: moveleft(); + break; + + case KEY_RIGHT:moveright(); + break; + + case KEY_DOWN: if(pos.y>15) + { + scrolld(); + } + else + { + pos.y++; + } + break; + + case KEY_UP: if(pos.y<2) + { + scrollu(); + } + else + { + pos.y--; + } + break; + + case KEY_NPAGE:for(i=0;i<16;i++) + { + scrolld(); + } + break; + + case KEY_PPAGE:for(i=0;i<16;i++) + { + scrollu(); + } + break; + + case KEY_HOME: initout(); + break; + + case KEY_END: if((*pmax).line > 15) + { + pdatas=pmax; + for(i=0;((*pdatas).plast) && (i < 16);i++) + { + pdatas=(*pdatas).plast; + } + while((*pdatas).pnext) + { + scrolld(); + } + } + break; + // input characters + default: if(mode==0) + { + if(isdigit(inpch) || (toupper(inpch)>='A' && toupper(inpch)<='F') ) + { + waddch(whex,toupper(inpch)); + char hch[]="00"; + // fill hch correct (old values) + int xpos=pos.x; // save position + switchmode(); // recalc. coordinates + ptv=tonumsysx(charout(),16); // actual char in Hex. + //* --- DEBUG ONLY + mvwaddstr(wbot,2,30,tonumsysx(charout(),16)); + // --- END DEBUG + switchmode(); // recalc coordinates + pos.x=xpos; // restore coorinates + if (insovr=='O') // Predef. only in Override! + { + hch[0] = *(ptv+6); + hch[1] = *(ptv+7); + } + else // In Insert only first char + { + if(pos.x%3==0) + hch[0] = *(ptv+6); + } + //* --- DEBUG ONLY + mvwaddstr(wbot,2,1,hch); + mvwaddstr(wbot,2,20,ptv); + // --- END DEBUG + hch[(pos.x%3)?0:1]=inpch; + switchmode(); // recalc coordinates + charin(xto10(hch,16),0,xpos); // calc Hex -> Ascii+charin + switchmode(); // and back + pos.x=xpos; // restore positions + moveright(); + } + } + + else + { + if((inpch < 256)) + { + waddch(wascii,inpch); + charin(inpch,1,pos.x); + moveright(); + } + } + }; + + bottomout(); + scrrefresh(); + + } + + WINDOW* s=derwin(whex,3,17,7,17); + wattrset(s,COLOR_PAIR(1)); + box(s,0,0); + wattrset(s,COLOR_PAIR(3)); + mvwaddstr(s,1,1,"SAVE FILE (Y/N)"); + wrefresh(s); + int saveit= wgetch(s); + endscr(); + unallocmem(saveit,argv[1]); + + return (fclose(fp)); +} diff --git a/tiahv2.cc b/tiahv2.cc new file mode 100644 index 0000000..c125255 --- /dev/null +++ b/tiahv2.cc @@ -0,0 +1,286 @@ +#include +#include +#include + +struct data +{ +struct data* plast; +struct data* pnext; +char vals[16][16]; +}; + +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII"; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +int result; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data daten,*pdaten,*pfirst, *phelp; + +void initvar() +{ + pfirst=&daten; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Zwei Fenster + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// Scrollen erlauben + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// Color-Pairs anlegen (Farbkombinationen) + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(2)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +char* tohex(int base10) +{ + int newbase=16; + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +int loadcontents() +{ + int retval=0; + int x,y; + char ch; + int k=0; + + + pdaten=pfirst; + + ch=fgetc(fp); + + while (!(ch==EOF)) + { + k++; + + for(x=0;x<16;x++) + for(y=0;y<16;y++) + { + ((*pdaten).vals[y][x])=ch; + if ((*pdaten).vals[y][x]==EOF) (*pdaten).vals[y][x]=0; + ch=fgetc(fp); + } + + if(!(ch==EOF)) + { + phelp=pdaten; + printf("Vorher:%p\n",pdaten); + pdaten=((*pdaten).pnext)=(malloc(sizeof(daten))); + printf("Nachher:%p\n" ,pdaten); + (*pdaten).plast=phelp; + (*pdaten).pnext=NULL; + } + + } +/* D E B U G O N L Y +getchar(); + + pdaten=pfirst; + + +while (k--) + { + for(x=0;x<16;x++) + for(y=0;y<16;y++) + { + printf("%c-",(*pdaten).vals[y][x]); + } + pdaten=(*pdaten).pnext; + } +getchar(); + --- END DEBUG ---*/ + + return (retval); +} + +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +void unallocmem() +{ +int zv; +pdaten=pfirst; +for(;(*pdaten).pnext;pdaten=(*pdaten).pnext) + ; +while((*pdaten).plast) +{ +phelp=(*pdaten).plast; +free(pdaten); +pdaten=phelp; +} + + + +} + + +int main(int argc, char *argv[]) +{ +//Variablen + unsigned int i,z; + unsigned long zeile=0,pos=0; + + printf("%s\n",tohex(0)); + printf("%s\n",tohex(10)); + printf("%s\n",tohex(20)); + printf("%s\n",tohex(30)); + printf("%s\n",tohex(40)); + printf("%s\n",tohex(50)); + +//Vorbereitung + //Parameter angegeben? + if(argc < 2) + { + fprintf(stderr,"THX-MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //Datei öffnen + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"THX-MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // pointer setzen + initvar(); + //Datei -> Speicher + if(loadcontents()) + { + fprintf(stderr,"THX-MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // Terminal initialisieren + if(screeninit()) + { + endscr(); + fprintf(stderr,"!!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + mvwaddstr(wbot,1,1,argv[1]); + + +//Hauptprogramm + + for (i=1;i<17;i++) + { + ptv = tohex(i-1); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,i,1,soffs); + for (z=0;z<16;z++) + { + ptv = tohex(daten.vals[z][i-1]); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,i,1,shex); + for (z=0;z<16;z++) + { + xascii[z] = ((daten.vals[z][i-1]) > 33)?(daten.vals[z][i-1]):254; + } + mvwaddstr(wascii,i,1,sascii); + } + + wrefresh(woffs); + wrefresh(whex); + wrefresh(wascii); + wrefresh(wtop); + wrefresh(wbot); + + + sleep(1); + endscr(); + unallocmem(); + return (fclose(fp)); +} diff --git a/tiahv3.cc b/tiahv3.cc new file mode 100644 index 0000000..0534219 --- /dev/null +++ b/tiahv3.cc @@ -0,0 +1,463 @@ +#include +#include +#include + +struct data +{ +struct data* plast; +struct data* pnext; +char vals[16]; +unsigned long zeile; +char len; +}; + +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII"; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +int result; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data daten,*pdaten,*pfirst, *phelp; + +void initvar() +{ + pfirst=&daten; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + (*pfirst).zeile=0; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +void miniinit() +{ +xoffs = " "; +xhex = " "; +xascii = " "; +} + +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Zwei Fenster + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// Scrollen erlauben + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// Tastatureingabe erlauben + keypad(stdscr,1); + keypad(whex,1); + keypad(wascii,1); + +// Color-Pairs anlegen (Farbkombinationen) + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(2)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +char* tohex(int base10) +{ + int newbase=16; + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +int loadcontents() +{ + int retval=0; + int x; + char ch; + int k=0; + unsigned long zeile=0; + + + pdaten=pfirst; + + ch=fgetc(fp); + printf("Printing hashmark for every 8 Kbytes.\n"); + while (!(ch==EOF)) + { + for(x=0;x<16;x++) + { + ((*pdaten).vals[x])=ch; + if ((*pdaten).vals[x]==EOF) + { + (*pdaten).vals[x]=0; + break; + } + (*pdaten).len=x+1; + ch=fgetc(fp); + } + (*pdaten).zeile=zeile++; + if(!(ch==EOF)) + { + phelp=pdaten; + if (!(zeile%500)) {fprintf(stderr,"%c",'#');} + pdaten=((*pdaten).pnext)=(malloc(sizeof(daten))); + (*pdaten).plast=phelp; + (*pdaten).pnext=NULL; + } + + } +/* D E B U G O N L Y +getchar(); + + pdaten=pfirst; + +printf("%d\n",zeile); + +while (zeile--) + { + for(x=0;x<16;x++) + { + printf("%c-",(*pdaten).vals[x]); + } + pdaten=(*pdaten).pnext; + } +getchar(); + --- END DEBUG ---*/ + + return (retval); +} + +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +void unallocmem() +{ +int zv; +pdaten=pfirst; +for(;(*pdaten).pnext;pdaten=(*pdaten).pnext) + ; +while((*pdaten).plast) +{ +phelp=(*pdaten).plast; +free(pdaten); +pdaten=phelp; +} +} + + +void scrrefresh() +{ + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(2)); + wattrset(wbot,COLOR_PAIR(2)); + + wrefresh(woffs); + wrefresh(whex); + wrefresh(wascii); + wrefresh(wtop); + wrefresh(wbot); +} + +void lineout(unsigned long zeile) +{ + int z; + + miniinit(); + + ptv = tohex((*pdaten).zeile); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,zeile,1,soffs); + for (z=0;z<(*pdaten).len;z++) + { + ptv = tohex((*pdaten).vals[z]); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,zeile,1,shex); + for (z=0;z<(*pdaten).len;z++) + { + xascii[z] = (((*pdaten).vals[z]) > 33)?((*pdaten).vals[z]):254; + } + mvwaddstr(wascii,zeile,1,sascii); +} + + +void scrolld() +{ + wscrl(woffs,1); + wscrl(whex,1); + wscrl(wascii,1); + + int z; + + miniinit(); + + ptv = tohex((*pdaten).zeile); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,16,1,soffs); + for (z=0;z<(*pdaten).len;z++) + { + ptv = tohex((*pdaten).vals[z]); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,16,1,shex); + for (z=0;z<(*pdaten).len;z++) + { + xascii[z] = (((*pdaten).vals[z]) > 33)?((*pdaten).vals[z]):254; + } + mvwaddstr(wascii,16,1,sascii); +} + +void scrollu() +{ + wscrl(woffs,-1); + wscrl(whex,-1); + wscrl(wascii,-1); + + miniinit(); + + int z; + ptv = tohex((*pdaten).zeile); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,1,1,soffs); + for (z=0;z<(*pdaten).len;z++) + { + ptv = tohex((*pdaten).vals[z]); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,1,1,shex); + for (z=0;z<(*pdaten).len;z++) + { + xascii[z] = (((*pdaten).vals[z]) > 33)?((*pdaten).vals[z]):254; + } + mvwaddstr(wascii,1,1,sascii); +} + + +int main(int argc, char *argv[]) +{ +//Variablen + unsigned int i,z; + unsigned long zeile=0,pos=0; + bool schalter; + int inpch; + +//Vorbereitung + //Parameter angegeben? + if(argc < 2) + { + fprintf(stderr,"THX-MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //Datei öffnen + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"THX-MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // pointer setzen + initvar(); + //Datei -> Speicher + if(loadcontents()) + { + fprintf(stderr,"THX-MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // Terminal initialisieren + if(screeninit()) + { + endscr(); + fprintf(stderr,"!!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + mvwaddstr(wbot,1,1,argv[1]); + + +//Hauptprogramm + +schalter=false; +zeile=1; +pdaten=pfirst; + +//Initiale Ausgabe (Alle) +for(i=0;i<16;i++) + { + lineout(i+1); + if (schalter) break; + pdaten=(*pdaten).pnext; + if ((*pdaten).pnext == NULL) schalter=true; + } + scrrefresh(); + +if(i>15) pdaten=(*pdaten).plast; + +//Eventschleife + while(inpch!=KEY_END) + { + inpch=wgetch(whex); + switch(inpch) + { + case KEY_DOWN: + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + break; + case KEY_UP: + z=0; + while(((*pdaten).plast != NULL)&&(z < 16)) + { + pdaten=(*pdaten).plast; + z++; + } + if(z>15) + { + z--; + scrollu(); + } + while(z--) + { + if ((*pdaten).pnext != NULL) + { + pdaten=(*pdaten).pnext; + } + } + break; + case KEY_NPAGE: + for(i=0;i<16;i++) + { + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + } + break; + case KEY_PPAGE: + for(i=0;i<16;i++) + { + z=0; + while(((*pdaten).plast != NULL)&&(z < 16)) + { + pdaten=(*pdaten).plast; + z++; + } + if(z>15) + { + z--; + scrollu(); + } + while(z--) + { + if ((*pdaten).pnext != NULL) + { + pdaten=(*pdaten).pnext; + } + } + } + break; + }; + scrrefresh(); + } + sleep(1); + endscr(); + unallocmem(); + return (fclose(fp)); +} diff --git a/tiahv4.cc b/tiahv4.cc new file mode 100644 index 0000000..8c91a47 --- /dev/null +++ b/tiahv4.cc @@ -0,0 +1,450 @@ +#include +#include +#include + +struct data +{ +struct data* plast; +struct data* pnext; +char vals[16]; +unsigned long zeile; +char len; +}; + +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII "; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +int result; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data daten,*pdaten,*pfirst, *phelp; + +void initvar() +{ + pfirst=&daten; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + (*pfirst).zeile=0; + (*pfirst).len=0; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +void miniinit() +{ +xoffs = " "; +xhex = " "; +xascii = " "; +} + +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Zwei Fenster + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// Scrollen erlauben + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// Tastatureingabe erlauben + keypad(stdscr,1); + keypad(whex,1); + keypad(wascii,1); + +// Color-Pairs anlegen (Farbkombinationen) + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(2)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +char* tonumsysx(int base10,int newbase) +{ + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +int loadcontents() +{ + int retval=0; + int x; + char ch; + int k=0; + unsigned long zeile=0; + + + pdaten=pfirst; + + ch=fgetc(fp); + printf("Printing hashmark for every 8 Kbytes.\n"); + while (!(feof(fp))) + { + for(x=0;x<16;x++) + { + ((*pdaten).vals[x])=ch; + if (feof(fp)) //(*pdaten).vals[x]==EOF) + { + (*pdaten).vals[x]=0; + break; + } + (*pdaten).len=x+1; + ch=fgetc(fp); + } + (*pdaten).zeile=zeile++; + if(!(feof(fp))) + { + phelp=pdaten; + if (!(zeile%500)) {fprintf(stderr,"%c",'#');} + pdaten=((*pdaten).pnext)=(malloc(sizeof(daten))); +// BUG: Check for Memory-Full does not work. + if(!pdaten) {abort();} + (*pdaten).plast=phelp; + (*pdaten).pnext=NULL; + } + + } +/* D E B U G O N L Y +getchar(); + + pdaten=pfirst; + +printf("%d\n",zeile); + +while (zeile--) + { + for(x=0;x<16;x++) + { + printf("%c-",(*pdaten).vals[x]); + } + pdaten=(*pdaten).pnext; + } +getchar(); + --- END DEBUG ---*/ + return (retval); +} + +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +void unallocmem() +{ +int zv; +pdaten=pfirst; +for(;(*pdaten).pnext;pdaten=(*pdaten).pnext) + ; +while((*pdaten).plast) +{ +phelp=(*pdaten).plast; +free(pdaten); +pdaten=phelp; +} +} + + +void scrrefresh() +{ + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(2)); + wattrset(wbot,COLOR_PAIR(2)); + + wrefresh(woffs); + wrefresh(whex); + wrefresh(wascii); + wrefresh(wtop); + wrefresh(wbot); +} + +void lineout(unsigned long zeile) +{ + int z; + + miniinit(); + + ptv = tonumsysx((*pdaten).zeile*16,16); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,zeile,1,soffs); + for (z=0;z<(*pdaten).len;z++) + { + ptv = tonumsysx((*pdaten).vals[z],16); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,zeile,1,shex); + for (z=0;z<(*pdaten).len;z++) + { + xascii[z] = (((*pdaten).vals[z]) > 31)?((*pdaten).vals[z]):254; + } + mvwaddstr(wascii,zeile,1,sascii); +} + +void initout() +{ +//Initiale Ausgabe (Alle) +int i; +bool schalter=false; +unsigned long zeile=1; +pdaten=pfirst; + +if ((*pdaten).pnext == NULL) schalter=true; +for(i=0;i<16;i++) + { + lineout(i+1); + if (schalter) break; + pdaten=(*pdaten).pnext; + if ((*pdaten).pnext == NULL) schalter=true; + } + scrrefresh(); + +if(i>15) pdaten=(*pdaten).plast; +} + +void scrolld() +{ + wscrl(woffs,1); + wscrl(whex,1); + wscrl(wascii,1); + + int z; + + miniinit(); + + lineout(16); +} + +void scrollu() +{ + wscrl(woffs,-1); + wscrl(whex,-1); + wscrl(wascii,-1); + + miniinit(); + + lineout(1); +} + + +int main(int argc, char *argv[]) +{ +//Variablen + unsigned int i,z; + unsigned long zeile=0,pos=0; + bool schalter; + int inpch; + + printf("GHEX Version 0.5\n"); + +//Vorbereitung + //Parameter angegeben? + if(argc < 2) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //Datei öffnen + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // pointer setzen + initvar(); + //Datei -> Speicher + if(loadcontents()) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // Terminal initialisieren + if(screeninit()) + { + endscr(); + fprintf(stderr,"!!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + mvwaddstr(wbot,1,1,argv[1]); + + +//Hauptprogramm + +initout(); + +//Eventschleife + while(inpch!=KEY_F(10)) + { + inpch=wgetch(whex); + switch(inpch) + { + case KEY_DOWN: + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + break; + case KEY_UP: + z=0; + while(((*pdaten).plast != NULL)&&(z < 16)) + { + pdaten=(*pdaten).plast; + z++; + } + if(z>15) + { + z--; + scrollu(); + } + while(z--) + { + if ((*pdaten).pnext != NULL) + { + pdaten=(*pdaten).pnext; + } + } + break; + case KEY_NPAGE: + for(i=0;i<16;i++) + { + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + } + break; + case KEY_PPAGE: + for(i=0;i<16;i++) + { + z=0; + while(((*pdaten).plast != NULL)&&(z < 16)) + { + pdaten=(*pdaten).plast; + z++; + } + if(z>15) + { + z--; + scrollu(); + } + while(z--) + { + if ((*pdaten).pnext != NULL) + { + pdaten=(*pdaten).pnext; + } + } + } + break; + case KEY_HOME: + initout(); + break; + case KEY_END: + while((*pdaten).pnext) + { + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + } + break; + + }; + scrrefresh(); + } + sleep(1); + endscr(); + unallocmem(); + return (fclose(fp)); +} diff --git a/tiahv5.cc b/tiahv5.cc new file mode 100644 index 0000000..237a432 --- /dev/null +++ b/tiahv5.cc @@ -0,0 +1,766 @@ +#include +#include +#include +#include +#include + +struct data // Eine Zeile Speicher +{ +struct data* plast; +struct data* pnext; +unsigned char vals[16]; +unsigned long zeile; +char len; +}; + +struct cpos // Cursor-Position +{ + char x,y; +}pos; + +int mode=1; +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII "; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +char empty78[]=" "; +int result; +char *filename; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data daten, *pdaten, *pfirst, *phelp, *pmax; + +void initvar() +{ + pfirst=&daten; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + (*pfirst).zeile=0; + (*pfirst).len=0; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +void miniinit() +{ +xoffs = " "; +xhex = " "; +xascii = " "; +} + +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Zwei Fenster + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// Scrollen erlauben + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// Tastatureingabe erlauben + keypad(stdscr,1); + keypad(whex,1); + keypad(wascii,1); + +// Echo ausschalten + noecho(); + +// Cursor auf Groß setzen +// if(curs_set(2)==ERR) retval=1 ; + +// Color-Pairs anlegen (Farbkombinationen) + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + if(init_pair(3,COLOR_MAGENTA,COLOR_BLACK)) retval=1; + + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +char* tonumsysx(int base10,int newbase) +{ + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +unsigned long xto10(char *c, int oldbase) +{ +unsigned long base10=0; +unsigned long rest,erg; +int pos=0; +int i; + for (i=0;c[i];i++, c[i]=toupper(c[i])); + while(c[pos++]); + pos--; + int len=pos-1; + while(pos--) + { + if(isdigit(c[len-pos])) + { + base10 = base10 + (unsigned long)( (double)(c[len-pos]-'0') * ( pow( (double)oldbase,(double)pos ) ) ) ; + } + else + { + base10 = base10 + (unsigned long)( ((double)(c[len-pos]-'A')+10) * ( pow( (double)oldbase,(double)pos ) ) ); + } + } + return(base10); +} + +int loadcontents() +{ + int retval=0; + int x; + char ch; + int k=0; + unsigned long zeile=0; + + + pdaten=pfirst; + + ch=fgetc(fp); + printf("Printing hashmark for every 8 Kbytes.\n"); + while (!(feof(fp))) + { + for(x=0;x<16;x++) + { + ((*pdaten).vals[x])=ch; + if (feof(fp)) //(*pdaten).vals[x]==EOF) + { + (*pdaten).vals[x]=0; + break; + } + (*pdaten).len=x+1; + ch=fgetc(fp); + } + (*pdaten).zeile=zeile++; + if(!(feof(fp))) + { + phelp=pdaten; + if (!(zeile%500)) {fprintf(stderr,"%c",'#');} + pdaten=((*pdaten).pnext)=(malloc(sizeof(daten))); +// BUG: Check for Memory-Full does not work. + if(!pdaten) {abort();} + (*pdaten).plast=phelp; + (*pdaten).pnext=NULL; + } + pmax=pdaten; + + } +/* D E B U G O N L Y + + pdaten=pfirst; + +printf("%d\n",zeile); + +while (zeile--) + { + for(x=0;x<16;x++) + { + printf("%c-",(*pdaten).vals[x]); + } + pdaten=(*pdaten).pnext; + } +getchar(); + --- END DEBUG ---*/ + return (retval); +} + +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +void unallocmem() +{ +int zv; + +/* DEBUG ONLY + pdaten=pfirst; +int zeile=16; +int x; +printf("%d\n",zeile); + +while (zeile--) + { + for(x=0;x<16;x++) + { + printf("%c-",(*pdaten).vals[x]); + } + pdaten=(*pdaten).pnext; + } +getchar(); + --- END DEBUG ---*/ + +pdaten=pmax; +while((*pdaten).plast) +{ +phelp=(*pdaten).plast; +free(pdaten); +pdaten=phelp; +} +} + +void scrrefresh() +{ + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + !mode?wmove(whex,pos.y,pos.x):wmove(wascii,pos.y,pos.x); + + wrefresh(woffs); + wrefresh(wtop); + wrefresh(wbot); + // die Reihenfolge wird anhand des Modus bestimmt (das letzte ist aktiv) + !mode?wrefresh(wascii):wrefresh(whex); + mode?wrefresh(wascii):wrefresh(whex); +} + +void lineout(unsigned long zeile) +{ + int z; + + miniinit(); + + ptv = tonumsysx((*pdaten).zeile*16,16); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,zeile,1,soffs); + for (z=0;z<(*pdaten).len;z++) + { + ptv = tonumsysx((*pdaten).vals[z],16); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,zeile,1,shex); + for (z=0;z<(*pdaten).len;z++) + { +// BUG: Die Ober- und Untergrenze genau bestimmen. + xascii[z] = (isgraph((*pdaten).vals[z]))?((*pdaten).vals[z]):'.'; + } + mvwaddstr(wascii,zeile,1,sascii); +} + +void initout() +{ +//Initiale Ausgabe (Alle) +int i; +bool schalter=false; +unsigned long zeile=1; +pdaten=pfirst; + +if ((*pdaten).pnext == NULL) schalter=true; +for(i=0;i<16;i++) + { + lineout(i+1); + if (schalter) break; + pdaten=(*pdaten).pnext; + if ((*pdaten).pnext == NULL) schalter=true; + } + scrrefresh(); + +if(i>15) pdaten=(*pdaten).plast; +} + +void scrolld() +{ + wscrl(woffs,1); + wscrl(whex,1); + wscrl(wascii,1); + + int z; + + miniinit(); + + lineout(16); +} + +void scrollu() +{ + wscrl(woffs,-1); + wscrl(whex,-1); + wscrl(wascii,-1); + + miniinit(); + + lineout(1); +} + +void switchmode() +{ + if(mode==0) + { + mode=1; // modus:Hex->Ascii + // Umrechnen der Cursorposition + pos.x=((pos.x+1)/3); + } + else + { + mode=0; // modue:Ascii->Hex + // Umrechnen der Cursorposition + pos.x=((pos.x*3)-1); + } +} + +void moveleft() +{ + if(mode==0) + { + if(pos.x > 2)(pos.x%3)?pos.x-=2:pos.x--; + } + else + { + if(pos.x > 1)pos.x--; + } +} + +void moveright() +{ + if(mode==0) + { + if(pos.x < 48)(pos.x%3)?pos.x++:pos.x+=2; + } + else + { + if(pos.x < 16)pos.x++; + } +} + +unsigned long getactpos() +{ + int i=0; + unsigned long actpos; + int posxsave=9999; + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // im Speicher zur Cusrorposition blättern + while(i<(16-pos.y)&&((*pdaten).plast)) + { + i++; + pdaten=(*pdaten).plast; + } + // Position holen + actpos=((*pdaten).zeile*16)+pos.x-1; + // wieder zurückblättern + while(i--) + { + pdaten=(*pdaten).pnext; + } + if (posxsave!=9999) + { + switchmode(); + pos.x=posxsave; + } + + return(actpos); +} + +void charin(char ch) +{ + int i=0; + // im Speicher zur Cusrorposition blättern + while(i<(16-pos.y)&&((*pdaten).plast)) + { + i++; + pdaten=(*pdaten).plast; + } + // Wert schreiben + (*pdaten).vals[pos.x-1]=ch; + // Ausgabe aktualisieren + lineout(pos.y); + // wieder zurückblättern + while(i--) + { + pdaten=(*pdaten).pnext; + } +} + +int charout() +{ + int i=0; + int ch; + // im Speicher zur Cusrorposition blättern + while(i<(16-pos.y)&&((*pdaten).plast)) + { + i++; + pdaten=(*pdaten).plast; + } + // Wert lesen + ch=((*pdaten).vals[pos.x-1]); + // wieder zurückblättern + while(i--) + { + pdaten=(*pdaten).pnext; + } + return(ch); +} + +void bottomout() +{ + mvwaddstr(wbot,1,1,empty78); + mvwaddstr(wbot,2,1,empty78); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,1,"CHR:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddch(wbot,1,6,32); + mvwaddch(wbot,1,5,(isgraph(charout()))?charout():'.'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,9,"HEX:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,13,(tonumsysx(charout(),16))+6); + mvwaddch(wbot,1,15,'h'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,18,"DEC:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,22,(tonumsysx(charout(),10))+5); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,27,"OCT:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,31,(tonumsysx(charout(),8))+4); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,37,"BIN:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,41,tonumsysx(charout(),2)); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,52,"Offset:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,59,tonumsysx(getactpos(),16)); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,67,"h -"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,71,tonumsysx(getactpos(),10)); + + mvwaddstr(wbot,2,1,filename); + +} + + +int main(int argc, char *argv[]) +{ +/* --- DEBUG ONLY +printf("DEBUG: 5-%u\n",xto10(tonumsysx(5,2),2)); +printf("DEBUG: 16-%u\n",xto10(tonumsysx(16,16),16)); +printf("DEBUG: 36-%u\n",xto10 (tonumsysx(36,36),36)); +printf("DEBUG: 255-%u\n",xto10(tonumsysx(255,16),16)); +printf("DEBUG: 255-%u\n",xto10("FF",16)); +printf("DEBUG: 125-%u\n",xto10(tonumsysx(125,8),8)); +printf("DEBUG: 255-%u\n",xto10(tonumsysx(255,8),8)); +getchar(); + --- END DEBUG */ +//Variablen + unsigned int i,z; // Allgemeine Zähler + unsigned long zeile=0; // Zeilennummer + bool schalter; // Schalter für lesevorlauf + int inpch; // Eingabezeichen + +//Vorbereitung + printf("GHEX Version 0.5\n"); + //Parameter angegeben? + if(argc < 2) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //Datei öffnen + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // pointer setzen + initvar(); + // sonstige Variableninitialisierung + pos.x = pos.y = 1; + filename=argv[1]; + //Datei -> Speicher + if(loadcontents()) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // Terminal initialisieren + if(screeninit()) + { + endscr(); + fprintf(stderr,"!!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + + +//Hauptprogramm + +initout(); +bottomout(); +scrrefresh(); +/* --- DEBUG ONLY + fprintf(stdout,"D%d\n",charout()); + pos.x++; + fprintf(stdout,"D%d\n",charout()); + pos.x++; + fprintf(stdout,"D%d\n",charout()); + pos.x++; + fprintf(stdout,"D%d\n",charout()); + pos.x++; + fprintf(stdout,"D%d\n",charout()); + + getchar(); + -- END DEBUG */ + +//Eventschleife + while(inpch!=KEY_F(10)) + { + if (mode==0) + { + inpch=wgetch(whex); + } + else + { + inpch=wgetch(wascii); + } + switch(inpch) + { + case KEY_F(9): switchmode(); + break; + case KEY_LEFT: moveleft(); + break; + case KEY_RIGHT:moveright(); + break; + case KEY_DOWN: if(pos.y>15) + { + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + } + else + { + pos.y++; + } + break; + case KEY_UP: if(pos.y<2) + { + z=0; + while(((*pdaten).plast != NULL)&&(z < 16)) + { + pdaten=(*pdaten).plast; + z++; + } + if(z>15) + { + z--; + scrollu(); + } + while(z--) + { + if ((*pdaten).pnext != NULL) + { + pdaten=(*pdaten).pnext; + } + } + } + else + { + pos.y--; + } + break; + case KEY_NPAGE: + for(i=0;i<16;i++) + { + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + } + break; + case KEY_PPAGE: + for(i=0;i<16;i++) + { + z=0; + while(((*pdaten).plast != NULL)&&(z < 16)) + { + pdaten=(*pdaten).plast; + z++; + } + if(z>15) + { + z--; + scrollu(); + } + while(z--) + { + if ((*pdaten).pnext != NULL) + { + pdaten=(*pdaten).pnext; + } + } + } + break; + case KEY_HOME: + initout(); + break; + case KEY_END: pdaten=pmax; + for(i=0;((*pdaten).plast) && (i < 16);i++) + { + pdaten=(*pdaten).plast; + } + while((*pdaten).pnext) + { + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + } + break; + default: + if(mode==0) + { + if(isdigit(inpch) || (toupper(inpch)>='A' && toupper(inpch)<='F') ) + { + waddch(whex,toupper(inpch)); + char hch[]="FF"; + // hch korrekt füllen. + int xpos=pos.x; // Position sichern + switchmode(); // um die Koordinaten umzurechnen + ptv=tonumsysx(charout(),16); // Aktuelles Zeichen in Hex. + //* --- DEBUG ONLY + mvwaddstr(wbot,2,30,tonumsysx(charout(),16)); + // --- END DEBUG + switchmode(); // Koordinaten zurückrechnen + pos.x=xpos; // position restaurieren + hch[0] = *(ptv+6); + hch[1] = *(ptv+7); + //* --- DEBUG ONLY + mvwaddstr(wbot,2,1,hch); + mvwaddstr(wbot,2,20,ptv); + // --- END DEBUG + hch[(pos.x%3)?0:1]=inpch; + switchmode(); // um die Koordinaten umzurechnen + charin(xto10(hch,16)); // Umrechnen Hex -> Ascii+charin + switchmode(); // und wieder zurücksetzen + pos.x=xpos; // position restaurieren + moveright(); + } + + else + { + //if((inpch > 31)&&(inpch < 256)) + if((inpch < 256)) + { + waddch(wascii,inpch); + charin(inpch); + moveright(); + } + } + } + }; + + bottomout(); + scrrefresh(); + +// fprintf(stderr,"x: %d %d",pos.x,charout()); + + } + sleep(1); + endscr(); + unallocmem(); + return (fclose(fp)); +} diff --git a/tiahv6.cc b/tiahv6.cc new file mode 100644 index 0000000..779f1c0 --- /dev/null +++ b/tiahv6.cc @@ -0,0 +1,811 @@ +#include +#include +#include +#include +#include + +struct data // Eine Zeile Speicher +{ +struct data* plast; +struct data* pnext; +unsigned char vals[16]; +unsigned long zeile; +char len; +}; + +struct cpos // Cursor-Position +{ + char x,y; +}pos; + +char insovr='O'; // Isert/Override-Mode +int mode=1; +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII "; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +char empty78[]=" "; +int result; +char *filename; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data daten, *pdaten, *pfirst, *phelp, *pmax; + +void initvar() +{ + pfirst=&daten; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + (*pfirst).zeile=0; + (*pfirst).len=0; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +void miniinit() +{ +xoffs = " "; +xhex = " "; +xascii = " "; +} + +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Zwei Fenster + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// Scrollen erlauben + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// Tastatureingabe erlauben + keypad(stdscr,1); + keypad(whex,1); + keypad(wascii,1); + +// Echo ausschalten + noecho(); + +// Cursor auf Groß setzen +// if(curs_set(2)==ERR) retval=1 ; + +// Color-Pairs anlegen (Farbkombinationen) + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + if(init_pair(3,COLOR_MAGENTA,COLOR_BLACK)) retval=1; + + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +char* tonumsysx(int base10,int newbase) +{ + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +unsigned long xto10(char *c, int oldbase) +{ +unsigned long base10=0; +unsigned long rest,erg; +int pos=0; +int i; + for (i=0;c[i];i++, c[i]=toupper(c[i])); + while(c[pos++]); + pos--; + int len=pos-1; + while(pos--) + { + if(isdigit(c[len-pos])) + { + base10 = base10 + (unsigned long)( (double)(c[len-pos]-'0') * ( pow( (double)oldbase,(double)pos ) ) ) ; + } + else + { + base10 = base10 + (unsigned long)( ((double)(c[len-pos]-'A')+10) * ( pow( (double)oldbase,(double)pos ) ) ); + } + } + return(base10); +} + +int loadcontents() +{ + int retval=0; + int x; + char ch; + int k=0; + unsigned long zeile=0; + + + pdaten=pfirst; + + ch=fgetc(fp); + printf("Printing hashmark for every 8 Kbytes.\n"); + while (!(feof(fp))) + { + for(x=0;x<16;x++) + { + ((*pdaten).vals[x])=ch; + if (feof(fp)) //(*pdaten).vals[x]==EOF) + { + (*pdaten).vals[x]=0; + break; + } + (*pdaten).len=x+1; + ch=fgetc(fp); + } + (*pdaten).zeile=zeile++; + if(!(feof(fp))) + { + phelp=pdaten; + if (!(zeile%500)) {fprintf(stderr,"%c",'#');} + pdaten=((*pdaten).pnext)=(malloc(sizeof(daten))); +// BUG: Check for Memory-Full does not work. + if(!pdaten) {abort();} + (*pdaten).plast=phelp; + (*pdaten).pnext=NULL; + } + pmax=pdaten; + + } +/* D E B U G O N L Y + + pdaten=pfirst; + +printf("%d\n",zeile); + +while (zeile--) + { + for(x=0;x<16;x++) + { + printf("%c-",(*pdaten).vals[x]); + } + pdaten=(*pdaten).pnext; + } +getchar(); + --- END DEBUG ---*/ + return (retval); +} + +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +void unallocmem() +{ +int zv; + +/* DEBUG ONLY + pdaten=pfirst; +int zeile=16; +int x; +printf("%d\n",zeile); + +while (zeile--) + { + for(x=0;x<16;x++) + { + printf("%c-",(*pdaten).vals[x]); + } + pdaten=(*pdaten).pnext; + } +getchar(); + --- END DEBUG ---*/ + +pdaten=pmax; +while((*pdaten).plast) +{ +phelp=(*pdaten).plast; +free(pdaten); +pdaten=phelp; +} +} + +void scrrefresh() +{ + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + !mode?wmove(whex,pos.y,pos.x):wmove(wascii,pos.y,pos.x); + + wrefresh(woffs); + wrefresh(wtop); + wrefresh(wbot); + // die Reihenfolge wird anhand des Modus bestimmt (das letzte ist aktiv) + !mode?wrefresh(wascii):wrefresh(whex); + mode?wrefresh(wascii):wrefresh(whex); +} + +void lineout(unsigned long zeile) +{ + int z; + + miniinit(); + + ptv = tonumsysx((*pdaten).zeile*16,16); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,zeile,1,soffs); + for (z=0;z<(*pdaten).len;z++) + { + ptv = tonumsysx((*pdaten).vals[z],16); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,zeile,1,shex); + for (z=0;z<(*pdaten).len;z++) + { +// BUG: Die Ober- und Untergrenze genau bestimmen. + xascii[z] = (isgraph((*pdaten).vals[z]))?((*pdaten).vals[z]):'.'; + } + mvwaddstr(wascii,zeile,1,sascii); +} + +void initout() +{ +//Initiale Ausgabe (Alle) +int i; +bool schalter=false; +unsigned long zeile=1; +pdaten=pfirst; + +if ((*pdaten).pnext == NULL) schalter=true; +for(i=0;i<16;i++) + { + lineout(i+1); + if (schalter) break; + pdaten=(*pdaten).pnext; + if ((*pdaten).pnext == NULL) schalter=true; + } + scrrefresh(); + +if(i>15) pdaten=(*pdaten).plast; +} + +void scrolld() +{ + wscrl(woffs,1); + wscrl(whex,1); + wscrl(wascii,1); + + int z; + + miniinit(); + + lineout(16); +} + +void scrollu() +{ + wscrl(woffs,-1); + wscrl(whex,-1); + wscrl(wascii,-1); + + miniinit(); + + lineout(1); +} + +void switchmode() +{ + if(mode==0) + { + mode=1; // modus:Hex->Ascii + // Umrechnen der Cursorposition + pos.x=((pos.x+1)/3); + } + else + { + mode=0; // modue:Ascii->Hex + // Umrechnen der Cursorposition + pos.x=((pos.x*3)-1); + } +} + +void moveleft() +{ + if(mode==0) + { + if(pos.x > 2)(pos.x%3)?pos.x-=2:pos.x--; + } + else + { + if(pos.x > 1)pos.x--; + } +} + +void moveright() +{ + if(mode==0) + { + if(pos.x < 48)(pos.x%3)?pos.x++:pos.x+=2; + } + else + { + if(pos.x < 16)pos.x++; + } +} + +unsigned long getactpos() +{ + int i=0; + unsigned long actpos; + int posxsave=9999; + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // im Speicher zur Cusrorposition blättern + while(i<(16-pos.y)&&((*pdaten).plast)) + { + i++; + pdaten=(*pdaten).plast; + } + // Position holen + actpos=((*pdaten).zeile*16)+pos.x-1; + // wieder zurückblättern + while(i--) + { + pdaten=(*pdaten).pnext; + } + if (posxsave!=9999) + { + switchmode(); + pos.x=posxsave; + } + + return(actpos); +} + +void charin(char ch) +{ + int i=0; + int z=0; + int sicchar=9999; + // Das eigentliche Enfügen des Zeichens + // im Speicher zur Cusrorposition blättern + while(i<(16-pos.y)&&((*pdaten).plast)) + { + i++; + pdaten=(*pdaten).plast; + } + if (insovr=='I') // In Override-Mode + { + phelp=pdaten; + pdaten=pmax; + if ((*pdaten).len < 16) + { + (*pdaten).len++; + } + else + { + // allocate new element + } + while(pdaten!=phelp) + { + for(z=(*pdaten).len;z>0;z--) + { + (*pdaten).vals[z]=(*pdaten).vals[z-1]; + } + (*pdaten).vals[0]=(*(*pdaten).plast).vals[15]; + pdaten=(*pdaten).plast; + } + + for(z=(*pdaten).len;z+1>pos.x;z--) + { + (*pdaten).vals[z]=(*pdaten).vals[z-1]; + } + pdaten=phelp; + } + // Wert schreiben + (*pdaten).vals[pos.x-1]=ch; + // Ausgabe aktualisieren + lineout(pos.y); + // wieder zurückblättern + while(i--) + { + pdaten=(*pdaten).pnext; + } +} + +int charout() +{ + int i=0; + int ch; + // im Speicher zur Cusrorposition blättern + while(i<(16-pos.y)&&((*pdaten).plast)) + { + i++; + pdaten=(*pdaten).plast; + } + // Wert lesen + ch=((*pdaten).vals[pos.x-1]); + // wieder zurückblättern + while(i--) + { + pdaten=(*pdaten).pnext; + } + return(ch); +} + +void bottomout() +{ + mvwaddstr(wbot,1,1,empty78); + mvwaddstr(wbot,2,1,empty78); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,1,"CHR:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddch(wbot,1,6,32); + mvwaddch(wbot,1,5,(isgraph(charout()))?charout():'.'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,9,"HEX:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,13,(tonumsysx(charout(),16))+6); + mvwaddch(wbot,1,15,'h'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,18,"DEC:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,22,(tonumsysx(charout(),10))+5); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,27,"OCT:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,31,(tonumsysx(charout(),8))+4); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,37,"BIN:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,41,tonumsysx(charout(),2)); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,52,"Offset:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,59,tonumsysx(getactpos(),16)); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,67,"h -"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,71,tonumsysx(getactpos(),10)); + + mvwaddstr(wbot,2,1,filename); + mvwaddstr(wbot,2,76,(insovr=='O')?"OVR":"INS"); +} + + +int main(int argc, char *argv[]) +{ +/* --- DEBUG ONLY +printf("DEBUG: 5-%u\n",xto10(tonumsysx(5,2),2)); +printf("DEBUG: 16-%u\n",xto10(tonumsysx(16,16),16)); +printf("DEBUG: 36-%u\n",xto10 (tonumsysx(36,36),36)); +printf("DEBUG: 255-%u\n",xto10(tonumsysx(255,16),16)); +printf("DEBUG: 255-%u\n",xto10("FF",16)); +printf("DEBUG: 125-%u\n",xto10(tonumsysx(125,8),8)); +printf("DEBUG: 255-%u\n",xto10(tonumsysx(255,8),8)); +getchar(); + --- END DEBUG */ +//Variablen + unsigned int i,z; // Allgemeine Zähler + unsigned long zeile=0; // Zeilennummer + bool schalter; // Schalter für lesevorlauf + int inpch; // Eingabezeichen + +//Vorbereitung + printf("GHEX Version 0.5\n"); + //Parameter angegeben? + if(argc < 2) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //Datei öffnen + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // pointer setzen + initvar(); + // sonstige Variableninitialisierung + pos.x = pos.y = 1; + filename=argv[1]; + //Datei -> Speicher + if(loadcontents()) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // Terminal initialisieren + if(screeninit()) + { + endscr(); + fprintf(stderr,"!!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + + +//Hauptprogramm + +initout(); +bottomout(); +scrrefresh(); +/* --- DEBUG ONLY + fprintf(stdout,"D%d\n",charout()); + pos.x++; + fprintf(stdout,"D%d\n",charout()); + pos.x++; + fprintf(stdout,"D%d\n",charout()); + pos.x++; + fprintf(stdout,"D%d\n",charout()); + pos.x++; + fprintf(stdout,"D%d\n",charout()); + + getchar(); + -- END DEBUG */ + +//Eventschleife + while(inpch!=KEY_F(10)) + { + if (mode==0) + { + inpch=wgetch(whex); + } + else + { + inpch=wgetch(wascii); + } + switch(inpch) + { + case KEY_F(9): switchmode(); + break; + + case KEY_F(1): initout(); + break; + case KEY_EIC: + //fallthru + case KEY_IC : if (insovr=='O') + { + insovr='I'; + } + else + { + insovr='O'; + } + break; + case KEY_LEFT: moveleft(); + break; + case KEY_RIGHT:moveright(); + break; + case KEY_DOWN: if(pos.y>15) + { + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + } + else + { + pos.y++; + } + break; + case KEY_UP: if(pos.y<2) + { + z=0; + while(((*pdaten).plast != NULL)&&(z < 16)) + { + pdaten=(*pdaten).plast; + z++; + } + if(z>15) + { + z--; + scrollu(); + } + while(z--) + { + if ((*pdaten).pnext != NULL) + { + pdaten=(*pdaten).pnext; + } + } + } + else + { + pos.y--; + } + break; + case KEY_NPAGE: + for(i=0;i<16;i++) + { + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + } + break; + case KEY_PPAGE: + for(i=0;i<16;i++) + { + z=0; + while(((*pdaten).plast != NULL)&&(z < 16)) + { + pdaten=(*pdaten).plast; + z++; + } + if(z>15) + { + z--; + scrollu(); + } + while(z--) + { + if ((*pdaten).pnext != NULL) + { + pdaten=(*pdaten).pnext; + } + } + } + break; + case KEY_HOME: + initout(); + break; + case KEY_END: pdaten=pmax; + for(i=0;((*pdaten).plast) && (i < 16);i++) + { + pdaten=(*pdaten).plast; + } + while((*pdaten).pnext) + { + if ((*pdaten).pnext != NULL) + { pdaten=(*pdaten).pnext; + scrolld(); + } + } + break; + default: + if(mode==0) + { + if(isdigit(inpch) || (toupper(inpch)>='A' && toupper(inpch)<='F') ) + { + waddch(whex,toupper(inpch)); + char hch[]="FF"; + // hch korrekt füllen. + int xpos=pos.x; // Position sichern + switchmode(); // um die Koordinaten umzurechnen + ptv=tonumsysx(charout(),16); // Aktuelles Zeichen in Hex. + //* --- DEBUG ONLY + mvwaddstr(wbot,2,30,tonumsysx(charout(),16)); + // --- END DEBUG + switchmode(); // Koordinaten zurückrechnen + pos.x=xpos; // position restaurieren + hch[0] = *(ptv+6); + hch[1] = *(ptv+7); + //* --- DEBUG ONLY + mvwaddstr(wbot,2,1,hch); + mvwaddstr(wbot,2,20,ptv); + // --- END DEBUG + hch[(pos.x%3)?0:1]=inpch; + switchmode(); // um die Koordinaten umzurechnen + charin(xto10(hch,16)); // Umrechnen Hex -> Ascii+charin + switchmode(); // und wieder zurücksetzen + pos.x=xpos; // position restaurieren + moveright(); + } + } + + else + { + if((inpch < 256)) + { + waddch(wascii,inpch); + charin(inpch); + moveright(); + } + } + }; + + bottomout(); + scrrefresh(); + +// fprintf(stderr,"x: %d %d",pos.x,charout()); + + } + sleep(1); + endscr(); + unallocmem(); + return (fclose(fp)); +} diff --git a/tiahv7.cc b/tiahv7.cc new file mode 100644 index 0000000..2ee807b --- /dev/null +++ b/tiahv7.cc @@ -0,0 +1,927 @@ +#include +#include +#include +#include +#include + +struct data // Eine Zeile Speicher +{ +struct data* plast; +struct data* pnext; +unsigned char vals[16]; +unsigned long zeile; +char len; +}; + +struct cpos // Cursor-Position +{ + char x,y; +}pos; + +char insovr='O'; // Isert/Override-Mode +int mode=1; +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII "; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +char empty78[]=" "; +int result; +char *filename; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data daten, *pdaten, *pfirst, *phelp, *pmax; + +void initvar() +{ + pfirst=&daten; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + (*pfirst).zeile=0; + (*pfirst).len=0; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +void miniinit() +{ +xoffs = " "; +xhex = " "; +xascii = " "; +} + +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Zwei Fenster + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// Scrollen erlauben + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// Tastatureingabe erlauben + keypad(stdscr,1); + keypad(whex,1); + keypad(wascii,1); + +// Echo ausschalten + noecho(); + +// Cursor auf Groß setzen +// if(curs_set(2)==ERR) retval=1 ; + +// Color-Pairs anlegen (Farbkombinationen) + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + if(init_pair(3,COLOR_MAGENTA,COLOR_BLACK)) retval=1; + + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +char* tonumsysx(int base10,int newbase) +{ + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +unsigned long xto10(char *c, int oldbase) +{ +unsigned long base10=0; +unsigned long rest,erg; +int pos=0; +int i; + for (i=0;c[i];i++, c[i]=toupper(c[i])); + while(c[pos++]); + pos--; + int len=pos-1; + while(pos--) + { + if(isdigit(c[len-pos])) + { + base10 = base10 + (unsigned long)( (double)(c[len-pos]-'0') * ( pow( (double)oldbase,(double)pos ) ) ) ; + } + else + { + base10 = base10 + (unsigned long)( ((double)(c[len-pos]-'A')+10) * ( pow( (double)oldbase,(double)pos ) ) ); + } + } + return(base10); +} + +int loadcontents() +{ + int retval=0; + int x; + char ch; + int k=0; + unsigned long zeile=0; + + + pdaten=pfirst; + ch=fgetc(fp); + // Zero-byte-file. + if(feof(fp)) + { + fprintf(stderr,"opening 0-Byte file"); + pfirst=pmax=pdaten=(data*)(malloc(sizeof(daten))); + (*pdaten).pnext=NULL; + (*pdaten).plast=NULL; + (*pdaten).zeile=0; + (*pdaten).len=0; + + } + printf("Printing hashmark for every 8 Kbytes.\n"); + while (!(feof(fp))) + { + for(x=0;x<16;x++) + { + ((*pdaten).vals[x])=ch; + if (feof(fp)) //(*pdaten).vals[x]==EOF) + { + (*pdaten).vals[x]=0; + break; + } + (*pdaten).len=x+1; + ch=fgetc(fp); + } + (*pdaten).zeile=zeile++; + if(!(feof(fp))) + { + phelp=pdaten; + if (!(zeile%500)) {fprintf(stderr,"%c",'#');} + pdaten=((*pdaten).pnext)=(data*)(malloc(sizeof(daten))); +// BUG: Check for Memory-Full does not work. + if(!pdaten) {abort();} + (*pdaten).plast=phelp; + (*pdaten).pnext=NULL; + } + pmax=pdaten; + + } +/* D E B U G O N L Y + + pdaten=pfirst; + +printf("%d\n",zeile); + +while (zeile--) + { + for(x=0;x<16;x++) + { + printf("%c-",(*pdaten).vals[x]); + } + pdaten=(*pdaten).pnext; + } +getchar(); + --- END DEBUG ---*/ + return (retval); +} + +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +void unallocmem() +{ +int zv; + +/* DEBUG ONLY + pdaten=pfirst; +int zeile=16; +int x; +printf("%d\n",zeile); + +while (zeile--) + { + for(x=0;x<16;x++) + { + printf("%c-",(*pdaten).vals[x]); + } + pdaten=(*pdaten).pnext; + } +getchar(); + --- END DEBUG ---*/ + +pdaten=pmax; +while((*pdaten).plast) +{ +phelp=(*pdaten).plast; +free(pdaten); +pdaten=phelp; +} +} + +void scrrefresh() +{ + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + !mode?wmove(whex,pos.y,pos.x):wmove(wascii,pos.y,pos.x); + + wrefresh(woffs); + wrefresh(wtop); + wrefresh(wbot); + // die Reihenfolge wird anhand des Modus bestimmt (das letzte ist aktiv) + !mode?wrefresh(wascii):wrefresh(whex); + mode?wrefresh(wascii):wrefresh(whex); +} + +void lineout(unsigned long zeile) +{ + int z; + + miniinit(); + + ptv = tonumsysx((*pdaten).zeile*16,16); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,zeile,1,soffs); + for (z=0;z<(*pdaten).len;z++) + { + ptv = tonumsysx((*pdaten).vals[z],16); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,zeile,1,shex); + for (z=0;z<(*pdaten).len;z++) + { +// BUG: Die Ober- und Untergrenze genau bestimmen. + xascii[z] = (isgraph((*pdaten).vals[z]))?((*pdaten).vals[z]):'.'; + } + mvwaddstr(wascii,zeile,1,sascii); +} + +void initout() +{ +//Initiale Ausgabe (Alle) +int i; +bool schalter=false; +unsigned long zeile=1; +pdaten=pfirst; + +if ((*pdaten).pnext == NULL) schalter=true; +for(i=0;i<16;i++) + { + lineout(i+1); + if (schalter) break; + pdaten=(*pdaten).pnext; + if ((*pdaten).pnext == NULL) schalter=true; + } + scrrefresh(); + +if(i>15) pdaten=(*pdaten).plast; +} + +int scrolld() +{ + int resval; + if ((*pdaten).pnext != NULL) + { + pdaten=(*pdaten).pnext; + wscrl(woffs,1); + wscrl(whex,1); + wscrl(wascii,1); + + miniinit(); + + lineout(16); + resval=1; + } + else + { + resval=0; + } + return(resval); +} + +int scrollu() +{ + int z=0; + int result; + while(((*pdaten).plast != NULL)&&(z < 16)) + { + pdaten=(*pdaten).plast; + z++; + } + result= (z!=15)?1:0; + if(z>15) + { + z--; + wscrl(woffs,-1); + wscrl(whex,-1); + wscrl(wascii,-1); + + miniinit(); + + lineout(1); + } + while(z--) + { + if ((*pdaten).pnext != NULL) + { + pdaten=(*pdaten).pnext; + } + } + return(result); +} + +void switchmode() +{ + if(mode==0) + { + mode=1; // modus:Hex->Ascii + // Umrechnen der Cursorposition + pos.x=((pos.x+1)/3); + } + else + { + mode=0; // modus:Ascii->Hex + // Umrechnen der Cursorposition + pos.x=((pos.x*3)-1); + } +} + +void moveleft() +{ + if(mode==0) + { + if(pos.x > 2) + { + (pos.x%3)?pos.x-=2:pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=48; + } + else + { + if(scrollu())pos.x=48; + } + } + } + else + { + if(pos.x > 1) + { + pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=16; + } + else + { + if(scrollu())pos.x=16; + } + } + } +} + +void moveright() +{ + if(mode==0) + { + if(pos.x < 48) + { + (pos.x%3)?pos.x++:pos.x+=2; + } + else + { + if(pos.y < 16) + { + pos.x=2; + pos.y++; + } + else + { + if(scrolld()) pos.x=2; + } + } + } + else + { + if((pos.x < 16)) + { + pos.x++; + } + else + { + if(pos.y < 16) + { + pos.x=1; + pos.y++; + } + else + { + if(scrolld()) pos.x=1; + } + } + } +} + +unsigned long getactpos() +{ + int i=0; + unsigned long actpos; + int posxsave=9999; + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // im Speicher zur Cusrorposition blättern + while(i<(16-pos.y)&&((*pdaten).plast)) + { + i++; + pdaten=(*pdaten).plast; + } + // Position holen + actpos=((*pdaten).zeile*16)+pos.x-1; + // wieder zurückblättern + while(i--) + { + pdaten=(*pdaten).pnext; + } + if (posxsave!=9999) + { + switchmode(); + pos.x=posxsave; + } + + return(actpos); +} + +void charin(char ch, int realmode, int realxpos) +{ + int i=0; + int z=0; + int sicchar=9999; + data *phelp2; + unsigned long zhelp; + // im Speicher zur Cusrorposition blättern + while(i<(16-pos.y)&&((*pdaten).plast)) + { + i++; + pdaten=(*pdaten).plast; + } + fprintf(stderr,"%d Pos. zurück\n",i); + // Für den Einfügemodus + if (((insovr=='I') && (realmode==1))||((insovr=='I') && (realmode==0) && (realxpos%3==2)) && (realmode!=3)) + { + phelp=pdaten; + pdaten=pmax; + if ((*pdaten).len < 16) + { + (*pdaten).len++; + } + else + { + // allocate new element + phelp2=pmax; + zhelp=(*pmax).zeile+1; + pdaten=pmax=((*pdaten).pnext)=(data*)(malloc(sizeof(daten))); + if(!pmax) {abort();} + (*pmax).plast=phelp2; + (*pmax).pnext=NULL; + (*pmax).len=1; + (*pmax).zeile=zhelp; + } + while(pdaten!=phelp) + { + for(z=(*pdaten).len-1;z>0;z--) + { + (*pdaten).vals[z]=(*pdaten).vals[z-1]; + } + (*pdaten).vals[0]=(*(*pdaten).plast).vals[15]; + pdaten=(*pdaten).plast; + } + + for(z=(*pdaten).len-1;z+1>pos.x;z--) + { + (*pdaten).vals[z]=(*pdaten).vals[z-1]; + } + pdaten=phelp; + } + // Wert schreiben + (*pdaten).vals[pos.x-1]=ch; + // Ausgabe aktualisieren + lineout(pos.y); + // wieder zurückblättern + while(i--) + { + if((*pdaten).pnext) + { + pdaten=(*pdaten).pnext; + lineout(16-i); + } + } + +} + +int charout() +{ + int i=0; + int ch; + // im Speicher zur Cusrorposition blättern + while(i<(16-pos.y)&&((*pdaten).plast)) + { + i++; + pdaten=(*pdaten).plast; + } + // Wert lesen + ch=((*pdaten).vals[pos.x-1]); + // wieder zurückblättern + while(i--) + { + //fprintf(stderr,"l"); + pdaten=(*pdaten).pnext; + } + return(ch); +} + +void bottomout() +{ + int realmode,realposx; + realmode=mode; + + if (realmode == 0) + { + realposx=pos.x; + switchmode(); + } + mvwaddstr(wbot,1,1,empty78); + mvwaddstr(wbot,2,1,empty78); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,1,"CHR:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddch(wbot,1,6,32); + mvwaddch(wbot,1,5,(isgraph(charout()))?charout():'.'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,9,"HEX:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,13,(tonumsysx(charout(),16))+6); + mvwaddch(wbot,1,15,'h'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,18,"DEC:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,22,(tonumsysx(charout(),10))+5); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,27,"OCT:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,31,(tonumsysx(charout(),8))+4); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,37,"BIN:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,41,tonumsysx(charout(),2)); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,52,"Offset:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,59,tonumsysx(getactpos(),16)); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,67,"h -"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,71,tonumsysx(getactpos(),10)); + + mvwaddstr(wbot,2,1,filename); + mvwaddstr(wbot,2,76,(insovr=='O')?"OVR":"INS"); + if (realmode == 0) + { + switchmode(); + pos.x=realposx; + } +} + +void swapbits(char bit) +{ + char bin[]="12345678",*pbin; // Um Bits abzubilden + pbin=bin; + int realposx, realmode; + realmode=mode; + if (realmode==0) + { + realposx=pos.x; + switchmode(); + } + pbin=tonumsysx(charout(),2); + pbin[bit]= (pbin[bit]=='0')?'1':'0'; + charin(xto10(pbin,2),3,pos.x); + if (realmode==0) + { + switchmode(); + pos.x=realposx; + } +} + +int main(int argc, char *argv[]) +{ +//Variablen + unsigned int i,z; // Allgemeine Zähler + unsigned long zeile=0; // Zeilennummer + bool schalter; // Schalter für lesevorlauf + int inpch; // Eingabezeichen + +//Vorbereitung + printf("GHEX Version 0.5\n"); + //Parameter angegeben? + if(argc < 2) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //Datei öffnen + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // pointer setzen + initvar(); + // sonstige Variableninitialisierung + pos.x = pos.y = 1; + filename=argv[1]; + //Datei -> Speicher + if(loadcontents()) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // Terminal initialisieren + if(screeninit()) + { + endscr(); + fprintf(stderr,"!!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + + +//Hauptprogramm +initout(); +bottomout(); +scrrefresh(); + +//Eventschleife + while(inpch!=KEY_F(10)) + { + if (mode==0) + { + inpch=wgetch(whex); + } + else + { + inpch=wgetch(wascii); + } + switch(inpch) + { + case KEY_F(9): switchmode(); + break; + + case KEY_F(1): swapbits(0); + break; + + case KEY_F(2): swapbits(1); + break; + + case KEY_F(3): swapbits(2); + break; + + case KEY_F(4): swapbits(3); + break; + + case KEY_F(5): swapbits(4); + break; + + case KEY_F(6): swapbits(5); + break; + + case KEY_F(7): swapbits(6); + break; + + case KEY_F(8): swapbits(7); + break; + + case KEY_EIC: //fallthru + case KEY_IC : if (insovr=='O') + { + insovr='I'; + } + else + { + insovr='O'; + } + break; + + case KEY_LEFT: moveleft(); + break; + + case KEY_RIGHT:moveright(); + break; + + case KEY_DOWN: if(pos.y>15) + { + scrolld(); + } + else + { + pos.y++; + } + break; + + case KEY_UP: if(pos.y<2) + { + scrollu(); + } + else + { + pos.y--; + } + break; + + case KEY_NPAGE:for(i=0;i<16;i++) + { + scrolld(); + } + break; + + case KEY_PPAGE:for(i=0;i<16;i++) + { + scrollu(); + } + break; + + case KEY_HOME: initout(); + break; + + case KEY_END: pdaten=pmax; + for(i=0;((*pdaten).plast) && (i < 16);i++) + { + pdaten=(*pdaten).plast; + } + while((*pdaten).pnext) + { + scrolld(); + } + break; + + default: if(mode==0) + { + if(isdigit(inpch) || (toupper(inpch)>='A' && toupper(inpch)<='F') ) + { + waddch(whex,toupper(inpch)); + char hch[]="00"; + // hch korrekt füllen. + int xpos=pos.x; // Position sichern + switchmode(); // um die Koordinaten umzurechnen + ptv=tonumsysx(charout(),16); // Aktuelles Zeichen in Hex. + //* --- DEBUG ONLY + mvwaddstr(wbot,2,30,tonumsysx(charout(),16)); + // --- END DEBUG + switchmode(); // Koordinaten zurückrechnen + pos.x=xpos; // position restaurieren + if (insovr=='O') // Predef. only in Override! + { + hch[0] = *(ptv+6); + hch[1] = *(ptv+7); + } + else // In Insert only first char + { + if(pos.x%3==0) + hch[0] = *(ptv+6); + } + //* --- DEBUG ONLY + mvwaddstr(wbot,2,1,hch); + mvwaddstr(wbot,2,20,ptv); + // --- END DEBUG + hch[(pos.x%3)?0:1]=inpch; + switchmode(); // um die Koordinaten umzurechnen + charin(xto10(hch,16),0,xpos); // Umrechnen Hex -> Ascii+charin + switchmode(); // und wieder zurücksetzen + pos.x=xpos; // position restaurieren + moveright(); + } + } + + else + { + if((inpch < 256)) + { + waddch(wascii,inpch); + charin(inpch,1,pos.x); + moveright(); + } + } + }; + + bottomout(); + scrrefresh(); + +// fprintf(stderr,"x: %d %d",pos.x,charout()); + + } + sleep(1); + endscr(); + unallocmem(); + return (fclose(fp)); +} diff --git a/tiahv8.cc b/tiahv8.cc new file mode 100644 index 0000000..af8d8aa --- /dev/null +++ b/tiahv8.cc @@ -0,0 +1,973 @@ +/* + * GHEX -- an advanced HEX-Editor + * Copyright (C) 1999 by Joerg Tretter + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include +#include +#include +#include +#include + +struct data // one line in Memory +{ +struct data* plast; +struct data* pnext; +unsigned char vals[16]; +unsigned long line; +char len; +}; + +struct cpos // Cursor-Position +{ + char x,y; +}pos; + +char insovr='O'; // Isert/Override-Mode +int mode=1; +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII "; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +char empty78[]=" "; +int result; +char *filename; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data datas, *pdatas, *pfirst, *phelp, *pmax; + +void initvar() +{ + pfirst=&datas; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + (*pfirst).line=0; + (*pfirst).len=0; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +void miniinit() +{ +xoffs = " "; +xhex = " "; +xascii = " "; +} + +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Create Curses-space for the windows +//TODO: check whether creation was fine. + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// enable scrolling + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// enable Keyboard-control + keypad(stdscr,1); + keypad(whex,1); + keypad(wascii,1); + +// disable echoing of input + noecho(); + +// swich cursor to big size +// TODO: check why this not works in most cases +// if(curs_set(2)==ERR) retval=1 ; + +// creation of curses "Color-Pairs" + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + if(init_pair(3,COLOR_MAGENTA,COLOR_BLACK)) retval=1; + +// join the windows with the color-pairs + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + +// paint the boxes around the windows (in fact it's inside) + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + // change the color-pairs of the windows again. + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +char* tonumsysx(int base10,int newbase) +{ + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +unsigned long xto10(char *c, int oldbase) +{ +unsigned long base10=0; +int pos=0; +int i; + for (i=0;c[i];i++, c[i]=toupper(c[i])); + while(c[pos++]); + pos--; + int len=pos-1; + while(pos--) + { + if(isdigit(c[len-pos])) + { + base10 = base10 + (unsigned long)( (double)(c[len-pos]-'0') * ( pow( (double)oldbase,(double)pos ) ) ) ; + } + else + { + base10 = base10 + (unsigned long)( ((double)(c[len-pos]-'A')+10) * ( pow( (double)oldbase,(double)pos ) ) ); + } + } + return(base10); +} + +int loadcontents() +{ + int retval=0; + int x; + char ch; + unsigned long line=0; + + + pdatas=pfirst; + ch=fgetc(fp); + // Zero-byte-file. + if(feof(fp)) + { + fprintf(stderr,"opening 0-Byte file"); + pfirst=pmax=pdatas=(data*)(malloc(sizeof(datas))); + (*pdatas).pnext=NULL; + (*pdatas).plast=NULL; + (*pdatas).line=0; + (*pdatas).len=0; + + } + printf("Printing hashmark for every 8 Kbytes.\n"); + while (!(feof(fp))) + { + for(x=0;x<16;x++) + { + ((*pdatas).vals[x])=ch; + if (feof(fp)) //(*pdatas).vals[x]==EOF) + { + (*pdatas).vals[x]=0; + break; + } + (*pdatas).len=x+1; + ch=fgetc(fp); + } + (*pdatas).line=line++; + if(!(feof(fp))) + { + phelp=pdatas; + if (!(line%500)) {printf("%c",'#');} + pdatas=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); +// BUG: Check for Memory-Full does not work. + if(!pdatas) {abort();} + (*pdatas).plast=phelp; + (*pdatas).pnext=NULL; + } + pmax=pdatas; + + } + return (retval); +} + +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +void unallocmem() +{ +pdatas=pmax; +while((*pdatas).plast) +{ +phelp=(*pdatas).plast; +free(pdatas); +pdatas=phelp; +} +} + +void scrrefresh() +{ + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + // depending on the mode (ascii/hex) set the cursor-position + !mode?wmove(whex,pos.y,pos.x):wmove(wascii,pos.y,pos.x); + + wrefresh(woffs); + wrefresh(wtop); + wrefresh(wbot); + // the order is given by the mode (ascii/hex). the last window refreshed + // it the one, in which the cursor appears and the input-focus is. + !mode?wrefresh(wascii):wrefresh(whex); + mode?wrefresh(wascii):wrefresh(whex); +} + +void lineout(unsigned long line) +{ + int z; + + miniinit(); + + ptv = tonumsysx((*pdatas).line*16,16); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,line,1,soffs); + for (z=0;z<(*pdatas).len;z++) + { + ptv = tonumsysx((*pdatas).vals[z],16); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,line,1,shex); + for (z=0;z<(*pdatas).len;z++) + { +// TODO: There are many other characters that are displayable. + xascii[z] = (isgraph((*pdatas).vals[z]))?((*pdatas).vals[z]):'.'; + } + mvwaddstr(wascii,line,1,sascii); +} + +void initout() +{ +//Initial output (All) +int i; +bool toggleswitch=false; +pdatas=pfirst; + +if ((*pdatas).pnext == NULL) toggleswitch=true; +for(i=0;i<16;i++) + { + lineout(i+1); + if (toggleswitch) break; + pdatas=(*pdatas).pnext; + if ((*pdatas).pnext == NULL) toggleswitch=true; + } + scrrefresh(); + +if(i>15) pdatas=(*pdatas).plast; +} + +int scrolld() +{ + int resval; + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + wscrl(woffs,1); + wscrl(whex,1); + wscrl(wascii,1); + + miniinit(); + + lineout(16); + resval=1; + } + else + { + resval=0; + } + return(resval); +} + +int scrollu() +{ + int z=0; + int result; + while(((*pdatas).plast != NULL)&&(z < 16)) + { + pdatas=(*pdatas).plast; + z++; + } + result= (z!=15)?1:0; + if(z>15) + { + z--; + wscrl(woffs,-1); + wscrl(whex,-1); + wscrl(wascii,-1); + + miniinit(); + + lineout(1); + } + while(z--) + { + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + } + } + return(result); +} + +void switchmode() +{ + if(mode==0) + { + mode=1; // mode-toggle:Hex->Ascii + // recalculate cursor-position + pos.x=((pos.x+1)/3); + } + else + { + mode=0; // mode-toggle:ascii->Hex + // recalculate cursor-position + pos.x=((pos.x*3)-1); + } +} + +void moveleft() +{ + if(mode==0) + { + if(pos.x > 2) + { + (pos.x%3)?pos.x-=2:pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=48; + } + else + { + if(scrollu())pos.x=48; + } + } + } + else + { + if(pos.x > 1) + { + pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=16; + } + else + { + if(scrollu())pos.x=16; + } + } + } +} + +void moveright() +{ + if(mode==0) + { + if(pos.x < 48) + { + (pos.x%3)?pos.x++:pos.x+=2; + } + else + { + if(pos.y < 16) + { + pos.x=2; + pos.y++; + } + else + { + if(scrolld()) pos.x=2; + } + } + } + else + { + if((pos.x < 16)) + { + pos.x++; + } + else + { + if(pos.y < 16) + { + pos.x=1; + pos.y++; + } + else + { + if(scrolld()) pos.x=1; + } + } + } +} + +unsigned long getactpos() +{ + int i=0; + unsigned long actpos; + int distance; + int posxsave=9999; + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the position + actpos=((*pdatas).line*16)+pos.x-1; + // and move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + if (posxsave!=9999) + { + switchmode(); + pos.x=posxsave; + } + + return(actpos); +} + +void charin(char ch, int realmode, int realxpos) +{ + int i=0; + int z=0; + int distance=0; + bool toggleswitch; + data *phelp2; + char rbmode; //rollbackmode (if file < 255 chars = 1 else 2) + unsigned long zhelp; + // im move to the cursor-position in memory + // Is the file smaller then one page, it has to be handled in another way. + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + rbmode=1; + } + else + { + distance=16-pos.y; + rbmode=2; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // For the insert-mode + if (((insovr=='I') && (realmode==1))||((insovr=='I') && (realmode==0) && (realxpos%3==2)) && (realmode!=3)) + { + phelp=pdatas; + pdatas=pmax; + if ((*pdatas).len < 16) + { + (*pdatas).len++; + } + else + { + // allocate new element + phelp2=pmax; + zhelp=(*pmax).line+1; + pdatas=pmax=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); + if(!pmax) {abort();} + (*pmax).plast=phelp2; + (*pmax).pnext=NULL; + (*pmax).len=1; + (*pmax).line=zhelp; + } + while(pdatas!=phelp) + { + for(z=(*pdatas).len-1;z>=0;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + (*pdatas).vals[0]=(*(*pdatas).plast).vals[15]; + pdatas=(*pdatas).plast; + } + + for(z=(*pdatas).len-1;z+1>pos.x;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + pdatas=phelp; + } + // Write value + (*pdatas).vals[pos.x-1]=ch; + // refresh output of current line (not needed, I think) + lineout(pos.y); + // moving backwards again and print the lines immediate. + if (rbmode==1) + { + pdatas=pfirst; + if ((*pdatas).pnext == NULL) toggleswitch=true; + for(z=0;z<16;z++) + { + lineout(z+1); + if (toggleswitch) break; + pdatas=(*pdatas).pnext; + if ((*pdatas).pnext == NULL) toggleswitch=true; + } + } + else + { + while(i--) + { + if((*pdatas).pnext) + { + pdatas=(*pdatas).pnext; + lineout(16-i); + } + } + } +} + +int charout() +{ + int i=0; + int distance; + int posxsave; + int ch; + // Move in memory to cursor-position + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the value + ch=((*pdatas).vals[pos.x-1]); + // move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + return(ch); +} + +void bottomout() +{ + int realmode,realposx; + realmode=mode; + + if (realmode == 0) + { + realposx=pos.x; + switchmode(); + } + mvwaddstr(wbot,1,1,empty78); + mvwaddstr(wbot,2,1,empty78); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,1,"CHR:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddch(wbot,1,6,32); + mvwaddch(wbot,1,5,(isgraph(charout()))?charout():'.'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,9,"HEX:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,13,(tonumsysx(charout(),16))+6); + mvwaddch(wbot,1,15,'h'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,18,"DEC:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,22,(tonumsysx(charout(),10))+5); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,27,"OCT:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,31,(tonumsysx(charout(),8))+4); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,37,"BIN:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,41,tonumsysx(charout(),2)); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,52,"Offset:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,59,tonumsysx(getactpos(),16)); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,67,"h -"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,71,tonumsysx(getactpos(),10)); + + mvwaddstr(wbot,2,1,filename); + mvwaddstr(wbot,2,76,(insovr=='O')?"OVR":"INS"); + if (realmode == 0) + { + switchmode(); + pos.x=realposx; + } +} + +void swapbits(char bit) +{ + char bin[]="12345678",*pbin; // for the Bits + pbin=bin; + int realposx, realmode; + realmode=mode; + if (realmode==0) + { + realposx=pos.x; + switchmode(); + } + pbin=tonumsysx(charout(),2); + pbin[bit]= (pbin[bit]=='0')?'1':'0'; + charin(xto10(pbin,2),3,pos.x); + if (realmode==0) + { + switchmode(); + pos.x=realposx; + } +} + +void delactchar() +{ + +} + +int main(int argc, char *argv[]) +{ +//Variables + unsigned int i; // Public counter + int inpch; // The char of input + +//Preperation + printf("GHEX Version 0.5\n"); + //Is there a param? + if(argc < 2) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //open the file + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // set the pointers + initvar(); + // init of other useful varibles + pos.x = pos.y = 1; + filename=argv[1]; + //file -> ram + if(loadcontents()) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // init of terminal + if(screeninit()) + { + endscr(); + fprintf(stderr,"!!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + + +//Main-procedure +initout(); +bottomout(); +scrrefresh(); + +//Event-loop + while(inpch!=KEY_F(10)) + { + if (mode==0) + { + inpch=wgetch(whex); + } + else + { + inpch=wgetch(wascii); + } + switch(inpch) + { + // The Keys F1-F8 (bit-setting) + case KEY_F(9): switchmode(); + break; + + case KEY_F(1): swapbits(0); + break; + + case KEY_F(2): swapbits(1); + break; + + case KEY_F(3): swapbits(2); + break; + + case KEY_F(4): swapbits(3); + break; + + case KEY_F(5): swapbits(4); + break; + + case KEY_F(6): swapbits(5); + break; + + case KEY_F(7): swapbits(6); + break; + + case KEY_F(8): swapbits(7); + break; + + //the delete character-key + case KEY_DC : delactchar(); + break; + + //the insert-key, switch insert mode + case KEY_EIC: //fallthru + case KEY_IC : if (insovr=='O') + { + insovr='I'; + } + else + { + insovr='O'; + } + break; + + //Cursor-movement + case KEY_LEFT: moveleft(); + break; + + case KEY_RIGHT:moveright(); + break; + + case KEY_DOWN: if(pos.y>15) + { + scrolld(); + } + else + { + pos.y++; + } + break; + + case KEY_UP: if(pos.y<2) + { + scrollu(); + } + else + { + pos.y--; + } + break; + + case KEY_NPAGE:for(i=0;i<16;i++) + { + scrolld(); + } + break; + + case KEY_PPAGE:for(i=0;i<16;i++) + { + scrollu(); + } + break; + + case KEY_HOME: initout(); + break; + + case KEY_END: pdatas=pmax; + for(i=0;((*pdatas).plast) && (i < 16);i++) + { + pdatas=(*pdatas).plast; + } + while((*pdatas).pnext) + { + scrolld(); + } + break; + // input characters + default: if(mode==0) + { + if(isdigit(inpch) || (toupper(inpch)>='A' && toupper(inpch)<='F') ) + { + waddch(whex,toupper(inpch)); + char hch[]="00"; + // fill hch correct (old values) + int xpos=pos.x; // save position + switchmode(); // recalc. coordinates + ptv=tonumsysx(charout(),16); // actual char in Hex. + //* --- DEBUG ONLY + mvwaddstr(wbot,2,30,tonumsysx(charout(),16)); + // --- END DEBUG + switchmode(); // recalc coordinates + pos.x=xpos; // restore coorinates + if (insovr=='O') // Predef. only in Override! + { + hch[0] = *(ptv+6); + hch[1] = *(ptv+7); + } + else // In Insert only first char + { + if(pos.x%3==0) + hch[0] = *(ptv+6); + } + //* --- DEBUG ONLY + mvwaddstr(wbot,2,1,hch); + mvwaddstr(wbot,2,20,ptv); + // --- END DEBUG + hch[(pos.x%3)?0:1]=inpch; + switchmode(); // recalc coordinates + charin(xto10(hch,16),0,xpos); // calc Hex -> Ascii+charin + switchmode(); // and back + pos.x=xpos; // restore positions + moveright(); + } + } + + else + { + if((inpch < 256)) + { + waddch(wascii,inpch); + charin(inpch,1,pos.x); + moveright(); + } + } + }; + + bottomout(); + scrrefresh(); + + } + endscr(); + unallocmem(); + + return (fclose(fp)); +} diff --git a/tiahv9.cc b/tiahv9.cc new file mode 100644 index 0000000..4740862 --- /dev/null +++ b/tiahv9.cc @@ -0,0 +1,1079 @@ +/* + * GHEX -- an advanced HEX-Editor + * Copyright (C) 1999 by Joerg Tretter + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include +#include + +struct data // one line in Memory +{ +struct data* plast; +struct data* pnext; +unsigned char vals[16]; +unsigned long line; +char len; +}; + +struct cpos // Cursor-Position +{ + char x,y; +}pos; + +char insovr='O'; // Isert/Override-Mode +int mode=1; +WINDOW *stdscr,*woffs,*whex, *wascii, *wtop, *wbot; +const char *topline= " Offset Hex ASCII "; +char xoffs[] = "12345678",*soffs; +char xhex[] = " 14 15 16 32 32 34 20 54 61 67 65 73 61 6D 74 0D ",*shex; +char xascii[] = "1234567890123456",*sascii; +char empty78[]=" "; +int result; +char *filename; +char *ptv="12345678"; +FILE *fp; +int ipoint; +data datas, *pdatas, *pfirst, *phelp, *pmax; + +/*************************************************************************** + * FUNCTION: void initvar() * + * GOOD FOR: Initialization of basic-variables * + ***************************************************************************/ +void initvar() +{ + pfirst=&datas; + (*pfirst).plast=NULL; + (*pfirst).pnext=NULL; + (*pfirst).line=0; + (*pfirst).len=0; + soffs=xoffs; + shex=xhex; + sascii=xascii; +} + +/*************************************************************************** + * FUNCTION: void miniinit() * + * GOOD FOR: Initialization of frequent used variables * + ***************************************************************************/ +void miniinit() +{ +xoffs = " "; +xhex = " "; +xascii = " "; +} + +/*************************************************************************** + * FUNCTION: int screeninit() * + * GOOD FOR: Initialization of the n-curses-screen * + * RESULTS : 1:There was an error / 2:Everything ok. * + ***************************************************************************/ +int screeninit() +{ + int retval=0; + + stdscr= initscr(); + if(start_color()) retval=1; + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +// Create Curses-space for the windows +//TODO: check whether creation was fine. + woffs =newwin(18,10,3 ,0 ); + whex =newwin(18,52,3 ,10); + wascii=newwin(18,18,3 ,62); + wtop =newwin(3 ,80,0 ,0 ); + wbot =newwin(4 ,80,21,0 ); + +// enable scrolling + scrollok(woffs,1); + scrollok(whex,1); + scrollok(wascii,1); + +// enable Keyboard-control + keypad(stdscr,1); + keypad(whex,1); + keypad(wascii,1); + +// disable echoing of input + noecho(); + +// swich cursor to big size +// TODO: check why this not works in most cases +// if(curs_set(2)==ERR) retval=1 ; + +// creation of curses "Color-Pairs" + if(init_pair(1,COLOR_BLUE,COLOR_BLACK)) retval=1; + if(init_pair(2,COLOR_CYAN,COLOR_BLACK)) retval=1; + if(init_pair(3,COLOR_MAGENTA,COLOR_BLACK)) retval=1; + +// join the windows with the color-pairs + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + +// paint the boxes around the windows (in fact it's inside) + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + // change the color-pairs of the windows again. + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + return(retval); +} + +/*************************************************************************** + * FUNCTION: char* tonumsysx() * + * GOOD FOR: Shift a number to another numbering system * + * INPUTS : - int base10 -The number in decimal system * + * - int newbase -The new system (16->Hex,2->Bin,...) * + * RESULTS : The number in the new system as a pointer of char * + ***************************************************************************/ +char* tonumsysx(int base10,int newbase) +{ + int erg; + int rest=0; + int pos=0; + int i; + static char swap[9]; + char c[9]; + + swap="********"; + c[pos]='0'+rest; + while (base10 > 0) + { + erg=base10/newbase; + rest=base10%newbase; + if (rest <= 9) + c[pos]='0'+rest; + else + c[pos]='A'+(rest-10); + + base10=erg; + pos++; + c[pos]='\0'; + } + while(pos < 8) + { + c[pos]='0'; + pos++; + } + pos--; + for (i=0;i<=pos;i++) + { + swap[i]=c[pos-i]; + } + +return(swap); + + +} + +/*************************************************************************** + * FUNCTION: unsigned long xto10(char *c, int oldbase) * + * GOOD FOR: Shift a number from a foreign numbering system to decimal * + * INPUTS : - char* c -The number in foreign system * + * - int oldbase -The old system (16->Hex,2->Bin,...) * + * RESULTS : The number in decimal system as a unsigned long * + ***************************************************************************/ +unsigned long xto10(char *c, int oldbase) +{ +unsigned long base10=0; +int pos=0; +int i; + for (i=0;c[i];i++, c[i]=toupper(c[i])); + while(c[pos++]); + pos--; + int len=pos-1; + while(pos--) + { + if(isdigit(c[len-pos])) + { + base10 = base10 + (unsigned long)( (double)(c[len-pos]-'0') * ( pow( (double)oldbase,(double)pos ) ) ) ; + } + else + { + base10 = base10 + (unsigned long)( ((double)(c[len-pos]-'A')+10) * ( pow( (double)oldbase,(double)pos ) ) ); + } + } + return(base10); +} + +/*************************************************************************** + * FUNCTION: int loadcontents() * + * GOOD FOR: Loading the contents of a file into the memory * + * RESULTS : The status of the loading (not used yet) * + ***************************************************************************/ +int loadcontents() +{ + int retval=0; + int x; + char ch; + unsigned long line=0; + + + pdatas=pfirst; + ch=fgetc(fp); + // Zero-byte-file. + if(feof(fp)) + { + fprintf(stderr,"opening 0-Byte file"); + pfirst=pmax=pdatas=(data*)(malloc(sizeof(datas))); + (*pdatas).pnext=NULL; + (*pdatas).plast=NULL; + (*pdatas).line=0; + (*pdatas).len=0; + + } + // TODO: how to flush the buffer of stdout??? + printf("Printing hashmark for every 8 Kbytes.\n"); + while (!(feof(fp))) + { + for(x=0;x<16;x++) + { + ((*pdatas).vals[x])=ch; + if (feof(fp)) //(*pdatas).vals[x]==EOF) + { + (*pdatas).vals[x]=0; + break; + } + (*pdatas).len=x+1; + ch=fgetc(fp); + } + (*pdatas).line=line++; + if(!(feof(fp))) + { + phelp=pdatas; + if (!(line%500)) {printf("%c",'#');} + pdatas=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); +// BUG: Check for Memory-Full does not work. + if(!pdatas) {abort();} + (*pdatas).plast=phelp; + (*pdatas).pnext=NULL; + } + pmax=pdatas; + + } + return (retval); +} + +/*************************************************************************** + * FUNCTION: void endscr() * + * GOOD FOR: End the scrren session on curses-level * + ***************************************************************************/ +void endscr() +{ + delwin(woffs); + delwin(whex); + delwin(wascii); + delwin(wtop); + delwin(wbot); + endwin(); +} + +/*************************************************************************** + * FUNCTION: void unallocmem() * + * GOOD FOR: Free the memory from the bits & bytes * + ***************************************************************************/ +void unallocmem() +{ +pdatas=pmax; +while((*pdatas).plast) +{ +phelp=(*pdatas).plast; +free(pdatas); +pdatas=phelp; +} +} + +/*************************************************************************** + * FUNCTION: void scrrefresh() * + * GOOD FOR: Refreah the basic-screen-elements (not the contents) * + ***************************************************************************/ +void scrrefresh() +{ + wattrset(woffs,COLOR_PAIR(1)); + wattrset(whex,COLOR_PAIR(1)); + wattrset(wascii,COLOR_PAIR(1)); + wattrset(wbot,COLOR_PAIR(1)); + wattrset(wtop,COLOR_PAIR(1)); + + box(woffs,0,0); + box(whex,0,0); + box(wascii,0,0); + box(wtop,0,0); + box(wbot,0,0); + + wattrset(woffs,COLOR_PAIR(2)); + wattrset(whex,COLOR_PAIR(2)); + wattrset(wascii,COLOR_PAIR(2)); + wattrset(wtop,COLOR_PAIR(3)); + wattrset(wbot,COLOR_PAIR(2)); + + // depending on the mode (ascii/hex) set the cursor-position + !mode?wmove(whex,pos.y,pos.x):wmove(wascii,pos.y,pos.x); + + wrefresh(woffs); + wrefresh(wtop); + wrefresh(wbot); + // the order is given by the mode (ascii/hex). the last window refreshed + // it the one, in which the cursor appears and the input-focus is. + !mode?wrefresh(wascii):wrefresh(whex); + mode?wrefresh(wascii):wrefresh(whex); +} + +/*************************************************************************** + * FUNCTION: void lineout(unsigned long line) * + * GOOD FOR: Writing one line (the actual one) from the memory to screen * + ***************************************************************************/ +void lineout(unsigned long line) +{ + int z; + + miniinit(); + + ptv = tonumsysx((*pdatas).line*16,16); + for(z=0;z<9;z++) + { + xoffs[z] = *(ptv+z); + } + mvwaddstr(woffs,line,1,soffs); + for (z=0;z<(*pdatas).len;z++) + { + ptv = tonumsysx((*pdatas).vals[z],16); + xhex[z*3+1] = *(ptv+6); + xhex[z*3+2] = *(ptv+7); + } + mvwaddstr(whex,line,1,shex); + for (z=0;z<(*pdatas).len;z++) + { +// TODO: There are many other characters that are displayable. + xascii[z] = (isgraph((*pdatas).vals[z]))?((*pdatas).vals[z]):'.'; + } + mvwaddstr(wascii,line,1,sascii); +} + +/*************************************************************************** + * FUNCTION: void initout() * + * GOOD FOR: First output after loading the file // home-Key * + ***************************************************************************/ +void initout() +{ +//Initial output (All) +int i; +bool toggleswitch=false; +pdatas=pfirst; + +if ((*pdatas).pnext == NULL) toggleswitch=true; +for(i=0;i<16;i++) + { + lineout(i+1); + if (toggleswitch) break; + pdatas=(*pdatas).pnext; + if ((*pdatas).pnext == NULL) toggleswitch=true; + } + scrrefresh(); + +if(i>15) pdatas=(*pdatas).plast; +} + +/*************************************************************************** + * FUNCTION: int scrolld() * + * GOOD FOR: Scrolling the scrren one line down * + * RESULTS : 1: no more elements // 0: everything was nice * + ***************************************************************************/ +int scrolld() +{ + int resval; + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + wscrl(woffs,1); + wscrl(whex,1); + wscrl(wascii,1); + + miniinit(); + + lineout(16); + resval=1; + } + else + { + resval=0; + } + return(resval); +} + +/*************************************************************************** + * FUNCTION: int scrollu() * + * GOOD FOR: Scrolling the scrren one line up * + * RESULTS : 1: at first element // 0: everything was nice * + ***************************************************************************/ +int scrollu() +{ + int z=0; + int result; + while(((*pdatas).plast != NULL)&&(z < 16)) + { + pdatas=(*pdatas).plast; + z++; + } + result= (z!=15)?1:0; + if(z>15) + { + z--; + wscrl(woffs,-1); + wscrl(whex,-1); + wscrl(wascii,-1); + + miniinit(); + + lineout(1); + } + while(z--) + { + if ((*pdatas).pnext != NULL) + { + pdatas=(*pdatas).pnext; + } + } + return(result); +} + +/*************************************************************************** + * FUNCTION: void switchmode() * + * GOOD FOR: recalculation of cursor-position ascii<->hex * + ***************************************************************************/ +void switchmode() +{ + if(mode==0) + { + mode=1; // mode-toggle:Hex->Ascii + // recalculate cursor-position + pos.x=((pos.x+1)/3); + } + else + { + mode=0; // mode-toggle:ascii->Hex + // recalculate cursor-position + pos.x=((pos.x*3)-1); + } +} + +/*************************************************************************** + * FUNCTION: void moveleft() * + * GOOD FOR: move the cursor one position to the left * + ***************************************************************************/ +void moveleft() +{ + if(mode==0) + { + if(pos.x > 2) + { + (pos.x%3)?pos.x-=2:pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=48; + } + else + { + if(scrollu())pos.x=48; + } + } + } + else + { + if(pos.x > 1) + { + pos.x--; + } + else + { + if(pos.y >1) + { + pos.y--; + pos.x=16; + } + else + { + if(scrollu())pos.x=16; + } + } + } +} + +/*************************************************************************** + * FUNCTION: void moveright() * + * GOOD FOR: move the cursor one position to the right * + ***************************************************************************/ +void moveright() +{ + if(mode==0) + { + if(pos.x < 48) + { + (pos.x%3)?pos.x++:pos.x+=2; + } + else + { + if(pos.y < 16) + { + pos.x=2; + pos.y++; + } + else + { + if(scrolld()) pos.x=2; + } + } + } + else + { + if((pos.x < 16)) + { + pos.x++; + } + else + { + if(pos.y < 16) + { + pos.x=1; + pos.y++; + } + else + { + if(scrolld()) pos.x=1; + } + } + } +} + +/*************************************************************************** + * FUNCTION: unsigned long getactpos() * + * GOOD FOR: get the acutal cursor-position as a file-offset * + * RESULTS : The file-offset on the cursor-position as unsigned long * + ***************************************************************************/ +unsigned long getactpos() +{ + int i=0; + unsigned long actpos; + int distance; + int posxsave=9999; + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the position + actpos=((*pdatas).line*16)+pos.x-1; + // and move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + if (posxsave!=9999) + { + switchmode(); + pos.x=posxsave; + } + + return(actpos); +} + +/*************************************************************************** + * FUNCTION: void charin(char ch, int realmode, int realxpos) * + * GOOD FOR: insert/override one character in the memory * + * INPUTS : - char ch -the char to insert * + * - int realmode -the real mode (hex/ascii) * + * - int realxpos -the real x-position of the cursor * + ***************************************************************************/ +void charin(char ch, int realmode, int realxpos) +{ + int i=0; + int z=0; + int distance=0; + bool toggleswitch; + data *phelp2; + char rbmode; //rollbackmode (if file < 255 chars = 1 else 2) + unsigned long zhelp; + // im move to the cursor-position in memory + // Is the file smaller then one page, it has to be handled in another way. + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + rbmode=1; + } + else + { + distance=16-pos.y; + rbmode=2; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // For the insert-mode + if (((insovr=='I') && (realmode==1))||((insovr=='I') && (realmode==0) && (realxpos%3==2)) && (realmode!=3)) + { + phelp=pdatas; + pdatas=pmax; + if ((*pdatas).len < 16) + { + (*pdatas).len++; + } + else + { + // allocate new element + phelp2=pmax; + zhelp=(*pmax).line+1; + pdatas=pmax=((*pdatas).pnext)=(data*)(malloc(sizeof(datas))); + if(!pmax) {abort();} + (*pmax).plast=phelp2; + (*pmax).pnext=NULL; + (*pmax).len=1; + (*pmax).line=zhelp; + } + while(pdatas!=phelp) + { + for(z=(*pdatas).len-1;z>=0;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + (*pdatas).vals[0]=(*(*pdatas).plast).vals[15]; + pdatas=(*pdatas).plast; + } + + for(z=(*pdatas).len-1;z+1>pos.x;z--) + { + (*pdatas).vals[z]=(*pdatas).vals[z-1]; + } + pdatas=phelp; + } + // Write value + (*pdatas).vals[pos.x-1]=ch; + // refresh output of current line (not needed, I think) + lineout(pos.y); + // moving backwards again and print the lines immediate. + if (rbmode==1) + { + pdatas=pfirst; + if ((*pdatas).pnext == NULL) toggleswitch=true; + for(z=0;z<16;z++) + { + lineout(z+1); + if (toggleswitch) break; + pdatas=(*pdatas).pnext; + if ((*pdatas).pnext == NULL) toggleswitch=true; + } + } + else + { + while(i--) + { + if((*pdatas).pnext) + { + pdatas=(*pdatas).pnext; + lineout(16-i); + } + } + } +} + +/*************************************************************************** + * FUNCTION: int charout() * + * GOOD FOR: get the char at the actual cursor-position * + * RESULTS : the char as an integer (char should work too normaly) * + ***************************************************************************/ +int charout() +{ + int i=0; + int distance; + int posxsave; + int ch; + // Move in memory to cursor-position + if (!mode) + { + posxsave=pos.x; + switchmode(); + } + // move in Memory to the cursor-position + if((pdatas==pmax) && ((*pdatas).line<15)) + { + distance=(*pdatas).line-pos.y+1; + } + else + { + distance=16-pos.y; + } + while(i<(distance)&&((*pdatas).plast)) + { + i++; + pdatas=(*pdatas).plast; + } + // get the value + ch=((*pdatas).vals[pos.x-1]); + // move back + while(i--) + { + pdatas=(*pdatas).pnext; + } + return(ch); +} + +/*************************************************************************** + * FUNCTION: void bottomout() * + * GOOD FOR: refreshing the output of the bottom-window * + ***************************************************************************/ +void bottomout() +{ + int realmode,realposx; + realmode=mode; + + if (realmode == 0) + { + realposx=pos.x; + switchmode(); + } + mvwaddstr(wbot,1,1,empty78); + mvwaddstr(wbot,2,1,empty78); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,1,"CHR:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddch(wbot,1,6,32); + mvwaddch(wbot,1,5,(isgraph(charout()))?charout():'.'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,9,"HEX:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,13,(tonumsysx(charout(),16))+6); + mvwaddch(wbot,1,15,'h'); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,18,"DEC:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,22,(tonumsysx(charout(),10))+5); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,27,"OCT:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,31,(tonumsysx(charout(),8))+4); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,37,"BIN:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,41,tonumsysx(charout(),2)); + + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,52,"Offset:"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,59,tonumsysx(getactpos(),16)); + wattrset(wbot,COLOR_PAIR(3)); + mvwaddstr(wbot,1,67,"h -"); + wattrset(wbot,COLOR_PAIR(2)); + mvwaddstr(wbot,1,71,tonumsysx(getactpos(),10)); + + mvwaddstr(wbot,2,1,filename); + mvwaddstr(wbot,2,76,(insovr=='O')?"OVR":"INS"); + if (realmode == 0) + { + switchmode(); + pos.x=realposx; + } +} + +/*************************************************************************** + * FUNCTION: void swapbits() * + * GOOD FOR: toggle the bit [par1] an the actual position * + * INPUTS : - char bit the number (1-8) of the bit to toggle * + ***************************************************************************/ +void swapbits(char bit) +{ + char bin[]="12345678",*pbin; // for the Bits + pbin=bin; + int realposx, realmode; + realmode=mode; + if (realmode==0) + { + realposx=pos.x; + switchmode(); + } + pbin=tonumsysx(charout(),2); + pbin[bit]= (pbin[bit]=='0')?'1':'0'; + charin(xto10(pbin,2),3,pos.x); + if (realmode==0) + { + switchmode(); + pos.x=realposx; + } +} + +/*************************************************************************** + * FUNCTION: void delactchar() * + * GOOD FOR: delete the char under the actual cursor-position * + ***************************************************************************/ +void delactchar() +{ + +} + +int main(int argc, char *argv[]) +{ +//Variables + unsigned int i; // Public counter + int inpch; // The char of input + +//Preperation + printf("GHEX Version 0.5\n"); + //Is there a param? + if(argc < 2) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nUSAGE: %s \n",argv[0]); + exit(1); + } + //open the file + fp=fopen(argv[1],"r"); + if (fp == NULL) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR opening file: %s \n",argv[1]); + exit(2); + } + // set the pointers + initvar(); + // init of other useful varibles + pos.x = pos.y = 1; + filename=argv[1]; + //file -> ram + if(loadcontents()) + { + fprintf(stderr,"GHEX-MESSAGE:\n\nERROR reading %s (too less RAM?)\n",argv[1]); + exit(3); + } + // init of terminal + if(screeninit()) + { + endscr(); + fprintf(stderr,"!!!Terminal not supported!!! -> Abort \n"); + exit(5); + } + mvwaddstr(wtop,1,1,topline); + + +//Main-procedure +initout(); +bottomout(); +scrrefresh(); + +//Event-loop + while(inpch!=KEY_F(10)) + { + if (mode==0) + { + inpch=wgetch(whex); + } + else + { + inpch=wgetch(wascii); + } + switch(inpch) + { + // The Keys F1-F8 (bit-setting) + case KEY_F(9): switchmode(); + break; + + case KEY_F(1): swapbits(0); + break; + + case KEY_F(2): swapbits(1); + break; + + case KEY_F(3): swapbits(2); + break; + + case KEY_F(4): swapbits(3); + break; + + case KEY_F(5): swapbits(4); + break; + + case KEY_F(6): swapbits(5); + break; + + case KEY_F(7): swapbits(6); + break; + + case KEY_F(8): swapbits(7); + break; + + //the delete character-key + case KEY_DC : delactchar(); + break; + + //the insert-key, switch insert mode + case KEY_EIC: //fallthru + case KEY_IC : if (insovr=='O') + { + insovr='I'; + } + else + { + insovr='O'; + } + break; + + //Cursor-movement + case KEY_LEFT: moveleft(); + break; + + case KEY_RIGHT:moveright(); + break; + + case KEY_DOWN: if(pos.y>15) + { + scrolld(); + } + else + { + pos.y++; + } + break; + + case KEY_UP: if(pos.y<2) + { + scrollu(); + } + else + { + pos.y--; + } + break; + + case KEY_NPAGE:for(i=0;i<16;i++) + { + scrolld(); + } + break; + + case KEY_PPAGE:for(i=0;i<16;i++) + { + scrollu(); + } + break; + + case KEY_HOME: initout(); + break; + + // BUG: should only work when file > one screen + case KEY_END: pdatas=pmax; + for(i=0;((*pdatas).plast) && (i < 16);i++) + { + pdatas=(*pdatas).plast; + } + while((*pdatas).pnext) + { + scrolld(); + } + break; + // input characters + default: if(mode==0) + { + if(isdigit(inpch) || (toupper(inpch)>='A' && toupper(inpch)<='F') ) + { + waddch(whex,toupper(inpch)); + char hch[]="00"; + // fill hch correct (old values) + int xpos=pos.x; // save position + switchmode(); // recalc. coordinates + ptv=tonumsysx(charout(),16); // actual char in Hex. + //* --- DEBUG ONLY + mvwaddstr(wbot,2,30,tonumsysx(charout(),16)); + // --- END DEBUG + switchmode(); // recalc coordinates + pos.x=xpos; // restore coorinates + if (insovr=='O') // Predef. only in Override! + { + hch[0] = *(ptv+6); + hch[1] = *(ptv+7); + } + else // In Insert only first char + { + if(pos.x%3==0) + hch[0] = *(ptv+6); + } + //* --- DEBUG ONLY + mvwaddstr(wbot,2,1,hch); + mvwaddstr(wbot,2,20,ptv); + // --- END DEBUG + hch[(pos.x%3)?0:1]=inpch; + switchmode(); // recalc coordinates + charin(xto10(hch,16),0,xpos); // calc Hex -> Ascii+charin + switchmode(); // and back + pos.x=xpos; // restore positions + moveright(); + } + } + + else + { + if((inpch < 256)) + { + waddch(wascii,inpch); + charin(inpch,1,pos.x); + moveright(); + } + } + }; + + bottomout(); + scrrefresh(); + + } + endscr(); + unallocmem(); + + return (fclose(fp)); +} diff --git a/uu b/uu new file mode 100644 index 0000000..32e2f22 --- /dev/null +++ b/uu @@ -0,0 +1 @@ +‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ diff --git a/webprefs b/webprefs new file mode 100644 index 0000000..d31cd7c --- /dev/null +++ b/webprefs @@ -0,0 +1,5 @@ +Homepage: http://www.fortunecity.de/wolkenkratzer/messeturm/358 +user: ghex +Pass: bitmaster + +ftp: ftp.fortunecity.de diff --git a/x b/x new file mode 100644 index 0000000..bbd09e8 Binary files /dev/null and b/x differ diff --git a/y b/y new file mode 100644 index 0000000..e69de29 diff --git a/zweites.cc b/zweites.cc new file mode 100644 index 0000000..b253bd0 --- /dev/null +++ b/zweites.cc @@ -0,0 +1,58 @@ +#include + +int main() +{ + WINDOW *stdscr,*w1,*w2; + char str1[] = "Zwei",*Pstr; + + int z; + +Pstr=str1; + +stdscr = initscr(); +int sc=start_color(); + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); +// Zwei Fenster + w1=newwin(10,10,0,0); + w2=newwin(10,10,0,20); + +// Scrollen erlauben + scrollok(w1,1); + scrollok(w2,1); + +// Color-Pairs anlegen (Farbkombinationen) + int cp=init_pair(1,COLOR_MAGENTA,COLOR_BLUE); + cp=init_pair(2,COLOR_BLUE,COLOR_MAGENTA); + COLOR_PAIR(1); + + for (z=0;z < 500;z++) + { + str1[1]='A'+(z%19); + +// Color-Pair1 für window w1 benutzen + wattrset(w1,COLOR_PAIR(1)); + if (z > 9) wscrl(w1,1); + wmove(w1,z < 9?(z%9):9,0); + waddstr(w1,Pstr); + wrefresh(w1); + + wattrset(w2,COLOR_PAIR(2)); + if (z > 9) wscrl(w2,-1); + wmove(w2,z<9?(z%9):0,0); + waddstr(w2,Pstr); + wrefresh(w2); + + usleep(100); + usleep(100); + usleep(100); + usleep(100); + usleep(100); + } + sleep(1); + delwin(w1); + delwin(w2); + endwin(); + return 0; +}