wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * linux/lib/string.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * stupid library routines.. The optimized versions should generally be found |
| 9 | * as inline code in <asm-xx/string.h> |
| 10 | * |
| 11 | * These are buggy as well.. |
| 12 | * |
| 13 | * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de> |
| 14 | * - Added strsep() which will replace strtok() soon (because strsep() is |
| 15 | * reentrant and should be faster). Use only strsep() in new code, please. |
| 16 | */ |
| 17 | |
Simon Glass | 43e571d | 2018-11-06 15:21:38 -0700 | [diff] [blame] | 18 | #include <config.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 19 | #include <linux/types.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/ctype.h> |
| 22 | #include <malloc.h> |
| 23 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 24 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 25 | /** |
Simon Glass | 459af69 | 2012-12-05 14:46:35 +0000 | [diff] [blame] | 26 | * strncasecmp - Case insensitive, length-limited string comparison |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 27 | * @s1: One string |
| 28 | * @s2: The other string |
| 29 | * @len: the maximum number of characters to compare |
| 30 | */ |
Simon Glass | 459af69 | 2012-12-05 14:46:35 +0000 | [diff] [blame] | 31 | int strncasecmp(const char *s1, const char *s2, size_t len) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 32 | { |
| 33 | /* Yes, Virginia, it had better be unsigned */ |
| 34 | unsigned char c1, c2; |
| 35 | |
| 36 | c1 = 0; c2 = 0; |
| 37 | if (len) { |
| 38 | do { |
| 39 | c1 = *s1; c2 = *s2; |
| 40 | s1++; s2++; |
| 41 | if (!c1) |
| 42 | break; |
| 43 | if (!c2) |
| 44 | break; |
| 45 | if (c1 == c2) |
| 46 | continue; |
| 47 | c1 = tolower(c1); |
| 48 | c2 = tolower(c2); |
| 49 | if (c1 != c2) |
| 50 | break; |
| 51 | } while (--len); |
| 52 | } |
| 53 | return (int)c1 - (int)c2; |
| 54 | } |
Simon Glass | 459af69 | 2012-12-05 14:46:35 +0000 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * strcasecmp - Case insensitive string comparison |
| 58 | * @s1: One string |
| 59 | * @s2: The other string |
| 60 | */ |
| 61 | int strcasecmp(const char *s1, const char *s2) |
| 62 | { |
| 63 | return strncasecmp(s1, s2, -1U); |
| 64 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 65 | |
| 66 | char * ___strtok; |
| 67 | |
| 68 | #ifndef __HAVE_ARCH_STRCPY |
| 69 | /** |
| 70 | * strcpy - Copy a %NUL terminated string |
| 71 | * @dest: Where to copy the string to |
| 72 | * @src: Where to copy the string from |
| 73 | */ |
| 74 | char * strcpy(char * dest,const char *src) |
| 75 | { |
| 76 | char *tmp = dest; |
| 77 | |
| 78 | while ((*dest++ = *src++) != '\0') |
| 79 | /* nothing */; |
| 80 | return tmp; |
| 81 | } |
| 82 | #endif |
| 83 | |
| 84 | #ifndef __HAVE_ARCH_STRNCPY |
| 85 | /** |
| 86 | * strncpy - Copy a length-limited, %NUL-terminated string |
| 87 | * @dest: Where to copy the string to |
| 88 | * @src: Where to copy the string from |
| 89 | * @count: The maximum number of bytes to copy |
| 90 | * |
| 91 | * Note that unlike userspace strncpy, this does not %NUL-pad the buffer. |
| 92 | * However, the result is not %NUL-terminated if the source exceeds |
| 93 | * @count bytes. |
| 94 | */ |
| 95 | char * strncpy(char * dest,const char *src,size_t count) |
| 96 | { |
| 97 | char *tmp = dest; |
| 98 | |
| 99 | while (count-- && (*dest++ = *src++) != '\0') |
| 100 | /* nothing */; |
| 101 | |
| 102 | return tmp; |
| 103 | } |
| 104 | #endif |
| 105 | |
Masahiro Yamada | 0588ce1 | 2014-11-20 21:20:32 +0900 | [diff] [blame] | 106 | #ifndef __HAVE_ARCH_STRLCPY |
| 107 | /** |
| 108 | * strlcpy - Copy a C-string into a sized buffer |
| 109 | * @dest: Where to copy the string to |
| 110 | * @src: Where to copy the string from |
| 111 | * @size: size of destination buffer |
| 112 | * |
| 113 | * Compatible with *BSD: the result is always a valid |
| 114 | * NUL-terminated string that fits in the buffer (unless, |
| 115 | * of course, the buffer size is zero). It does not pad |
| 116 | * out the result like strncpy() does. |
| 117 | */ |
| 118 | size_t strlcpy(char *dest, const char *src, size_t size) |
| 119 | { |
| 120 | size_t ret = strlen(src); |
| 121 | |
| 122 | if (size) { |
| 123 | size_t len = (ret >= size) ? size - 1 : ret; |
| 124 | memcpy(dest, src, len); |
| 125 | dest[len] = '\0'; |
| 126 | } |
| 127 | return ret; |
| 128 | } |
| 129 | #endif |
| 130 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 131 | #ifndef __HAVE_ARCH_STRCAT |
| 132 | /** |
| 133 | * strcat - Append one %NUL-terminated string to another |
| 134 | * @dest: The string to be appended to |
| 135 | * @src: The string to append to it |
| 136 | */ |
| 137 | char * strcat(char * dest, const char * src) |
| 138 | { |
| 139 | char *tmp = dest; |
| 140 | |
| 141 | while (*dest) |
| 142 | dest++; |
| 143 | while ((*dest++ = *src++) != '\0') |
| 144 | ; |
| 145 | |
| 146 | return tmp; |
| 147 | } |
| 148 | #endif |
| 149 | |
| 150 | #ifndef __HAVE_ARCH_STRNCAT |
| 151 | /** |
| 152 | * strncat - Append a length-limited, %NUL-terminated string to another |
| 153 | * @dest: The string to be appended to |
| 154 | * @src: The string to append to it |
| 155 | * @count: The maximum numbers of bytes to copy |
| 156 | * |
| 157 | * Note that in contrast to strncpy, strncat ensures the result is |
| 158 | * terminated. |
| 159 | */ |
| 160 | char * strncat(char *dest, const char *src, size_t count) |
| 161 | { |
| 162 | char *tmp = dest; |
| 163 | |
| 164 | if (count) { |
| 165 | while (*dest) |
| 166 | dest++; |
| 167 | while ((*dest++ = *src++)) { |
| 168 | if (--count == 0) { |
| 169 | *dest = '\0'; |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return tmp; |
| 176 | } |
| 177 | #endif |
| 178 | |
| 179 | #ifndef __HAVE_ARCH_STRCMP |
| 180 | /** |
| 181 | * strcmp - Compare two strings |
| 182 | * @cs: One string |
| 183 | * @ct: Another string |
| 184 | */ |
| 185 | int strcmp(const char * cs,const char * ct) |
| 186 | { |
| 187 | register signed char __res; |
| 188 | |
| 189 | while (1) { |
| 190 | if ((__res = *cs - *ct++) != 0 || !*cs++) |
| 191 | break; |
| 192 | } |
| 193 | |
| 194 | return __res; |
| 195 | } |
| 196 | #endif |
| 197 | |
| 198 | #ifndef __HAVE_ARCH_STRNCMP |
| 199 | /** |
| 200 | * strncmp - Compare two length-limited strings |
| 201 | * @cs: One string |
| 202 | * @ct: Another string |
| 203 | * @count: The maximum number of bytes to compare |
| 204 | */ |
| 205 | int strncmp(const char * cs,const char * ct,size_t count) |
| 206 | { |
| 207 | register signed char __res = 0; |
| 208 | |
| 209 | while (count) { |
| 210 | if ((__res = *cs - *ct++) != 0 || !*cs++) |
| 211 | break; |
| 212 | count--; |
| 213 | } |
| 214 | |
| 215 | return __res; |
| 216 | } |
| 217 | #endif |
| 218 | |
| 219 | #ifndef __HAVE_ARCH_STRCHR |
| 220 | /** |
| 221 | * strchr - Find the first occurrence of a character in a string |
| 222 | * @s: The string to be searched |
| 223 | * @c: The character to search for |
| 224 | */ |
| 225 | char * strchr(const char * s, int c) |
| 226 | { |
| 227 | for(; *s != (char) c; ++s) |
| 228 | if (*s == '\0') |
| 229 | return NULL; |
| 230 | return (char *) s; |
| 231 | } |
| 232 | #endif |
| 233 | |
Simon Glass | 4355d42 | 2017-05-18 20:09:28 -0600 | [diff] [blame] | 234 | const char *strchrnul(const char *s, int c) |
| 235 | { |
| 236 | for (; *s != (char)c; ++s) |
| 237 | if (*s == '\0') |
| 238 | break; |
| 239 | return s; |
| 240 | } |
| 241 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 242 | #ifndef __HAVE_ARCH_STRRCHR |
| 243 | /** |
| 244 | * strrchr - Find the last occurrence of a character in a string |
| 245 | * @s: The string to be searched |
| 246 | * @c: The character to search for |
| 247 | */ |
| 248 | char * strrchr(const char * s, int c) |
| 249 | { |
| 250 | const char *p = s + strlen(s); |
| 251 | do { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 252 | if (*p == (char)c) |
| 253 | return (char *)p; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 254 | } while (--p >= s); |
| 255 | return NULL; |
| 256 | } |
| 257 | #endif |
| 258 | |
| 259 | #ifndef __HAVE_ARCH_STRLEN |
| 260 | /** |
| 261 | * strlen - Find the length of a string |
| 262 | * @s: The string to be sized |
| 263 | */ |
| 264 | size_t strlen(const char * s) |
| 265 | { |
| 266 | const char *sc; |
| 267 | |
| 268 | for (sc = s; *sc != '\0'; ++sc) |
| 269 | /* nothing */; |
| 270 | return sc - s; |
| 271 | } |
| 272 | #endif |
| 273 | |
| 274 | #ifndef __HAVE_ARCH_STRNLEN |
| 275 | /** |
| 276 | * strnlen - Find the length of a length-limited string |
| 277 | * @s: The string to be sized |
| 278 | * @count: The maximum number of bytes to search |
| 279 | */ |
| 280 | size_t strnlen(const char * s, size_t count) |
| 281 | { |
| 282 | const char *sc; |
| 283 | |
| 284 | for (sc = s; count-- && *sc != '\0'; ++sc) |
| 285 | /* nothing */; |
| 286 | return sc - s; |
| 287 | } |
| 288 | #endif |
| 289 | |
Simon Glass | 0d0e3c9 | 2017-05-18 20:09:29 -0600 | [diff] [blame] | 290 | #ifndef __HAVE_ARCH_STRCSPN |
| 291 | /** |
| 292 | * strcspn - Calculate the length of the initial substring of @s which does |
| 293 | * not contain letters in @reject |
| 294 | * @s: The string to be searched |
| 295 | * @reject: The string to avoid |
| 296 | */ |
| 297 | size_t strcspn(const char *s, const char *reject) |
| 298 | { |
| 299 | const char *p; |
| 300 | const char *r; |
| 301 | size_t count = 0; |
| 302 | |
| 303 | for (p = s; *p != '\0'; ++p) { |
| 304 | for (r = reject; *r != '\0'; ++r) { |
| 305 | if (*p == *r) |
| 306 | return count; |
| 307 | } |
| 308 | ++count; |
| 309 | } |
| 310 | return count; |
| 311 | } |
| 312 | #endif |
| 313 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 314 | #ifndef __HAVE_ARCH_STRDUP |
| 315 | char * strdup(const char *s) |
| 316 | { |
| 317 | char *new; |
| 318 | |
| 319 | if ((s == NULL) || |
| 320 | ((new = malloc (strlen(s) + 1)) == NULL) ) { |
| 321 | return NULL; |
| 322 | } |
| 323 | |
| 324 | strcpy (new, s); |
| 325 | return new; |
| 326 | } |
| 327 | #endif |
| 328 | |
Thierry Reding | f056182 | 2019-04-15 11:32:14 +0200 | [diff] [blame] | 329 | char * strndup(const char *s, size_t n) |
| 330 | { |
| 331 | size_t len; |
| 332 | char *new; |
| 333 | |
| 334 | if (s == NULL) |
| 335 | return NULL; |
| 336 | |
| 337 | len = strlen(s); |
| 338 | |
| 339 | if (n < len) |
| 340 | len = n; |
| 341 | |
| 342 | new = malloc(len + 1); |
| 343 | if (new == NULL) |
| 344 | return NULL; |
| 345 | |
| 346 | strncpy(new, s, len); |
| 347 | new[len] = '\0'; |
| 348 | |
| 349 | return new; |
| 350 | } |
| 351 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 352 | #ifndef __HAVE_ARCH_STRSPN |
| 353 | /** |
| 354 | * strspn - Calculate the length of the initial substring of @s which only |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 355 | * contain letters in @accept |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 356 | * @s: The string to be searched |
| 357 | * @accept: The string to search for |
| 358 | */ |
| 359 | size_t strspn(const char *s, const char *accept) |
| 360 | { |
| 361 | const char *p; |
| 362 | const char *a; |
| 363 | size_t count = 0; |
| 364 | |
| 365 | for (p = s; *p != '\0'; ++p) { |
| 366 | for (a = accept; *a != '\0'; ++a) { |
| 367 | if (*p == *a) |
| 368 | break; |
| 369 | } |
| 370 | if (*a == '\0') |
| 371 | return count; |
| 372 | ++count; |
| 373 | } |
| 374 | |
| 375 | return count; |
| 376 | } |
| 377 | #endif |
| 378 | |
| 379 | #ifndef __HAVE_ARCH_STRPBRK |
| 380 | /** |
| 381 | * strpbrk - Find the first occurrence of a set of characters |
| 382 | * @cs: The string to be searched |
| 383 | * @ct: The characters to search for |
| 384 | */ |
| 385 | char * strpbrk(const char * cs,const char * ct) |
| 386 | { |
| 387 | const char *sc1,*sc2; |
| 388 | |
| 389 | for( sc1 = cs; *sc1 != '\0'; ++sc1) { |
| 390 | for( sc2 = ct; *sc2 != '\0'; ++sc2) { |
| 391 | if (*sc1 == *sc2) |
| 392 | return (char *) sc1; |
| 393 | } |
| 394 | } |
| 395 | return NULL; |
| 396 | } |
| 397 | #endif |
| 398 | |
| 399 | #ifndef __HAVE_ARCH_STRTOK |
| 400 | /** |
| 401 | * strtok - Split a string into tokens |
| 402 | * @s: The string to be searched |
| 403 | * @ct: The characters to search for |
| 404 | * |
| 405 | * WARNING: strtok is deprecated, use strsep instead. |
| 406 | */ |
| 407 | char * strtok(char * s,const char * ct) |
| 408 | { |
| 409 | char *sbegin, *send; |
| 410 | |
| 411 | sbegin = s ? s : ___strtok; |
| 412 | if (!sbegin) { |
| 413 | return NULL; |
| 414 | } |
| 415 | sbegin += strspn(sbegin,ct); |
| 416 | if (*sbegin == '\0') { |
| 417 | ___strtok = NULL; |
| 418 | return( NULL ); |
| 419 | } |
| 420 | send = strpbrk( sbegin, ct); |
| 421 | if (send && *send != '\0') |
| 422 | *send++ = '\0'; |
| 423 | ___strtok = send; |
| 424 | return (sbegin); |
| 425 | } |
| 426 | #endif |
| 427 | |
| 428 | #ifndef __HAVE_ARCH_STRSEP |
| 429 | /** |
| 430 | * strsep - Split a string into tokens |
| 431 | * @s: The string to be searched |
| 432 | * @ct: The characters to search for |
| 433 | * |
| 434 | * strsep() updates @s to point after the token, ready for the next call. |
| 435 | * |
| 436 | * It returns empty tokens, too, behaving exactly like the libc function |
| 437 | * of that name. In fact, it was stolen from glibc2 and de-fancy-fied. |
| 438 | * Same semantics, slimmer shape. ;) |
| 439 | */ |
| 440 | char * strsep(char **s, const char *ct) |
| 441 | { |
| 442 | char *sbegin = *s, *end; |
| 443 | |
| 444 | if (sbegin == NULL) |
| 445 | return NULL; |
| 446 | |
| 447 | end = strpbrk(sbegin, ct); |
| 448 | if (end) |
| 449 | *end++ = '\0'; |
| 450 | *s = end; |
| 451 | |
| 452 | return sbegin; |
| 453 | } |
| 454 | #endif |
| 455 | |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 456 | #ifndef __HAVE_ARCH_STRSWAB |
| 457 | /** |
| 458 | * strswab - swap adjacent even and odd bytes in %NUL-terminated string |
| 459 | * s: address of the string |
| 460 | * |
| 461 | * returns the address of the swapped string or NULL on error. If |
| 462 | * string length is odd, last byte is untouched. |
| 463 | */ |
| 464 | char *strswab(const char *s) |
| 465 | { |
Wolfgang Denk | c7ca7cf | 2005-09-25 16:15:17 +0200 | [diff] [blame] | 466 | char *p, *q; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 467 | |
| 468 | if ((NULL == s) || ('\0' == *s)) { |
| 469 | return (NULL); |
| 470 | } |
| 471 | |
Wolfgang Denk | df70a42 | 2005-10-04 23:38:07 +0200 | [diff] [blame] | 472 | for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) { |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 473 | char tmp; |
Wolfgang Denk | c7ca7cf | 2005-09-25 16:15:17 +0200 | [diff] [blame] | 474 | |
| 475 | tmp = *p; |
| 476 | *p = *q; |
| 477 | *q = tmp; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | return (char *) s; |
| 481 | } |
| 482 | #endif |
| 483 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 484 | #ifndef __HAVE_ARCH_MEMSET |
| 485 | /** |
| 486 | * memset - Fill a region of memory with the given value |
| 487 | * @s: Pointer to the start of the area. |
| 488 | * @c: The byte to fill the area with |
| 489 | * @count: The size of the area. |
| 490 | * |
| 491 | * Do not use memset() to access IO space, use memset_io() instead. |
| 492 | */ |
| 493 | void * memset(void * s,int c,size_t count) |
| 494 | { |
Alessandro Rubini | fe1a2fb | 2009-10-10 11:51:16 +0200 | [diff] [blame] | 495 | unsigned long *sl = (unsigned long *) s; |
Alessandro Rubini | fe1a2fb | 2009-10-10 11:51:16 +0200 | [diff] [blame] | 496 | char *s8; |
Simon Glass | e2f94ae | 2017-04-02 09:50:28 -0600 | [diff] [blame] | 497 | |
| 498 | #if !CONFIG_IS_ENABLED(TINY_MEMSET) |
| 499 | unsigned long cl = 0; |
Alessandro Rubini | fe1a2fb | 2009-10-10 11:51:16 +0200 | [diff] [blame] | 500 | int i; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 501 | |
Alessandro Rubini | fe1a2fb | 2009-10-10 11:51:16 +0200 | [diff] [blame] | 502 | /* do it one word at a time (32 bits or 64 bits) while possible */ |
| 503 | if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) { |
| 504 | for (i = 0; i < sizeof(*sl); i++) { |
| 505 | cl <<= 8; |
| 506 | cl |= c & 0xff; |
| 507 | } |
| 508 | while (count >= sizeof(*sl)) { |
| 509 | *sl++ = cl; |
| 510 | count -= sizeof(*sl); |
| 511 | } |
| 512 | } |
Simon Glass | e2f94ae | 2017-04-02 09:50:28 -0600 | [diff] [blame] | 513 | #endif /* fill 8 bits at a time */ |
Alessandro Rubini | fe1a2fb | 2009-10-10 11:51:16 +0200 | [diff] [blame] | 514 | s8 = (char *)sl; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 515 | while (count--) |
Alessandro Rubini | fe1a2fb | 2009-10-10 11:51:16 +0200 | [diff] [blame] | 516 | *s8++ = c; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 517 | |
| 518 | return s; |
| 519 | } |
| 520 | #endif |
| 521 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 522 | #ifndef __HAVE_ARCH_MEMCPY |
| 523 | /** |
| 524 | * memcpy - Copy one area of memory to another |
| 525 | * @dest: Where to copy to |
| 526 | * @src: Where to copy from |
| 527 | * @count: The size of the area. |
| 528 | * |
| 529 | * You should not use this function to access IO space, use memcpy_toio() |
| 530 | * or memcpy_fromio() instead. |
| 531 | */ |
Alessandro Rubini | f881b54 | 2009-10-10 11:51:05 +0200 | [diff] [blame] | 532 | void * memcpy(void *dest, const void *src, size_t count) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 533 | { |
Alessandro Rubini | f881b54 | 2009-10-10 11:51:05 +0200 | [diff] [blame] | 534 | unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src; |
| 535 | char *d8, *s8; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 536 | |
Matthias Weisser | 5bf6348 | 2011-05-22 23:03:55 +0000 | [diff] [blame] | 537 | if (src == dest) |
| 538 | return dest; |
| 539 | |
Alessandro Rubini | f881b54 | 2009-10-10 11:51:05 +0200 | [diff] [blame] | 540 | /* while all data is aligned (common case), copy a word at a time */ |
| 541 | if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) { |
| 542 | while (count >= sizeof(*dl)) { |
| 543 | *dl++ = *sl++; |
| 544 | count -= sizeof(*dl); |
| 545 | } |
| 546 | } |
| 547 | /* copy the reset one byte at a time */ |
| 548 | d8 = (char *)dl; |
| 549 | s8 = (char *)sl; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 550 | while (count--) |
Alessandro Rubini | f881b54 | 2009-10-10 11:51:05 +0200 | [diff] [blame] | 551 | *d8++ = *s8++; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 552 | |
| 553 | return dest; |
| 554 | } |
| 555 | #endif |
| 556 | |
| 557 | #ifndef __HAVE_ARCH_MEMMOVE |
| 558 | /** |
| 559 | * memmove - Copy one area of memory to another |
| 560 | * @dest: Where to copy to |
| 561 | * @src: Where to copy from |
| 562 | * @count: The size of the area. |
| 563 | * |
| 564 | * Unlike memcpy(), memmove() copes with overlapping areas. |
| 565 | */ |
| 566 | void * memmove(void * dest,const void *src,size_t count) |
| 567 | { |
| 568 | char *tmp, *s; |
| 569 | |
| 570 | if (dest <= src) { |
Simon Glass | 03f827e | 2017-04-05 16:23:31 -0600 | [diff] [blame] | 571 | memcpy(dest, src, count); |
| 572 | } else { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 573 | tmp = (char *) dest + count; |
| 574 | s = (char *) src + count; |
| 575 | while (count--) |
| 576 | *--tmp = *--s; |
| 577 | } |
| 578 | |
| 579 | return dest; |
| 580 | } |
| 581 | #endif |
| 582 | |
| 583 | #ifndef __HAVE_ARCH_MEMCMP |
| 584 | /** |
| 585 | * memcmp - Compare two areas of memory |
| 586 | * @cs: One area of memory |
| 587 | * @ct: Another area of memory |
| 588 | * @count: The size of the area. |
| 589 | */ |
| 590 | int memcmp(const void * cs,const void * ct,size_t count) |
| 591 | { |
| 592 | const unsigned char *su1, *su2; |
| 593 | int res = 0; |
| 594 | |
| 595 | for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) |
| 596 | if ((res = *su1 - *su2) != 0) |
| 597 | break; |
| 598 | return res; |
| 599 | } |
| 600 | #endif |
| 601 | |
| 602 | #ifndef __HAVE_ARCH_MEMSCAN |
| 603 | /** |
| 604 | * memscan - Find a character in an area of memory. |
| 605 | * @addr: The memory area |
| 606 | * @c: The byte to search for |
| 607 | * @size: The size of the area. |
| 608 | * |
| 609 | * returns the address of the first occurrence of @c, or 1 byte past |
| 610 | * the area if @c is not found |
| 611 | */ |
| 612 | void * memscan(void * addr, int c, size_t size) |
| 613 | { |
| 614 | unsigned char * p = (unsigned char *) addr; |
| 615 | |
| 616 | while (size) { |
| 617 | if (*p == c) |
| 618 | return (void *) p; |
| 619 | p++; |
| 620 | size--; |
| 621 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 622 | return (void *) p; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 623 | } |
| 624 | #endif |
| 625 | |
| 626 | #ifndef __HAVE_ARCH_STRSTR |
| 627 | /** |
| 628 | * strstr - Find the first substring in a %NUL terminated string |
| 629 | * @s1: The string to be searched |
| 630 | * @s2: The string to search for |
| 631 | */ |
| 632 | char * strstr(const char * s1,const char * s2) |
| 633 | { |
| 634 | int l1, l2; |
| 635 | |
| 636 | l2 = strlen(s2); |
| 637 | if (!l2) |
| 638 | return (char *) s1; |
| 639 | l1 = strlen(s1); |
| 640 | while (l1 >= l2) { |
| 641 | l1--; |
| 642 | if (!memcmp(s1,s2,l2)) |
| 643 | return (char *) s1; |
| 644 | s1++; |
| 645 | } |
| 646 | return NULL; |
| 647 | } |
| 648 | #endif |
| 649 | |
| 650 | #ifndef __HAVE_ARCH_MEMCHR |
| 651 | /** |
| 652 | * memchr - Find a character in an area of memory. |
| 653 | * @s: The memory area |
| 654 | * @c: The byte to search for |
| 655 | * @n: The size of the area. |
| 656 | * |
| 657 | * returns the address of the first occurrence of @c, or %NULL |
| 658 | * if @c is not found |
| 659 | */ |
| 660 | void *memchr(const void *s, int c, size_t n) |
| 661 | { |
| 662 | const unsigned char *p = s; |
| 663 | while (n-- != 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 664 | if ((unsigned char)c == *p++) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 665 | return (void *)(p-1); |
| 666 | } |
| 667 | } |
| 668 | return NULL; |
| 669 | } |
| 670 | |
| 671 | #endif |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 672 | #ifndef __HAVE_ARCH_MEMCHR_INV |
| 673 | static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes) |
| 674 | { |
| 675 | while (bytes) { |
| 676 | if (*start != value) |
| 677 | return (void *)start; |
| 678 | start++; |
| 679 | bytes--; |
| 680 | } |
| 681 | return NULL; |
| 682 | } |
| 683 | /** |
| 684 | * memchr_inv - Find an unmatching character in an area of memory. |
| 685 | * @start: The memory area |
| 686 | * @c: Find a character other than c |
| 687 | * @bytes: The size of the area. |
| 688 | * |
| 689 | * returns the address of the first character other than @c, or %NULL |
| 690 | * if the whole buffer contains just @c. |
| 691 | */ |
| 692 | void *memchr_inv(const void *start, int c, size_t bytes) |
| 693 | { |
| 694 | u8 value = c; |
| 695 | u64 value64; |
| 696 | unsigned int words, prefix; |
| 697 | |
| 698 | if (bytes <= 16) |
| 699 | return check_bytes8(start, value, bytes); |
| 700 | |
| 701 | value64 = value; |
| 702 | value64 |= value64 << 8; |
| 703 | value64 |= value64 << 16; |
| 704 | value64 |= value64 << 32; |
| 705 | |
| 706 | prefix = (unsigned long)start % 8; |
| 707 | if (prefix) { |
| 708 | u8 *r; |
| 709 | |
| 710 | prefix = 8 - prefix; |
| 711 | r = check_bytes8(start, value, prefix); |
| 712 | if (r) |
| 713 | return r; |
| 714 | start += prefix; |
| 715 | bytes -= prefix; |
| 716 | } |
| 717 | |
| 718 | words = bytes / 8; |
| 719 | |
| 720 | while (words) { |
| 721 | if (*(u64 *)start != value64) |
| 722 | return check_bytes8(start, value, 8); |
| 723 | start += 8; |
| 724 | words--; |
| 725 | } |
| 726 | |
| 727 | return check_bytes8(start, value, bytes % 8); |
| 728 | } |
| 729 | #endif |