• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 #ifndef _STRING_H
30 #define _STRING_H
31 
32 #include <sys/cdefs.h>
33 #include <stddef.h>
34 #include <xlocale.h>
35 
36 #include <bits/strcasecmp.h>
37 
38 __BEGIN_DECLS
39 
40 #if defined(__USE_BSD)
41 #include <strings.h>
42 #endif
43 
44 void* _Nullable memccpy(void* _Nonnull __dst, const void* _Nonnull __src, int __stop_char, size_t __n);
45 void* _Nullable memchr(const void* _Nonnull __s, int __ch, size_t __n) __attribute_pure__;
46 #if defined(__cplusplus)
47 extern "C++" void* _Nullable memrchr(void* _Nonnull __s, int __ch, size_t __n) __RENAME(memrchr) __attribute_pure__;
48 extern "C++" const void* _Nullable memrchr(const void* _Nonnull __s, int __ch, size_t __n) __RENAME(memrchr) __attribute_pure__;
49 #else
50 void* _Nullable memrchr(const void* _Nonnull __s, int __ch, size_t __n) __attribute_pure__;
51 #endif
52 int memcmp(const void* _Nonnull __lhs, const void* _Nonnull __rhs, size_t __n) __attribute_pure__;
53 void* _Nonnull memcpy(void* _Nonnull, const void* _Nonnull, size_t);
54 #if defined(__USE_GNU)
55 void* _Nonnull mempcpy(void* _Nonnull __dst, const void* _Nonnull __src, size_t __n) __INTRODUCED_IN(23);
56 #endif
57 void* _Nonnull memmove(void* _Nonnull __dst, const void* _Nonnull __src, size_t __n);
58 
59 /**
60  * [memset(3)](http://man7.org/linux/man-pages/man3/memset.3.html) writes the
61  * bottom 8 bits of the given int to the next `n` bytes of `dst`.
62  *
63  * Returns `dst`.
64  */
65 void* _Nonnull memset(void* _Nonnull __dst, int __ch, size_t __n);
66 
67 /**
68  * [memset_explicit(3)](http://man7.org/linux/man-pages/man3/memset_explicit.3.html)
69  * writes the bottom 8 bits of the given int to the next `n` bytes of `dst`,
70  * but won't be optimized out by the compiler.
71  *
72  * Returns `dst`.
73  */
74 void* _Nonnull memset_explicit(void* _Nonnull __dst, int __ch, size_t __n) __INTRODUCED_IN(34);
75 
76 void* _Nullable memmem(const void* _Nonnull __haystack, size_t __haystack_size, const void* _Nonnull __needle, size_t __needle_size) __attribute_pure__;
77 
78 char* _Nullable strchr(const char* _Nonnull __s, int __ch) __attribute_pure__;
79 char* _Nullable __strchr_chk(const char* _Nonnull __s, int __ch, size_t __n) __INTRODUCED_IN(18);
80 #if defined(__USE_GNU)
81 #if defined(__cplusplus)
82 extern "C++" char* _Nonnull strchrnul(char* _Nonnull __s, int __ch) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24);
83 extern "C++" const char* _Nonnull strchrnul(const char* _Nonnull __s, int __ch) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24);
84 #else
85 char* _Nonnull strchrnul(const char* _Nonnull __s, int __ch) __attribute_pure__ __INTRODUCED_IN(24);
86 #endif
87 #endif
88 
89 char* _Nullable strrchr(const char* _Nonnull __s, int __ch) __attribute_pure__;
90 char* _Nullable __strrchr_chk(const char* _Nonnull __s, int __ch, size_t __n) __INTRODUCED_IN(18);
91 
92 size_t strlen(const char* _Nonnull __s) __attribute_pure__;
93 size_t __strlen_chk(const char* _Nonnull __s, size_t __n) __INTRODUCED_IN(17);
94 
95 int strcmp(const char* _Nonnull __lhs, const char* _Nonnull __rhs) __attribute_pure__;
96 char* _Nonnull stpcpy(char* _Nonnull __dst, const char* _Nonnull __src) __INTRODUCED_IN(21);
97 char* _Nonnull strcpy(char* _Nonnull __dst, const char* _Nonnull __src);
98 char* _Nonnull strcat(char* _Nonnull __dst, const char* _Nonnull __src);
99 char* _Nullable strdup(const char* _Nonnull __s);
100 
101 char* _Nullable strstr(const char* _Nonnull __haystack, const char* _Nonnull __needle) __attribute_pure__;
102 #if defined(__cplusplus)
103 extern "C++" char* _Nullable strcasestr(char* _Nonnull, const char* _Nonnull) __RENAME(strcasestr) __attribute_pure__;
104 extern "C++" const char* _Nullable strcasestr(const char* _Nonnull, const char* _Nonnull) __RENAME(strcasestr) __attribute_pure__;
105 #else
106 char* _Nullable strcasestr(const char* _Nonnull __haystack, const char* _Nonnull __needle) __attribute_pure__;
107 #endif
108 char* _Nullable strtok(char* _Nullable __s, const char* _Nonnull __delimiter);
109 char* _Nullable strtok_r(char* _Nullable __s, const char* _Nonnull __delimiter, char* _Nonnull * _Nonnull __pos_ptr);
110 
111 char* _Nonnull strerror(int __errno_value);
112 char* _Nonnull strerror_l(int __errno_value, locale_t _Nonnull __l) __INTRODUCED_IN(23);
113 #if defined(__USE_GNU) && __ANDROID_API__ >= 23
114 char* _Nonnull strerror_r(int __errno_value, char* _Nullable __buf, size_t __n) __RENAME(__gnu_strerror_r) __INTRODUCED_IN(23);
115 #else /* POSIX */
116 int strerror_r(int __errno_value, char* _Nonnull __buf, size_t __n);
117 #endif
118 
119 size_t strnlen(const char* _Nonnull __s, size_t __n) __attribute_pure__;
120 char* _Nonnull strncat(char* _Nonnull __dst, const char* _Nonnull __src, size_t __n);
121 char* _Nullable strndup(const char* _Nonnull __s, size_t __n);
122 int strncmp(const char* _Nonnull __lhs, const char* _Nonnull __rhs, size_t __n) __attribute_pure__;
123 char* _Nonnull stpncpy(char* _Nonnull __dst, const char* _Nonnull __src, size_t __n) __INTRODUCED_IN(21);
124 char* _Nonnull strncpy(char* _Nonnull __dst, const char* _Nonnull __src, size_t __n);
125 
126 size_t strlcat(char* _Nonnull __dst, const char* _Nonnull __src, size_t __n);
127 size_t strlcpy(char* _Nonnull __dst, const char* _Nonnull __src, size_t __n);
128 
129 size_t strcspn(const char* _Nonnull __s, const char* _Nonnull __reject) __attribute_pure__;
130 char* _Nullable strpbrk(const char* _Nonnull __s, const char* _Nonnull __accept) __attribute_pure__;
131 char* _Nullable strsep(char* _Nullable * _Nonnull __s_ptr, const char* _Nonnull __delimiter);
132 size_t strspn(const char* _Nonnull __s, const char* _Nonnull __accept);
133 
134 char* _Nonnull strsignal(int __signal);
135 
136 int strcoll(const char* _Nonnull __lhs, const char* _Nonnull __rhs) __attribute_pure__;
137 size_t strxfrm(char* __BIONIC_COMPLICATED_NULLNESS __dst, const char* _Nonnull __src, size_t __n);
138 
139 int strcoll_l(const char* _Nonnull __lhs, const char* _Nonnull __rhs, locale_t _Nonnull __l) __attribute_pure__ __INTRODUCED_IN(21);
140 size_t strxfrm_l(char* __BIONIC_COMPLICATED_NULLNESS __dst, const char* _Nonnull __src, size_t __n, locale_t _Nonnull __l) __INTRODUCED_IN(21);
141 
142 #if defined(__USE_GNU) && !defined(basename)
143 /*
144  * glibc has a basename in <string.h> that's different to the POSIX one in <libgen.h>.
145  * It doesn't modify its argument, and in C++ it's const-correct.
146  */
147 #if defined(__cplusplus)
148 extern "C++" char* _Nonnull basename(char* _Nullable __path) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
149 extern "C++" const char* _Nonnull basename(const char* _Nonnull __path) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
150 #else
151 char* _Nonnull basename(const char* _Nonnull __path) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
152 #endif
153 #endif
154 
155 #if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
156 #include <bits/fortify/string.h>
157 #endif
158 
159 /* Const-correct overloads. Placed after FORTIFY so we call those functions, if possible. */
160 #if defined(__cplusplus)
161 /*
162  * Use two enable_ifs so these overloads don't conflict with + are preferred over libcxx's. This can
163  * be reduced to 1 after libcxx recognizes that we have const-correct overloads.
164  */
165 #define __prefer_this_overload __enable_if(true, "preferred overload") __enable_if(true, "")
166 extern "C++" {
167 inline __always_inline
__bionic_memchr(const void * _Nonnull const s __pass_object_size,int c,size_t n)168 void* _Nullable __bionic_memchr(const void* _Nonnull const s __pass_object_size, int c, size_t n) {
169     return memchr(s, c, n);
170 }
171 
172 inline __always_inline
memchr(const void * _Nonnull const s __pass_object_size,int c,size_t n)173 const void* _Nullable memchr(const void* _Nonnull const s __pass_object_size, int c, size_t n)
174         __prefer_this_overload {
175     return __bionic_memchr(s, c, n);
176 }
177 
178 inline __always_inline
memchr(void * _Nonnull const s __pass_object_size,int c,size_t n)179 void* _Nullable memchr(void* _Nonnull const s __pass_object_size, int c, size_t n) __prefer_this_overload {
180     return __bionic_memchr(s, c, n);
181 }
182 
183 inline __always_inline
__bionic_strchr(const char * _Nonnull const s __pass_object_size,int c)184 char* _Nullable __bionic_strchr(const char* _Nonnull const s __pass_object_size, int c) {
185     return strchr(s, c);
186 }
187 
188 inline __always_inline
strchr(const char * _Nonnull const s __pass_object_size,int c)189 const char* _Nullable strchr(const char* _Nonnull const s __pass_object_size, int c)
190         __prefer_this_overload {
191     return __bionic_strchr(s, c);
192 }
193 
194 inline __always_inline
strchr(char * _Nonnull const s __pass_object_size,int c)195 char* _Nullable strchr(char* _Nonnull const s __pass_object_size, int c)
196         __prefer_this_overload {
197     return __bionic_strchr(s, c);
198 }
199 
200 inline __always_inline
__bionic_strrchr(const char * _Nonnull const s __pass_object_size,int c)201 char* _Nullable __bionic_strrchr(const char* _Nonnull const s __pass_object_size, int c) {
202     return strrchr(s, c);
203 }
204 
205 inline __always_inline
strrchr(const char * _Nonnull const s __pass_object_size,int c)206 const char* _Nullable strrchr(const char* _Nonnull const s __pass_object_size, int c) __prefer_this_overload {
207     return __bionic_strrchr(s, c);
208 }
209 
210 inline __always_inline
strrchr(char * _Nonnull const s __pass_object_size,int c)211 char* _Nullable strrchr(char* _Nonnull const s __pass_object_size, int c) __prefer_this_overload {
212     return __bionic_strrchr(s, c);
213 }
214 
215 /* Functions with no FORTIFY counterpart. */
216 inline __always_inline
__bionic_strstr(const char * _Nonnull h,const char * _Nonnull n)217 char* _Nullable __bionic_strstr(const char* _Nonnull h, const char* _Nonnull n) { return strstr(h, n); }
218 
219 inline __always_inline
strstr(const char * _Nonnull h,const char * _Nonnull n)220 const char* _Nullable strstr(const char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload {
221     return __bionic_strstr(h, n);
222 }
223 
224 inline __always_inline
strstr(char * _Nonnull h,const char * _Nonnull n)225 char* _Nullable strstr(char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload {
226     return __bionic_strstr(h, n);
227 }
228 
229 inline __always_inline
__bionic_strpbrk(const char * _Nonnull h,const char * _Nonnull n)230 char* _Nullable __bionic_strpbrk(const char* _Nonnull h, const char* _Nonnull n) { return strpbrk(h, n); }
231 
232 inline __always_inline
strpbrk(char * _Nonnull h,const char * _Nonnull n)233 char* _Nullable strpbrk(char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload {
234     return __bionic_strpbrk(h, n);
235 }
236 
237 inline __always_inline
strpbrk(const char * _Nonnull h,const char * _Nonnull n)238 const char* _Nullable strpbrk(const char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload {
239     return __bionic_strpbrk(h, n);
240 }
241 }
242 #undef __prefer_this_overload
243 #endif
244 
245 __END_DECLS
246 
247 #endif
248