blob: 6384d57cdc469ccf7a3474de0ca0daad29142141 [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 */
28#ifndef _STRING_H_
29#define _STRING_H_
30
31#include <sys/cdefs.h>
32#include <stddef.h>
33#include <malloc.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
Nick Kralevich1c462b72013-05-07 10:00:21 -070038extern void* memccpy(void* __restrict, const void* __restrict, int, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070039extern void* memchr(const void *, int, size_t) __purefunc;
40extern void* memrchr(const void *, int, size_t) __purefunc;
41extern int memcmp(const void *, const void *, size_t) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070042extern void* memcpy(void* __restrict, const void* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043extern void* memmove(void *, const void *, size_t);
44extern void* memset(void *, int, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070045extern void* memmem(const void *, size_t, const void *, size_t) __purefunc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080046
Nick Kralevicha6779072012-03-21 08:48:18 -070047extern char* strchr(const char *, int) __purefunc;
Pavel Chupin3c4b50f2013-07-26 16:50:11 +040048extern char* __strchr_chk(const char *, int, size_t);
49
Nick Kralevicha6779072012-03-21 08:48:18 -070050extern char* strrchr(const char *, int) __purefunc;
Elliott Hughes53e43292014-02-24 18:00:43 -080051extern char* __strrchr_chk(const char *, int, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052
Nick Kralevicha6779072012-03-21 08:48:18 -070053extern size_t strlen(const char *) __purefunc;
Nick Kralevichcf870192013-05-30 16:48:53 -070054extern size_t __strlen_chk(const char *, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070055extern int strcmp(const char *, const char *) __purefunc;
Christopher Ferris950a58e2014-04-04 14:38:18 -070056extern char* stpcpy(char* __restrict, const char* __restrict);
Nick Kralevich1c462b72013-05-07 10:00:21 -070057extern char* strcpy(char* __restrict, const char* __restrict);
58extern char* strcat(char* __restrict, const char* __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059
Nick Kralevicha6779072012-03-21 08:48:18 -070060extern int strcasecmp(const char *, const char *) __purefunc;
61extern int strncasecmp(const char *, const char *, size_t) __purefunc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080062extern char* strdup(const char *);
63
Nick Kralevicha6779072012-03-21 08:48:18 -070064extern char* strstr(const char *, const char *) __purefunc;
65extern char* strcasestr(const char *haystack, const char *needle) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070066extern char* strtok(char* __restrict, const char* __restrict);
67extern char* strtok_r(char* __restrict, const char* __restrict, char** __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068
69extern char* strerror(int);
70extern int strerror_r(int errnum, char *buf, size_t n);
71
Nick Kralevicha6779072012-03-21 08:48:18 -070072extern size_t strnlen(const char *, size_t) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070073extern char* strncat(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074extern char* strndup(const char *, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070075extern int strncmp(const char *, const char *, size_t) __purefunc;
Christopher Ferris950a58e2014-04-04 14:38:18 -070076extern char* stpncpy(char* __restrict, const char* __restrict, size_t);
Nick Kralevich1c462b72013-05-07 10:00:21 -070077extern char* strncpy(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078
Nick Kralevich1c462b72013-05-07 10:00:21 -070079extern size_t strlcat(char* __restrict, const char* __restrict, size_t);
80extern size_t strlcpy(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081
Nick Kralevicha6779072012-03-21 08:48:18 -070082extern size_t strcspn(const char *, const char *) __purefunc;
83extern char* strpbrk(const char *, const char *) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070084extern char* strsep(char** __restrict, const char* __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080085extern size_t strspn(const char *, const char *);
86
87extern char* strsignal(int sig);
88
Nick Kralevicha6779072012-03-21 08:48:18 -070089extern int strcoll(const char *, const char *) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070090extern size_t strxfrm(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091
Dan Albertdfb5ce42014-07-09 22:51:34 +000092extern int strcoll_l(const char *, const char *, locale_t) __purefunc;
93extern size_t strxfrm_l(char* __restrict, const char* __restrict, size_t, locale_t);
94
Elliott Hughes890c8ed2013-03-22 10:58:55 -070095#if defined(__BIONIC_FORTIFY)
Nick Kralevich0a230152012-06-04 15:20:25 -070096
97__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -070098void* memcpy(void* __restrict dest, const void* __restrict src, size_t copy_amount) {
Nick Kralevichf3913b52012-07-12 15:10:03 -070099 char *d = (char *) dest;
100 const char *s = (const char *) src;
Nick Kralevichbd8e6742013-08-28 13:22:52 -0700101 size_t d_len = __bos0(d);
Nick Kralevichf3913b52012-07-12 15:10:03 -0700102
Nick Kralevichc37fc1a2012-07-13 17:49:10 -0700103 return __builtin___memcpy_chk(dest, src, copy_amount, d_len);
Nick Kralevich0a230152012-06-04 15:20:25 -0700104}
105
106__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700107void* memmove(void *dest, const void *src, size_t len) {
Nick Kralevichbd8e6742013-08-28 13:22:52 -0700108 return __builtin___memmove_chk(dest, src, len, __bos0(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700109}
110
111__BIONIC_FORTIFY_INLINE
Christopher Ferris950a58e2014-04-04 14:38:18 -0700112char* stpcpy(char* __restrict dest, const char* __restrict src) {
113 return __builtin___stpcpy_chk(dest, src, __bos(dest));
114}
115
116__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700117char* strcpy(char* __restrict dest, const char* __restrict src) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700118 return __builtin___strcpy_chk(dest, src, __bos(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700119}
120
Christopher Ferris950a58e2014-04-04 14:38:18 -0700121extern char* __stpncpy_chk2(char* __restrict, const char* __restrict, size_t, size_t, size_t);
122
123__BIONIC_FORTIFY_INLINE
124char* stpncpy(char* __restrict dest, const char* __restrict src, size_t n) {
125 size_t bos_dest = __bos(dest);
126 size_t bos_src = __bos(src);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700127
128 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
129 return __builtin___stpncpy_chk(dest, src, n, bos_dest);
130 }
131
132 if (__builtin_constant_p(n) && (n <= bos_src)) {
133 return __builtin___stpncpy_chk(dest, src, n, bos_dest);
134 }
135
136 size_t slen = __builtin_strlen(src);
137 if (__builtin_constant_p(slen)) {
138 return __builtin___stpncpy_chk(dest, src, n, bos_dest);
139 }
140
141 return __stpncpy_chk2(dest, src, n, bos_dest, bos_src);
142}
143
Nick Kralevich93501d32013-08-28 10:47:43 -0700144extern char* __strncpy_chk2(char* __restrict, const char* __restrict, size_t, size_t, size_t);
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700145
Nick Kralevich0a230152012-06-04 15:20:25 -0700146__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700147char* strncpy(char* __restrict dest, const char* __restrict src, size_t n) {
Nick Kralevich93501d32013-08-28 10:47:43 -0700148 size_t bos_dest = __bos(dest);
149 size_t bos_src = __bos(src);
Nick Kralevich93501d32013-08-28 10:47:43 -0700150
151 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
152 return __builtin___strncpy_chk(dest, src, n, bos_dest);
153 }
154
Nick Kralevichd13c2b12013-09-27 13:21:24 -0700155 if (__builtin_constant_p(n) && (n <= bos_src)) {
156 return __builtin___strncpy_chk(dest, src, n, bos_dest);
157 }
158
Nick Kralevich93501d32013-08-28 10:47:43 -0700159 size_t slen = __builtin_strlen(src);
160 if (__builtin_constant_p(slen)) {
161 return __builtin___strncpy_chk(dest, src, n, bos_dest);
162 }
163
164 return __strncpy_chk2(dest, src, n, bos_dest, bos_src);
Nick Kralevich0a230152012-06-04 15:20:25 -0700165}
166
167__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700168char* strcat(char* __restrict dest, const char* __restrict src) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700169 return __builtin___strcat_chk(dest, src, __bos(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700170}
171
172__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700173char *strncat(char* __restrict dest, const char* __restrict src, size_t n) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700174 return __builtin___strncat_chk(dest, src, n, __bos(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700175}
176
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700177__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700178void* memset(void *s, int c, size_t n) {
Nick Kralevichbd8e6742013-08-28 13:22:52 -0700179 return __builtin___memset_chk(s, c, n, __bos0(s));
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700180}
181
Nick Kralevich1c462b72013-05-07 10:00:21 -0700182extern size_t __strlcpy_real(char* __restrict, const char* __restrict, size_t)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700183 __asm__(__USER_LABEL_PREFIX__ "strlcpy");
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700184extern size_t __strlcpy_chk(char *, const char *, size_t, size_t);
185
186__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700187size_t strlcpy(char* __restrict dest, const char* __restrict src, size_t size) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700188 size_t bos = __bos(dest);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700189
Nick Kralevich8bafa742013-06-20 12:17:44 -0700190#if !defined(__clang__)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700191 // Compiler doesn't know destination size. Don't call __strlcpy_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700192 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700193 return __strlcpy_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700194 }
195
196 // Compiler can prove, at compile time, that the passed in size
197 // is always <= the actual object size. Don't call __strlcpy_chk
198 if (__builtin_constant_p(size) && (size <= bos)) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700199 return __strlcpy_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700200 }
Nick Kralevich8bafa742013-06-20 12:17:44 -0700201#endif /* !defined(__clang__) */
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700202
203 return __strlcpy_chk(dest, src, size, bos);
204}
205
Nick Kralevich1c462b72013-05-07 10:00:21 -0700206extern size_t __strlcat_real(char* __restrict, const char* __restrict, size_t)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700207 __asm__(__USER_LABEL_PREFIX__ "strlcat");
Nick Kralevich1c462b72013-05-07 10:00:21 -0700208extern size_t __strlcat_chk(char* __restrict, const char* __restrict, size_t, size_t);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700209
210
211__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700212size_t strlcat(char* __restrict dest, const char* __restrict src, size_t size) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700213 size_t bos = __bos(dest);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700214
Nick Kralevicha6cde392013-06-29 08:15:25 -0700215#if !defined(__clang__)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700216 // Compiler doesn't know destination size. Don't call __strlcat_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700217 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700218 return __strlcat_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700219 }
220
221 // Compiler can prove, at compile time, that the passed in size
222 // is always <= the actual object size. Don't call __strlcat_chk
223 if (__builtin_constant_p(size) && (size <= bos)) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700224 return __strlcat_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700225 }
Nick Kralevicha6cde392013-06-29 08:15:25 -0700226#endif /* !defined(__clang__) */
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700227
228 return __strlcat_chk(dest, src, size, bos);
229}
230
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700231__BIONIC_FORTIFY_INLINE
232size_t strlen(const char *s) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700233 size_t bos = __bos(s);
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700234
Nick Kralevich16d1af12013-06-17 14:49:19 -0700235#if !defined(__clang__)
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700236 // Compiler doesn't know destination size. Don't call __strlen_chk
237 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800238 return __builtin_strlen(s);
239 }
240
241 size_t slen = __builtin_strlen(s);
242 if (__builtin_constant_p(slen)) {
243 return slen;
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700244 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700245#endif /* !defined(__clang__) */
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700246
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700247 return __strlen_chk(s, bos);
248}
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700249
Nick Kralevich049e5832012-11-30 15:15:58 -0800250__BIONIC_FORTIFY_INLINE
251char* strchr(const char *s, int c) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700252 size_t bos = __bos(s);
Nick Kralevich049e5832012-11-30 15:15:58 -0800253
Nick Kralevich16d1af12013-06-17 14:49:19 -0700254#if !defined(__clang__)
Nick Kralevich049e5832012-11-30 15:15:58 -0800255 // Compiler doesn't know destination size. Don't call __strchr_chk
256 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800257 return __builtin_strchr(s, c);
258 }
259
260 size_t slen = __builtin_strlen(s);
261 if (__builtin_constant_p(slen) && (slen < bos)) {
262 return __builtin_strchr(s, c);
Nick Kralevich049e5832012-11-30 15:15:58 -0800263 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700264#endif /* !defined(__clang__) */
Nick Kralevich049e5832012-11-30 15:15:58 -0800265
266 return __strchr_chk(s, c, bos);
267}
268
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800269__BIONIC_FORTIFY_INLINE
270char* strrchr(const char *s, int c) {
Nick Kralevich3b2e6bc2013-04-30 14:19:23 -0700271 size_t bos = __bos(s);
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800272
Nick Kralevich16d1af12013-06-17 14:49:19 -0700273#if !defined(__clang__)
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800274 // Compiler doesn't know destination size. Don't call __strrchr_chk
275 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800276 return __builtin_strrchr(s, c);
277 }
278
279 size_t slen = __builtin_strlen(s);
280 if (__builtin_constant_p(slen) && (slen < bos)) {
281 return __builtin_strrchr(s, c);
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800282 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700283#endif /* !defined(__clang__) */
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800284
285 return __strrchr_chk(s, c, bos);
286}
287
Nick Kralevich049e5832012-11-30 15:15:58 -0800288
Elliott Hughes890c8ed2013-03-22 10:58:55 -0700289#endif /* defined(__BIONIC_FORTIFY) */
Nick Kralevich0a230152012-06-04 15:20:25 -0700290
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800291__END_DECLS
292
293#endif /* _STRING_H_ */