]> git.saurik.com Git - apple/xnu.git/blob - osfmk/libsa/string.h
a5a10300aec1573556f40ee2b6f0334cced7362b
[apple/xnu.git] / osfmk / libsa / string.h
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
30 * support for mandatory and extensible security protections. This notice
31 * is included in support of clause 2.2 (b) of the Apple Public License,
32 * Version 2.0.
33 */
34 /*
35 * HISTORY
36 * @OSF_COPYRIGHT@
37 */
38 #ifndef _STRING_H_
39 #define _STRING_H_ 1
40
41 #ifdef MACH_KERNEL_PRIVATE
42 #include <types.h>
43 #else
44 #include <sys/types.h>
45 #endif
46
47 #if defined(KERNEL)
48 #include <sys/cdefs.h>
49 #endif
50
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54
55 #ifndef NULL
56 #if defined (__cplusplus)
57 #if __cplusplus >= 201103L
58 #define NULL nullptr
59 #else
60 #define NULL 0
61 #endif
62 #else
63 #define NULL ((void *)0)
64 #endif
65 #endif
66
67 extern void *memcpy(void *, const void *, size_t);
68 extern int memcmp(const void *, const void *, size_t);
69 extern void *memmove(void *, const void *, size_t);
70 extern void *memset(void *, int, size_t);
71 extern int memset_s(void *, size_t, int, size_t);
72
73 #ifdef XNU_KERNEL_PRIVATE
74 /* memcmp_zero_ptr_aligned() checks string s of n bytes contains all zeros.
75 * Address and size of the string s must be pointer-aligned.
76 * Return 0 if true, 1 otherwise. Also return 0 if n is 0.
77 */
78 extern unsigned long memcmp_zero_ptr_aligned(const void *s, size_t n);
79 #endif
80
81 extern size_t strlen(const char *);
82 extern size_t strnlen(const char *, size_t);
83
84 /* strcpy() and strncpy() are deprecated. Please use strlcpy() instead. */
85 __kpi_deprecated_arm64_macos_unavailable
86 extern char *strcpy(char *, const char *) __deprecated;
87
88 __kpi_deprecated_arm64_macos_unavailable
89 extern char *strncpy(char *, const char *, size_t);
90
91 /* strcat() and strncat() are deprecated. Please use strlcat() instead. */
92 __kpi_deprecated_arm64_macos_unavailable
93 extern char *strcat(char *, const char *) __deprecated;
94
95 __kpi_deprecated_arm64_macos_unavailable
96 extern char *strncat(char *, const char *, size_t);
97
98 /* strcmp() is deprecated. Please use strncmp() instead. */
99 __kpi_deprecated_arm64_macos_unavailable
100 extern int strcmp(const char *, const char *);
101
102 extern size_t strlcpy(char *, const char *, size_t);
103 extern size_t strlcat(char *, const char *, size_t);
104 extern int strncmp(const char *, const char *, size_t);
105
106 extern int strcasecmp(const char *s1, const char *s2);
107 extern int strncasecmp(const char *s1, const char *s2, size_t n);
108 #ifdef XNU_KERNEL_PRIVATE
109 extern const char *strnstr(const char *s, const char *find, size_t slen);
110 #else
111 extern char *strnstr(const char *s, const char *find, size_t slen);
112 #endif
113 extern char *strchr(const char *s, int c);
114 #ifdef XNU_KERNEL_PRIVATE
115 extern char *strrchr(const char *s, int c);
116 #endif
117 extern char *STRDUP(const char *, int);
118 extern int strprefix(const char *s1, const char *s2);
119
120 extern int bcmp(const void *, const void *, size_t);
121 extern void bcopy(const void *, void *, size_t);
122 extern void bzero(void *, size_t);
123 extern int timingsafe_bcmp(const void *b1, const void *b2, size_t n);
124
125 #ifdef PRIVATE
126 #include <san/memintrinsics.h>
127 #endif
128
129 #if __has_builtin(__builtin_dynamic_object_size)
130 #define XNU_BOS __builtin_dynamic_object_size
131 #else
132 #define XNU_BOS __builtin_object_size
133 #endif
134
135
136 /* __nochk_ functions for opting out of type 1 bounds checking */
137 __attribute__((always_inline)) static inline void *
138 __nochk_memcpy(void *dest, const void *src, size_t len)
139 {
140 return __builtin___memcpy_chk(dest, src, len, XNU_BOS(dest, 0));
141 }
142 __attribute__((always_inline)) static inline void *
143 __nochk_memmove(void *dest, const void *src, size_t len)
144 {
145 return __builtin___memmove_chk(dest, src, len, XNU_BOS(dest, 0));
146 }
147 __attribute__((always_inline)) static inline void
148 __nochk_bcopy(const void *src, void *dest, size_t len)
149 {
150 __builtin___memmove_chk(dest, src, len, XNU_BOS(dest, 0));
151 }
152
153 #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_13
154 /* older deployment target */
155 #elif defined(KASAN) || (defined (_FORTIFY_SOURCE) && _FORTIFY_SOURCE == 0)
156 /* _FORTIFY_SOURCE disabled */
157 #else /* _chk macros */
158
159 #if defined XNU_KERNEL_PRIVATE || defined(_FORTIFY_SOURCE_STRICT)
160 /* Stricter checking is optional for kexts. When type is set to 1, __builtin_object_size
161 * returns the size of the closest surrounding sub-object, which would detect copying past
162 * the end of a struct member. */
163 #define BOS_COPY_TYPE 1
164 #else
165 #define BOS_COPY_TYPE 0
166 #endif
167
168 #if __has_builtin(__builtin___memcpy_chk)
169 #define memcpy(dest, src, len) __builtin___memcpy_chk(dest, src, len, XNU_BOS(dest, BOS_COPY_TYPE))
170 #endif
171
172 #if __has_builtin(__builtin___memmove_chk)
173 #define memmove(dest, src, len) __builtin___memmove_chk(dest, src, len, XNU_BOS(dest, BOS_COPY_TYPE))
174 #endif
175
176 #if __has_builtin(__builtin___strncpy_chk)
177 #define strncpy(dest, src, len) __builtin___strncpy_chk(dest, src, len, XNU_BOS(dest, 1))
178 #endif
179
180 #if __has_builtin(__builtin___strncat_chk)
181 #define strncat(dest, src, len) __builtin___strncat_chk(dest, src, len, XNU_BOS(dest, 1))
182 #endif
183
184 #if __has_builtin(__builtin___strlcat_chk)
185 #define strlcat(dest, src, len) __builtin___strlcat_chk(dest, src, len, XNU_BOS(dest, 1))
186 #endif
187
188 #if __has_builtin(__builtin___strlcpy_chk)
189 #define strlcpy(dest, src, len) __builtin___strlcpy_chk(dest, src, len, XNU_BOS(dest, 1))
190 #endif
191
192 #if __has_builtin(__builtin___strcpy_chk)
193 #define strcpy(dest, src, len) __builtin___strcpy_chk(dest, src, XNU_BOS(dest, 1))
194 #endif
195
196 #if __has_builtin(__builtin___strcat_chk)
197 #define strcat(dest, src) __builtin___strcat_chk(dest, src, XNU_BOS(dest, 1))
198 #endif
199
200 #if __has_builtin(__builtin___memmove_chk)
201 #define bcopy(src, dest, len) __builtin___memmove_chk(dest, src, len, XNU_BOS(dest, BOS_COPY_TYPE))
202 #endif
203
204 #endif /* _chk macros */
205 #ifdef __cplusplus
206 }
207 #endif
208
209 #endif /* _STRING_H_ */