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 * @OSF_COPYRIGHT@
30 */
31/*
32 *(C)UNIX System Laboratories, Inc. all or some portions of this file are
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.
37 */
38
39/*
40 * Mach Operating System
41 * Copyright (c) 1993,1991,1990,1989,1988 Carnegie Mellon University
42 * All Rights Reserved.
43 *
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.
49 *
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.
53 *
54 * Carnegie Mellon requests users of this software to return to
55 *
56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
57 * School of Computer Science
58 * Carnegie Mellon University
59 * Pittsburgh PA 15213-3890
60 *
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 */
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 */
132/*
133 * Random device subroutines and stubs.
134 */
135
136#include <vm/vm_kern.h>
137#include <kern/misc_protos.h>
138#include <libsa/stdlib.h>
139#include <sys/malloc.h>
140
141/* String routines, from CMU */
142#ifdef strcpy
143#undef strcmp
144#undef strncmp
145#undef strcpy
146#undef strlen
147#endif
148
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
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.
168 * Deprecation Warning:
169 * strcmp() is being deprecated. Please use strncmp() instead.
170 */
171
172int
173strcmp(
174 const char *s1,
175 const char *s2)
176{
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 } while (a != '\0');
187
188 return 0; /* both are zero */
189}
190
191/*
192 * Abstract:
193 * strncmp (s1, s2, n) compares the strings "s1" and "s2"
194 * in exactly the same way as strcmp does. Except the
195 * comparison runs for at most "n" characters.
196 */
197
198#if !defined __arm__ && !defined __arm64__
199// ARM implementation in ../arm/strncmp.s
200// ARM64 implementation in ../arm64/strncmp.s
201int
202strncmp(
203 const char *s1,
204 const char *s2,
205 size_t n)
206{
207 unsigned int a, b;
208
209 while (n != 0) {
210 a = *s1++;
211 b = *s2++;
212 if (a != b)
213 return a-b; /* includes case when
214 'a' is zero and 'b' is not zero
215 or vice versa */
216 if (a == '\0')
217 return 0; /* both are zero */
218 n--;
219 }
220
221 return 0;
222}
223#endif // #ifndef __arm__
224
225
226//
227// Lame implementation just for use by strcasecmp/strncasecmp
228//
229static int
230tolower(unsigned char ch)
231{
232 if (ch >= 'A' && ch <= 'Z')
233 ch = 'a' + (ch - 'A');
234
235 return ch;
236}
237
238int
239strcasecmp(const char *s1, const char *s2)
240{
241 const unsigned char *us1 = (const u_char *)s1,
242 *us2 = (const u_char *)s2;
243
244 while (tolower(*us1) == tolower(*us2++))
245 if (*us1++ == '\0')
246 return (0);
247 return (tolower(*us1) - tolower(*--us2));
248}
249
250int
251strncasecmp(const char *s1, const char *s2, size_t n)
252{
253 if (n != 0) {
254 const unsigned char *us1 = (const u_char *)s1,
255 *us2 = (const u_char *)s2;
256
257 do {
258 if (tolower(*us1) != tolower(*us2++))
259 return (tolower(*us1) - tolower(*--us2));
260 if (*us1++ == '\0')
261 break;
262 } while (--n != 0);
263 }
264 return (0);
265}
266
267char *
268strchr(const char *s, int c)
269{
270 if (!s) {
271 return NULL;
272 }
273
274 do {
275 if (*s == c) {
276 return __CAST_AWAY_QUALIFIER(s, const, char *);
277 }
278 } while (*s++);
279
280 return NULL;
281}
282
283char *
284strrchr(const char *s, int c)
285{
286 const char *found = NULL;
287
288 if (!s) {
289 return NULL;
290 }
291
292 do {
293 if (*s == c) {
294 found = s;
295 }
296 } while (*s++);
297
298 return __CAST_AWAY_QUALIFIER(found, const, char *);
299}
300
301/*
302 * Abstract:
303 * strcpy copies the contents of the string "from" including
304 * the null terminator to the string "to". A pointer to "to"
305 * is returned.
306 * Deprecation Warning:
307 * strcpy() is being deprecated. Please use strlcpy() instead.
308 */
309#if !CONFIG_EMBEDDED
310char *
311strcpy(
312 char *to,
313 const char *from)
314{
315 char *ret = to;
316
317 while ((*to++ = *from++) != '\0')
318 continue;
319
320 return ret;
321}
322#endif
323
324/*
325 * Abstract:
326 * strncpy copies "count" characters from the "from" string to
327 * the "to" string. If "from" contains less than "count" characters
328 * "to" will be padded with null characters until exactly "count"
329 * characters have been written. The return value is a pointer
330 * to the "to" string.
331 */
332
333#if !defined __arm__ && !defined __arm64__
334// ARM and ARM64 implementation in ../arm/strncpy.c
335#undef strncpy
336char *
337strncpy(
338 char *s1,
339 const char *s2,
340 size_t n)
341{
342 char *os1 = s1;
343 unsigned long i;
344
345 for (i = 0; i < n;)
346 if ((*s1++ = *s2++) == '\0')
347 for (i++; i < n; i++)
348 *s1++ = '\0';
349 else
350 i++;
351 return (os1);
352}
353#endif // #ifndef __arm__
354
355/*
356 * atoi:
357 *
358 * This function converts an ascii string into an integer.
359 *
360 * input : string
361 * output : a number
362 */
363
364int
365atoi(const char *cp)
366{
367 int number;
368
369 for (number = 0; ('0' <= *cp) && (*cp <= '9'); cp++)
370 number = (number * 10) + (*cp - '0');
371
372 return( number );
373}
374
375/*
376 * convert an ASCII string (decimal radix) to an integer
377 * inputs:
378 * p string pointer.
379 * t char **, return a pointer to the cahr which terminates the
380 * numeric string.
381 * returns:
382 * integer value of the numeric string.
383 * side effect:
384 * pointer to terminating char.
385 */
386
387int
388atoi_term(
389 char *p, /* IN */
390 char **t) /* OUT */
391{
392 int n;
393 int f;
394
395 n = 0;
396 f = 0;
397 for(;;p++) {
398 switch(*p) {
399 case ' ':
400 case '\t':
401 continue;
402 case '-':
403 f++;
404 case '+':
405 p++;
406 }
407 break;
408 }
409 while(*p >= '0' && *p <= '9')
410 n = n*10 + *p++ - '0';
411
412 /* return pointer to terminating character */
413 if ( t )
414 *t = p;
415
416 return(f? -n: n);
417}
418
419/*
420 * Does the same thing as strlen, except only looks up
421 * to max chars inside the buffer.
422 * Taken from archive/kern-stuff/sbf_machine.c in
423 * seatbelt.
424 * inputs:
425 * s string whose length is to be measured
426 * max maximum length of string to search for null
427 * outputs:
428 * length of s or max; whichever is smaller
429 */
430
431#if !defined __arm__ && !defined __arm64__
432// ARM implementation in ../arm/strnlen.s
433// ARM64 implementation in ../arm64/strnlen.s
434#undef strnlen
435size_t
436strnlen(const char *s, size_t max) {
437 const char *es = s + max, *p = s;
438 while(*p && p != es)
439 p++;
440
441 return p - s;
442}
443#endif // #ifndef __arm__
444
445/*
446 * convert an integer to an ASCII string.
447 * inputs:
448 * num integer to be converted
449 * str string pointer.
450 *
451 * outputs:
452 * pointer to string start.
453 */
454
455char *
456itoa(
457 int num,
458 char *str)
459{
460 char digits[11];
461 char *dp;
462 char *cp = str;
463
464 if (num == 0) {
465 *cp++ = '0';
466 }
467 else {
468 dp = digits;
469 while (num) {
470 *dp++ = '0' + num % 10;
471 num /= 10;
472 }
473 while (dp != digits) {
474 *cp++ = *--dp;
475 }
476 }
477 *cp++ = '\0';
478
479 return str;
480}
481
482/*
483 * Deprecation Warning:
484 * strcat() is being deprecated. Please use strlcat() instead.
485 */
486#if !CONFIG_EMBEDDED
487char *
488strcat(
489 char *dest,
490 const char *src)
491{
492 char *old = dest;
493
494 while (*dest)
495 ++dest;
496 while ((*dest++ = *src++))
497 ;
498 return (old);
499}
500#endif
501
502/*
503 * Appends src to string dst of size siz (unlike strncat, siz is the
504 * full size of dst, not space left). At most siz-1 characters
505 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
506 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
507 * If retval >= siz, truncation occurred.
508 */
509#undef strlcat
510size_t
511strlcat(char *dst, const char *src, size_t siz)
512{
513 char *d = dst;
514 const char *s = src;
515 size_t n = siz;
516 size_t dlen;
517
518 /* Find the end of dst and adjust bytes left but don't go past end */
519 while (n-- != 0 && *d != '\0')
520 d++;
521 dlen = d - dst;
522 n = siz - dlen;
523
524 if (n == 0)
525 return(dlen + strlen(s));
526 while (*s != '\0') {
527 if (n != 1) {
528 *d++ = *s;
529 n--;
530 }
531 s++;
532 }
533 *d = '\0';
534
535 return(dlen + (s - src)); /* count does not include NUL */
536}
537
538/*
539 * Copy src to string dst of size siz. At most siz-1 characters
540 * will be copied. Always NUL terminates (unless siz == 0).
541 * Returns strlen(src); if retval >= siz, truncation occurred.
542 */
543
544#if !defined __arm__ && !defined __arm64__
545// ARM and ARM64 implementation in ../arm/strlcpy.c
546#undef strlcpy
547size_t
548strlcpy(char *dst, const char *src, size_t siz)
549{
550 char *d = dst;
551 const char *s = src;
552 size_t n = siz;
553
554 /* Copy as many bytes as will fit */
555 if (n != 0 && --n != 0) {
556 do {
557 if ((*d++ = *s++) == 0)
558 break;
559 } while (--n != 0);
560 }
561
562 /* Not enough room in dst, add NUL and traverse rest of src */
563 if (n == 0) {
564 if (siz != 0)
565 *d = '\0'; /* NUL-terminate dst */
566 while (*s++)
567 ;
568 }
569
570 return(s - src - 1); /* count does not include NUL */
571}
572#endif
573
574/*
575 * STRDUP
576 *
577 * Description: The STRDUP function allocates sufficient memory for a copy
578 * of the string "string", does the copy, and returns a pointer
579 * it. The pointer may subsequently be used as an argument to
580 * the macro FREE().
581 *
582 * Parameters: string String to be duplicated
583 * type type of memory to be allocated (normally
584 * M_TEMP)
585 *
586 * Returns: char * A pointer to the newly allocated string with
587 * duplicated contents in it.
588 *
589 * NULL If MALLOC() fails.
590 *
591 * Note: This function can *not* be called from interrupt context as
592 * it calls MALLOC with M_WAITOK. In fact, you really
593 * shouldn't be doing string manipulation in interrupt context
594 * ever.
595 *
596 * This function name violates the kernel style(9) guide
597 * by being all caps. This was done on purpose to emphasize
598 * one should use FREE() with the allocated buffer.
599 *
600 */
601char *
602STRDUP(const char *string, int type)
603{
604 size_t len;
605 char *copy;
606
607 len = strlen(string) + 1;
608 MALLOC(copy, char *, len, type, M_WAITOK);
609 if (copy == NULL)
610 return (NULL);
611 bcopy(string, copy, len);
612 return (copy);
613}
614
615/*
616 * Return TRUE(1) if string 2 is a prefix of string 1.
617 */
618int
619strprefix(const char *s1, const char *s2)
620{
621 int c;
622
623 while ((c = *s2++) != '\0') {
624 if (c != *s1++)
625 return (0);
626 }
627 return (1);
628}
629
630char *
631strnstr(char *s, const char *find, size_t slen)
632{
633 char c, sc;
634 size_t len;
635
636 if ((c = *find++) != '\0') {
637 len = strlen(find);
638 do {
639 do {
640 if ((sc = *s++) == '\0' || slen-- < 1)
641 return (NULL);
642 } while (sc != c);
643 if (len > slen)
644 return (NULL);
645 } while (strncmp(s, find, len) != 0);
646 s--;
647 }
648 return (s);
649}
650
651void * __memcpy_chk(void *dst, void const *src, size_t s, size_t chk_size);
652void * __memmove_chk(void *dst, void const *src, size_t s, size_t chk_size);
653void * __memset_chk(void *dst, int c, size_t s, size_t chk_size);
654size_t __strlcpy_chk(char *dst, char const *src, size_t s, size_t chk_size);
655size_t __strlcat_chk(char *dst, char const *src, size_t s, size_t chk_size);
656char * __strncpy_chk (char *restrict dst, char *restrict src, size_t len, size_t chk_size);
657char * __strncat_chk (char *restrict dst, const char *restrict src, size_t len, size_t chk_size);
658char * __strcpy_chk(char *restrict dst, const char *restrict src, size_t chk_size);
659char * __strcat_chk (char *restrict dst, const char *restrict src, size_t chk_size);
660
661void *
662__memcpy_chk(void *dst, void const *src, size_t s, size_t chk_size)
663{
664 if (__improbable(chk_size < s))
665 panic("__memcpy_chk object size check failed: dst %p, src %p, (%zu < %zu)", dst, src, chk_size, s);
666 return memcpy(dst, src, s);
667}
668
669void *
670__memmove_chk(void *dst, void const *src, size_t s, size_t chk_size)
671{
672 if (__improbable(chk_size < s))
673 panic("__memmove_chk object size check failed: dst %p, src %p, (%zu < %zu)", dst, src, chk_size, s);
674 return memmove(dst, src, s);
675}
676
677void *
678__memset_chk(void *dst, int c, size_t s, size_t chk_size)
679{
680 if (__improbable(chk_size < s))
681 panic("__memset_chk object size check failed: dst %p, c %c, (%zu < %zu)", dst, c, chk_size, s);
682 return memset(dst, c, s);
683}
684
685size_t
686__strlcat_chk(char *dst, char const *src, size_t s, size_t chk_size)
687{
688 if (__improbable(chk_size < s))
689 panic("__strlcat_chk object size check failed: dst %p, src %p, (%zu < %zu)", dst, src, chk_size, s);
690 return strlcat(dst, src, s);
691}
692
693size_t
694__strlcpy_chk(char *dst, char const *src, size_t s, size_t chk_size)
695{
696 if (__improbable(chk_size < s))
697 panic("__strlcpy_chk object size check failed: dst %p, src %p, (%zu < %zu)", dst, src, chk_size, s);
698 return strlcpy(dst, src, s);
699}
700
701char *
702__strncpy_chk (char *restrict dst, char *restrict src,
703 size_t len, size_t chk_size)
704{
705 if (__improbable(chk_size < len)) {
706 panic("__strncpy_chk object size check failed: dst %p, src %p, (%zu < %zu)", dst, src, chk_size, len);
707 }
708 return strncpy(dst, src, len);
709}
710
711char *
712__strncat_chk (char *restrict dst, const char *restrict src,
713 size_t len, size_t chk_size)
714{
715 size_t len1 = strlen(dst);
716 size_t len2 = strnlen(src, len);
717 if (__improbable (chk_size < len1 + len2 + 1)) {
718 panic("__strncat_chk object size check failed: dst %p, src %p, (%zu < %zu + %zu + 1)", dst, src, chk_size, len1, len2);
719 }
720 return strncat(dst, src, len);
721}
722
723char *
724__strcpy_chk (char *restrict dst, const char *restrict src, size_t chk_size)
725{
726 size_t len = strlen(src);
727 if (__improbable (chk_size < len + 1)) {
728 panic("__strcpy_chk object size check failed: dst %p, src %p, (%zu < %zu + 1)", dst, src, chk_size, len);
729 }
730 memcpy(dst, src, len+1);
731 return dst;
732}
733
734char *
735__strcat_chk (char *restrict dst, const char *restrict src, size_t chk_size)
736{
737 size_t len1 = strlen(dst);
738 size_t len2 = strlen(src);
739 size_t required_len = len1 + len2 + 1;
740 if (__improbable (chk_size < required_len)) {
741 panic("__strcat_chk object size check failed: dst %p, src %p, (%zu < %zu + %zu + 1)", dst, src, chk_size, len1, len2);
742 }
743 memcpy(dst + len1, src, len2 + 1);
744 return dst;
745}
746