]> andersk Git - udis86.git/blob - libudis86/udis86.c
Wait to include system headers until we know they are wanted.
[udis86.git] / libudis86 / udis86.c
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
27 #include "input.h"
28 #include "extern.h"
29
30 #ifndef __UD_STANDALONE__
31 # include <stdlib.h>
32 # include <string.h>
33 #endif /* __UD_STANDALONE__ */
34
35 /* =============================================================================
36  * ud_init() - Initializes ud_t object.
37  * =============================================================================
38  */
39 extern void 
40 ud_init(struct ud* u)
41 {
42   memset((void*)u, 0, sizeof(struct ud));
43   ud_set_mode(u, 16);
44   u->mnemonic = UD_Iinvalid;
45   ud_set_pc(u, 0);
46 #ifndef __UD_STANDALONE__
47   ud_set_input_file(u, stdin);
48 #endif /* __UD_STANDALONE__ */
49 }
50
51 /* =============================================================================
52  * ud_disassemble() - disassembles one instruction and returns the number of 
53  * bytes disassembled. A zero means end of disassembly.
54  * =============================================================================
55  */
56 extern unsigned int
57 ud_disassemble(struct ud* u)
58 {
59   if (ud_input_end(u))
60         return 0;
61
62  
63   u->insn_buffer[0] = u->insn_hexcode[0] = 0;
64
65  
66   if (ud_decode(u) == 0)
67         return 0;
68   if (u->translator)
69         u->translator(u);
70   return ud_insn_len(u);
71 }
72
73 /* =============================================================================
74  * ud_set_mode() - Set Disassemly Mode.
75  * =============================================================================
76  */
77 extern void 
78 ud_set_mode(struct ud* u, uint8_t m)
79 {
80   switch(m) {
81         case 16:
82         case 32:
83         case 64: u->dis_mode = m ; return;
84         default: u->dis_mode = 16; return;
85   }
86 }
87
88 /* =============================================================================
89  * ud_set_vendor() - Set vendor.
90  * =============================================================================
91  */
92 extern void 
93 ud_set_vendor(struct ud* u, unsigned v)
94 {
95   switch(v) {
96         case UD_VENDOR_INTEL:
97                 u->vendor = v;
98                 break;
99         case UD_VENDOR_ANY:
100                 u->vendor = v;
101                 break;
102         default:
103                 u->vendor = UD_VENDOR_AMD;
104   }
105 }
106
107 /* =============================================================================
108  * ud_set_pc() - Sets code origin. 
109  * =============================================================================
110  */
111 extern void 
112 ud_set_pc(struct ud* u, uint64_t o)
113 {
114   u->pc = o;
115 }
116
117 /* =============================================================================
118  * ud_set_syntax() - Sets the output syntax.
119  * =============================================================================
120  */
121 extern void 
122 ud_set_syntax(struct ud* u, void (*t)(struct ud*))
123 {
124   u->translator = t;
125 }
126
127 /* =============================================================================
128  * ud_insn() - returns the disassembled instruction
129  * =============================================================================
130  */
131 extern char* 
132 ud_insn_asm(struct ud* u) 
133 {
134   return u->insn_buffer;
135 }
136
137 /* =============================================================================
138  * ud_insn_offset() - Returns the offset.
139  * =============================================================================
140  */
141 extern uint64_t
142 ud_insn_off(struct ud* u) 
143 {
144   return u->insn_offset;
145 }
146
147
148 /* =============================================================================
149  * ud_insn_hex() - Returns hex form of disassembled instruction.
150  * =============================================================================
151  */
152 extern char* 
153 ud_insn_hex(struct ud* u) 
154 {
155   return u->insn_hexcode;
156 }
157
158 /* =============================================================================
159  * ud_insn_ptr() - Returns code disassembled.
160  * =============================================================================
161  */
162 extern uint8_t* 
163 ud_insn_ptr(struct ud* u) 
164 {
165   return u->inp_sess;
166 }
167
168 /* =============================================================================
169  * ud_insn_len() - Returns the count of bytes disassembled.
170  * =============================================================================
171  */
172 extern unsigned int 
173 ud_insn_len(struct ud* u) 
174 {
175   return u->inp_ctr;
176 }
This page took 0.048832 seconds and 5 git commands to generate.