Commit | Line | Data |
---|---|---|
67bf71fe DD |
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 | #ifndef LEB128_H | |
25 | #define LEB128_H | |
26 | ||
27 | /* Get a definition for inline. */ | |
28 | #include "ansidecl.h" | |
29 | ||
30 | /* Get a definition for NULL, size_t. */ | |
31 | #include <stddef.h> | |
32 | ||
33 | /* Decode the unsigned LEB128 constant at BUF into the variable pointed to | |
34 | by R, and return the number of bytes read. | |
35 | If we read off the end of the buffer, zero is returned, | |
36 | and nothing is stored in R. | |
37 | ||
38 | Note: The result is an int instead of a pointer to the next byte to be | |
39 | read to avoid const-vs-non-const problems. */ | |
40 | ||
41 | static inline size_t | |
42 | read_uleb128_to_ull (const unsigned char *buf, const unsigned char *buf_end, | |
43 | unsigned long long *r) | |
44 | { | |
45 | const unsigned char *p = buf; | |
46 | unsigned int shift = 0; | |
47 | unsigned long long result = 0; | |
48 | unsigned char byte; | |
49 | ||
50 | while (1) | |
51 | { | |
52 | if (p >= buf_end) | |
53 | return 0; | |
54 | ||
55 | byte = *p++; | |
56 | result |= ((unsigned long long) (byte & 0x7f)) << shift; | |
57 | if ((byte & 0x80) == 0) | |
58 | break; | |
59 | shift += 7; | |
60 | } | |
61 | ||
62 | *r = result; | |
63 | return p - buf; | |
64 | } | |
65 | ||
66 | /* Decode the signed LEB128 constant at BUF into the variable pointed to | |
67 | by R, and return the number of bytes read. | |
68 | If we read off the end of the buffer, zero is returned, | |
69 | and nothing is stored in R. | |
70 | ||
71 | Note: The result is an int instead of a pointer to the next byte to be | |
72 | read to avoid const-vs-non-const problems. */ | |
73 | ||
74 | static inline size_t | |
75 | read_sleb128_to_ll (const unsigned char *buf, const unsigned char *buf_end, | |
76 | long long *r) | |
77 | { | |
78 | const unsigned char *p = buf; | |
79 | unsigned int shift = 0; | |
80 | long long result = 0; | |
81 | unsigned char byte; | |
82 | ||
83 | while (1) | |
84 | { | |
85 | if (p >= buf_end) | |
86 | return 0; | |
87 | ||
88 | byte = *p++; | |
89 | result |= ((unsigned long long) (byte & 0x7f)) << shift; | |
90 | shift += 7; | |
91 | if ((byte & 0x80) == 0) | |
92 | break; | |
93 | } | |
94 | if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0) | |
95 | result |= -(((unsigned long long) 1) << shift); | |
96 | ||
97 | *r = result; | |
98 | return p - buf; | |
99 | } | |
100 | ||
101 | /* Return the number of bytes to read to skip past an LEB128 number in BUF. | |
102 | If the end isn't found before reaching BUF_END, return zero. | |
103 | ||
104 | Note: The result is an int instead of a pointer to the next byte to be | |
105 | read to avoid const-vs-non-const problems. */ | |
106 | ||
107 | static inline size_t | |
108 | skip_leb128 (const unsigned char *buf, const unsigned char *buf_end) | |
109 | { | |
110 | const unsigned char *p = buf; | |
111 | unsigned char byte; | |
112 | ||
113 | while (1) | |
114 | { | |
115 | if (p == buf_end) | |
116 | return 0; | |
117 | ||
118 | byte = *p++; | |
119 | if ((byte & 0x80) == 0) | |
120 | return p - buf; | |
121 | } | |
122 | } | |
123 | ||
124 | #endif /* LEB128_H */ |