]> git.saurik.com Git - apple/xnu.git/blame - osfmk/device/subrs.c
xnu-6153.41.3.tar.gz
[apple/xnu.git] / osfmk / device / subrs.c
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 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 *(C)UNIX System Laboratories, Inc. all or some portions of this file are
0a7de745
A
33 * derived from material licensed to the University of California by
34 * American Telephone and Telegraph Co. or UNIX System Laboratories,
35 * Inc. and are reproduced herein with the permission of UNIX System
36 * Laboratories, Inc.
1c79356b
A
37 */
38
0a7de745 39/*
1c79356b
A
40 * Mach Operating System
41 * Copyright (c) 1993,1991,1990,1989,1988 Carnegie Mellon University
42 * All Rights Reserved.
0a7de745 43 *
1c79356b
A
44 * Permission to use, copy, modify and distribute this software and its
45 * documentation is hereby granted, provided that both the copyright
46 * notice and this permission notice appear in all copies of the
47 * software, derivative works or modified versions, and any portions
48 * thereof, and that both notices appear in supporting documentation.
0a7de745 49 *
1c79356b
A
50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
52 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
0a7de745 53 *
1c79356b 54 * Carnegie Mellon requests users of this software to return to
0a7de745 55 *
1c79356b
A
56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
57 * School of Computer Science
58 * Carnegie Mellon University
59 * Pittsburgh PA 15213-3890
0a7de745 60 *
1c79356b
A
61 * any improvements or extensions that they make and grant Carnegie Mellon
62 * the rights to redistribute these changes.
63 */
64/*
65 */
66/*
67 * Copyright (c) 1988 Regents of the University of California.
68 * All rights reserved.
69 *
70 * Redistribution and use in source and binary forms, with or without
71 * modification, are permitted provided that the following conditions
72 * are met:
73 * 1. Redistributions of source code must retain the above copyright
74 * notice, this list of conditions and the following disclaimer.
75 * 2. Redistributions in binary form must reproduce the above copyright
76 * notice, this list of conditions and the following disclaimer in the
77 * documentation and/or other materials provided with the distribution.
78 * 3. All advertising materials mentioning features or use of this software
79 * must display the following acknowledgement:
80 * This product includes software developed by the University of
81 * California, Berkeley and its contributors.
82 * 4. Neither the name of the University nor the names of its contributors
83 * may be used to endorse or promote products derived from this software
84 * without specific prior written permission.
85 *
86 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
87 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
88 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
89 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
90 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
91 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
92 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
93 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
94 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
95 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
96 * SUCH DAMAGE.
97 */
2d21ac55
A
98
99/*
100 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
101 * All rights reserved.
102 *
103 * Redistribution and use in source and binary forms, with or without
104 * modification, are permitted provided that the following conditions
105 * are met:
106 * 1. Redistributions of source code must retain the above copyright
107 * notice, this list of conditions and the following disclaimer.
108 * 2. Redistributions in binary form must reproduce the above copyright
109 * notice, this list of conditions and the following disclaimer in the
110 * documentation and/or other materials provided with the distribution.
111 * 3. The name of the author may not be used to endorse or promote products
112 * derived from this software without specific prior written permission.
113 *
114 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
115 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
116 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
117 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
118 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
119 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
120 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
121 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
122 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
123 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
124 */
125
126/*
127 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
128 * support for mandatory and extensible security protections. This notice
129 * is included in support of clause 2.2 (b) of the Apple Public License,
130 * Version 2.0.
131 */
1c79356b
A
132/*
133 * Random device subroutines and stubs.
134 */
135
136#include <vm/vm_kern.h>
137#include <kern/misc_protos.h>
2d21ac55
A
138#include <libsa/stdlib.h>
139#include <sys/malloc.h>
1c79356b
A
140
141/* String routines, from CMU */
0a7de745 142#ifdef strcpy
1c79356b
A
143#undef strcmp
144#undef strncmp
145#undef strcpy
1c79356b
A
146#undef strlen
147#endif
148
5ba3f43e
A
149/* to prevent recursion in the _chk functions */
150#undef strcat
151#undef strncpy
152#undef strncat
153#undef memcpy
154#undef memset
155#undef memmove
156#undef strlcpy
157#undef strlcat
1c79356b
A
158/*
159 * Abstract:
160 * strcmp (s1, s2) compares the strings "s1" and "s2".
161 * It returns 0 if the strings are identical. It returns
162 * > 0 if the first character that differs in the two strings
163 * is larger in s1 than in s2 or if s1 is longer than s2 and
164 * the contents are identical up to the length of s2.
165 * It returns < 0 if the first differing character is smaller
166 * in s1 than in s2 or if s1 is shorter than s2 and the
167 * contents are identical upto the length of s1.
2d21ac55
A
168 * Deprecation Warning:
169 * strcmp() is being deprecated. Please use strncmp() instead.
1c79356b
A
170 */
171
172int
173strcmp(
0a7de745
A
174 const char *s1,
175 const char *s2)
1c79356b 176{
0a7de745
A
177 unsigned int a, b;
178
179 do {
180 a = *s1++;
181 b = *s2++;
182 if (a != b) {
183 return a - b; /* includes case when
184 * 'a' is zero and 'b' is not zero
185 * or vice versa */
186 }
1c79356b
A
187 } while (a != '\0');
188
0a7de745 189 return 0; /* both are zero */
1c79356b
A
190}
191
192/*
193 * Abstract:
194 * strncmp (s1, s2, n) compares the strings "s1" and "s2"
195 * in exactly the same way as strcmp does. Except the
196 * comparison runs for at most "n" characters.
197 */
198
5ba3f43e 199#if !defined __arm__ && !defined __arm64__
316670eb 200// ARM implementation in ../arm/strncmp.s
fe8ab488 201// ARM64 implementation in ../arm64/strncmp.s
1c79356b
A
202int
203strncmp(
0a7de745
A
204 const char *s1,
205 const char *s2,
206 size_t n)
1c79356b 207{
0a7de745
A
208 unsigned int a, b;
209
210 while (n != 0) {
211 a = *s1++;
212 b = *s2++;
213 if (a != b) {
214 return a - b; /* includes case when
215 * 'a' is zero and 'b' is not zero
216 * or vice versa */
217 }
218 if (a == '\0') {
219 return 0; /* both are zero */
220 }
221 n--;
1c79356b
A
222 }
223
0a7de745 224 return 0;
1c79356b 225}
5ba3f43e 226#endif // #ifndef __arm__
1c79356b 227
91447636
A
228
229//
230// Lame implementation just for use by strcasecmp/strncasecmp
231//
232static int
233tolower(unsigned char ch)
234{
0a7de745
A
235 if (ch >= 'A' && ch <= 'Z') {
236 ch = 'a' + (ch - 'A');
237 }
91447636 238
0a7de745 239 return ch;
91447636
A
240}
241
242int
243strcasecmp(const char *s1, const char *s2)
244{
0a7de745
A
245 const unsigned char *us1 = (const u_char *)s1,
246 *us2 = (const u_char *)s2;
91447636 247
0a7de745
A
248 while (tolower(*us1) == tolower(*us2++)) {
249 if (*us1++ == '\0') {
250 return 0;
251 }
252 }
253 return tolower(*us1) - tolower(*--us2);
91447636
A
254}
255
256int
257strncasecmp(const char *s1, const char *s2, size_t n)
258{
0a7de745
A
259 if (n != 0) {
260 const unsigned char *us1 = (const u_char *)s1,
261 *us2 = (const u_char *)s2;
91447636 262
0a7de745
A
263 do {
264 if (tolower(*us1) != tolower(*us2++)) {
265 return tolower(*us1) - tolower(*--us2);
266 }
267 if (*us1++ == '\0') {
268 break;
269 }
270 } while (--n != 0);
271 }
272 return 0;
91447636
A
273}
274
5ba3f43e
A
275char *
276strchr(const char *s, int c)
277{
278 if (!s) {
279 return NULL;
280 }
281
282 do {
283 if (*s == c) {
284 return __CAST_AWAY_QUALIFIER(s, const, char *);
285 }
286 } while (*s++);
287
288 return NULL;
289}
290
291char *
292strrchr(const char *s, int c)
293{
294 const char *found = NULL;
295
296 if (!s) {
297 return NULL;
298 }
299
300 do {
301 if (*s == c) {
302 found = s;
303 }
304 } while (*s++);
305
306 return __CAST_AWAY_QUALIFIER(found, const, char *);
307}
91447636 308
1c79356b
A
309/*
310 * Abstract:
311 * strcpy copies the contents of the string "from" including
312 * the null terminator to the string "to". A pointer to "to"
313 * is returned.
0a7de745 314 * Deprecation Warning:
2d21ac55 315 * strcpy() is being deprecated. Please use strlcpy() instead.
1c79356b 316 */
5ba3f43e 317#if !CONFIG_EMBEDDED
1c79356b
A
318char *
319strcpy(
0a7de745
A
320 char *to,
321 const char *from)
1c79356b 322{
0a7de745 323 char *ret = to;
1c79356b 324
0a7de745
A
325 while ((*to++ = *from++) != '\0') {
326 continue;
327 }
1c79356b 328
0a7de745 329 return ret;
1c79356b 330}
5ba3f43e 331#endif
1c79356b
A
332
333/*
334 * Abstract:
335 * strncpy copies "count" characters from the "from" string to
336 * the "to" string. If "from" contains less than "count" characters
337 * "to" will be padded with null characters until exactly "count"
338 * characters have been written. The return value is a pointer
339 * to the "to" string.
340 */
341
5ba3f43e 342#if !defined __arm__ && !defined __arm64__
fe8ab488 343// ARM and ARM64 implementation in ../arm/strncpy.c
5ba3f43e 344#undef strncpy
1c79356b
A
345char *
346strncpy(
0a7de745 347 char *s1,
1c79356b
A
348 const char *s2,
349 size_t n)
350{
0a7de745
A
351 char *os1 = s1;
352 unsigned long i;
353
354 for (i = 0; i < n;) {
355 if ((*s1++ = *s2++) == '\0') {
356 for (i++; i < n; i++) {
357 *s1++ = '\0';
358 }
359 } else {
360 i++;
361 }
362 }
363 return os1;
1c79356b 364}
5ba3f43e 365#endif // #ifndef __arm__
1c79356b 366
1c79356b
A
367/*
368 * atoi:
369 *
370 * This function converts an ascii string into an integer.
371 *
372 * input : string
373 * output : a number
374 */
375
376int
2d21ac55 377atoi(const char *cp)
1c79356b 378{
0a7de745 379 int number;
1c79356b 380
0a7de745
A
381 for (number = 0; ('0' <= *cp) && (*cp <= '9'); cp++) {
382 number = (number * 10) + (*cp - '0');
383 }
1c79356b 384
0a7de745 385 return number;
1c79356b
A
386}
387
388/*
389 * convert an ASCII string (decimal radix) to an integer
390 * inputs:
391 * p string pointer.
392 * t char **, return a pointer to the cahr which terminates the
393 * numeric string.
394 * returns:
395 * integer value of the numeric string.
396 * side effect:
397 * pointer to terminating char.
398 */
399
400int
401atoi_term(
0a7de745
A
402 char *p, /* IN */
403 char **t) /* OUT */
1c79356b 404{
0a7de745
A
405 int n;
406 int f;
407
408 n = 0;
409 f = 0;
410 for (;; p++) {
411 switch (*p) {
412 case ' ':
413 case '\t':
414 continue;
415 case '-':
416 f++;
417 case '+':
418 p++;
419 }
420 break;
421 }
422 while (*p >= '0' && *p <= '9') {
423 n = n * 10 + *p++ - '0';
424 }
425
426 /* return pointer to terminating character */
427 if (t) {
428 *t = p;
429 }
430
431 return f? -n: n;
1c79356b
A
432}
433
6601e61a
A
434/*
435 * Does the same thing as strlen, except only looks up
0a7de745
A
436 * to max chars inside the buffer.
437 * Taken from archive/kern-stuff/sbf_machine.c in
438 * seatbelt.
6601e61a 439 * inputs:
0a7de745 440 * s string whose length is to be measured
2d21ac55 441 * max maximum length of string to search for null
6601e61a 442 * outputs:
2d21ac55 443 * length of s or max; whichever is smaller
6601e61a 444 */
316670eb 445
5ba3f43e 446#if !defined __arm__ && !defined __arm64__
316670eb 447// ARM implementation in ../arm/strnlen.s
fe8ab488 448// ARM64 implementation in ../arm64/strnlen.s
5ba3f43e 449#undef strnlen
39236c6e 450size_t
0a7de745
A
451strnlen(const char *s, size_t max)
452{
6601e61a 453 const char *es = s + max, *p = s;
0a7de745 454 while (*p && p != es) {
6601e61a 455 p++;
0a7de745 456 }
6601e61a
A
457
458 return p - s;
459}
5ba3f43e 460#endif // #ifndef __arm__
6601e61a 461
1c79356b
A
462/*
463 * convert an integer to an ASCII string.
464 * inputs:
465 * num integer to be converted
466 * str string pointer.
467 *
468 * outputs:
469 * pointer to string start.
470 */
471
472char *
473itoa(
0a7de745
A
474 int num,
475 char *str)
1c79356b 476{
0a7de745
A
477 char digits[11];
478 char *dp;
479 char *cp = str;
480
481 if (num == 0) {
482 *cp++ = '0';
483 } else {
484 dp = digits;
485 while (num) {
486 *dp++ = '0' + num % 10;
487 num /= 10;
488 }
489 while (dp != digits) {
490 *cp++ = *--dp;
491 }
492 }
493 *cp++ = '\0';
1c79356b
A
494
495 return str;
496}
497
0a7de745 498/*
2d21ac55
A
499 * Deprecation Warning:
500 * strcat() is being deprecated. Please use strlcat() instead.
501 */
5ba3f43e 502#if !CONFIG_EMBEDDED
1c79356b
A
503char *
504strcat(
2d21ac55
A
505 char *dest,
506 const char *src)
1c79356b
A
507{
508 char *old = dest;
509
0a7de745 510 while (*dest) {
1c79356b 511 ++dest;
0a7de745
A
512 }
513 while ((*dest++ = *src++)) {
1c79356b 514 ;
0a7de745
A
515 }
516 return old;
1c79356b 517}
5ba3f43e 518#endif
1c79356b 519
2d21ac55
A
520/*
521 * Appends src to string dst of size siz (unlike strncat, siz is the
522 * full size of dst, not space left). At most siz-1 characters
523 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
524 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
525 * If retval >= siz, truncation occurred.
526 */
5ba3f43e 527#undef strlcat
2d21ac55
A
528size_t
529strlcat(char *dst, const char *src, size_t siz)
530{
531 char *d = dst;
532 const char *s = src;
533 size_t n = siz;
534 size_t dlen;
535
536 /* Find the end of dst and adjust bytes left but don't go past end */
0a7de745 537 while (n-- != 0 && *d != '\0') {
2d21ac55 538 d++;
0a7de745 539 }
2d21ac55
A
540 dlen = d - dst;
541 n = siz - dlen;
542
0a7de745
A
543 if (n == 0) {
544 return dlen + strlen(s);
545 }
2d21ac55
A
546 while (*s != '\0') {
547 if (n != 1) {
548 *d++ = *s;
549 n--;
550 }
551 s++;
552 }
553 *d = '\0';
554
0a7de745 555 return dlen + (s - src); /* count does not include NUL */
2d21ac55
A
556}
557
558/*
559 * Copy src to string dst of size siz. At most siz-1 characters
560 * will be copied. Always NUL terminates (unless siz == 0).
561 * Returns strlen(src); if retval >= siz, truncation occurred.
562 */
316670eb 563
5ba3f43e 564#if !defined __arm__ && !defined __arm64__
fe8ab488 565// ARM and ARM64 implementation in ../arm/strlcpy.c
5ba3f43e 566#undef strlcpy
2d21ac55
A
567size_t
568strlcpy(char *dst, const char *src, size_t siz)
569{
570 char *d = dst;
571 const char *s = src;
572 size_t n = siz;
573
574 /* Copy as many bytes as will fit */
575 if (n != 0 && --n != 0) {
576 do {
0a7de745 577 if ((*d++ = *s++) == 0) {
2d21ac55 578 break;
0a7de745 579 }
2d21ac55
A
580 } while (--n != 0);
581 }
582
583 /* Not enough room in dst, add NUL and traverse rest of src */
584 if (n == 0) {
0a7de745
A
585 if (siz != 0) {
586 *d = '\0'; /* NUL-terminate dst */
587 }
588 while (*s++) {
2d21ac55 589 ;
0a7de745 590 }
2d21ac55
A
591 }
592
0a7de745 593 return s - src - 1; /* count does not include NUL */
2d21ac55 594}
5ba3f43e 595#endif
2d21ac55
A
596
597/*
598 * STRDUP
599 *
600 * Description: The STRDUP function allocates sufficient memory for a copy
601 * of the string "string", does the copy, and returns a pointer
602 * it. The pointer may subsequently be used as an argument to
603 * the macro FREE().
604 *
605 * Parameters: string String to be duplicated
606 * type type of memory to be allocated (normally
0a7de745 607 * M_TEMP)
2d21ac55
A
608 *
609 * Returns: char * A pointer to the newly allocated string with
610 * duplicated contents in it.
611 *
612 * NULL If MALLOC() fails.
613 *
614 * Note: This function can *not* be called from interrupt context as
615 * it calls MALLOC with M_WAITOK. In fact, you really
616 * shouldn't be doing string manipulation in interrupt context
617 * ever.
618 *
619 * This function name violates the kernel style(9) guide
620 * by being all caps. This was done on purpose to emphasize
621 * one should use FREE() with the allocated buffer.
622 *
623 */
6d2010ae 624char *
2d21ac55
A
625STRDUP(const char *string, int type)
626{
627 size_t len;
0a7de745 628 char *copy;
2d21ac55
A
629
630 len = strlen(string) + 1;
631 MALLOC(copy, char *, len, type, M_WAITOK);
0a7de745
A
632 if (copy == NULL) {
633 return NULL;
634 }
2d21ac55 635 bcopy(string, copy, len);
0a7de745 636 return copy;
2d21ac55
A
637}
638
639/*
640 * Return TRUE(1) if string 2 is a prefix of string 1.
39037602
A
641 */
642int
643strprefix(const char *s1, const char *s2)
644{
645 int c;
646
647 while ((c = *s2++) != '\0') {
0a7de745
A
648 if (c != *s1++) {
649 return 0;
650 }
39037602 651 }
0a7de745 652 return 1;
2d21ac55 653}
39236c6e
A
654
655char *
656strnstr(char *s, const char *find, size_t slen)
657{
0a7de745
A
658 char c, sc;
659 size_t len;
660
661 if ((c = *find++) != '\0') {
662 len = strlen(find);
663 do {
664 do {
665 if ((sc = *s++) == '\0' || slen-- < 1) {
666 return NULL;
667 }
668 } while (sc != c);
669 if (len > slen) {
670 return NULL;
671 }
672 } while (strncmp(s, find, len) != 0);
673 s--;
674 }
675 return s;
39236c6e
A
676}
677
5ba3f43e
A
678void * __memcpy_chk(void *dst, void const *src, size_t s, size_t chk_size);
679void * __memmove_chk(void *dst, void const *src, size_t s, size_t chk_size);
680void * __memset_chk(void *dst, int c, size_t s, size_t chk_size);
681size_t __strlcpy_chk(char *dst, char const *src, size_t s, size_t chk_size);
682size_t __strlcat_chk(char *dst, char const *src, size_t s, size_t chk_size);
0a7de745
A
683char * __strncpy_chk(char *restrict dst, char *restrict src, size_t len, size_t chk_size);
684char * __strncat_chk(char *restrict dst, const char *restrict src, size_t len, size_t chk_size);
5ba3f43e 685char * __strcpy_chk(char *restrict dst, const char *restrict src, size_t chk_size);
0a7de745 686char * __strcat_chk(char *restrict dst, const char *restrict src, size_t chk_size);
5ba3f43e
A
687
688void *
689__memcpy_chk(void *dst, void const *src, size_t s, size_t chk_size)
690{
0a7de745
A
691 if (__improbable(chk_size < s)) {
692 panic("__memcpy_chk object size check failed: dst %p, src %p, (%zu < %zu)", dst, src, chk_size, s);
693 }
694 return memcpy(dst, src, s);
5ba3f43e
A
695}
696
697void *
698__memmove_chk(void *dst, void const *src, size_t s, size_t chk_size)
699{
0a7de745
A
700 if (__improbable(chk_size < s)) {
701 panic("__memmove_chk object size check failed: dst %p, src %p, (%zu < %zu)", dst, src, chk_size, s);
702 }
703 return memmove(dst, src, s);
5ba3f43e
A
704}
705
706void *
707__memset_chk(void *dst, int c, size_t s, size_t chk_size)
708{
0a7de745
A
709 if (__improbable(chk_size < s)) {
710 panic("__memset_chk object size check failed: dst %p, c %c, (%zu < %zu)", dst, c, chk_size, s);
711 }
712 return memset(dst, c, s);
5ba3f43e
A
713}
714
715size_t
716__strlcat_chk(char *dst, char const *src, size_t s, size_t chk_size)
717{
0a7de745
A
718 if (__improbable(chk_size < s)) {
719 panic("__strlcat_chk object size check failed: dst %p, src %p, (%zu < %zu)", dst, src, chk_size, s);
720 }
721 return strlcat(dst, src, s);
5ba3f43e
A
722}
723
724size_t
725__strlcpy_chk(char *dst, char const *src, size_t s, size_t chk_size)
726{
0a7de745
A
727 if (__improbable(chk_size < s)) {
728 panic("__strlcpy_chk object size check failed: dst %p, src %p, (%zu < %zu)", dst, src, chk_size, s);
729 }
730 return strlcpy(dst, src, s);
5ba3f43e
A
731}
732
733char *
0a7de745
A
734__strncpy_chk(char *restrict dst, char *restrict src,
735 size_t len, size_t chk_size)
5ba3f43e 736{
0a7de745
A
737 if (__improbable(chk_size < len)) {
738 panic("__strncpy_chk object size check failed: dst %p, src %p, (%zu < %zu)", dst, src, chk_size, len);
739 }
740 return strncpy(dst, src, len);
5ba3f43e
A
741}
742
743char *
0a7de745
A
744__strncat_chk(char *restrict dst, const char *restrict src,
745 size_t len, size_t chk_size)
5ba3f43e 746{
0a7de745
A
747 size_t len1 = strlen(dst);
748 size_t len2 = strnlen(src, len);
749 if (__improbable(chk_size < len1 + len2 + 1)) {
750 panic("__strncat_chk object size check failed: dst %p, src %p, (%zu < %zu + %zu + 1)", dst, src, chk_size, len1, len2);
751 }
752 return strncat(dst, src, len);
5ba3f43e
A
753}
754
755char *
0a7de745 756__strcpy_chk(char *restrict dst, const char *restrict src, size_t chk_size)
5ba3f43e 757{
0a7de745
A
758 size_t len = strlen(src);
759 if (__improbable(chk_size < len + 1)) {
760 panic("__strcpy_chk object size check failed: dst %p, src %p, (%zu < %zu + 1)", dst, src, chk_size, len);
761 }
762 memcpy(dst, src, len + 1);
763 return dst;
5ba3f43e
A
764}
765
766char *
0a7de745 767__strcat_chk(char *restrict dst, const char *restrict src, size_t chk_size)
5ba3f43e 768{
0a7de745
A
769 size_t len1 = strlen(dst);
770 size_t len2 = strlen(src);
771 size_t required_len = len1 + len2 + 1;
772 if (__improbable(chk_size < required_len)) {
773 panic("__strcat_chk object size check failed: dst %p, src %p, (%zu < %zu + %zu + 1)", dst, src, chk_size, len1, len2);
774 }
775 memcpy(dst + len1, src, len2 + 1);
776 return dst;
5ba3f43e 777}