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