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.
33 * Initializes the input system.
36 inp_init(struct ud
*u
)
45 /* -----------------------------------------------------------------------------
46 * inp_buff_hook() - Hook for buffered inputs.
47 * -----------------------------------------------------------------------------
50 inp_buff_hook(struct ud
* u
)
52 if (u
->inp_buff
< u
->inp_buff_end
)
53 return *u
->inp_buff
++;
57 #ifndef __UD_STANDALONE__
58 /* -----------------------------------------------------------------------------
59 * inp_file_hook() - Hook for FILE inputs.
60 * -----------------------------------------------------------------------------
63 inp_file_hook(struct ud
* u
)
65 return fgetc(u
->inp_file
);
67 #endif /* __UD_STANDALONE__*/
69 /* =============================================================================
70 * ud_inp_set_hook() - Sets input hook.
71 * =============================================================================
74 ud_set_input_hook(register struct ud
* u
, int (*hook
)(struct ud
*))
80 /* =============================================================================
81 * ud_inp_set_buffer() - Set buffer as input.
82 * =============================================================================
85 ud_set_input_buffer(register struct ud
* u
, const uint8_t* buf
, size_t len
)
87 u
->inp_hook
= inp_buff_hook
;
89 u
->inp_buff_end
= buf
+ len
;
93 #ifndef __UD_STANDALONE__
94 /* =============================================================================
95 * ud_input_set_file() - Set buffer as input.
96 * =============================================================================
99 ud_set_input_file(register struct ud
* u
, FILE* f
)
101 u
->inp_hook
= inp_file_hook
;
105 #endif /* __UD_STANDALONE__ */
107 /* =============================================================================
108 * ud_input_skip() - Skip n input bytes.
109 * =============================================================================
112 ud_input_skip(struct ud
* u
, size_t n
)
119 /* =============================================================================
120 * ud_input_end() - Test for end of input.
121 * =============================================================================
124 ud_input_end(const struct ud
* u
)
126 return (u
->inp_curr
== u
->inp_fill
) && u
->inp_end
;
132 * Loads and returns the next byte from input.
134 * inp_curr and inp_fill are pointers to the cache. The program is
135 * written based on the property that they are 8-bits in size, and
136 * will eventually wrap around forming a circular buffer. So, the
137 * size of the cache is 256 in size, kind of unnecessary yet
140 * A buffer inp_sess stores the bytes disassembled for a single
144 ud_inp_next(struct ud
* u
)
147 /* if current pointer is not upto the fill point in the
150 if (u
->inp_curr
!= u
->inp_fill
) {
151 c
= u
->inp_cache
[ ++u
->inp_curr
];
152 /* if !end-of-input, call the input hook and get a byte */
153 } else if (u
->inp_end
|| (c
= u
->inp_hook(u
)) == -1) {
154 /* end-of-input, mark it as an error, since the decoder,
155 * expected a byte more.
157 UDERR(u
, "byte expected, eoi received");
158 /* flag end of input */
162 /* increment pointers, we have a new byte. */
163 u
->inp_curr
= ++u
->inp_fill
;
164 /* add the byte to the cache */
165 u
->inp_cache
[u
->inp_fill
] = c
;
167 /* record bytes input per decode-session. */
168 u
->inp_sess
[u
->inp_ctr
++] = c
;
174 vim: set ts=2 sw=2 expandtab