1 /* ndisasm.c the Netwide Disassembler main module
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.
20 #define BPL 8 /* bytes per line of hex dump */
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";
33 static void output_ins (unsigned long, unsigned char *, int, char *);
34 static void skip (unsigned long dist, FILE *fp);
36 int main(int argc, char **argv)
38 unsigned char buffer[INSN_MAX * 2], *p, *q;
41 char *filename = NULL;
42 unsigned long nextsync, synclen, initskip = 0L;
55 char *v, *vv, *p = *++argv;
56 if (*p == '-' && p[1]) {
58 while (*p) switch (tolower(*p)) {
59 case 'a': /* auto or intelligent sync */
65 fprintf(stderr, help);
68 fprintf(stderr, "NDISASM version " NASM_VER "\n");
75 v = p[1] ? p+1 : --argc ? *++argv : NULL;
77 fprintf(stderr, "%s: `-b' requires an argument\n", pname);
82 else if (!strcmp(v, "32"))
85 fprintf(stderr, "%s: argument to `-b' should"
86 " be `16' or `32'\n", pname);
88 p = ""; /* force to next argument */
90 case 'o': /* origin */
91 v = p[1] ? p+1 : --argc ? *++argv : NULL;
93 fprintf(stderr, "%s: `-o' requires an argument\n", pname);
96 offset = readnum (v, &rn_error);
98 fprintf(stderr, "%s: `-o' requires a numeric argument\n",
102 p = ""; /* force to next argument */
104 case 's': /* sync point */
105 v = p[1] ? p+1 : --argc ? *++argv : NULL;
107 fprintf(stderr, "%s: `-s' requires an argument\n", pname);
110 add_sync (readnum (v, &rn_error), 0L);
112 fprintf(stderr, "%s: `-s' requires a numeric argument\n",
116 p = ""; /* force to next argument */
118 case 'e': /* skip a header */
119 v = p[1] ? p+1 : --argc ? *++argv : NULL;
121 fprintf(stderr, "%s: `-e' requires an argument\n", pname);
124 initskip = readnum (v, &rn_error);
126 fprintf(stderr, "%s: `-e' requires a numeric argument\n",
130 p = ""; /* force to next argument */
132 case 'k': /* skip a region */
133 v = p[1] ? p+1 : --argc ? *++argv : NULL;
135 fprintf(stderr, "%s: `-k' requires an argument\n", pname);
140 fprintf(stderr, "%s: `-k' requires two numbers separated"
141 " by a comma\n", pname);
145 nextsync = readnum (v, &rn_error);
147 fprintf(stderr, "%s: `-k' requires numeric arguments\n",
151 synclen = readnum (vv, &rn_error);
153 fprintf(stderr, "%s: `-k' requires numeric arguments\n",
157 add_sync (nextsync, synclen);
158 p = ""; /* force to next argument */
161 } else if (!filename) {
164 fprintf(stderr, "%s: more than one filename specified\n", pname);
170 fprintf(stderr, help, pname);
174 if (strcmp(filename, "-")) {
175 fp = fopen(filename, "rb");
177 fprintf(stderr, "%s: unable to open `%s': %s\n",
178 pname, filename, strerror(errno));
188 * This main loop is really horrible, and wants rewriting with
189 * an axe. It'll stay the way it is for a while though, until I
194 nextsync = next_sync (offset, &synclen);
196 unsigned long to_read = buffer+sizeof(buffer)-p;
197 if (to_read > nextsync-offset-(p-q))
198 to_read = nextsync-offset-(p-q);
200 lenread = fread (p, 1, to_read, fp);
202 eof = TRUE; /* help along systems with bad feof */
206 if (offset == nextsync) {
208 printf("%08lX skipping 0x%lX bytes\n", offset, synclen);
213 nextsync = next_sync (offset, &synclen);
215 while (p > q && (p - q >= INSN_MAX || lenread == 0)) {
216 lendis = disasm (q, outbuf, bits, offset, autosync);
217 if (!lendis || lendis > (p - q) ||
218 lendis > nextsync-offset)
219 lendis = eatbyte (q, outbuf);
220 output_ins (offset, q, lendis, outbuf);
224 if (q >= buffer+INSN_MAX) {
225 unsigned char *r = buffer, *s = q;
232 } while (lenread > 0 || !(eof || feof(fp)));
240 static void output_ins (unsigned long offset, unsigned char *data,
241 int datalen, char *insn)
244 printf("%08lX ", offset);
247 while (datalen > 0 && bytes < BPL) {
248 printf("%02X", *data++);
253 printf("%*s%s\n", (BPL+1-bytes)*2, "", insn);
255 while (datalen > 0) {
258 while (datalen > 0 && bytes < BPL) {
259 printf("%02X", *data++);
268 * Skip a certain amount of data in a file, either by seeking if
269 * possible, or if that fails then by reading and discarding.
271 static void skip (unsigned long dist, FILE *fp)
273 char buffer[256]; /* should fit on most stacks :-) */
276 * Got to be careful with fseek: at least one fseek I've tried
277 * doesn't approve of SEEK_CUR. So I'll use SEEK_SET and
278 * ftell... horrible but apparently necessary.
280 if (fseek (fp, dist+ftell(fp), SEEK_SET)) {
282 unsigned long len = (dist < sizeof(buffer) ?
283 dist : sizeof(buffer));
284 if (fread (buffer, 1, len, fp) < len) {