]>
Commit | Line | Data |
---|---|---|
bbe45369 | 1 | /* udis86 - libudis86/udis86.c |
2 | * | |
3 | * Copyright (c) 2002-2009 Vivek Thampi | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without modification, | |
7 | * are permitted provided that the following conditions are met: | |
8 | * | |
9 | * * Redistributions of source code must retain the above copyright notice, | |
10 | * this list of conditions and the following disclaimer. | |
11 | * * Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
14 | * | |
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
19 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
22 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
25 | */ | |
26 | #ifndef __UD_STANDALONE__ | |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
29 | #endif /* __UD_STANDALONE__ */ | |
30 | ||
31 | #include "input.h" | |
32 | #include "extern.h" | |
33 | ||
34 | /* ============================================================================= | |
35 | * ud_init() - Initializes ud_t object. | |
36 | * ============================================================================= | |
37 | */ | |
38 | extern void | |
39 | ud_init(struct ud* u) | |
40 | { | |
41 | memset((void*)u, 0, sizeof(struct ud)); | |
42 | ud_set_mode(u, 16); | |
43 | u->mnemonic = UD_Iinvalid; | |
44 | ud_set_pc(u, 0); | |
45 | #ifndef __UD_STANDALONE__ | |
46 | ud_set_input_file(u, stdin); | |
47 | #endif /* __UD_STANDALONE__ */ | |
48 | } | |
49 | ||
50 | /* ============================================================================= | |
51 | * ud_disassemble() - disassembles one instruction and returns the number of | |
52 | * bytes disassembled. A zero means end of disassembly. | |
53 | * ============================================================================= | |
54 | */ | |
55 | extern unsigned int | |
56 | ud_disassemble(struct ud* u) | |
57 | { | |
58 | if (ud_input_end(u)) | |
59 | return 0; | |
60 | ||
61 | ||
62 | u->insn_buffer[0] = u->insn_hexcode[0] = 0; | |
63 | ||
64 | ||
65 | if (ud_decode(u) == 0) | |
66 | return 0; | |
67 | if (u->translator) | |
68 | u->translator(u); | |
69 | return ud_insn_len(u); | |
70 | } | |
71 | ||
72 | /* ============================================================================= | |
73 | * ud_set_mode() - Set Disassemly Mode. | |
74 | * ============================================================================= | |
75 | */ | |
76 | extern void | |
77 | ud_set_mode(struct ud* u, uint8_t m) | |
78 | { | |
79 | switch(m) { | |
80 | case 16: | |
81 | case 32: | |
82 | case 64: u->dis_mode = m ; return; | |
83 | default: u->dis_mode = 16; return; | |
84 | } | |
85 | } | |
86 | ||
87 | /* ============================================================================= | |
88 | * ud_set_vendor() - Set vendor. | |
89 | * ============================================================================= | |
90 | */ | |
91 | extern void | |
92 | ud_set_vendor(struct ud* u, unsigned v) | |
93 | { | |
94 | switch(v) { | |
95 | case UD_VENDOR_INTEL: | |
96 | u->vendor = v; | |
97 | break; | |
98 | case UD_VENDOR_ANY: | |
99 | u->vendor = v; | |
100 | break; | |
101 | default: | |
102 | u->vendor = UD_VENDOR_AMD; | |
103 | } | |
104 | } | |
105 | ||
106 | /* ============================================================================= | |
107 | * ud_set_pc() - Sets code origin. | |
108 | * ============================================================================= | |
109 | */ | |
110 | extern void | |
111 | ud_set_pc(struct ud* u, uint64_t o) | |
112 | { | |
113 | u->pc = o; | |
114 | } | |
115 | ||
116 | /* ============================================================================= | |
117 | * ud_set_syntax() - Sets the output syntax. | |
118 | * ============================================================================= | |
119 | */ | |
120 | extern void | |
121 | ud_set_syntax(struct ud* u, void (*t)(struct ud*)) | |
122 | { | |
123 | u->translator = t; | |
124 | } | |
125 | ||
126 | /* ============================================================================= | |
127 | * ud_insn() - returns the disassembled instruction | |
128 | * ============================================================================= | |
129 | */ | |
130 | extern char* | |
131 | ud_insn_asm(struct ud* u) | |
132 | { | |
133 | return u->insn_buffer; | |
134 | } | |
135 | ||
136 | /* ============================================================================= | |
137 | * ud_insn_offset() - Returns the offset. | |
138 | * ============================================================================= | |
139 | */ | |
140 | extern uint64_t | |
141 | ud_insn_off(struct ud* u) | |
142 | { | |
143 | return u->insn_offset; | |
144 | } | |
145 | ||
146 | ||
147 | /* ============================================================================= | |
148 | * ud_insn_hex() - Returns hex form of disassembled instruction. | |
149 | * ============================================================================= | |
150 | */ | |
151 | extern char* | |
152 | ud_insn_hex(struct ud* u) | |
153 | { | |
154 | return u->insn_hexcode; | |
155 | } | |
156 | ||
157 | /* ============================================================================= | |
158 | * ud_insn_ptr() - Returns code disassembled. | |
159 | * ============================================================================= | |
160 | */ | |
161 | extern uint8_t* | |
162 | ud_insn_ptr(struct ud* u) | |
163 | { | |
164 | return u->inp_sess; | |
165 | } | |
166 | ||
167 | /* ============================================================================= | |
168 | * ud_insn_len() - Returns the count of bytes disassembled. | |
169 | * ============================================================================= | |
170 | */ | |
171 | extern unsigned int | |
172 | ud_insn_len(struct ud* u) | |
173 | { | |
174 | return u->inp_ctr; | |
175 | } |