blob: d67928c0f1790c38d56ec6c9fb1a37ca7e1a7c06 [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>
Dan Albertdfb5ce42014-07-09 22:51:34 +000034#include <xlocale.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035
36__BEGIN_DECLS
37
Elliott Hughes76f89162015-01-26 13:34:58 -080038#if defined(__USE_BSD)
39#include <strings.h>
40#endif
41
Nick Kralevich1c462b72013-05-07 10:00:21 -070042extern void* memccpy(void* __restrict, const void* __restrict, int, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070043extern void* memchr(const void *, int, size_t) __purefunc;
44extern void* memrchr(const void *, int, size_t) __purefunc;
45extern int memcmp(const void *, const void *, size_t) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070046extern void* memcpy(void* __restrict, const void* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047extern void* memmove(void *, const void *, size_t);
48extern void* memset(void *, int, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070049extern void* memmem(const void *, size_t, const void *, size_t) __purefunc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050
Nick Kralevicha6779072012-03-21 08:48:18 -070051extern char* strchr(const char *, int) __purefunc;
Pavel Chupin3c4b50f2013-07-26 16:50:11 +040052extern char* __strchr_chk(const char *, int, size_t);
53
Nick Kralevicha6779072012-03-21 08:48:18 -070054extern char* strrchr(const char *, int) __purefunc;
Elliott Hughes53e43292014-02-24 18:00:43 -080055extern char* __strrchr_chk(const char *, int, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056
Nick Kralevicha6779072012-03-21 08:48:18 -070057extern size_t strlen(const char *) __purefunc;
Nick Kralevichcf870192013-05-30 16:48:53 -070058extern size_t __strlen_chk(const char *, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070059extern int strcmp(const char *, const char *) __purefunc;
Christopher Ferris950a58e2014-04-04 14:38:18 -070060extern char* stpcpy(char* __restrict, const char* __restrict);
Nick Kralevich1c462b72013-05-07 10:00:21 -070061extern char* strcpy(char* __restrict, const char* __restrict);
62extern char* strcat(char* __restrict, const char* __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063
Elliott Hughesb20c2442014-11-06 15:04:08 -080064int strcasecmp(const char*, const char*) __purefunc;
65int strcasecmp_l(const char*, const char*, locale_t) __purefunc;
66int strncasecmp(const char*, const char*, size_t) __purefunc;
67int strncasecmp_l(const char*, const char*, size_t, locale_t) __purefunc;
68
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080069extern char* strdup(const char *);
70
Nick Kralevicha6779072012-03-21 08:48:18 -070071extern char* strstr(const char *, const char *) __purefunc;
72extern char* strcasestr(const char *haystack, const char *needle) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070073extern char* strtok(char* __restrict, const char* __restrict);
74extern char* strtok_r(char* __restrict, const char* __restrict, char** __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075
Elliott Hughes416d7dd2014-08-18 17:28:32 -070076extern char* strerror(int);
Elliott Hughesb20c2442014-11-06 15:04:08 -080077extern char* strerror_l(int, locale_t);
Elliott Hughes416d7dd2014-08-18 17:28:32 -070078#if defined(__USE_GNU)
79extern char* strerror_r(int, char*, size_t) __RENAME(__gnu_strerror_r);
80#else /* POSIX */
81extern int strerror_r(int, char*, size_t);
82#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083
Nick Kralevicha6779072012-03-21 08:48:18 -070084extern size_t strnlen(const char *, size_t) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070085extern char* strncat(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086extern char* strndup(const char *, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070087extern int strncmp(const char *, const char *, size_t) __purefunc;
Christopher Ferris950a58e2014-04-04 14:38:18 -070088extern char* stpncpy(char* __restrict, const char* __restrict, size_t);
Nick Kralevich1c462b72013-05-07 10:00:21 -070089extern char* strncpy(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080090
Nick Kralevich1c462b72013-05-07 10:00:21 -070091extern size_t strlcat(char* __restrict, const char* __restrict, size_t);
92extern size_t strlcpy(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093
Nick Kralevicha6779072012-03-21 08:48:18 -070094extern size_t strcspn(const char *, const char *) __purefunc;
95extern char* strpbrk(const char *, const char *) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070096extern char* strsep(char** __restrict, const char* __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080097extern size_t strspn(const char *, const char *);
98
99extern char* strsignal(int sig);
100
Nick Kralevicha6779072012-03-21 08:48:18 -0700101extern int strcoll(const char *, const char *) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -0700102extern size_t strxfrm(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103
Dan Albertdfb5ce42014-07-09 22:51:34 +0000104extern int strcoll_l(const char *, const char *, locale_t) __purefunc;
105extern size_t strxfrm_l(char* __restrict, const char* __restrict, size_t, locale_t);
106
Elliott Hughes09c39d62014-08-19 14:30:30 -0700107#if defined(__USE_GNU) && !defined(__bionic_using_posix_basename)
108/*
109 * glibc has a basename in <string.h> that's different to the POSIX one in <libgen.h>.
110 * It doesn't modify its argument, and in C++ it's const-correct.
111 */
112#if defined(__cplusplus)
113extern "C++" char* basename(char*) __RENAME(__gnu_basename) __nonnull((1));
114extern "C++" const char* basename(const char*) __RENAME(__gnu_basename) __nonnull((1));
115#else
116extern char* basename(const char*) __RENAME(__gnu_basename) __nonnull((1));
117#endif
118#define __bionic_using_gnu_basename
119#endif
120
Dan Albert658727e2014-10-07 11:10:36 -0700121extern char* __stpncpy_chk2(char* __restrict, const char* __restrict, size_t, size_t, size_t);
122extern char* __strncpy_chk2(char* __restrict, const char* __restrict, size_t, size_t, size_t);
123extern size_t __strlcpy_real(char* __restrict, const char* __restrict, size_t) __RENAME(strlcpy);
124extern size_t __strlcpy_chk(char *, const char *, size_t, size_t);
125extern size_t __strlcat_real(char* __restrict, const char* __restrict, size_t) __RENAME(strlcat);
126extern size_t __strlcat_chk(char* __restrict, const char* __restrict, size_t, size_t);
127
Elliott Hughes890c8ed2013-03-22 10:58:55 -0700128#if defined(__BIONIC_FORTIFY)
Nick Kralevich0a230152012-06-04 15:20:25 -0700129
130__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700131void* memcpy(void* __restrict dest, const void* __restrict src, size_t copy_amount) {
Nick Kralevichb84f6672014-10-05 06:52:24 -0700132 return __builtin___memcpy_chk(dest, src, copy_amount, __bos0(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700133}
134
135__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700136void* memmove(void *dest, const void *src, size_t len) {
Nick Kralevichbd8e6742013-08-28 13:22:52 -0700137 return __builtin___memmove_chk(dest, src, len, __bos0(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700138}
139
140__BIONIC_FORTIFY_INLINE
Christopher Ferris950a58e2014-04-04 14:38:18 -0700141char* stpcpy(char* __restrict dest, const char* __restrict src) {
142 return __builtin___stpcpy_chk(dest, src, __bos(dest));
143}
144
145__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700146char* strcpy(char* __restrict dest, const char* __restrict src) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700147 return __builtin___strcpy_chk(dest, src, __bos(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700148}
149
Christopher Ferris950a58e2014-04-04 14:38:18 -0700150__BIONIC_FORTIFY_INLINE
151char* stpncpy(char* __restrict dest, const char* __restrict src, size_t n) {
152 size_t bos_dest = __bos(dest);
153 size_t bos_src = __bos(src);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700154
155 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
156 return __builtin___stpncpy_chk(dest, src, n, bos_dest);
157 }
158
159 if (__builtin_constant_p(n) && (n <= bos_src)) {
160 return __builtin___stpncpy_chk(dest, src, n, bos_dest);
161 }
162
163 size_t slen = __builtin_strlen(src);
164 if (__builtin_constant_p(slen)) {
165 return __builtin___stpncpy_chk(dest, src, n, bos_dest);
166 }
167
168 return __stpncpy_chk2(dest, src, n, bos_dest, bos_src);
169}
170
Nick Kralevich0a230152012-06-04 15:20:25 -0700171__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700172char* strncpy(char* __restrict dest, const char* __restrict src, size_t n) {
Nick Kralevich93501d32013-08-28 10:47:43 -0700173 size_t bos_dest = __bos(dest);
174 size_t bos_src = __bos(src);
Nick Kralevich93501d32013-08-28 10:47:43 -0700175
176 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
177 return __builtin___strncpy_chk(dest, src, n, bos_dest);
178 }
179
Nick Kralevichd13c2b12013-09-27 13:21:24 -0700180 if (__builtin_constant_p(n) && (n <= bos_src)) {
181 return __builtin___strncpy_chk(dest, src, n, bos_dest);
182 }
183
Nick Kralevich93501d32013-08-28 10:47:43 -0700184 size_t slen = __builtin_strlen(src);
185 if (__builtin_constant_p(slen)) {
186 return __builtin___strncpy_chk(dest, src, n, bos_dest);
187 }
188
189 return __strncpy_chk2(dest, src, n, bos_dest, bos_src);
Nick Kralevich0a230152012-06-04 15:20:25 -0700190}
191
192__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700193char* strcat(char* __restrict dest, const char* __restrict src) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700194 return __builtin___strcat_chk(dest, src, __bos(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700195}
196
197__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700198char *strncat(char* __restrict dest, const char* __restrict src, size_t n) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700199 return __builtin___strncat_chk(dest, src, n, __bos(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700200}
201
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700202__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700203void* memset(void *s, int c, size_t n) {
Nick Kralevichbd8e6742013-08-28 13:22:52 -0700204 return __builtin___memset_chk(s, c, n, __bos0(s));
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700205}
206
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700207__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700208size_t strlcpy(char* __restrict dest, const char* __restrict src, size_t size) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700209 size_t bos = __bos(dest);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700210
Nick Kralevich8bafa742013-06-20 12:17:44 -0700211#if !defined(__clang__)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700212 // Compiler doesn't know destination size. Don't call __strlcpy_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700213 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700214 return __strlcpy_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700215 }
216
217 // Compiler can prove, at compile time, that the passed in size
218 // is always <= the actual object size. Don't call __strlcpy_chk
219 if (__builtin_constant_p(size) && (size <= bos)) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700220 return __strlcpy_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700221 }
Nick Kralevich8bafa742013-06-20 12:17:44 -0700222#endif /* !defined(__clang__) */
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700223
224 return __strlcpy_chk(dest, src, size, bos);
225}
226
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700227
228__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700229size_t strlcat(char* __restrict dest, const char* __restrict src, size_t size) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700230 size_t bos = __bos(dest);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700231
Nick Kralevicha6cde392013-06-29 08:15:25 -0700232#if !defined(__clang__)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700233 // Compiler doesn't know destination size. Don't call __strlcat_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700234 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700235 return __strlcat_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700236 }
237
238 // Compiler can prove, at compile time, that the passed in size
239 // is always <= the actual object size. Don't call __strlcat_chk
240 if (__builtin_constant_p(size) && (size <= bos)) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700241 return __strlcat_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700242 }
Nick Kralevicha6cde392013-06-29 08:15:25 -0700243#endif /* !defined(__clang__) */
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700244
245 return __strlcat_chk(dest, src, size, bos);
246}
247
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700248__BIONIC_FORTIFY_INLINE
249size_t strlen(const char *s) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700250 size_t bos = __bos(s);
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700251
Nick Kralevich16d1af12013-06-17 14:49:19 -0700252#if !defined(__clang__)
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700253 // Compiler doesn't know destination size. Don't call __strlen_chk
254 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800255 return __builtin_strlen(s);
256 }
257
258 size_t slen = __builtin_strlen(s);
259 if (__builtin_constant_p(slen)) {
260 return slen;
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700261 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700262#endif /* !defined(__clang__) */
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700263
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700264 return __strlen_chk(s, bos);
265}
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700266
Nick Kralevich049e5832012-11-30 15:15:58 -0800267__BIONIC_FORTIFY_INLINE
268char* strchr(const char *s, int c) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700269 size_t bos = __bos(s);
Nick Kralevich049e5832012-11-30 15:15:58 -0800270
Nick Kralevich16d1af12013-06-17 14:49:19 -0700271#if !defined(__clang__)
Nick Kralevich049e5832012-11-30 15:15:58 -0800272 // Compiler doesn't know destination size. Don't call __strchr_chk
273 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800274 return __builtin_strchr(s, c);
275 }
276
277 size_t slen = __builtin_strlen(s);
278 if (__builtin_constant_p(slen) && (slen < bos)) {
279 return __builtin_strchr(s, c);
Nick Kralevich049e5832012-11-30 15:15:58 -0800280 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700281#endif /* !defined(__clang__) */
Nick Kralevich049e5832012-11-30 15:15:58 -0800282
283 return __strchr_chk(s, c, bos);
284}
285
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800286__BIONIC_FORTIFY_INLINE
287char* strrchr(const char *s, int c) {
Nick Kralevich3b2e6bc2013-04-30 14:19:23 -0700288 size_t bos = __bos(s);
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800289
Nick Kralevich16d1af12013-06-17 14:49:19 -0700290#if !defined(__clang__)
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800291 // Compiler doesn't know destination size. Don't call __strrchr_chk
292 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800293 return __builtin_strrchr(s, c);
294 }
295
296 size_t slen = __builtin_strlen(s);
297 if (__builtin_constant_p(slen) && (slen < bos)) {
298 return __builtin_strrchr(s, c);
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800299 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700300#endif /* !defined(__clang__) */
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800301
302 return __strrchr_chk(s, c, bos);
303}
304
Nick Kralevich049e5832012-11-30 15:15:58 -0800305
Elliott Hughes890c8ed2013-03-22 10:58:55 -0700306#endif /* defined(__BIONIC_FORTIFY) */
Nick Kralevich0a230152012-06-04 15:20:25 -0700307
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800308__END_DECLS
309
Elliott Hughes09c39d62014-08-19 14:30:30 -0700310#endif /* _STRING_H */