1
2 /*--------------------------------------------------------------------*/
3 /*--- Standalone libc stuff. pub_tool_libcbase.h ---*/
4 /*--------------------------------------------------------------------*/
5
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
10 Copyright (C) 2000-2015 Julian Seward
11 jseward@acm.org
12
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29 */
30
31 #ifndef __PUB_TOOL_LIBCBASE_H
32 #define __PUB_TOOL_LIBCBASE_H
33
34 #include "pub_tool_basics.h" // VG_ macro
35
36 /* ---------------------------------------------------------------------
37 Char functions.
38 ------------------------------------------------------------------ */
39
40 extern Bool VG_(isspace) ( HChar c );
41 extern Bool VG_(isdigit) ( HChar c );
42 extern HChar VG_(tolower) ( HChar c );
43
44 /* ---------------------------------------------------------------------
45 Converting strings to numbers
46 ------------------------------------------------------------------ */
47
48 // Convert strings to numbers according to various bases. Leading
49 // whitespace is ignored. A subsequent '-' or '+' is accepted. For strtoll16,
50 // accepts an initial "0x" or "0X" prefix, but only if it's followed by a
51 // hex digit (if not, the '0' will be read and then it will stop on the
52 // "x"/"X".) If 'endptr' isn't NULL, it gets filled in with the first
53 // non-digit char. Returns 0 if no number could be converted, and 'endptr'
54 // is set to the start of the string. None of them test that the number
55 // fits into 64 bits.
56 //
57 // Nb: we also don't provide VG_(atoll*); these functions are worse than
58 // useless because they don't do any error checking and so accept malformed
59 // numbers and non-numbers -- eg. "123xyz" gives 123, and "foo" gives 0!
60 // If you really want that behaviour, you can use "VG_(strtoll10)(str, NULL)".
61 extern Long VG_(strtoll10) ( const HChar* str, HChar** endptr );
62 extern Long VG_(strtoll16) ( const HChar* str, HChar** endptr );
63 extern ULong VG_(strtoull10) ( const HChar* str, HChar** endptr );
64 extern ULong VG_(strtoull16) ( const HChar* str, HChar** endptr );
65
66 // Convert a string to a double. After leading whitespace is ignored, a
67 // '+' or '-' is allowed, and then it accepts a non-empty sequence of
68 // decimal digits possibly containing a '.'. Hexadecimal floats are not
69 // accepted, nor are "fancy" floats (eg. "3.4e-5", "NAN").
70 extern double VG_(strtod) ( const HChar* str, HChar** endptr );
71
72 /* ---------------------------------------------------------------------
73 String functions and macros
74 ------------------------------------------------------------------ */
75
76 /* Use this for normal null-termination-style string comparison. */
77 #define VG_STREQ(s1,s2) ( (s1 != NULL && s2 != NULL \
78 && VG_(strcmp)((s1),(s2))==0) ? True : False )
79 #define VG_STREQN(n,s1,s2) ( (s1 != NULL && s2 != NULL \
80 && VG_(strncmp)((s1),(s2),(n))==0) ? True : False )
81
82 extern SizeT VG_(strlen) ( const HChar* str );
83 extern HChar* VG_(strcat) ( HChar* dest, const HChar* src );
84 extern HChar* VG_(strncat) ( HChar* dest, const HChar* src, SizeT n );
85 extern HChar* VG_(strpbrk) ( const HChar* s, const HChar* accpt );
86 extern HChar* VG_(strcpy) ( HChar* dest, const HChar* src );
87 extern HChar* VG_(strncpy) ( HChar* dest, const HChar* src, SizeT ndest );
88 extern Int VG_(strcmp) ( const HChar* s1, const HChar* s2 );
89 extern Int VG_(strcasecmp) ( const HChar* s1, const HChar* s2 );
90 extern Int VG_(strncmp) ( const HChar* s1, const HChar* s2, SizeT nmax );
91 extern Int VG_(strncasecmp) ( const HChar* s1, const HChar* s2, SizeT nmax );
92 extern HChar* VG_(strstr) ( const HChar* haystack, const HChar* needle );
93 extern HChar* VG_(strcasestr) ( const HChar* haystack, const HChar* needle );
94 extern HChar* VG_(strchr) ( const HChar* s, HChar c );
95 extern HChar* VG_(strrchr) ( const HChar* s, HChar c );
96 extern SizeT VG_(strspn) ( const HChar* s, const HChar* accpt );
97 extern SizeT VG_(strcspn) ( const HChar* s, const HChar* reject );
98
99 /* strtok* functions and some parsing utilities. */
100 extern HChar* VG_(strtok_r) (HChar* s, const HChar* delim, HChar** saveptr);
101 extern HChar* VG_(strtok) (HChar* s, const HChar* delim);
102
103 /* Parse a 32- or 64-bit hex number, including leading 0x, from string
104 starting at *ppc, putting result in *result, and return True. Or
105 fail, in which case *ppc and *result are undefined, and return
106 False. */
107 extern Bool VG_(parse_Addr) ( const HChar** ppc, Addr* result );
108
109 /* Parse an "enum set" made of one or more words comma separated.
110 The allowed word values are given in 'tokens', separated by comma.
111 If a word in 'tokens' is found in 'input', the corresponding bit
112 will be set in *enum_set (words in 'tokens' are numbered starting from 0).
113 Using in 'tokens' the special token "-" (a minus character) indicates that
114 the corresponding bit position cannot be set.
115 In addition to the words specified in 'tokens', VG_(parse_enum_set)
116 automatically accept the word "none" to indicate an empty enum_set (0).
117 If allow_all, VG_(parse_enum_set) automatically accept the word "all"
118 to indicate an enum_set with all bits corresponding to the words in tokens
119 set.
120 If "none" or "all" is present in 'input', no other word can be given
121 in 'input'.
122 If parsing is successful, returns True and sets *enum_set.
123 If parsing fails, returns False. */
124 extern Bool VG_(parse_enum_set) ( const HChar *tokens,
125 Bool allow_all,
126 const HChar *input,
127 UInt *enum_set);
128
129 /* ---------------------------------------------------------------------
130 mem* functions
131 ------------------------------------------------------------------ */
132
133 extern void* VG_(memcpy) ( void *d, const void *s, SizeT sz );
134 extern void* VG_(memmove)( void *d, const void *s, SizeT sz );
135 extern void* VG_(memset) ( void *s, Int c, SizeT sz );
136 extern Int VG_(memcmp) ( const void* s1, const void* s2, SizeT n );
137
138 /* Zero out up to 12 words quickly in-line. Do not use this for blocks
139 of size which are unknown at compile time, since the whole point is
140 for it to be inlined, and then for gcc to remove all code except
141 for the relevant 'sz' case. */
142 inline __attribute__((always_inline))
VG_(bzero_inline)143 static void VG_(bzero_inline) ( void* s, SizeT sz )
144 {
145 if (LIKELY(0 == (((Addr)sz) & (Addr)(sizeof(UWord)-1)))
146 && LIKELY(0 == (((Addr)s) & (Addr)(sizeof(UWord)-1)))) {
147 UWord* p = (UWord*)s;
148 switch (sz / (SizeT)sizeof(UWord)) {
149 case 12: p[0] = p[1] = p[2] = p[3]
150 = p[4] = p[5] = p[6] = p[7]
151 = p[8] = p[9] = p[10] = p[11] = 0UL; return;
152 case 11: p[0] = p[1] = p[2] = p[3]
153 = p[4] = p[5] = p[6] = p[7]
154 = p[8] = p[9] = p[10] = 0UL; return;
155 case 10: p[0] = p[1] = p[2] = p[3]
156 = p[4] = p[5] = p[6] = p[7]
157 = p[8] = p[9] = 0UL; return;
158 case 9: p[0] = p[1] = p[2] = p[3]
159 = p[4] = p[5] = p[6] = p[7]
160 = p[8] = 0UL; return;
161 case 8: p[0] = p[1] = p[2] = p[3]
162 = p[4] = p[5] = p[6] = p[7] = 0UL; return;
163 case 7: p[0] = p[1] = p[2] = p[3]
164 = p[4] = p[5] = p[6] = 0UL; return;
165 case 6: p[0] = p[1] = p[2] = p[3]
166 = p[4] = p[5] = 0UL; return;
167 case 5: p[0] = p[1] = p[2] = p[3] = p[4] = 0UL; return;
168 case 4: p[0] = p[1] = p[2] = p[3] = 0UL; return;
169 case 3: p[0] = p[1] = p[2] = 0UL; return;
170 case 2: p[0] = p[1] = 0UL; return;
171 case 1: p[0] = 0UL; return;
172 case 0: return;
173 default: break;
174 }
175 }
176 VG_(memset)(s, 0, sz);
177 }
178
179
180 /* ---------------------------------------------------------------------
181 Address computation helpers
182 ------------------------------------------------------------------ */
183
184 // Check if an address/whatever is aligned
185 #define VG_IS_2_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1)))
186 #define VG_IS_4_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x3)))
187 #define VG_IS_8_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x7)))
188 #define VG_IS_16_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0xf)))
189 #define VG_IS_32_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)0x1f)))
190 #define VG_IS_WORD_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(sizeof(Addr)-1))))
191 #define VG_IS_PAGE_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & ((Addr)(VKI_PAGE_SIZE-1))))
192
193 // 'a' -- the alignment -- must be a power of 2.
194 // The latter two require the vki-*.h header to be imported also.
195 #define VG_ROUNDDN(p, a) ((Addr)(p) & ~((Addr)(a)-1))
196 #define VG_ROUNDUP(p, a) VG_ROUNDDN((p)+(a)-1, (a))
197 #define VG_PGROUNDDN(p) VG_ROUNDDN(p, VKI_PAGE_SIZE)
198 #define VG_PGROUNDUP(p) VG_ROUNDUP(p, VKI_PAGE_SIZE)
199
200 /* ---------------------------------------------------------------------
201 Misc useful functions
202 ------------------------------------------------------------------ */
203
204 /* Like qsort(). The name VG_(ssort) is for historical reasons -- it used
205 * to be a shell sort, but is now a quicksort. */
206 extern void VG_(ssort)( void* base, SizeT nmemb, SizeT size,
207 Int (*compar)(const void*, const void*) );
208
209 /* Returns the base-2 logarithm of a 32 bit unsigned number. Returns
210 -1 if it is not a power of two. Nb: VG_(log2)(1) == 0. */
211 extern Int VG_(log2) ( UInt x );
212
213 /* Ditto for 64 bit unsigned numbers. */
214 extern Int VG_(log2_64)( ULong x );
215
216 // A pseudo-random number generator returning a random UInt. If pSeed
217 // is NULL, it uses its own seed, which starts at zero. If pSeed is
218 // non-NULL, it uses and updates whatever pSeed points at.
219 extern UInt VG_(random) ( /*MOD*/UInt* pSeed );
220
221 /* Update a running Adler-32 checksum with the bytes buf[0..len-1] and
222 return the updated checksum. If buf is NULL, this function returns
223 the required initial value for the checksum. An Adler-32 checksum is
224 almost as reliable as a CRC32 but can be computed much faster. */
225 extern UInt VG_(adler32)( UInt adler, const UChar* buf, UInt len);
226
227 #endif // __PUB_TOOL_LIBCBASE_H
228
229 /*--------------------------------------------------------------------*/
230 /*--- end ---*/
231 /*--------------------------------------------------------------------*/
232