config.gcc (hppa*-*-linux*): Add pa/t-pa to tmake_file.
[official-gcc.git] / include / leb128.h
blob389fe7a6e79e5e68cb805a47713292aaff4fff13
1 /* Utilities for reading leb128 values.
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
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.
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.
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. */
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".
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. */
29 #ifndef LEB128_H
30 #define LEB128_H
32 /* Get a definition for inline. */
33 #include "ansidecl.h"
35 /* Get a definition for NULL, size_t. */
36 #include <stddef.h>
38 #ifdef HAVE_STDINT_H
39 #include <stdint.h>
40 #endif
41 #ifdef HAVE_INTTYPES_H
42 #include <inttypes.h>
43 #endif
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.
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. */
53 static inline size_t
54 read_uleb128_to_uint64 (const unsigned char *buf, const unsigned char *buf_end,
55 uint64_t *r)
57 const unsigned char *p = buf;
58 unsigned int shift = 0;
59 uint64_t result = 0;
60 unsigned char byte;
62 while (1)
64 if (p >= buf_end)
65 return 0;
67 byte = *p++;
68 result |= ((uint64_t) (byte & 0x7f)) << shift;
69 if ((byte & 0x80) == 0)
70 break;
71 shift += 7;
74 *r = result;
75 return p - buf;
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.
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. */
86 static inline size_t
87 read_sleb128_to_int64 (const unsigned char *buf, const unsigned char *buf_end,
88 int64_t *r)
90 const unsigned char *p = buf;
91 unsigned int shift = 0;
92 int64_t result = 0;
93 unsigned char byte;
95 while (1)
97 if (p >= buf_end)
98 return 0;
100 byte = *p++;
101 result |= ((uint64_t) (byte & 0x7f)) << shift;
102 shift += 7;
103 if ((byte & 0x80) == 0)
104 break;
106 if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
107 result |= -(((uint64_t) 1) << shift);
109 *r = result;
110 return p - buf;
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.
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. */
119 static inline size_t
120 skip_leb128 (const unsigned char *buf, const unsigned char *buf_end)
122 const unsigned char *p = buf;
123 unsigned char byte;
125 while (1)
127 if (p == buf_end)
128 return 0;
130 byte = *p++;
131 if ((byte & 0x80) == 0)
132 return p - buf;
136 #endif /* LEB128_H */