]> git.saurik.com Git - apple/xnu.git/blame - osfmk/libsa/string.h
xnu-7195.101.1.tar.gz
[apple/xnu.git] / osfmk / libsa / string.h
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55
A
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.
1c79356b
A
33 */
34/*
0a7de745 35 * HISTORY
1c79356b
A
36 * @OSF_COPYRIGHT@
37 */
0a7de745
A
38#ifndef _STRING_H_
39#define _STRING_H_ 1
1c79356b
A
40
41#ifdef MACH_KERNEL_PRIVATE
42#include <types.h>
43#else
44#include <sys/types.h>
45#endif
46
f427ee49
A
47#if defined(KERNEL)
48#include <sys/cdefs.h>
49#endif
50
1c79356b
A
51#ifdef __cplusplus
52extern "C" {
53#endif
54
0a7de745 55#ifndef NULL
2d21ac55 56#if defined (__cplusplus)
cb323159
A
57#if __cplusplus >= 201103L
58#define NULL nullptr
59#else
2d21ac55 60#define NULL 0
cb323159 61#endif
2d21ac55
A
62#else
63#define NULL ((void *)0)
64#endif
1c79356b
A
65#endif
66
0a7de745
A
67extern void *memcpy(void *, const void *, size_t);
68extern int memcmp(const void *, const void *, size_t);
69extern void *memmove(void *, const void *, size_t);
70extern void *memset(void *, int, size_t);
71extern int memset_s(void *, size_t, int, size_t);
1c79356b 72
f427ee49
A
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 */
78extern unsigned long memcmp_zero_ptr_aligned(const void *s, size_t n);
79#endif
80
0a7de745
A
81extern size_t strlen(const char *);
82extern size_t strnlen(const char *, size_t);
2d21ac55 83
f427ee49
A
84/* strcpy() and strncpy() are deprecated. Please use strlcpy() instead. */
85__kpi_deprecated_arm64_macos_unavailable
0a7de745 86extern char *strcpy(char *, const char *) __deprecated;
2d21ac55 87
f427ee49
A
88__kpi_deprecated_arm64_macos_unavailable
89extern char *strncpy(char *, const char *, size_t);
2d21ac55 90
f427ee49
A
91/* strcat() and strncat() are deprecated. Please use strlcat() instead. */
92__kpi_deprecated_arm64_macos_unavailable
0a7de745 93extern char *strcat(char *, const char *) __deprecated;
f427ee49
A
94
95__kpi_deprecated_arm64_macos_unavailable
0a7de745 96extern char *strncat(char *, const char *, size_t);
2d21ac55 97
0a7de745 98extern int strcmp(const char *, const char *);
a991bd8d 99extern int strncmp(const char *, const char *, size_t);
f427ee49
A
100
101extern size_t strlcpy(char *, const char *, size_t);
102extern size_t strlcat(char *, const char *, size_t);
2d21ac55 103
0a7de745
A
104extern int strcasecmp(const char *s1, const char *s2);
105extern int strncasecmp(const char *s1, const char *s2, size_t n);
f427ee49
A
106#ifdef XNU_KERNEL_PRIVATE
107extern const char *strnstr(const char *s, const char *find, size_t slen);
108#else
109extern char *strnstr(const char *s, const char *find, size_t slen);
110#endif
0a7de745 111extern char *strchr(const char *s, int c);
5ba3f43e 112#ifdef XNU_KERNEL_PRIVATE
0a7de745 113extern char *strrchr(const char *s, int c);
5ba3f43e 114#endif
0a7de745
A
115extern char *STRDUP(const char *, int);
116extern int strprefix(const char *s1, const char *s2);
91447636 117
0a7de745
A
118extern int bcmp(const void *, const void *, size_t);
119extern void bcopy(const void *, void *, size_t);
120extern void bzero(void *, size_t);
cb323159 121extern int timingsafe_bcmp(const void *b1, const void *b2, size_t n);
1c79356b 122
5ba3f43e
A
123#ifdef PRIVATE
124#include <san/memintrinsics.h>
125#endif
126
cb323159
A
127#if __has_builtin(__builtin_dynamic_object_size)
128#define XNU_BOS __builtin_dynamic_object_size
129#else
130#define XNU_BOS __builtin_object_size
131#endif
132
133
134/* __nochk_ functions for opting out of type 1 bounds checking */
135__attribute__((always_inline)) static inline void *
136__nochk_memcpy(void *dest, const void *src, size_t len)
137{
138 return __builtin___memcpy_chk(dest, src, len, XNU_BOS(dest, 0));
139}
140__attribute__((always_inline)) static inline void *
141__nochk_memmove(void *dest, const void *src, size_t len)
142{
143 return __builtin___memmove_chk(dest, src, len, XNU_BOS(dest, 0));
144}
145__attribute__((always_inline)) static inline void
146__nochk_bcopy(const void *src, void *dest, size_t len)
147{
148 __builtin___memmove_chk(dest, src, len, XNU_BOS(dest, 0));
149}
150
5ba3f43e
A
151#if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_13
152/* older deployment target */
153#elif defined(KASAN) || (defined (_FORTIFY_SOURCE) && _FORTIFY_SOURCE == 0)
cb323159 154/* _FORTIFY_SOURCE disabled */
5ba3f43e 155#else /* _chk macros */
cb323159 156
f427ee49
A
157#if defined XNU_KERNEL_PRIVATE || defined(_FORTIFY_SOURCE_STRICT)
158/* Stricter checking is optional for kexts. When type is set to 1, __builtin_object_size
cb323159
A
159 * returns the size of the closest surrounding sub-object, which would detect copying past
160 * the end of a struct member. */
161#define BOS_COPY_TYPE 1
162#else
163#define BOS_COPY_TYPE 0
164#endif
165
5ba3f43e 166#if __has_builtin(__builtin___memcpy_chk)
cb323159 167#define memcpy(dest, src, len) __builtin___memcpy_chk(dest, src, len, XNU_BOS(dest, BOS_COPY_TYPE))
5ba3f43e
A
168#endif
169
170#if __has_builtin(__builtin___memmove_chk)
cb323159 171#define memmove(dest, src, len) __builtin___memmove_chk(dest, src, len, XNU_BOS(dest, BOS_COPY_TYPE))
5ba3f43e
A
172#endif
173
174#if __has_builtin(__builtin___strncpy_chk)
cb323159 175#define strncpy(dest, src, len) __builtin___strncpy_chk(dest, src, len, XNU_BOS(dest, 1))
5ba3f43e
A
176#endif
177
178#if __has_builtin(__builtin___strncat_chk)
cb323159 179#define strncat(dest, src, len) __builtin___strncat_chk(dest, src, len, XNU_BOS(dest, 1))
5ba3f43e
A
180#endif
181
182#if __has_builtin(__builtin___strlcat_chk)
cb323159 183#define strlcat(dest, src, len) __builtin___strlcat_chk(dest, src, len, XNU_BOS(dest, 1))
5ba3f43e
A
184#endif
185
186#if __has_builtin(__builtin___strlcpy_chk)
cb323159 187#define strlcpy(dest, src, len) __builtin___strlcpy_chk(dest, src, len, XNU_BOS(dest, 1))
5ba3f43e
A
188#endif
189
190#if __has_builtin(__builtin___strcpy_chk)
cb323159 191#define strcpy(dest, src, len) __builtin___strcpy_chk(dest, src, XNU_BOS(dest, 1))
5ba3f43e
A
192#endif
193
194#if __has_builtin(__builtin___strcat_chk)
cb323159 195#define strcat(dest, src) __builtin___strcat_chk(dest, src, XNU_BOS(dest, 1))
5ba3f43e
A
196#endif
197
198#if __has_builtin(__builtin___memmove_chk)
cb323159 199#define bcopy(src, dest, len) __builtin___memmove_chk(dest, src, len, XNU_BOS(dest, BOS_COPY_TYPE))
5ba3f43e
A
200#endif
201
202#endif /* _chk macros */
1c79356b
A
203#ifdef __cplusplus
204}
205#endif
206
0a7de745 207#endif /* _STRING_H_ */