1 /* Utilities for reading leb128 values.
2 Copyright (C) 2012-2021 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. */
32 /* Get a definition for inline. */
35 /* Get a definition for NULL, size_t. */
41 #ifdef HAVE_INTTYPES_H
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. */
54 read_uleb128_to_uint64 (const unsigned char *buf
, const unsigned char *buf_end
,
57 const unsigned char *p
= buf
;
58 unsigned int shift
= 0;
68 result
|= ((uint64_t) (byte
& 0x7f)) << shift
;
69 if ((byte
& 0x80) == 0)
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. */
87 read_sleb128_to_int64 (const unsigned char *buf
, const unsigned char *buf_end
,
90 const unsigned char *p
= buf
;
91 unsigned int shift
= 0;
101 result
|= ((uint64_t) (byte
& 0x7f)) << shift
;
103 if ((byte
& 0x80) == 0)
106 if (shift
< (sizeof (*r
) * 8) && (byte
& 0x40) != 0)
107 result
|= -(((uint64_t) 1) << shift
);
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. */
120 skip_leb128 (const unsigned char *buf
, const unsigned char *buf_end
)
122 const unsigned char *p
= buf
;
131 if ((byte
& 0x80) == 0)
136 #endif /* LEB128_H */