]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/device/subrs.c
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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
40 * Mach Operating System
41 * Copyright (c) 1993,1991,1990,1989,1988 Carnegie Mellon University
42 * All Rights Reserved.
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.
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.
54 * Carnegie Mellon requests users of this software to return to
56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
57 * School of Computer Science
58 * Carnegie Mellon University
59 * Pittsburgh PA 15213-3890
61 * any improvements or extensions that they make and grant Carnegie Mellon
62 * the rights to redistribute these changes.
67 * Copyright (c) 1988 Regents of the University of California.
68 * All rights reserved.
70 * Redistribution and use in source and binary forms, with or without
71 * modification, are permitted provided that the following conditions
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.
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
100 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
101 * All rights reserved.
103 * Redistribution and use in source and binary forms, with or without
104 * modification, are permitted provided that the following conditions
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.
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.
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,
133 * Random device subroutines and stubs.
136 #include <vm/vm_kern.h>
137 #include <kern/misc_protos.h>
138 #include <libsa/stdlib.h>
139 #include <sys/malloc.h>
141 /* String routines, from CMU */
152 * strcmp (s1, s2) compares the strings "s1" and "s2".
153 * It returns 0 if the strings are identical. It returns
154 * > 0 if the first character that differs in the two strings
155 * is larger in s1 than in s2 or if s1 is longer than s2 and
156 * the contents are identical up to the length of s2.
157 * It returns < 0 if the first differing character is smaller
158 * in s1 than in s2 or if s1 is shorter than s2 and the
159 * contents are identical upto the length of s1.
160 * Deprecation Warning:
161 * strcmp() is being deprecated. Please use strncmp() instead.
175 return a
-b
; /* includes case when
176 'a' is zero and 'b' is not zero
180 return 0; /* both are zero */
185 * strncmp (s1, s2, n) compares the strings "s1" and "s2"
186 * in exactly the same way as strcmp does. Except the
187 * comparison runs for at most "n" characters.
190 // ARM implementation in ../arm/strncmp.s
191 // ARM64 implementation in ../arm64/strncmp.s
204 return a
-b
; /* includes case when
205 'a' is zero and 'b' is not zero
208 return 0; /* both are zero */
217 // Lame implementation just for use by strcasecmp/strncasecmp
220 tolower(unsigned char ch
)
222 if (ch
>= 'A' && ch
<= 'Z')
223 ch
= 'a' + (ch
- 'A');
229 strcasecmp(const char *s1
, const char *s2
)
231 const unsigned char *us1
= (const u_char
*)s1
,
232 *us2
= (const u_char
*)s2
;
234 while (tolower(*us1
) == tolower(*us2
++))
237 return (tolower(*us1
) - tolower(*--us2
));
241 strncasecmp(const char *s1
, const char *s2
, size_t n
)
244 const unsigned char *us1
= (const u_char
*)s1
,
245 *us2
= (const u_char
*)s2
;
248 if (tolower(*us1
) != tolower(*us2
++))
249 return (tolower(*us1
) - tolower(*--us2
));
260 * strcpy copies the contents of the string "from" including
261 * the null terminator to the string "to". A pointer to "to"
263 * Deprecation Warning:
264 * strcpy() is being deprecated. Please use strlcpy() instead.
273 while ((*to
++ = *from
++) != '\0')
281 * strncpy copies "count" characters from the "from" string to
282 * the "to" string. If "from" contains less than "count" characters
283 * "to" will be padded with null characters until exactly "count"
284 * characters have been written. The return value is a pointer
285 * to the "to" string.
288 // ARM and ARM64 implementation in ../arm/strncpy.c
299 if ((*s1
++ = *s2
++) == '\0')
300 for (i
++; i
< n
; i
++)
310 * This function converts an ascii string into an integer.
321 for (number
= 0; ('0' <= *cp
) && (*cp
<= '9'); cp
++)
322 number
= (number
* 10) + (*cp
- '0');
328 * convert an ASCII string (decimal radix) to an integer
331 * t char **, return a pointer to the cahr which terminates the
334 * integer value of the numeric string.
336 * pointer to terminating char.
361 while(*p
>= '0' && *p
<= '9')
362 n
= n
*10 + *p
++ - '0';
364 /* return pointer to terminating character */
372 * Does the same thing as strlen, except only looks up
373 * to max chars inside the buffer.
374 * Taken from archive/kern-stuff/sbf_machine.c in
377 * s string whose length is to be measured
378 * max maximum length of string to search for null
380 * length of s or max; whichever is smaller
383 // ARM implementation in ../arm/strnlen.s
384 // ARM64 implementation in ../arm64/strnlen.s
386 strnlen(const char *s
, size_t max
) {
387 const char *es
= s
+ max
, *p
= s
;
395 * convert an integer to an ASCII string.
397 * num integer to be converted
398 * str string pointer.
401 * pointer to string start.
419 *dp
++ = '0' + num
% 10;
422 while (dp
!= digits
) {
432 * Deprecation Warning:
433 * strcat() is being deprecated. Please use strlcat() instead.
444 while ((*dest
++ = *src
++))
450 * Appends src to string dst of size siz (unlike strncat, siz is the
451 * full size of dst, not space left). At most siz-1 characters
452 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
453 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
454 * If retval >= siz, truncation occurred.
457 strlcat(char *dst
, const char *src
, size_t siz
)
464 /* Find the end of dst and adjust bytes left but don't go past end */
465 while (n
-- != 0 && *d
!= '\0')
471 return(dlen
+ strlen(s
));
481 return(dlen
+ (s
- src
)); /* count does not include NUL */
485 * Copy src to string dst of size siz. At most siz-1 characters
486 * will be copied. Always NUL terminates (unless siz == 0).
487 * Returns strlen(src); if retval >= siz, truncation occurred.
490 // ARM and ARM64 implementation in ../arm/strlcpy.c
492 strlcpy(char *dst
, const char *src
, size_t siz
)
498 /* Copy as many bytes as will fit */
499 if (n
!= 0 && --n
!= 0) {
501 if ((*d
++ = *s
++) == 0)
506 /* Not enough room in dst, add NUL and traverse rest of src */
509 *d
= '\0'; /* NUL-terminate dst */
514 return(s
- src
- 1); /* count does not include NUL */
520 * Description: The STRDUP function allocates sufficient memory for a copy
521 * of the string "string", does the copy, and returns a pointer
522 * it. The pointer may subsequently be used as an argument to
525 * Parameters: string String to be duplicated
526 * type type of memory to be allocated (normally
529 * Returns: char * A pointer to the newly allocated string with
530 * duplicated contents in it.
532 * NULL If MALLOC() fails.
534 * Note: This function can *not* be called from interrupt context as
535 * it calls MALLOC with M_WAITOK. In fact, you really
536 * shouldn't be doing string manipulation in interrupt context
539 * This function name violates the kernel style(9) guide
540 * by being all caps. This was done on purpose to emphasize
541 * one should use FREE() with the allocated buffer.
545 STRDUP(const char *string
, int type
)
550 len
= strlen(string
) + 1;
551 MALLOC(copy
, char *, len
, type
, M_WAITOK
);
554 bcopy(string
, copy
, len
);
559 * Return TRUE(1) if string 2 is a prefix of string 1.
562 strprefix(const char *s1
, const char *s2
)
566 while ((c
= *s2
++) != '\0') {
574 strnstr(char *s
, const char *find
, size_t slen
)
579 if ((c
= *find
++) != '\0') {
583 if ((sc
= *s
++) == '\0' || slen
-- < 1)
588 } while (strncmp(s
, find
, len
) != 0);