leave: we should also cache time zone
[freebsd/src.git] / include / ssp / wchar.h
blob7ed84e087f5d0421beb91248cfb449cf40f23f91
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2024, Klara, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
27 #ifndef _SSP_WCHAR_H_
28 #define _SSP_WCHAR_H_
30 #include <ssp/ssp.h>
32 #if __SSP_FORTIFY_LEVEL > 0
34 __ssp_inline int
35 __ssp_wchar_overlap(const void *leftp, const void *rightp, size_t len)
38 if (len > __SIZE_T_MAX / sizeof(wchar_t))
39 return (1);
40 return (__ssp_overlap(leftp, rightp, len * sizeof(wchar_t)));
44 * __ssp_wbos for w*() calls where the size parameters are in sizeof(wchar_t)
45 * units, so the result needs to be scaled appropriately.
47 __ssp_inline size_t
48 __ssp_wbos(void *ptr)
50 const size_t ptrsize = __ssp_bos(ptr);
52 if (ptrsize == (size_t)-1)
53 return (ptrsize);
55 return (ptrsize / sizeof(wchar_t));
58 __BEGIN_DECLS
59 __ssp_redirect_raw_impl(wchar_t *, wmemcpy, wmemcpy,
60 (wchar_t *__restrict buf, const wchar_t *__restrict src, size_t len))
62 const size_t slen = __ssp_wbos(buf);
64 if (len > slen)
65 __chk_fail();
66 if (__ssp_wchar_overlap(src, buf, len))
67 __chk_fail();
69 return (__ssp_real(wmemcpy)(buf, src, len));
72 __ssp_redirect_raw_impl(wchar_t *, wmempcpy, wmempcpy,
73 (wchar_t *__restrict buf, const wchar_t *__restrict src, size_t len))
75 const size_t slen = __ssp_wbos(buf);
77 if (len > slen)
78 __chk_fail();
79 if (__ssp_wchar_overlap(src, buf, len))
80 __chk_fail();
82 return (__ssp_real(wmempcpy)(buf, src, len));
85 __ssp_redirect_raw_impl(wchar_t *, wmemmove, wmemmove,
86 (wchar_t *buf, const wchar_t *src, size_t len))
88 const size_t slen = __ssp_wbos(buf);
90 if (len > slen)
91 __chk_fail();
93 return (__ssp_real(wmemmove)(buf, src, len));
96 __ssp_redirect_raw_impl(wchar_t *, wmemset, wmemset,
97 (wchar_t *buf, wchar_t c, size_t len))
99 const size_t slen = __ssp_wbos(buf);
101 if (len > slen)
102 __chk_fail();
103 return (__ssp_real(wmemset)(buf, c, len));
106 __ssp_redirect_raw_impl(wchar_t *, wcpcpy, wcpcpy,
107 (wchar_t *__restrict buf, const wchar_t *__restrict src))
109 const size_t slen = __ssp_wbos(buf);
110 const size_t len = wcslen(src);
112 if (len >= slen)
113 __chk_fail();
114 if (__ssp_wchar_overlap(buf, src, len))
115 __chk_fail();
117 (void)__ssp_real(wmemcpy)(buf, src, len + 1);
118 return (buf + len);
121 __ssp_redirect_raw_impl(wchar_t *, wcpncpy, wcpncpy,
122 (wchar_t *__restrict buf, const wchar_t *__restrict src, size_t len))
124 const size_t slen = __ssp_wbos(buf);
126 if (len > slen)
127 __chk_fail();
128 if (__ssp_wchar_overlap(buf, src, len))
129 __chk_fail();
131 return (__ssp_real(wcpncpy)(buf, src, len));
134 __ssp_redirect_raw_impl(wchar_t *, wcscat, wcscat,
135 (wchar_t *__restrict buf, const wchar_t *__restrict src))
137 size_t slen = __ssp_wbos(buf);
138 wchar_t *cp;
140 cp = buf;
141 while (*cp != L'\0') {
142 cp++;
143 if (slen-- == 0)
144 __chk_fail();
147 while (*src != L'\0') {
148 if (slen-- == 0)
149 __chk_fail();
150 *cp++ = *src++;
153 if (slen-- == 0)
154 __chk_fail();
155 *cp = '\0';
156 return (buf);
159 __ssp_redirect_raw_impl(wchar_t *, wcscpy, wcscpy,
160 (wchar_t *__restrict buf, const wchar_t *__restrict src))
162 const size_t slen = __ssp_wbos(buf);
163 size_t len = wcslen(src) + 1;
165 if (len > slen)
166 __chk_fail();
167 if (__ssp_wchar_overlap(buf, src, len))
168 __chk_fail();
170 return (__ssp_real(wmemcpy)(buf, src, len));
173 __ssp_redirect_raw_impl(wchar_t *, wcsncat, wcsncat,
174 (wchar_t *__restrict buf, const wchar_t *__restrict src, size_t len))
176 const size_t slen = __ssp_wbos(buf);
178 if (len == 0)
179 return (buf);
180 if (len > slen)
181 __chk_fail();
182 if (__ssp_wchar_overlap(buf, src, len))
183 __chk_fail();
185 return (__ssp_real(wcsncat)(buf, src, len));
188 __ssp_redirect_raw_impl(size_t, wcslcat, wcslcat,
189 (wchar_t *__restrict buf, const wchar_t *__restrict src, size_t len))
191 const size_t slen = __ssp_wbos(buf);
193 if (len > slen)
194 __chk_fail();
195 if (__ssp_wchar_overlap(buf, src, len))
196 __chk_fail();
198 return (__ssp_real(wcslcat)(buf, src, len));
201 __ssp_redirect_raw_impl(wchar_t *, wcsncpy, wcsncpy,
202 (wchar_t *__restrict buf, const wchar_t *__restrict src, size_t len))
204 const size_t slen = __ssp_wbos(buf);
206 if (len > slen)
207 __chk_fail();
208 if (__ssp_wchar_overlap(buf, src, len))
209 __chk_fail();
211 return (__ssp_real(wcsncpy)(buf, src, len));
214 __ssp_redirect_raw_impl(size_t, wcslcpy, wcslcpy,
215 (wchar_t *__restrict buf, const wchar_t *__restrict src, size_t len))
217 const size_t slen = __ssp_wbos(buf);
219 if (len > slen)
220 __chk_fail();
221 if (__ssp_wchar_overlap(buf, src, len))
222 __chk_fail();
224 return (__ssp_real(wcslcpy)(buf, src, len));
226 __END_DECLS
228 #endif /* __SSP_FORTIFY_LEVEL > 0 */
229 #endif /* _SSP_WCHAR_H_ */