Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /* -----------------------------------------------------------------------------
  2.  * input.c
  3.  *
  4.  * Copyright (c) 2004, 2005, 2006, Vivek Mohan <vivek@sig9.com>
  5.  * All rights reserved. See LICENSE
  6.  * -----------------------------------------------------------------------------
  7.  */
  8. #include "extern.h"
  9. #include "types.h"
  10. #include "input.h"
  11.  
  12. /* -----------------------------------------------------------------------------
  13.  * inp_buff_hook() - Hook for buffered inputs.
  14.  * -----------------------------------------------------------------------------
  15.  */
  16. static int
  17. inp_buff_hook(struct ud* u)
  18. {
  19.   if (u->inp_buff < u->inp_buff_end)
  20.         return *u->inp_buff++;
  21.   else  return -1;
  22. }
  23.  
  24. #ifndef __UD_STANDALONE__
  25. /* -----------------------------------------------------------------------------
  26.  * inp_file_hook() - Hook for FILE inputs.
  27.  * -----------------------------------------------------------------------------
  28.  */
  29. static int
  30. inp_file_hook(struct ud* u)
  31. {
  32.   return fgetc(u->inp_file);
  33. }
  34. #endif /* __UD_STANDALONE__*/
  35.  
  36. /* =============================================================================
  37.  * ud_inp_set_hook() - Sets input hook.
  38.  * =============================================================================
  39.  */
  40. extern void
  41. ud_set_input_hook(register struct ud* u, int (*hook)(struct ud*))
  42. {
  43.   u->inp_hook = hook;
  44.   inp_init(u);
  45. }
  46.  
  47. /* =============================================================================
  48.  * ud_inp_set_buffer() - Set buffer as input.
  49.  * =============================================================================
  50.  */
  51. extern void
  52. ud_set_input_buffer(register struct ud* u, uint8_t* buf, size_t len)
  53. {
  54.   u->inp_hook = inp_buff_hook;
  55.   u->inp_buff = buf;
  56.   u->inp_buff_end = buf + len;
  57.   inp_init(u);
  58. }
  59.  
  60. #ifndef __UD_STANDALONE__
  61. /* =============================================================================
  62.  * ud_input_set_file() - Set buffer as input.
  63.  * =============================================================================
  64.  */
  65. extern void
  66. ud_set_input_file(register struct ud* u, FILE* f)
  67. {
  68.   u->inp_hook = inp_file_hook;
  69.   u->inp_file = f;
  70.   inp_init(u);
  71. }
  72. #endif /* __UD_STANDALONE__ */
  73.  
  74. /* =============================================================================
  75.  * ud_input_skip() - Skip n input bytes.
  76.  * =============================================================================
  77.  */
  78. extern void
  79. ud_input_skip(struct ud* u, size_t n)
  80. {
  81.   while (n--) {
  82.         u->inp_hook(u);
  83.   }
  84. }
  85.  
  86. /* =============================================================================
  87.  * ud_input_end() - Test for end of input.
  88.  * =============================================================================
  89.  */
  90. extern int
  91. ud_input_end(struct ud* u)
  92. {
  93.   return (u->inp_curr == u->inp_fill) && u->inp_end;
  94. }
  95.  
  96. /* -----------------------------------------------------------------------------
  97.  * inp_next() - Loads and returns the next byte from input.
  98.  *
  99.  * inp_curr and inp_fill are pointers to the cache. The program is written based
  100.  * on the property that they are 8-bits in size, and will eventually wrap around
  101.  * forming a circular buffer. So, the size of the cache is 256 in size, kind of
  102.  * unnecessary yet optimized.
  103.  *
  104.  * A buffer inp_sess stores the bytes disassembled for a single session.
  105.  * -----------------------------------------------------------------------------
  106.  */
  107. extern uint8_t inp_next(struct ud* u)
  108. {
  109.   int c = -1;
  110.   /* if current pointer is not upto the fill point in the
  111.    * input cache.
  112.    */
  113.   if ( u->inp_curr != u->inp_fill ) {
  114.         c = u->inp_cache[ ++u->inp_curr ];
  115.   /* if !end-of-input, call the input hook and get a byte */
  116.   } else if ( u->inp_end || ( c = u->inp_hook( u ) ) == -1 ) {
  117.         /* end-of-input, mark it as an error, since the decoder,
  118.          * expected a byte more.
  119.          */
  120.         u->error = 1;
  121.         /* flag end of input */
  122.         u->inp_end = 1;
  123.         return 0;
  124.   } else {
  125.         /* increment pointers, we have a new byte.  */
  126.         u->inp_curr = ++u->inp_fill;
  127.         /* add the byte to the cache */
  128.         u->inp_cache[ u->inp_fill ] = c;
  129.   }
  130.   /* record bytes input per decode-session. */
  131.   u->inp_sess[ u->inp_ctr++ ] = c;
  132.   /* return byte */
  133.   return ( uint8_t ) c;
  134. }
  135.  
  136. /* -----------------------------------------------------------------------------
  137.  * inp_back() - Move back a single byte in the stream.
  138.  * -----------------------------------------------------------------------------
  139.  */
  140. extern void
  141. inp_back(struct ud* u)
  142. {
  143.   if ( u->inp_ctr > 0 ) {
  144.         --u->inp_curr;
  145.         --u->inp_ctr;
  146.   }
  147. }
  148.  
  149. /* -----------------------------------------------------------------------------
  150.  * inp_peek() - Peek into the next byte in source.
  151.  * -----------------------------------------------------------------------------
  152.  */
  153. extern uint8_t
  154. inp_peek(struct ud* u)
  155. {
  156.   uint8_t r = inp_next(u);
  157.   if ( !u->error ) inp_back(u); /* Don't backup if there was an error */
  158.   return r;
  159. }
  160.  
  161. /* -----------------------------------------------------------------------------
  162.  * inp_move() - Move ahead n input bytes.
  163.  * -----------------------------------------------------------------------------
  164.  */
  165. extern void
  166. inp_move(struct ud* u, size_t n)
  167. {
  168.   while (n--)
  169.         inp_next(u);
  170. }
  171.  
  172. /*------------------------------------------------------------------------------
  173.  *  inp_uintN() - return uintN from source.
  174.  *------------------------------------------------------------------------------
  175.  */
  176. extern uint8_t
  177. inp_uint8(struct ud* u)
  178. {
  179.   return inp_next(u);
  180. }
  181.  
  182. extern uint16_t
  183. inp_uint16(struct ud* u)
  184. {
  185.   uint16_t r, ret;
  186.  
  187.   ret = inp_next(u);
  188.   r = inp_next(u);
  189.   return ret | (r << 8);
  190. }
  191.  
  192. extern uint32_t
  193. inp_uint32(struct ud* u)
  194. {
  195.   uint32_t r, ret;
  196.  
  197.   ret = inp_next(u);
  198.   r = inp_next(u);
  199.   ret = ret | (r << 8);
  200.   r = inp_next(u);
  201.   ret = ret | (r << 16);
  202.   r = inp_next(u);
  203.   return ret | (r << 24);
  204. }
  205.  
  206. extern uint64_t
  207. inp_uint64(struct ud* u)
  208. {
  209.   uint64_t r, ret;
  210.  
  211.   ret = inp_next(u);
  212.   r = inp_next(u);
  213.   ret = ret | (r << 8);
  214.   r = inp_next(u);
  215.   ret = ret | (r << 16);
  216.   r = inp_next(u);
  217.   ret = ret | (r << 24);
  218.   r = inp_next(u);
  219.   ret = ret | (r << 32);
  220.   r = inp_next(u);
  221.   ret = ret | (r << 40);
  222.   r = inp_next(u);
  223.   ret = ret | (r << 48);
  224.   r = inp_next(u);
  225.   return ret | (r << 56);
  226. }
  227.