home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2013 < prev    next >
Internet Message Format  |  1990-12-28  |  22KB

  1. From: davidsen@sixhub.UUCP (Wm E. Davidsen Jr)
  2. Newsgroups: alt.sources
  3. Subject: bpe - binary patch editor
  4. Message-ID: <2188@sixhub.UUCP>
  5. Date: 29 Oct 90 02:40:03 GMT
  6.  
  7.  
  8.   I'm posting this because a number of people have asked me to mail it
  9. and I can't. I will post it to comp.sources.misc when the new moderator
  10. is in place.
  11.  
  12.   The DOS version has been on cbip in the past.
  13.  
  14. #!/bin/sh
  15. # shar:    Shell Archiver  (v1.27)
  16. #
  17. #    Run the following text with /bin/sh to create:
  18. #      bpe.c
  19. #      hexsrch.c
  20. #      makefile
  21. #      readme
  22. #      bpe.1
  23. #
  24. sed 's/^X//' << 'SHAR_EOF' > bpe.c &&
  25. X/***************************************************************************
  26. X
  27. XVersion History:
  28. X
  29. XVer.No    Comment                        By
  30. X===========================================================================
  31. X1.0     first version (seems to do things right)       andy@mssx
  32. X1.1    some bugs fixed (tks to Maarten)        andy@mssx.uucp
  33. X                            maart@cs.vu.nl
  34. X1.2    works with NOKEYPAD in all cases        davidsen@crdos1.uucp
  35. X    make + and - move in 2 line increments
  36. X    add 'L' look for hex byte
  37. X    end edit with ^E, ^C gives signal in BSD
  38. X    / remembers the last search string, can repeat
  39. X1.3    integrate 1.2 (davidsen) and 1.2 (andy/jon)    davidsen@crdos1.uucp
  40. X    find hex string code added, author        jon@joblab
  41. X
  42. XBUG REPORTS:
  43. X============
  44. X    - The offset count in the first column is wrong,
  45. X      except for the first line; it's 0x10 too high.
  46. X                                (fixed)
  47. X    - The test in disp() if a char is printable, fails
  48. X      for chars >= 0177.
  49. X                                (fixed)
  50. X
  51. X    - Help message for 'H' incorrect            (fixed)
  52. X
  53. X
  54. XI declare this program as freeware, i.e. you may duplicate it, give it
  55. Xto your friends, and transfer it to any machine you like, as long as
  56. Xyou do not change or delete the build in copyright message.
  57. X
  58. X    Andreas Pleschutznig
  59. X    Teichhofweg 2
  60. X    8044 Graz
  61. X    Austria 
  62. X
  63. XComments and bug reports to:
  64. X    andy@mssx    (mcvax!tuvie!mssx!andy)
  65. X
  66. X
  67. X*****************************************************************************/
  68. X
  69. X#include <stdio.h>
  70. X#include <curses.h>
  71. X#include <fcntl.h>
  72. X#include <signal.h>
  73. X#include <ctype.h>
  74. X
  75. X/* this is needed for MS-DOS compilation */
  76. X#ifndef    O_BINARY
  77. X#define O_BINARY    0
  78. X#endif
  79. X
  80. X#define        CTRL(c)        ((c) & 037)
  81. X#define        DEL        '\177'
  82. X
  83. X#ifdef NOKEYPAD
  84. X#define        KEY_LEFT    CTRL('H')
  85. X#define        KEY_DOWN    CTRL('J')
  86. X#define        KEY_UP        CTRL('K')
  87. X#define        KEY_RIGHT    CTRL('L')
  88. X#ifndef O_RDWR
  89. X#define        O_RDWR        2
  90. X#endif
  91. X#define        cbreak()    crmode()
  92. X#define        beep()        putchar(7)
  93. X#endif
  94. X
  95. X#define    BELL    0x07
  96. X#define ASCX    63
  97. X#define ASCY    6
  98. X#define HEXY    6
  99. X#define HEXX    12
  100. X
  101. Xint     path;                   /* path to file to patch */
  102. Xlong    filpos;            /* position in file */
  103. Xunsigned char secbuf[256];    /* sector read buffer */
  104. X
  105. Xint     donix();                /* default signal handling routine */
  106. Xchar    filename[60];
  107. Xint    length;            /* length of read sector */
  108. X
  109. Xmain(argc,argv)
  110. Xint argc;
  111. Xchar    **argv;
  112. X{
  113. X    if (argc != 2) {
  114. X        fprintf(stderr,"Usage: %s filename\n",argv[0]);
  115. X        exit(1);
  116. X    }
  117. X    if (( path = open(argv[1],O_RDWR|O_BINARY)) == -1) {
  118. X        fprintf(stderr,"%s: Can't open '%s'\n",argv[0],argv[1]);
  119. X        exit(1);
  120. X    }
  121. X    sprintf(filename,"%s",argv[1]);
  122. X    initscr();
  123. X    refresh();
  124. X    signal(SIGINT,donix);
  125. X#ifdef    SIGQUIT
  126. X    signal(SIGQUIT,donix);
  127. X#endif    /* no QUIT in MS-DOS */
  128. X    cbreak();                       /* set single char input */
  129. X    noecho();
  130. X#ifndef NOKEYPAD
  131. X    keypad(stdscr,TRUE);
  132. X#endif
  133. X    filpos = 0;            /* set global position to 0 */
  134. X    length = 0;
  135. X    command();
  136. X    clear();
  137. X    refresh();
  138. X    endwin();
  139. X    close(path);
  140. X}
  141. X
  142. Xcommand()
  143. X{
  144. X    int inval;
  145. X
  146. X    header("BPE Version 1.3",filename,"(C) 1988 MSS Graz");
  147. X    inval = 0;
  148. X    while ((inval != 'q') && (inval != 'Q')) {
  149. X        move(2,0);
  150. X        mvprintw(2,0,"COMMAND : ");
  151. X        refresh();
  152. X        inval = getch();
  153. X        switch (inval) {
  154. X            case 'q':
  155. X            case 'Q':
  156. X                break;
  157. X            case 'h':
  158. X            case 'H':
  159. X                find_hex();
  160. X                dump();
  161. X                break;
  162. X            case '?':
  163. X                help();
  164. X                break;
  165. X            case 'f':
  166. X            case 'F':
  167. X            case '/':
  168. X                find_string();
  169. X                dump();
  170. X                break;
  171. X            case '+':
  172. X                filpos += 32;
  173. X                dump();
  174. X                break;
  175. X            case 'n':
  176. X            case 'N':
  177. X                filpos += 256;
  178. X                dump();
  179. X                break;
  180. X            case '-':
  181. X                filpos -= 32;
  182. X                if (filpos < 0)
  183. X                    filpos = 0;
  184. X                dump();
  185. X                break;
  186. X            case 'p':
  187. X            case 'P':
  188. X                filpos -= 256;
  189. X                if (filpos < 0)
  190. X                    filpos = 0;
  191. X                dump();
  192. X                break;
  193. X            case 'D':
  194. X            case 'd':
  195. X                dump();
  196. X                break;
  197. X            case 's':
  198. X            case 'S':
  199. X                set();
  200. X                dump();
  201. X                break;
  202. X            case 'e':
  203. X                edit_ascii();
  204. X                break;
  205. X            case 'E':
  206. X                edit_hex();
  207. X                break;
  208. X            case 'w':
  209. X            case 'W':
  210. X                wrsec();
  211. X                break;
  212. X            default:
  213. X                werr("Invalid Command !");
  214. X        }
  215. X    }
  216. X}
  217. X
  218. Xedit_ascii()
  219. X{
  220. X    int inval = 0;
  221. X    int cury,curx;
  222. X
  223. X    if (length == 0)
  224. X        length = dump();
  225. X    move(2,0);
  226. X    clrtoeol();
  227. X#ifdef NOKEYPAD
  228. X    printw("Left ^H - down ^J - up ^K - right ^L - end editing with ^E");
  229. X#else
  230. X    printw("End editing with ^E");
  231. X#endif
  232. X    curx = cury = 0;
  233. X    while (inval != CTRL('E')) {
  234. X        move(ASCY+cury,ASCX+curx);
  235. X        refresh();
  236. X        inval = getch();
  237. X        switch (inval) {
  238. X            case KEY_UP:
  239. X                if (cury)
  240. X                    cury--;
  241. X                else
  242. X                    beep();
  243. X                break;
  244. X            case KEY_DOWN:
  245. X                if (cury < 15)
  246. X                    cury++;
  247. X                else
  248. X                    beep();
  249. X                break;
  250. X            case KEY_RIGHT:
  251. X                if (curx < 15)
  252. X                    curx++;
  253. X                else
  254. X                    beep();
  255. X                break;
  256. X            case KEY_LEFT:
  257. X                if (curx)
  258. X                    curx--;
  259. X                else
  260. X                    beep();
  261. X                break;
  262. X            default:
  263. X                if ((inval >= 0x20) && (inval <= 0x7e)) {
  264. X                    secbuf[cury*16+curx] =inval;
  265. X                    curx++;
  266. X                    if (curx > 15) {
  267. X                        curx=0;
  268. X                        cury++;
  269. X                        }
  270. X                    if (cury > 15)
  271. X                        cury = 0;
  272. X                    disp(length);
  273. X                    }
  274. X                break;
  275. X        }
  276. X    }
  277. X    move(2,0);
  278. X    clrtoeol();
  279. X}
  280. X
  281. Xgethex(cury,curx)
  282. Xint    cury,curx;
  283. X{
  284. X    int val;
  285. X    int inlen;
  286. X    int value;
  287. X    char *hexvals = "0123456789ABCDEF";
  288. X    char *strchr(), *wkptr;
  289. X
  290. X    inlen = 0;
  291. X    while (inlen < 2) {
  292. X        val = getch();
  293. X        if (val > 0xff) return(val);
  294. X        if (islower(val)) val = toupper(val);
  295. X        wkptr = strchr(hexvals, val);
  296. X        if (wkptr == NULL) return(val|0x2000);
  297. X        else val = wkptr - hexvals;
  298. X
  299. X        switch (inlen) {
  300. X        case 0:
  301. X            value = val << 4;
  302. X            secbuf[cury*16+curx] = value;
  303. X            disp(length);
  304. X            move(HEXY+cury,HEXX+curx*3+1);
  305. X            refresh();
  306. X            break;
  307. X        case 1:
  308. X            value += val ;
  309. X            break;
  310. X        }
  311. X        inlen++;
  312. X    }
  313. X    return(value);
  314. X}
  315. X
  316. Xedit_hex()
  317. X{
  318. X    int inval = 0;
  319. X    int cury,curx;
  320. X
  321. X    if (length == 0)
  322. X        length = dump();
  323. X    move(2,0);
  324. X    clrtoeol();
  325. X#ifdef NOKEYPAD
  326. X    printw("Left ^H - down ^J - up ^K - right ^L - end editing with ^E");
  327. X#else
  328. X    printw("End editing with ^E");
  329. X#endif
  330. X    curx = cury = 0;
  331. X    while (inval != -1) {
  332. X        move(HEXY+cury,HEXX+curx*3);
  333. X        refresh();
  334. X        inval = gethex(cury,curx);
  335. X        if (inval > 0xff) {
  336. X            /* this is control information */
  337. X            if (inval > 0x1fff)
  338. X                inval &= 0xff;
  339. X            switch (inval) {
  340. X            case KEY_UP:
  341. X                if (cury)
  342. X                    cury--;
  343. X                else
  344. X                    beep();
  345. X                break;
  346. X            case KEY_DOWN:
  347. X                if (cury < 15)
  348. X                    cury++;
  349. X                else
  350. X                    beep();
  351. X                break;
  352. X            case KEY_RIGHT:
  353. X                if (curx < 15)
  354. X                    curx++;
  355. X                else
  356. X                    beep();
  357. X                break;
  358. X            case KEY_LEFT:
  359. X                if (curx)
  360. X                    curx--;
  361. X                else
  362. X                    beep();
  363. X                break;
  364. X            case CTRL('E'):
  365. X                inval = -1;
  366. X                break;
  367. X            }
  368. X        }
  369. X        else {
  370. X            secbuf[cury*16+curx] =inval;
  371. X            curx++;
  372. X            if (curx > 15) {
  373. X                curx=0;
  374. X                cury++;
  375. X            }
  376. X            if (cury > 15)
  377. X                cury = 0;
  378. X            disp(length);
  379. X        }
  380. X    }
  381. X    move(2,0);
  382. X    clrtoeol();
  383. X}
  384. X
  385. Xfind_string()
  386. X{
  387. X    int     stlen;
  388. X    char     string[60];
  389. Xstatic    char    laststring[60];
  390. Xstatic    int    re_search = 0, old_filpos;
  391. X    int    found;
  392. X    int     searchpos;
  393. X
  394. X    move(2,0);
  395. X    clrtoeol();
  396. X    printw("String to search : ");
  397. X    refresh();
  398. X    echo();
  399. X    getstr(string);
  400. X    if (strlen(string) == 0) {
  401. X        if (strlen(laststring) > 0)
  402. X            strcpy(string, laststring);
  403. X        else {
  404. X            beep();
  405. X            return;
  406. X        }
  407. X    }
  408. X    else {
  409. X        strcpy(laststring, string);
  410. X    }
  411. X    noecho();
  412. X    move(2,0);
  413. X    clrtoeol();
  414. X    printw("Searching for '%s'",string);
  415. X    found = 0;
  416. X    searchpos = (filpos == old_filpos ? re_search : 0);
  417. X    stlen = strlen(string);
  418. X    while (found == 0) {
  419. X        while ((256 - searchpos) >= stlen) {
  420. X            if (testchar(secbuf+searchpos,string,stlen))
  421. X                searchpos++;
  422. X            else {
  423. X                filpos += searchpos;
  424. X                old_filpos = filpos;
  425. X#ifdef    CLINES
  426. X                if (filpos >= 16*CLINES) {
  427. X                    filpos -= 16*CLINES;
  428. X                }
  429. X                else {
  430. X                    filpos = 0;
  431. X                }
  432. X#endif    /* context lines */
  433. X#ifdef    ALLIGN
  434. X                filpos &= ~0xf;
  435. X#endif    /* allign */
  436. X                re_search = old_filpos - filpos + 1;
  437. X                old_filpos = filpos;
  438. X                found = 1;
  439. X                break;
  440. X            }
  441. X        }
  442. X        if (found == 0) {
  443. X            filpos += searchpos;
  444. X            searchpos = 0;
  445. X        }
  446. X        if (rdsec() == 0) {
  447. X            found = 1;    
  448. X        }
  449. X        refresh();
  450. X    }
  451. X    move(2, 0);
  452. X    clrtoeol();
  453. X}
  454. X
  455. Xtestchar(buffer,string,length)
  456. Xchar    *buffer;
  457. Xchar    *string;
  458. Xint    length;
  459. X{
  460. X    register int i;
  461. X    
  462. X    i = 0;
  463. X    while ( i < length) {
  464. X        if (buffer[i] != string[i])
  465. X            break;
  466. X        i++;
  467. X    }
  468. X    if ( i == length)
  469. X        return(0);
  470. X    return(1);
  471. X}
  472. X
  473. Xset()
  474. X{
  475. X    echo();
  476. X    move(2,0);
  477. X    clrtoeol();
  478. X    printw("New File Position : ");
  479. X    refresh();
  480. X    scanw("%lx",&filpos);
  481. X    move(2,0);
  482. X    clrtoeol();
  483. X    noecho();
  484. X}
  485. X
  486. Xdisp(length)
  487. Xint    length;
  488. X{
  489. X    int    i, j, c;
  490. X
  491. X    /* output headings adjusted for the starting position */
  492. X    mvprintw(4,0, " ADDRESS   ");
  493. X    for (i = 0, j = filpos & 0x0f; i < 16; ++i) {
  494. X        printw(" 0%c", "0123456789ABCDEF"[j]);
  495. X        j = (j + 1) % 16;
  496. X    }
  497. X    printw("      ASCII");
  498. X
  499. X    mvprintw(5,0, "%s%s",
  500. X        "=======================================",
  501. X        "========================================");
  502. X
  503. X    for ( i = 0; i < 16; i++) {
  504. X        mvprintw(ASCY+i,0,"%08lX",filpos+i*16);
  505. X        for (j = 0; j < 16; j++) {
  506. X            if (( i*16 + j ) >= length) {
  507. X                clrtoeol();
  508. X                goto Disp1;
  509. X            }
  510. X            mvprintw(ASCY+i,HEXX+j*3,"%02X",secbuf[i*16+j] & 0xFF);
  511. X        }
  512. XDisp1:
  513. X        for (j = 0; j < 16; j++) {
  514. X            if (( i*16 + j ) >= length) {
  515. X                clrtobot();
  516. X                goto Disp2;
  517. X            }
  518. X            if (' ' <= (c = secbuf[i * 16 + j]) && c < DEL)
  519. X                mvprintw(ASCY+i,ASCX+j,"%c", c);
  520. X            else
  521. X                mvprintw(ASCY+i,ASCX+j,".");
  522. X        }
  523. X    }
  524. XDisp2:
  525. X    refresh();
  526. X}
  527. X
  528. X
  529. Xdump()
  530. X{
  531. X    int    i,j;
  532. X
  533. X    length = rdsec();
  534. X    disp(length);
  535. X    return(length);
  536. X}
  537. X
  538. Xrdsec()
  539. X{
  540. X    mvprintw(2,55,"Rel. Position : %08lX",filpos);
  541. X    refresh();
  542. X    lseek(path,filpos,0);
  543. X    length = read(path,secbuf,256);
  544. X    return(length);
  545. X}
  546. X
  547. Xwrsec()
  548. X{
  549. X    lseek(path,filpos,0);
  550. X    write(path,secbuf,length);
  551. X}
  552. X
  553. Xhelp()
  554. X{
  555. X    WINDOW    *win;
  556. X
  557. X    win = newwin(0,0,0,0);
  558. X    wclear(win);
  559. X    mvwprintw(win,3,10,"Valid Commands are :");
  560. X    mvwprintw(win,5,15,"D - Dump one page from current file position");
  561. X    mvwprintw(win,6,15,"S - Set current file pointer");
  562. X    mvwprintw(win,7,15,
  563. X        "F - Find string in file (beginning from curr. position)");
  564. X    mvwprintw(win,8,15,
  565. X        "H - locate hex bytes in file (beginning from curr. position)");
  566. X    mvwprintw(win,9,15,"N - Display next sector");
  567. X    mvwprintw(win,10,15,"P - Display previous sector");
  568. X    mvwprintw(win,11,15,"+ - Scroll forward 2 lines");
  569. X    mvwprintw(win,12,15,"- - Scroll back 2 lines");
  570. X    mvwprintw(win,13,15,"e - Edit ASCII portion of file");
  571. X    mvwprintw(win,14,15,"E - Edit binary portion of file");
  572. X    mvwprintw(win,15,15,"W - Write modified sector back to disk");
  573. X    mvwprintw(win,16,15,"Q - Quit Program");
  574. X    mvwprintw(win,18,20,"Continue with any char.");
  575. X    wrefresh(win);
  576. X    getch();
  577. X    delwin(win);
  578. X    touchwin(stdscr);
  579. X    refresh();
  580. X}
  581. X
  582. Xwerr(errstr)
  583. Xchar    *errstr;
  584. X
  585. X{
  586. X    beep();
  587. X    move(LINES-1,0);
  588. X    printw("%s",errstr);
  589. X    refresh();
  590. X    sleep(2);
  591. X    move(LINES-1,0);
  592. X    clrtoeol();
  593. X    refresh();
  594. X}
  595. X
  596. X    
  597. X
  598. Xheader(left,mid,right)
  599. Xchar    *left;
  600. Xchar    *mid;
  601. Xchar    *right;
  602. X
  603. X{
  604. X    mvprintw(0,0,"%s",left);
  605. X    mvprintw(0,79-strlen(right),"%s",right);
  606. X    mvprintw(0,40-strlen(mid)/2,"%s",mid);
  607. X}
  608. X
  609. Xdonix(sig)
  610. Xint sig;
  611. X
  612. X{
  613. X    signal(sig,donix);
  614. X}
  615. X
  616. SHAR_EOF
  617. chmod 0664 bpe.c || echo "restore of bpe.c fails"
  618. sed 's/^X//' << 'SHAR_EOF' > hexsrch.c &&
  619. X
  620. X/* Added by Jon LaBadie jon@jonlab.UUCP
  621. X   to implement the H (hex search) option */
  622. X
  623. X#include    <stdio.h>
  624. X#include    <ctype.h>
  625. X#include    <curses.h>
  626. X#define        beep()        putchar(7)
  627. X
  628. X/*
  629. X** hex_2_byte returns the integer value of a byte
  630. X** represented by the two characters passed to it.
  631. X** For example, passed an '8' and an 'A', it will
  632. X** return 170 (8 * 16 + 10).  Returns -1 on any error.
  633. X*/
  634. X
  635. Xint
  636. Xhex_2_byte(a, b)
  637. Xchar a, b;
  638. X{
  639. X    int v = 0;
  640. X
  641. X    if (!isxdigit(a) || !isxdigit(b))
  642. X        return -1;
  643. X    
  644. X    a = toupper(a);
  645. X    b = toupper(b);
  646. X
  647. X    if (isdigit(a))
  648. X        v = (a - '0') * 16;
  649. X    else
  650. X        v = (a - 'A' + 10) * 16;
  651. X    
  652. X    if (isdigit(b))
  653. X        v += (b - '0');
  654. X    else
  655. X        v += (b - 'A' + 10);
  656. X    
  657. X    return v;
  658. X}
  659. X
  660. X
  661. X/* Take two strings as arguments.
  662. X** First is a sequence of hex digit pairs.
  663. X** Each pair is to be converted into the
  664. X** equivalent unsigned 1 byte value and
  665. X** stored in the second array.
  666. X*/
  667. X
  668. Xint
  669. Xcvt_str(s, h)
  670. Xchar *s;
  671. Xunsigned char *h;
  672. X{
  673. X    int c;
  674. X    int len = 0;
  675. X
  676. X    while(*s != '\0')
  677. X    {
  678. X        if (*(s+1) == '\0')
  679. X            return -1;
  680. X        c = hex_2_byte(*s, *(s+1));
  681. X        if (c >= 0)
  682. X            *h++ = c;
  683. X        else
  684. X            return -1;
  685. X        len++;
  686. X        s += 2;
  687. X    }
  688. X    *h = '\0';
  689. X    return len;
  690. X}
  691. X
  692. Xfind_hex()
  693. X{
  694. X    int     stlen;
  695. X    char     string[60];
  696. X    char    *strstart;
  697. Xstatic    char    laststring[60];
  698. Xstatic    int    re_search = 0, old_filpos;
  699. X    unsigned char hexstr[30];
  700. X    unsigned char *up;
  701. X    int    found;
  702. X    int     searchpos;
  703. X    extern    char secbuf[];
  704. X    extern    long filpos;
  705. X
  706. X    move(2,0);
  707. X    clrtoeol();
  708. X    printw("HEX string to search for: ");
  709. X    refresh();
  710. X    echo();
  711. X    string[0] = '0';
  712. X    getstr(&string[1]);
  713. X    if (strlen(string) == 1) {
  714. X        if (strlen(laststring) > 0)
  715. X            strcpy(string, laststring);
  716. X        else {
  717. X            beep();
  718. X            return;
  719. X        }
  720. X    }
  721. X    else {
  722. X        strcpy(laststring, string);
  723. X    }
  724. X    noecho();
  725. X    move(2,0);
  726. X    clrtoeol();
  727. X    if (strlen(string) % 2)
  728. X        strstart = &string[1];
  729. X    else
  730. X        strstart = &string[0];
  731. X    stlen = cvt_str(strstart, hexstr);
  732. X    if (stlen < 0)
  733. X    {
  734. X        printw("Invalid Hex string: %s", strstart);
  735. X        refresh();
  736. X        sleep(1);
  737. X        return;
  738. X    }
  739. X    printw("Searching for '%s'", strstart);
  740. X    refresh();
  741. X    found = 0;
  742. X    searchpos = 1;
  743. X    while (found == 0) {
  744. X        while ((256 - searchpos) >= stlen) {
  745. X            if (secbuf[searchpos] != hexstr[0] || memcmp(secbuf + searchpos + 1, hexstr + 1, stlen - 1))
  746. X                searchpos++;
  747. X            else {
  748. X                filpos += searchpos;
  749. X#ifdef    CLINES
  750. X                if (filpos >= 16*CLINES) {
  751. X                    filpos -= 16*CLINES;
  752. X                }
  753. X                else {
  754. X                    filpos = 0;
  755. X                }
  756. X#endif    /* context lines */
  757. X#ifdef    ALLIGN
  758. X                filpos &= ~0xf;
  759. X#endif    /* allign */
  760. X                re_search = old_filpos - filpos + 1;
  761. X                old_filpos = filpos;
  762. X                found = 1;
  763. X                break;
  764. X                }
  765. X            }
  766. X        if (found == 0) {
  767. X            filpos += searchpos;
  768. X            searchpos = 0;
  769. X            }
  770. X        if (rdsec() == 0) {
  771. X            found = 1;    
  772. X            }
  773. X        refresh();
  774. X        }
  775. X    move (2,0);
  776. X    clrtoeol();
  777. X}
  778. SHAR_EOF
  779. chmod 0644 hexsrch.c || echo "restore of hexsrch.c fails"
  780. sed 's/^X//' << 'SHAR_EOF' > makefile &&
  781. X#________________ Start customizing here ________________
  782. X#
  783. X# If your Terminals and your curses lib supports keypad()
  784. X# comment out the next line. You probably need it for BSD.
  785. XNKEYPAD     = -DNOKEYPAD
  786. X
  787. X# if you want the search operations to show found patterns in context,
  788. X# set CLINES to the number of line of data to display before the pattern.
  789. X# else comment out the next line
  790. X# CLINES      = -DCLINES=1
  791. X
  792. X# if you want the search operations to start the display on a mod 16 
  793. X# boundary, leave the next line, else comment out
  794. X#ALLIGN        = -DALLIGN
  795. X
  796. X# libraries to make curses work on your machine. Probably just curses
  797. X# for V.2 and later, as is for BSD. You could try termlib instead of 
  798. X# termcap if the termcap library is not available.
  799. XLIBES       = -lcurses -ltermcap
  800. X
  801. X# local compilation and link options needed, such a 286 model selection, etc
  802. XLOCAL       =
  803. X#
  804. X# ________________ Stop customizing here ________________
  805. X
  806. X
  807. XCFLAGS = -O $(NKEYPAD) $(CLINES) $(ALLIGN)
  808. XOBJS = bpe.o hexsrch.o
  809. XSRCS = bpe.c hexsrch.c
  810. XEXEC = bpe
  811. X
  812. X# for making a shar file
  813. XSHARLIST = $(SRCS) makefile readme bpe.1
  814. XSHAR = shar
  815. X
  816. X$(EXEC): $(OBJS)
  817. X    $(CC) -o $(EXEC) $(LOCAL) $(OBJS) $(LIBES)
  818. X
  819. X#$(OBJS): $(SRCS)
  820. X#    $(CC) -c $(CFLAGS) $(LOCAL) $(SRCS)
  821. X# special makerules here, portable
  822. X.c.o:
  823. X    $(CC) -c $(CFLAGS) $(LOCAL) $?
  824. X
  825. Xshar:        bpe.shar
  826. Xbpe.shar:    $(SHARLIST)
  827. X    $(SHAR) $(SHARLIST) > bpe.shar
  828. X
  829. Xarc:        bpe.arc
  830. Xbpe.arc:    bpe.exe bpe.doc
  831. X    arc a bpe $?
  832. X    rm bpe.doc
  833. X
  834. Xbpe.doc:    bpe.1
  835. X    nroff -man -Tlp bpe.1 | col > bpe.doc
  836. SHAR_EOF
  837. chmod 0644 makefile || echo "restore of makefile fails"
  838. sed 's/^X//' << 'SHAR_EOF' > readme &&
  839. XThis is Version 1.1 for the BPE Program I wrote some time ago,
  840. Xthere has been some bug fixes(tks to maart@cs.vu.nl).
  841. X
  842. XTo generate the new Version of the BPE program type
  843. X
  844. X    ed <bpe.diffs bpe.c
  845. X
  846. Xthen look at the new makefile, and correct the NKEYPAD value to
  847. Xsuit for your System. 
  848. X
  849. X
  850. X
  851. XAny further comments and bug reports are welcomed.
  852. X
  853. X
  854. X
  855. X
  856. XAndreas Pleschutznig
  857. XMicro Systems Software
  858. XGraz, Austria
  859. Xandy@mssx
  860. X________________________________________________________________
  861. X
  862. XComments on v1.2:
  863. X
  864. X  The changes to work without KEYPAD defined failed on my system. After
  865. Xmaking changes to get them functional, I rewrote part of the makefile. I
  866. Xwanted to have found strings in context, so I added an option to leave
  867. Xsome data before the found string. I also added an option to start the
  868. Xdisplay after find on a 16 byte boundary to allow matching with other
  869. Xdumps.
  870. X
  871. X  Then I added search for a byte, since I wanted that several times
  872. Xwhile debugging, and finally after trying to move it to a BSD system I
  873. Xchanged the "end edit" key to ^E instead of ^C, since ^C is normally the
  874. Xinterrupt character on most of the systems I use, and signals are
  875. Xignored. The CTRL macro wasn't quite correct, and I cleaned up the
  876. Xgethex procedure, which didn't quite work on my systems.
  877. X
  878. X  Oh yes, I wrote a man page for it, too. I still have a wish list if
  879. Xanyone feels ambitious.
  880. X
  881. X        bill davidsen, 3/1/89  (davidsen@crdos1.uucp)
  882. X________________________________________________________________
  883. X
  884. XI sent my changes back to the original author, and he sent me *his*
  885. Xversion 1.2 with other enhancements over 1.1. He asked me to merge them,
  886. Xand after looking at his changes and mine I added the code to find a hex
  887. Xstring to my code. We had fixed many of the same things and my makefile
  888. Xseemed a bit cleaner. I also added a number of shell scripts to perform
  889. Xmakes, including MS-DOS. The man page has been updated to reflect the
  890. Xcombined version.
  891. X
  892. X        bill davidsen (davidsen@crdos1.uucp)
  893. X
  894. SHAR_EOF
  895. chmod 0644 readme || echo "restore of readme fails"
  896. sed 's/^X//' << 'SHAR_EOF' > bpe.1 &&
  897. X.TH BPE 1 LOCAL
  898. X.SH NAME
  899. X\fBbpe\fR - examine and patch binary files
  900. X.SH SYNOPSIS
  901. X\fBbpe\fR allows a file to be searched and modified in either ASCII or
  902. Xhexadecimal. Each buffer is displayed in both modes.
  903. X.SH DESCRIPTION
  904. Xbpe binary.file
  905. X.ne 15
  906. X.SS Commands
  907. X.nf
  908. XD - Dump one page from current file position
  909. XS - Set current file pointer
  910. XF - Find string in file (beginning from curr. position)
  911. X/ - Same as F
  912. XH - locate hex bytes in file (beginning from curr. position)
  913. XN - Display next sector
  914. XP - Display previous sector
  915. X+ - Scroll forward 2 lines
  916. X- - Scroll back 2 lines
  917. Xe - Edit ASCII portion of file
  918. XE - Edit binary portion of file
  919. XW - Write modified sector back to disk
  920. XQ - Quit Program
  921. X? - help
  922. X.fi
  923. X.ne 5
  924. X.SS Editing a file
  925. XEnter an editing mode by typing 'e' (for ASCII edit) or 'E' for hex
  926. Xedit. The cursor may be moved either by the arrow keys or the vi-style
  927. X^H, ^J, ^K, ^L keys, depending on compilation options. Place the cursor
  928. Xon the byte to change and type either a printing ASCII character or two
  929. Xdigit hex value. Exit either edit mode by typing ^E. When you are
  930. Xsatisfied with your editing, enter the W command to write the modified
  931. Xportion of the file back to disk.
  932. X.ne 5
  933. X.SS Searching for data
  934. XYou may search for hex data by giving the h command and entering a
  935. Xstring of hex digits. The search procedes forward from the current
  936. Xlocation until a matching string is found. The display is adjusted to
  937. Xput the first byte at the top of the screen (but see configuration
  938. Xoptions). If no pattern is specified the previous pattern is used.
  939. X.P
  940. XTo search for a string, enter the F (for find) command, or the vi style
  941. X/ command. At the prompt type in a string and press return. The search
  942. Xprocedes as with a hex byte search. If you wish to repeat a string
  943. Xsearch, enter the F command again and press return. This will repeat the
  944. Xsearch for the previous string. If the starting position has not been
  945. Xadjusted the search will start one character past the location of the
  946. Xlast occurence found.
  947. X.SS Configuration options
  948. XThe makefile contains configuration option, identifiable by comments.
  949. XThese are all at the top of the makefile. If your curses supports KEYPAD
  950. Xdefinition for your terminal, you may use the cursor keys on the keypad.
  951. XThis is generally only found in SysV systems with termlib versions of
  952. Xcurses. If you don't have that feature the NOKEYPAD option will allow
  953. Xuse of vi style cursor keys.
  954. X.P
  955. XWhen searching, if you prefer to see the pattern in context you may set
  956. Xthe CLINES to the number of lines of context displayed before the
  957. Xpattern found. This may cause problems doing hex searches. In addition,
  958. Xif you want the display to start on a 16 byte boundary, to match od or
  959. Xhd output, you may enable the ALLIGN option.
  960. X.SH WARNINGS
  961. XIllegal commands are flagged as such.
  962. X.SH SEE ALSO
  963. Xod, hd, possibly adb.
  964. X.SH DIAGNOSTICS
  965. X.SH LIMITATIONS
  966. XIf the l or L commands are used with no pattern specified before a
  967. Xpattern has been specified the value zero will be used.
  968. X.ne 15
  969. X.SH AUTHOR
  970. X.nf
  971. XOriginal author:
  972. X    Andreas Pleschutznig
  973. X    Teichhofweg 2
  974. X    8044 Graz
  975. X    Austria 
  976. XContributions by:
  977. X        maart@cs.vu.nl
  978. Xv1.2 features added by:
  979. X    Bill Davidsen, Box 8 KW-C206, Schenectady NY 12345
  980. X
  981. XComments and bug reports to:
  982. X    andy@mssx    (mcvax!tuvie!mssx!andy)
  983. XBugs in features documented as being added in v1.2 to
  984. X    davidsen@crdos1.uucp    (uunet!crd.ge.com!davidsen)
  985. X.fi
  986. SHAR_EOF
  987. chmod 0644 bpe.1 || echo "restore of bpe.1 fails"
  988. exit 0
  989.  
  990. -- 
  991. bill davidsen - davidsen@sixhub.uucp (uunet!crdgw1!sixhub!davidsen)
  992.     sysop *IX BBS and Public Access UNIX
  993.     moderator of comp.binaries.ibm.pc and 80386 mailing list
  994. "Stupidity, like virtue, is its own reward" -me
  995.