]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 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. | |
8f6c56a5 | 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. | |
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 | |
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. | |
8f6c56a5 | 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 | |
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 | */ | |
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 */ | |
142 | #ifdef strcpy | |
143 | #undef strcmp | |
144 | #undef strncmp | |
145 | #undef strcpy | |
146 | #undef strncpy | |
147 | #undef strlen | |
148 | #endif | |
149 | ||
150 | /* | |
151 | * Abstract: | |
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. | |
2d21ac55 A |
160 | * Deprecation Warning: |
161 | * strcmp() is being deprecated. Please use strncmp() instead. | |
1c79356b A |
162 | */ |
163 | ||
164 | int | |
165 | strcmp( | |
2d21ac55 A |
166 | const char *s1, |
167 | const char *s2) | |
1c79356b | 168 | { |
2d21ac55 | 169 | unsigned int a, b; |
1c79356b A |
170 | |
171 | do { | |
172 | a = *s1++; | |
173 | b = *s2++; | |
174 | if (a != b) | |
175 | return a-b; /* includes case when | |
176 | 'a' is zero and 'b' is not zero | |
177 | or vice versa */ | |
178 | } while (a != '\0'); | |
179 | ||
180 | return 0; /* both are zero */ | |
181 | } | |
182 | ||
183 | /* | |
184 | * Abstract: | |
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. | |
188 | */ | |
189 | ||
316670eb | 190 | // ARM implementation in ../arm/strncmp.s |
1c79356b A |
191 | int |
192 | strncmp( | |
2d21ac55 A |
193 | const char *s1, |
194 | const char *s2, | |
1c79356b A |
195 | size_t n) |
196 | { | |
2d21ac55 | 197 | unsigned int a, b; |
1c79356b A |
198 | |
199 | while (n != 0) { | |
200 | a = *s1++; | |
201 | b = *s2++; | |
202 | if (a != b) | |
203 | return a-b; /* includes case when | |
204 | 'a' is zero and 'b' is not zero | |
205 | or vice versa */ | |
206 | if (a == '\0') | |
207 | return 0; /* both are zero */ | |
208 | n--; | |
209 | } | |
210 | ||
211 | return 0; | |
212 | } | |
213 | ||
91447636 A |
214 | |
215 | // | |
216 | // Lame implementation just for use by strcasecmp/strncasecmp | |
217 | // | |
218 | static int | |
219 | tolower(unsigned char ch) | |
220 | { | |
221 | if (ch >= 'A' && ch <= 'Z') | |
222 | ch = 'a' + (ch - 'A'); | |
223 | ||
224 | return ch; | |
225 | } | |
226 | ||
227 | int | |
228 | strcasecmp(const char *s1, const char *s2) | |
229 | { | |
230 | const unsigned char *us1 = (const u_char *)s1, | |
231 | *us2 = (const u_char *)s2; | |
232 | ||
233 | while (tolower(*us1) == tolower(*us2++)) | |
234 | if (*us1++ == '\0') | |
235 | return (0); | |
236 | return (tolower(*us1) - tolower(*--us2)); | |
237 | } | |
238 | ||
239 | int | |
240 | strncasecmp(const char *s1, const char *s2, size_t n) | |
241 | { | |
242 | if (n != 0) { | |
243 | const unsigned char *us1 = (const u_char *)s1, | |
244 | *us2 = (const u_char *)s2; | |
245 | ||
246 | do { | |
247 | if (tolower(*us1) != tolower(*us2++)) | |
248 | return (tolower(*us1) - tolower(*--us2)); | |
249 | if (*us1++ == '\0') | |
250 | break; | |
251 | } while (--n != 0); | |
252 | } | |
253 | return (0); | |
254 | } | |
255 | ||
256 | ||
1c79356b A |
257 | /* |
258 | * Abstract: | |
259 | * strcpy copies the contents of the string "from" including | |
260 | * the null terminator to the string "to". A pointer to "to" | |
261 | * is returned. | |
2d21ac55 A |
262 | * Deprecation Warning: |
263 | * strcpy() is being deprecated. Please use strlcpy() instead. | |
1c79356b | 264 | */ |
6d2010ae | 265 | #if !CONFIG_EMBEDDED |
1c79356b A |
266 | char * |
267 | strcpy( | |
2d21ac55 A |
268 | char *to, |
269 | const char *from) | |
1c79356b | 270 | { |
2d21ac55 | 271 | char *ret = to; |
1c79356b A |
272 | |
273 | while ((*to++ = *from++) != '\0') | |
274 | continue; | |
275 | ||
276 | return ret; | |
277 | } | |
6d2010ae | 278 | #endif |
1c79356b A |
279 | |
280 | /* | |
281 | * Abstract: | |
282 | * strncpy copies "count" characters from the "from" string to | |
283 | * the "to" string. If "from" contains less than "count" characters | |
284 | * "to" will be padded with null characters until exactly "count" | |
285 | * characters have been written. The return value is a pointer | |
286 | * to the "to" string. | |
287 | */ | |
288 | ||
316670eb | 289 | // ARM implementation in ../arm/strncpy.s |
1c79356b A |
290 | char * |
291 | strncpy( | |
292 | char *s1, | |
293 | const char *s2, | |
294 | size_t n) | |
295 | { | |
296 | char *os1 = s1; | |
297 | unsigned long i; | |
298 | ||
299 | for (i = 0; i < n;) | |
300 | if ((*s1++ = *s2++) == '\0') | |
301 | for (i++; i < n; i++) | |
302 | *s1++ = '\0'; | |
303 | else | |
304 | i++; | |
305 | return (os1); | |
306 | } | |
307 | ||
1c79356b A |
308 | /* |
309 | * atoi: | |
310 | * | |
311 | * This function converts an ascii string into an integer. | |
312 | * | |
313 | * input : string | |
314 | * output : a number | |
315 | */ | |
316 | ||
317 | int | |
2d21ac55 | 318 | atoi(const char *cp) |
1c79356b A |
319 | { |
320 | int number; | |
321 | ||
322 | for (number = 0; ('0' <= *cp) && (*cp <= '9'); cp++) | |
323 | number = (number * 10) + (*cp - '0'); | |
324 | ||
325 | return( number ); | |
326 | } | |
327 | ||
328 | /* | |
329 | * convert an ASCII string (decimal radix) to an integer | |
330 | * inputs: | |
331 | * p string pointer. | |
332 | * t char **, return a pointer to the cahr which terminates the | |
333 | * numeric string. | |
334 | * returns: | |
335 | * integer value of the numeric string. | |
336 | * side effect: | |
337 | * pointer to terminating char. | |
338 | */ | |
339 | ||
340 | int | |
341 | atoi_term( | |
342 | char *p, /* IN */ | |
343 | char **t) /* OUT */ | |
344 | { | |
2d21ac55 A |
345 | int n; |
346 | int f; | |
1c79356b A |
347 | |
348 | n = 0; | |
349 | f = 0; | |
350 | for(;;p++) { | |
351 | switch(*p) { | |
352 | case ' ': | |
353 | case '\t': | |
354 | continue; | |
355 | case '-': | |
356 | f++; | |
357 | case '+': | |
358 | p++; | |
359 | } | |
360 | break; | |
361 | } | |
362 | while(*p >= '0' && *p <= '9') | |
363 | n = n*10 + *p++ - '0'; | |
364 | ||
365 | /* return pointer to terminating character */ | |
366 | if ( t ) | |
367 | *t = p; | |
368 | ||
369 | return(f? -n: n); | |
370 | } | |
371 | ||
6601e61a A |
372 | /* |
373 | * Does the same thing as strlen, except only looks up | |
374 | * to max chars inside the buffer. | |
375 | * Taken from archive/kern-stuff/sbf_machine.c in | |
376 | * seatbelt. | |
377 | * inputs: | |
2d21ac55 A |
378 | * s string whose length is to be measured |
379 | * max maximum length of string to search for null | |
6601e61a | 380 | * outputs: |
2d21ac55 | 381 | * length of s or max; whichever is smaller |
6601e61a | 382 | */ |
316670eb A |
383 | |
384 | // ARM implementation in ../arm/strnlen.s | |
6601e61a A |
385 | size_t |
386 | strnlen(const char *s, size_t max) { | |
387 | const char *es = s + max, *p = s; | |
388 | while(*p && p != es) | |
389 | p++; | |
390 | ||
391 | return p - s; | |
392 | } | |
393 | ||
1c79356b A |
394 | /* |
395 | * convert an integer to an ASCII string. | |
396 | * inputs: | |
397 | * num integer to be converted | |
398 | * str string pointer. | |
399 | * | |
400 | * outputs: | |
401 | * pointer to string start. | |
402 | */ | |
403 | ||
404 | char * | |
405 | itoa( | |
406 | int num, | |
407 | char *str) | |
408 | { | |
409 | char digits[11]; | |
2d21ac55 A |
410 | char *dp; |
411 | char *cp = str; | |
1c79356b A |
412 | |
413 | if (num == 0) { | |
414 | *cp++ = '0'; | |
415 | } | |
416 | else { | |
417 | dp = digits; | |
418 | while (num) { | |
419 | *dp++ = '0' + num % 10; | |
420 | num /= 10; | |
421 | } | |
422 | while (dp != digits) { | |
423 | *cp++ = *--dp; | |
424 | } | |
425 | } | |
426 | *cp++ = '\0'; | |
427 | ||
428 | return str; | |
429 | } | |
430 | ||
2d21ac55 A |
431 | /* |
432 | * Deprecation Warning: | |
433 | * strcat() is being deprecated. Please use strlcat() instead. | |
434 | */ | |
6d2010ae | 435 | #if !CONFIG_EMBEDDED |
1c79356b A |
436 | char * |
437 | strcat( | |
2d21ac55 A |
438 | char *dest, |
439 | const char *src) | |
1c79356b A |
440 | { |
441 | char *old = dest; | |
442 | ||
443 | while (*dest) | |
444 | ++dest; | |
2d21ac55 | 445 | while ((*dest++ = *src++)) |
1c79356b A |
446 | ; |
447 | return (old); | |
448 | } | |
6d2010ae | 449 | #endif |
1c79356b | 450 | |
2d21ac55 A |
451 | /* |
452 | * Appends src to string dst of size siz (unlike strncat, siz is the | |
453 | * full size of dst, not space left). At most siz-1 characters | |
454 | * will be copied. Always NUL terminates (unless siz <= strlen(dst)). | |
455 | * Returns strlen(src) + MIN(siz, strlen(initial dst)). | |
456 | * If retval >= siz, truncation occurred. | |
457 | */ | |
458 | size_t | |
459 | strlcat(char *dst, const char *src, size_t siz) | |
460 | { | |
461 | char *d = dst; | |
462 | const char *s = src; | |
463 | size_t n = siz; | |
464 | size_t dlen; | |
465 | ||
466 | /* Find the end of dst and adjust bytes left but don't go past end */ | |
467 | while (n-- != 0 && *d != '\0') | |
468 | d++; | |
469 | dlen = d - dst; | |
470 | n = siz - dlen; | |
471 | ||
472 | if (n == 0) | |
473 | return(dlen + strlen(s)); | |
474 | while (*s != '\0') { | |
475 | if (n != 1) { | |
476 | *d++ = *s; | |
477 | n--; | |
478 | } | |
479 | s++; | |
480 | } | |
481 | *d = '\0'; | |
482 | ||
483 | return(dlen + (s - src)); /* count does not include NUL */ | |
484 | } | |
485 | ||
486 | /* | |
487 | * Copy src to string dst of size siz. At most siz-1 characters | |
488 | * will be copied. Always NUL terminates (unless siz == 0). | |
489 | * Returns strlen(src); if retval >= siz, truncation occurred. | |
490 | */ | |
316670eb A |
491 | |
492 | // ARM implementation in ../arm/strlcpy.s | |
2d21ac55 A |
493 | size_t |
494 | strlcpy(char *dst, const char *src, size_t siz) | |
495 | { | |
496 | char *d = dst; | |
497 | const char *s = src; | |
498 | size_t n = siz; | |
499 | ||
500 | /* Copy as many bytes as will fit */ | |
501 | if (n != 0 && --n != 0) { | |
502 | do { | |
503 | if ((*d++ = *s++) == 0) | |
504 | break; | |
505 | } while (--n != 0); | |
506 | } | |
507 | ||
508 | /* Not enough room in dst, add NUL and traverse rest of src */ | |
509 | if (n == 0) { | |
510 | if (siz != 0) | |
511 | *d = '\0'; /* NUL-terminate dst */ | |
512 | while (*s++) | |
513 | ; | |
514 | } | |
515 | ||
516 | return(s - src - 1); /* count does not include NUL */ | |
517 | } | |
518 | ||
519 | /* | |
520 | * STRDUP | |
521 | * | |
522 | * Description: The STRDUP function allocates sufficient memory for a copy | |
523 | * of the string "string", does the copy, and returns a pointer | |
524 | * it. The pointer may subsequently be used as an argument to | |
525 | * the macro FREE(). | |
526 | * | |
527 | * Parameters: string String to be duplicated | |
528 | * type type of memory to be allocated (normally | |
529 | * M_TEMP) | |
530 | * | |
531 | * Returns: char * A pointer to the newly allocated string with | |
532 | * duplicated contents in it. | |
533 | * | |
534 | * NULL If MALLOC() fails. | |
535 | * | |
536 | * Note: This function can *not* be called from interrupt context as | |
537 | * it calls MALLOC with M_WAITOK. In fact, you really | |
538 | * shouldn't be doing string manipulation in interrupt context | |
539 | * ever. | |
540 | * | |
541 | * This function name violates the kernel style(9) guide | |
542 | * by being all caps. This was done on purpose to emphasize | |
543 | * one should use FREE() with the allocated buffer. | |
544 | * | |
545 | */ | |
6d2010ae | 546 | char * |
2d21ac55 A |
547 | STRDUP(const char *string, int type) |
548 | { | |
549 | size_t len; | |
550 | char *copy; | |
551 | ||
552 | len = strlen(string) + 1; | |
553 | MALLOC(copy, char *, len, type, M_WAITOK); | |
554 | if (copy == NULL) | |
555 | return (NULL); | |
556 | bcopy(string, copy, len); | |
557 | return (copy); | |
558 | } | |
559 | ||
560 | /* | |
561 | * Return TRUE(1) if string 2 is a prefix of string 1. | |
562 | */ | |
563 | int | |
564 | strprefix(register const char *s1, register const char *s2) | |
565 | { | |
566 | register int c; | |
567 | ||
568 | while ((c = *s2++) != '\0') { | |
569 | if (c != *s1++) | |
570 | return (0); | |
571 | } | |
572 | return (1); | |
573 | } |