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