Home | History | Annotate | Download | only in include
      1 /* Utilities for reading leb128 values.
      2    Copyright (C) 2012 Free Software Foundation, Inc.
      3 
      4 This file is part of the libiberty library.
      5 Libiberty is free software; you can redistribute it and/or
      6 modify it under the terms of the GNU Library General Public
      7 License as published by the Free Software Foundation; either
      8 version 2 of the License, or (at your option) any later version.
      9 
     10 Libiberty is distributed in the hope that it will be useful,
     11 but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13 Library General Public License for more details.
     14 
     15 You should have received a copy of the GNU Library General Public
     16 License along with libiberty; see the file COPYING.LIB.  If not, write
     17 to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
     18 Boston, MA 02110-1301, USA.  */
     19 
     20 /* The functions defined here can be speed critical.
     21    Since they are all pretty small we keep things simple and just define
     22    them all as "static inline".
     23 
     24    WARNING: This file is used by GDB which is stuck at C90. :-(
     25    Though it can use stdint.h, inttypes.h.
     26    Therefore if you want to add support for "long long" you need
     27    to wrap it in #ifdef CC_HAS_LONG_LONG.  */
     28 
     29 #ifndef LEB128_H
     30 #define LEB128_H
     31 
     32 /* Get a definition for inline.  */
     33 #include "ansidecl.h"
     34 
     35 /* Get a definition for NULL, size_t.  */
     36 #include <stddef.h>
     37 
     38 #ifdef HAVE_STDINT_H
     39 #include <stdint.h>
     40 #endif
     41 #ifdef HAVE_INTTYPES_H
     42 #include <inttypes.h>
     43 #endif
     44 
     45 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
     46    by R, and return the number of bytes read.
     47    If we read off the end of the buffer, zero is returned,
     48    and nothing is stored in R.
     49 
     50    Note: The result is an int instead of a pointer to the next byte to be
     51    read to avoid const-vs-non-const problems.  */
     52 
     53 static inline size_t
     54 read_uleb128_to_uint64 (const unsigned char *buf, const unsigned char *buf_end,
     55 			uint64_t *r)
     56 {
     57   const unsigned char *p = buf;
     58   unsigned int shift = 0;
     59   uint64_t result = 0;
     60   unsigned char byte;
     61 
     62   while (1)
     63     {
     64       if (p >= buf_end)
     65 	return 0;
     66 
     67       byte = *p++;
     68       result |= ((uint64_t) (byte & 0x7f)) << shift;
     69       if ((byte & 0x80) == 0)
     70 	break;
     71       shift += 7;
     72     }
     73 
     74   *r = result;
     75   return p - buf;
     76 }
     77 
     78 /* Decode the signed LEB128 constant at BUF into the variable pointed to
     79    by R, and return the number of bytes read.
     80    If we read off the end of the buffer, zero is returned,
     81    and nothing is stored in R.
     82 
     83    Note: The result is an int instead of a pointer to the next byte to be
     84    read to avoid const-vs-non-const problems.  */
     85 
     86 static inline size_t
     87 read_sleb128_to_int64 (const unsigned char *buf, const unsigned char *buf_end,
     88 		       int64_t *r)
     89 {
     90   const unsigned char *p = buf;
     91   unsigned int shift = 0;
     92   int64_t result = 0;
     93   unsigned char byte;
     94 
     95   while (1)
     96     {
     97       if (p >= buf_end)
     98 	return 0;
     99 
    100       byte = *p++;
    101       result |= ((uint64_t) (byte & 0x7f)) << shift;
    102       shift += 7;
    103       if ((byte & 0x80) == 0)
    104 	break;
    105     }
    106   if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
    107     result |= -(((uint64_t) 1) << shift);
    108 
    109   *r = result;
    110   return p - buf;
    111 }
    112 
    113 /* Return the number of bytes to read to skip past an LEB128 number in BUF.
    114    If the end isn't found before reaching BUF_END, return zero.
    115 
    116    Note: The result is an int instead of a pointer to the next byte to be
    117    read to avoid const-vs-non-const problems.  */
    118 
    119 static inline size_t
    120 skip_leb128 (const unsigned char *buf, const unsigned char *buf_end)
    121 {
    122   const unsigned char *p = buf;
    123   unsigned char byte;
    124 
    125   while (1)
    126     {
    127       if (p == buf_end)
    128 	return 0;
    129 
    130       byte = *p++;
    131       if ((byte & 0x80) == 0)
    132 	return p - buf;
    133     }
    134 }
    135 
    136 #endif /* LEB128_H */
    137