]> andersk Git - udis86.git/blame - libudis86/input.c
change manual docbook public dtd
[udis86.git] / libudis86 / input.c
CommitLineData
bbe45369 1/* udis86 - libudis86/input.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#include "extern.h"
27#include "types.h"
28#include "input.h"
29
30/* -----------------------------------------------------------------------------
31 * inp_buff_hook() - Hook for buffered inputs.
32 * -----------------------------------------------------------------------------
33 */
34static int
35inp_buff_hook(struct ud* u)
36{
37 if (u->inp_buff < u->inp_buff_end)
38 return *u->inp_buff++;
39 else return -1;
40}
41
42#ifndef __UD_STANDALONE__
43/* -----------------------------------------------------------------------------
44 * inp_file_hook() - Hook for FILE inputs.
45 * -----------------------------------------------------------------------------
46 */
47static int
48inp_file_hook(struct ud* u)
49{
50 return fgetc(u->inp_file);
51}
52#endif /* __UD_STANDALONE__*/
53
54/* =============================================================================
55 * ud_inp_set_hook() - Sets input hook.
56 * =============================================================================
57 */
58extern void
59ud_set_input_hook(register struct ud* u, int (*hook)(struct ud*))
60{
61 u->inp_hook = hook;
62 inp_init(u);
63}
64
65extern void
66ud_set_user_opaque_data( struct ud * u, void * opaque )
67{
68 u->user_opaque_data = opaque;
69}
70
71extern void *
72ud_get_user_opaque_data( struct ud * u )
73{
74 return u->user_opaque_data;
75}
76
77/* =============================================================================
78 * ud_inp_set_buffer() - Set buffer as input.
79 * =============================================================================
80 */
81extern void
82ud_set_input_buffer(register struct ud* u, uint8_t* buf, size_t len)
83{
84 u->inp_hook = inp_buff_hook;
85 u->inp_buff = buf;
86 u->inp_buff_end = buf + len;
87 inp_init(u);
88}
89
90#ifndef __UD_STANDALONE__
91/* =============================================================================
92 * ud_input_set_file() - Set buffer as input.
93 * =============================================================================
94 */
95extern void
96ud_set_input_file(register struct ud* u, FILE* f)
97{
98 u->inp_hook = inp_file_hook;
99 u->inp_file = f;
100 inp_init(u);
101}
102#endif /* __UD_STANDALONE__ */
103
104/* =============================================================================
105 * ud_input_skip() - Skip n input bytes.
106 * =============================================================================
107 */
108extern void
109ud_input_skip(struct ud* u, size_t n)
110{
111 while (n--) {
112 u->inp_hook(u);
113 }
114}
115
116/* =============================================================================
117 * ud_input_end() - Test for end of input.
118 * =============================================================================
119 */
120extern int
121ud_input_end(struct ud* u)
122{
123 return (u->inp_curr == u->inp_fill) && u->inp_end;
124}
125
126/* -----------------------------------------------------------------------------
127 * inp_next() - Loads and returns the next byte from input.
128 *
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.
133 *
134 * A buffer inp_sess stores the bytes disassembled for a single session.
135 * -----------------------------------------------------------------------------
136 */
137extern uint8_t inp_next(struct ud* u)
138{
139 int c = -1;
140 /* if current pointer is not upto the fill point in the
141 * input cache.
142 */
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.
149 */
150 u->error = 1;
151 /* flag end of input */
152 u->inp_end = 1;
153 return 0;
154 } else {
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;
159 }
160 /* record bytes input per decode-session. */
161 u->inp_sess[ u->inp_ctr++ ] = c;
162 /* return byte */
163 return ( uint8_t ) c;
164}
165
166/* -----------------------------------------------------------------------------
167 * inp_back() - Move back a single byte in the stream.
168 * -----------------------------------------------------------------------------
169 */
170extern void
171inp_back(struct ud* u)
172{
173 if ( u->inp_ctr > 0 ) {
174 --u->inp_curr;
175 --u->inp_ctr;
176 }
177}
178
179/* -----------------------------------------------------------------------------
180 * inp_peek() - Peek into the next byte in source.
181 * -----------------------------------------------------------------------------
182 */
183extern uint8_t
184inp_peek(struct ud* u)
185{
186 uint8_t r = inp_next(u);
187 if ( !u->error ) inp_back(u); /* Don't backup if there was an error */
188 return r;
189}
190
191/* -----------------------------------------------------------------------------
192 * inp_move() - Move ahead n input bytes.
193 * -----------------------------------------------------------------------------
194 */
195extern void
196inp_move(struct ud* u, size_t n)
197{
198 while (n--)
199 inp_next(u);
200}
201
202/*------------------------------------------------------------------------------
203 * inp_uintN() - return uintN from source.
204 *------------------------------------------------------------------------------
205 */
206extern uint8_t
207inp_uint8(struct ud* u)
208{
209 return inp_next(u);
210}
211
212extern uint16_t
213inp_uint16(struct ud* u)
214{
215 uint16_t r, ret;
216
217 ret = inp_next(u);
218 r = inp_next(u);
219 return ret | (r << 8);
220}
221
222extern uint32_t
223inp_uint32(struct ud* u)
224{
225 uint32_t r, ret;
226
227 ret = inp_next(u);
228 r = inp_next(u);
229 ret = ret | (r << 8);
230 r = inp_next(u);
231 ret = ret | (r << 16);
232 r = inp_next(u);
233 return ret | (r << 24);
234}
235
236extern uint64_t
237inp_uint64(struct ud* u)
238{
239 uint64_t r, ret;
240
241 ret = inp_next(u);
242 r = inp_next(u);
243 ret = ret | (r << 8);
244 r = inp_next(u);
245 ret = ret | (r << 16);
246 r = inp_next(u);
247 ret = ret | (r << 24);
248 r = inp_next(u);
249 ret = ret | (r << 32);
250 r = inp_next(u);
251 ret = ret | (r << 40);
252 r = inp_next(u);
253 ret = ret | (r << 48);
254 r = inp_next(u);
255 return ret | (r << 56);
256}
This page took 0.084569 seconds and 5 git commands to generate.