blob: 0a1d653b8b5485ef65418d0a67ccae36263642ae [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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 */
Elliott Hughes09c39d62014-08-19 14:30:30 -070028
29#ifndef _STRING_H
30#define _STRING_H
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031
32#include <sys/cdefs.h>
33#include <stddef.h>
34#include <malloc.h>
Dan Albertdfb5ce42014-07-09 22:51:34 +000035#include <xlocale.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036
37__BEGIN_DECLS
38
Nick Kralevich1c462b72013-05-07 10:00:21 -070039extern void* memccpy(void* __restrict, const void* __restrict, int, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070040extern void* memchr(const void *, int, size_t) __purefunc;
41extern void* memrchr(const void *, int, size_t) __purefunc;
42extern int memcmp(const void *, const void *, size_t) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070043extern void* memcpy(void* __restrict, const void* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080044extern void* memmove(void *, const void *, size_t);
45extern void* memset(void *, int, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070046extern void* memmem(const void *, size_t, const void *, size_t) __purefunc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047
Nick Kralevicha6779072012-03-21 08:48:18 -070048extern char* strchr(const char *, int) __purefunc;
Pavel Chupin3c4b50f2013-07-26 16:50:11 +040049extern char* __strchr_chk(const char *, int, size_t);
50
Nick Kralevicha6779072012-03-21 08:48:18 -070051extern char* strrchr(const char *, int) __purefunc;
Elliott Hughes53e43292014-02-24 18:00:43 -080052extern char* __strrchr_chk(const char *, int, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053
Nick Kralevicha6779072012-03-21 08:48:18 -070054extern size_t strlen(const char *) __purefunc;
Nick Kralevichcf870192013-05-30 16:48:53 -070055extern size_t __strlen_chk(const char *, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070056extern int strcmp(const char *, const char *) __purefunc;
Christopher Ferris950a58e2014-04-04 14:38:18 -070057extern char* stpcpy(char* __restrict, const char* __restrict);
Nick Kralevich1c462b72013-05-07 10:00:21 -070058extern char* strcpy(char* __restrict, const char* __restrict);
59extern char* strcat(char* __restrict, const char* __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080060
Nick Kralevicha6779072012-03-21 08:48:18 -070061extern int strcasecmp(const char *, const char *) __purefunc;
62extern int strncasecmp(const char *, const char *, size_t) __purefunc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063extern char* strdup(const char *);
64
Nick Kralevicha6779072012-03-21 08:48:18 -070065extern char* strstr(const char *, const char *) __purefunc;
66extern char* strcasestr(const char *haystack, const char *needle) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070067extern char* strtok(char* __restrict, const char* __restrict);
68extern char* strtok_r(char* __restrict, const char* __restrict, char** __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080069
70extern char* strerror(int);
71extern int strerror_r(int errnum, char *buf, size_t n);
72
Nick Kralevicha6779072012-03-21 08:48:18 -070073extern size_t strnlen(const char *, size_t) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070074extern char* strncat(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075extern char* strndup(const char *, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070076extern int strncmp(const char *, const char *, size_t) __purefunc;
Christopher Ferris950a58e2014-04-04 14:38:18 -070077extern char* stpncpy(char* __restrict, const char* __restrict, size_t);
Nick Kralevich1c462b72013-05-07 10:00:21 -070078extern char* strncpy(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079
Nick Kralevich1c462b72013-05-07 10:00:21 -070080extern size_t strlcat(char* __restrict, const char* __restrict, size_t);
81extern size_t strlcpy(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080082
Nick Kralevicha6779072012-03-21 08:48:18 -070083extern size_t strcspn(const char *, const char *) __purefunc;
84extern char* strpbrk(const char *, const char *) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070085extern char* strsep(char** __restrict, const char* __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086extern size_t strspn(const char *, const char *);
87
88extern char* strsignal(int sig);
89
Nick Kralevicha6779072012-03-21 08:48:18 -070090extern int strcoll(const char *, const char *) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070091extern size_t strxfrm(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092
Dan Albertdfb5ce42014-07-09 22:51:34 +000093extern int strcoll_l(const char *, const char *, locale_t) __purefunc;
94extern size_t strxfrm_l(char* __restrict, const char* __restrict, size_t, locale_t);
95
Elliott Hughes09c39d62014-08-19 14:30:30 -070096#if defined(__USE_GNU) && !defined(__bionic_using_posix_basename)
97/*
98 * glibc has a basename in <string.h> that's different to the POSIX one in <libgen.h>.
99 * It doesn't modify its argument, and in C++ it's const-correct.
100 */
101#if defined(__cplusplus)
102extern "C++" char* basename(char*) __RENAME(__gnu_basename) __nonnull((1));
103extern "C++" const char* basename(const char*) __RENAME(__gnu_basename) __nonnull((1));
104#else
105extern char* basename(const char*) __RENAME(__gnu_basename) __nonnull((1));
106#endif
107#define __bionic_using_gnu_basename
108#endif
109
Elliott Hughes890c8ed2013-03-22 10:58:55 -0700110#if defined(__BIONIC_FORTIFY)
Nick Kralevich0a230152012-06-04 15:20:25 -0700111
112__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700113void* memcpy(void* __restrict dest, const void* __restrict src, size_t copy_amount) {
Nick Kralevichf3913b52012-07-12 15:10:03 -0700114 char *d = (char *) dest;
115 const char *s = (const char *) src;
Nick Kralevichbd8e6742013-08-28 13:22:52 -0700116 size_t s_len = __bos0(s);
117 size_t d_len = __bos0(d);
Nick Kralevichf3913b52012-07-12 15:10:03 -0700118
Nick Kralevichc37fc1a2012-07-13 17:49:10 -0700119 return __builtin___memcpy_chk(dest, src, copy_amount, d_len);
Nick Kralevich0a230152012-06-04 15:20:25 -0700120}
121
122__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700123void* memmove(void *dest, const void *src, size_t len) {
Nick Kralevichbd8e6742013-08-28 13:22:52 -0700124 return __builtin___memmove_chk(dest, src, len, __bos0(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700125}
126
127__BIONIC_FORTIFY_INLINE
Christopher Ferris950a58e2014-04-04 14:38:18 -0700128char* stpcpy(char* __restrict dest, const char* __restrict src) {
129 return __builtin___stpcpy_chk(dest, src, __bos(dest));
130}
131
132__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700133char* strcpy(char* __restrict dest, const char* __restrict src) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700134 return __builtin___strcpy_chk(dest, src, __bos(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700135}
136
Christopher Ferris950a58e2014-04-04 14:38:18 -0700137extern char* __stpncpy_chk2(char* __restrict, const char* __restrict, size_t, size_t, size_t);
138
139__BIONIC_FORTIFY_INLINE
140char* stpncpy(char* __restrict dest, const char* __restrict src, size_t n) {
141 size_t bos_dest = __bos(dest);
142 size_t bos_src = __bos(src);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700143
144 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
145 return __builtin___stpncpy_chk(dest, src, n, bos_dest);
146 }
147
148 if (__builtin_constant_p(n) && (n <= bos_src)) {
149 return __builtin___stpncpy_chk(dest, src, n, bos_dest);
150 }
151
152 size_t slen = __builtin_strlen(src);
153 if (__builtin_constant_p(slen)) {
154 return __builtin___stpncpy_chk(dest, src, n, bos_dest);
155 }
156
157 return __stpncpy_chk2(dest, src, n, bos_dest, bos_src);
158}
159
Nick Kralevich93501d32013-08-28 10:47:43 -0700160extern char* __strncpy_chk2(char* __restrict, const char* __restrict, size_t, size_t, size_t);
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700161
Nick Kralevich0a230152012-06-04 15:20:25 -0700162__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700163char* strncpy(char* __restrict dest, const char* __restrict src, size_t n) {
Nick Kralevich93501d32013-08-28 10:47:43 -0700164 size_t bos_dest = __bos(dest);
165 size_t bos_src = __bos(src);
Nick Kralevich93501d32013-08-28 10:47:43 -0700166
167 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
168 return __builtin___strncpy_chk(dest, src, n, bos_dest);
169 }
170
Nick Kralevichd13c2b12013-09-27 13:21:24 -0700171 if (__builtin_constant_p(n) && (n <= bos_src)) {
172 return __builtin___strncpy_chk(dest, src, n, bos_dest);
173 }
174
Nick Kralevich93501d32013-08-28 10:47:43 -0700175 size_t slen = __builtin_strlen(src);
176 if (__builtin_constant_p(slen)) {
177 return __builtin___strncpy_chk(dest, src, n, bos_dest);
178 }
179
180 return __strncpy_chk2(dest, src, n, bos_dest, bos_src);
Nick Kralevich0a230152012-06-04 15:20:25 -0700181}
182
183__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700184char* strcat(char* __restrict dest, const char* __restrict src) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700185 return __builtin___strcat_chk(dest, src, __bos(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700186}
187
188__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700189char *strncat(char* __restrict dest, const char* __restrict src, size_t n) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700190 return __builtin___strncat_chk(dest, src, n, __bos(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700191}
192
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700193__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700194void* memset(void *s, int c, size_t n) {
Nick Kralevichbd8e6742013-08-28 13:22:52 -0700195 return __builtin___memset_chk(s, c, n, __bos0(s));
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700196}
197
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700198extern size_t __strlcpy_real(char* __restrict, const char* __restrict, size_t) __RENAME(strlcpy);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700199extern size_t __strlcpy_chk(char *, const char *, size_t, size_t);
200
201__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700202size_t strlcpy(char* __restrict dest, const char* __restrict src, size_t size) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700203 size_t bos = __bos(dest);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700204
Nick Kralevich8bafa742013-06-20 12:17:44 -0700205#if !defined(__clang__)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700206 // Compiler doesn't know destination size. Don't call __strlcpy_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700207 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700208 return __strlcpy_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700209 }
210
211 // Compiler can prove, at compile time, that the passed in size
212 // is always <= the actual object size. Don't call __strlcpy_chk
213 if (__builtin_constant_p(size) && (size <= bos)) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700214 return __strlcpy_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700215 }
Nick Kralevich8bafa742013-06-20 12:17:44 -0700216#endif /* !defined(__clang__) */
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700217
218 return __strlcpy_chk(dest, src, size, bos);
219}
220
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700221extern size_t __strlcat_real(char* __restrict, const char* __restrict, size_t) __RENAME(strlcat);
Nick Kralevich1c462b72013-05-07 10:00:21 -0700222extern size_t __strlcat_chk(char* __restrict, const char* __restrict, size_t, size_t);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700223
224
225__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700226size_t strlcat(char* __restrict dest, const char* __restrict src, size_t size) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700227 size_t bos = __bos(dest);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700228
Nick Kralevicha6cde392013-06-29 08:15:25 -0700229#if !defined(__clang__)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700230 // Compiler doesn't know destination size. Don't call __strlcat_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700231 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700232 return __strlcat_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700233 }
234
235 // Compiler can prove, at compile time, that the passed in size
236 // is always <= the actual object size. Don't call __strlcat_chk
237 if (__builtin_constant_p(size) && (size <= bos)) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700238 return __strlcat_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700239 }
Nick Kralevicha6cde392013-06-29 08:15:25 -0700240#endif /* !defined(__clang__) */
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700241
242 return __strlcat_chk(dest, src, size, bos);
243}
244
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700245__BIONIC_FORTIFY_INLINE
246size_t strlen(const char *s) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700247 size_t bos = __bos(s);
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700248
Nick Kralevich16d1af12013-06-17 14:49:19 -0700249#if !defined(__clang__)
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700250 // Compiler doesn't know destination size. Don't call __strlen_chk
251 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800252 return __builtin_strlen(s);
253 }
254
255 size_t slen = __builtin_strlen(s);
256 if (__builtin_constant_p(slen)) {
257 return slen;
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700258 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700259#endif /* !defined(__clang__) */
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700260
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700261 return __strlen_chk(s, bos);
262}
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700263
Nick Kralevich049e5832012-11-30 15:15:58 -0800264__BIONIC_FORTIFY_INLINE
265char* strchr(const char *s, int c) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700266 size_t bos = __bos(s);
Nick Kralevich049e5832012-11-30 15:15:58 -0800267
Nick Kralevich16d1af12013-06-17 14:49:19 -0700268#if !defined(__clang__)
Nick Kralevich049e5832012-11-30 15:15:58 -0800269 // Compiler doesn't know destination size. Don't call __strchr_chk
270 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800271 return __builtin_strchr(s, c);
272 }
273
274 size_t slen = __builtin_strlen(s);
275 if (__builtin_constant_p(slen) && (slen < bos)) {
276 return __builtin_strchr(s, c);
Nick Kralevich049e5832012-11-30 15:15:58 -0800277 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700278#endif /* !defined(__clang__) */
Nick Kralevich049e5832012-11-30 15:15:58 -0800279
280 return __strchr_chk(s, c, bos);
281}
282
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800283__BIONIC_FORTIFY_INLINE
284char* strrchr(const char *s, int c) {
Nick Kralevich3b2e6bc2013-04-30 14:19:23 -0700285 size_t bos = __bos(s);
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800286
Nick Kralevich16d1af12013-06-17 14:49:19 -0700287#if !defined(__clang__)
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800288 // Compiler doesn't know destination size. Don't call __strrchr_chk
289 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800290 return __builtin_strrchr(s, c);
291 }
292
293 size_t slen = __builtin_strlen(s);
294 if (__builtin_constant_p(slen) && (slen < bos)) {
295 return __builtin_strrchr(s, c);
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800296 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700297#endif /* !defined(__clang__) */
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800298
299 return __strrchr_chk(s, c, bos);
300}
301
Nick Kralevich049e5832012-11-30 15:15:58 -0800302
Elliott Hughes890c8ed2013-03-22 10:58:55 -0700303#endif /* defined(__BIONIC_FORTIFY) */
Nick Kralevich0a230152012-06-04 15:20:25 -0700304
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800305__END_DECLS
306
Elliott Hughes09c39d62014-08-19 14:30:30 -0700307#endif /* _STRING_H */