The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 37 | extern void* memccpy(void *, const void *, int, size_t); |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 38 | extern void* memchr(const void *, int, size_t) __purefunc; |
| 39 | extern void* memrchr(const void *, int, size_t) __purefunc; |
| 40 | extern int memcmp(const void *, const void *, size_t) __purefunc; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 41 | extern void* memcpy(void *, const void *, size_t); |
| 42 | extern void* memmove(void *, const void *, size_t); |
| 43 | extern void* memset(void *, int, size_t); |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 44 | extern void* memmem(const void *, size_t, const void *, size_t) __purefunc; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 45 | extern void memswap(void *, void *, size_t); |
| 46 | |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 47 | extern char* index(const char *, int) __purefunc; |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 48 | extern char* strchr(const char *, int) __purefunc; |
| 49 | extern char* strrchr(const char *, int) __purefunc; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 50 | |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 51 | extern size_t strlen(const char *) __purefunc; |
| 52 | extern int strcmp(const char *, const char *) __purefunc; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 53 | extern char* strcpy(char *, const char *); |
| 54 | extern char* strcat(char *, const char *); |
| 55 | |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 56 | extern int strcasecmp(const char *, const char *) __purefunc; |
| 57 | extern int strncasecmp(const char *, const char *, size_t) __purefunc; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 58 | extern char* strdup(const char *); |
| 59 | |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 60 | extern char* strstr(const char *, const char *) __purefunc; |
| 61 | extern char* strcasestr(const char *haystack, const char *needle) __purefunc; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 62 | extern char* strtok(char *, const char *); |
| 63 | extern char* strtok_r(char *, const char *, char**); |
| 64 | |
| 65 | extern char* strerror(int); |
| 66 | extern int strerror_r(int errnum, char *buf, size_t n); |
| 67 | |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 68 | extern size_t strnlen(const char *, size_t) __purefunc; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 69 | extern char* strncat(char *, const char *, size_t); |
| 70 | extern char* strndup(const char *, size_t); |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 71 | extern int strncmp(const char *, const char *, size_t) __purefunc; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 72 | extern char* strncpy(char *, const char *, size_t); |
| 73 | |
| 74 | extern size_t strlcat(char *, const char *, size_t); |
| 75 | extern size_t strlcpy(char *, const char *, size_t); |
| 76 | |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 77 | extern size_t strcspn(const char *, const char *) __purefunc; |
| 78 | extern char* strpbrk(const char *, const char *) __purefunc; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 79 | extern char* strsep(char **, const char *); |
| 80 | extern size_t strspn(const char *, const char *); |
| 81 | |
| 82 | extern char* strsignal(int sig); |
| 83 | |
Nick Kralevich | a677907 | 2012-03-21 08:48:18 -0700 | [diff] [blame] | 84 | extern int strcoll(const char *, const char *) __purefunc; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 85 | extern size_t strxfrm(char *, const char *, size_t); |
| 86 | |
Elliott Hughes | 890c8ed | 2013-03-22 10:58:55 -0700 | [diff] [blame] | 87 | #if defined(__BIONIC_FORTIFY) |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 88 | |
Nick Kralevich | f3913b5 | 2012-07-12 15:10:03 -0700 | [diff] [blame] | 89 | extern void __memcpy_dest_size_error() |
| 90 | __attribute__((__error__("memcpy called with size bigger than destination"))); |
| 91 | extern void __memcpy_src_size_error() |
| 92 | __attribute__((__error__("memcpy called with size bigger than source"))); |
Nick Kralevich | f3913b5 | 2012-07-12 15:10:03 -0700 | [diff] [blame] | 93 | |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 94 | __BIONIC_FORTIFY_INLINE |
Nick Kralevich | f3913b5 | 2012-07-12 15:10:03 -0700 | [diff] [blame] | 95 | void *memcpy (void *dest, const void *src, size_t copy_amount) { |
| 96 | char *d = (char *) dest; |
| 97 | const char *s = (const char *) src; |
| 98 | size_t s_len = __builtin_object_size(s, 0); |
| 99 | size_t d_len = __builtin_object_size(d, 0); |
| 100 | |
| 101 | if (__builtin_constant_p(copy_amount) && (copy_amount > d_len)) { |
| 102 | __memcpy_dest_size_error(); |
| 103 | } |
| 104 | |
| 105 | if (__builtin_constant_p(copy_amount) && (copy_amount > s_len)) { |
| 106 | __memcpy_src_size_error(); |
| 107 | } |
| 108 | |
Nick Kralevich | c37fc1a | 2012-07-13 17:49:10 -0700 | [diff] [blame] | 109 | return __builtin___memcpy_chk(dest, src, copy_amount, d_len); |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | __BIONIC_FORTIFY_INLINE |
| 113 | void *memmove (void *dest, const void *src, size_t len) { |
Nick Kralevich | 71a18dd | 2012-06-07 14:01:26 -0700 | [diff] [blame] | 114 | return __builtin___memmove_chk(dest, src, len, __builtin_object_size (dest, 0)); |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | __BIONIC_FORTIFY_INLINE |
| 118 | char *strcpy(char *dest, const char *src) { |
Nick Kralevich | 71a18dd | 2012-06-07 14:01:26 -0700 | [diff] [blame] | 119 | return __builtin___strcpy_chk(dest, src, __builtin_object_size (dest, 0)); |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame^] | 122 | extern void __strncpy_error() |
| 123 | __attribute__((__error__("strncpy called with size bigger than buffer"))); |
| 124 | |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 125 | __BIONIC_FORTIFY_INLINE |
| 126 | char *strncpy(char *dest, const char *src, size_t n) { |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame^] | 127 | size_t bos = __bos(dest); |
| 128 | if (__builtin_constant_p(n) && (n > bos)) { |
| 129 | __strncpy_error(); |
| 130 | } |
| 131 | return __builtin___strncpy_chk(dest, src, n, bos); |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | __BIONIC_FORTIFY_INLINE |
| 135 | char *strcat(char *dest, const char *src) { |
Nick Kralevich | 71a18dd | 2012-06-07 14:01:26 -0700 | [diff] [blame] | 136 | return __builtin___strcat_chk(dest, src, __builtin_object_size (dest, 0)); |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | __BIONIC_FORTIFY_INLINE |
| 140 | char *strncat(char *dest, const char *src, size_t n) { |
| 141 | return __builtin___strncat_chk(dest, src, n, __builtin_object_size (dest, 0)); |
| 142 | } |
| 143 | |
Nick Kralevich | 71a18dd | 2012-06-07 14:01:26 -0700 | [diff] [blame] | 144 | __BIONIC_FORTIFY_INLINE |
| 145 | void *memset (void *s, int c, size_t n) { |
| 146 | return __builtin___memset_chk(s, c, n, __builtin_object_size (s, 0)); |
| 147 | } |
| 148 | |
Nick Kralevich | cb228fb | 2012-06-26 16:05:19 -0700 | [diff] [blame] | 149 | extern size_t __strlcpy_real(char *, const char *, size_t) |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 150 | __asm__(__USER_LABEL_PREFIX__ "strlcpy"); |
| 151 | extern void __strlcpy_error() |
Nick Kralevich | cb228fb | 2012-06-26 16:05:19 -0700 | [diff] [blame] | 152 | __attribute__((__error__("strlcpy called with size bigger than buffer"))); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 153 | extern size_t __strlcpy_chk(char *, const char *, size_t, size_t); |
| 154 | |
| 155 | __BIONIC_FORTIFY_INLINE |
| 156 | size_t strlcpy(char *dest, const char *src, size_t size) { |
| 157 | size_t bos = __builtin_object_size(dest, 0); |
| 158 | |
| 159 | // Compiler doesn't know destination size. Don't call __strlcpy_chk |
Nick Kralevich | 9b6cc22 | 2012-07-13 14:46:36 -0700 | [diff] [blame] | 160 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Nick Kralevich | cb228fb | 2012-06-26 16:05:19 -0700 | [diff] [blame] | 161 | return __strlcpy_real(dest, src, size); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Compiler can prove, at compile time, that the passed in size |
| 165 | // is always <= the actual object size. Don't call __strlcpy_chk |
| 166 | if (__builtin_constant_p(size) && (size <= bos)) { |
Nick Kralevich | cb228fb | 2012-06-26 16:05:19 -0700 | [diff] [blame] | 167 | return __strlcpy_real(dest, src, size); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // Compiler can prove, at compile time, that the passed in size |
| 171 | // is always > the actual object size. Force a compiler error. |
| 172 | if (__builtin_constant_p(size) && (size > bos)) { |
| 173 | __strlcpy_error(); |
| 174 | } |
| 175 | |
| 176 | return __strlcpy_chk(dest, src, size, bos); |
| 177 | } |
| 178 | |
Nick Kralevich | cb228fb | 2012-06-26 16:05:19 -0700 | [diff] [blame] | 179 | extern size_t __strlcat_real(char *, const char *, size_t) |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 180 | __asm__(__USER_LABEL_PREFIX__ "strlcat"); |
| 181 | extern void __strlcat_error() |
Nick Kralevich | cb228fb | 2012-06-26 16:05:19 -0700 | [diff] [blame] | 182 | __attribute__((__error__("strlcat called with size bigger than buffer"))); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 183 | extern size_t __strlcat_chk(char *, const char *, size_t, size_t); |
| 184 | |
| 185 | |
| 186 | __BIONIC_FORTIFY_INLINE |
| 187 | size_t strlcat(char *dest, const char *src, size_t size) { |
| 188 | size_t bos = __builtin_object_size(dest, 0); |
| 189 | |
| 190 | // Compiler doesn't know destination size. Don't call __strlcat_chk |
Nick Kralevich | 9b6cc22 | 2012-07-13 14:46:36 -0700 | [diff] [blame] | 191 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Nick Kralevich | cb228fb | 2012-06-26 16:05:19 -0700 | [diff] [blame] | 192 | return __strlcat_real(dest, src, size); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Compiler can prove, at compile time, that the passed in size |
| 196 | // is always <= the actual object size. Don't call __strlcat_chk |
| 197 | if (__builtin_constant_p(size) && (size <= bos)) { |
Nick Kralevich | cb228fb | 2012-06-26 16:05:19 -0700 | [diff] [blame] | 198 | return __strlcat_real(dest, src, size); |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // Compiler can prove, at compile time, that the passed in size |
| 202 | // is always > the actual object size. Force a compiler error. |
| 203 | if (__builtin_constant_p(size) && (size > bos)) { |
| 204 | __strlcat_error(); |
| 205 | } |
| 206 | |
| 207 | return __strlcat_chk(dest, src, size, bos); |
| 208 | } |
| 209 | |
Nick Kralevich | 260bf8c | 2012-07-13 11:27:06 -0700 | [diff] [blame] | 210 | extern size_t __strlen_chk(const char *, size_t); |
| 211 | |
| 212 | __BIONIC_FORTIFY_INLINE |
| 213 | size_t strlen(const char *s) { |
| 214 | size_t bos = __builtin_object_size(s, 0); |
Nick Kralevich | 9b6cc22 | 2012-07-13 14:46:36 -0700 | [diff] [blame] | 215 | |
| 216 | // Compiler doesn't know destination size. Don't call __strlen_chk |
| 217 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Nick Kralevich | a44e9af | 2013-01-17 15:41:33 -0800 | [diff] [blame] | 218 | return __builtin_strlen(s); |
| 219 | } |
| 220 | |
| 221 | size_t slen = __builtin_strlen(s); |
| 222 | if (__builtin_constant_p(slen)) { |
| 223 | return slen; |
Nick Kralevich | 260bf8c | 2012-07-13 11:27:06 -0700 | [diff] [blame] | 224 | } |
Nick Kralevich | 9b6cc22 | 2012-07-13 14:46:36 -0700 | [diff] [blame] | 225 | |
Nick Kralevich | 260bf8c | 2012-07-13 11:27:06 -0700 | [diff] [blame] | 226 | return __strlen_chk(s, bos); |
| 227 | } |
Nick Kralevich | 8df49ad | 2012-06-13 16:57:27 -0700 | [diff] [blame] | 228 | |
Nick Kralevich | 049e583 | 2012-11-30 15:15:58 -0800 | [diff] [blame] | 229 | extern char* __strchr_chk(const char *, int, size_t); |
| 230 | |
| 231 | __BIONIC_FORTIFY_INLINE |
| 232 | char* strchr(const char *s, int c) { |
| 233 | size_t bos = __builtin_object_size(s, 0); |
| 234 | |
| 235 | // Compiler doesn't know destination size. Don't call __strchr_chk |
| 236 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Nick Kralevich | a44e9af | 2013-01-17 15:41:33 -0800 | [diff] [blame] | 237 | return __builtin_strchr(s, c); |
| 238 | } |
| 239 | |
| 240 | size_t slen = __builtin_strlen(s); |
| 241 | if (__builtin_constant_p(slen) && (slen < bos)) { |
| 242 | return __builtin_strchr(s, c); |
Nick Kralevich | 049e583 | 2012-11-30 15:15:58 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | return __strchr_chk(s, c, bos); |
| 246 | } |
| 247 | |
Nick Kralevich | 9a4d305 | 2012-12-03 10:36:13 -0800 | [diff] [blame] | 248 | extern char* __strrchr_chk(const char *, int, size_t); |
| 249 | |
| 250 | __BIONIC_FORTIFY_INLINE |
| 251 | char* strrchr(const char *s, int c) { |
| 252 | size_t bos = __builtin_object_size(s, 0); |
| 253 | |
| 254 | // Compiler doesn't know destination size. Don't call __strrchr_chk |
| 255 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
Nick Kralevich | a44e9af | 2013-01-17 15:41:33 -0800 | [diff] [blame] | 256 | return __builtin_strrchr(s, c); |
| 257 | } |
| 258 | |
| 259 | size_t slen = __builtin_strlen(s); |
| 260 | if (__builtin_constant_p(slen) && (slen < bos)) { |
| 261 | return __builtin_strrchr(s, c); |
Nick Kralevich | 9a4d305 | 2012-12-03 10:36:13 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | return __strrchr_chk(s, c, bos); |
| 265 | } |
| 266 | |
Nick Kralevich | 049e583 | 2012-11-30 15:15:58 -0800 | [diff] [blame] | 267 | |
Elliott Hughes | 890c8ed | 2013-03-22 10:58:55 -0700 | [diff] [blame] | 268 | #endif /* defined(__BIONIC_FORTIFY) */ |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 269 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 270 | __END_DECLS |
| 271 | |
| 272 | #endif /* _STRING_H_ */ |