blob: ac31f7d1eb794bd0502fa5f6f1d45f5d2cfdcb10 [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;
49extern char* strrchr(const char *, int) __purefunc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050
Nick Kralevicha6779072012-03-21 08:48:18 -070051extern size_t strlen(const char *) __purefunc;
Nick Kralevichcf870192013-05-30 16:48:53 -070052extern size_t __strlen_chk(const char *, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070053extern int strcmp(const char *, const char *) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070054extern char* strcpy(char* __restrict, const char* __restrict);
55extern char* strcat(char* __restrict, const char* __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056
Nick Kralevicha6779072012-03-21 08:48:18 -070057extern int strcasecmp(const char *, const char *) __purefunc;
58extern int strncasecmp(const char *, const char *, size_t) __purefunc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059extern char* strdup(const char *);
60
Nick Kralevicha6779072012-03-21 08:48:18 -070061extern char* strstr(const char *, const char *) __purefunc;
62extern char* strcasestr(const char *haystack, const char *needle) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070063extern char* strtok(char* __restrict, const char* __restrict);
64extern char* strtok_r(char* __restrict, const char* __restrict, char** __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080065
66extern char* strerror(int);
67extern int strerror_r(int errnum, char *buf, size_t n);
68
Nick Kralevicha6779072012-03-21 08:48:18 -070069extern size_t strnlen(const char *, size_t) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070070extern char* strncat(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071extern char* strndup(const char *, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070072extern int strncmp(const char *, const char *, size_t) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070073extern char* strncpy(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074
Nick Kralevich1c462b72013-05-07 10:00:21 -070075extern size_t strlcat(char* __restrict, const char* __restrict, size_t);
76extern size_t strlcpy(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080077
Nick Kralevicha6779072012-03-21 08:48:18 -070078extern size_t strcspn(const char *, const char *) __purefunc;
79extern char* strpbrk(const char *, const char *) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070080extern char* strsep(char** __restrict, const char* __restrict);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081extern size_t strspn(const char *, const char *);
82
83extern char* strsignal(int sig);
84
Nick Kralevicha6779072012-03-21 08:48:18 -070085extern int strcoll(const char *, const char *) __purefunc;
Nick Kralevich1c462b72013-05-07 10:00:21 -070086extern size_t strxfrm(char* __restrict, const char* __restrict, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080087
Elliott Hughes890c8ed2013-03-22 10:58:55 -070088#if defined(__BIONIC_FORTIFY)
Nick Kralevich0a230152012-06-04 15:20:25 -070089
Nick Kralevichb24c0632013-06-18 10:46:02 -070090__errordecl(__memcpy_dest_size_error, "memcpy called with size bigger than destination");
91__errordecl(__memcpy_src_size_error, "memcpy called with size bigger than source");
Nick Kralevichf3913b52012-07-12 15:10:03 -070092
Nick Kralevich0a230152012-06-04 15:20:25 -070093__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -070094void* memcpy(void* __restrict dest, const void* __restrict src, size_t copy_amount) {
Nick Kralevichf3913b52012-07-12 15:10:03 -070095 char *d = (char *) dest;
96 const char *s = (const char *) src;
97 size_t s_len = __builtin_object_size(s, 0);
98 size_t d_len = __builtin_object_size(d, 0);
99
100 if (__builtin_constant_p(copy_amount) && (copy_amount > d_len)) {
101 __memcpy_dest_size_error();
102 }
103
104 if (__builtin_constant_p(copy_amount) && (copy_amount > s_len)) {
105 __memcpy_src_size_error();
106 }
107
Nick Kralevichc37fc1a2012-07-13 17:49:10 -0700108 return __builtin___memcpy_chk(dest, src, copy_amount, d_len);
Nick Kralevich0a230152012-06-04 15:20:25 -0700109}
110
111__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700112void* memmove(void *dest, const void *src, size_t len) {
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700113 return __builtin___memmove_chk(dest, src, len, __builtin_object_size (dest, 0));
Nick Kralevich0a230152012-06-04 15:20:25 -0700114}
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
Nick Kralevichb24c0632013-06-18 10:46:02 -0700121__errordecl(__strncpy_error, "strncpy called with size bigger than buffer");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700122
Nick Kralevich0a230152012-06-04 15:20:25 -0700123__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700124char* strncpy(char* __restrict dest, const char* __restrict src, size_t n) {
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700125 size_t bos = __bos(dest);
126 if (__builtin_constant_p(n) && (n > bos)) {
127 __strncpy_error();
128 }
129 return __builtin___strncpy_chk(dest, src, n, bos);
Nick Kralevich0a230152012-06-04 15:20:25 -0700130}
131
132__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700133char* strcat(char* __restrict dest, const char* __restrict src) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700134 return __builtin___strcat_chk(dest, src, __bos(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700135}
136
137__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700138char *strncat(char* __restrict dest, const char* __restrict src, size_t n) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700139 return __builtin___strncat_chk(dest, src, n, __bos(dest));
Nick Kralevich0a230152012-06-04 15:20:25 -0700140}
141
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700142__BIONIC_FORTIFY_INLINE
Nick Kralevich16d1af12013-06-17 14:49:19 -0700143void* memset(void *s, int c, size_t n) {
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700144 return __builtin___memset_chk(s, c, n, __builtin_object_size (s, 0));
145}
146
Nick Kralevich16d1af12013-06-17 14:49:19 -0700147#if !defined(__clang__)
Nick Kralevich1c462b72013-05-07 10:00:21 -0700148extern size_t __strlcpy_real(char* __restrict, const char* __restrict, size_t)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700149 __asm__(__USER_LABEL_PREFIX__ "strlcpy");
Nick Kralevichb24c0632013-06-18 10:46:02 -0700150__errordecl(__strlcpy_error, "strlcpy called with size bigger than buffer");
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700151extern size_t __strlcpy_chk(char *, const char *, size_t, size_t);
152
153__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700154size_t strlcpy(char* __restrict dest, const char* __restrict src, size_t size) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700155 size_t bos = __bos(dest);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700156
157 // Compiler doesn't know destination size. Don't call __strlcpy_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700158 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700159 return __strlcpy_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700160 }
161
162 // Compiler can prove, at compile time, that the passed in size
163 // is always <= the actual object size. Don't call __strlcpy_chk
164 if (__builtin_constant_p(size) && (size <= bos)) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700165 return __strlcpy_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700166 }
167
168 // Compiler can prove, at compile time, that the passed in size
169 // is always > the actual object size. Force a compiler error.
170 if (__builtin_constant_p(size) && (size > bos)) {
171 __strlcpy_error();
172 }
173
174 return __strlcpy_chk(dest, src, size, bos);
175}
Nick Kralevich16d1af12013-06-17 14:49:19 -0700176#endif /* !defined(__clang__) */
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700177
Nick Kralevich16d1af12013-06-17 14:49:19 -0700178#if !defined(__clang__)
Nick Kralevich1c462b72013-05-07 10:00:21 -0700179extern size_t __strlcat_real(char* __restrict, const char* __restrict, size_t)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700180 __asm__(__USER_LABEL_PREFIX__ "strlcat");
Nick Kralevichb24c0632013-06-18 10:46:02 -0700181__errordecl(__strlcat_error, "strlcat called with size bigger than buffer");
Nick Kralevich1c462b72013-05-07 10:00:21 -0700182extern size_t __strlcat_chk(char* __restrict, const char* __restrict, size_t, size_t);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700183
184
185__BIONIC_FORTIFY_INLINE
Nick Kralevich1c462b72013-05-07 10:00:21 -0700186size_t strlcat(char* __restrict dest, const char* __restrict src, size_t size) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700187 size_t bos = __bos(dest);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700188
189 // Compiler doesn't know destination size. Don't call __strlcat_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700190 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700191 return __strlcat_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700192 }
193
194 // Compiler can prove, at compile time, that the passed in size
195 // is always <= the actual object size. Don't call __strlcat_chk
196 if (__builtin_constant_p(size) && (size <= bos)) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700197 return __strlcat_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700198 }
199
200 // Compiler can prove, at compile time, that the passed in size
201 // is always > the actual object size. Force a compiler error.
202 if (__builtin_constant_p(size) && (size > bos)) {
203 __strlcat_error();
204 }
205
206 return __strlcat_chk(dest, src, size, bos);
207}
Nick Kralevich16d1af12013-06-17 14:49:19 -0700208#endif /* !defined(__clang__) */
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700209
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700210__BIONIC_FORTIFY_INLINE
211size_t strlen(const char *s) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700212 size_t bos = __bos(s);
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700213
Nick Kralevich16d1af12013-06-17 14:49:19 -0700214#if !defined(__clang__)
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700215 // Compiler doesn't know destination size. Don't call __strlen_chk
216 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800217 return __builtin_strlen(s);
218 }
219
220 size_t slen = __builtin_strlen(s);
221 if (__builtin_constant_p(slen)) {
222 return slen;
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700223 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700224#endif /* !defined(__clang__) */
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700225
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700226 return __strlen_chk(s, bos);
227}
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700228
Nick Kralevich049e5832012-11-30 15:15:58 -0800229extern char* __strchr_chk(const char *, int, size_t);
230
231__BIONIC_FORTIFY_INLINE
232char* strchr(const char *s, int c) {
Nick Kralevich9020fd52013-04-30 11:31:35 -0700233 size_t bos = __bos(s);
Nick Kralevich049e5832012-11-30 15:15:58 -0800234
Nick Kralevich16d1af12013-06-17 14:49:19 -0700235#if !defined(__clang__)
Nick Kralevich049e5832012-11-30 15:15:58 -0800236 // Compiler doesn't know destination size. Don't call __strchr_chk
237 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800238 return __builtin_strchr(s, c);
239 }
240
241 size_t slen = __builtin_strlen(s);
242 if (__builtin_constant_p(slen) && (slen < bos)) {
243 return __builtin_strchr(s, c);
Nick Kralevich049e5832012-11-30 15:15:58 -0800244 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700245#endif /* !defined(__clang__) */
Nick Kralevich049e5832012-11-30 15:15:58 -0800246
247 return __strchr_chk(s, c, bos);
248}
249
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800250extern char* __strrchr_chk(const char *, int, size_t);
251
252__BIONIC_FORTIFY_INLINE
253char* strrchr(const char *s, int c) {
Nick Kralevich3b2e6bc2013-04-30 14:19:23 -0700254 size_t bos = __bos(s);
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800255
Nick Kralevich16d1af12013-06-17 14:49:19 -0700256#if !defined(__clang__)
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800257 // Compiler doesn't know destination size. Don't call __strrchr_chk
258 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevicha44e9af2013-01-17 15:41:33 -0800259 return __builtin_strrchr(s, c);
260 }
261
262 size_t slen = __builtin_strlen(s);
263 if (__builtin_constant_p(slen) && (slen < bos)) {
264 return __builtin_strrchr(s, c);
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800265 }
Nick Kralevich16d1af12013-06-17 14:49:19 -0700266#endif /* !defined(__clang__) */
Nick Kralevich9a4d3052012-12-03 10:36:13 -0800267
268 return __strrchr_chk(s, c, bos);
269}
270
Nick Kralevich049e5832012-11-30 15:15:58 -0800271
Elliott Hughes890c8ed2013-03-22 10:58:55 -0700272#endif /* defined(__BIONIC_FORTIFY) */
Nick Kralevich0a230152012-06-04 15:20:25 -0700273
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800274__END_DECLS
275
276#endif /* _STRING_H_ */