blob: d1dc4c41b618b2ff9377b1014ca074d5b950ec9a [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
37extern void* memccpy(void *, const void *, 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;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041extern void* memcpy(void *, const void *, size_t);
42extern 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;
48extern char* rindex(const char *, int) __purefunc;
49extern char* strchr(const char *, int) __purefunc;
50extern char* strrchr(const char *, int) __purefunc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051
Nick Kralevicha6779072012-03-21 08:48:18 -070052extern size_t strlen(const char *) __purefunc;
53extern int strcmp(const char *, const char *) __purefunc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054extern char* strcpy(char *, const char *);
55extern char* strcat(char *, const char *);
56
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;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063extern char* strtok(char *, const char *);
64extern char* strtok_r(char *, const char *, char**);
65
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;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080070extern char* strncat(char *, const char *, size_t);
71extern char* strndup(const char *, size_t);
Nick Kralevicha6779072012-03-21 08:48:18 -070072extern int strncmp(const char *, const char *, size_t) __purefunc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073extern char* strncpy(char *, const char *, size_t);
74
75extern size_t strlcat(char *, const char *, size_t);
76extern size_t strlcpy(char *, const char *, size_t);
77
Nick Kralevicha6779072012-03-21 08:48:18 -070078extern size_t strcspn(const char *, const char *) __purefunc;
79extern char* strpbrk(const char *, const char *) __purefunc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080extern char* strsep(char **, const char *);
81extern 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;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086extern size_t strxfrm(char *, const char *, size_t);
87
Nick Kralevich71a18dd2012-06-07 14:01:26 -070088#if defined(__BIONIC_FORTIFY_INLINE)
Nick Kralevich0a230152012-06-04 15:20:25 -070089
Nick Kralevichf3913b52012-07-12 15:10:03 -070090extern void __memcpy_dest_size_error()
91 __attribute__((__error__("memcpy called with size bigger than destination")));
92extern void __memcpy_src_size_error()
93 __attribute__((__error__("memcpy called with size bigger than source")));
94extern void __memcpy_overlap_error()
95 __attribute__((__error__("memcpy called with overlapping regions")));
Nick Kralevichf3913b52012-07-12 15:10:03 -070096
Nick Kralevich0a230152012-06-04 15:20:25 -070097__BIONIC_FORTIFY_INLINE
Nick Kralevichf3913b52012-07-12 15:10:03 -070098void *memcpy (void *dest, const void *src, size_t copy_amount) {
99 char *d = (char *) dest;
100 const char *s = (const char *) src;
101 size_t s_len = __builtin_object_size(s, 0);
102 size_t d_len = __builtin_object_size(d, 0);
103
104 if (__builtin_constant_p(copy_amount) && (copy_amount > d_len)) {
105 __memcpy_dest_size_error();
106 }
107
108 if (__builtin_constant_p(copy_amount) && (copy_amount > s_len)) {
109 __memcpy_src_size_error();
110 }
111
112 if (__builtin_constant_p(d - s) && __builtin_constant_p(copy_amount)
113 && (((size_t)(d - s) < copy_amount) || ((size_t)(s - d) < copy_amount))) {
114 __memcpy_overlap_error();
115 }
116
Nick Kralevichc37fc1a2012-07-13 17:49:10 -0700117 return __builtin___memcpy_chk(dest, src, copy_amount, d_len);
Nick Kralevich0a230152012-06-04 15:20:25 -0700118}
119
120__BIONIC_FORTIFY_INLINE
121void *memmove (void *dest, const void *src, size_t len) {
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700122 return __builtin___memmove_chk(dest, src, len, __builtin_object_size (dest, 0));
Nick Kralevich0a230152012-06-04 15:20:25 -0700123}
124
125__BIONIC_FORTIFY_INLINE
126char *strcpy(char *dest, const char *src) {
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700127 return __builtin___strcpy_chk(dest, src, __builtin_object_size (dest, 0));
Nick Kralevich0a230152012-06-04 15:20:25 -0700128}
129
130__BIONIC_FORTIFY_INLINE
131char *strncpy(char *dest, const char *src, size_t n) {
132 return __builtin___strncpy_chk(dest, src, n, __builtin_object_size (dest, 0));
133}
134
135__BIONIC_FORTIFY_INLINE
136char *strcat(char *dest, const char *src) {
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700137 return __builtin___strcat_chk(dest, src, __builtin_object_size (dest, 0));
Nick Kralevich0a230152012-06-04 15:20:25 -0700138}
139
140__BIONIC_FORTIFY_INLINE
141char *strncat(char *dest, const char *src, size_t n) {
142 return __builtin___strncat_chk(dest, src, n, __builtin_object_size (dest, 0));
143}
144
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700145__BIONIC_FORTIFY_INLINE
146void *memset (void *s, int c, size_t n) {
147 return __builtin___memset_chk(s, c, n, __builtin_object_size (s, 0));
148}
149
Shih-wei Liaod6006172012-08-06 10:57:37 -0700150#if !defined(__clang__)
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700151extern size_t __strlcpy_real(char *, const char *, size_t)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700152 __asm__(__USER_LABEL_PREFIX__ "strlcpy");
153extern void __strlcpy_error()
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700154 __attribute__((__error__("strlcpy called with size bigger than buffer")));
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700155extern size_t __strlcpy_chk(char *, const char *, size_t, size_t);
156
157__BIONIC_FORTIFY_INLINE
158size_t strlcpy(char *dest, const char *src, size_t size) {
159 size_t bos = __builtin_object_size(dest, 0);
160
161 // Compiler doesn't know destination size. Don't call __strlcpy_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700162 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700163 return __strlcpy_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700164 }
165
166 // Compiler can prove, at compile time, that the passed in size
167 // is always <= the actual object size. Don't call __strlcpy_chk
168 if (__builtin_constant_p(size) && (size <= bos)) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700169 return __strlcpy_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700170 }
171
172 // Compiler can prove, at compile time, that the passed in size
173 // is always > the actual object size. Force a compiler error.
174 if (__builtin_constant_p(size) && (size > bos)) {
175 __strlcpy_error();
176 }
177
178 return __strlcpy_chk(dest, src, size, bos);
179}
180
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700181extern size_t __strlcat_real(char *, const char *, size_t)
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700182 __asm__(__USER_LABEL_PREFIX__ "strlcat");
183extern void __strlcat_error()
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700184 __attribute__((__error__("strlcat called with size bigger than buffer")));
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700185extern size_t __strlcat_chk(char *, const char *, size_t, size_t);
186
187
188__BIONIC_FORTIFY_INLINE
189size_t strlcat(char *dest, const char *src, size_t size) {
190 size_t bos = __builtin_object_size(dest, 0);
191
192 // Compiler doesn't know destination size. Don't call __strlcat_chk
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700193 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700194 return __strlcat_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700195 }
196
197 // Compiler can prove, at compile time, that the passed in size
198 // is always <= the actual object size. Don't call __strlcat_chk
199 if (__builtin_constant_p(size) && (size <= bos)) {
Nick Kralevichcb228fb2012-06-26 16:05:19 -0700200 return __strlcat_real(dest, src, size);
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700201 }
202
203 // Compiler can prove, at compile time, that the passed in size
204 // is always > the actual object size. Force a compiler error.
205 if (__builtin_constant_p(size) && (size > bos)) {
206 __strlcat_error();
207 }
208
209 return __strlcat_chk(dest, src, size, bos);
210}
211
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700212__purefunc extern size_t __strlen_real(const char *)
213 __asm__(__USER_LABEL_PREFIX__ "strlen");
214extern size_t __strlen_chk(const char *, size_t);
215
216__BIONIC_FORTIFY_INLINE
217size_t strlen(const char *s) {
218 size_t bos = __builtin_object_size(s, 0);
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700219
220 // Compiler doesn't know destination size. Don't call __strlen_chk
221 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700222 return __strlen_real(s);
223 }
Nick Kralevich9b6cc222012-07-13 14:46:36 -0700224
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700225 return __strlen_chk(s, bos);
226}
Shih-wei Liao9a3d53f2012-08-06 09:50:56 -0700227#endif /* !defined(__clang__) */
Nick Kralevich260bf8c2012-07-13 11:27:06 -0700228
Nick Kralevich8df49ad2012-06-13 16:57:27 -0700229
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700230#endif /* defined(__BIONIC_FORTIFY_INLINE) */
Nick Kralevich0a230152012-06-04 15:20:25 -0700231
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800232__END_DECLS
233
234#endif /* _STRING_H_ */