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 | */ |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 28 | |
| 29 | #ifndef _STRING_H |
| 30 | #define _STRING_H |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 31 | |
| 32 | #include <sys/cdefs.h> |
| 33 | #include <stddef.h> |
Dan Albert | dfb5ce4 | 2014-07-09 22:51:34 +0000 | [diff] [blame] | 34 | #include <xlocale.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | |
Josh Gao | c3cec27 | 2016-04-07 13:39:49 -0700 | [diff] [blame] | 36 | #include <bits/strcasecmp.h> |
| 37 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 38 | __BEGIN_DECLS |
| 39 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 40 | #if defined(__clang__) |
| 41 | #pragma clang diagnostic push |
| 42 | #pragma clang diagnostic ignored "-Wnullability-completeness" |
| 43 | #endif |
| 44 | |
Elliott Hughes | 76f8916 | 2015-01-26 13:34:58 -0800 | [diff] [blame] | 45 | #if defined(__USE_BSD) |
| 46 | #include <strings.h> |
| 47 | #endif |
| 48 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 49 | void* memccpy(void* _Nonnull __restrict, const void* _Nonnull __restrict, int, size_t); |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 50 | void* memchr(const void* _Nonnull, int, size_t) __attribute_pure__ __overloadable |
| 51 | __RENAME_CLANG(memchr); |
| 52 | void* memrchr(const void* _Nonnull, int, size_t) __attribute_pure__ __overloadable |
| 53 | __RENAME_CLANG(memrchr); |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 54 | int memcmp(const void* _Nonnull, const void* _Nonnull, size_t) __attribute_pure__; |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 55 | void* memcpy(void* _Nonnull __restrict, const void* _Nonnull __restrict, size_t) |
| 56 | __overloadable __RENAME_CLANG(memcpy); |
Elliott Hughes | 3cfb52a | 2015-02-18 21:29:13 -0800 | [diff] [blame] | 57 | #if defined(__USE_GNU) |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 58 | void* mempcpy(void* _Nonnull __restrict, const void* _Nonnull __restrict, size_t) __INTRODUCED_IN(23); |
Elliott Hughes | 3cfb52a | 2015-02-18 21:29:13 -0800 | [diff] [blame] | 59 | #endif |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 60 | void* memmove(void* _Nonnull, const void* _Nonnull, size_t) __overloadable |
| 61 | __RENAME_CLANG(memmove); |
| 62 | void* memset(void* _Nonnull, int, size_t) __overloadable __RENAME_CLANG(memset); |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 63 | void* memmem(const void* _Nonnull, size_t, const void* _Nonnull, size_t) __attribute_pure__; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 64 | |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 65 | char* strchr(const char* _Nonnull, int) __attribute_pure__ __overloadable |
| 66 | __RENAME_CLANG(strchr); |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 67 | char* __strchr_chk(const char* _Nonnull, int, size_t) __INTRODUCED_IN(18); |
Elliott Hughes | 7ac3c12 | 2015-08-26 09:59:29 -0700 | [diff] [blame] | 68 | #if defined(__USE_GNU) |
| 69 | #if defined(__cplusplus) |
Josh Gao | 16d9ba8 | 2017-06-30 13:20:28 -0700 | [diff] [blame] | 70 | extern "C++" char* strchrnul(char* _Nonnull, int) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24); |
| 71 | extern "C++" const char* strchrnul(const char* _Nonnull, int) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24); |
Elliott Hughes | 7ac3c12 | 2015-08-26 09:59:29 -0700 | [diff] [blame] | 72 | #else |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 73 | char* strchrnul(const char* _Nonnull, int) __attribute_pure__ __INTRODUCED_IN(24); |
Elliott Hughes | 7ac3c12 | 2015-08-26 09:59:29 -0700 | [diff] [blame] | 74 | #endif |
| 75 | #endif |
Pavel Chupin | 3c4b50f | 2013-07-26 16:50:11 +0400 | [diff] [blame] | 76 | |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 77 | char* strrchr(const char* _Nonnull, int) __attribute_pure__ __overloadable |
| 78 | __RENAME_CLANG(strrchr); |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 79 | char* __strrchr_chk(const char* _Nonnull, int, size_t) __INTRODUCED_IN(18); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 80 | |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 81 | size_t strlen(const char* _Nonnull) __attribute_pure__ __overloadable |
| 82 | __RENAME_CLANG(strlen); |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 83 | size_t __strlen_chk(const char* _Nonnull, size_t) __INTRODUCED_IN(17); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 84 | |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 85 | int strcmp(const char* _Nonnull, const char* _Nonnull) __attribute_pure__; |
| 86 | char* stpcpy(char* _Nonnull __restrict, const char* _Nonnull __restrict) |
| 87 | __overloadable __RENAME_CLANG(stpcpy) __INTRODUCED_IN(21); |
| 88 | char* strcpy(char* _Nonnull __restrict, const char* _Nonnull __restrict) |
| 89 | __overloadable __RENAME_CLANG(strcpy); |
| 90 | char* strcat(char* _Nonnull __restrict, const char* _Nonnull __restrict) |
| 91 | __overloadable __RENAME_CLANG(strcat); |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 92 | char* strdup(const char* _Nonnull); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 93 | |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 94 | char* strstr(const char* _Nonnull, const char* _Nonnull) __attribute_pure__; |
| 95 | char* strcasestr(const char* _Nonnull, const char* _Nonnull) __attribute_pure__; |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 96 | char* strtok(char* __restrict, const char* _Nonnull __restrict); |
| 97 | char* strtok_r(char* __restrict, const char* _Nonnull __restrict, char** _Nonnull __restrict); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 98 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 99 | char* strerror(int); |
| 100 | char* strerror_l(int, locale_t) __INTRODUCED_IN(23); |
Dan Albert | 8b154b1 | 2017-02-14 16:33:06 -0800 | [diff] [blame] | 101 | #if defined(__USE_GNU) && __ANDROID_API__ >= 23 |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 102 | char* strerror_r(int, char*, size_t) __RENAME(__gnu_strerror_r) __INTRODUCED_IN(23); |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 103 | #else /* POSIX */ |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 104 | int strerror_r(int, char*, size_t); |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 105 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 106 | |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 107 | size_t strnlen(const char* _Nonnull, size_t) __attribute_pure__; |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 108 | char* strncat(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t) |
| 109 | __overloadable __RENAME_CLANG(strncat); |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 110 | char* strndup(const char* _Nonnull, size_t); |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 111 | int strncmp(const char* _Nonnull, const char* _Nonnull, size_t) __attribute_pure__; |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 112 | char* stpncpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t) |
| 113 | __overloadable __RENAME_CLANG(stpncpy) __INTRODUCED_IN(21); |
| 114 | char* strncpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t) |
| 115 | __overloadable __RENAME_CLANG(strncpy); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 116 | |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 117 | size_t strlcat(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t) |
| 118 | __overloadable __RENAME_CLANG(strlcat); |
| 119 | size_t strlcpy(char* _Nonnull __restrict, const char* _Nonnull __restrict, size_t) |
| 120 | __overloadable __RENAME_CLANG(strlcpy); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 121 | |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 122 | size_t strcspn(const char* _Nonnull, const char* _Nonnull) __attribute_pure__; |
| 123 | char* strpbrk(const char* _Nonnull, const char* _Nonnull) __attribute_pure__; |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 124 | char* strsep(char** _Nonnull __restrict, const char* _Nonnull __restrict); |
| 125 | size_t strspn(const char* _Nonnull, const char* _Nonnull); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 126 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 127 | char* strsignal(int); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 128 | |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 129 | int strcoll(const char* _Nonnull, const char* _Nonnull) __attribute_pure__; |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 130 | size_t strxfrm(char* __restrict, const char* _Nonnull __restrict, size_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 131 | |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 132 | #if __ANDROID_API__ >= __ANDROID_API_L__ |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 133 | int strcoll_l(const char* _Nonnull, const char* _Nonnull, locale_t) __attribute_pure__ __INTRODUCED_IN(21); |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 134 | size_t strxfrm_l(char* __restrict, const char* _Nonnull __restrict, size_t, locale_t) __INTRODUCED_IN(21); |
Josh Gao | 6cd9fb0 | 2016-09-23 14:06:05 -0700 | [diff] [blame] | 135 | #else |
| 136 | // Implemented as static inlines before 21. |
| 137 | #endif |
Dan Albert | dfb5ce4 | 2014-07-09 22:51:34 +0000 | [diff] [blame] | 138 | |
Josh Gao | eb9b925 | 2015-11-03 18:46:02 -0800 | [diff] [blame] | 139 | #if defined(__USE_GNU) && !defined(basename) |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 140 | /* |
| 141 | * glibc has a basename in <string.h> that's different to the POSIX one in <libgen.h>. |
| 142 | * It doesn't modify its argument, and in C++ it's const-correct. |
| 143 | */ |
| 144 | #if defined(__cplusplus) |
Josh Gao | 16d9ba8 | 2017-06-30 13:20:28 -0700 | [diff] [blame] | 145 | extern "C++" char* basename(char* _Nonnull) __RENAME(__gnu_basename) __INTRODUCED_IN(23); |
| 146 | extern "C++" const char* basename(const char* _Nonnull) __RENAME(__gnu_basename) __INTRODUCED_IN(23); |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 147 | #else |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 148 | char* basename(const char* _Nonnull) __RENAME(__gnu_basename) __INTRODUCED_IN(23); |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 149 | #endif |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 150 | #endif |
| 151 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame^] | 152 | #if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS) |
| 153 | #include <bits/fortify/string.h> |
| 154 | #endif |
Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 155 | |
George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 156 | /* Const-correct overloads. Placed after FORTIFY so we call those functions, if possible. */ |
| 157 | #if defined(__cplusplus) && defined(__clang__) |
| 158 | /* |
| 159 | * Use two enable_ifs so these overloads don't conflict with + are preferred over libcxx's. This can |
| 160 | * be reduced to 1 after libcxx recognizes that we have const-correct overloads. |
| 161 | */ |
| 162 | #define __prefer_this_overload __enable_if(true, "preferred overload") __enable_if(true, "") |
| 163 | extern "C++" { |
| 164 | inline __always_inline |
| 165 | void* __bionic_memchr(const void* const _Nonnull s __pass_object_size, int c, size_t n) { |
| 166 | return memchr(s, c, n); |
| 167 | } |
| 168 | |
| 169 | inline __always_inline |
| 170 | const void* memchr(const void* const _Nonnull s __pass_object_size, int c, size_t n) |
| 171 | __prefer_this_overload { |
| 172 | return __bionic_memchr(s, c, n); |
| 173 | } |
| 174 | |
| 175 | inline __always_inline |
| 176 | void* memchr(void* const _Nonnull s __pass_object_size, int c, size_t n) __prefer_this_overload { |
| 177 | return __bionic_memchr(s, c, n); |
| 178 | } |
| 179 | |
| 180 | inline __always_inline |
| 181 | char* __bionic_strchr(const char* const _Nonnull s __pass_object_size, int c) { |
| 182 | return strchr(s, c); |
| 183 | } |
| 184 | |
| 185 | inline __always_inline |
| 186 | const char* strchr(const char* const _Nonnull s __pass_object_size, int c) |
| 187 | __prefer_this_overload { |
| 188 | return __bionic_strchr(s, c); |
| 189 | } |
| 190 | |
| 191 | inline __always_inline |
| 192 | char* strchr(char* const _Nonnull s __pass_object_size, int c) |
| 193 | __prefer_this_overload { |
| 194 | return __bionic_strchr(s, c); |
| 195 | } |
| 196 | |
| 197 | inline __always_inline |
| 198 | char* __bionic_strrchr(const char* const _Nonnull s __pass_object_size, int c) { |
| 199 | return strrchr(s, c); |
| 200 | } |
| 201 | |
| 202 | inline __always_inline |
| 203 | const char* strrchr(const char* const _Nonnull s __pass_object_size, int c) __prefer_this_overload { |
| 204 | return __bionic_strrchr(s, c); |
| 205 | } |
| 206 | |
| 207 | inline __always_inline |
| 208 | char* strrchr(char* const _Nonnull s __pass_object_size, int c) __prefer_this_overload { |
| 209 | return __bionic_strrchr(s, c); |
| 210 | } |
| 211 | |
| 212 | /* Functions with no FORTIFY counterpart. */ |
| 213 | inline __always_inline |
| 214 | char* __bionic_strstr(const char* _Nonnull h, const char* _Nonnull n) { return strstr(h, n); } |
| 215 | |
| 216 | inline __always_inline |
| 217 | const char* strstr(const char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload { |
| 218 | return __bionic_strstr(h, n); |
| 219 | } |
| 220 | |
| 221 | inline __always_inline |
| 222 | char* strstr(char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload { |
| 223 | return __bionic_strstr(h, n); |
| 224 | } |
| 225 | |
| 226 | inline __always_inline |
| 227 | char* __bionic_strpbrk(const char* _Nonnull h, const char* _Nonnull n) { return strpbrk(h, n); } |
| 228 | |
| 229 | inline __always_inline |
| 230 | char* strpbrk(char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload { |
| 231 | return __bionic_strpbrk(h, n); |
| 232 | } |
| 233 | |
| 234 | inline __always_inline |
| 235 | const char* strpbrk(const char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload { |
| 236 | return __bionic_strpbrk(h, n); |
| 237 | } |
| 238 | } |
| 239 | #undef __prefer_this_overload |
| 240 | #endif |
| 241 | |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 242 | #if defined(__clang__) |
| 243 | #pragma clang diagnostic pop |
| 244 | #endif |
| 245 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 246 | __END_DECLS |
| 247 | |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 248 | #endif /* _STRING_H */ |