1/*
2 * Copyright (c) 2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#ifndef _STRING_H_
25# error "Never use <secure/_string.h> directly; include <string.h> instead."
26#endif
27
28#ifndef _SECURE__STRING_H_
29#define _SECURE__STRING_H_
30
31#include <Availability.h>
32#include <sys/cdefs.h>
33#include <secure/_common.h>
34
35#if _USE_FORTIFY_LEVEL > 0
36
37#ifndef __has_builtin
38#define _undef__has_builtin
39#define __has_builtin(x) 0
40#endif
41
42/* <rdar://problem/12622659> */
43#if defined(__clang__) && \
44    ((defined(__apple_build_version__) && __apple_build_version__ >= 4260006) || \
45     (!defined(__apple_build_version__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 3))))
46#define __HAS_FIXED_CHK_PROTOTYPES 1
47#else
48#define __HAS_FIXED_CHK_PROTOTYPES 0
49#endif
50
51/* memccpy, memcpy, mempcpy, memmove, memset, strcpy, strlcpy, stpcpy,
52   strncpy, stpncpy, strcat, strlcat, and strncat */
53
54#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000 || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
55#if __has_builtin(__builtin___memccpy_chk) && __HAS_FIXED_CHK_PROTOTYPES
56#undef memccpy
57#define memccpy(dest, src, c, len)                                  \
58  __builtin___memccpy_chk (dest, src, c, len, __darwin_obsz0 (dest))
59#endif
60#endif
61
62#if __has_builtin(__builtin___memcpy_chk) || defined(__GNUC__)
63#undef memcpy
64#define memcpy(dest, src, len)					\
65  __builtin___memcpy_chk (dest, src, len, __darwin_obsz0 (dest))
66#endif
67
68#if __has_builtin(__builtin___memmove_chk) || defined(__GNUC__)
69#undef memmove
70#define memmove(dest, src, len)					\
71  __builtin___memmove_chk (dest, src, len, __darwin_obsz0 (dest))
72#endif
73
74#if __has_builtin(__builtin___memset_chk) || defined(__GNUC__)
75#undef memset
76#define memset(dest, val, len)					\
77  __builtin___memset_chk (dest, val, len, __darwin_obsz0 (dest))
78#endif
79
80#if __has_builtin(__builtin___strcpy_chk) || defined(__GNUC__)
81#undef strcpy
82#define strcpy(dest, src)					\
83  __builtin___strcpy_chk (dest, src, __darwin_obsz (dest))
84#endif
85
86#if __DARWIN_C_LEVEL >= 200809L
87#if __has_builtin(__builtin___stpcpy_chk) || defined(__GNUC__)
88#undef stpcpy
89#define stpcpy(dest, src)					\
90  __builtin___stpcpy_chk (dest, src, __darwin_obsz (dest))
91#endif
92
93#if __has_builtin(__builtin___stpncpy_chk) || __APPLE_CC__ >= 5666 || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
94#undef stpncpy
95#define stpncpy(dest, src, len)					\
96  __builtin___stpncpy_chk (dest, src, len, __darwin_obsz (dest))
97#endif
98#endif /* _DARWIN_C_LEVEL >= 200809L */
99
100#if __DARWIN_C_LEVEL >= __DARWIN_C_FULL
101#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000 || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
102#if __has_builtin(__builtin___strlcpy_chk) && __HAS_FIXED_CHK_PROTOTYPES
103#undef strlcpy
104#define strlcpy(dest, src, len)                                 \
105  __builtin___strlcpy_chk (dest, src, len, __darwin_obsz (dest))
106#endif
107
108#if __has_builtin(__builtin___strlcat_chk) && __HAS_FIXED_CHK_PROTOTYPES
109#undef strlcat
110#define strlcat(dest, src, len)                                 \
111  __builtin___strlcat_chk (dest, src, len, __darwin_obsz (dest))
112#endif
113#endif /* __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000 || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090 */
114#endif /* __DARWIN_C_LEVEL >= __DARWIN_C_FULL */
115
116#if __has_builtin(__builtin___strncpy_chk) || defined(__GNUC__)
117#undef strncpy
118#define strncpy(dest, src, len)					\
119  __builtin___strncpy_chk (dest, src, len, __darwin_obsz (dest))
120#endif
121
122#if __has_builtin(__builtin___strcat_chk) || defined(__GNUC__)
123#undef strcat
124#define strcat(dest, src)					\
125  __builtin___strcat_chk (dest, src, __darwin_obsz (dest))
126#endif
127
128#if ! (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 32000)
129#if __has_builtin(__builtin___strncat_chk) || defined(__GNUC__)
130#undef strncat
131#define strncat(dest, src, len)					\
132  __builtin___strncat_chk (dest, src, len, __darwin_obsz (dest))
133#endif
134#endif
135
136#ifdef _undef__has_builtin
137#undef _undef__has_builtin
138#undef __has_builtin
139#endif
140
141#undef __HAS_FIXED_CHK_PROTOTYPES
142
143#endif /* _USE_FORTIFY_LEVEL > 0 */
144#endif /* _SECURE__STRING_H_ */
145