annotate udis86-1.4/udcli/udcli.c @ 1:4a9dcbd9e54f

-files of 0.13 beta -fixes so that it now compiles with the current dmd version
author marton@basel.hu
date Tue, 05 Apr 2011 20:44:01 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
1 /* -----------------------------------------------------------------------------
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
2 * udcli.c - front end to udis86.
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
3 *
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
4 * Copyright (c) 2004, 2005, 2006 Vivek Mohan <vivek@sig9.com>
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
5 * All rights reserved.
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
6 * See (LICENSE)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
7 * -----------------------------------------------------------------------------
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
8 */
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
9
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
10 #include <stdlib.h>
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
11 #include <stdio.h>
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
12 #include <string.h>
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
13 #include <ctype.h>
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
14 #include "../udis86.h"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
15
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
16 #if defined(__amd64__) || defined(__x86_64__)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
17 # define FMT "l"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
18 #else
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
19 # define FMT "ll"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
20 #endif
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
21
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
22 #ifdef _WIN32
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
23 # include <io.h>
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
24 # include <fcntl.h>
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
25 #endif
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
26
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
27 /* help string */
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
28 static char help[] =
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
29 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
30 "Usage: %s [-option[s]] file\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
31 "Options:\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
32 " -16 : Set the disassembly mode to 16 bits. \n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
33 " -32 : Set the disassembly mode to 32 bits. (default)\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
34 " -64 : Set the disassembly mode to 64 bits.\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
35 " -intel : Set the output to INTEL (NASM like) syntax. (default)\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
36 " -att : Set the output to AT&T (GAS like) syntax.\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
37 " -v <v> : Set vendor. <v> = {intel, amd}.\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
38 " -o <pc> : Set the value of program counter to <pc>. (default = 0)\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
39 " -s <n> : Set the number of bytes to skip before disassembly to <n>.\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
40 " -c <n> : Set the number of bytes to disassemble to <n>.\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
41 " -x : Set the input mode to whitespace seperated 8-bit numbers in\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
42 " hexadecimal representation. Example: 0f 01 ae 00\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
43 " -noff : Do not display the offset of instructions.\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
44 " -nohex : Do not display the hexadecimal code of instructions.\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
45 " -h : Display this help message.\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
46 "\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
47 "Udcli is a front-end to the Udis86 Disassembler Library.\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
48 "http://udis86.sourceforge.net/\n"
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
49 };
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
50
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
51 FILE* fptr = NULL;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
52 uint64_t o_skip = 0;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
53 uint64_t o_count = 0;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
54 unsigned char o_do_count= 0;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
55 unsigned char o_do_off = 1;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
56 unsigned char o_do_hex = 1;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
57 unsigned char o_do_x = 0;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
58 unsigned o_vendor = UD_VENDOR_AMD;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
59
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
60 int input_hook_x(ud_t* u);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
61 int input_hook_file(ud_t* u);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
62
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
63 int main(int argc, char **argv)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
64 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
65 char *prog_path = *argv;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
66 char *s;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
67 ud_t ud_obj;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
68 int mode = 0;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
69
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
70 /* initialize */
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
71 ud_init(&ud_obj);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
72 ud_set_mode(&ud_obj, 32);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
73 ud_set_syntax(&ud_obj, UD_SYN_INTEL);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
74
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
75 #ifdef _WIN32
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
76 _setmode(_fileno(stdin), _O_BINARY);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
77 #endif
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
78
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
79 fptr = stdin;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
80
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
81 argv++;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
82
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
83 /* loop through the args */
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
84 while(--argc > 0) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
85 if (strcmp(*argv,"-16") == 0) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
86 ud_set_mode(&ud_obj, 16);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
87 mode = 16;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
88 } else if (strcmp(*argv,"-32") == 0) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
89 ud_set_mode(&ud_obj, 32);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
90 mode = 32;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
91 } else if (strcmp(*argv,"-64") == 0) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
92 ud_set_mode(&ud_obj, 64);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
93 mode = 64;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
94 } else if (strcmp(*argv,"-intel") == 0)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
95 ud_set_syntax(&ud_obj, UD_SYN_INTEL);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
96 else if (strcmp(*argv,"-att") == 0)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
97 ud_set_syntax(&ud_obj, UD_SYN_ATT);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
98 else if (strcmp(*argv,"-noff") == 0)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
99 o_do_off = 0;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
100 else if (strcmp(*argv,"-nohex") == 0)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
101 o_do_hex = 0;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
102 else if (strcmp(*argv,"-x") == 0)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
103 o_do_x = 1;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
104 else if (strcmp(*argv,"-s") == 0)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
105 if (--argc) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
106 s = *(++argv);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
107 if (sscanf(s, "%" FMT "d", &o_skip) == 0)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
108 fprintf(stderr, "Invalid value given for -s.\n");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
109 } else {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
110 fprintf(stderr, "No value given for -s.\n");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
111 printf(help, prog_path);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
112 exit(EXIT_FAILURE);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
113 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
114 else if (strcmp(*argv,"-c") == 0)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
115 if (--argc) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
116 o_do_count= 1;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
117 s = *(++argv);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
118 if (sscanf(s, "%" FMT "d", &o_count) == 0)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
119 fprintf(stderr, "Invalid value given for -c.\n");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
120 } else {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
121 fprintf(stderr, "No value given for -c.\n");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
122 printf(help, prog_path);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
123 exit(EXIT_FAILURE);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
124 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
125 else if (strcmp(*argv,"-v") == 0)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
126 if (--argc) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
127 s = *(++argv);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
128 if (*s == 'i')
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
129 ud_set_vendor(&ud_obj, UD_VENDOR_INTEL);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
130 } else {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
131 fprintf(stderr, "No value given for -v.\n");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
132 printf(help, prog_path);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
133 exit(EXIT_FAILURE);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
134 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
135 else if (strcmp(*argv,"-o") == 0) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
136 if (--argc) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
137 uint64_t pc = 0;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
138 s = *(++argv);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
139 if (sscanf(s, "%" FMT "x", &pc) == 0)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
140 fprintf(stderr, "Invalid value given for -o.\n");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
141 ud_set_pc(&ud_obj, pc);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
142 } else {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
143 fprintf(stderr, "No value given for -o.\n");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
144 printf(help, prog_path);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
145 exit(EXIT_FAILURE);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
146 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
147 } else if((*argv)[0] == '-') {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
148 fprintf(stderr, "Invalid option %s.\n", *argv);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
149 printf(help, prog_path);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
150 exit(EXIT_FAILURE);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
151 } else {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
152 static int i = 0;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
153 s = *argv;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
154 if (i) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
155 fprintf(stderr, "Multiple files specified.\n");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
156 exit(EXIT_FAILURE);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
157 } else i = 1;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
158 if ((fptr = fopen(s, "rb")) == NULL) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
159 fprintf(stderr, "Failed to open file: %s.\n", s);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
160 exit(EXIT_FAILURE);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
161 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
162 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
163 argv++;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
164 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
165
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
166 if (o_do_x)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
167 ud_set_input_hook(&ud_obj, input_hook_x);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
168 else ud_set_input_hook(&ud_obj, input_hook_file);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
169
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
170 if (o_skip) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
171 o_count += o_skip;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
172 ud_input_skip(&ud_obj, o_skip);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
173 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
174
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
175 /* disassembly loop */
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
176 while (ud_disassemble(&ud_obj)) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
177 if (o_do_off)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
178 printf("%016" FMT "x ", ud_insn_off(&ud_obj));
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
179 if (o_do_hex) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
180 char* hex1, *hex2;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
181 char c;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
182 hex1 = ud_insn_hex(&ud_obj);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
183 hex2 = hex1 + 16;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
184 c = hex1[16];
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
185 hex1[16] = 0;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
186 printf("%-16s %-24s", hex1, ud_insn_asm(&ud_obj));
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
187 hex1[16] = c;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
188 if (strlen(hex1) > 16) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
189 printf("\n");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
190 if (o_do_off)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
191 printf("%15s -", "");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
192 printf("%-16s", hex2);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
193 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
194 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
195 else printf(" %-24s", ud_insn_asm(&ud_obj));
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
196
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
197 printf("\n");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
198 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
199
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
200 exit(EXIT_SUCCESS);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
201 return 0;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
202 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
203
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
204 int input_hook_x(ud_t* u)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
205 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
206 unsigned int c, i;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
207
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
208 if (o_do_count) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
209 if (! o_count)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
210 return UD_EOI;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
211 else --o_count;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
212 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
213
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
214 i = fscanf(fptr, "%x", &c);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
215
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
216 if (i == EOF)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
217 return UD_EOI;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
218 if (i == 0) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
219 fprintf(stderr, "Error: Invalid input, should be in hexadecimal form (8-bit).\n");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
220 return UD_EOI;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
221 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
222 if (c > 0xFF)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
223 fprintf(stderr, "Warning: Casting non-8-bit input (%x), to %x.\n", c, c & 0xFF);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
224 return (int) (c & 0xFF);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
225 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
226
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
227 int input_hook_file(ud_t* u)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
228 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
229 int c;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
230
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
231 if (o_do_count) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
232 if (! o_count) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
233 return -1;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
234 } else o_count -- ;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
235 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
236
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
237 if ((c = fgetc(fptr)) == EOF)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
238 return UD_EOI;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
239 return c;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
240 }