annotate include/leb128.h @ 145:1830386684a0

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