]>
git.saurik.com Git - apple/javascriptcore.git/blob - disassembler/udis86/udis86_input.c
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 #include "udis86_extern.h"
31 #include "udis86_types.h"
32 #include "udis86_input.h"
34 /* -----------------------------------------------------------------------------
35 * inp_buff_hook() - Hook for buffered inputs.
36 * -----------------------------------------------------------------------------
39 inp_buff_hook(struct ud
* u
)
41 if (u
->inp_buff
< u
->inp_buff_end
)
42 return *u
->inp_buff
++;
46 #ifndef __UD_STANDALONE__
47 /* -----------------------------------------------------------------------------
48 * inp_file_hook() - Hook for FILE inputs.
49 * -----------------------------------------------------------------------------
52 inp_file_hook(struct ud
* u
)
54 return fgetc(u
->inp_file
);
56 #endif /* __UD_STANDALONE__*/
58 /* =============================================================================
59 * ud_inp_set_hook() - Sets input hook.
60 * =============================================================================
63 ud_set_input_hook(register struct ud
* u
, int (*hook
)(struct ud
*))
70 ud_set_user_opaque_data( struct ud
* u
, void * opaque
)
72 u
->user_opaque_data
= opaque
;
76 ud_get_user_opaque_data( struct ud
* u
)
78 return u
->user_opaque_data
;
81 /* =============================================================================
82 * ud_inp_set_buffer() - Set buffer as input.
83 * =============================================================================
86 ud_set_input_buffer(register struct ud
* u
, uint8_t* buf
, size_t len
)
88 u
->inp_hook
= inp_buff_hook
;
90 u
->inp_buff_end
= buf
+ len
;
94 #ifndef __UD_STANDALONE__
95 /* =============================================================================
96 * ud_input_set_file() - Set buffer as input.
97 * =============================================================================
100 ud_set_input_file(register struct ud
* u
, FILE* f
)
102 u
->inp_hook
= inp_file_hook
;
106 #endif /* __UD_STANDALONE__ */
108 /* =============================================================================
109 * ud_input_skip() - Skip n input bytes.
110 * =============================================================================
113 ud_input_skip(struct ud
* u
, size_t n
)
120 /* =============================================================================
121 * ud_input_end() - Test for end of input.
122 * =============================================================================
125 ud_input_end(struct ud
* u
)
127 return (u
->inp_curr
== u
->inp_fill
) && u
->inp_end
;
130 /* -----------------------------------------------------------------------------
131 * ud_inp_next() - Loads and returns the next byte from input.
133 * inp_curr and inp_fill are pointers to the cache. The program is written based
134 * on the property that they are 8-bits in size, and will eventually wrap around
135 * forming a circular buffer. So, the size of the cache is 256 in size, kind of
136 * unnecessary yet optimized.
138 * A buffer inp_sess stores the bytes disassembled for a single session.
139 * -----------------------------------------------------------------------------
141 extern uint8_t ud_inp_next(struct ud
* u
)
144 /* if current pointer is not upto the fill point in the
147 if ( u
->inp_curr
!= u
->inp_fill
) {
148 c
= u
->inp_cache
[ ++u
->inp_curr
];
149 /* if !end-of-input, call the input hook and get a byte */
150 } else if ( u
->inp_end
|| ( c
= u
->inp_hook( u
) ) == -1 ) {
151 /* end-of-input, mark it as an error, since the decoder,
152 * expected a byte more.
155 /* flag end of input */
159 /* increment pointers, we have a new byte. */
160 u
->inp_curr
= ++u
->inp_fill
;
161 /* add the byte to the cache */
162 u
->inp_cache
[ u
->inp_fill
] = c
;
164 /* record bytes input per decode-session. */
165 u
->inp_sess
[ u
->inp_ctr
++ ] = c
;
167 return ( uint8_t ) c
;
170 /* -----------------------------------------------------------------------------
171 * ud_inp_back() - Move back a single byte in the stream.
172 * -----------------------------------------------------------------------------
175 ud_inp_back(struct ud
* u
)
177 if ( u
->inp_ctr
> 0 ) {
183 /* -----------------------------------------------------------------------------
184 * ud_inp_peek() - Peek into the next byte in source.
185 * -----------------------------------------------------------------------------
188 ud_inp_peek(struct ud
* u
)
190 uint8_t r
= ud_inp_next(u
);
191 if ( !u
->error
) ud_inp_back(u
); /* Don't backup if there was an error */
195 /* -----------------------------------------------------------------------------
196 * ud_inp_move() - Move ahead n input bytes.
197 * -----------------------------------------------------------------------------
200 ud_inp_move(struct ud
* u
, size_t n
)
206 /*------------------------------------------------------------------------------
207 * ud_inp_uintN() - return uintN from source.
208 *------------------------------------------------------------------------------
211 ud_inp_uint8(struct ud
* u
)
213 return ud_inp_next(u
);
217 ud_inp_uint16(struct ud
* u
)
221 ret
= ud_inp_next(u
);
223 return ret
| (r
<< 8);
227 ud_inp_uint32(struct ud
* u
)
231 ret
= ud_inp_next(u
);
233 ret
= ret
| (r
<< 8);
235 ret
= ret
| (r
<< 16);
237 return ret
| (r
<< 24);
241 ud_inp_uint64(struct ud
* u
)
245 ret
= ud_inp_next(u
);
247 ret
= ret
| (r
<< 8);
249 ret
= ret
| (r
<< 16);
251 ret
= ret
| (r
<< 24);
253 ret
= ret
| (r
<< 32);
255 ret
= ret
| (r
<< 40);
257 ret
= ret
| (r
<< 48);
259 return ret
| (r
<< 56);
262 #endif // USE(UDIS86)