home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume03 / bpe < prev    next >
Encoding:
Text File  |  1991-08-27  |  10.4 KB  |  550 lines

  1. Path: uunet!wyse!vsi1!ubvax!ames!ncar!husc6!necntc!ncoast!allbery
  2. From: andy@uunet.UU.NET@mssx.UUCP (Andreas Pleschutznig)
  3. Newsgroups: comp.sources.misc
  4. Subject: v03i064: Screen oriented binary patch editor
  5. Keywords: binary patch editor
  6. Message-ID: <671@mssx.UUCP>
  7. Date: 27 Jun 88 18:46:29 GMT
  8. Sender: allbery@ncoast.UUCP
  9. Reply-To: andy@uunet.UU.NET@mssx.UUCP (Andreas Pleschutznig)
  10. Organization: Micro Systems Software, Graz
  11. Lines: 536
  12. Approved: allbery@ncoast.UUCP
  13.  
  14. Posting-number: Volume 3, Issue 64
  15. Submitted-by: "Andreas Pleschutznig" <andy@uunet.UU.NET@mssx.UUCP>
  16. Archive-name: bpe
  17.  
  18. [The author called this "patch", perhaps he's not aware of lwall's magnum opus.
  19. Having seen "bp" and "bpatch", I named this "bpe" instead.  ++bsa]
  20.  
  21. I wrote this, because in std unix sometimes i failed to see the struc.
  22. of a file in a format I wanted to see it. I hope this program is useful to
  23. some of you, so i post it to comp.sources.unix to make it available to all
  24. of you. If you have any questions or suggestions or patches please kontact 
  25. me.
  26.     Andreas Pleschutznig
  27.     Micro Systems Software
  28.     8044 Graz, Austria
  29.     Teichhofweg 2
  30.  
  31. #------------------------------- cut here ---------------------------
  32. # To recreate the file(s) in this archive execute this file with sh
  33. # Do not use csh!!!.  
  34. #
  35. # Created from : andy@mssx
  36. # date:       Mon Jun 27 18:36:30 GMT 1988
  37. echo x - makefile
  38.  sed 's/^X//' >makefile << '+SHAR+MARK+'
  39. XCFLAGS = -O 
  40. XLIBES = -lcurses
  41. XOBJS = bpe.o
  42. XSRCS = bpe.c
  43. XEXEC = bpe
  44. X
  45. X$(EXEC): $(OBJS)
  46. X    cc $(OBJS) -o $(EXEC) $(LIBES)
  47. X
  48. X$(OBJS): $(SRCS)
  49. X    cc -c $(CFLAGS) $(SRCS)
  50. +SHAR+MARK+
  51. echo '-rw-r--r--   1 andy     other        166 Jun  6 12:22 makefile     (as sent)'
  52. chmod u=rw,g=r,o=r makefile
  53. ls -l makefile
  54. echo x - bpe.c
  55.  sed 's/^X//' >bpe.c << '+SHAR+MARK+'
  56. X/***************************************************************************
  57. X
  58. XVersion History:
  59. X
  60. XVer.No    Comment                            By
  61. X===========================================================================
  62. X1.0     first version (seems to to things right)           andy@mssx
  63. X
  64. XI declare this program as freeware, i.e. you may duplicate it, give it
  65. Xto your friends, and transfer it to any machine you like, as long as
  66. Xyou do not change or delete the build in copyright message.
  67. X
  68. X    Andreas Pleschutznig
  69. X    Teichhofweg 2
  70. X    8044 Graz
  71. X    Austria 
  72. X
  73. XComments and bug reports to:
  74. X    andy@mssx    (mcvax!tuvie!mssx!andy)
  75. X
  76. X
  77. X*****************************************************************************/
  78. X
  79. X
  80. X#include <curses.h>
  81. X#include <fcntl.h>
  82. X#include <signal.h>
  83. X
  84. X#define    BELL    0x07
  85. X#define ASCX    63
  86. X#define ASCY    6
  87. X#define HEXY    6
  88. X#define HEXX    12
  89. X
  90. Xint     path;                   /* path to file to patch */
  91. Xlong    filpos;            /* position in file */
  92. Xchar    secbuf[256];        /* sector read buffer */
  93. X
  94. Xint     donix();                /* default signal handling routine */
  95. Xchar    filename[60];
  96. Xint    length;            /* length of read sector */
  97. X
  98. Xmain(argc,argv)
  99. Xint argc;
  100. Xchar *argv[];
  101. X
  102. X{
  103. X    if (argc != 2) {
  104. X        fprintf(stderr,"Usage: %s filename\n",argv[0]);
  105. X        exit(1);
  106. X        }
  107. X    if (( path = open(argv[1],O_RDWR)) == -1) {
  108. X        fprintf(stderr,"%s: Can't open '%s'\n",argv[0],argv[1]);
  109. X        exit(1);
  110. X        }
  111. X    sprintf(filename,"%s",argv[1]);
  112. X    initscr();
  113. X    refresh();
  114. X    signal(SIGINT,donix);
  115. X    signal(SIGQUIT,donix);
  116. X    cbreak();                       /* set single char input */
  117. X    noecho();
  118. X    keypad(stdscr,TRUE);
  119. X    filpos = 0;            /* set global position to 0 */
  120. X    length = 0;
  121. X    command();
  122. X    endwin();
  123. X    close(path);
  124. X}
  125. X
  126. Xcommand()
  127. X
  128. X{
  129. X    int inval;
  130. X
  131. X    header("PATCH Version 1.0",filename,"(C) 1988 MSS Graz");
  132. X    inval = 0;
  133. X    while ((inval != 'q') && (inval != 'Q')) {
  134. X        move(2,0);
  135. X        mvprintw(2,0,"COMMAND : ");
  136. X        refresh();
  137. X        inval = getch();
  138. X        switch (inval) {
  139. X            case 'q':
  140. X            case 'Q':
  141. X                break;
  142. X            case 'h':
  143. X            case 'H':
  144. X            case '?':
  145. X                help();
  146. X                break;
  147. X            case 'f':
  148. X            case 'F':
  149. X                find_string();
  150. X                dump();
  151. X                break;
  152. X            case '+':
  153. X            case 'n':
  154. X            case 'N':
  155. X                filpos += 256;
  156. X                dump();
  157. X                break;
  158. X            case '-':
  159. X            case 'p':
  160. X            case 'P':
  161. X                filpos -= 256;
  162. X                if (filpos < 0)
  163. X                    filpos = 0;
  164. X                dump();
  165. X                break;
  166. X            case 'D':
  167. X            case 'd':
  168. X                dump();
  169. X                break;
  170. X            case 's':
  171. X            case 'S':
  172. X                set();
  173. X                dump();
  174. X                break;
  175. X            case 'e':
  176. X                edit_ascii();
  177. X                break;
  178. X            case 'E':
  179. X                edit_hex();
  180. X                break;
  181. X            case 'w':
  182. X            case 'W':
  183. X                wrsec();
  184. X                break;
  185. X            default:
  186. X                werr("Invalid Command !");
  187. X            }
  188. X        }
  189. X
  190. X}
  191. X
  192. Xedit_ascii()
  193. X{
  194. X    int inval = 0;
  195. X    int cury,curx;
  196. X
  197. X    if (length == 0)
  198. X        length = dump();
  199. X    move(2,0);
  200. X    clrtoeol();
  201. X    printw("End editing with CNTRL-C");
  202. X    curx = cury = 0;
  203. X    while (inval != 0x03) {
  204. X        move(ASCY+cury,ASCX+curx);
  205. X        refresh();
  206. X        inval = getch();
  207. X        switch (inval) {
  208. X            case KEY_UP:
  209. X                if (cury)
  210. X                    cury--;
  211. X                else
  212. X                    beep();
  213. X                break;
  214. X            case KEY_DOWN:
  215. X                if (cury < 15)
  216. X                    cury++;
  217. X                else
  218. X                    beep();
  219. X                break;
  220. X            case KEY_RIGHT:
  221. X                if (curx < 15)
  222. X                    curx++;
  223. X                else
  224. X                    beep();
  225. X                break;
  226. X            case KEY_LEFT:
  227. X                if (curx)
  228. X                    curx--;
  229. X                else
  230. X                    beep();
  231. X                break;
  232. X            default:
  233. X                if ((inval >= 0x20) && (inval <= 0x7e)) {
  234. X                    secbuf[cury*16+curx] =inval;
  235. X                    curx++;
  236. X                    if (curx > 15) {
  237. X                        curx=0;
  238. X                        cury++;
  239. X                        }
  240. X                    if (cury > 15)
  241. X                        cury = 0;
  242. X                    disp(length);
  243. X                    }
  244. X                break;
  245. X            }
  246. X        }
  247. X    move(2,0);
  248. X    clrtoeol();
  249. X}
  250. X
  251. Xgethex(cury,curx)
  252. Xint    cury,curx;
  253. X{
  254. X    int val;
  255. X    int inlen;
  256. X    int value;
  257. X
  258. X    inlen = 0;
  259. X    while (inlen < 2) {
  260. X        val = getch();
  261. X        if (val > 255) 
  262. X            return(val);
  263. X        if (val < '0')
  264. X            return(-1);
  265. X        if (val > '9') val &= ~0x20;
  266. X        if (((val >= '0') && (val <='9')) || 
  267. X            ((val >= 'A') && (val <= 'F'))) {
  268. X            if (val <= '9')
  269. X                val -= '0';
  270. X            else
  271. X                val = val - 'A' + 0x0a;
  272. X            switch (inlen) {
  273. X                case 0:
  274. X                    value = val << 4;
  275. X                    secbuf[cury*16+curx] = value;
  276. X                    disp(length);
  277. X                    move(HEXY+cury,HEXX+curx*3+1);
  278. X                    refresh();
  279. X                    break;
  280. X                case 1:
  281. X                    value += val ;
  282. X                    break;
  283. X                }
  284. X            inlen++;
  285. X            }
  286. X        }
  287. X    return(value);
  288. X}
  289. X
  290. Xedit_hex()
  291. X{
  292. X    int inval = 0;
  293. X    int cury,curx;
  294. X
  295. X    if (length == 0)
  296. X        length = dump();
  297. X    move(2,0);
  298. X    clrtoeol();
  299. X    printw("End editing with CNTRL-C");
  300. X    curx = cury = 0;
  301. X    while (inval != -1) {
  302. X        move(HEXY+cury,HEXX+curx*3);
  303. X        refresh();
  304. X        inval = gethex(cury,curx);
  305. X        switch (inval) {
  306. X            case KEY_UP:
  307. X                if (cury)
  308. X                    cury--;
  309. X                else
  310. X                    beep();
  311. X                break;
  312. X            case KEY_DOWN:
  313. X                if (cury < 15)
  314. X                    cury++;
  315. X                else
  316. X                    beep();
  317. X                break;
  318. X            case KEY_RIGHT:
  319. X                if (curx < 15)
  320. X                    curx++;
  321. X                else
  322. X                    beep();
  323. X                break;
  324. X            case KEY_LEFT:
  325. X                if (curx)
  326. X                    curx--;
  327. X                else
  328. X                    beep();
  329. X                break;
  330. X            default:
  331. X                if ((inval >= 0x00) && (inval <= 0xff)) {
  332. X                    secbuf[cury*16+curx] =inval;
  333. X                    curx++;
  334. X                    if (curx > 15) {
  335. X                        curx=0;
  336. X                        cury++;
  337. X                        }
  338. X                    if (cury > 15)
  339. X                        cury = 0;
  340. X                    disp(length);
  341. X                    }
  342. X                break;
  343. X            }
  344. X        }
  345. X    move(2,0);
  346. X    clrtoeol();
  347. X}
  348. X
  349. Xfind_string()
  350. X
  351. X{
  352. X    int     stlen;
  353. X    char     string[60];
  354. X    int    found;
  355. X    int     searchpos;
  356. X
  357. X    move(2,0);
  358. X    clrtoeol();
  359. X    printw("String to search : ");
  360. X    refresh();
  361. X    echo();
  362. X    getstr(string);
  363. X    noecho();
  364. X    move(2,0);
  365. X    clrtoeol();
  366. X    printw("Searching for '%s'",string);
  367. X    found = 0;
  368. X    searchpos = 0;
  369. X    stlen = strlen(string);
  370. X    while (found == 0) {
  371. X        while ((256 - searchpos) >= stlen) {
  372. X            if (testchar(secbuf+searchpos,string,stlen))
  373. X                searchpos++;
  374. X            else {
  375. X                filpos += searchpos;
  376. X                found = 1;
  377. X                break;
  378. X                }
  379. X            }
  380. X        if (found == 0) {
  381. X            filpos += searchpos;
  382. X            searchpos = 0;
  383. X            }
  384. X        if (rdsec() == 0) {
  385. X            found = 1;    
  386. X            }
  387. X        refresh();
  388. X        }
  389. X    move (2,0);
  390. X    clrtoeol();
  391. X}
  392. X
  393. Xtestchar(buffer,string,length)
  394. Xchar    *buffer;
  395. Xchar    *string;
  396. Xint    length;
  397. X{
  398. X    register int i;
  399. X    
  400. X    i = 0;
  401. X    while ( i < length) {
  402. X        if (buffer[i] != string[i])
  403. X            break;
  404. X        i++;
  405. X        }
  406. X    if ( i == length)
  407. X        return(0);
  408. X    return(1);
  409. X}
  410. X
  411. Xset()
  412. X
  413. X{
  414. X    echo();
  415. X    move(2,0);
  416. X    clrtoeol();
  417. X    printw("New File Position : ");
  418. X    refresh();
  419. X    scanw("%lx",&filpos);
  420. X    move(2,0);
  421. X    clrtoeol();
  422. X    noecho();
  423. X}
  424. X
  425. Xdisp(length)
  426. Xint    length;
  427. X{
  428. X    int    i,j;
  429. X
  430. X    j = 0;
  431. X    mvprintw(4,0," ADDRESS    00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F      ASCII");
  432. X    mvprintw(5,0,"===============================================================================");
  433. X    for ( i = 0; i < 16; i++) {
  434. X        mvprintw(ASCY+i,0,"%08lX",filpos+i*16+j);
  435. X        for (j = 0; j < 16; j++) {
  436. X            if (( i*16 + j ) >= length) {
  437. X                clrtoeol();
  438. X                goto Disp1;
  439. X                }
  440. X            mvprintw(ASCY+i,HEXX+j*3,"%02X",secbuf[i*16+j] & 0xFF);
  441. X            }
  442. XDisp1:
  443. X        for (j = 0; j < 16; j++) {
  444. X            if (( i*16 + j ) >= length) {
  445. X                clrtobot();
  446. X                goto Disp2;
  447. X                }
  448. X            if (secbuf[i*16+j] >= ' ')
  449. X                mvprintw(ASCY+i,ASCX+j,"%c",secbuf[i*16+j]);
  450. X            else
  451. X                mvprintw(ASCY+i,ASCX+j,".");
  452. X            }
  453. X        }
  454. XDisp2:
  455. X    refresh();
  456. X}
  457. X
  458. X
  459. Xdump()
  460. X
  461. X{
  462. X    int    i,j;
  463. X
  464. X    length = rdsec();
  465. X    disp(length);
  466. X    return(length);
  467. X}
  468. X
  469. Xrdsec()
  470. X
  471. X{
  472. X    mvprintw(2,55,"Rel. Position : %08lX",filpos);
  473. X    lseek(path,filpos,0);
  474. X    length = read(path,secbuf,256);
  475. X    return(length);
  476. X}
  477. X
  478. Xwrsec()
  479. X{
  480. X    lseek(path,filpos,0);
  481. X    write(path,secbuf,length);
  482. X}
  483. X
  484. Xhelp()
  485. X
  486. X{
  487. X    WINDOW    *win;
  488. X
  489. X    win = newwin(0,0,0,0);
  490. X    wclear(win);
  491. X    mvwprintw(win,3,10,"Valid Commands are :");
  492. X    mvwprintw(win,5,15,"D - Dump one page from current file position");
  493. X    mvwprintw(win,6,15,"S - Set current file pointer");
  494. X    mvwprintw(win,7,15,"F - Find string in file (beginning from curr. position)");
  495. X    mvwprintw(win,8,15,"H - Find hex string in file (beginnig from current position)");
  496. X    mvwprintw(win,9,15,"+ - Display next sector");
  497. X    mvwprintw(win,10,15,"N - Display next sector");
  498. X    mvwprintw(win,11,15,"- - Display previous sector");
  499. X    mvwprintw(win,12,15,"P - Display previous sector");
  500. X    mvwprintw(win,19,15,"Q - Quit Program");
  501. X    mvwprintw(win,13,15,"e - Edit ASCII portion of file");
  502. X    mvwprintw(win,14,15,"E - Edit binary portion of file");
  503. X    mvwprintw(win,15,15,"W - Write modified sector back to disk");
  504. X    mvwprintw(win,20,20,"Continue with any char.");
  505. X    wrefresh(win);
  506. X    getch();
  507. X    delwin(win);
  508. X    touchwin(stdscr);
  509. X    refresh();
  510. X}
  511. X
  512. Xwerr(errstr)
  513. Xchar    *errstr;
  514. X
  515. X{
  516. X    beep();
  517. X    move(LINES-1,0);
  518. X    printw("%s",errstr);
  519. X    refresh();
  520. X    sleep(2);
  521. X    move(LINES-1,0);
  522. X    clrtoeol();
  523. X    refresh();
  524. X}
  525. X
  526. X    
  527. X
  528. Xheader(left,mid,right)
  529. Xchar    *left;
  530. Xchar    *mid;
  531. Xchar    *right;
  532. X
  533. X{
  534. X    mvprintw(0,0,"%s",left);
  535. X    mvprintw(0,79-strlen(right),"%s",right);
  536. X    mvprintw(0,40-strlen(mid)/2,"%s",mid);
  537. X}
  538. X
  539. Xdonix(sig)
  540. Xint sig;
  541. X
  542. X{
  543. X    signal(sig,donix);
  544. X}
  545. +SHAR+MARK+
  546. echo '-rw-rw-r--   1 andy     other       8369 Jun  8 10:49 bpe.c     (as sent)'
  547. chmod u=rw,g=rw,o=r bpe.c
  548. ls -l bpe.c
  549. exit 0
  550.