blob: 10ff722305bbb9a0862b9d1d5a8c51abdfc61b23 [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>
34
35__BEGIN_DECLS
36
Nick Kralevich1c462b72013-05-07 10:00:21 -070037extern void* memccpy(void* __restrict, const void* __restrict, int, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070038extern void* memchr(const void *, int, size_t) __purefunc;
39extern void* memrchr(const void *, int, size_t) __purefunc;
40extern int memcmp(const void *, const void *, size_t) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070041extern void* memcpy(void* __restrict, const void* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042extern void* memmove(void *, const void *, size_t);
43extern void* memset(void *, int, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070044extern void* memmem(const void *, size_t, const void *, size_t) __purefunc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080045extern void memswap(void *, void *, size_t);
46
Nick Kralevicha6779072012-03-21 08:48:18 -070047extern char* index(const char *, int) __purefunc;
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;
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;
Nick Kralevich1c462b72013-05-07 10:00:21 -070056extern char* strcpy(char* __restrict, const char* __restrict);
57extern char* strcat(char* __restrict, const char* __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080058
Nick Kralevicha6779072012-03-21 08:48:18 -070059extern int strcasecmp(const char *, const char *) __purefunc;
60extern int strncasecmp(const char *, const char *, size_t) __purefunc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061extern char* strdup(const char *);
62
Nick Kralevicha6779072012-03-21 08:48:18 -070063extern char* strstr(const char *, const char *) __purefunc;
64extern char* strcasestr(const char *haystack, const char *needle) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070065extern char* strtok(char* __restrict, const char* __restrict);
66extern char* strtok_r(char* __restrict, const char* __restrict, char** __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080067
68extern char* strerror(int);
69extern int strerror_r(int errnum, char *buf, size_t n);
70
Nick Kralevicha6779072012-03-21 08:48:18 -070071extern size_t strnlen(const char *, size_t) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070072extern char* strncat(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073extern char* strndup(const char *, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070074extern int strncmp(const char *, const char *, size_t) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070075extern char* strncpy(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080076
Nick Kralevich1c462b72013-05-07 10:00:21 -070077extern size_t strlcat(char* __restrict, const char* __restrict, size_t);
78extern size_t strlcpy(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079
Nick Kralevicha6779072012-03-21 08:48:18 -070080extern size_t strcspn(const char *, const char *) __purefunc;
81extern char* strpbrk(const char *, const char *) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070082extern char* strsep(char** __restrict, const char* __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083extern size_t strspn(const char *, const char *);
84
85extern char* strsignal(int sig);
86
Nick Kralevicha6779072012-03-21 08:48:18 -070087extern int strcoll(const char *, const char *) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070088extern size_t strxfrm(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080089
Elliott Hughes890c8ed2013-03-22 10:58:55 -070090#if defined(__BIONIC_FORTIFY)
Nick Kralevich0a230152012-06-04 15:20:25 -070091
Nick Kralevichb24c0632013-06-18 10:46:02 -070092__errordecl(__memcpy_dest_size_error, "memcpy called with size bigger than destination");
93__errordecl(__memcpy_src_size_error, "memcpy called with size bigger than source");
Nick Kralevichf3913b52012-07-12 15:10:03 -070094
Nick Kralevich0a230152012-06-04 15:20:25 -070095__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -070096void* memcpy(void* __restrict dest, const void* __restrict src, size_t copy_amount) {
Nick Kralevichf3913b52012-07-12 15:10:03 -070097 char *d = (char *) dest;
98 const char *s = (const char *) src;
Nick Kralevichbd8e6742013-08-28 13:22:52 -070099 size_t s_len = __bos0(s);
100 size_t d_len = __bos0(d);
Nick Kralevichf3913b52012-07-12 15:10:03 -0700101
102 if (__builtin_constant_p(copy_amount) && (copy_amount > d_len)) {
103 __memcpy_dest_size_error();
104 }
105
106 if (__builtin_constant_p(copy_amount) && (copy_amount > s_len)) {
107 __memcpy_src_size_error();
108 }
109
Nick Kralevichc37fc1a2012-07-13 17:49:10 -0700110 return __builtin___memcpy_chk(dest, src, copy_amount, d_len);
Nick Kralevich0a230152012-06-04 15:20:25 -0700111}
112
113__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700114void* memmove(void *dest, const void *src, size_t len) {
Nick Kralevichbd8e6742013-08-28 13:22:52 -0700115 return __builtin___memmove_chk(dest, src, len, __bos0(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700116}
117
118__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700119char* strcpy(char* __restrict dest, const char* __restrict src) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700120 return __builtin___strcpy_chk(dest, src, __bos(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700121}
122
Nick Kralevichb24c0632013-06-18 10:46:02 -0700123__errordecl(__strncpy_error, "strncpy called with size bigger than buffer");
Nick Kralevich93501d32013-08-28 10:47:43 -0700124extern char* __strncpy_chk2(char* __restrict, const char* __restrict, size_t, size_t, size_t);
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700125
Nick Kralevich0a230152012-06-04 15:20:25 -0700126__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700127char* strncpy(char* __restrict dest, const char* __restrict src, size_t n) {
Nick Kralevich93501d32013-08-28 10:47:43 -0700128 size_t bos_dest = __bos(dest);
129 size_t bos_src = __bos(src);
130 if (__builtin_constant_p(n) && (n > bos_dest)) {
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700131 __strncpy_error();
132 }
Nick Kralevich93501d32013-08-28 10:47:43 -0700133
134 if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
135 return __builtin___strncpy_chk(dest, src, n, bos_dest);
136 }
137
138 size_t slen = __builtin_strlen(src);
139 if (__builtin_constant_p(slen)) {
140 return __builtin___strncpy_chk(dest, src, n, bos_dest);
141 }
142
143 return __strncpy_chk2(dest, src, n, bos_dest, bos_src);
Nick Kralevich0a230152012-06-04 15:20:25 -0700144}
145
146__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700147char* strcat(char* __restrict dest, const char* __restrict src) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700148 return __builtin___strcat_chk(dest, src, __bos(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700149}
150
151__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700152char *strncat(char* __restrict dest, const char* __restrict src, size_t n) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700153 return __builtin___strncat_chk(dest, src, n, __bos(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700154}
155
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700156__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700157void* memset(void *s, int c, size_t n) {
Nick Kralevichbd8e6742013-08-28 13:22:52 -0700158 return __builtin___memset_chk(s, c, n, __bos0(s));
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700159}
160
Nick Kralevich1c462b72013-05-07 10:00:21 -0700161extern size_t __strlcpy_real(char* __restrict, const char* __restrict, size_t)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700162 __asm__(__USER_LABEL_PREFIX__ "strlcpy");
Nick Kralevichb24c0632013-06-18 10:46:02 -0700163__errordecl(__strlcpy_error, "strlcpy called with size bigger than buffer");
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700164extern size_t __strlcpy_chk(char *, const char *, size_t, size_t);
165
166__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700167size_t strlcpy(char* __restrict dest, const char* __restrict src, size_t size) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700168 size_t bos = __bos(dest);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700169
Nick Kralevich8bafa742013-06-20 12:17:44 -0700170#if !defined(__clang__)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700171 // Compiler doesn't know destination size. Don't call __strlcpy_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700172 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700173 return __strlcpy_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700174 }
175
176 // Compiler can prove, at compile time, that the passed in size
177 // is always <= the actual object size. Don't call __strlcpy_chk
178 if (__builtin_constant_p(size) && (size <= bos)) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700179 return __strlcpy_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700180 }
181
182 // Compiler can prove, at compile time, that the passed in size
183 // is always > the actual object size. Force a compiler error.
184 if (__builtin_constant_p(size) && (size > bos)) {
185 __strlcpy_error();
186 }
Nick Kralevich8bafa742013-06-20 12:17:44 -0700187#endif /* !defined(__clang__) */
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700188
189 return __strlcpy_chk(dest, src, size, bos);
190}
191
Nick Kralevich1c462b72013-05-07 10:00:21 -0700192extern size_t __strlcat_real(char* __restrict, const char* __restrict, size_t)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700193 __asm__(__USER_LABEL_PREFIX__ "strlcat");
Nick Kralevichb24c0632013-06-18 10:46:02 -0700194__errordecl(__strlcat_error, "strlcat called with size bigger than buffer");
Nick Kralevich1c462b72013-05-07 10:00:21 -0700195extern size_t __strlcat_chk(char* __restrict, const char* __restrict, size_t, size_t);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700196
197
198__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700199size_t strlcat(char* __restrict dest, const char* __restrict src, size_t size) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700200 size_t bos = __bos(dest);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700201
Nick Kralevicha6cde392013-06-29 08:15:25 -0700202#if !defined(__clang__)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700203 // Compiler doesn't know destination size. Don't call __strlcat_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700204 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700205 return __strlcat_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700206 }
207
208 // Compiler can prove, at compile time, that the passed in size
209 // is always <= the actual object size. Don't call __strlcat_chk
210 if (__builtin_constant_p(size) && (size <= bos)) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700211 return __strlcat_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700212 }
213
214 // Compiler can prove, at compile time, that the passed in size
215 // is always > the actual object size. Force a compiler error.
216 if (__builtin_constant_p(size) && (size > bos)) {
217 __strlcat_error();
218 }
Nick Kralevicha6cde392013-06-29 08:15:25 -0700219#endif /* !defined(__clang__) */
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700220
221 return __strlcat_chk(dest, src, size, bos);
222}
223
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700224__BIONIC_FORTIFY_INLINE
225size_t strlen(const char *s) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700226 size_t bos = __bos(s);
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700227
Nick Kralevich16d1af12013-06-17 14:49:19 -0700228#if !defined(__clang__)
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700229 // Compiler doesn't know destination size. Don't call __strlen_chk
230 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800231 return __builtin_strlen(s);
232 }
233
234 size_t slen = __builtin_strlen(s);
235 if (__builtin_constant_p(slen)) {
236 return slen;
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700237 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700238#endif /* !defined(__clang__) */
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700239
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700240 return __strlen_chk(s, bos);
241}
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700242
Nick Kralevich049e5832012-11-30 15:15:58 -0800243__BIONIC_FORTIFY_INLINE
244char* strchr(const char *s, int c) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700245 size_t bos = __bos(s);
Nick Kralevich049e5832012-11-30 15:15:58 -0800246
Nick Kralevich16d1af12013-06-17 14:49:19 -0700247#if !defined(__clang__)
Nick Kralevich049e5832012-11-30 15:15:58 -0800248 // Compiler doesn't know destination size. Don't call __strchr_chk
249 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800250 return __builtin_strchr(s, c);
251 }
252
253 size_t slen = __builtin_strlen(s);
254 if (__builtin_constant_p(slen) && (slen < bos)) {
255 return __builtin_strchr(s, c);
Nick Kralevich049e5832012-11-30 15:15:58 -0800256 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700257#endif /* !defined(__clang__) */
Nick Kralevich049e5832012-11-30 15:15:58 -0800258
259 return __strchr_chk(s, c, bos);
260}
261
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800262extern char* __strrchr_chk(const char *, int, size_t);
263
264__BIONIC_FORTIFY_INLINE
265char* strrchr(const char *s, int c) {
Nick Kralevich3b2e6bc2013-04-30 14:19:23 -0700266 size_t bos = __bos(s);
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800267
Nick Kralevich16d1af12013-06-17 14:49:19 -0700268#if !defined(__clang__)
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800269 // Compiler doesn't know destination size. Don't call __strrchr_chk
270 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800271 return __builtin_strrchr(s, c);
272 }
273
274 size_t slen = __builtin_strlen(s);
275 if (__builtin_constant_p(slen) && (slen < bos)) {
276 return __builtin_strrchr(s, c);
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800277 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700278#endif /* !defined(__clang__) */
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800279
280 return __strrchr_chk(s, c, bos);
281}
282
Nick Kralevich049e5832012-11-30 15:15:58 -0800283
Elliott Hughes890c8ed2013-03-22 10:58:55 -0700284#endif /* defined(__BIONIC_FORTIFY) */
Nick Kralevich0a230152012-06-04 15:20:25 -0700285
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800286__END_DECLS
287
288#endif /* _STRING_H_ */