1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #ifndef _STRING_H
30 #define _STRING_H
31
32 #include <sys/cdefs.h>
33 #include <stddef.h>
34 #include <xlocale.h>
35
36 __BEGIN_DECLS
37
38 #if defined(__USE_BSD)
39 #include <strings.h>
40 #endif
41
42 extern void* memccpy(void* __restrict, const void* __restrict, int, size_t);
43 extern void* memchr(const void *, int, size_t) __purefunc;
44 extern void* memrchr(const void *, int, size_t) __purefunc;
45 extern int memcmp(const void *, const void *, size_t) __purefunc;
46 extern void* memcpy(void* __restrict, const void* __restrict, size_t);
47 #if defined(__USE_GNU)
48 extern void* mempcpy(void* __restrict, const void* __restrict, size_t);
49 #endif
50 extern void* memmove(void *, const void *, size_t);
51 extern void* memset(void *, int, size_t);
52 extern void* memmem(const void *, size_t, const void *, size_t) __purefunc;
53
54 extern char* strchr(const char *, int) __purefunc;
55 extern char* __strchr_chk(const char *, int, size_t);
56 #if defined(__USE_GNU)
57 #if defined(__cplusplus)
58 extern "C++" char* strchrnul(char*, int) __RENAME(strchrnul) __purefunc;
59 extern "C++" const char* strchrnul(const char*, int) __RENAME(strchrnul) __purefunc;
60 #else
61 char* strchrnul(const char*, int) __purefunc;
62 #endif
63 #endif
64
65 extern char* strrchr(const char *, int) __purefunc;
66 extern char* __strrchr_chk(const char *, int, size_t);
67
68 extern size_t strlen(const char *) __purefunc;
69 extern size_t __strlen_chk(const char *, size_t);
70 extern int strcmp(const char *, const char *) __purefunc;
71 extern char* stpcpy(char* __restrict, const char* __restrict);
72 extern char* strcpy(char* __restrict, const char* __restrict);
73 extern char* strcat(char* __restrict, const char* __restrict);
74
75 int strcasecmp(const char*, const char*) __purefunc;
76 int strcasecmp_l(const char*, const char*, locale_t) __purefunc;
77 int strncasecmp(const char*, const char*, size_t) __purefunc;
78 int strncasecmp_l(const char*, const char*, size_t, locale_t) __purefunc;
79
80 extern char* strdup(const char *);
81
82 extern char* strstr(const char *, const char *) __purefunc;
83 extern char* strcasestr(const char *haystack, const char *needle) __purefunc;
84 extern char* strtok(char* __restrict, const char* __restrict);
85 extern char* strtok_r(char* __restrict, const char* __restrict, char** __restrict);
86
87 extern char* strerror(int);
88 extern char* strerror_l(int, locale_t);
89 #if defined(__USE_GNU)
90 extern char* strerror_r(int, char*, size_t) __RENAME(__gnu_strerror_r);
91 #else /* POSIX */
92 extern int strerror_r(int, char*, size_t);
93 #endif
94
95 extern size_t strnlen(const char *, size_t) __purefunc;
96 extern char* strncat(char* __restrict, const char* __restrict, size_t);
97 extern char* strndup(const char *, size_t);
98 extern int strncmp(const char *, const char *, size_t) __purefunc;
99 extern char* stpncpy(char* __restrict, const char* __restrict, size_t);
100 extern char* strncpy(char* __restrict, const char* __restrict, size_t);
101
102 extern size_t strlcat(char* __restrict, const char* __restrict, size_t);
103 extern size_t strlcpy(char* __restrict, const char* __restrict, size_t);
104
105 extern size_t strcspn(const char *, const char *) __purefunc;
106 extern char* strpbrk(const char *, const char *) __purefunc;
107 extern char* strsep(char** __restrict, const char* __restrict);
108 extern size_t strspn(const char *, const char *);
109
110 extern char* strsignal(int sig);
111
112 extern int strcoll(const char *, const char *) __purefunc;
113 extern size_t strxfrm(char* __restrict, const char* __restrict, size_t);
114
115 extern int strcoll_l(const char *, const char *, locale_t) __purefunc;
116 extern size_t strxfrm_l(char* __restrict, const char* __restrict, size_t, locale_t);
117
118 #if defined(__USE_GNU) && !defined(basename)
119 /*
120 * glibc has a basename in <string.h> that's different to the POSIX one in <libgen.h>.
121 * It doesn't modify its argument, and in C++ it's const-correct.
122 */
123
124 #if defined(__cplusplus)
125 extern "C++" char* basename(char*) __RENAME(__gnu_basename) __nonnull((1));
126 extern "C++" const char* basename(const char*) __RENAME(__gnu_basename) __nonnull((1));
127 #else
128 extern char* basename(const char*) __RENAME(__gnu_basename) __nonnull((1));
129 #endif
130 #endif
131
132 extern void* __memchr_chk(const void*, int, size_t, size_t);
133 __errordecl(__memchr_buf_size_error, "memchr called with size bigger than buffer");
134
135 extern void* __memrchr_chk(const void*, int, size_t, size_t);
136 __errordecl(__memrchr_buf_size_error, "memrchr called with size bigger than buffer");
137 extern void* __memrchr_real(const void*, int, size_t) __RENAME(memrchr);
138
139 extern char* __stpncpy_chk2(char* __restrict, const char* __restrict, size_t, size_t, size_t);
140 extern char* __strncpy_chk2(char* __restrict, const char* __restrict, size_t, size_t, size_t);
141 extern size_t __strlcpy_real(char* __restrict, const char* __restrict, size_t) __RENAME(strlcpy);
142 extern size_t __strlcpy_chk(char *, const char *, size_t, size_t);
143 extern size_t __strlcat_real(char* __restrict, const char* __restrict, size_t) __RENAME(strlcat);
144 extern size_t __strlcat_chk(char* __restrict, const char* __restrict, size_t, size_t);
145
146 #if defined(__BIONIC_FORTIFY)
147
148 __BIONIC_FORTIFY_INLINE
memchr(const void * s,int c,size_t n)149 void* memchr(const void *s, int c, size_t n) {
150 size_t bos = __bos(s);
151
152 #if !defined(__clang__)
153 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
154 return __builtin_memchr(s, c, n);
155 }
156
157 if (__builtin_constant_p(n) && (n > bos)) {
158 __memchr_buf_size_error();
159 }
160
161 if (__builtin_constant_p(n) && (n <= bos)) {
162 return __builtin_memchr(s, c, n);
163 }
164 #endif
165
166 return __memchr_chk(s, c, n, bos);
167 }
168
169 __BIONIC_FORTIFY_INLINE
memrchr(const void * s,int c,size_t n)170 void* memrchr(const void *s, int c, size_t n) {
171 size_t bos = __bos(s);
172
173 #if !defined(__clang__)
174 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
175 return __memrchr_real(s, c, n);
176 }
177
178 if (__builtin_constant_p(n) && (n > bos)) {
179 __memrchr_buf_size_error();
180 }
181
182 if (__builtin_constant_p(n) && (n <= bos)) {
183 return __memrchr_real(s, c, n);
184 }
185 #endif
186
187 return __memrchr_chk(s, c, n, bos);
188 }
189
190 __BIONIC_FORTIFY_INLINE
memcpy(void * __restrict dest,const void * __restrict src,size_t copy_amount)191 void* memcpy(void* __restrict dest, const void* __restrict src, size_t copy_amount) {
192 return __builtin___memcpy_chk(dest, src, copy_amount, __bos0(dest));
193 }
194
195 __BIONIC_FORTIFY_INLINE
memmove(void * dest,const void * src,size_t len)196 void* memmove(void *dest, const void *src, size_t len) {
197 return __builtin___memmove_chk(dest, src, len, __bos0(dest));
198 }
199
200 __BIONIC_FORTIFY_INLINE
stpcpy(char * __restrict dest,const char * __restrict src)201 char* stpcpy(char* __restrict dest, const char* __restrict src) {
202 return __builtin___stpcpy_chk(dest, src, __bos(dest));
203 }
204
205 __BIONIC_FORTIFY_INLINE
strcpy(char * __restrict dest,const char * __restrict src)206 char* strcpy(char* __restrict dest, const char* __restrict src) {
207 return __builtin___strcpy_chk(dest, src, __bos(dest));
208 }
209
210 __BIONIC_FORTIFY_INLINE
stpncpy(char * __restrict dest,const char * __restrict src,size_t n)211 char* stpncpy(char* __restrict dest, const char* __restrict src, size_t n) {
212 size_t bos_dest = __bos(dest);
213 size_t bos_src = __bos(src);
214
215 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
216 return __builtin___stpncpy_chk(dest, src, n, bos_dest);
217 }
218
219 if (__builtin_constant_p(n) && (n <= bos_src)) {
220 return __builtin___stpncpy_chk(dest, src, n, bos_dest);
221 }
222
223 size_t slen = __builtin_strlen(src);
224 if (__builtin_constant_p(slen)) {
225 return __builtin___stpncpy_chk(dest, src, n, bos_dest);
226 }
227
228 return __stpncpy_chk2(dest, src, n, bos_dest, bos_src);
229 }
230
231 __BIONIC_FORTIFY_INLINE
strncpy(char * __restrict dest,const char * __restrict src,size_t n)232 char* strncpy(char* __restrict dest, const char* __restrict src, size_t n) {
233 size_t bos_dest = __bos(dest);
234 size_t bos_src = __bos(src);
235
236 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
237 return __builtin___strncpy_chk(dest, src, n, bos_dest);
238 }
239
240 if (__builtin_constant_p(n) && (n <= bos_src)) {
241 return __builtin___strncpy_chk(dest, src, n, bos_dest);
242 }
243
244 size_t slen = __builtin_strlen(src);
245 if (__builtin_constant_p(slen)) {
246 return __builtin___strncpy_chk(dest, src, n, bos_dest);
247 }
248
249 return __strncpy_chk2(dest, src, n, bos_dest, bos_src);
250 }
251
252 __BIONIC_FORTIFY_INLINE
strcat(char * __restrict dest,const char * __restrict src)253 char* strcat(char* __restrict dest, const char* __restrict src) {
254 return __builtin___strcat_chk(dest, src, __bos(dest));
255 }
256
257 __BIONIC_FORTIFY_INLINE
strncat(char * __restrict dest,const char * __restrict src,size_t n)258 char *strncat(char* __restrict dest, const char* __restrict src, size_t n) {
259 return __builtin___strncat_chk(dest, src, n, __bos(dest));
260 }
261
262 __BIONIC_FORTIFY_INLINE
memset(void * s,int c,size_t n)263 void* memset(void *s, int c, size_t n) {
264 return __builtin___memset_chk(s, c, n, __bos0(s));
265 }
266
267 __BIONIC_FORTIFY_INLINE
strlcpy(char * __restrict dest,const char * __restrict src,size_t size)268 size_t strlcpy(char* __restrict dest, const char* __restrict src, size_t size) {
269 size_t bos = __bos(dest);
270
271 #if !defined(__clang__)
272 // Compiler doesn't know destination size. Don't call __strlcpy_chk
273 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
274 return __strlcpy_real(dest, src, size);
275 }
276
277 // Compiler can prove, at compile time, that the passed in size
278 // is always <= the actual object size. Don't call __strlcpy_chk
279 if (__builtin_constant_p(size) && (size <= bos)) {
280 return __strlcpy_real(dest, src, size);
281 }
282 #endif /* !defined(__clang__) */
283
284 return __strlcpy_chk(dest, src, size, bos);
285 }
286
287
288 __BIONIC_FORTIFY_INLINE
strlcat(char * __restrict dest,const char * __restrict src,size_t size)289 size_t strlcat(char* __restrict dest, const char* __restrict src, size_t size) {
290 size_t bos = __bos(dest);
291
292 #if !defined(__clang__)
293 // Compiler doesn't know destination size. Don't call __strlcat_chk
294 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
295 return __strlcat_real(dest, src, size);
296 }
297
298 // Compiler can prove, at compile time, that the passed in size
299 // is always <= the actual object size. Don't call __strlcat_chk
300 if (__builtin_constant_p(size) && (size <= bos)) {
301 return __strlcat_real(dest, src, size);
302 }
303 #endif /* !defined(__clang__) */
304
305 return __strlcat_chk(dest, src, size, bos);
306 }
307
308 __BIONIC_FORTIFY_INLINE
strlen(const char * s)309 size_t strlen(const char *s) {
310 size_t bos = __bos(s);
311
312 #if !defined(__clang__)
313 // Compiler doesn't know destination size. Don't call __strlen_chk
314 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
315 return __builtin_strlen(s);
316 }
317
318 size_t slen = __builtin_strlen(s);
319 if (__builtin_constant_p(slen)) {
320 return slen;
321 }
322 #endif /* !defined(__clang__) */
323
324 return __strlen_chk(s, bos);
325 }
326
327 __BIONIC_FORTIFY_INLINE
strchr(const char * s,int c)328 char* strchr(const char *s, int c) {
329 size_t bos = __bos(s);
330
331 #if !defined(__clang__)
332 // Compiler doesn't know destination size. Don't call __strchr_chk
333 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
334 return __builtin_strchr(s, c);
335 }
336
337 size_t slen = __builtin_strlen(s);
338 if (__builtin_constant_p(slen) && (slen < bos)) {
339 return __builtin_strchr(s, c);
340 }
341 #endif /* !defined(__clang__) */
342
343 return __strchr_chk(s, c, bos);
344 }
345
346 __BIONIC_FORTIFY_INLINE
strrchr(const char * s,int c)347 char* strrchr(const char *s, int c) {
348 size_t bos = __bos(s);
349
350 #if !defined(__clang__)
351 // Compiler doesn't know destination size. Don't call __strrchr_chk
352 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
353 return __builtin_strrchr(s, c);
354 }
355
356 size_t slen = __builtin_strlen(s);
357 if (__builtin_constant_p(slen) && (slen < bos)) {
358 return __builtin_strrchr(s, c);
359 }
360 #endif /* !defined(__clang__) */
361
362 return __strrchr_chk(s, c, bos);
363 }
364
365
366 #endif /* defined(__BIONIC_FORTIFY) */
367
368 __END_DECLS
369
370 #endif /* _STRING_H */
371