home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / nasm097s.zip / NDISASM.C < prev    next >
C/C++ Source or Header  |  1997-11-18  |  7KB  |  279 lines

  1. /* ndisasm.c   the Netwide Disassembler main module
  2.  *
  3.  * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
  4.  * Julian Hall. All rights reserved. The software is
  5.  * redistributable under the licence given in the file "Licence"
  6.  * distributed in the NASM archive.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include <errno.h>
  14.  
  15. #include "nasm.h"
  16. #include "nasmlib.h"
  17. #include "sync.h"
  18. #include "disasm.h"
  19.  
  20. #define BPL 8                   /* bytes per line of hex dump */
  21.  
  22. static const char *help =
  23. "usage: ndisasm [-a] [-i] [-h] [-r] [-u] [-b bits] [-o origin] [-s sync...]\n"
  24. "               [-e bytes] [-k start,bytes] file\n"
  25. "   -a or -i activates auto (intelligent) sync\n"
  26. "   -u sets USE32 (32-bit mode)\n"
  27. "   -b 16 or -b 32 sets number of bits too\n"
  28. "   -h displays this text\n"
  29. "   -r displays the version number\n"
  30. "   -e skips <bytes> bytes of header\n"
  31. "   -k avoids disassembling <bytes> bytes from position <start>\n";
  32.  
  33. static void output_ins (unsigned long, unsigned char *, int, char *);
  34. static void skip (unsigned long dist, FILE *fp);
  35.  
  36. int main(int argc, char **argv) {
  37.     unsigned char buffer[INSN_MAX * 2], *p, *q;
  38.     char outbuf[256];
  39.     char *pname = *argv;
  40.     char *filename = NULL;
  41.     unsigned long nextsync, synclen, initskip = 0L;
  42.     int lenread, lendis;
  43.     int autosync = FALSE;
  44.     int bits = 16;
  45.     int eof = FALSE;
  46.     int rn_error;
  47.     long offset;
  48.     FILE *fp;
  49.  
  50.     offset = 0;
  51.     init_sync();
  52.  
  53.     while (--argc) {
  54.     char *v, *vv, *p = *++argv;
  55.     if (*p == '-') {
  56.         p++;
  57.         while (*p) switch (tolower(*p)) {
  58.           case 'a':               /* auto or intelligent sync */
  59.           case 'i':
  60.         autosync = TRUE;
  61.         p++;
  62.         break;
  63.           case 'h':
  64.         fprintf(stderr, help);
  65.         return 0;
  66.           case 'r':
  67.         fprintf(stderr, "NDISASM version " NASM_VER "\n");
  68.         return 0;
  69.           case 'u':               /* USE32 */
  70.         bits = 32;
  71.         p++;
  72.         break;
  73.           case 'b':               /* bits */
  74.         v = p[1] ? p+1 : --argc ? *++argv : NULL;
  75.         if (!v) {
  76.             fprintf(stderr, "%s: `-b' requires an argument\n", pname);
  77.             return 1;
  78.         }
  79.         if (!strcmp(v, "16"))
  80.             bits = 16;
  81.         else if (!strcmp(v, "32"))
  82.             bits = 32;
  83.         else {
  84.             fprintf(stderr, "%s: argument to `-b' should"
  85.                 " be `16' or `32'\n", pname);
  86.         }
  87.         p = "";               /* force to next argument */
  88.         break;
  89.           case 'o':               /* origin */
  90.         v = p[1] ? p+1 : --argc ? *++argv : NULL;
  91.         if (!v) {
  92.             fprintf(stderr, "%s: `-o' requires an argument\n", pname);
  93.             return 1;
  94.         }
  95.         offset = readnum (v, &rn_error);
  96.         if (rn_error) {
  97.             fprintf(stderr, "%s: `-o' requires a numeric argument\n",
  98.                 pname);
  99.             return 1;
  100.         }
  101.         p = "";               /* force to next argument */
  102.         break;
  103.           case 's':               /* sync point */
  104.         v = p[1] ? p+1 : --argc ? *++argv : NULL;
  105.         if (!v) {
  106.             fprintf(stderr, "%s: `-s' requires an argument\n", pname);
  107.             return 1;
  108.         }
  109.         add_sync (readnum (v, &rn_error), 0L);
  110.         if (rn_error) {
  111.             fprintf(stderr, "%s: `-s' requires a numeric argument\n",
  112.                 pname);
  113.             return 1;
  114.         }
  115.         p = "";               /* force to next argument */
  116.         break;
  117.           case 'e':               /* skip a header */
  118.         v = p[1] ? p+1 : --argc ? *++argv : NULL;
  119.         if (!v) {
  120.             fprintf(stderr, "%s: `-e' requires an argument\n", pname);
  121.             return 1;
  122.         }
  123.         initskip = readnum (v, &rn_error);
  124.         if (rn_error) {
  125.             fprintf(stderr, "%s: `-e' requires a numeric argument\n",
  126.                 pname);
  127.             return 1;
  128.         }
  129.         p = "";               /* force to next argument */
  130.         break;
  131.           case 'k':               /* skip a region */
  132.         v = p[1] ? p+1 : --argc ? *++argv : NULL;
  133.         if (!v) {
  134.             fprintf(stderr, "%s: `-k' requires an argument\n", pname);
  135.             return 1;
  136.         }
  137.         vv = strchr(v, ',');
  138.         if (!vv) {
  139.             fprintf(stderr, "%s: `-k' requires two numbers separated"
  140.                 " by a comma\n", pname);
  141.             return 1;
  142.         }
  143.         *vv++ = '\0';
  144.         nextsync = readnum (v, &rn_error);
  145.         if (rn_error) {
  146.             fprintf(stderr, "%s: `-k' requires numeric arguments\n",
  147.                 pname);
  148.             return 1;
  149.         }
  150.         synclen = readnum (vv, &rn_error);
  151.         if (rn_error) {
  152.             fprintf(stderr, "%s: `-k' requires numeric arguments\n",
  153.                 pname);
  154.             return 1;
  155.         }
  156.         add_sync (nextsync, synclen);
  157.         p = "";               /* force to next argument */
  158.         break;
  159.         }
  160.     } else if (!filename) {
  161.         filename = p;
  162.     } else {
  163.         fprintf(stderr, "%s: more than one filename specified\n", pname);
  164.         return 1;
  165.     }
  166.     }
  167.  
  168.     if (!filename) {
  169.     fprintf(stderr, help, pname);
  170.     return 0;
  171.     }
  172.  
  173.     fp = fopen(filename, "rb");
  174.     if (!fp) {
  175.     fprintf(stderr, "%s: unable to open `%s': %s\n",
  176.         pname, filename, strerror(errno));
  177.     return 1;
  178.     }
  179.     if (initskip > 0)
  180.     skip (initskip, fp);
  181.  
  182.     /*
  183.      * This main loop is really horrible, and wants rewriting with
  184.      * an axe. It'll stay the way it is for a while though, until I
  185.      * find the energy...
  186.      */
  187.  
  188.     p = q = buffer;
  189.     nextsync = next_sync (offset, &synclen);
  190.     do {
  191.     unsigned long to_read = buffer+sizeof(buffer)-p;
  192.     if (to_read > nextsync-offset-(p-q))
  193.         to_read = nextsync-offset-(p-q);
  194.     lenread = fread (p, 1, to_read, fp);
  195.     if (lenread == 0)
  196.         eof = TRUE;               /* help along systems with bad feof */
  197.     p += lenread;
  198.     if (offset == nextsync) {
  199.         if (synclen) {
  200.         printf("%08lX  skipping 0x%lX bytes\n", offset, synclen);
  201.         offset += synclen;
  202.         skip (synclen, fp);
  203.         }
  204.         p = q = buffer;
  205.         nextsync = next_sync (offset, &synclen);
  206.     }
  207.     while (p > q && (p - q >= INSN_MAX || lenread == 0)) {
  208.         lendis = disasm (q, outbuf, bits, offset, autosync);
  209.         if (!lendis || lendis > (p - q) ||
  210.         lendis > nextsync-offset)
  211.         lendis = eatbyte (q, outbuf);
  212.         output_ins (offset, q, lendis, outbuf);
  213.         q += lendis;
  214.         offset += lendis;
  215.     }
  216.     if (q >= buffer+INSN_MAX) {
  217.         unsigned char *r = buffer, *s = q;
  218.         int count = p - q;
  219.         while (count--)
  220.         *r++ = *s++;
  221.         p -= (q - buffer);
  222.         q = buffer;
  223.     }
  224.     } while (lenread > 0 || !(eof || feof(fp)));
  225.     fclose (fp);
  226.     return 0;
  227. }
  228.  
  229. static void output_ins (unsigned long offset, unsigned char *data,
  230.             int datalen, char *insn) {
  231.     int bytes;
  232.     printf("%08lX  ", offset);
  233.  
  234.     bytes = 0;
  235.     while (datalen > 0 && bytes < BPL) {
  236.     printf("%02X", *data++);
  237.     bytes++;
  238.     datalen--;
  239.     }
  240.  
  241.     printf("%*s%s\n", (BPL+1-bytes)*2, "", insn);
  242.  
  243.     while (datalen > 0) {
  244.     printf("         -");
  245.     bytes = 0;
  246.     while (datalen > 0 && bytes < BPL) {
  247.         printf("%02X", *data++);
  248.         bytes++;
  249.         datalen--;
  250.     }
  251.     printf("\n");
  252.     }
  253. }
  254.  
  255. /*
  256.  * Skip a certain amount of data in a file, either by seeking if
  257.  * possible, or if that fails then by reading and discarding.
  258.  */
  259. static void skip (unsigned long dist, FILE *fp) {
  260.     char buffer[256];               /* should fit on most stacks :-) */
  261.  
  262.     /*
  263.      * Got to be careful with fseek: at least one fseek I've tried
  264.      * doesn't approve of SEEK_CUR. So I'll use SEEK_SET and
  265.      * ftell... horrible but apparently necessary.
  266.      */
  267.     if (fseek (fp, dist+ftell(fp), SEEK_SET)) {
  268.     while (dist > 0) {
  269.         unsigned long len = (dist < sizeof(buffer) ?
  270.                  dist : sizeof(buffer));
  271.         if (fread (buffer, 1, len, fp) < len) {
  272.         perror("fread");
  273.         exit(1);
  274.         }
  275.         dist -= len;
  276.     }
  277.     }
  278. }
  279.