1 /* udis86 - libudis86/input.c
3 * Copyright (c) 2002-2009 Vivek Thampi
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
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.
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.
30 /* -----------------------------------------------------------------------------
31 * inp_buff_hook() - Hook for buffered inputs.
32 * -----------------------------------------------------------------------------
35 inp_buff_hook(struct ud* u)
37 if (u->inp_buff < u->inp_buff_end)
38 return *u->inp_buff++;
42 #ifndef __UD_STANDALONE__
43 /* -----------------------------------------------------------------------------
44 * inp_file_hook() - Hook for FILE inputs.
45 * -----------------------------------------------------------------------------
48 inp_file_hook(struct ud* u)
50 return fgetc(u->inp_file);
52 #endif /* __UD_STANDALONE__*/
54 /* =============================================================================
55 * ud_inp_set_hook() - Sets input hook.
56 * =============================================================================
59 ud_set_input_hook(register struct ud* u, int (*hook)(struct ud*))
66 ud_set_user_opaque_data( struct ud * u, void * opaque )
68 u->user_opaque_data = opaque;
72 ud_get_user_opaque_data( struct ud * u )
74 return u->user_opaque_data;
77 /* =============================================================================
78 * ud_inp_set_buffer() - Set buffer as input.
79 * =============================================================================
82 ud_set_input_buffer(register struct ud* u, uint8_t* buf, size_t len)
84 u->inp_hook = inp_buff_hook;
86 u->inp_buff_end = buf + len;
90 #ifndef __UD_STANDALONE__
91 /* =============================================================================
92 * ud_input_set_file() - Set buffer as input.
93 * =============================================================================
96 ud_set_input_file(register struct ud* u, FILE* f)
98 u->inp_hook = inp_file_hook;
102 #endif /* __UD_STANDALONE__ */
104 /* =============================================================================
105 * ud_input_skip() - Skip n input bytes.
106 * =============================================================================
109 ud_input_skip(struct ud* u, size_t n)
116 /* =============================================================================
117 * ud_input_end() - Test for end of input.
118 * =============================================================================
121 ud_input_end(struct ud* u)
123 return (u->inp_curr == u->inp_fill) && u->inp_end;
126 /* -----------------------------------------------------------------------------
127 * inp_next() - Loads and returns the next byte from input.
129 * inp_curr and inp_fill are pointers to the cache. The program is written based
130 * on the property that they are 8-bits in size, and will eventually wrap around
131 * forming a circular buffer. So, the size of the cache is 256 in size, kind of
132 * unnecessary yet optimized.
134 * A buffer inp_sess stores the bytes disassembled for a single session.
135 * -----------------------------------------------------------------------------
137 extern uint8_t inp_next(struct ud* u)
140 /* if current pointer is not upto the fill point in the
143 if ( u->inp_curr != u->inp_fill ) {
144 c = u->inp_cache[ ++u->inp_curr ];
145 /* if !end-of-input, call the input hook and get a byte */
146 } else if ( u->inp_end || ( c = u->inp_hook( u ) ) == -1 ) {
147 /* end-of-input, mark it as an error, since the decoder,
148 * expected a byte more.
151 /* flag end of input */
155 /* increment pointers, we have a new byte. */
156 u->inp_curr = ++u->inp_fill;
157 /* add the byte to the cache */
158 u->inp_cache[ u->inp_fill ] = c;
160 /* record bytes input per decode-session. */
161 u->inp_sess[ u->inp_ctr++ ] = c;
163 return ( uint8_t ) c;
166 /* -----------------------------------------------------------------------------
167 * inp_back() - Move back a single byte in the stream.
168 * -----------------------------------------------------------------------------
171 inp_back(struct ud* u)
173 if ( u->inp_ctr > 0 ) {
179 /* -----------------------------------------------------------------------------
180 * inp_peek() - Peek into the next byte in source.
181 * -----------------------------------------------------------------------------
184 inp_peek(struct ud* u)
186 uint8_t r = inp_next(u);
187 if ( !u->error ) inp_back(u); /* Don't backup if there was an error */
191 /* -----------------------------------------------------------------------------
192 * inp_move() - Move ahead n input bytes.
193 * -----------------------------------------------------------------------------
196 inp_move(struct ud* u, size_t n)
202 /*------------------------------------------------------------------------------
203 * inp_uintN() - return uintN from source.
204 *------------------------------------------------------------------------------
207 inp_uint8(struct ud* u)
213 inp_uint16(struct ud* u)
219 return ret | (r << 8);
223 inp_uint32(struct ud* u)
229 ret = ret | (r << 8);
231 ret = ret | (r << 16);
233 return ret | (r << 24);
237 inp_uint64(struct ud* u)
243 ret = ret | (r << 8);
245 ret = ret | (r << 16);
247 ret = ret | (r << 24);
249 ret = ret | (r << 32);
251 ret = ret | (r << 40);
253 ret = ret | (r << 48);
255 return ret | (r << 56);