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