/src/binutils-gdb/include/leb128.h
Line | Count | Source |
1 | | /* Utilities for reading leb128 values. |
2 | | Copyright (C) 2012-2026 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 | 0 | { |
57 | 0 | const unsigned char *p = buf; |
58 | 0 | unsigned int shift = 0; |
59 | 0 | uint64_t result = 0; |
60 | 0 | unsigned char byte; |
61 | 0 |
|
62 | 0 | while (1) |
63 | 0 | { |
64 | 0 | if (p >= buf_end) |
65 | 0 | return 0; |
66 | 0 |
|
67 | 0 | byte = *p++; |
68 | 0 | result |= ((uint64_t) (byte & 0x7f)) << shift; |
69 | 0 | if ((byte & 0x80) == 0) |
70 | 0 | break; |
71 | 0 | shift += 7; |
72 | 0 | } |
73 | 0 |
|
74 | 0 | *r = result; |
75 | 0 | return p - buf; |
76 | 0 | } |
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 | 0 | { |
90 | 0 | const unsigned char *p = buf; |
91 | 0 | unsigned int shift = 0; |
92 | 0 | int64_t result = 0; |
93 | 0 | unsigned char byte; |
94 | |
|
95 | 0 | while (1) |
96 | 0 | { |
97 | 0 | if (p >= buf_end) |
98 | 0 | return 0; |
99 | | |
100 | 0 | byte = *p++; |
101 | 0 | result |= ((uint64_t) (byte & 0x7f)) << shift; |
102 | 0 | shift += 7; |
103 | 0 | if ((byte & 0x80) == 0) |
104 | 0 | break; |
105 | 0 | } |
106 | 0 | if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0) |
107 | 0 | result |= -(((uint64_t) 1) << shift); |
108 | |
|
109 | 0 | *r = result; |
110 | 0 | return p - buf; |
111 | 0 | } |
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 | 0 | { |
122 | 0 | const unsigned char *p = buf; |
123 | 0 | unsigned char byte; |
124 | 0 |
|
125 | 0 | while (1) |
126 | 0 | { |
127 | 0 | if (p == buf_end) |
128 | 0 | return 0; |
129 | 0 |
|
130 | 0 | byte = *p++; |
131 | 0 | if ((byte & 0x80) == 0) |
132 | 0 | return p - buf; |
133 | 0 | } |
134 | 0 | } |
135 | | |
136 | | #endif /* LEB128_H */ |